AP_OpticalFlow: px4flow retries init 10 times at startup

This resolves an issue in which some px4flow sensors are slow to startup
This commit is contained in:
Randy Mackay 2017-08-22 13:49:44 +09:00
parent fdb4c7b5ee
commit bc38affcb1
1 changed files with 32 additions and 22 deletions

View File

@ -28,6 +28,7 @@
extern const AP_HAL::HAL& hal;
#define PX4FLOW_BASE_I2C_ADDR 0x42
#define PX4FLOW_INIT_RETRIES 10 // attempt to initialise the sensor up to 10 times at startup
// constructor
AP_OpticalFlow_PX4Flow::AP_OpticalFlow_PX4Flow(OpticalFlow &_frontend) :
@ -55,13 +56,17 @@ AP_OpticalFlow_PX4Flow *AP_OpticalFlow_PX4Flow::detect(OpticalFlow &_frontend)
*/
bool AP_OpticalFlow_PX4Flow::scan_buses(void)
{
bool success = false;
uint8_t retry_attempt = 0;
while (!success && retry_attempt < PX4FLOW_INIT_RETRIES) {
for (uint8_t bus = 0; bus < 3; bus++) {
#ifdef HAL_OPTFLOW_PX4FLOW_I2C_BUS
#ifdef HAL_OPTFLOW_PX4FLOW_I2C_BUS
// only one bus from HAL
if (bus != HAL_OPTFLOW_PX4FLOW_I2C_BUS) {
continue;
}
#endif
#endif
AP_HAL::OwnPtr<AP_HAL::Device> tdev = hal.i2c_mgr->get_device(bus, PX4FLOW_BASE_I2C_ADDR + get_address());
if (!tdev) {
continue;
@ -70,15 +75,20 @@ bool AP_OpticalFlow_PX4Flow::scan_buses(void)
continue;
}
struct i2c_integral_frame frame;
bool ok = tdev->read_registers(REG_INTEGRAL_FRAME, (uint8_t *)&frame, sizeof(frame));
success = tdev->read_registers(REG_INTEGRAL_FRAME, (uint8_t *)&frame, sizeof(frame));
tdev->get_semaphore()->give();
if (ok) {
if (success) {
printf("Found PX4Flow on bus %u\n", bus);
dev = std::move(tdev);
break;
}
}
return !!dev;
retry_attempt++;
if (!success) {
hal.scheduler->delay(10);
}
}
return success;
}
// setup the device