2016-05-04 00:02:44 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-05-03 23:55:05 -03:00
|
|
|
#include "AP_RangeFinder_MAVLink.h"
|
2022-03-12 06:37:29 -04:00
|
|
|
|
|
|
|
#if AP_RANGEFINDER_MAVLINK_ENABLED
|
|
|
|
|
2016-05-04 00:02:44 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
Set the distance based on a MAVLINK message
|
|
|
|
*/
|
2019-04-30 07:22:49 -03:00
|
|
|
void AP_RangeFinder_MAVLink::handle_msg(const mavlink_message_t &msg)
|
2016-05-04 00:02:44 -03:00
|
|
|
{
|
|
|
|
mavlink_distance_sensor_t packet;
|
2019-04-30 07:22:49 -03:00
|
|
|
mavlink_msg_distance_sensor_decode(&msg, &packet);
|
2016-05-04 00:02:44 -03:00
|
|
|
|
2023-08-14 17:21:56 -03:00
|
|
|
// only accept distances for the configured orientation
|
2021-08-30 11:27:41 -03:00
|
|
|
if (packet.orientation == orientation()) {
|
AP_RangeFinder: support last_reading_ms
Benewake, LeddarOne, LightWareSerial, MAVLink, MaxsonarI2CXL, MaxsonarSerialLV, NMEA, PX4_PWM, uLanding and Wasp already stored the last read time so for these drivers, this change just moves that storage to the state structure
analog, BBB_PRU, Bebop, LightWareI2C, PulsedLightLRF, TeraRangerI2C, VL53L0X did not store the last read time so this was added
2018-08-27 04:02:51 -03:00
|
|
|
state.last_reading_ms = AP_HAL::millis();
|
2016-12-23 02:02:36 -04:00
|
|
|
distance_cm = packet.current_distance;
|
2020-12-05 02:49:18 -04:00
|
|
|
_max_distance_cm = packet.max_distance;
|
|
|
|
_min_distance_cm = packet.min_distance;
|
2020-12-04 21:39:17 -04:00
|
|
|
sensor_type = (MAV_DISTANCE_SENSOR)packet.type;
|
2023-08-14 17:21:56 -03:00
|
|
|
signal_quality = packet.signal_quality;
|
|
|
|
if (signal_quality == 0) {
|
|
|
|
// MAVLink's 0 means invalid/unset, so we map it to -1
|
|
|
|
signal_quality = -1;
|
|
|
|
} else if (signal_quality == 1) {
|
|
|
|
// Map 1 to 0 as that is what ardupilot uses as the worst signal quality
|
|
|
|
signal_quality = 0;
|
|
|
|
}
|
2016-12-23 02:02:36 -04:00
|
|
|
}
|
2016-05-04 00:02:44 -03:00
|
|
|
}
|
|
|
|
|
2020-12-25 04:38:49 -04:00
|
|
|
int16_t AP_RangeFinder_MAVLink::max_distance_cm() const
|
|
|
|
{
|
|
|
|
if (_max_distance_cm == 0 && _min_distance_cm == 0) {
|
|
|
|
// we assume if both of these are zero that we ignore both
|
|
|
|
return params.max_distance_cm;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params.max_distance_cm < _max_distance_cm) {
|
|
|
|
return params.max_distance_cm;
|
|
|
|
}
|
|
|
|
return _max_distance_cm;
|
|
|
|
}
|
|
|
|
int16_t AP_RangeFinder_MAVLink::min_distance_cm() const
|
|
|
|
{
|
|
|
|
if (_max_distance_cm == 0 && _min_distance_cm == 0) {
|
|
|
|
// we assume if both of these are zero that we ignore both
|
|
|
|
return params.min_distance_cm;
|
|
|
|
}
|
|
|
|
if (params.min_distance_cm > _min_distance_cm) {
|
|
|
|
return params.min_distance_cm;
|
|
|
|
}
|
|
|
|
return _min_distance_cm;
|
|
|
|
}
|
|
|
|
|
2016-05-03 23:55:05 -03:00
|
|
|
/*
|
2016-05-04 00:02:44 -03:00
|
|
|
update the state of the sensor
|
|
|
|
*/
|
2016-05-03 23:55:05 -03:00
|
|
|
void AP_RangeFinder_MAVLink::update(void)
|
2016-05-04 00:02:44 -03:00
|
|
|
{
|
|
|
|
//Time out on incoming data; if we don't get new
|
|
|
|
//data in 500ms, dump it
|
AP_RangeFinder: support last_reading_ms
Benewake, LeddarOne, LightWareSerial, MAVLink, MaxsonarI2CXL, MaxsonarSerialLV, NMEA, PX4_PWM, uLanding and Wasp already stored the last read time so for these drivers, this change just moves that storage to the state structure
analog, BBB_PRU, Bebop, LightWareI2C, PulsedLightLRF, TeraRangerI2C, VL53L0X did not store the last read time so this was added
2018-08-27 04:02:51 -03:00
|
|
|
if (AP_HAL::millis() - state.last_reading_ms > AP_RANGEFINDER_MAVLINK_TIMEOUT_MS) {
|
2019-11-01 02:10:52 -03:00
|
|
|
set_status(RangeFinder::Status::NoData);
|
2021-10-18 02:45:33 -03:00
|
|
|
state.distance_m = 0.0f;
|
2016-05-04 00:02:44 -03:00
|
|
|
} else {
|
2021-10-18 02:45:33 -03:00
|
|
|
state.distance_m = distance_cm * 0.01f;
|
2016-10-11 00:06:22 -03:00
|
|
|
update_status();
|
2016-05-04 00:02:44 -03:00
|
|
|
}
|
|
|
|
}
|
2022-03-12 06:37:29 -04:00
|
|
|
|
2023-08-14 17:21:56 -03:00
|
|
|
bool AP_RangeFinder_MAVLink::get_signal_quality_pct(int8_t &quality_pct) const
|
|
|
|
{
|
|
|
|
if (status() != RangeFinder::Status::Good) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
quality_pct = signal_quality;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-12 06:37:29 -04:00
|
|
|
#endif
|