코딩
raspberry pi 자동차 구현
김염인
2022. 9. 19. 12:13


위의 프로젝트는 국민대학교 '창업연계공학설계입문'에서 진행 한 프로젝트로 위와 같은 환경에서 실시하였습니다.
검은색 라인을 따라 라즈베리 자동차의 라인트레이싱을 한뒤 빨간색을 만나면 멈추고 장애물을 만날시 후진 한뒤 장애물을 넘어 갈 수 있도록 설계 해주었습니다.





창의성 판단
창의성① : 장애물과의 거리 판단
LED센서 와 초음파 센서를 이용하여 장애물과의 거리가 점점 가까워 질 수록 LED모듈의 깜빡이는 속도가 점점 빨라진다. 예를 들어 장애물과 구동체 사이 거리가 30CM 일 때 3초에 한번 깜빡인다면 20CM 일 때 2초에 한번 10cm 일 때 1초에 한번 깜빡이게 실행시킨다.
창의성② : 기존 보다 더욱더 안정적인 턴
라인트레이싱 중앙 주행상태를 center에서 rightless로 바꾸어 턴을 할때 leftmost센서가
기존보다 안쪽에 위치하게하여 턴을 더 매끄럽게 할수있도록 한다.

Main.py Code
from car import Car
from BUZZER.Buzzer import Buzzer
import time
import threading
class myCar(object):
def __init__(self, car_name):
self.car = Car(car_name)
self.buzzer = Buzzer()
def drive_parking(self):
self.car.drive_parking()
self.buzzer.finish = True
# =======================================================================
# 2ND_ASSIGNMENT_CODE
# Complete the code to perform Second Assignment
# =======================================================================
def car_startup(self):
t = threading.Thread(target=self.buzzer.song)
t.start()
INITIAL_SPEED = 30
SPEED = 30
self.car.steering.center_alignment()
self.car.accelerator.go_forward(SPEED)
line_detector = self.car.line_detector
preLine = [0,0,0,0,0]
count = 0
count_not_obs = 0
while count <= 2:
# 신호등 감지
rgb = self.car.color_getter.get_raw_data()
if rgb[0] > 700 and rgb[1] < 400 and rgb[2] < 400:
print("RED DETECTED!!")
self.car.accelerator.stop()
self.buzzer.stop = True
time.sleep(3)
self.car.accelerator.go_forward(SPEED)
time.sleep(0.5)
# 장애물 감지
distance = self.car.distance_detector.get_distance()
if 0 < distance < 30:
time.sleep(0.1)
if not self.car.distance_detector.get_distance() < 30: # 장애물을 재확인
continue
print("Obstacle Detected")
SPEED = 40
self.car.accelerator.go_forward(SPEED)
self.buzzer.speed = 20 / SPEED
self.car.steering.turn(90-35) # 좌회전
while line_detector.is_in_line():
continue
while not line_detector.is_in_line():
continue
self.car.steering.turn(90+35) # 우회전
while line_detector.is_in_line():
continue
while not line_detector.is_in_line():
continue
else:
count_not_obs += 1
if count_not_obs == 8:
if SPEED < 70:
SPEED += 1
print("SPEED", SPEED)
self.car.accelerator.go_forward(SPEED)
self.buzzer.speed = 20 / SPEED
count_not_obs = 0
# 라인 벗어날 경우 (급커브)
if not line_detector.is_in_line():
print("Curve")
self.car.accelerator.stop()
self.car.steering.turn(90 + preLine[0] * 35 + preLine[4] * -35)
self.car.accelerator.go_backward(SPEED)
while not line_detector.is_in_line():
continue
time.sleep(0.1)
self.car.accelerator.stop()
self.car.accelerator.go_forward(SPEED)
# 라인 트레이싱
line = line_detector.read_digital()
degree = [-20 if line[1] else -35, -5 if line[2] else -10, 0, 5 if line[2] else 10, 20 if line[3] else 35]
degree = [x*y for x, y in zip(line, degree)]
self.car.steering.turn(90 + sum(degree))
preLine = line
# 정지선 카운트
if [1,1,1,1,1] == line:
self.car.steering.center_alignment()
while line_detector.read_digital() == [1,1,1,1,1]:
continue
count += 1
print("meet end", count)
self.drive_parking()
if __name__ == "__main__":
try:
myCar = myCar("CarName")
myCar.car_startup()
except KeyboardInterrupt:
# when the Ctrl+C key has been pressed,
# the moving object will be stopped
myCar.drive_parking()