Skip to content

Commit bd3e8e5

Browse files
committed
Update VL53L4CX after VL53L4CD PR feedback
1 parent 9b99d45 commit bd3e8e5

File tree

1 file changed

+23
-100
lines changed

1 file changed

+23
-100
lines changed

src/components/i2c/drivers/WipperSnapper_I2C_Driver_VL53L4CX.h

Lines changed: 23 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -51,50 +51,6 @@ class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
5151
delete _VL53L4CX;
5252
}
5353

54-
/**************************************************************************/
55-
/*!
56-
@brief Gets a human-readable description of a VL53L4CX status code.
57-
@param statusCode
58-
The status code to describe.
59-
@returns A human-readable description of the status code.
60-
*/
61-
const char *getVL53L4CXStatusDescription(int statusCode) {
62-
switch (statusCode) {
63-
case VL53L4CX_RANGESTATUS_RANGE_VALID:
64-
return "VL53L4CX_RANGESTATUS_RANGE_VALID";
65-
case VL53L4CX_RANGESTATUS_SIGMA_FAIL:
66-
return "VL53L4CX_RANGESTATUS_SIGMA_FAIL";
67-
case VL53L4CX_RANGESTATUS_RANGE_VALID_MIN_RANGE_CLIPPED:
68-
return "VL53L4CX_RANGESTATUS_RANGE_VALID_MIN_RANGE_CLIPPED";
69-
case VL53L4CX_RANGESTATUS_OUTOFBOUNDS_FAIL:
70-
return "VL53L4CX_RANGESTATUS_OUTOFBOUNDS_FAIL";
71-
case VL53L4CX_RANGESTATUS_HARDWARE_FAIL:
72-
return "VL53L4CX_RANGESTATUS_HARDWARE_FAIL";
73-
case VL53L4CX_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL:
74-
return "VL53L4CX_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL";
75-
case VL53L4CX_RANGESTATUS_WRAP_TARGET_FAIL:
76-
return "VL53L4CX_RANGESTATUS_WRAP_TARGET_FAIL";
77-
case VL53L4CX_RANGESTATUS_PROCESSING_FAIL:
78-
return "VL53L4CX_RANGESTATUS_PROCESSING_FAIL";
79-
case VL53L4CX_RANGESTATUS_XTALK_SIGNAL_FAIL:
80-
return "VL53L4CX_RANGESTATUS_XTALK_SIGNAL_FAIL";
81-
case VL53L4CX_RANGESTATUS_SYNCRONISATION_INT:
82-
return "VL53L4CX_RANGESTATUS_SYNCRONISATION_INT";
83-
case VL53L4CX_RANGESTATUS_RANGE_VALID_MERGED_PULSE:
84-
return "VL53L4CX_RANGESTATUS_RANGE_VALID_MERGED_PULSE";
85-
case VL53L4CX_RANGESTATUS_TARGET_PRESENT_LACK_OF_SIGNAL:
86-
return "VL53L4CX_RANGESTATUS_TARGET_PRESENT_LACK_OF_SIGNAL";
87-
case VL53L4CX_RANGESTATUS_MIN_RANGE_FAIL:
88-
return "VL53L4CX_RANGESTATUS_MIN_RANGE_FAIL";
89-
case VL53L4CX_RANGESTATUS_RANGE_INVALID:
90-
return "VL53L4CX_RANGESTATUS_RANGE_INVALID";
91-
case VL53L4CX_RANGESTATUS_NONE:
92-
return "VL53L4CX_RANGESTATUS_NONE";
93-
default:
94-
return (("UNKNOWN STATUS: ") + String(statusCode)).c_str();
95-
}
96-
}
97-
9854
/*******************************************************************************/
9955
/*!
10056
@brief Initializes the VL53L4CX sensor and begins I2C.
@@ -104,53 +60,20 @@ class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
10460
bool begin() {
10561
_VL53L4CX = new VL53L4CX(_i2c, -1);
10662

107-
/* Reinstate if shutdown pin utilised
108-
* // Configure VL53L4CX satellite component.
109-
* _VL53L4CX->begin();
110-
*
111-
* // Switch off VL53L4CX satellite component.
112-
* _VL53L4CX->VL53L4CX_Off();
113-
*/
114-
11563
if (_VL53L4CX->InitSensor((uint8_t)_sensorAddress) != VL53L4CX_ERROR_NONE) {
11664
WS_DEBUG_PRINTLN("Failed to initialize VL53L4CX sensor!");
11765
return false;
11866
}
119-
// // Program the highest possible TimingBudget, no interval time
120-
// if (_VL53L4CX-> VL53L4CX_SetRangeTiming(200, 0) != VL53L4CX_ERROR_NONE) {
121-
// WS_DEBUG_PRINTLN("Failed to set VL53L4CX timing!");
122-
// return false;
123-
// }
124-
125-
// uint16_t signalThreshold = -1;
126-
// if (_VL53L4CX->VL53L4CX_GetSignalThreshold(&signalThreshold) !=
127-
// VL53L4CX_ERROR_NONE) {
128-
// WS_DEBUG_PRINTLN("Failed to get VL53L4CX signal threshold!");
129-
// } else {
130-
// WS_DEBUG_PRINT("VL53L4CX old signal threshold: ");
131-
// WS_DEBUG_PRINTLN(signalThreshold);
132-
// }
133-
// if (_VL53L4CX->VL53L4CX_SetSignalThreshold(50) != VL53L4CX_ERROR_NONE) {
134-
// WS_DEBUG_PRINTLN("Failed to set VL53L4CX signal threshold!");
135-
// } else {
136-
// WS_DEBUG_PRINT("VL53L4CX new signal threshold: ");
137-
// WS_DEBUG_PRINTLN(8);
138-
// }
67+
68+
// Set 1 second measurement time, the highest possible TimingBudget is 10seconds
69+
if (_VL53L4CX->VL53L4CX_SetMeasurementTimingBudgetMicroSeconds(1000000) != VL53L4CX_ERROR_NONE) {
70+
WS_DEBUG_PRINTLN("Failed to set VL53L4CX timing budget!");
71+
return false;
72+
}
13973

