-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathVL53L0X.py
More file actions
executable file
·189 lines (154 loc) · 6.71 KB
/
Copy pathVL53L0X.py
File metadata and controls
executable file
·189 lines (154 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/python
# MIT License
#
# Copyright (c) 2017 John Bryan Moore
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
from ctypes import (CDLL, CFUNCTYPE, POINTER,
c_int, pointer, c_ubyte, c_uint8, c_uint32)
import smbus2 as smbus
THIS_FILE_DIR = os.path.dirname(os.path.abspath(__file__))
class Vl53l0xError(RuntimeError):
pass
class Vl53l0xAccuracyMode:
GOOD = 0 # 33 ms timing budget 1.2m range
BETTER = 1 # 66 ms timing budget 1.2m range
BEST = 2 # 200 ms 1.2m range
LONG_RANGE = 3 # 33 ms timing budget 2m range
HIGH_SPEED = 4 # 20 ms timing budget 1.2m range
class Vl53l0xDeviceMode:
SINGLE_RANGING = 0
CONTINUOUS_RANGING = 1
SINGLE_HISTOGRAM = 2
CONTINUOUS_TIMED_RANGING = 3
SINGLE_ALS = 10
GPIO_DRIVE = 20
GPIO_OSC = 21
class Vl53l0xGpioAlarmType:
OFF = 0
THRESHOLD_CROSSED_LOW = 1
THRESHOLD_CROSSED_HIGH = 2
THRESHOLD_CROSSED_OUT = 3
NEW_MEASUREMENT_READY = 4
class Vl53l0xInterruptPolarity:
LOW = 0
HIGH = 1
# Read/write function pointer types.
_I2C_READ_FUNC = CFUNCTYPE(c_int, c_ubyte, c_ubyte, POINTER(c_ubyte), c_ubyte)
_I2C_WRITE_FUNC = CFUNCTYPE(c_int, c_ubyte, c_ubyte, POINTER(c_ubyte), c_ubyte)
# Load VL53L0X shared lib
# If built locally it will be in bin folder, if installed it will be next to the this file.
_POSSIBLE_LIBRARY_LOCATIONS = [THIS_FILE_DIR, os.path.join(THIS_FILE_DIR, '..', 'bin')]
for lib_location in _POSSIBLE_LIBRARY_LOCATIONS:
tof_lib = os.path.join(lib_location, "vl53l0x_python.so")
if os.path.isfile(tof_lib):
_TOF_LIBRARY = CDLL(tof_lib)
break
else:
raise FileNotFoundError('Could not find vl53l0x_python.so (have you built it?)')
class VL53L0X:
"""VL53L0X ToF."""
def __init__(self, i2c_bus=1, i2c_address=0x29, tca9548a_num=255, tca9548a_addr=0):
"""Initialize the VL53L0X ToF Sensor from ST"""
self._i2c_bus = i2c_bus
self.i2c_address = i2c_address
self._tca9548a_num = tca9548a_num
self._tca9548a_addr = tca9548a_addr
self._i2c = smbus.SMBus()
self._dev = None
def open(self):
self._i2c.open(bus=self._i2c_bus)
self._configure_i2c_library_functions()
self._dev = _TOF_LIBRARY.initialise(self.i2c_address, self._tca9548a_num, self._tca9548a_addr)
def close(self):
self._i2c.close()
self._dev = None
def _configure_i2c_library_functions(self):
# I2C bus read callback for low level library.
def _i2c_read(address, reg, data_p, length):
ret_val = 0
result = []
try:
result = self._i2c.read_i2c_block_data(address, reg, length)
except IOError:
ret_val = -1
if ret_val == 0:
for index in range(length):
data_p[index] = result[index]
return ret_val
# I2C bus write callback for low level library.
def _i2c_write(address, reg, data_p, length):
ret_val = 0
data = []
for index in range(length):
data.append(data_p[index])
try:
self._i2c.write_i2c_block_data(address, reg, data)
except IOError:
ret_val = -1
return ret_val
# Pass i2c read/write function pointers to VL53L0X library.
self._i2c_read_func = _I2C_READ_FUNC(_i2c_read)
self._i2c_write_func = _I2C_WRITE_FUNC(_i2c_write)
_TOF_LIBRARY.VL53L0X_set_i2c(self._i2c_read_func, self._i2c_write_func)
def start_ranging(self, mode=Vl53l0xAccuracyMode.GOOD):
"""Start VL53L0X ToF Sensor Ranging"""
_TOF_LIBRARY.startRanging(self._dev, mode)
def stop_ranging(self):
"""Stop VL53L0X ToF Sensor Ranging"""
_TOF_LIBRARY.stopRanging(self._dev)
def get_distance(self):
"""Get distance from VL53L0X ToF Sensor"""
return _TOF_LIBRARY.getDistance(self._dev)
# This function included to show how to access the ST library directly
# from python instead of through the simplified interface
def get_timing(self):
budget = c_uint32(0)
budget_p = pointer(budget)
status = _TOF_LIBRARY.VL53L0X_GetMeasurementTimingBudgetMicroSeconds(self._dev, budget_p)
if status == 0:
return budget.value + 1000
else:
return 0
def configure_gpio_interrupt(
self, proximity_alarm_type=Vl53l0xGpioAlarmType.THRESHOLD_CROSSED_LOW,
interrupt_polarity=Vl53l0xInterruptPolarity.HIGH, threshold_low_mm=250, threshold_high_mm=500):
"""
Configures a GPIO interrupt from device, be sure to call "clear_interrupt" after interrupt is processed.
"""
pin = c_uint8(0) # 0 is only GPIO pin.
device_mode = c_uint8(Vl53l0xDeviceMode.CONTINUOUS_RANGING)
functionality = c_uint8(proximity_alarm_type)
polarity = c_uint8(interrupt_polarity)
status = _TOF_LIBRARY.VL53L0X_SetGpioConfig(self._dev, pin, device_mode, functionality, polarity)
if status != 0:
raise Vl53l0xError('Error setting VL53L0X GPIO config')
threshold_low = c_uint32(threshold_low_mm << 16)
threshold_high = c_uint32(threshold_high_mm << 16)
status = _TOF_LIBRARY.VL53L0X_SetInterruptThresholds(self._dev, device_mode, threshold_low, threshold_high)
if status != 0:
raise Vl53l0xError('Error setting VL53L0X thresholds')
# Ensure any pending interrupts are cleared.
self.clear_interrupt()
def clear_interrupt(self):
mask = c_uint32(0)
status = _TOF_LIBRARY.VL53L0X_ClearInterruptMask(self._dev, mask)
if status != 0:
raise Vl53l0xError('Error clearing VL53L0X interrupt')