电子说
第1步:准备
步骤2:获取这些设置
硬件要求:
6个HC-SR04 USS(单击此处)
6个USS Br托架(单击此处)
3个双向电平转换器-确保它们是I2C安全的(单击此处)
从女性到女性的跨接电缆束(单击此处)
Raspberry pi Robot
软件设置:
sudo apt-get install pigpio
第3步:连接东西
双向水平移位器上的示例连接
LV +3.3V
HV +5V
L1 GPIO Pin of your choice for trigger1
H1 Trig Pin On USS1
L2 GPIO Pin of your choice for ECHO1
H2 ECHO Pin On USS2
L3 GPIO Pin of your choice for trigger2
H3 Trig Pin On USS2
L4 GPIO Pin of your choice for ECHO2
H4 ECHO Pin On USS2
GND GND Rail
在USS上的连接示例
VCC +5v
GND GND Rail
Trigg H1 Level Shifter
Echo H2 Level Shifter
可以看到我们有两个USS使用一个相同的电平转换器,所以对于6个USS,我们有3个电平转换器。
您可以参考本文档,将USS通过电平转换器连接到Raspberry Pi(单击此处)
步骤4:最终运行所有6个传感器
这是我用来运行所有6个USS的Python代码
如您所见,Memcached也可用于从此脚本中提取值。
此脚本适用于提供的示例文档中提到的引脚在上一步中,需要专门编辑引脚号。
#!/usr/bin/env python
#
# HC-SR04 interface code for the Raspberry Pi
#
# William Henning @ http://Mikronauts.com
#
# uses joan‘s excellent pigpio library
#
# Does not quite work in one pin mode, will be updated in the future
#
import time
import pigpio
#import memcache
#mc = memcache.Client([’127.0.0.1:11211‘], debug=0)
def _echo1(gpio, level, tick):
global _high
_high = tick
def _echo0(gpio, level, tick):
global _done, _high, _time
_time = tick - _high
_done = True
def readDistance2(_trig, _echo):
global pi, _done, _time
_done = False
pi.set_mode(_trig, pigpio.OUTPUT)
pi.gpio_trigger(_trig,50,1)
pi.set_mode(_echo, pigpio.INPUT)
time.sleep(0.0001)
tim = 0
while not _done:
time.sleep(0.001)
tim = tim+1
if tim 》 50:
return 2900
return _time
pi = pigpio.pi(’localhost‘,1234)
if __name__ == “__main__”:
my_echo1 = pi.callback(10, pigpio.RISING_EDGE, _echo1)
my_echo0 = pi.callback(10, pigpio.FALLING_EDGE, _echo0)
my_echo1 = pi.callback(25, pigpio.RISING_EDGE, _echo1)
my_echo0 = pi.callback(25, pigpio.FALLING_EDGE, _echo0)
my_echo1 = pi.callback(8, pigpio.RISING_EDGE, _echo1)
my_echo0 = pi.callback(8, pigpio.FALLING_EDGE, _echo0)
my_echo1 = pi.callback(5, pigpio.RISING_EDGE, _echo1)
my_echo0 = pi.callback(5, pigpio.FALLING_EDGE, _echo0)
my_echo1 = pi.callback(12, pigpio.RISING_EDGE, _echo1)
my_echo0 = pi.callback(12, pigpio.FALLING_EDGE, _echo0)
my_echo1 = pi.callback(16, pigpio.RISING_EDGE, _echo1)
my_echo0 = pi.callback(16, pigpio.FALLING_EDGE, _echo0)
while 1:
print “DISTANCE 1: ”,(readDistance2(24,10)/58)
print “DISTANCE 2: ”,(readDistance2(9,25)/58)
print “DISTANCE 3: ”,(readDistance2(11,8)/58)
print “DISTANCE 4: ”,(readDistance2(7,5)/58)
print “DISTANCE 5: ”,(readDistance2(6,12)/58)
print “DISTANCE 6: ”,(readDistance2(19,16)/58)
#mc.set(“d1”,(readDistance2(24,10)/58))
#mc.set(“d2”,(readDistance2(9,25)/58))
#mc.set(“d3”,(readDistance2(11,8)/58))
#mc.set(“d4”,(readDistance2(7,5)/58))
#mc.set(“d5”,(readDistance2(6,12)/58))
#mc.set(“d6”,(readDistance2(19,16)/58))
time.sleep(0.05)
# my_echo1.cancel()
# my_echo0.cancel()
责任编辑:wv
全部0条评论
快来发表一下你的评论吧 !