From f92a25ddd6ea3571a47b4a8b34f4aab45fa824b7 Mon Sep 17 00:00:00 2001 From: Kai Bepperling Date: Thu, 30 Dec 2021 14:16:06 +0100 Subject: [PATCH 1/2] Fixing ROI settings --- components/roode/__init__.py | 52 +++++++++++++++++++++++++++--------- components/roode/roode.h | 2 ++ peopleCounter.yaml | 14 +++++----- peopleCounter32.yaml | 14 +++++----- peopleCounterDev.yaml | 14 +++++----- 5 files changed, 66 insertions(+), 30 deletions(-) diff --git a/components/roode/__init__.py b/components/roode/__init__.py index db16f034..cab30ca1 100644 --- a/components/roode/__init__.py +++ b/components/roode/__init__.py @@ -2,7 +2,13 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import sensor -from esphome.const import CONF_ID, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT, UNIT_EMPTY, UNIT_METER +from esphome.const import ( + CONF_ID, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, + UNIT_EMPTY, + UNIT_METER, +) # DEPENDENCIES = ["i2c"] @@ -32,6 +38,8 @@ CONF_CALIBRATION_ACTIVE = "calibration_active" CONF_TIMING_BUDGET = "timing_budget" CONF_USE_SAMPLING = "use_sampling" +CONF_ROI = "roi" +CONF_ROI_ACTIVE = "roi_active" TYPES = [ CONF_RESTORE_VALUES, CONF_INVERT_DIRECTION, @@ -78,16 +86,6 @@ "manual_mode", f"{CONF_SENSOR_MODE}, {CONF_ROI_HEIGHT}, {CONF_ROI_WIDTH} and {CONF_MANUAL_THRESHOLD} must be used together", ): cv.int_range(min=-1, max=3), - cv.Inclusive( - CONF_ROI_HEIGHT, - "manual_mode", - f"{CONF_SENSOR_MODE}, {CONF_ROI_HEIGHT}, {CONF_ROI_WIDTH} and {CONF_MANUAL_THRESHOLD} must be used together", - ): cv.int_range(min=4, max=16), - cv.Inclusive( - CONF_ROI_WIDTH, - "manual_mode", - f"{CONF_SENSOR_MODE}, {CONF_ROI_HEIGHT}, {CONF_ROI_WIDTH} and {CONF_MANUAL_THRESHOLD} must be used together", - ): cv.int_range(min=4, max=16), cv.Inclusive( CONF_MANUAL_THRESHOLD, "manual_mode", @@ -95,10 +93,32 @@ ): cv.int_range(min=40, max=4000), } ), + cv.Optional(CONF_ROI): cv.Schema( + { + cv.Optional(CONF_ROI_ACTIVE, default="true"): cv.boolean, + cv.Optional(CONF_ROI_HEIGHT, default=16): cv.int_range(min=4, max=16), + cv.Optional(CONF_ROI_WIDTH, default=6): cv.int_range(min=4, max=16), + } + ), } ).extend(cv.polling_component_schema("100ms")) +def validate_roi_settings(config): + roi = config.get(CONF_ROI) + manual = config.get(CONF_MANUAL) + if CONF_CALIBRATION in config: + roi_calibration = config.get(CONF_CALIBRATION).get(CONF_ROI_CALIBRATION) + if roi_calibration == True and roi != None: + raise cv.Invalid( + "ROI calibration cannot be used with manual ROI width and height" + ) + if roi_calibration == False and roi == None: + raise cv.Invalid("You need to set the ROI manually or use ROI calibration") + if manual != None and roi == None: + raise cv.Invalid("You need to set the ROI manually if manual mode is active") + + async def setup_conf(config, key, hub): if key in config: cg.add(getattr(hub, f"set_{key}")(config[key])) @@ -116,12 +136,20 @@ def setup_calibration_mode(config, hub): cg.add(getattr(hub, f"set_{key}")(calibration[key])) +def setup_manual_roi(config, hub): + roi = config[CONF_ROI] + for key in roi: + cg.add(getattr(hub, f"set_{key}")(roi[key])) + + async def to_code(config): hub = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(hub, config) cg.add_library("EEPROM", None) cg.add_library("Wire", None) - cg.add_library("pololu", "1.3.0", "VL53L1X") + + validate_roi_settings(config) + for key in TYPES: await setup_conf(config, key, hub) if CONF_MANUAL in config: diff --git a/components/roode/roode.h b/components/roode/roode.h index 72187ad4..353b06fe 100644 --- a/components/roode/roode.h +++ b/components/roode/roode.h @@ -58,6 +58,7 @@ namespace esphome void set_calibration_active(bool val) { calibration_active_ = val; } void set_manual_active(bool val) { manual_active_ = val; } + void set_roi_active(bool val) { roi_active_ = val; } void set_roi_calibration(bool val) { roi_calibration_ = val; } void set_timing_budget(int timing_budget) { timing_budget_ = timing_budget; } void set_manual_threshold(int val) { manual_threshold_ = val; } @@ -121,6 +122,7 @@ namespace esphome int getSum(int *values, int size); bool calibration_active_{false}; bool manual_active_{false}; + bool roi_active_{false}; bool roi_calibration_{false}; int sensor_mode{-1}; bool advised_sensor_orientation_{true}; diff --git a/peopleCounter.yaml b/peopleCounter.yaml index d387f7fc..b3f22413 100644 --- a/peopleCounter.yaml +++ b/peopleCounter.yaml @@ -71,17 +71,19 @@ i2c: roode: id: roode_platform i2c_address: 0x29 - update_interval: 100ms + update_interval: 10ms + # roi: + # roi_height: 16 + # roi_width: 6 calibration: max_threshold_percentage: 85 min_threshold_percentage: 5 roi_calibration: true # manual: - # sensor_mode: 2 - # roi_height: 16 - # roi_width: 6 - # manual_threshold: 1300 - # timing_budget: 100 + # sensor_mode: 3 + # manual_threshold: 1280 + # timing_budget: 200 + # use_sampling: true invert_direction: true restore_values: false diff --git a/peopleCounter32.yaml b/peopleCounter32.yaml index ee94e4b9..4f2d897d 100644 --- a/peopleCounter32.yaml +++ b/peopleCounter32.yaml @@ -74,17 +74,19 @@ i2c: roode: id: roode_platform i2c_address: 0x29 - update_interval: 100ms + update_interval: 10ms + # roi: + # roi_height: 16 + # roi_width: 6 calibration: max_threshold_percentage: 85 min_threshold_percentage: 5 roi_calibration: true # manual: - # sensor_mode: 2 - # roi_height: 16 - # roi_width: 6 - # manual_threshold: 1300 - # timing_budget: 100 + # sensor_mode: 3 + # manual_threshold: 1280 + # timing_budget: 200 + # use_sampling: true invert_direction: true restore_values: false diff --git a/peopleCounterDev.yaml b/peopleCounterDev.yaml index bc529584..92fb8c03 100644 --- a/peopleCounterDev.yaml +++ b/peopleCounterDev.yaml @@ -60,17 +60,19 @@ i2c: roode: id: roode_platform i2c_address: 0x29 - update_interval: 100ms + update_interval: 10ms + # roi: + # roi_height: 16 + # roi_width: 6 calibration: max_threshold_percentage: 85 min_threshold_percentage: 5 roi_calibration: true # manual: - # sensor_mode: 2 - # roi_height: 16 - # roi_width: 6 - # manual_threshold: 1300 - # timing_budget: 100 + # sensor_mode: 3 + # manual_threshold: 1280 + # timing_budget: 200 + # use_sampling: true invert_direction: true restore_values: false From a977499e8c67f69122f315f68d0e7bd60c7e7c7c Mon Sep 17 00:00:00 2001 From: Kai Bepperling Date: Thu, 30 Dec 2021 14:17:09 +0100 Subject: [PATCH 2/2] Add pololu lib again --- components/roode/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/components/roode/__init__.py b/components/roode/__init__.py index cab30ca1..a55a3f6c 100644 --- a/components/roode/__init__.py +++ b/components/roode/__init__.py @@ -147,6 +147,7 @@ async def to_code(config): await cg.register_component(hub, config) cg.add_library("EEPROM", None) cg.add_library("Wire", None) + cg.add_library("pololu", "1.3.0", "VL53L1X") validate_roi_settings(config)