Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sns/VL53L1X: improve sensor setup and validation #2313

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions code/espurna/config/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion code/espurna/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +2037 to +2043
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk if this is necessary here

  • looking at the library source, you could just check setMeasurementTimingBudget() boolean result by proxying it via the sensor method and setting internal error
  • this could also happen with setInterMeasurementPeriod(), never allowing startContiniuous in the begin()

thus, replacing #error with debug_msg_p() call


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);
}
Expand Down
12 changes: 6 additions & 6 deletions code/espurna/sensors/VL53L1XSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Collaborator

@mcspr mcspr Aug 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to note, this implicitly means:

_vl53l1x->setMeasurementTimingBudget(static_cast<uint32_t>(static_cast<int>(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;
}

Expand Down Expand Up @@ -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;

};
Expand Down