import RPi.GPIO as GPIO
import time
import speech_recognition as sr
import pyttsx3 # TTS 엔진 라이브러리
# TTS 엔진 초기화
engine = pyttsx3.init()
# GPIO 모드 설정
GPIO.setmode(GPIO.BCM)
# 모터 A와 B에 대한 핀 번호 설정
motorA_pin1 = 26
motorA_pin2 = 19
motorB_pin1 = 17
motorB_pin2 = 27
# 모터 핀을 출력으로 설정
GPIO.setup(motorA_pin1, GPIO.OUT)
GPIO.setup(motorA_pin2, GPIO.OUT)
GPIO.setup(motorB_pin1, GPIO.OUT)
GPIO.setup(motorB_pin2, GPIO.OUT)
# 모터 제어 함수 정의
def forward(seconds):
engine.say("앞으로 전진하겠습니다")
engine.runAndWait() # 음성 출력이 끝날 때까지 대기
# 모터 전진
GPIO.output(motorA_pin1, GPIO.HIGH)
GPIO.output(motorA_pin2, GPIO.LOW)
GPIO.output(motorB_pin1, GPIO.HIGH)
GPIO.output(motorB_pin2, GPIO.LOW)
time.sleep(seconds)
stop_motors()
def backward(seconds):
engine.say("뒤로 후진하겠습니다")
engine.runAndWait() # 음성 출력이 끝날 때까지 대기
# 모터 후진
GPIO.output(motorA_pin1, GPIO.LOW)
GPIO.output(motorA_pin2, GPIO.HIGH)
GPIO.output(motorB_pin1, GPIO.LOW)
GPIO.output(motorB_pin2, GPIO.HIGH)
time.sleep(seconds)
stop_motors()
def turn_right(seconds):
engine.say("우회전하겠습니다")
engine.runAndWait() # 음성 출력이 끝날 때까지 대기
# 오른쪽으로 회전
GPIO.output(motorA_pin1, GPIO.HIGH)
GPIO.output(motorA_pin2, GPIO.LOW)
GPIO.output(motorB_pin1, GPIO.LOW)
GPIO.output(motorB_pin2, GPIO.HIGH)
time.sleep(seconds)
stop_motors()
def turn_left(seconds):
engine.say("좌회전하겠습니다")
engine.runAndWait() # 음성 출력이 끝날 때까지 대기
# 왼쪽으로 회전
GPIO.output(motorA_pin1, GPIO.LOW)
GPIO.output(motorA_pin2, GPIO.HIGH)
GPIO.output(motorB_pin1, GPIO.HIGH)
GPIO.output(motorB_pin2, GPIO.LOW)
time.sleep(seconds)
stop_motors()
def stop_motors():
# 모터 정지
GPIO.output(motorA_pin1, GPIO.LOW)
GPIO.output(motorA_pin2, GPIO.LOW)
GPIO.output(motorB_pin1, GPIO.LOW)
GPIO.output(motorB_pin2, GPIO.LOW)
# 음성 인식 함수 정의
def recognize_command():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = recognizer.listen(source)
try:
return recognizer.recognize_google(audio, language="ko-KR")
except sr.UnknownValueError:
return "Could not understand audio"
except sr.RequestError as e:
return f"API unavailable {e}"
# 메인 루프
try:
while True:
command = recognize_command().lower()
if "앞으로" in command:
forward(1)
print("Moving Forward")
elif "뒤로" in command:
backward(1)
print("Moving Backward")
elif "우회전" in command:
turn_right(1)
print("Turning Right")
elif "좌회전" in command:
turn_left(1)
print("Turning Left")
finally:
GPIO.cleanup()
Copyright ⓒ TECH79 All right reserved