基于python的树莓派同时使用多个VL53L0X传感器的方法
VL53l0x這個傳感器的坑爹之處看到這個博文的童鞋應該很清楚,作為一個地址可編譯的I2C傳感器,VL53L0X的默認I2C地址是29。但是無論你把他的地址改成什么,斷電重啟之后就會直接恢復默認,所以如何同時使用多個該處理器是個難題。
?
題主不幸的遇到了這么個問題,但是全網(wǎng)都找不到基于python的這個問題完美的解決方案。搗鼓了一天終于把這個問題解決了。這里把解決的過程分享給大家,防止后來人再踩到我踩的坑。
首先,本博文參考了github piromoni開源項目VL53L0X-python/python at master · pimoroni/VL53L0X-python · GitHub
?
樹莓派如何連接VL53L0X的例子很好找,值得一提的是樹莓派只要接5根線就行了。這里附上代碼中的接線圖
題主開始直接照搬了piromoni的github項目,但是發(fā)現(xiàn)并不能正確使用。piromoni的代碼結構很清晰,但是存在一定的邏輯性錯誤。
import time import VL53L0X import RPi.GPIO as GPIO# GPIO for Sensor 1 shutdown pin sensor1_shutdown = 20 # GPIO for Sensor 2 shutdown pin sensor2_shutdown = 16GPIO.setwarnings(False)# Setup GPIO for shutdown pins on each VL53L0X GPIO.setmode(GPIO.BCM) GPIO.setup(sensor1_shutdown, GPIO.OUT) GPIO.setup(sensor2_shutdown, GPIO.OUT)# Set all shutdown pins low to turn off each VL53L0X GPIO.output(sensor1_shutdown, GPIO.LOW) GPIO.output(sensor2_shutdown, GPIO.LOW)# Keep all low for 500 ms or so to make sure they reset time.sleep(0.50)# Create one object per VL53L0X passing the address to give to # each. tof = VL53L0X.VL53L0X(i2c_address=0x2B) tof1 = VL53L0X.VL53L0X(i2c_address=0x2D) tof.open() tof1.open()# Set shutdown pin high for the first VL53L0X then # call to start ranging GPIO.output(sensor1_shutdown, GPIO.HIGH) time.sleep(0.50) tof.start_ranging(VL53L0X.Vl53l0xAccuracyMode.BETTER)# Set shutdown pin high for the second VL53L0X then # call to start ranging GPIO.output(sensor2_shutdown, GPIO.HIGH) time.sleep(0.50) tof1.start_ranging(VL53L0X.Vl53l0xAccuracyMode.BETTER)這里的shutdown的端口只要查找樹莓派的各輸出端口代號就行。
piromoni的代碼想通過直接改地址來解決一次多用的問題,但是修改地址的時候GPIO output必須是高電壓。
也就是說他的下面這段代碼是錯誤的。
GPIO.output(sensor1_shutdown, GPIO.LOW) GPIO.output(sensor2_shutdown, GPIO.LOW)# Keep all low for 500 ms or so to make sure they reset time.sleep(0.50)# Create one object per VL53L0X passing the address to give to # each. tof = VL53L0X.VL53L0X(i2c_address=0x2B) tof1 = VL53L0X.VL53L0X(i2c_address=0x2D) tof.open() tof1.open()# Set shutdown pin high for the first VL53L0X then # call to start ranging GPIO.output(sensor1_shutdown, GPIO.HIGH) time.sleep(0.50) tof.start_ranging(VL53L0X.Vl53l0xAccuracyMode.BETTER)# Set shutdown pin high for the second VL53L0X then # call to start ranging GPIO.output(sensor2_shutdown, GPIO.HIGH) time.sleep(0.50) tof1.start_ranging(VL53L0X.Vl53l0xAccuracyMode.BETTER)改代碼的思路應該是每使能一個VL53L0X,改一個地址,這樣才能正確修改地址。同時每次重啟的時候,所有的VL53L0X的地址都是29,所以開始讀入的時候都是29.
所以上圖代碼應改成了下圖所示:
GPIO.output(sensor1_shutdown, GPIO.LOW) GPIO.output(sensor2_shutdown, GPIO.LOW)# Keep all low for 500 ms or so to make sure they reset time.sleep(0.5) # Create one object per VL53L0X passing the address to give to # each. tof1 = VL53L0X.VL53L0X(i2c_address=0x29) tof2 = VL53L0X.VL53L0X(i2c_address=0x29)GPIO.output(sensor1_shutdown, GPIO.HIGH) tof.change_address(0x21)GPIO.output(sensor2_shutdown, GPIO.HIGH) tof1.change_address(0x22)tof1.open() tof2.open()但是!!!!!!!!盡管你修改了代碼,該程序很大概率會報I2C ERROR,當時困擾了我很久。在排除了各種接線的原因之后,我的視線聚集到了下面這行代碼:
time.sleep(0.5)?
這也是VL53L0X極為坑爹的一個點:初始化極慢。所以在運行這個程序時,把sleep改成3s,就可以完美解決這個問題。
到這,如果你連線正確,那每個VL53L0X就可以正常工作了。
這里附上正確的初始化代碼
import time import VL53L0X import RPi.GPIO as GPIO # GPIO for Sensor 2 shutdown pin # GPIO for Sensor 1 shutdown pin sensor1_shutdown = 20 sensor2_shutdown = 16GPIO.setwarnings(False)# Setup GPIO for shutdown pins on each VL53L0X GPIO.setmode(GPIO.BCM) GPIO.setup(sensor1_shutdown, GPIO.OUT) GPIO.setup(sensor2_shutdown, GPIO.OUT)# Set all shutdown pins low to turn off each VL53L0X GPIO.output(sensor1_shutdown, GPIO.LOW) GPIO.output(sensor2_shutdown, GPIO.LOW)# Keep all low for 500 ms or so to make sure they reset time.sleep(3) # Create one object per VL53L0X passing the address to give to # each. tof1 = VL53L0X.VL53L0X(i2c_address=0x29) tof2 = VL53L0X.VL53L0X(i2c_address=0x29)GPIO.output(sensor2_shutdown, GPIO.HIGH) tof1.change_address(0x22)GPIO.output(sensor3_shutdown, GPIO.HIGH) tof2.change_address(0x23)tof1.open() tof2.open()# Set shutdown pin high for the first VL53L0X then # call to start ranging tof.start_ranging(VL53L0X.Vl53l0xAccuracyMode.BETTER)# Set shutdown pin high for the second VL53L0X then # call to start ranging tof1.start_ranging(VL53L0X.Vl53l0xAccuracyMode.BETTER)?
總結
以上是生活随笔為你收集整理的基于python的树莓派同时使用多个VL53L0X传感器的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 清明节放假公告
- 下一篇: linux 利用cat写文件名,在LIN