-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtof.py
More file actions
40 lines (36 loc) · 1.23 KB
/
tof.py
File metadata and controls
40 lines (36 loc) · 1.23 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
import board
import busio
from adafruit_vl53l1x import VL53L1X
import logging
import threading
import time
class ToF:
"""
Class representing a Time of Flight (ToF) sensor.
"""
def __init__(self):
self.log = logging.getLogger(__name__)
i2c = busio.I2C(board.SCL, board.SDA)
self.vl53 = VL53L1X(i2c)
self.distance = 0
threading.Thread(target=self.get_tof_distance, daemon=True).start()
def get_tof_distance(self):
"""
Get the distance from the rear ToF sensor.
"""
self.vl53.stop_ranging() # Ensure sensor is stopped before starting
time.sleep(0.1) # Short delay to ensure sensor is ready
self.vl53.start_ranging()
while True:
try:
if self.vl53.data_ready:
distance = self.vl53.distance
if distance is None:
self.distance = 0 # en cm
else:
self.distance = distance # en cm
self.vl53.clear_interrupt()
time.sleep(0.05)
except Exception as e:
self.log.error(f"Error reading rear ToF sensor: {e}")
return None