第1步:準(zhǔn)備
步驟2:獲取這些設(shè)置
硬件要求:
6個HC-SR04 USS(單擊此處)
6個USS Br托架(單擊此處)
3個雙向電平轉(zhuǎn)換器-確保它們是I2C安全的(單擊此處)
從女性到女性的跨接電纜束(單擊此處)
Raspberry pi Robot
軟件設(shè)置:
sudo apt-get install pigpio
第3步:連接?xùn)|西
雙向水平移位器上的示例連接
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使用一個相同的電平轉(zhuǎn)換器,所以對于6個USS,我們有3個電平轉(zhuǎn)換器。
您可以參考本文檔,將USS通過電平轉(zhuǎn)換器連接到Raspberry Pi(單擊此處)
步驟4:最終運(yùn)行所有6個傳感器
這是我用來運(yùn)行所有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()
責(zé)任編輯:wv
-
超聲波傳感器
+關(guān)注
關(guān)注
18文章
583瀏覽量
35943 -
樹莓派
+關(guān)注
關(guān)注
116文章
1708瀏覽量
105695
發(fā)布評論請先 登錄
相關(guān)推薦
評論