-
Notifications
You must be signed in to change notification settings - Fork 1
/
UpdateTOF.py
56 lines (42 loc) · 1.65 KB
/
UpdateTOF.py
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
"""Houses the UpdateTOF task class.
"""
from task_share import Share
import adafruit_vl53l1x
from machine import I2C
class UpdateTOF:
def __init__(self, s_tofdistance: Share, i2c):
"""Updates an dafruit_vl53l1x Time of Flight sensor position.
Args:
s_tofposition (task_share.Share): Conveys the Time of Flight position, reported in millimeters.
i2c (machine.I2C): Pre-initialized I2C object, designating the i2c bus used.
"""
self._tofdistance = s_tofdistance
self._tofdistance.put(400) # start with a long distance on initialization
self._distance = 400
self._i2c = i2c
self.vl53 = adafruit_vl53l1x.VL53L1X(self._i2c)
self.vl53.distance_mode = 1 # short range = 1, long range = 2
self.vl53.timing_budget = 20 # ms
#init state
self._state = 0
def run(self):
"""Implementation as a generator function.
Yields:
int: Machine state.
"""
while True:
# immediately go to run state after tof initialization
if self._state == 0:
self.vl53.start_ranging()
self.vl53.clear_interrupt()
self._state = 1
# get the position and put it in the share
elif self._state == 1:
try:
self._distance = int(self.vl53.distance)
self._tofdistance.put(self._distance)
except TypeError:
self._tofdistance.put(self._distance)
self.vl53.clear_interrupt()
#gc.collect()
yield self._state