I have a wheeled robot moving towards an object. I would like to stop the rover when it approaches 10cm from the object in front of it.
Sometimes it works, but 20% of the cases it fails to detect the distance. Can you help me understand what could be the problem?
I use aruduino nano iot.
Here is the code:
I use the library “STM32duino VL53L3CX” by STMicroelectronics
#include <vl53lx_class.h>
#define DEV_I2C Wire
VL53LX sensor_vl53lx_sat(&DEV_I2C, A1);
void setup(){
// init distance sensor
DEV_I2C.begin();
sensor_vl53lx_sat.begin();
sensor_vl53lx_sat.VL53LX_Off();
sensor_vl53lx_sat.InitSensor(0x12);
}
void loop(){
// start measuring distance
sensor_vl53lx_sat.VL53LX_StartMeasurement();
// measure distance until 10 cm
do {
VL53LX_MultiRangingData_t MultiRangingData;
VL53LX_MultiRangingData_t* pMultiRangingData = &MultiRangingData;
uint8_t NewDataReady = 0;
// int no_of_object_found = 0, j;
// char report[64];
int status;
do {
status = sensor_vl53lx_sat.VL53LX_GetMeasurementDataReady(&NewDataReady);
} while (!NewDataReady);
if ((!status) && (NewDataReady != 0)) {
status = sensor_vl53lx_sat.VL53LX_GetMultiRangingData(pMultiRangingData);
// no_of_object_found = pMultiRangingData->NumberOfObjectsFound;
char buffer[10];
itoa(pMultiRangingData->RangeData[0].RangeMilliMeter, buffer, 10);
log(buffer, true);
if (status == 0) {
status = sensor_vl53lx_sat.VL53LX_ClearInterruptAndStartMeasurement();
}
if (status == 0 && pMultiRangingData->NumberOfObjectsFound == 1 && pMultiRangingData->RangeData[0].RangeMilliMeter <= 100) {
log("Distance sensor: object found at 10cm", true);
break;
}
}
} while (true);
}
If you post some pictures of your setup that shows the board and all of your connections, then I can check those and let you know if there are any potential issues there. What is the object you are trying to detect, how fast does the robot approach the object, and what type of environment are you operating in?
It might be a good idea for you to log or otherwise output some more information from your program so you can better understand what is going wrong. Specifically, you are looking for the condition if (status == 0 && pMultiRangingData->NumberOfObjectsFound == 1 && pMultiRangingData->RangeData[0].RangeMilliMeter <= 100); in the cases where you expect that condition to be met, but it is not, which part of the condition is not true? (Is the status non-zero, is the number of objects found not 1, or is the range inaccurate?) It looks like you’re already logging buffer; does that give you additional information? Could you post some of the log output here?
If other parts of your system are failing at the same time, and it corresponds with the motors turning on, that makes it sound like you might be dealing electrical noise or an inadequate power supply. How are you powering your robot? What motors does it use, and what other components might be drawing significant power? You might consider testing your robot with a different power supply, and you could try monitoring the I2C and power supply lines with an oscilloscope to look for signs of noise or the power dropping out.
the replies to your questions
How are you powering your robot? with 2x LiFePo 4 batteries in series, they are charged and give 6.4v. The rover goes forwards, picks up the bottle of wine, and return back home where it get charged.
What motors does it use?
150:1 Micro Metal Gearmotor HP 6V
what other components might be drawing significant power? I have those 2 motors, the Arduino Nano IoT (maybe wifi gets a lot) and a big stepper motor, but this gets activated only at the end (it moves the grippers to pick up the bottle). Wifi and Distance sensor start not working before I activate the stepper.
When that line is logged, do you expect the sensor to be seeing something <10cm away? Are the four reported distances reasonable given what is in front of the sensor?
According to the VL53L3CX user manual, the status of 12 means “Indicate that there is a target, but the signal is too low to report ranging”. This might indicate that you are trying to sense something under marginal conditions where the sensor is unable to give a reliable result. Additionally, Objs=4 suggests there is probably more than one object detected, so you might try changing the conditional in your program to look for 1 or more objects instead of exactly 1 object.
If you don’t think that explains your issues, it would definitely be a good idea to look at things with a scope as Patrick mentioned to see if there are any signs of power instability or excessive noise. Another thing you could try is to temporarily disconnect the other parts of your system (including the motors and Wi-Fi) to see if that improves the behavior of the distance sensor. Also, it would still be helpful for you to show some pictures of your setup.