From badf5ed795a5da062ba2e1a877737cc9059f37ed Mon Sep 17 00:00:00 2001 From: Rui Marinho Date: Sun, 2 Aug 2020 00:46:14 +0100 Subject: [PATCH] sns/VL53L1X: improve sensor setup and validation --- code/espurna/config/sensors.h | 26 ++++++++++++++------------ code/espurna/sensor.cpp | 10 +++++++++- code/espurna/sensors/VL53L1XSensor.h | 12 ++++++------ 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 239b97bb81..aa88e9f12f 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -1218,27 +1218,29 @@ #endif #ifndef VL53L1X_I2C_ADDRESS -#define VL53L1X_I2C_ADDRESS 0x00 // 0x00 means auto +#define VL53L1X_I2C_ADDRESS 0x00 // 0x00 means auto #endif #ifndef VL53L1X_DISTANCE_MODE -#define VL53L1X_DISTANCE_MODE VL53L1X::Long // The distance mode of the sensor. Can be one of -#endif // `VL53L1X::Short`, `VL53L1X::Medium`, or `VL53L1X::Long. - // Shorter distance modes are less affected by ambient light - // but have lower maximum ranges, especially in the dark. +#define VL53L1X_DISTANCE_MODE VL53L1X::Medium // The distance mode of the sensor. Can be one of + // `VL53L1X::Short`, `VL53L1X::Medium`, or `VL53L1X::Long. + // Shorter distance modes are less affected by ambient light + // but have lower maximum ranges, especially in the dark. +#endif // Medium mode allows for up to 290cm of object distance. #ifndef VL53L1X_MEASUREMENT_TIMING_BUDGET -#define VL53L1X_MEASUREMENT_TIMING_BUDGET 140000 // The time, in microseconds, allocated for a single - // measurement. A longer timing budget allows for more - // accurate at the cost of power. The minimum budget is - // 20 ms (20000 us) in short distance mode and 33 ms for - // medium and long distance modes. +#define VL53L1X_MEASUREMENT_TIMING_BUDGET 33 // The time, in milliseconds, allocated for a single + // measurement. A longer timing budget allows for more + // accurate at the cost of power. The minimum budget is + // 20 ms in short distance mode and 33 ms for medium and + // long distance modes. 33ms is the minimum timing budget + // which can work for all distance modes. Range is [20 - 500]. #endif #ifndef VL53L1X_INTER_MEASUREMENT_PERIOD -#define VL53L1X_INTER_MEASUREMENT_PERIOD 50 // Period, in milliseconds, determining how -#endif // often the sensor takes a measurement. +#define VL53L1X_INTER_MEASUREMENT_PERIOD 50 // Period, in milliseconds, determining how +#endif // often the sensor takes a measurement. //------------------------------------------------------------------------------ // MAX6675 diff --git a/code/espurna/sensor.cpp b/code/espurna/sensor.cpp index ae74e09b65..ebb9e1a199 100644 --- a/code/espurna/sensor.cpp +++ b/code/espurna/sensor.cpp @@ -2034,9 +2034,17 @@ void _sensorLoad() { #if VL53L1X_SUPPORT { + #if (VL53L1X_INTER_MEASUREMENT_PERIOD <= (VL53L1X_MEASUREMENT_TIMING_BUDGET + 4)) + #error "[VL53L1X] Intermeasurement period must be greater than the timing budget + 4ms" + #endif + + #if (VL53L1X_MEASUREMENT_TIMING_BUDGET < 20 || VL53L1X_MEASUREMENT_TIMING_BUDGET > 500) + #error "[VL53L1X] Timing budget is limited to [20, 500]" + #endif + VL53L1XSensor * sensor = new VL53L1XSensor(); - sensor->setInterMeasurementPeriod(VL53L1X_INTER_MEASUREMENT_PERIOD); sensor->setDistanceMode(VL53L1X_DISTANCE_MODE); + sensor->setInterMeasurementPeriod(VL53L1X_INTER_MEASUREMENT_PERIOD); sensor->setMeasurementTimingBudget(VL53L1X_MEASUREMENT_TIMING_BUDGET); _sensors.push_back(sensor); } diff --git a/code/espurna/sensors/VL53L1XSensor.h b/code/espurna/sensors/VL53L1XSensor.h index 16be7dae3b..ebf3dbb30f 100644 --- a/code/espurna/sensors/VL53L1XSensor.h +++ b/code/espurna/sensors/VL53L1XSensor.h @@ -36,13 +36,13 @@ class VL53L1XSensor : public I2CSensor<> { _vl53l1x->setDistanceMode(mode); } - void setMeasurementTimingBudget(uint32_t budget_us) { - _vl53l1x->setMeasurementTimingBudget(budget_us); + void setMeasurementTimingBudget(uint16_t budget_ms) { + _vl53l1x->setMeasurementTimingBudget(budget_ms * 1000); } - void setInterMeasurementPeriod(unsigned int period) { - if (_inter_measurement_period == period) return; - _inter_measurement_period = period; + void setInterMeasurementPeriod(uint16_t period_ms) { + if (_inter_measurement_period == period_ms) return; + _inter_measurement_period = period_ms; _dirty = true; } @@ -108,7 +108,7 @@ class VL53L1XSensor : public I2CSensor<> { protected: VL53L1X * _vl53l1x = NULL; - unsigned int _inter_measurement_period; + uint16_t _inter_measurement_period; double _distance = 0; };