import serial
import RPi.GPIO as GPIO
M1=13
M2=6
M3=7
M4=12
GPIO.setmode(GPIO.BCM)
GPIO.setup(M1,GPIO.OUT)
GPIO.setup(M2,GPIO.OUT)
GPIO.setup(M3,GPIO.OUT)
GPIO.setup(M4,GPIO.OUT)
port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=1)
data1=" "
data=" "
def left(): #left
print("forward")
GPIO.output(M1, True)
GPIO.output(M2, False)
GPIO.output(M3, True)
GPIO.output(M4, False)
def right():
print('backward')
GPIO.output(M1, False)
GPIO.output(M2, True)
GPIO.output(M3, False)
GPIO.output(M4, True)
def backward():
print('right')
GPIO.output(M1, False)
GPIO.output(M2, True)
GPIO.output(M3, True)
GPIO.output(M4, False)
def forward():
print('left')
GPIO.output(M1, True)
GPIO.output(M2, False)
GPIO.output(M3, False)
GPIO.output(M4, True)
def stop():
print('stop')
GPIO.output(M1, True)
GPIO.output(M2, True)
GPIO.output(M3, False)
GPIO.output(M4, False)
stop()
while 1:
data = str(port.read(1))
data1+=data
a=data1[3]
#print (data1)
if 'U' in data1:
forward()
if 'D'in data1:
backward()
if 'R' in data1:
right()
if 'L' in data1:
left()
if 'C' in data1:
stop()
print(data1[3])
port.flush()
data=" "
data1=" "