已有car.py,代码如下import time
from pyb import UART
from pyb import Servo
def sendcommand(buffer10):
uart=UART(3,115200)
for i in range(0,10):
uart.writechar(buffer10[i])
time.sleep_us(1)
def initbuffer():
frame_head1=0xFA
frame_head2=0xAF
machine_id=0x00
command_id=0x04
commandone_high=0x00
commandone_low=0x00
commandtwo_high=0x00
commandtwo_low=0x00
examine=machine_id+command_id+commandone_high+commandone_low+commandtwo_high+commandtwo_low
frame_end=0xED
buffer10=[frame_head1,frame_head2,machine_id,command_id,commandone_high,commandone_low,commandtwo_high,commandtwo_low,examine,frame_end]
return buffer10
def opentheled(_id):
machine_id=_id
command_id=0x04
commandone_high=0x00
commandone_low=0x00
commandtwo_high=0x00
commandtwo_low=0x00
examine=machine_id+command_id+commandone_high+commandone_low+commandtwo_high+commandtwo_low
buffer10=[250,0xAF,machine_id,command_id,commandone_high,commandone_low,commandtwo_high,commandtwo_low,examine,0xED]
return buffer10
def closetheled(_id):
machine_id=_id
command_id=0x04
commandone_high=0x01
commandone_low=0x00
commandtwo_high=0x00
commandtwo_low=0x00
examine=machine_id+command_id+commandone_high+commandone_low+commandtwo_high+commandtwo_low
buffer10=[0xFA,0xAF,machine_id,command_id,commandone_high,commandone_low,commandtwo_high,commandtwo_low,examine,0xED]
return buffer10
def motor_rotate(_id,direcation,speed):
machine_id=_id
command_id=0x01
if direcation=="front":
commandone_high=0xFD
elif direcation=="back":
commandone_high=0xFE
commandone_low=0x00
commandtwo_high=0x00
commandtwo_low=speed
examine=machine_id+command_id+commandone_high+commandone_low+commandtwo_high+commandtwo_low
buffer10=[0xFA,0xAF,machine_id,command_id,commandone_high,commandone_low,commandtwo_high,commandtwo_low,examine,0xED]
return buffer10
def getspeed(_id):
bool1="false"
uart=UART(3,115200)
examine=_id+0x02
temp_buf=[0xFA,0xAF,_id,0x02,0x00,0x00,0x00,0x00,examine,0xED]
for i in range(0,75):
sendcommand(temp_buf)
str1=uart.readline()
if str1!=None and bool1=="True":
print(str1)
elif str1!=None:
bool1="True"
time.sleep_ms(1)
def moveforward(_speed):
buf=motor_rotate(_id=1,direcation="front",speed=_speed)
for i in range(0,75):
sendcommand(buf)
time.sleep_ms(10)
buf=motor_rotate(_id=2,direcation="back",speed=_speed)
for i in range(0,75):
sendcommand(buf)
def moveback(_speed):
buf=motor_rotate(_id=1,direcation="back",speed=_speed)
for i in range(0,75):
sendcommand(buf)
time.sleep_ms(10)
buf=motor_rotate(_id=2,direcation="front",speed=_speed)
for i in range(0,75):
sendcommand(buf)
def stop():
moveforward(_speed=0)
s1 = Servo(1)
s1.angle(0,0)