Merge branch 'master' of code.giller.dev:m.giller/mash-sensor-tof-pc

This commit is contained in:
Maximilian Giller 2024-02-14 23:55:07 +01:00
commit eee9e2e85c
4 changed files with 45 additions and 5 deletions

View file

@ -47,11 +47,11 @@ In the photos below you can see my current setup. The piece of cardboard is supp
The piece of cardboard is on the side of my room, which might help you to orient the sensor properly.
<p align="left">
<img height="200px" src="/images/door.png" />
<img height="200px" src="https://code.giller.dev/m.giller/mash-sensor-tof-pc/raw/branch/master/images/door.png" />
<br/>
<img height="200px" src="/images/controller.jpeg" />
<img height="200px" src="https://code.giller.dev/m.giller/mash-sensor-tof-pc/raw/branch/master/images/controller.jpeg" />
<br/>
<img height="200px" src="/images/sensor.jpeg" />
<img height="200px" src="https://code.giller.dev/m.giller/mash-sensor-tof-pc/raw/branch/master/images/sensor.jpeg" />
</p>
Sadly, due to my limited time, there is so far no proper, easy way of changing the direction of the sensor in software. It is on my todo list tho (see below) and please don't hesistate to open issue if you require a certain feature! I am more than happy to help and expand this project.

View file

@ -6,6 +6,7 @@ import threading
COUNTING_CB = "counting"
TRIGGER_CB = "trigger"
CHANGE_CB = "changes"
MEASUREMENT_CB = "measurement"
START_TIME = "start_time"
END_TIME = "end_time"
TRIGGER_DISTANCES = "trigger_distances"
@ -15,9 +16,15 @@ END_DISTANCE = "end_distance"
class PeopleCounter:
def __init__(self, sensor: ToFSensor, maxTriggerDistanceInCm: int = 90) -> None:
self.sensor = sensor
self.callbacks = {COUNTING_CB: [], TRIGGER_CB: [], CHANGE_CB: []}
self.callbacks = {COUNTING_CB: [], TRIGGER_CB: [], CHANGE_CB: [], MEASUREMENT_CB: []}
self.maxTriggerDistance = maxTriggerDistanceInCm
def hookMeasurement(self, cb) -> None:
self.callbacks[MEASUREMENT_CB].append(cb)
def unhookMeasurement(self, cb) -> None:
self.callbacks[MEASUREMENT_CB].remove(cb)
def hookCounting(self, cb) -> None:
self.callbacks[COUNTING_CB].append(cb)
@ -54,6 +61,9 @@ class PeopleCounter:
distance: float = self.sensor.getDistance()
changed: bool = self.updateState(direction, distance)
th = threading.Thread(target=self.handleMeasurementCallbacks, args=(direction, distance))
th.start()
if changed:
countChange: int = self.getCountChange(self.directionState)
@ -133,6 +143,10 @@ class PeopleCounter:
#! TODO: Should be based on the distance from the ground, not from the sensor
return distance <= self.maxTriggerDistance
def handleMeasurementCallbacks(self, direction: Directions, distance: float) -> None:
for cb in self.callbacks[MEASUREMENT_CB]:
cb(direction, distance)
def handleCallbacks(self, countChange: int):
self.handleChangeCallbacks(countChange)
self.handleCountingCallbacks(countChange)

View file

@ -1,9 +1,10 @@
from sensors import VL53L1XSensor, VL53L3CXSensor, PeopleCounter
from statistics.debug_logging import register_debug_logger
from datetime import time
import logging
LOG_FILE_PATH = "log.txt" # Path for logs
logging.getLogger().setLevel(logging.INFO)
logging.getLogger().setLevel(logging.DEBUG)
# If the distance (in cm) is lower or equal to this value, the people counter will trigger
MAX_TRIGGER_DISTANCE = 130
@ -32,3 +33,5 @@ hue_conf = {
sensor = VL53L1XSensor()
counter: PeopleCounter = PeopleCounter(sensor, MAX_TRIGGER_DISTANCE) # Sensor object
register_debug_logger(counter)

View file

@ -0,0 +1,23 @@
from ..sensors.tof_sensor import Directions
from ..sensors.people_counter import PeopleCounter
import logging
import json
def debug_log_change(countChange: int, directionState: dict) -> None:
json_state = json.dumps(directionState)
logging.debug(f"CHANGE;Count Change;{str(countChange)};Direction State;{json_state};")
def debug_log_trigger(triggerState: dict) -> None:
logging.debug(f"TRIGGER;Inside Triggered;{str(triggerState[Directions.INSIDE])};Outside Triggered;{str(triggerState[Directions.OUTSIDE])};")
def debug_log_counting(countChange: int) -> None:
logging.debug(f"COUNTING;Count Change;{str(countChange)};")
def debug_log_measurement(direction: Directions, distance: float) -> None:
logging.debug(f"MEASUREMENT;Direction;{str(direction)};Distance;{str(distance)};")
def register_debug_logger(counter: PeopleCounter) -> None:
counter.hookChange(debug_log_change)
counter.hookCounting(debug_log_counting)
counter.hookTrigger(debug_log_trigger)
counter.hookMeasurement(debug_log_measurement)