Skip to content

Commit 2063989

Browse files
committed
AP_RangeFinder: ensure that too low and high far states are propagated correctly in DroneCAN
1 parent d6f64da commit 2063989

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

libraries/AP_RangeFinder/AP_RangeFinder_Backend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class AP_RangeFinder_Backend
6666
bool has_data() const;
6767

6868
// returns count of consecutive good readings
69+
// note that this method returning zero does not mean that the device is unhealthy:
6970
uint8_t range_valid_count() const { return state.range_valid_count; }
7071

7172
// return a 3D vector defining the position offset of the sensor

libraries/AP_RangeFinder/AP_RangeFinder_DroneCAN.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,28 @@ AP_RangeFinder_DroneCAN* AP_RangeFinder_DroneCAN::get_dronecan_backend(AP_DroneC
8181
void AP_RangeFinder_DroneCAN::update()
8282
{
8383
WITH_SEMAPHORE(_sem);
84-
if ((AP_HAL::millis() - _last_reading_ms) > 500) {
85-
//if data is older than 500ms, report NoData
84+
if ((AP_HAL::millis() - _last_reading_ms) > 500U) {
85+
// if last read was more than 500ms, report NoData
8686
set_status(RangeFinder::Status::NoData);
87-
} else if (_status == RangeFinder::Status::Good && new_data) {
88-
//copy over states
87+
return;
88+
}
89+
90+
if (!new_data) {
91+
return;
92+
}
93+
94+
if (_status == RangeFinder::Status::Good) {
95+
// copy over states
8996
state.distance_m = _distance_m;
9097
state.last_reading_ms = _last_reading_ms;
9198
update_status();
92-
new_data = false;
93-
} else if (_status != RangeFinder::Status::Good) {
94-
//handle additional states received by measurement handler
99+
} else {
100+
// handle additional states received by measurement handler
101+
state.last_reading_ms = _last_reading_ms;
95102
set_status(_status);
96103
}
104+
105+
new_data = false;
97106
}
98107

99108
//RangeFinder message handler
@@ -120,12 +129,14 @@ void AP_RangeFinder_DroneCAN::handle_measurement(AP_DroneCAN *ap_dronecan, const
120129
{
121130
driver->_last_reading_ms = AP_HAL::millis();
122131
driver->_status = RangeFinder::Status::OutOfRangeLow;
132+
driver->new_data = true;
123133
break;
124134
}
125135
case UAVCAN_EQUIPMENT_RANGE_SENSOR_MEASUREMENT_READING_TYPE_TOO_FAR:
126136
{
127137
driver->_last_reading_ms = AP_HAL::millis();
128138
driver->_status = RangeFinder::Status::OutOfRangeHigh;
139+
driver->new_data = true;
129140
break;
130141
}
131142
default:

libraries/AP_RangeFinder/AP_RangeFinder_DroneCAN.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class AP_RangeFinder_DroneCAN : public AP_RangeFinder_Backend {
2828
}
2929
private:
3030
uint8_t _instance;
31+
// _status is the state received from the peripheral - or "NoData" in case of timeout
3132
RangeFinder::Status _status;
3233
float _distance_m;
3334
uint32_t _last_reading_ms;

0 commit comments

Comments
 (0)