37
37
38
38
* Adafruit CircuitPython firmware for the supported boards:
39
39
https://github.com/adafruit/circuitpython/releases
40
-
40
+
41
41
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
42
42
43
43
"""
52
52
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LIDARLite.git"
53
53
54
54
55
-
56
55
_ADDR_DEFAULT = const (0x62 )
56
+ _REG_ACQ_COMMAND = const (0x00 )
57
+ _CMD_RESET = const (0 )
58
+ _CMD_DISTANCENOBIAS = const (3 )
59
+ _CMD_DISTANCEWITHBIAS = const (4 )
60
+
57
61
CONFIG_DEFAULT = 0
58
62
CONFIG_SHORTFAST = 1
59
63
CONFIG_DEFAULTFAST = 2
60
64
CONFIG_MAXRANGE = 3
61
65
CONFIG_HIGHSENSITIVE = 4
62
66
CONFIG_LOWSENSITIVE = 5
63
- _REG_ACQ_COMMAND = const (0x00 )
64
- _CMD_RESET = const (0 )
65
- _CMD_DISTANCENOBIAS = const (3 )
66
- _CMD_DISTANCEWITHBIAS = const (4 )
67
67
68
68
STATUS_BUSY = 0x01
69
69
STATUS_REF_OVERFLOW = 0x02
73
73
STATUS_HEALTHY = 0x20
74
74
STATUS_SYS_ERROR = 0x40
75
75
76
-
77
76
# The various configuration register values, from arduino library
78
- _configurations = ((0x80 , 0x08 , 0x00 ), # default
79
- (0x1D , 0x08 , 0x00 ), # short range, high speed
80
- (0x80 , 0x00 , 0x00 ), # default range, higher speed short range
81
- (0xFF , 0x08 , 0x00 ), # maximum range
82
- (0x80 , 0x08 , 0x80 ), # high sensitivity & error
83
- (0x80 , 0x08 , 0xb0 )) # low sensitivity & error
77
+ _LIDAR_CONFIGS = ((0x80 , 0x08 , 0x00 ), # default
78
+ (0x1D , 0x08 , 0x00 ), # short range, high speed
79
+ (0x80 , 0x00 , 0x00 ), # default range, higher speed short range
80
+ (0xFF , 0x08 , 0x00 ), # maximum range
81
+ (0x80 , 0x08 , 0x80 ), # high sensitivity & error
82
+ (0x80 , 0x08 , 0xb0 )) # low sensitivity & error
83
+
84
84
class LIDARLite :
85
85
"""
86
86
A driver for the Garmin LIDAR Lite laser distance sensor.
@@ -89,15 +89,27 @@ class LIDARLite:
89
89
:param int address: (optional) The I2C address of the device to set after initialization.
90
90
"""
91
91
92
- def __init__ (self , i2c_bus , * , reset_pin = None , configuration = CONFIG_DEFAULT , address = _ADDR_DEFAULT ):
92
+ def __init__ (self , i2c_bus , * , reset_pin = None ,
93
+ configuration = CONFIG_DEFAULT , address = _ADDR_DEFAULT ):
94
+ """Initialize the hardware for the LIDAR over I2C. You can pass in an
95
+ optional reset_pin for when you call reset(). There are a few common
96
+ configurations Garmin suggests: CONFIG_DEFAULT, CONFIG_SHORTFAST,
97
+ CONFIG_DEFAULTFAST, CONFIG_MAXRANGE, CONFIG_HIGHSENSITIVE, and
98
+ CONFIG_LOWSENSITIVE. For the I2C address, the default is 0x62 but if you
99
+ pass a different number in, we'll try to change the address so multiple
100
+ LIDARs can be connected. (Note all but one need to be in reset for this
101
+ to work!)"""
93
102
self .i2c_device = I2CDevice (i2c_bus , address )
94
103
self ._buf = bytearray (2 )
95
104
self ._bias_count = 0
96
105
self ._reset = reset_pin
97
106
time .sleep (0.5 )
98
107
self .configure (configuration )
99
-
108
+ self ._status = self .status
109
+
100
110
def reset (self ):
111
+ """Hardware reset (if pin passed into init) or software reset. Will take
112
+ 100 readings in order to 'flush' measurement unit, otherwise data is off."""
101
113
# Optional hardware reset pin
102
114
if self ._reset is not None :
103
115
self ._reset .direction = Direction .OUTPUT
@@ -119,54 +131,61 @@ def reset(self):
119
131
pass
120
132
121
133
def configure (self , config ):
122
- settings = _configurations [config ]
134
+ """Set the LIDAR desired style of measurement. There are a few common
135
+ configurations Garmin suggests: CONFIG_DEFAULT, CONFIG_SHORTFAST,
136
+ CONFIG_DEFAULTFAST, CONFIG_MAXRANGE, CONFIG_HIGHSENSITIVE, and
137
+ CONFIG_LOWSENSITIVE."""
138
+ settings = _LIDAR_CONFIGS [config ]
123
139
self ._write_reg (0x02 , settings [0 ])
124
140
self ._write_reg (0x04 , settings [1 ])
125
141
self ._write_reg (0x1c , settings [2 ])
126
142
127
143
def read_distance (self , bias = False ):
144
+ """Perform a distance reading with or without 'bias'. It's recommended
145
+ to take a bias measurement every 100 non-bias readings (they're slower)"""
128
146
if bias :
129
147
self ._write_reg (_REG_ACQ_COMMAND , _CMD_DISTANCEWITHBIAS )
130
148
else :
131
149
self ._write_reg (_REG_ACQ_COMMAND , _CMD_DISTANCENOBIAS )
132
- d = self ._read_reg (0x8F , 2 )
150
+ dist = self ._read_reg (0x8F , 2 )
133
151
if self ._status & (STATUS_NO_PEAK | STATUS_SECOND_RETURN ):
134
152
raise RuntimeError ("Measurement failure" )
135
153
if (self ._status & STATUS_SYS_ERROR ) or (not self ._status & STATUS_HEALTHY ):
136
154
raise RuntimeError ("System failure" )
137
- dist = d [0 ] << 8 | d [1 ]
138
- return dist
155
+ return dist [0 ] << 8 | dist [1 ]
139
156
140
157
@property
141
158
def distance (self ):
159
+ """The measured distance in cm. Will take a bias reading every 100 calls"""
142
160
self ._bias_count -= 1
143
161
if self ._bias_count < 0 :
144
162
self ._bias_count = 100 # every 100 reads, check bias
145
163
return self .read_distance (self ._bias_count <= 0 )
146
-
164
+
147
165
@property
148
166
def status (self ):
167
+ """The status byte, check datasheet for bitmask"""
149
168
buf = bytearray ([0x1 ])
150
169
with self .i2c_device as i2c :
151
170
i2c .write_then_readinto (buf , buf )
152
171
return buf [0 ]
153
172
154
- def _write_reg (self , reg , value ):
173
+ def _write_reg (self , reg , value ):
155
174
self ._buf [0 ] = reg
156
175
self ._buf [1 ] = value
157
176
with self .i2c_device as i2c :
158
177
#print("Writing: ", [hex(i) for i in self._buf])
159
178
i2c .write (self ._buf )
160
179
time .sleep (0.001 ) # there's a delay in arduino library
161
180
162
- def _read_reg (self , reg , len ):
181
+ def _read_reg (self , reg , num ):
163
182
while True :
164
183
self ._status = self .status
165
184
if not self ._status & STATUS_BUSY :
166
185
break
167
186
# no longer busy
168
187
self ._buf [0 ] = reg
169
188
with self .i2c_device as i2c :
170
- i2c .write_then_readinto (self ._buf , self ._buf , out_end = 1 , in_end = len )
189
+ i2c .write_then_readinto (self ._buf , self ._buf , out_end = 1 , in_end = num )
171
190
#print("Read from ", hex(reg), [hex(i) for i in self._buf])
172
191
return self ._buf
0 commit comments