Skip to content

Commit d4eaabc

Browse files
committed
ROI (Region Of Interest) support
1 parent 5e365bb commit d4eaabc

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

examples/roi.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python
2+
3+
import sys, signal
4+
sys.path.insert(0, "build/lib.linux-armv7l-2.7/")
5+
6+
import VL53L1X
7+
import time
8+
from datetime import datetime
9+
10+
tof = VL53L1X.VL53L1X(i2c_bus=1, i2c_address=0x29)
11+
print("Python: Initialized")
12+
tof.open()
13+
print("Python: Opened")
14+
15+
# Left, right, top and bottom are relative to the SPAD matrix coordinates,
16+
# which will be mirrored in real scene coordinates.
17+
# (or even rotated, depending on the VM53L1X element alignment on the board and on the board position)
18+
#
19+
# ROI in SPAD matrix coords:
20+
#
21+
# 15 top-left
22+
# | X____
23+
# | | |
24+
# | |____X
25+
# | bottom-right
26+
# 0__________15
27+
#
28+
29+
def scan(type="w"):
30+
if type == "w":
31+
# Wide scan forward ~30deg angle
32+
print "Scan: wide"
33+
return VL53L1X.VL53L1xUserRoi(0, 15, 15, 0)
34+
elif type == "c":
35+
# Focused scan forward
36+
print "Scan: center"
37+
return VL53L1X.VL53L1xUserRoi(6, 9, 9, 6)
38+
elif type == "t":
39+
# Focused scan top
40+
print "Scan: top"
41+
return VL53L1X.VL53L1xUserRoi(6, 15, 9, 12)
42+
elif type == "b":
43+
# Focused scan bottom
44+
print "Scan: bottom"
45+
return VL53L1X.VL53L1xUserRoi(6, 3, 9, 0)
46+
elif type == "l":
47+
# Focused scan left
48+
print "Scan: left"
49+
return VL53L1X.VL53L1xUserRoi(0, 9, 3, 6)
50+
elif type == "r":
51+
# Focused scan right
52+
print "Scan: right"
53+
return VL53L1X.VL53L1xUserRoi(12, 9, 15, 6)
54+
else:
55+
print("Scan: wide (default)")
56+
return VL53L1X.VL53L1xUserRoi(0, 15, 15, 0)
57+
58+
if len(sys.argv) == 2:
59+
roi = scan(sys.argv[1])
60+
else:
61+
roi = scan("default")
62+
63+
tof.set_user_roi(roi)
64+
65+
tof.start_ranging(1)
66+
67+
def exit_handler(signal, frame):
68+
tof.stop_ranging()
69+
tof.close()
70+
sys.exit(0)
71+
72+
signal.signal(signal.SIGINT, exit_handler)
73+
74+
while True:
75+
distance_mm = tof.get_distance()
76+
if distance_mm < 0:
77+
# Error -1185 may occur if you didn't stop ranging in a previous test
78+
print("Error: {}".format(distance_mm))
79+
else:
80+
print("Distance: {}cm".format(distance_mm/10))
81+
time.sleep(0.5)

python/VL53L1X.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ class VL53L1xDistanceMode:
3939
LONG = 3
4040

4141

42+
class VL53L1xUserRoi:
43+
def __init__(self, tlx=0, tly=0, brx=15, bry=15):
44+
self.top_left_x = tlx
45+
self.top_left_y = tly
46+
self.bot_right_x = brx
47+
self.bot_right_y = bry
48+
49+
4250
# Read/write function pointer types.
4351
_I2C_MULTI_FUNC = CFUNCTYPE(c_int, c_ubyte, c_ubyte)
4452
_I2C_READ_FUNC = CFUNCTYPE(c_int, c_ubyte, c_ubyte, POINTER(c_ubyte), c_ubyte)
@@ -150,6 +158,16 @@ def _i2c_multi(address, reg):
150158
self._i2c_write_func = _I2C_WRITE_FUNC(_i2c_write)
151159
_TOF_LIBRARY.VL53L1_set_i2c(self._i2c_multi_func, self._i2c_read_func, self._i2c_write_func)
152160

161+
# The ROI is a square or rectangle defined by two corners: top left and bottom right.
162+
# Default ROI is 16x16 (indices 0-15). The minimum ROI size is 4x4.
163+
def set_user_roi(self, user_roi):
164+
"""Set Region Of Interest (ROI)"""
165+
_TOF_LIBRARY.setUserRoi(self._dev,
166+
user_roi.top_left_x,
167+
user_roi.top_left_y,
168+
user_roi.bot_right_x,
169+
user_roi.bot_right_y)
170+
153171
def start_ranging(self, mode=VL53L1xDistanceMode.LONG):
154172
"""Start VL53L1X ToF Sensor Ranging"""
155173
_TOF_LIBRARY.startRanging(self._dev, mode)

python_lib/vl53l1x_python.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,25 @@ VL53L1_Error setDistanceMode(VL53L1_Dev_t *dev, int mode)
125125
return Status;
126126
}
127127

128+
/******************************************************************************
129+
* @brief Set User ROI (Region Of Interest)
130+
* @param userRoi - user ROI
131+
* @retval Error code, 0 for success.
132+
*****************************************************************************/
133+
VL53L1_Error setUserRoi(VL53L1_Dev_t *dev, int topLeftX, int topLeftY, int botRightX, int botRightY)
134+
{
135+
VL53L1_UserRoi_t userRoi;
136+
userRoi.TopLeftX = topLeftX;
137+
userRoi.TopLeftY = topLeftY;
138+
userRoi.BotRightX = botRightX;
139+
userRoi.BotRightY = botRightY;
140+
141+
VL53L1_Error Status = VL53L1_ERROR_NONE;
142+
Status = VL53L1_SetUserROI(dev, &userRoi);
143+
return Status;
144+
}
145+
146+
128147
/******************************************************************************
129148
* @brief Start Ranging
130149
* @param mode - ranging mode

0 commit comments

Comments
 (0)