Skip to content

Commit

Permalink
Create Zone.roi_override that config writes to separate from current …
Browse files Browse the repository at this point in the history
…ROI.

This allows ROI to be recalibrated later without losing what the config overrides are.
  • Loading branch information
CarsonF committed Jan 14, 2022
1 parent 19c0e62 commit fae27dc
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion components/roode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def setup_zone(name: str, config: Dict, roode: cg.Pvariable):
zone_config = config[CONF_ZONES][name]
zone_var = cg.MockObj(f"{roode}->{name}", "->")

roi_var = cg.MockObj(f"{zone_var}->roi", "->")
roi_var = cg.MockObj(f"{zone_var}->roi_override", "->")
setup_roi(roi_var, zone_config.get(CONF_ROI, {}), config.get(CONF_ROI, {}))

threshold_var = cg.MockObj(f"{zone_var}->threshold", "->")
Expand Down
14 changes: 4 additions & 10 deletions components/roode/roode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ void Roode::setup() {
version_sensor->publish_state(VERSION);
}
ESP_LOGI(SETUP, "Using sampling with sampling size: %d", samples);
ESP_LOGI(SETUP, "Creating entry and exit zones.");
createEntryAndExitZone();
calibrateZones();
}
Expand Down Expand Up @@ -60,15 +59,6 @@ bool Roode::handleSensorStatus() {
return check_status;
}

void Roode::createEntryAndExitZone() {
if (!entry->roi->center) {
entry->roi->center = orientation_ == Parallel ? 167 : 195;
}
if (!exit->roi->center) {
exit->roi->center = orientation_ == Parallel ? 231 : 60;
}
}

VL53L1_Error Roode::getAlternatingZoneDistances() {
this->current_zone->readDistance(distanceSensor);
App.feed_wdt();
Expand Down Expand Up @@ -254,6 +244,10 @@ const RangingMode *Roode::determineRangingMode(uint16_t average_entry_zone_dista

void Roode::calibrateZones() {
ESP_LOGI(SETUP, "Calibrating sensor zones");

entry->reset_roi(orientation_ == Parallel ? 167 : 195);
exit->reset_roi(orientation_ == Parallel ? 231 : 60);

calibrateDistance();

entry->roi_calibration(entry->threshold->idle, exit->threshold->idle, orientation_);
Expand Down
1 change: 0 additions & 1 deletion components/roode/roode.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ class Roode : public PollingComponent {
VL53L1_Error getAlternatingZoneDistances();
void doPathTracking(Zone *zone);
bool handleSensorStatus();
void createEntryAndExitZone();
void calibrateDistance();
void calibrateZones();
const RangingMode *determineRangingMode(uint16_t average_entry_zone_distance, uint16_t average_exit_zone_distance);
Expand Down
24 changes: 16 additions & 8 deletions components/roode/zone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,18 @@ VL53L1_Error Zone::readDistance(TofSensor *distanceSensor) {
return sensor_status;
}

/**
* This sets the ROI for the zone to the given overrides or the standard default.
* This is needed to do initial calibration of thresholds & ROI.
*/
void Zone::reset_roi(uint8_t default_center) {
roi->width = roi_override->width ?: 6;
roi->height = roi_override->height ?: 16;
roi->center = roi_override->center ?: default_center;
}

void Zone::calibrateThreshold(TofSensor *distanceSensor, int number_attempts) {
ESP_LOGI(CALIBRATION, "Beginning. zoneId: %d", id);
ESP_LOGD(CALIBRATION, "Beginning. zoneId: %d", id);
int *zone_distances = new int[number_attempts];
int sum = 0;
for (int i = 0; i < number_attempts; i++) {
Expand All @@ -48,13 +58,11 @@ void Zone::roi_calibration(uint16_t entry_threshold, uint16_t exit_threshold, Or
// center of the two zones
int function_of_the_distance = 16 * (1 - (0.15 * 2) / (0.34 * (min(entry_threshold, exit_threshold) / 1000)));
int ROI_size = min(8, max(4, function_of_the_distance));
if (!this->roi->width) {
this->roi->width = ROI_size;
}
if (!this->roi->height) {
this->roi->height = ROI_size * 2;
}
if (!this->roi->center) {
this->roi->width = this->roi_override->width ?: ROI_size;
this->roi->height = this->roi_override->height ?: ROI_size * 2;
if (this->roi_override->center) {
this->roi->center = this->roi_override->center;
} else {
// now we set the position of the center of the two zones
if (orientation == Parallel) {
switch (ROI_size) {
Expand Down
2 changes: 2 additions & 0 deletions components/roode/zone.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ class Zone {
public:
explicit Zone(uint8_t id) : id{id} {};
VL53L1_Error readDistance(TofSensor *distanceSensor);
void reset_roi(uint8_t default_center);
void calibrateThreshold(TofSensor *distanceSensor, int number_attempts);
void roi_calibration(uint16_t entry_threshold, uint16_t exit_threshold, Orientation orientation);
const uint8_t id;
uint16_t getDistance() const;
uint16_t getMinDistance() const;
ROI *roi = new ROI();
ROI *roi_override = new ROI();
Threshold *threshold = new Threshold();
void set_max_samples(uint8_t max) { max_samples = max; };

Expand Down

0 comments on commit fae27dc

Please sign in to comment.