-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
roode.cpp
281 lines (251 loc) · 9.35 KB
/
roode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "roode.h"
namespace esphome {
namespace roode {
void Roode::dump_config() {
ESP_LOGCONFIG(TAG, "Roode:");
ESP_LOGCONFIG(TAG, " Sample size: %d", samples);
LOG_UPDATE_INTERVAL(this);
entry->dump_config();
exit->dump_config();
}
void Roode::setup() {
ESP_LOGI(SETUP, "Booting Roode %s", VERSION);
if (version_sensor != nullptr) {
version_sensor->publish_state(VERSION);
}
ESP_LOGI(SETUP, "Using sampling with sampling size: %d", samples);
if (this->distanceSensor->is_failed()) {
this->mark_failed();
ESP_LOGE(TAG, "Roode cannot be setup without a valid VL53L1X sensor");
return;
}
calibrate_zones();
}
void Roode::update() {
if (distance_entry != nullptr) {
distance_entry->publish_state(entry->getDistance());
}
if (distance_exit != nullptr) {
distance_exit->publish_state(exit->getDistance());
}
}
void Roode::loop() {
// unsigned long start = micros();
this->current_zone->readDistance(distanceSensor);
// uint16_t samplingDistance = sampling(this->current_zone);
path_tracking(this->current_zone);
handle_sensor_status();
this->current_zone = this->current_zone == this->entry ? this->exit : this->entry;
// ESP_LOGI("Experimental", "Entry zone: %d, exit zone: %d",
// entry->getDistance(Roode::distanceSensor, Roode::sensor_status),
// exit->getDistance(Roode::distanceSensor, Roode::sensor_status)); unsigned
// long end = micros(); unsigned long delta = end - start; ESP_LOGI("Roode
// loop", "loop took %lu microseconds", delta);
}
bool Roode::handle_sensor_status() {
bool check_status = false;
if (last_sensor_status != sensor_status && sensor_status == VL53L1_ERROR_NONE) {
if (status_sensor != nullptr) {
status_sensor->publish_state(sensor_status);
}
check_status = true;
}
if (sensor_status < 28 && sensor_status != VL53L1_ERROR_NONE) {
ESP_LOGE(TAG, "Ranging failed with an error. status: %d", sensor_status);
status_sensor->publish_state(sensor_status);
check_status = false;
}
last_sensor_status = sensor_status;
sensor_status = VL53L1_ERROR_NONE;
return check_status;
}
void Roode::path_tracking(Zone *zone) {
static int PathTrack[] = {0, 0, 0, 0};
static int PathTrackFillingSize = 1; // init this to 1 as we start from state
// where nobody is any of the zones
static int LeftPreviousStatus = NOBODY;
static int RightPreviousStatus = NOBODY;
int CurrentZoneStatus = NOBODY;
int AllZonesCurrentStatus = 0;
int AnEventHasOccured = 0;
// PathTrack algorithm
if (zone->getMinDistance() < zone->threshold->max && zone->getMinDistance() > zone->threshold->min) {
// Someone is in the sensing area
CurrentZoneStatus = SOMEONE;
if (presence_sensor != nullptr) {
presence_sensor->publish_state(true);
}
}
// left zone
if (zone == (this->invert_direction_ ? this->exit : this->entry)) {
if (CurrentZoneStatus != LeftPreviousStatus) {
// event in left zone has occured
AnEventHasOccured = 1;
if (CurrentZoneStatus == SOMEONE) {
AllZonesCurrentStatus += 1;
}
// need to check right zone as well ...
if (RightPreviousStatus == SOMEONE) {
// event in right zone has occured
AllZonesCurrentStatus += 2;
}
// remember for next time
LeftPreviousStatus = CurrentZoneStatus;
}
}
// right zone
else {
if (CurrentZoneStatus != RightPreviousStatus) {
// event in right zone has occured
AnEventHasOccured = 1;
if (CurrentZoneStatus == SOMEONE) {
AllZonesCurrentStatus += 2;
}
// need to check left zone as well ...
if (LeftPreviousStatus == SOMEONE) {
// event in left zone has occured
AllZonesCurrentStatus += 1;
}
// remember for next time
RightPreviousStatus = CurrentZoneStatus;
}
}
// if an event has occured
if (AnEventHasOccured) {
ESP_LOGD(TAG, "Event has occured, AllZonesCurrentStatus: %d", AllZonesCurrentStatus);
if (PathTrackFillingSize < 4) {
PathTrackFillingSize++;
}
// if nobody anywhere lets check if an exit or entry has happened
if ((LeftPreviousStatus == NOBODY) && (RightPreviousStatus == NOBODY)) {
ESP_LOGD(TAG, "Nobody anywhere, AllZonesCurrentStatus: %d", AllZonesCurrentStatus);
// check exit or entry only if PathTrackFillingSize is 4 (for example 0 1
// 3 2) and last event is 0 (nobobdy anywhere)
if (PathTrackFillingSize == 4) {
// check exit or entry. no need to check PathTrack[0] == 0 , it is
// always the case
if ((PathTrack[1] == 1) && (PathTrack[2] == 3) && (PathTrack[3] == 2)) {
// This an exit
ESP_LOGI("Roode pathTracking", "Exit detected.");
this->updateCounter(-1);
if (entry_exit_event_sensor != nullptr) {
entry_exit_event_sensor->publish_state("Exit");
}
} else if ((PathTrack[1] == 2) && (PathTrack[2] == 3) && (PathTrack[3] == 1)) {
// This an entry
ESP_LOGI("Roode pathTracking", "Entry detected.");
this->updateCounter(1);
if (entry_exit_event_sensor != nullptr) {
entry_exit_event_sensor->publish_state("Entry");
}
}
}
PathTrackFillingSize = 1;
} else {
// update PathTrack
// example of PathTrack update
// 0
// 0 1
// 0 1 3
// 0 1 3 1
// 0 1 3 3
// 0 1 3 2 ==> if next is 0 : check if exit
PathTrack[PathTrackFillingSize - 1] = AllZonesCurrentStatus;
}
}
if (presence_sensor != nullptr) {
if (CurrentZoneStatus == NOBODY && LeftPreviousStatus == NOBODY && RightPreviousStatus == NOBODY) {
// nobody is in the sensing area
presence_sensor->publish_state(false);
}
}
}
void Roode::updateCounter(int delta) {
if (this->people_counter == nullptr) {
return;
}
auto next = this->people_counter->state + (float) delta;
ESP_LOGI(TAG, "Updating people count: %d", (int) next);
auto call = this->people_counter->make_call();
call.set_value(next);
call.perform();
}
void Roode::recalibration() { calibrate_zones(); }
const RangingMode *Roode::determine_raning_mode(uint16_t average_entry_zone_distance,
uint16_t average_exit_zone_distance) {
uint16_t min = average_entry_zone_distance < average_exit_zone_distance ? average_entry_zone_distance
: average_exit_zone_distance;
uint16_t max = average_entry_zone_distance > average_exit_zone_distance ? average_entry_zone_distance
: average_exit_zone_distance;
if (min <= short_distance_threshold) {
return Ranging::Short;
}
if (max > short_distance_threshold && min <= medium_distance_threshold) {
return Ranging::Medium;
}
if (max > medium_distance_threshold && min <= medium_long_distance_threshold) {
return Ranging::Long;
}
if (max > medium_long_distance_threshold && min <= long_distance_threshold) {
return Ranging::Longer;
}
return Ranging::Longest;
}
void Roode::calibrate_zones() {
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_);
entry->calibrateThreshold(distanceSensor, number_attempts);
exit->roi_calibration(entry->threshold->idle, exit->threshold->idle, orientation_);
exit->calibrateThreshold(distanceSensor, number_attempts);
publish_sensor_configuration(entry, exit, true);
App.feed_wdt();
publish_sensor_configuration(entry, exit, false);
ESP_LOGI(SETUP, "Finished calibrating sensor zones");
}
void Roode::calibrateDistance() {
auto *const initial = distanceSensor->get_ranging_mode_override().value_or(Ranging::Longest);
distanceSensor->set_ranging_mode(initial);
entry->calibrateThreshold(distanceSensor, number_attempts);
exit->calibrateThreshold(distanceSensor, number_attempts);
if (distanceSensor->get_ranging_mode_override().has_value()) {
return;
}
auto *mode = determine_raning_mode(entry->threshold->idle, exit->threshold->idle);
if (mode != initial) {
distanceSensor->set_ranging_mode(mode);
}
}
void Roode::publish_sensor_configuration(Zone *entry, Zone *exit, bool isMax) {
if (isMax) {
if (max_threshold_entry_sensor != nullptr) {
max_threshold_entry_sensor->publish_state(entry->threshold->max);
}
if (max_threshold_exit_sensor != nullptr) {
max_threshold_exit_sensor->publish_state(exit->threshold->max);
}
} else {
if (min_threshold_entry_sensor != nullptr) {
min_threshold_entry_sensor->publish_state(entry->threshold->min);
}
if (min_threshold_exit_sensor != nullptr) {
min_threshold_exit_sensor->publish_state(exit->threshold->min);
}
}
if (entry_roi_height_sensor != nullptr) {
entry_roi_height_sensor->publish_state(entry->roi->height);
}
if (entry_roi_width_sensor != nullptr) {
entry_roi_width_sensor->publish_state(entry->roi->width);
}
if (exit_roi_height_sensor != nullptr) {
exit_roi_height_sensor->publish_state(exit->roi->height);
}
if (exit_roi_width_sensor != nullptr) {
exit_roi_width_sensor->publish_state(exit->roi->width);
}
}
} // namespace roode
} // namespace esphome