mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-07-28 04:07:47 +00:00
[VL53L0X] Add filtering + make threshold more intuitive
This commit is contained in:
+5
-24
@@ -144,6 +144,9 @@ boolean Plugin_110(uint8_t function, struct EventStruct *event, String& string)
|
||||
|
||||
if (nullptr != P110_data) {
|
||||
const uint32_t interval_ms = Settings.TaskDeviceTimer[event->TaskIndex] * 1000;
|
||||
|
||||
// Clear the "previous" distance so there will be a new result when starting the task
|
||||
UserVar.setFloat(event->TaskIndex, 3, -1);
|
||||
success = P110_data->begin(interval_ms); // Start the sensor
|
||||
}
|
||||
break;
|
||||
@@ -153,29 +156,7 @@ boolean Plugin_110(uint8_t function, struct EventStruct *event, String& string)
|
||||
P110_data_struct *P110_data = static_cast<P110_data_struct *>(getPluginTaskData(event->TaskIndex));
|
||||
|
||||
if (nullptr != P110_data) {
|
||||
if (P110_data->isReadSuccessful()) {
|
||||
const int16_t dist = P110_data->getDistance();
|
||||
const int16_t p_dist = UserVar.getFloat(event->TaskIndex, 0);
|
||||
// Check trend:
|
||||
// 0 = equal
|
||||
// -1 = move closer
|
||||
// 1 = move away
|
||||
const int16_t direct = dist == p_dist ? 0 : (dist < p_dist ? -1 : 1);
|
||||
const bool triggered = (dist > (p_dist + P110_DELTA)) || (dist < (p_dist - P110_DELTA));
|
||||
|
||||
#ifdef P110_INFO_LOG
|
||||
|
||||
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
|
||||
addLog(LOG_LEVEL_INFO, strformat(F("VL53L0x: Perform read: trig: %d, prev: %d, dist: %d"), triggered, p_dist, dist));
|
||||
}
|
||||
#endif // ifdef P110_INFO_LOG
|
||||
|
||||
if (triggered || (P110_SEND_ALWAYS == 1)) {
|
||||
UserVar.setFloat(event->TaskIndex, 0, dist); // Value is classified as invalid when > 8190, so no conversion or 'split' needed
|
||||
UserVar.setFloat(event->TaskIndex, 1, direct); // Trend of value
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
success = P110_data->plugin_read(event);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -186,7 +167,7 @@ boolean Plugin_110(uint8_t function, struct EventStruct *event, String& string)
|
||||
|
||||
if (nullptr != P110_data) {
|
||||
if (P110_data->readDistance() >= 0) {
|
||||
Scheduler.schedule_task_device_timer(event->TaskIndex, millis() + 10);
|
||||
Scheduler.schedule_task_device_timer(event->TaskIndex, millis());
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -54,6 +54,55 @@ bool P110_data_struct::plugin_fifty_per_second() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool P110_data_struct::plugin_read(struct EventStruct *event) {
|
||||
bool success = false;
|
||||
|
||||
if (isReadSuccessful()) {
|
||||
const float new_distance = getDistance();
|
||||
const float prev_distance = _prev_distance;
|
||||
|
||||
const bool first_sample = (prev_distance < 0.0f);
|
||||
|
||||
const float filtered = first_sample
|
||||
? new_distance
|
||||
: 0.75f * prev_distance + 0.25f * new_distance;
|
||||
|
||||
|
||||
const float dist = filtered;
|
||||
const float p_dist = prev_distance;
|
||||
|
||||
// Check trend:
|
||||
// 0 = equal
|
||||
// -1 = move closer
|
||||
// 1 = move away
|
||||
|
||||
const int16_t displacement = first_sample ? 0 : roundf(dist - p_dist);
|
||||
const int16_t disp_dir = (displacement == 0)
|
||||
? 0
|
||||
: (displacement > 0) ? 1 : -1;
|
||||
|
||||
const bool direction_changed = disp_dir != static_cast<int16_t>(UserVar.getFloat(event->TaskIndex, 1));
|
||||
const bool triggered = direction_changed || (std::abs(displacement) > P110_DELTA);
|
||||
|
||||
# ifdef P110_INFO_LOG
|
||||
|
||||
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
|
||||
addLog(LOG_LEVEL_INFO, strformat(F("VL53L0x: Perform read: trig: %d, prev: %d, dist: %d"), triggered, p_dist, dist));
|
||||
}
|
||||
# endif // ifdef P110_INFO_LOG
|
||||
// Value is classified as invalid when > 8190, so no conversion or 'split' needed
|
||||
UserVar.setFloat(event->TaskIndex, 0, filtered);
|
||||
UserVar.setFloat(event->TaskIndex, 1, disp_dir); // Trend of value
|
||||
|
||||
if (first_sample || triggered || (P110_SEND_ALWAYS == 1)) {
|
||||
// Update the "previous" distance.
|
||||
_prev_distance = filtered;
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
int16_t P110_data_struct::getDistance() {
|
||||
const int res = _distance;
|
||||
|
||||
|
||||
@@ -53,10 +53,14 @@ public:
|
||||
bool isReadSuccessful() const;
|
||||
bool plugin_fifty_per_second();
|
||||
|
||||
bool plugin_read(struct EventStruct *event);
|
||||
|
||||
private:
|
||||
|
||||
VL53L0X sensor;
|
||||
|
||||
float _prev_distance = -1.0f;
|
||||
|
||||
const uint8_t _i2cAddress;
|
||||
const int _timing;
|
||||
const bool _range;
|
||||
|
||||
@@ -52,6 +52,7 @@ else:
|
||||
"-DUSES_P097", # Touch (ESP32)
|
||||
"-DUSES_P106", # BME680
|
||||
"-DUSES_P107", # SI1145 UV index
|
||||
"-DUSES_P110", # VL53L0x
|
||||
"-DUSES_P131", # NeoPixelMatrix
|
||||
|
||||
"-DUSES_P146", # Cache Reader
|
||||
|
||||
Reference in New Issue
Block a user