-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
roode.cpp
632 lines (598 loc) · 25.2 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#include "esphome/core/log.h"
#include "roode.h"
namespace esphome
{
namespace roode
{
static const char *const TAG = "Roode";
static const char *const SETUP = "Setup";
static const char *const CALIBRATION = "Calibration";
void Roode::dump_config()
{
ESP_LOGCONFIG(TAG, "dump config:");
LOG_I2C_DEVICE(this);
LOG_UPDATE_INTERVAL(this);
}
void Roode::setup()
{
ESP_LOGI(SETUP, "Booting Roode %s", VERSION);
if (version_sensor != nullptr)
{
version_sensor->publish_state(VERSION);
}
EEPROM.begin(EEPROM_SIZE);
Wire.begin();
Wire.setClock(400000);
distanceSensor.setBus(&Wire);
if (distanceSensor.getAddress() != address_)
{
distanceSensor.setAddress(address_);
}
if (invert_direction_)
{
ESP_LOGD(TAG, "Inverting direction");
LEFT = 1;
RIGHT = 0;
}
distanceSensor.setTimeout(500);
if (!distanceSensor.init())
{
ESP_LOGE(SETUP, "Failed to detect and initialize sensor!");
}
if (calibration_active_)
{
calibration(distanceSensor);
App.feed_wdt();
}
if (manual_active_)
{
center[0] = 167;
center[1] = 231;
distanceSensor.setROISize(Roode::roi_width_, Roode::roi_height_);
setSensorMode(sensor_mode, timing_budget_);
DIST_THRESHOLD_MAX[0] = Roode::manual_threshold_;
DIST_THRESHOLD_MAX[1] = Roode::manual_threshold_;
publishSensorConfiguration(DIST_THRESHOLD_MAX, true);
}
if (restore_values_)
{
ESP_LOGI("Roode setup", "Restoring last count value...");
peopleCounter = EEPROM.read(100);
if (peopleCounter == 255) // 255 is the default value if no value was stored
peopleCounter = 0;
ESP_LOGD("Roode setup", "last value: %u", peopleCounter);
}
sendCounter(peopleCounter);
distanceSensor.startContinuous(delay_between_measurements);
}
void Roode::update()
{
if (distance_sensor != nullptr)
{
distance_sensor->publish_state(distance);
}
}
void Roode::loop()
{
// unsigned long start = micros();
getZoneDistance();
zone++;
zone = zone % 2;
App.feed_wdt();
// unsigned long end = micros();
// unsigned long delta = end - start;
// ESP_LOGI("Roode loop", "loop took %lu microseconds", delta);
}
bool Roode::handleSensorStatus()
{
const char *statusString = VL53L1X::rangeStatusToString(sensor_status); // This function call will manipulate the range_status variable
ESP_LOGD(TAG, "Sensor status: %d, Last sensor status: %d", sensor_status, last_sensor_status);
if (last_sensor_status == sensor_status && sensor_status == 0)
{
if (status_sensor != nullptr)
{
status_sensor->publish_state(statusString);
}
return true;
}
if (sensor_status != 0 && sensor_status != 2 && sensor_status != 7)
{
ESP_LOGE(TAG, "Ranging failed with an error. status: %d, error: %s", sensor_status, statusString);
return false;
}
return true;
}
void Roode::getZoneDistance()
{
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;
static uint8_t DistancesTableSize[2] = {0, 0};
int CurrentZoneStatus = NOBODY;
int AllZonesCurrentStatus = 0;
int AnEventHasOccured = 0;
distanceSensor.setROICenter(center[zone]);
distanceSensor.startContinuous(delay_between_measurements);
last_sensor_status = sensor_status;
distance = distanceSensor.read();
distanceSensor.writeReg(distanceSensor.SYSTEM__MODE_START, 0x80); // stop reading
sensor_status = distanceSensor.ranging_data.range_status;
if (!handleSensorStatus())
{
return;
}
if (use_sampling_)
{
ESP_LOGD(SETUP, "Using sampling");
static uint16_t Distances[2][DISTANCES_ARRAY_SIZE];
uint16_t MinDistance;
uint8_t i;
if (DistancesTableSize[zone] < DISTANCES_ARRAY_SIZE)
{
Distances[zone][DistancesTableSize[zone]] = distance;
DistancesTableSize[zone]++;
ESP_LOGD(SETUP, "Distances[%d][DistancesTableSize[zone]] = %d", zone, Distances[zone][DistancesTableSize[zone]]);
}
else
{
for (i = 1; i < DISTANCES_ARRAY_SIZE; i++)
Distances[zone][i - 1] = Distances[zone][i];
Distances[zone][DISTANCES_ARRAY_SIZE - 1] = distance;
ESP_LOGD(SETUP, "Distances[%d][DISTANCES_ARRAY_SIZE - 1] = %d", zone, Distances[zone][DISTANCES_ARRAY_SIZE - 1]);
}
ESP_LOGD(SETUP, "Distances[%d][0]] = %d", zone, Distances[zone][0]);
ESP_LOGD(SETUP, "Distances[%d][1]] = %d", zone, Distances[zone][1]);
// pick up the min distance
MinDistance = Distances[zone][0];
if (DistancesTableSize[zone] >= 2)
{
for (i = 1; i < DistancesTableSize[zone]; i++)
{
if (Distances[zone][i] < MinDistance)
MinDistance = Distances[zone][i];
}
}
distance = MinDistance;
}
// PathTrack algorithm
if (distance < DIST_THRESHOLD_MAX[zone] && distance > DIST_THRESHOLD_MIN[zone])
{
// Someone is in the sensing area
CurrentZoneStatus = SOMEONE;
if (presence_sensor != nullptr)
{
presence_sensor->publish_state(true);
}
}
// left zone
if (zone == LEFT)
{
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)
{
if (PathTrackFillingSize < 4)
{
PathTrackFillingSize++;
}
// if nobody anywhere lets check if an exit or entry has happened
if ((LeftPreviousStatus == NOBODY) && (RightPreviousStatus == NOBODY))
{
// 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
if (peopleCounter > 0)
{
peopleCounter--;
sendCounter(peopleCounter);
ESP_LOGD("Roode pathTracking", "Exit detected.");
if (entry_exit_event_sensor != nullptr)
{
entry_exit_event_sensor->publish_state("Exit");
}
DistancesTableSize[0] = 0;
DistancesTableSize[1] = 0;
}
}
else if ((PathTrack[1] == 2) && (PathTrack[2] == 3) && (PathTrack[3] == 1))
{
// This an entry
peopleCounter++;
sendCounter(peopleCounter);
ESP_LOGD("Roode pathTracking", "Entry detected.");
if (entry_exit_event_sensor != nullptr)
{
entry_exit_event_sensor->publish_state("Entry");
}
DistancesTableSize[0] = 0;
DistancesTableSize[1] = 0;
}
else
{
// reset the table filling size also in case of unexpected path
DistancesTableSize[0] = 0;
DistancesTableSize[1] = 0;
}
}
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::sendCounter(uint16_t counter)
{
ESP_LOGI(SETUP, "Sending people count: %d", counter);
peopleCounter = counter;
if (people_counter_sensor != nullptr)
{
people_counter_sensor->publish_state(peopleCounter);
}
if (restore_values_)
{
EEPROM.write(100, peopleCounter);
EEPROM.commit();
}
}
void Roode::recalibration()
{
calibration(distanceSensor);
}
void Roode::roi_calibration(VL53L1X distanceSensor, int optimized_zone_0, int optimized_zone_1)
{
// the value of the average distance is used for computing the optimal size of the ROI and consequently also the center of the two zones
int function_of_the_distance = 16 * (1 - (0.15 * 2) / (0.34 * (min(optimized_zone_0, optimized_zone_1) / 1000)));
int ROI_size = min(8, max(4, function_of_the_distance));
Roode::roi_width_ = ROI_size;
Roode::roi_height_ = ROI_size * 2;
// now we set the position of the center of the two zones
if (advised_sensor_orientation_)
{
switch (ROI_size)
{
case 4:
center[0] = 150;
center[1] = 247;
break;
case 5:
center[0] = 159;
center[1] = 239;
break;
case 6:
center[0] = 159;
center[1] = 239;
break;
case 7:
center[0] = 167;
center[1] = 231;
break;
case 8:
center[0] = 167;
center[1] = 231;
break;
}
}
else
{
switch (ROI_size)
{
case 4:
center[0] = 193;
center[1] = 58;
break;
case 5:
center[0] = 194;
center[1] = 59;
break;
case 6:
center[0] = 194;
center[1] = 59;
break;
case 7:
center[0] = 195;
center[1] = 60;
break;
case 8:
center[0] = 195;
center[1] = 60;
break;
}
}
// we will now repeat the calculations necessary to define the thresholds with the updated zones
zone = 0;
int *values_zone_0 = new int[number_attempts];
int *values_zone_1 = new int[number_attempts];
distanceSensor.writeReg(distanceSensor.SYSTEM__MODE_START, 0x80);
distanceSensor.setROISize(Roode::roi_width_, Roode::roi_height_);
distanceSensor.startContinuous(delay_between_measurements);
for (int i = 0; i < number_attempts; i++)
{
// increase sum of values in Zone 0
distanceSensor.setROICenter(center[zone]);
distance = distanceSensor.read();
values_zone_0[i] = distance;
zone++;
zone = zone % 2;
App.feed_wdt();
// increase sum of values in Zone 1
distanceSensor.setROICenter(center[zone]);
distance = distanceSensor.read();
values_zone_1[i] = distance;
zone++;
zone = zone % 2;
}
optimized_zone_0 = getOptimizedValues(values_zone_0, getSum(values_zone_0, number_attempts), number_attempts);
optimized_zone_1 = getOptimizedValues(values_zone_1, getSum(values_zone_1, number_attempts), number_attempts);
}
void Roode::setSensorMode(int sensor_mode, int new_timing_budget)
{
distanceSensor.writeReg(distanceSensor.SYSTEM__MODE_START, 0x80);
switch (sensor_mode)
{
case 0: // short mode
time_budget_in_ms = time_budget_in_ms_short;
delay_between_measurements = time_budget_in_ms + 5;
status = distanceSensor.setDistanceMode(VL53L1X::Short);
if (!status)
{
ESP_LOGE(SETUP, "Could not set distance mode. mode: %d", VL53L1X::Short);
}
ESP_LOGI(SETUP, "Set short mode. timing_budget: %d", time_budget_in_ms);
break;
case 1: // medium mode
time_budget_in_ms = time_budget_in_ms_medium;
delay_between_measurements = time_budget_in_ms + 5;
status = distanceSensor.setDistanceMode(VL53L1X::Medium);
if (!status)
{
ESP_LOGE(SETUP, "Could not set distance mode. mode: %d", VL53L1X::Medium);
}
ESP_LOGI(SETUP, "Set medium mode. timing_budget: %d", time_budget_in_ms);
break;
case 2: // long mode
time_budget_in_ms = time_budget_in_ms_long;
delay_between_measurements = time_budget_in_ms + 5;
status = distanceSensor.setDistanceMode(VL53L1X::Long);
if (!status)
{
ESP_LOGE(SETUP, "Could not set distance mode. mode: %d", VL53L1X::Long);
}
ESP_LOGI(SETUP, "Set long range mode. timing_budget: %d", time_budget_in_ms);
break;
case 3: // custom mode
time_budget_in_ms = new_timing_budget;
delay_between_measurements = new_timing_budget + 5;
status = distanceSensor.setDistanceMode(VL53L1X::Long);
if (!status)
{
ESP_LOGE(SETUP, "Could not set distance mode. mode: %d", VL53L1X::Long);
}
ESP_LOGI(SETUP, "Manually set custom range mode. timing_budget: %d", time_budget_in_ms);
break;
default:
break;
}
status = distanceSensor.setMeasurementTimingBudget(time_budget_in_ms * 1000);
if (!status)
{
ESP_LOGE(SETUP, "Could not set timing budget. timing_budget: %d ms", time_budget_in_ms);
}
}
void Roode::setCorrectDistanceSettings(float average_zone_0, float average_zone_1)
{
if (average_zone_0 <= short_distance_threshold || average_zone_1 <= short_distance_threshold)
{
setSensorMode(0, time_budget_in_ms_short);
}
if ((average_zone_0 > short_distance_threshold && average_zone_0 <= medium_distance_threshold) || (average_zone_1 > short_distance_threshold && average_zone_1 <= medium_distance_threshold))
{
setSensorMode(1, time_budget_in_ms_medium);
}
if (average_zone_0 > medium_distance_threshold || average_zone_1 > medium_distance_threshold)
{
setSensorMode(2, time_budget_in_ms_long);
}
status = distanceSensor.setMeasurementTimingBudget(time_budget_in_ms * 1000);
if (!status)
{
ESP_LOGE(CALIBRATION, "Could not set timing budget. timing_budget: %d ms", time_budget_in_ms);
}
}
int Roode::getSum(int *array, int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum = sum + array[i];
App.feed_wdt();
}
return sum;
}
int Roode::getOptimizedValues(int *values, int sum, int size)
{
int sum_squared = 0;
int variance = 0;
int sd = 0;
int avg = sum / size;
for (int i = 0; i < size; i++)
{
sum_squared = sum_squared + (values[i] * values[i]);
App.feed_wdt();
}
variance = sum_squared / size - (avg * avg);
sd = sqrt(variance);
ESP_LOGD(CALIBRATION, "Zone AVG: %d", avg);
ESP_LOGD(CALIBRATION, "Zone 0 SD: %d", sd);
return avg - sd;
}
void Roode::calibration(VL53L1X distanceSensor)
{
distanceSensor.writeReg(distanceSensor.SYSTEM__MODE_START, 0x80);
// the sensor does 100 measurements for each zone (zones are predefined)
time_budget_in_ms = time_budget_in_ms_medium;
delay_between_measurements = time_budget_in_ms + 5;
distanceSensor.setDistanceMode(VL53L1X::Medium);
status = distanceSensor.setMeasurementTimingBudget(time_budget_in_ms * 1000);
if (!status)
{
ESP_LOGE(CALIBRATION, "Could not set timing budget. timing_budget: %d ms", time_budget_in_ms);
}
if (advised_sensor_orientation_)
{
center[0] = 167;
center[1] = 231;
}
else
{
center[0] = 195;
center[1] = 60;
uint16_t roi_width_temp = roi_width_;
uint16_t roi_height_temp = roi_height_;
roi_width_ = roi_height_;
roi_height_ = roi_width_;
}
zone = 0;
int *values_zone_0 = new int[number_attempts];
int *values_zone_1 = new int[number_attempts];
distanceSensor.setROISize(Roode::roi_width_, Roode::roi_height_);
distanceSensor.startContinuous(delay_between_measurements);
for (int i = 0; i < number_attempts; i++)
{
// increase sum of values in Zone 0
distanceSensor.setROICenter(center[zone]);
distance = distanceSensor.read();
values_zone_0[i] = distance;
zone++;
zone = zone % 2;
App.feed_wdt();
// increase sum of values in Zone 1
distanceSensor.setROICenter(center[zone]);
distance = distanceSensor.read();
values_zone_1[i] = distance;
zone++;
zone = zone % 2;
}
// after we have computed the sum for each zone, we can compute the average distance of each zone
optimized_zone_0 = getOptimizedValues(values_zone_0, getSum(values_zone_0, number_attempts), number_attempts);
optimized_zone_1 = getOptimizedValues(values_zone_1, getSum(values_zone_1, number_attempts), number_attempts);
setCorrectDistanceSettings(optimized_zone_0, optimized_zone_1);
if (roi_calibration_)
{
roi_calibration(distanceSensor, optimized_zone_0, optimized_zone_1);
}
DIST_THRESHOLD_MAX[0] = optimized_zone_0 * max_threshold_percentage_ / 100; // they can be int values, as we are not interested in the decimal part when defining the threshold
DIST_THRESHOLD_MAX[1] = optimized_zone_1 * max_threshold_percentage_ / 100;
int hundred_threshold_zone_0 = DIST_THRESHOLD_MAX[0] / 100;
int hundred_threshold_zone_1 = DIST_THRESHOLD_MAX[1] / 100;
int unit_threshold_zone_0 = DIST_THRESHOLD_MAX[0] - 100 * hundred_threshold_zone_0;
int unit_threshold_zone_1 = DIST_THRESHOLD_MAX[1] - 100 * hundred_threshold_zone_1;
publishSensorConfiguration(DIST_THRESHOLD_MAX, true);
App.feed_wdt();
if (min_threshold_percentage_ != 0)
{
DIST_THRESHOLD_MIN[0] = optimized_zone_0 * min_threshold_percentage_ / 100; // they can be int values, as we are not interested in the decimal part when defining the threshold
DIST_THRESHOLD_MIN[1] = optimized_zone_1 * min_threshold_percentage_ / 100;
publishSensorConfiguration(DIST_THRESHOLD_MIN, false);
}
distanceSensor.writeReg(distanceSensor.SYSTEM__MODE_START, 0x80);
}
void Roode::publishSensorConfiguration(int DIST_THRESHOLD_ARR[2], bool isMax)
{
if (isMax)
{
ESP_LOGI(SETUP, "Max threshold zone0: %dmm", DIST_THRESHOLD_ARR[0]);
ESP_LOGI(SETUP, "Max threshold zone1: %dmm", DIST_THRESHOLD_ARR[1]);
if (max_threshold_zone0_sensor != nullptr)
{
max_threshold_zone0_sensor->publish_state(DIST_THRESHOLD_ARR[0]);
}
if (max_threshold_zone1_sensor != nullptr)
{
max_threshold_zone1_sensor->publish_state(DIST_THRESHOLD_ARR[1]);
}
}
else
{
ESP_LOGI(SETUP, "Min threshold zone0: %dmm", DIST_THRESHOLD_ARR[0]);
ESP_LOGI(SETUP, "Min threshold zone1: %dmm", DIST_THRESHOLD_ARR[1]);
if (min_threshold_zone0_sensor != nullptr)
{
min_threshold_zone0_sensor->publish_state(DIST_THRESHOLD_ARR[0]);
}
if (min_threshold_zone1_sensor != nullptr)
{
min_threshold_zone1_sensor->publish_state(DIST_THRESHOLD_ARR[1]);
}
}
if (roi_height_sensor != nullptr)
{
roi_height_sensor->publish_state(roi_height_);
}
if (roi_width_sensor != nullptr)
{
roi_width_sensor->publish_state(roi_width_);
}
}
}
}