diff --git a/src/measurer.py b/src/measurer.py index 6ec6609..57fb10f 100644 --- a/src/measurer.py +++ b/src/measurer.py @@ -11,16 +11,20 @@ logging.getLogger().setLevel(logging.INFO) sensor = VL53L1XSensor() sensor.open() -while True: - sensor.setDirection(Directions.INSIDE) - distance_inside = sensor.getDistance() - - sensor.setDirection(Directions.OUTSIDE) - distance_outside = sensor.getDistance() - - logging.info("----------") - logging.info(f"Inside: {distance_inside} cm") - logging.info(f"Outside: {distance_outside} cm") - - sleep(DELAY_SECONDS) +try: + while True: + sensor.setDirection(Directions.INSIDE) + distance_inside = sensor.getDistance() + + sensor.setDirection(Directions.OUTSIDE) + distance_outside = sensor.getDistance() + + logging.info("----------") + logging.info(f"Inside: {distance_inside} cm") + logging.info(f"Outside: {distance_outside} cm") + + sleep(DELAY_SECONDS) + +finally: + sensor.close() diff --git a/src/sensors/vl53l1x_sensor.py b/src/sensors/vl53l1x_sensor.py index 89d1843..86bdf82 100644 --- a/src/sensors/vl53l1x_sensor.py +++ b/src/sensors/vl53l1x_sensor.py @@ -25,6 +25,7 @@ class VL53L1XSensor(ToFSensor): def open(self, ranging_mode: int = 2) -> None: self.sensor = VL53L1X.VL53L1X(i2c_bus=1, i2c_address=0x29) self.sensor.open() + self.roi = VL53L1X.VL53L1xUserRoi() # Optionally set an explicit timing budget # These values are measurement time in microseconds, @@ -45,15 +46,16 @@ class VL53L1XSensor(ToFSensor): 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: """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() + self.sensor.stop_ranging() return distance / 10