Some improvements

This commit is contained in:
Maximilian Giller 2023-10-09 18:39:47 +02:00
parent a8cf4068db
commit 83ee05d300
2 changed files with 22 additions and 16 deletions

View file

@ -11,6 +11,7 @@ logging.getLogger().setLevel(logging.INFO)
sensor = VL53L1XSensor() sensor = VL53L1XSensor()
sensor.open() sensor.open()
try:
while True: while True:
sensor.setDirection(Directions.INSIDE) sensor.setDirection(Directions.INSIDE)
distance_inside = sensor.getDistance() distance_inside = sensor.getDistance()
@ -24,3 +25,6 @@ while True:
sleep(DELAY_SECONDS) sleep(DELAY_SECONDS)
finally:
sensor.close()

View file

@ -25,6 +25,7 @@ class VL53L1XSensor(ToFSensor):
def open(self, ranging_mode: int = 2) -> None: def open(self, ranging_mode: int = 2) -> None:
self.sensor = VL53L1X.VL53L1X(i2c_bus=1, i2c_address=0x29) self.sensor = VL53L1X.VL53L1X(i2c_bus=1, i2c_address=0x29)
self.sensor.open() self.sensor.open()
self.roi = VL53L1X.VL53L1xUserRoi()
# Optionally set an explicit timing budget # Optionally set an explicit timing budget
# These values are measurement time in microseconds, # These values are measurement time in microseconds,
@ -45,15 +46,16 @@ class VL53L1XSensor(ToFSensor):
Directions.OUTSIDE: VL53L1X.VL53L1xUserRoi(6, 15, 9, 12), Directions.OUTSIDE: VL53L1X.VL53L1xUserRoi(6, 15, 9, 12),
} }
roi = direction_roi[direction] self.roi = direction_roi[direction]
self.sensor.stop_ranging()
self.sensor.set_user_roi(roi)
self.sensor.start_ranging(self.ranging)
def getDistance(self) -> float: def getDistance(self) -> float:
"""Returns new distance in cm.""" """Returns new distance in cm."""
self.sensor.set_user_roi(self.roi)
self.sensor.set_timing(66000, 70)
self.sensor.start_ranging(self.ranging)
distance = self.sensor.get_distance() distance = self.sensor.get_distance()
self.sensor.stop_ranging()
return distance / 10 return distance / 10