|
|
@ -30,6 +30,9 @@ AP_RangeFinder_LightWareI2C::AP_RangeFinder_LightWareI2C(RangeFinder &_ranger, u |
|
|
|
: AP_RangeFinder_Backend(_ranger, instance, _state) |
|
|
|
: AP_RangeFinder_Backend(_ranger, instance, _state) |
|
|
|
, _dev(std::move(dev)) |
|
|
|
, _dev(std::move(dev)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
// call timer() at 20Hz
|
|
|
|
|
|
|
|
_dev->register_periodic_callback(50000, |
|
|
|
|
|
|
|
FUNCTOR_BIND_MEMBER(&AP_RangeFinder_LightWareI2C::timer, bool)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
@ -42,13 +45,21 @@ AP_RangeFinder_Backend *AP_RangeFinder_LightWareI2C::detect(RangeFinder &_ranger |
|
|
|
AP_RangeFinder_LightWareI2C *sensor |
|
|
|
AP_RangeFinder_LightWareI2C *sensor |
|
|
|
= new AP_RangeFinder_LightWareI2C(_ranger, instance, _state, std::move(dev)); |
|
|
|
= new AP_RangeFinder_LightWareI2C(_ranger, instance, _state, std::move(dev)); |
|
|
|
|
|
|
|
|
|
|
|
uint16_t reading_cm; |
|
|
|
if (!sensor) { |
|
|
|
|
|
|
|
|
|
|
|
if (!sensor || !sensor->get_reading(reading_cm)) { |
|
|
|
|
|
|
|
delete sensor; |
|
|
|
delete sensor; |
|
|
|
return nullptr; |
|
|
|
return nullptr; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (sensor->_dev->get_semaphore()->take(0)) { |
|
|
|
|
|
|
|
uint16_t reading_cm; |
|
|
|
|
|
|
|
if (!sensor->get_reading(reading_cm)) { |
|
|
|
|
|
|
|
sensor->_dev->get_semaphore()->give(); |
|
|
|
|
|
|
|
delete sensor; |
|
|
|
|
|
|
|
return nullptr; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
sensor->_dev->get_semaphore()->give(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return sensor; |
|
|
|
return sensor; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -61,12 +72,6 @@ bool AP_RangeFinder_LightWareI2C::get_reading(uint16_t &reading_cm) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exit immediately if we can't take the semaphore
|
|
|
|
|
|
|
|
if (!_dev || !_dev->get_semaphore()->take(1)) { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// read the high and low byte distance registers
|
|
|
|
// read the high and low byte distance registers
|
|
|
|
bool ret = _dev->read((uint8_t *) &val, sizeof(val)); |
|
|
|
bool ret = _dev->read((uint8_t *) &val, sizeof(val)); |
|
|
|
if (ret) { |
|
|
|
if (ret) { |
|
|
@ -74,8 +79,6 @@ bool AP_RangeFinder_LightWareI2C::get_reading(uint16_t &reading_cm) |
|
|
|
reading_cm = be16toh(val); |
|
|
|
reading_cm = be16toh(val); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_dev->get_semaphore()->give(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ret; |
|
|
|
return ret; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -83,11 +86,17 @@ bool AP_RangeFinder_LightWareI2C::get_reading(uint16_t &reading_cm) |
|
|
|
update the state of the sensor |
|
|
|
update the state of the sensor |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void AP_RangeFinder_LightWareI2C::update(void) |
|
|
|
void AP_RangeFinder_LightWareI2C::update(void) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// nothing to do - its all done in the timer()
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool AP_RangeFinder_LightWareI2C::timer(void) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (get_reading(state.distance_cm)) { |
|
|
|
if (get_reading(state.distance_cm)) { |
|
|
|
// update range_valid state based on distance measured
|
|
|
|
// update range_valid state based on distance measured
|
|
|
|
update_status(); |
|
|
|
update_status(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
set_status(RangeFinder::RangeFinder_NoData); |
|
|
|
set_status(RangeFinder::RangeFinder_NoData); |
|
|
|
} |
|
|
|
}
|
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|