白色为发射管,黑色为接收管,3个针脚,一个输入输出引脚;有障碍物低电平2亮灯;没有障碍物,一个灯亮(小蓝色的方块调节灵敏度)
发射管一直在发射红外光,当前面的障碍物越近,反射回来的红外光越强,红外接收管的EC电压越接近0V,那么就会小于LM393的2脚的电压,LM393输出低电平,绿色指示灯亮。
在实际使用中红外接收管工作在放大区,也就是说C点的电压在不断的变化。所以我们调节LM393的2脚的电压来改变灵敏度。
#!/usr/bin/python
# coding=utf-8
#本段代码实现树莓派智能小车的红外避障效果
#代码使用的树莓派GPIO是用的BOARD编码方式。
import RPi.GPIO as GPIO
import time
import sys
SensorRight = 16 #右边的红外避障模块接口
SensorLeft = 12 #左边的红外避障接口
PWMA = 18
AIN1 = 22
AIN2 = 27
PWMB = 23
BIN1 = 25
BIN2 = 24
BtnPin = 19
Gpin = 5
Rpin = 6
#基本运动函数
def t_up(speed,t_time):
L_Motor.ChangeDutyCycle(speed)
GPIO.output(AIN2,False)#AIN2
GPIO.output(AIN1,True) #AIN1
R_Motor.ChangeDutyCycle(speed)
GPIO.output(BIN2,False)#BIN2
GPIO.output(BIN1,True) #BIN1
time.sleep(t_time)
def t_stop(t_time):
L_Motor.ChangeDutyCycle(0)
GPIO.output(AIN2,False)#AIN2
GPIO.output(AIN1,False) #AIN1
R_Motor.ChangeDutyCycle(0)
GPIO.output(BIN2,False)#BIN2
GPIO.output(BIN1,False) #BIN1
time.sleep(t_time)
def t_down(speed,t_time):
L_Motor.ChangeDutyCycle(speed)
GPIO.output(AIN2,True)#AIN2
GPIO.output(AIN1,False) #AIN1
R_Motor.ChangeDutyCycle(speed)
GPIO.output(BIN2,True)#BIN2
GPIO.output(BIN1,False) #BIN1
time.sleep(t_time)
def t_left(speed,t_time):
L_Motor.ChangeDutyCycle(speed)
GPIO.output(AIN2,True)#AIN2
GPIO.output(AIN1,False) #AIN1
R_Motor.ChangeDutyCycle(speed)
GPIO.output(BIN2,False)#BIN2
GPIO.output(BIN1,True) #BIN1
time.sleep(t_time)
def t_right(speed,t_time):
L_Motor.ChangeDutyCycle(speed)
GPIO.output(AIN2,False)#AIN2
GPIO.output(AIN1,True) #AIN1
R_Motor.ChangeDutyCycle(speed)
GPIO.output(BIN2,True)#BIN2
GPIO.output(BIN1,False) #BIN1
time.sleep(t_time)
def keysacn():#?????按钮??
val = GPIO.input(BtnPin)
while GPIO.input(BtnPin) == False:
val = GPIO.input(BtnPin)
while GPIO.input(BtnPin) == True:
time.sleep(0.01)
val = GPIO.input(BtnPin)
if val == True:
GPIO.output(Rpin,1)
while GPIO.input(BtnPin) == False:
GPIO.output(Rpin,0)
else:
GPIO.output(Rpin,0)
def setup():#设置输出输入接口
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # Numbers GPIOs by physical location
GPIO.setup(Gpin, GPIO.OUT) # Set Green Led Pin mode to output
GPIO.setup(Rpin, GPIO.OUT) # Set Red Led Pin mode to output
GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set BtnPin's mode is input, and pull up to high level(3.3V)
GPIO.setup(SensorRight,GPIO.IN)
GPIO.setup(SensorLeft,GPIO.IN)
GPIO.setup(AIN2,GPIO.OUT)
GPIO.setup(AIN1,GPIO.OUT)
GPIO.setup(PWMA,GPIO.OUT)
GPIO.setup(BIN1,GPIO.OUT)
GPIO.setup(BIN2,GPIO.OUT)
GPIO.setup(PWMB,GPIO.OUT)
if __name__ == '__main__':
setup()
keysacn()
L_Motor= GPIO.PWM(PWMA,100)#左右使能
L_Motor.start(0)
R_Motor = GPIO.PWM(PWMB,100)
R_Motor.start(0)
try:
while True:
#SR_2和SL_2分别读取左右两边----红外避障模块的状态;低电平有障碍物
SR_2 = GPIO.input(SensorRight)#高低电平判断!
SL_2 = GPIO.input(SensorLeft)
if SL_2 == True and SR_2 == True:#当碰到障碍物时候——红外模块输入低电平
print("t_up")
t_up(50,0)
elif SL_2 == True and SR_2 ==False:#右边有障碍物向左
print("Left")
t_left(50,0)
elif SL_2==False and SR_2 ==True:
print("Right")
t_right(50,0)
else:#左右两边都有障碍物!
t_stop(0.3)
t_down(50,0.4)
t_left(50,0.5)
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
GPIO.cleanup()