implement vl53l1x tof sensor

This commit is contained in:
interfisch 2021-10-13 19:01:22 +02:00
parent 5d47246bda
commit c588c99cd7
2 changed files with 43 additions and 13 deletions

View File

@ -214,7 +214,8 @@ platform = espressif8266 @ 2.5.0
board = d1_mini board = d1_mini
framework = arduino framework = arduino
monitor_port = /dev/ttyUSB0 #monitor_port = /dev/ttyUSB0
monitor_port = COM3
monitor_speed = 115200 monitor_speed = 115200
build_flags = build_flags =
@ -238,8 +239,9 @@ build_flags =
-D dataBH1750_senddelaymax=1000*60*2 -D dataBH1750_senddelaymax=1000*60*2
-D SENSOR_VL53L1X -D SENSOR_VL53L1X
-D dataBH1750_minchange=10.0 -D dataVL53L1X_minchange=100
-D dataBH1750_senddelaymax=1000*60*2 -D dataVL53L1X_senddelaymax=1000*30
-D dataVL53L1X_readdelay=1000

View File

@ -236,12 +236,12 @@ struct sensordata
#include <VL53L1X.h> #include <VL53L1X.h>
VL53L1X vl53l1x; VL53L1X vl53l1x;
bool vl53l1xinit_ok=false; bool vl53l1xinit_ok=false;
struct sensordata dataVL53L1X_range; struct sensordata dataVL53L1X;
struct sensordata dataVL53L1X_peak_signal_count_rate;
struct sensordata dataVL53L1X_ambient_count_rate;
uint16_t value_vl53l1x_range; uint16_t value_vl53l1x_range;
unsigned long lastread_vl53l1x=0; unsigned long lastread_vl53l1x=0;
VL53L1X::RangeStatus lastsentvalue_vl53l1x_status;
#endif #endif
#ifdef SENSOR_ANEMOMETER #ifdef SENSOR_ANEMOMETER
@ -487,10 +487,16 @@ void setup() {
vl53l1xinit_ok=true; vl53l1xinit_ok=true;
vl53l1x.setDistanceMode(VL53L1X::Long); vl53l1x.setDistanceMode(VL53L1X::Long);
vl53l1x.setMeasurementTimingBudget(50000); vl53l1x.setMeasurementTimingBudget(50000);
vl53l1x.startContinuous(50); //This period should be at least as long as the timing budget. vl53l1x.startContinuous(1000); //This period should be at least as long as the timing budget.
} }
#ifdef dataVL53L1X_range_minchange #ifdef dataVL53L1X_minchange
dataVL53L1X_range.minchange=dataVL53L1X_range_minchange; dataVL53L1X.minchange=dataVL53L1X_minchange;
#endif
#ifdef dataVL53L1X_senddelaymax
dataVL53L1X.senddelaymax=dataVL53L1X_senddelaymax;
#endif
#ifdef dataVL53L1X_readdelay
dataVL53L1X.readdelay=dataVL53L1X_readdelay;
#endif #endif
#endif #endif
@ -608,7 +614,10 @@ void setup() {
#endif #endif
#ifdef SENSOR_VL53L1X #ifdef SENSOR_VL53L1X
sensorNode.advertise("range"); sensorNode.advertise("tofstatus");
sensorNode.advertise("tofrange");
sensorNode.advertise("tofpeaksignal");
sensorNode.advertise("tofambient");
#endif #endif
#ifdef SENSOR_ANEMOMETER #ifdef SENSOR_ANEMOMETER
@ -1200,9 +1209,9 @@ void loop_TCS34725_colortemp()
#ifdef SENSOR_VL53L1X #ifdef SENSOR_VL53L1X
void loop_VL53L1X_range() void loop_VL53L1X()
{ {
sensordata &d=dataVL53L1X_range; sensordata &d=dataVL53L1X;
bool _changed=false; bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) { if (millis() >= (d.lastreadtime+d.readdelay)) {
@ -1213,18 +1222,27 @@ void loop_VL53L1X_range()
} }
value_vl53l1x_range=vl53l1x.ranging_data.range_mm; value_vl53l1x_range=vl53l1x.ranging_data.range_mm;
/* for debugging
Serial.print("range: "); Serial.print("range: ");
Serial.print(vl53l1x.ranging_data.range_mm); Serial.print(vl53l1x.ranging_data.range_mm);
Serial.print("\tstatus: "); Serial.print("\tstatus: ");
Serial.print(VL53L1X::rangeStatusToString(vl53l1x.ranging_data.range_status)); Serial.print(VL53L1X::rangeStatusToString(vl53l1x.ranging_data.range_status));
Serial.print("\tstatus=");
Serial.print(vl53l1x.ranging_data.range_status);
Serial.print("\tpeak signal: "); Serial.print("\tpeak signal: ");
Serial.print(vl53l1x.ranging_data.peak_signal_count_rate_MCPS); Serial.print(vl53l1x.ranging_data.peak_signal_count_rate_MCPS);
Serial.print("\tambient: "); Serial.print("\tambient: ");
Serial.print(vl53l1x.ranging_data.ambient_count_rate_MCPS); Serial.print(vl53l1x.ranging_data.ambient_count_rate_MCPS);
Serial.println();
*/
if (abs((int)d.lastsentvalue-value_vl53l1x_range)>=d.minchange){ //int abs if (abs((int)d.lastsentvalue-value_vl53l1x_range)>=d.minchange){ //int abs
_changed=true; _changed=true;
} }
if (lastsentvalue_vl53l1x_status!=vl53l1x.ranging_data.range_status) { //sensor status changed
_changed=true;
}
d.lastreadtime=millis(); d.lastreadtime=millis();
} }
@ -1235,10 +1253,14 @@ void loop_VL53L1X_range()
Homie.getLogger() << "range vl53l1x " << ": " << value_vl53l1x_range << endl; Homie.getLogger() << "range vl53l1x " << ": " << value_vl53l1x_range << endl;
sensorNode.setProperty("range").send(String(value_vl53l1x_range));
sensorNode.setProperty("tofstatus").send(VL53L1X::rangeStatusToString(vl53l1x.ranging_data.range_status));
sensorNode.setProperty("tofrange").send(String(value_vl53l1x_range));
sensorNode.setProperty("tofpeaksignal").send(String(vl53l1x.ranging_data.peak_signal_count_rate_MCPS));
sensorNode.setProperty("tofambient").send(String(vl53l1x.ranging_data.ambient_count_rate_MCPS));
d.lastsentvalue=value_vl53l1x_range; d.lastsentvalue=value_vl53l1x_range;
lastsentvalue_vl53l1x_status=vl53l1x.ranging_data.range_status;
d.lastsent=millis(); d.lastsent=millis();
} }
@ -1394,6 +1416,12 @@ void loopHandler() {
} }
#endif #endif
#ifdef SENSOR_VL53L1X
if (vl53l1xinit_ok) {
loop_VL53L1X();
}
#endif
#ifdef SENSOR_ANEMOMETER #ifdef SENSOR_ANEMOMETER
loop_anemometer(); loop_anemometer();
#endif #endif