Skip to content

Commit df3d35f

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

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
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: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,27 @@ 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
89-
state.distance_m = _distance_m;
90-
state.last_reading_ms = _last_reading_ms;
87+
return;
88+
}
89+
90+
if (!new_data) {
91+
return;
92+
}
93+
94+
if (_status == RangeFinder::Status::Good) {
95+
// copy over states
9196
update_status();
92-
new_data = false;
93-
} else if (_status != RangeFinder::Status::Good) {
94-
//handle additional states received by measurement handler
97+
} else {
98+
// handle additional states received by measurement handler
9599
set_status(_status);
96100
}
101+
102+
state.distance_m = _distance_m;
103+
state.last_reading_ms = _last_reading_ms;
104+
new_data = false;
97105
}
98106

99107
//RangeFinder message handler
@@ -118,14 +126,18 @@ void AP_RangeFinder_DroneCAN::handle_measurement(AP_DroneCAN *ap_dronecan, const
118126
//Additional states supported by RFND message
119127
case UAVCAN_EQUIPMENT_RANGE_SENSOR_MEASUREMENT_READING_TYPE_TOO_CLOSE:
120128
{
129+
driver->_distance_m = msg.range;
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
{
137+
driver->_distance_m = msg.range;
127138
driver->_last_reading_ms = AP_HAL::millis();
128139
driver->_status = RangeFinder::Status::OutOfRangeHigh;
140+
driver->new_data = true;
129141
break;
130142
}
131143
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)