|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# MIT License |
| 4 | +# |
| 5 | +# Copyright (c) 2017 John Bryan Moore |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | + |
| 25 | +import time |
| 26 | +import VL53L0X |
| 27 | +import RPi.GPIO as GPIO |
| 28 | + |
| 29 | +# GPIO for Sensor 1 shutdown pin |
| 30 | +sensor1_shutdown = 20 |
| 31 | +# GPIO for Sensor 2 shutdown pin |
| 32 | +sensor2_shutdown = 16 |
| 33 | + |
| 34 | +GPIO.setwarnings(False) |
| 35 | + |
| 36 | +# Setup GPIO for shutdown pins on each VL53L0X |
| 37 | +GPIO.setmode(GPIO.BCM) |
| 38 | +GPIO.setup(sensor1_shutdown, GPIO.OUT) |
| 39 | +GPIO.setup(sensor2_shutdown, GPIO.OUT) |
| 40 | + |
| 41 | +# Set all shutdown pins low to turn off each VL53L0X |
| 42 | +GPIO.output(sensor1_shutdown, GPIO.LOW) |
| 43 | +GPIO.output(sensor2_shutdown, GPIO.LOW) |
| 44 | + |
| 45 | +# Keep all low for 500 ms or so to make sure they reset |
| 46 | +time.sleep(0.50) |
| 47 | + |
| 48 | +# Create one object per VL53L0X passing the address to give to |
| 49 | +# each. |
| 50 | +tof = VL53L0X.VL53L0X(address=0x2B) |
| 51 | +tof1 = VL53L0X.VL53L0X(address=0x2D) |
| 52 | + |
| 53 | +# Set shutdown pin high for the first VL53L0X then |
| 54 | +# call to start ranging |
| 55 | +GPIO.output(sensor1_shutdown, GPIO.HIGH) |
| 56 | +time.sleep(0.50) |
| 57 | +tof.start_ranging(VL53L0X.VL53L0X_BETTER_ACCURACY_MODE) |
| 58 | + |
| 59 | +# Set shutdown pin high for the second VL53L0X then |
| 60 | +# call to start ranging |
| 61 | +GPIO.output(sensor2_shutdown, GPIO.HIGH) |
| 62 | +time.sleep(0.50) |
| 63 | +tof1.start_ranging(VL53L0X.VL53L0X_BETTER_ACCURACY_MODE) |
| 64 | + |
| 65 | +timing = tof.get_timing() |
| 66 | +if (timing < 20000): |
| 67 | + timing = 20000 |
| 68 | +print ("Timing %d ms" % (timing/1000)) |
| 69 | + |
| 70 | +for count in range(1,101): |
| 71 | + distance = tof.get_distance() |
| 72 | + if (distance > 0): |
| 73 | + print ("sensor %d - %d mm, %d cm, iteration %d" % (tof.my_object_number, distance, (distance/10), count)) |
| 74 | + else: |
| 75 | + print ("%d - Error" % tof.my_object_number) |
| 76 | + |
| 77 | + distance = tof1.get_distance() |
| 78 | + if (distance > 0): |
| 79 | + print ("sensor %d - %d mm, %d cm, iteration %d" % (tof1.my_object_number, distance, (distance/10), count)) |
| 80 | + else: |
| 81 | + print ("%d - Error" % tof.my_object_number) |
| 82 | + |
| 83 | + time.sleep(timing/1000000.00) |
| 84 | + |
| 85 | +tof1.stop_ranging() |
| 86 | +GPIO.output(sensor2_shutdown, GPIO.LOW) |
| 87 | +tof.stop_ranging() |
| 88 | +GPIO.output(sensor1_shutdown, GPIO.LOW) |
| 89 | + |
0 commit comments