140-
// uint16_t sigmaThreshold = -1;
141-
// if (_VL53L4CX->VL53L4CX_GetSigmaThreshold(&sigmaThreshold) !=
142-
// VL53L4CX_ERROR_NONE) {
143-
// WS_DEBUG_PRINTLN("Failed to get VL53L4CX sigma threshold!");
144-
// } else {
145-
// WS_DEBUG_PRINT("VL53L4CX old sigma threshold: ");
146-
// WS_DEBUG_PRINTLN(sigmaThreshold);
147-
// }
148-
// if (_VL53L4CX->VL53L4CX_SetSigmaThreshold(100) != VL53L4CX_ERROR_NONE) {
149-
// WS_DEBUG_PRINTLN("Failed to set VL53L4CX sigma threshold!");
150-
// } else {
151-
// WS_DEBUG_PRINT("VL53L4CX new sigma threshold: ");
152-
// WS_DEBUG_PRINTLN(100);
153-
// }
74+
if (_VL53L4CX->VL53L4CX_SetDistanceMode(VL53L4CX_DISTANCEMODE_LONG) != VL53L4CX_ERROR_NONE) {
75+
WS_DEBUG_PRINTLN("Failed to set VL53L4CX distance mode to long!");
76+
}
15477

15578
if (_VL53L4CX->VL53L4CX_StartMeasurement() != VL53L4CX_ERROR_NONE) {
15679
WS_DEBUG_PRINTLN("Failed to start VL53L4CX ranging!");
@@ -202,17 +125,17 @@ class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
202125
VL53L4CX_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
203126
uint8_t NewDataReady = 0;
204127
int no_of_object_found = 0;
205-
int j = 0;
128+
int currentObject = 0;
206129
int status;
207130
// Start fresh reading, seemed to be accepting stale value
208131
status = _VL53L4CX->VL53L4CX_ClearInterruptAndStartMeasurement();
209132
WS_DEBUG_PRINT("Waiting for VL53L4CX data ready...");
210133
delay(250);
211134

212-
for (uint8_t i = 0; (status = _VL53L4CX->VL53L4CX_GetMeasurementDataReady(
213-
&NewDataReady)) &&
214-
!NewDataReady && i < 3;
215-
i++) {
135+
for (uint8_t retries = 0;
136+
(status = _VL53L4CX->VL53L4CX_GetMeasurementDataReady(&NewDataReady)) &&
137+
!NewDataReady && retries < 3;
138+
retries++) {
216139
delay(300);
217140
WS_DEBUG_PRINT(" .");
218141
}
@@ -221,23 +144,23 @@ class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
221144
status = _VL53L4CX->VL53L4CX_GetMultiRangingData(pMultiRangingData);
222145
no_of_object_found = pMultiRangingData->NumberOfObjectsFound;
223146

224-
for (j = 0; j < no_of_object_found; j++) {
225-
if (pMultiRangingData->RangeData[j].RangeStatus ==
147+
for (currentObject = 0; currentObject < no_of_object_found;
148+
currentObject++) {
149+
if (pMultiRangingData->RangeData[currentObject].RangeStatus ==
226150
VL53L4CX_RANGESTATUS_RANGE_VALID ||
227-
pMultiRangingData->RangeData[j].RangeStatus ==
151+
pMultiRangingData->RangeData[currentObject].RangeStatus ==
228152
VL53L4CX_RANGESTATUS_RANGE_VALID_MERGED_PULSE) {
229-
int16_t mm = pMultiRangingData->RangeData[j].RangeMilliMeter;
230-
if (j == index) {
153+
int16_t mm =
154+
pMultiRangingData->RangeData[currentObject].RangeMilliMeter;
155+
if (currentObject == index) {
231156
proximityEvent->data[0] = (float)mm;
232157
return true;
233158
}
234159
}
235160
}
236-
// TODO: Once I2C sensors fire all data points during a single call, we
237-
// can return both distances and other metrics like the std deviation
238161
} else {
239-
WS_DEBUG_PRINTLN("VL53L4CX Error: " +
240-
String(getVL53L4CXStatusDescription(status)));
162+
WS_DEBUG_PRINT("VL53L4CX Error: ");
163+
WS_DEBUG_PRINTLN(status);
241164
}
242165
return false;
243166
}

0 commit comments

Comments
 (0)