Skip to content

Commit c48f93e

Browse files
committed
working single lidar sensor, add example boot config, remove transformations
1 parent c4c9442 commit c48f93e

File tree

5 files changed

+99
-199
lines changed

5 files changed

+99
-199
lines changed

examples/vl53l5cx_py_basic.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ def main():
2828
sensor.start_ranging()
2929

3030
while True:
31-
x, y, z = sensor.get_range()
32-
print(f"X: {x:.3f}, Y: {y:.3f}, Z: {z:.3f}")
31+
results = sensor.get_range()
32+
dist = results["distance_mm"][0]
33+
print(f"py results distance_mm[0]: {dist}")
3334

3435
if __name__=="__main__":
3536
res = main()

vl53l5cx_py/config.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# arena-robot vl53l5cx_py
2+
dtparam=i2c_arm=on,i2c_arm_baudrate=1000000
3+
dtoverlay=i2c4,baudrate=1000000,pins_6_7
4+
dtoverlay=i2c5,baudrate=1000000,pins_12_13

vl53l5cx_py/cython/vl53l5cx_py_wrapper.pyx

+71-22
Original file line numberDiff line numberDiff line change
@@ -9,67 +9,116 @@ This source code is licensed under the BSD-3-Clause license found in the
99
LICENSE file in the root directory of this source tree.
1010
"""
1111

12-
from libc.stdint cimport uint8_t, uint16_t, int32_t
12+
import atexit
13+
from libc.stdint cimport int32_t, uint8_t, uint16_t
1314
import numpy as np
1415
cimport numpy as np
1516

1617
cdef extern from "../VL53L5CX_Linux_driver_1.1.2/user/uld-driver/inc/vl53l5cx_api.h":
18+
enum: VL53L5CX_DEFAULT_I2C_ADDRESS
19+
enum: VL53L5CX_RESOLUTION_8X8
20+
enum: VL53L5CX_NB_TARGET_PER_ZONE
1721
ctypedef struct VL53L5CX_Configuration:
1822
pass
19-
cdef enum:
20-
VL53L5CX_DEFAULT_I2C_ADDRESS
23+
# ctypedef struct motion_indicator_t:
24+
# uint32_t global_indicator_1
25+
# uint32_t global_indicator_2
26+
# uint8_t status
27+
# uint8_t nb_of_detected_aggregates
28+
# uint8_t nb_of_aggregates
29+
# uint8_t spare
30+
# uint32_t motion[32]
31+
ctypedef struct VL53L5CX_ResultsData:
32+
#ifndef VL53L5CX_DISABLE_AMBIENT_PER_SPAD
33+
np.uint32_t ambient_per_spad[VL53L5CX_RESOLUTION_8X8]
34+
#endif
2135

22-
cdef extern from "../include/vl53l5cx_py.h":
23-
ctypedef struct XYZ_ZoneCoordinates_t:
24-
double Xpos[64]
25-
double Ypos[64]
26-
double Zpos[64]
36+
#ifndef VL53L5CX_DISABLE_NB_TARGET_DETECTED
37+
np.uint8_t nb_target_detected[VL53L5CX_RESOLUTION_8X8]
38+
#endif
39+
40+
#ifndef VL53L5CX_DISABLE_NB_SPADS_ENABLED
41+
np.uint32_t nb_spads_enabled[VL53L5CX_RESOLUTION_8X8]
42+
#endif
43+
44+
#ifndef VL53L5CX_DISABLE_SIGNAL_PER_SPAD
45+
np.uint32_t signal_per_spad[(VL53L5CX_RESOLUTION_8X8
46+
*VL53L5CX_NB_TARGET_PER_ZONE)]
47+
#endif
48+
49+
#ifndef VL53L5CX_DISABLE_RANGE_SIGMA_MM
50+
np.uint16_t range_sigma_mm[(VL53L5CX_RESOLUTION_8X8
51+
*VL53L5CX_NB_TARGET_PER_ZONE)]
52+
#endif
53+
54+
#ifndef VL53L5CX_DISABLE_DISTANCE_MM
55+
np.int16_t distance_mm[(VL53L5CX_RESOLUTION_8X8
56+
*VL53L5CX_NB_TARGET_PER_ZONE)]
57+
#endif
2758

59+
#ifndef VL53L5CX_DISABLE_REFLECTANCE_PERCENT
60+
np.uint8_t reflectance[(VL53L5CX_RESOLUTION_8X8
61+
*VL53L5CX_NB_TARGET_PER_ZONE)]
62+
#endif
63+
64+
#ifndef VL53L5CX_DISABLE_TARGET_STATUS
65+
np.uint8_t target_status[(VL53L5CX_RESOLUTION_8X8
66+
*VL53L5CX_NB_TARGET_PER_ZONE)]
67+
#endif
68+
69+
#ifndef VL53L5CX_DISABLE_MOTION_INDICATOR
70+
# motion_indicator_t motion_indicator
71+
#endif
72+
73+
cdef extern from "../include/vl53l5cx_py.h":
2874
int32_t vl53l5cx_py_init(VL53L5CX_Configuration* dev_conf,
2975
const char *dev_path, uint16_t target_addr,
3076
uint8_t freq)
3177
int32_t vl53l5cx_py_close(VL53L5CX_Configuration* dev_conf)
3278
int32_t vl53l5cx_py_start_ranging(VL53L5CX_Configuration* dev_conf)
3379
int32_t vl53l5cx_py_stop_ranging(VL53L5CX_Configuration* dev_conf)
3480
int32_t vl53l5cx_py_get_range(VL53L5CX_Configuration* dev_conf,
35-
uint8_t transform,
36-
XYZ_ZoneCoordinates_t XYZ_Coordinates)
81+
VL53L5CX_ResultsData* results)
3782

38-
cpdef VL53L5CX_PY_DEFAULT_ADDR = VL53L5CX_DEFAULT_I2C_ADDRESS
39-
cpdef VL53L5CX_PY_DEFAULT_FREQ = 15
40-
cpdef VL53L5CX_PY_DEFAULT_TRANSFORM = 0
83+
cdef VL53L5CX_PY_DEFAULT_ADDR = VL53L5CX_DEFAULT_I2C_ADDRESS
84+
cdef VL53L5CX_PY_DEFAULT_FREQ = 15
4185

4286
cdef class VL53L5CX:
4387
cdef VL53L5CX_Configuration dev_conf
44-
cdef uint8_t transform
88+
cdef bint ranging
4589

4690
def __cinit__(self, dev_path: str,
4791
target_addr: np.uint16_t = VL53L5CX_PY_DEFAULT_ADDR,
48-
freq: np.uint8_t = VL53L5CX_PY_DEFAULT_FREQ,
49-
transform: np.uint8_t = VL53L5CX_PY_DEFAULT_TRANSFORM) -> None:
50-
self.transform = transform
92+
freq: np.uint8_t = VL53L5CX_PY_DEFAULT_FREQ) -> None:
5193
cdef int32_t status = vl53l5cx_py_init(&self.dev_conf, dev_path.encode(), target_addr, freq)
5294
if status != 0:
5395
raise RuntimeError
96+
self.ranging = False
97+
atexit.register(self.__del__)
5498

55-
def __dealloc__(self) -> None:
99+
def __del__(self) -> None:
100+
if self.ranging:
101+
self.stop_ranging()
56102
cdef int32_t status = vl53l5cx_py_close(&self.dev_conf)
57103
if status != 0:
58104
raise RuntimeError
105+
atexit.unregister(self.__del__)
59106

60107
cpdef void start_ranging(self):
61108
cdef int32_t status = vl53l5cx_py_start_ranging(&self.dev_conf)
62109
if status != 0:
63110
raise RuntimeError
111+
self.ranging = True
64112

65113
cpdef void stop_ranging(self):
66114
cdef int32_t status = vl53l5cx_py_stop_ranging(&self.dev_conf)
67115
if status != 0:
68116
raise RuntimeError
117+
self.ranging = False
69118

70-
cpdef tuple get_range(self):
71-
cdef XYZ_ZoneCoordinates_t XYZ_Coordinates
72-
cdef int32_t status = vl53l5cx_py_get_range(&self.dev_conf, self.transform, XYZ_Coordinates)
119+
cpdef VL53L5CX_ResultsData get_range(self):
120+
cdef VL53L5CX_ResultsData results
121+
cdef int32_t status = vl53l5cx_py_get_range(&self.dev_conf, &results)
73122
if status != 0:
74123
raise RuntimeError
75-
return (XYZ_Coordinates.Xpos, XYZ_Coordinates.Ypos, XYZ_Coordinates.Zpos)
124+
return results

vl53l5cx_py/include/vl53l5cx_py.h

+1-14
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,13 @@
1919
extern "C" {
2020
#endif
2121

22-
typedef struct {
23-
double Xpos[64];
24-
double Ypos[64];
25-
double Zpos[64];
26-
} XYZ_ZoneCoordinates_t;
27-
28-
typedef enum XYZ_Coordinates_Transform_t {
29-
XYZ_COORDINATES_TRANSFORM_FORWARD = 0,
30-
XYZ_COORDINATES_TRANSFORM_LEFT = 1,
31-
XYZ_COORDINATES_TRANSFORM_RIGHT = 2,
32-
} XYZ_Coordinates_Transform_t;
33-
3422
int32_t vl53l5cx_py_init(VL53L5CX_Configuration* dev_conf, const char *dev_path,
3523
uint16_t target_addr, uint8_t freq);
3624
int32_t vl53l5cx_py_close(VL53L5CX_Configuration* dev_conf);
3725
int32_t vl53l5cx_py_start_ranging(VL53L5CX_Configuration* dev_conf);
3826
int32_t vl53l5cx_py_stop_ranging(VL53L5CX_Configuration* dev_conf);
3927
int32_t vl53l5cx_py_get_range(VL53L5CX_Configuration* dev_conf,
40-
uint8_t transform,
41-
XYZ_ZoneCoordinates_t XYZ_Coordinates);
28+
VL53L5CX_ResultsData* results);
4229

4330
#ifdef __cplusplus
4431
} // extern "C"

0 commit comments

Comments
 (0)