[英]Using multiple utrasonic sensors with Ubuntu Mate (Raspberry Pi 3) and python
我試圖將三個獨立的超聲波傳感器(HC-SR04)連接到運行Ubuntu Mate的Raspberry Pi。 目的是從傳感器讀取輸入並將其發送到LAMP驅動的服務器。 該系統可以在一個傳感器上正常運行,但是我不確定如何將多個傳感器連接到系統。 目前,我正在使用的代碼如下:
import RPi.GPIO as GPIO
import time
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
#set GPIO Pins
GPIO_TRIGGER = 16
GPIO_ECHO = 21
GPIO_ECHO2 = 24
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
GPIO.setup(GPIO_ECHO2, GPIO.IN)
#MySQL
import MySQLdb
dbConn = MySQLdb.connect("127.0.0.1","root","","test") or die ("could not connect to db")
cursor = dbConn.cursor()
def distance1():
# set Trigger to HIGH
GPIO.output(GPIO_TRIGGER, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
return distance1
def distance2():
# set Trigger to HIGH
GPIO.output(GPIO_TRIGGER, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(GPIO_ECHO2) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(GPIO_ECHO2) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
return distance2
if __name__ == '__main__':
try:
while True:
dist = distance1()
if distance1() < 20:
print ("1")
cursor.execute("INSERT INTO TILA (anturi, status) values (1, 0)")
dbConn.commit()
else:
print ("0")
cursor.execute("INSERT INTO TILA (anturi, status) values (1, 1)")
dbConn.commit()
time.sleep(1)
# Reset by pressing CTRL + C
except KeyboardInterrupt:
print("Measurement stopped by User")
cursor.close()
GPIO.cleanup()
if __name__ == '__main__':
try:
while True:
dist = distance2()
if distance2() < 20:
print ("1")
cursor.execute("INSERT INTO TILA (anturi, status) values (1, 0)")
dbConn.commit()
else:
print ("0")
cursor.execute("INSERT INTO TILA (anturi, status) values (1, 1)")
dbConn.commit()
time.sleep(1)
# Reset by pressing CTRL + C
except KeyboardInterrupt:
print("Measurement stopped by User")
cursor.close()
GPIO.cleanup()
現在,此代碼確實可以在終端中運行,但是不起作用。 它可以在一個傳感器上正常工作,但是在我更改代碼以包含兩個“回聲”和“距離”后,它停止工作。 這是我第一次使用python,我知道這段代碼可能有幾個錯誤。 如果有人能告訴我我是否走上了正確的道路以及如何從這里繼續,我將不勝感激!
您有太多重復的代碼。 如果要更改代碼以便可以檢查多個距離,則應制作一個通用distance
函數,該函數將傳感器作為參數並進行檢查。
def distance(gpio_echo):
# set Trigger to HIGH
GPIO.output(GPIO_TRIGGER, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(gpio_echo) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(gpio_echo) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
dist = (TimeElapsed * 34300) / 2
return dist
然后,您可以為所有傳感器多次調用此函數:
if __name__ == '__main__':
sensors_to_test = [GPIO_ECHO, GPIO_ECHO2]
try:
while True:
for sensor in sensors_to_test:
dist = distance(sensor)
if dist < 20:
print ("1")
cursor.execute("INSERT INTO TILA (anturi, status) values (1, 0)")
dbConn.commit()
else:
print ("0")
cursor.execute("INSERT INTO TILA (anturi, status) values (1, 1)")
dbConn.commit()
time.sleep(1)
except KeyboardInterrupt:
# Reset by pressing CTRL + C
print("Measurement stopped by User")
cursor.close()
GPIO.cleanup()
進行編程時,如果要多次執行某些操作,則請勿多次復制和粘貼代碼。 有一些稱為循環的編程構造。 在這里,我們使用一個for循環:
for sensor in sensors_to_test:
dist = distance(sensor)
始終將邏輯封裝到接受參數的函數中,並使用它們而不是復制代碼!
PS:在python中,我們從不使用if __name__ == '__main__':
在文件中多次使用,並且僅在可執行python腳本的末尾使用。 它不是C或Java中的主要功能。 這只是一個巧妙的技巧,因此當您從命令行調用python腳本中的代碼時,該代碼將被執行。 如果您想了解更多信息,請查看此問題: __name__ ==“ __main__”怎么辦?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.