这个模块,对我可谓是一波多折
1、首先期望它能输出5V电压,给树莓派供电,结果供电电压电流都较小,好像带动不了树莓派!
2、开始无论怎么设置,都不能让电机转动,查阅好多资料也无结果。后来偶然发现,本模块的接地端,必须与树莓派的GND口连接起来,否则电机不转!
3、IO口:INT1为高 INT2为低 INT3为高 INT4为低 时,输出电压的正负极如图所示
4、INT1+INT2控制左边的电机,INT3+INT4控制右边的电机
附简单代码如下(这个代码不可调速):
#===================================
# 2020.09.11 我开心
#===================================
try:
import RPi.GPIO as GPIO
import time
except RuntimeError:
print(" import 引入错误 ... ")
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False) #关闭警告
#===================================
# 电机
#===================================
INT1=1
INT2=7
INT3=8
INT4=11
GPIO.setup(INT1,GPIO.OUT)
GPIO.setup(INT2,GPIO.OUT)
GPIO.setup(INT3,GPIO.OUT)
GPIO.setup(INT4,GPIO.OUT)
def car_forward(): #定义前进函数
GPIO.output(INT1,GPIO.HIGH) #将INT1接口设置为高电压
GPIO.output(INT2,GPIO.LOW) #将INT2接口设置为低电压
GPIO.output(INT3,GPIO.HIGH) #将INT3接口设置为高电压
GPIO.output(INT4,GPIO.LOW) #将INT4接口设置为低电压
def car_back(): #定义后退函数
GPIO.output(INT1,GPIO.LOW)
GPIO.output(INT2,GPIO.HIGH)
GPIO.output(INT3,GPIO.LOW)
GPIO.output(INT4,GPIO.HIGH)
def car_left(): #定义左转函数
GPIO.output(INT1,GPIO.LOW)
GPIO.output(INT2,GPIO.HIGH)
GPIO.output(INT3,GPIO.HIGH)
GPIO.output(INT4,GPIO.LOW)
def car_right(): #定义右转函数
GPIO.output(INT1,GPIO.HIGH)
GPIO.output(INT2,GPIO.LOW)
GPIO.output(INT3,GPIO.LOW)
GPIO.output(INT4,GPIO.HIGH)
def car_stop(): #定义停止函数
GPIO.output(INT1,GPIO.LOW)
GPIO.output(INT2,GPIO.LOW)
GPIO.output(INT3,GPIO.LOW)
GPIO.output(INT4,GPIO.LOW)
#===================================
#===================================
# 主程序开始!!!!!
#===================================
#===================================
i=1 #循环次数
try:
while True:
print('第'+str(i)+'次开始')
print('开始前进')
car_forward()
time.sleep(15)
print('开始左转')
car_left()
time.sleep(15)
print('开始右转')
car_right()
time.sleep(15)
print('开始后退')
car_back()
time.sleep(15)
print('开始停车')
car_stop()
time.sleep(15)
#循环的次数+1
i=i+1
except KeyboardInterrupt:
print('KeyboardInterrupt 程序被人为中止....')
finally:
GPIO.cleanup()
print('Exit 程序共运行了'+str(i)+'次,程序结束。')
================
这个代码可以调速度
================
#!/usr/bin/python3
# -*- coding: utf-8 -*-
try:
import RPi.GPIO as GPIO
import time
except RuntimeError:
print("引入错误")
GPIO.setmode(GPIO.BCM)
INT1 = 1
INT2 = 7
INT3 = 8
INT4 = 11
GPIO.setup(INT1, GPIO.OUT)
GPIO.setup(INT2, GPIO.OUT)
GPIO.setup(INT3, GPIO.OUT)
GPIO.setup(INT4, GPIO.OUT)
pwm1 = GPIO.PWM(INT1, 500) #引脚,频率
pwm2 = GPIO.PWM(INT2, 500)
pwm3 = GPIO.PWM(INT3, 500)
pwm4 = GPIO.PWM(INT4, 500)
pwm1.start(0) #启用PWM,初始占空比(范围:0.0 <= dc >= 100.0)
pwm2.start(0)
pwm3.start(0)
pwm4.start(0)
def car_forward(speed):
pwm1.ChangeDutyCycle(speed)
pwm2.ChangeDutyCycle(0)
pwm3.ChangeDutyCycle(speed)
pwm4.ChangeDutyCycle(0)
def car_back(speed):
pwm1.ChangeDutyCycle(0)
pwm2.ChangeDutyCycle(speed)
pwm3.ChangeDutyCycle(0)
pwm4.ChangeDutyCycle(speed)
def car_stop():
pwm1.ChangeDutyCycle(0) #更改占空比,0-100
pwm2.ChangeDutyCycle(0)
pwm3.ChangeDutyCycle(0)
pwm4.ChangeDutyCycle(0)
def car_brake(): #刹车
pwm1.ChangeDutyCycle(100)
pwm2.ChangeDutyCycle(100)
pwm3.ChangeDutyCycle(100)
pwm4.ChangeDutyCycle(100)
#根据电机焊接和接入AB接口不同,调试时需要重新定义一下四个函数
#依次运行f前进/b倒车/c右转/o左转,看小车的实际行为
#根据实际行为,将以下函数重命名
def car_left(speed):
pwm1.ChangeDutyCycle(speed)
pwm2.ChangeDutyCycle(0)
pwm3.ChangeDutyCycle(0)
pwm4.ChangeDutyCycle(speed)
def car_right(speed):
pwm1.ChangeDutyCycle(0)
pwm2.ChangeDutyCycle(speed)
pwm3.ChangeDutyCycle(speed)
pwm4.ChangeDutyCycle(0)
try:
while True:
cmd = input("f前进/b倒车/c右转/o左转/s停车/k刹车 0-9, 例如f5:")
direction = cmd[0]
if cmd[1:] != '':
speed = int( cmd[1:] ) * 10
else:
print("未定义速度,默认值50%")
speed = 50
if speed <=30:
print("警告:功率过低,可能无法驱动电机!!!")
if direction == "f":
print("前进:速度{0}%".format(speed))
car_forward(speed)
elif direction == "b":
print("倒车:速度{0}%".format(speed))
car_back(speed)
elif direction == "c":
print("右转:速度{0}%".format(speed))
car_right(speed)
elif direction == "o":
print("左转:速度{0}%".format(speed))
car_left(speed)
elif direction == "s":
print("停车")
car_stop()
elif direction == "k":
print("刹车")
car_brake()
else:
print("未知命令{0},停车".format(direction))
car_stop()
except KeyboardInterrupt:
print("刹车")
car_brake()
pass
finally:
print("停止")
car_stop()
GPIO.cleanup()
print('Exit...' )
============
下面这个版本,3个超声波(前面、左面、右面)+HX1838红外遥控器,不需要用舵机 2020.09.15 21:10
===========
Screenshot_20200915_220326_com.huawei.himovie.jpg
#===================================
# 3个超声波(前面、左面、右面)+HX1838红外遥控器,不需要用舵机+红绿灯2个+LED屏幕
# 2020.09.15 21:10 我开心
#===================================
try:
import RPi.GPIO as GPIO
import time
import tm1637 #请保证tm1637.py已复制到当前目录下
except RuntimeError:
print(" import 引入错误 ... ")
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False) #关闭警告
#===================================
# 红绿灯
#===================================
#红绿灯-左
led_list_left =[17,27,22] #红R 黄Y 绿G
#红绿灯-右
led_list_right=[21,26,20] #红R 黄Y 绿G
GPIO.setup(led_list_left , GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(led_list_right, GPIO.OUT, initial=GPIO.LOW)
def led_liang(): #定义函数 LED所有灯常亮
for led in led_list_left:
GPIO.output(led, GPIO.HIGH)
for led in led_list_right:
GPIO.output(led, GPIO.HIGH)
def led_mie(): #定义函数 LED所有灯常灭
for led in led_list_left:
GPIO.output(led, GPIO.LOW)
for led in led_list_right:
GPIO.output(led, GPIO.LOW)
def led_left(): #定义函数 LED 左转流水灯
led_mie()
for led in led_list_left:
GPIO.output(led, GPIO.LOW)
time.sleep(0.05)
GPIO.output(led, GPIO.HIGH)
time.sleep(0.05)
def led_right(): #定义函数 LED 右转流水灯
led_mie()
for led in led_list_right:
GPIO.output(led, GPIO.LOW)
time.sleep(0.05)
GPIO.output(led, GPIO.HIGH)
time.sleep(0.05)
#===================================
# 超声波 前面
#===================================
trig=24 #send-pin
echo=25 #receive-pin
GPIO.setup(trig,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(echo,GPIO.IN)
def chaoshengbo(): #定义函数
#send
GPIO.output(trig,True)
time.sleep(0.00011) #1us
GPIO.output(trig,False)
#start recording
while GPIO.input(echo)==0:
pass
start=time.time()
#end recording
while GPIO.input(echo)==1:
pass
end=time.time()
#compute distance
distance=round((end-start)*343/2*100,2)
#print('距离前方:{0}cm'.format(distance))
return distance
#===================================
# 超声波 右面
#===================================
trig_r=3 #send-pin
echo_r=4 #receive-pin
GPIO.setup(trig_r,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(echo_r,GPIO.IN)
def chaoshengbo_r(): #定义函数右
#send
GPIO.output(trig_r,True)
time.sleep(0.00011) #1us
GPIO.output(trig_r,False)
#start recording
while GPIO.input(echo_r)==0:
pass
start=time.time()
#end recording
while GPIO.input(echo_r)==1:
pass
end=time.time()
#compute distance
distance_r=round((end-start)*343/2*100,2)
print('距离右方:{0}cm'.format(distance_r))
return distance_r
#===================================
# 超声波 左面
#===================================
trig_l=9 #send-pin
echo_l=11 #receive-pin
GPIO.setup(trig_l,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(echo_l,GPIO.IN)
def chaoshengbo_l(): #定义函数左
#send
GPIO.output(trig_l,True)
time.sleep(0.00011) #1us
GPIO.output(trig_l,False)
#start recording
while GPIO.input(echo_l)==0:
pass
start=time.time()
#end recording
while GPIO.input(echo_l)==1:
pass
end=time.time()
#compute distance
distance_l=round((end-start)*343/2*100,2)
print('距离左方:{0}cm'.format(distance_l))
return distance_l
#===================================
# 喇叭9号口
#===================================
GPIO.setup(9,GPIO.OUT,initial=GPIO.LOW)
def speak():
GPIO.output(9,GPIO.HIGH) #喇叭响
time.sleep(0.05)
GPIO.output(9,GPIO.LOW)
#===================================
# 五路循迹+碰撞检测
#===================================
'''
S1=15 #左1
S2= 4 #左2
S3=18 #中
S4=17 #右2
S5=27 #右1
CLP=23 #碰撞
NEAR=22 #测距
GPIO.setup(S1, GPIO.IN)
GPIO.setup(S2, GPIO.IN)
GPIO.setup(S3, GPIO.IN)
GPIO.setup(S4, GPIO.IN)
GPIO.setup(S5, GPIO.IN)
GPIO.setup(CLP, GPIO.IN)
GPIO.setup(NEAR, GPIO.IN)
def wulu(): #定义五路及碰撞检测
SS=SS1=SS2=SS3=SS4=SS5=CLPCLP=0
if GPIO.input(S1)==1:
SS1=1
print(' S1左1 检测到道路!')
if GPIO.input(S2)==1:
SS2=10
print(' S2左2 检测到道路!')
if GPIO.input(S3)==1:
SS3=100
print(' S3中间 检测到道路!')
if GPIO.input(S4)==1:
SS4=1000
print(' S4右2 检测到道路!')
if GPIO.input(S5)==1:
SS5=10000
print(' S5右1 检测到道路!')
if GPIO.input(CLP)==1:
CLPCLP=100000
print(' CLP碰撞 发生碰撞!!!!!!')
SS=SS1+SS2+SS3+SS4+SS5+CLPCLP #返回值,目前本算法不太精确,正在改进中!!!
#print(str(SS))
return SS
'''
#===================================
# 电机
#===================================
INT1=5
INT2=6
INT3=13
INT4=19
GPIO.setup(INT1, GPIO.OUT)
GPIO.setup(INT2, GPIO.OUT)
GPIO.setup(INT3, GPIO.OUT)
GPIO.setup(INT4, GPIO.OUT)
pwm1 = GPIO.PWM(INT1, 500) #引脚,频率
pwm2 = GPIO.PWM(INT2, 500)
pwm3 = GPIO.PWM(INT3, 500)
pwm4 = GPIO.PWM(INT4, 500)
pwm1.start(0) #启用PWM,初始占空比(范围:0.0 <= dc >= 100.0)
pwm2.start(0)
pwm3.start(0)
pwm4.start(0)
def car_back(speed):
pwm1.ChangeDutyCycle(speed)
pwm2.ChangeDutyCycle(0)
pwm3.ChangeDutyCycle(speed)
pwm4.ChangeDutyCycle(0)
def car_forward(speed):
pwm1.ChangeDutyCycle(0)
pwm2.ChangeDutyCycle(speed)
pwm3.ChangeDutyCycle(0)
pwm4.ChangeDutyCycle(speed)
def car_stop():
pwm1.ChangeDutyCycle(0) #更改占空比,0-100
pwm2.ChangeDutyCycle(0)
pwm3.ChangeDutyCycle(0)
pwm4.ChangeDutyCycle(0)
def car_brake(): #刹车
pwm1.ChangeDutyCycle(100)
pwm2.ChangeDutyCycle(100)
pwm3.ChangeDutyCycle(100)
pwm4.ChangeDutyCycle(100)
#根据电机焊接和接入AB接口不同,调试时需要重新定义一下四个函数
#依次运行前进 倒车 右转 左转,看小车的实际行为
#根据实际行为,将以下函数重命名
def car_left(speed):
pwm1.ChangeDutyCycle(speed)
pwm2.ChangeDutyCycle(0)
pwm3.ChangeDutyCycle(0)
pwm4.ChangeDutyCycle(speed)
def car_right(speed):
pwm1.ChangeDutyCycle(0)
pwm2.ChangeDutyCycle(speed)
pwm3.ChangeDutyCycle(speed)
pwm4.ChangeDutyCycle(0)
#===================================
# LED屏幕
#===================================
CLK=15
DIO=14
led = tm1637.TM1637(clk=CLK, dio=DIO) #实例化TM1637
GPIO.setup(CLK,GPIO.OUT)
GPIO.setup(DIO,GPIO.OUT)
GPIO.output(CLK,GPIO.HIGH)
GPIO.output(DIO,GPIO.HIGH)
def led8888():
#测试,全点亮
led.show('8888', True)
tm.sleep(1)
#全灭4个空格
led.show(' ')
tm.sleep(1)
#显示数字
led.number(1234)
tm.sleep(1)
#===================================
# hx1838遥控器
#===================================
hx1838_pin = 2;
GPIO.setup(hx1838_pin,GPIO.IN,GPIO.PUD_UP)
#print("等待中,请按下遥控器按钮...")
def ir_1838():
ir_key=""
if GPIO.input(hx1838_pin) == 0:
count = 0
while GPIO.input(hx1838_pin) == 0 and count < 200:
count += 1
time.sleep(0.00006)
count = 0
while GPIO.input(hx1838_pin) == 1 and count < 80:
count += 1
time.sleep(0.00006)
idx = 0
cnt = 0
data = [0,0,0,0]
for i in range(0,32):
count = 0
while GPIO.input(hx1838_pin) == 0 and count < 15:
count += 1
time.sleep(0.00006)
count = 0
while GPIO.input(hx1838_pin) == 1 and count < 40:
count += 1
time.sleep(0.00006)
if count > 8:
data[idx] |= 1<<cnt
if cnt == 7:
cnt = 0
idx += 1
else:
cnt += 1
if data[0]+data[1] == 0xFF and data[2]+data[3] == 0xFF:
#print("Get the key: 0x%02x" %data[2])
if (data[2]==0x45):
ir_key="1"
elif(data[2]==0x46):
ir_key="2"
elif(data[2]==0x47):
ir_key="3"
elif(data[2]==0x44):
ir_key="4"
elif(data[2]==0x40):
ir_key="5"
elif(data[2]==0x43):
ir_key="6"
elif(data[2]==0x07):
ir_key="7"
elif(data[2]==0x15):
ir_key="8"
elif(data[2]==0x09):
ir_key="9"
elif(data[2]==0x16):
ir_key="*"
elif(data[2]==0x19):
ir_key="0"
elif(data[2]==0x0d):
ir_key="#"
elif(data[2]==0x18):
ir_key="上"
elif(data[2]==0x52):
ir_key="下"
elif(data[2]==0x08):
ir_key="左"
elif(data[2]==0x5a):
ir_key="右"
elif(data[2]==0x1c):
ir_key="OK"
print("检测到按键: "+ir_key)
return ir_key
#===================================
#===================================
# 主程序开始!!!!!
#===================================
#===================================
i=1 #循环次数
juli =0 #超声波距离 前方
juli_l=0 #超声波距离 左方
juli_r=0 #超声波距离 右方
speed=35 #默认电机速度
#daolu=0 #返回的道路值
#led.scroll('Jia Hao 2020') # 4 fps
#==============================
# 遥控器遥控+超声波自动避障 主程序
#==============================
try:
while True:
#print('第 '+str(i)+' 次开始....................')
juli = chaoshengbo()
ir_key=ir_1838()
if ir_key=="左" :
led_left()
car_left(speed)
time.sleep(15/speed)
led_mie()
elif ir_key=="右" :
led_right()
car_right(speed)
time.sleep(15/speed)
led_mie()
elif ir_key=='1' or ir_key=='2' or ir_key=='3' or ir_key=='4' or ir_key=='5' or ir_key=='6' or ir_key=='7' or ir_key=='8' or ir_key=='9':
speed=int(ir_key)*10
led.number(speed) #在LED屏幕显内容
elif ir_key=="下":
led_liang()
car_back(speed)
time.sleep(speed/30)
led_mie()
elif juli< speed*1.5:
led_liang()
car_back(speed)
time.sleep(30/speed)
juli_l = chaoshengbo_l()
#led.number(round(juli_l)) #在LED屏幕显内容
juli_r = chaoshengbo_r()
#led.number(round(juli_r)) #在LED屏幕显内容
if juli_l>=juli_r:
led_left()
car_left(speed)
time.sleep(25/speed)
else:
led_right()
car_right(speed)
time.sleep(25/speed)
led_mie()
elif ir_key=="0" or ir_key=="OK" :
car_stop()
led.show('STOP', True)
time.sleep(2)
for s in range(10,-1,-1):
led.numbers(0, s)
time.sleep(1)
car_forward(speed)
led.number(round(juli)) #在LED屏幕显内容
#循环的次数+1
#i=i+1
#time.sleep(0.01)
#==============================
except KeyboardInterrupt:
GPIO.cleanup()
print('KeyboardInterrupt 程序被人为中止....')
finally:
GPIO.cleanup()
print('Exit 程序共运行了'+str(i)+'次,程序结束。')
============
下面这个版本,
树莓派+超声波+舵机+转向灯+LED4位数字屏幕 +LCD1602液晶屏+HX1838红外遥控
2020.09.20
===========
#===================================
# 树莓派+超声波+舵机+转向灯+LED4位数字屏幕 +LCD1602液晶屏+HX1838红外遥控
# 2020.09.20 我开心
#===================================
try:
import RPi.GPIO as GPIO
import time
import tm1637 #请保证tm1637.py已复制到当前目录下
import LCD1602 as LCD #请保证LCD1602.py已复制到当前目录下
import smbus
except RuntimeError:
print(" import 引入错误 ... ")
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False) #关闭警告
#===================================
# 红绿灯
#===================================
#红绿灯-左
led_list_left =[17,27,22] #红R 黄Y 绿G
#红绿灯-右
led_list_right=[21,26,20] #红R 黄Y 绿G
GPIO.setup(led_list_left , GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(led_list_right, GPIO.OUT, initial=GPIO.LOW)
def led_liang(): #定义函数 LED所有灯常亮
for led in led_list_left:
GPIO.output(led, GPIO.HIGH)
for led in led_list_right:
GPIO.output(led, GPIO.HIGH)
def led_mie(): #定义函数 LED所有灯常灭
for led in led_list_left:
GPIO.output(led, GPIO.LOW)
for led in led_list_right:
GPIO.output(led, GPIO.LOW)
def led_left(): #定义函数 LED 左转流水灯
led_mie()
for led in led_list_left:
GPIO.output(led, GPIO.LOW)
time.sleep(0.05)
GPIO.output(led, GPIO.HIGH)
time.sleep(0.05)
def led_right(): #定义函数 LED 右转流水灯
led_mie()
for led in led_list_right:
GPIO.output(led, GPIO.LOW)
time.sleep(0.05)
GPIO.output(led, GPIO.HIGH)
time.sleep(0.05)
#===================================
# 超声波 前面
#===================================
pin_trig=24 #send-pin
pin_echo=25 #receive-pin
GPIO.setup(pin_trig,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(pin_echo,GPIO.IN)
def chaoshengbo(): #定义函数
#send
GPIO.output(pin_trig,True)
time.sleep(0.00011) #1us
GPIO.output(pin_trig,False)
#start recording
while GPIO.input(pin_echo)==0:
pass
start=time.time()
#end recording
while GPIO.input(pin_echo)==1:
pass
end=time.time()
#compute distance
distance=round((end-start)*343/2*100,2)
#print('距离前方:{0}cm'.format(distance))
return distance
#===================================
# 电机
#===================================
pin_INT1=5
pin_INT2=6
pin_INT3=13
pin_INT4=19
GPIO.setup(pin_INT1, GPIO.OUT)
GPIO.setup(pin_INT2, GPIO.OUT)
GPIO.setup(pin_INT3, GPIO.OUT)
GPIO.setup(pin_INT4, GPIO.OUT)
pwm1 = GPIO.PWM(pin_INT1, 500) #引脚,频率
pwm2 = GPIO.PWM(pin_INT2, 500)
pwm3 = GPIO.PWM(pin_INT3, 500)
pwm4 = GPIO.PWM(pin_INT4, 500)
pwm1.start(0) #启用PWM,初始占空比(范围:0.0 <= dc >= 100.0)
pwm2.start(0)
pwm3.start(0)
pwm4.start(0)
def car_back(speed):
pwm1.ChangeDutyCycle(speed)
pwm2.ChangeDutyCycle(0)
pwm3.ChangeDutyCycle(speed)
pwm4.ChangeDutyCycle(0)
def car_forward(speed):
pwm1.ChangeDutyCycle(0)
pwm2.ChangeDutyCycle(speed)
pwm3.ChangeDutyCycle(0)
pwm4.ChangeDutyCycle(speed)
def car_stop():
pwm1.ChangeDutyCycle(0) #更改占空比,0-100
pwm2.ChangeDutyCycle(0)
pwm3.ChangeDutyCycle(0)
pwm4.ChangeDutyCycle(0)
def car_brake(): #刹车
pwm1.ChangeDutyCycle(100)
pwm2.ChangeDutyCycle(100)
pwm3.ChangeDutyCycle(100)
pwm4.ChangeDutyCycle(100)
#根据电机焊接和接入AB接口不同,调试时需要重新定义一下四个函数
#依次运行前进 倒车 右转 左转,看小车的实际行为
#根据实际行为,将以下函数重命名
def car_left(speed):
pwm1.ChangeDutyCycle(speed)
pwm2.ChangeDutyCycle(0)
pwm3.ChangeDutyCycle(0)
pwm4.ChangeDutyCycle(speed)
def car_right(speed):
pwm1.ChangeDutyCycle(0)
pwm2.ChangeDutyCycle(speed)
pwm3.ChangeDutyCycle(speed)
pwm4.ChangeDutyCycle(0)
#===================================
# LED屏幕
#===================================
pin_CLK=15
pin_DIO=14
led = tm1637.TM1637(clk=pin_CLK, dio=pin_DIO) #实例化TM1637
GPIO.setup(pin_CLK,GPIO.OUT)
GPIO.setup(pin_DIO,GPIO.OUT)
GPIO.output(pin_CLK,GPIO.HIGH)
GPIO.output(pin_DIO,GPIO.HIGH)
def led8888():
#测试,全点亮
led.show('8888', True)
tm.sleep(1)
#全灭4个空格
led.show(' ')
tm.sleep(1)
#显示数字
led.number(1234)
tm.sleep(1)
#===================================
# LCD1602 液晶屏
#===================================
def lcd1602_init():
LCD.init_lcd()
time.sleep(0.2)
LCD.print_lcd(1,0,'Li Jiahao 2020')
LCD.print_lcd(1,1,' My Car-001')
LCD.turn_light(1)
time.sleep(2)
LCD.clear_lcd()
#===================================
# hx1838遥控器
#===================================
pin_hx1838 = 23;
#GPIO.setup(pin_hx1838,GPIO.IN,GPIO.PUD_UP)
GPIO.setup(pin_hx1838, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.add_event_detect(pin_hx1838, GPIO.FALLING, bouncetime = 200)
print("等待中,请按下遥控器按钮...")
def ir_1838(pin_hx1838):
global ir_key
ir_key=""
if GPIO.input(pin_hx1838) == 0:
count = 0
while GPIO.input(pin_hx1838) == 0 and count < 200:
count += 1
time.sleep(0.00006)
count = 0
while GPIO.input(pin_hx1838) == 1 and count < 80:
count += 1
time.sleep(0.00006)
idx = 0
cnt = 0
data = [0,0,0,0]
for i in range(0,32):
count = 0
while GPIO.input(pin_hx1838) == 0 and count < 15:
count += 1
time.sleep(0.00006)
count = 0
while GPIO.input(pin_hx1838) == 1 and count < 40:
count += 1
time.sleep(0.00006)
if count > 8:
data[idx] |= 1<<cnt
if cnt == 7:
cnt = 0
idx += 1
else:
cnt += 1
if data[0]+data[1] == 0xFF and data[2]+data[3] == 0xFF:
#print("Get the key: 0x%02x" %data[2])
if (data[2]==0x45):
ir_key="1"
elif(data[2]==0x46):
ir_key="2"
elif(data[2]==0x47):
ir_key="3"
elif(data[2]==0x44):
ir_key="4"
elif(data[2]==0x40):
ir_key="5"
elif(data[2]==0x43):
ir_key="6"
elif(data[2]==0x07):
ir_key="7"
elif(data[2]==0x15):
ir_key="8"
elif(data[2]==0x09):
ir_key="9"
elif(data[2]==0x16):
ir_key="*"
elif(data[2]==0x19):
ir_key="0"
elif(data[2]==0x0d):
ir_key="#"
elif(data[2]==0x18):
ir_key="上"
elif(data[2]==0x52):
ir_key="下"
elif(data[2]==0x08):
ir_key="左"
elif(data[2]==0x5a):
ir_key="右"
elif(data[2]==0x1c):
ir_key="OK"
print("检测到按键: "+ir_key)
return ir_key
#===================================
# s90舵机
#===================================
#暗灰GND、红色VCC 4.8-7.2V 一般用5V、 橙黄线:脉冲输入
pin_s90 = 18
GPIO.setup(pin_s90, GPIO.OUT)
# 初始化PWM
s90_pwm = GPIO.PWM(pin_s90, 50)
s90_pwm.start(0)
def s90(angle): # 定义函数,输入角度,即可自动转向,0-180度,如0、90、180
s90_pwm.ChangeDutyCycle(2.5+angle/360*20)
'''
#开始角度测试:
s90(90)
time.sleep(2)
s90(180)
time.sleep(2)
s90(0)
time.sleep(2)
'''
# 释放资源
#s90_pwm.stop()
#gpio.cleanup()
#===================================
#===================================
# 主程序开始!!!!!
#===================================
#===================================
i=1 #循环次数
juli =0 #超声波距离 前方
speed=35 #默认电机速度
ir_key=""
GPIO.add_event_callback(pin_hx1838, ir_1838) #开始运行ir_1838(), 以便随时响应遥控器按键
lcd1602_init()
s90(90)
LCD.print_lcd(7,0,"Speed:35%")
try:
while True:
juli = chaoshengbo()
print('第 '+str(i)+' 次开始... 距离前方:'+str(juli)+str(ir_key))
led.number(round(juli)) #在LED屏幕显内容(前方距离)
LCD.print_lcd(0,1," "*16) #清理LCD屏幕内容
LCD.print_lcd(5,1,str(round(juli))+"cm") #在LCD屏幕显内容(前方距离 xxx cm)
#ir_key=ir_1838() #红外遥控器检测
if ir_key=="左" :
led_left()
car_left(speed)
time.sleep(15/speed)
led_mie()
elif ir_key=="右" :
led_right()
car_right(speed)
time.sleep(15/speed)
led_mie()
elif ir_key=='1' or ir_key=='2' or ir_key=='3' or ir_key=='4' or ir_key=='5' or ir_key=='6' or ir_key=='7' or ir_key=='8' or ir_key=='9':
speed=int(ir_key)*10
#led.number(speed) #在LED屏幕显内容
LCD.print_lcd(0,0," "*16) #清理LCD屏幕内容
LCD.print_lcd(7,0,"Speed:"+str(speed)+"%") #在LCD屏幕显内容(当前速度 xx%)
elif ir_key=="下":
led_liang()
car_back(speed)
time.sleep(10/speed)
led_mie()
elif ir_key=="0" or ir_key=="OK" :
car_stop()
led.show('STOP', True)
LCD.print_lcd(8,0,"--STOP--")
time.sleep(2)
for s in range(10,-1,-1):
led.numbers(0, s)
time.sleep(1)
LCD.print_lcd(8,0,"Speed:"+str(speed))
if juli< speed*1.5: #根据前方距离,正常速度下,如果距离小于约50厘米,则开始准备停车和倒退
led_liang()
car_back(speed)
time.sleep(10/speed)
#舵机转向左边,并检测左边距离
s90(180)
time.sleep(0.5)
juli_l = chaoshengbo()
print(' 距离左方:'+str(juli_l))
led.number(round(juli_l))
LCD.print_lcd(0,1," "*16)
LCD.print_lcd(0,1,str(round(juli_l))+"cm")
#舵机转向右边,并检测右边距离
s90(0)
time.sleep(0.5)
juli_r = chaoshengbo()
print(' 距离右方:'+str(juli_r))
led.number(round(juli_r))
LCD.print_lcd(8,1,str(round(juli_r))+"cm")
#舵机恢复面向前方
s90(90)
time.sleep(0.5)
#根据左右距离,判断应该向哪边转向(哪边距离大,就向哪边转)
if juli_l>=juli_r:
print(' <---向左转弯')
led_left()
car_left(speed)
time.sleep(25/speed)
else:
led_right()
print(' 向右转弯--->')
car_right(speed)
time.sleep(25/speed)
led_mie()
# 正常情况下(没有按下遥控器,并且前方没有障碍),一直向前行驶
car_forward(speed)
#循环的次数+1
i=i+1
LCD.print_lcd(0,0,str(i))
time.sleep(0.01)
#==============================
except KeyboardInterrupt:
s90_pwm.stop()
GPIO.cleanup()
print('KeyboardInterrupt 程序被人为中止....')
finally:
s90_pwm.stop()
GPIO.cleanup()
print('Exit 程序共运行了'+str(i)+'次,程序结束。')
网友评论