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"
|
2016-05-04 00:02:44 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2016-05-03 23:55:05 -03:00
|
|
|
/*
|
2016-05-04 00:02:44 -03:00
|
|
|
The constructor also initialises the rangefinder. Note that this
|
|
|
|
constructor is not called until detect() returns true, so we
|
|
|
|
already know that we should setup the rangefinder
|
|
|
|
*/
|
2018-07-04 11:22:17 -03:00
|
|
|
AP_RangeFinder_MAVLink::AP_RangeFinder_MAVLink(RangeFinder::RangeFinder_State &_state, AP_RangeFinder_Params &_params) :
|
|
|
|
AP_RangeFinder_Backend(_state, _params)
|
2016-05-04 00:02:44 -03:00
|
|
|
{
|
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-05-03 23:55:05 -03:00
|
|
|
distance_cm = 0;
|
2016-05-04 00:02:44 -03:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:55:05 -03:00
|
|
|
/*
|
|
|
|
detect if a MAVLink rangefinder is connected. We'll detect by
|
2016-05-12 13:49:45 -03:00
|
|
|
checking a parameter.
|
2016-05-04 00:02:44 -03:00
|
|
|
*/
|
2017-08-07 00:41:01 -03:00
|
|
|
bool AP_RangeFinder_MAVLink::detect()
|
2016-05-04 00:02:44 -03:00
|
|
|
{
|
2016-05-03 23:55:05 -03:00
|
|
|
// Assume that if the user set the RANGEFINDER_TYPE parameter to MAVLink,
|
|
|
|
// there is an attached MAVLink rangefinder
|
2016-05-04 00:02:44 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
|
|
|
2016-12-23 02:02:36 -04:00
|
|
|
// only accept distances for downward facing sensors
|
|
|
|
if (packet.orientation == MAV_SENSOR_ROTATION_PITCH_270) {
|
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;
|
|
|
|
}
|
2017-06-21 06:38:42 -03:00
|
|
|
sensor_type = (MAV_DISTANCE_SENSOR)packet.type;
|
2016-05-04 00:02:44 -03:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-05-04 00:02:44 -03:00
|
|
|
set_status(RangeFinder::RangeFinder_NoData);
|
|
|
|
state.distance_cm = 0;
|
|
|
|
} else {
|
2016-05-03 23:55:05 -03:00
|
|
|
state.distance_cm = distance_cm;
|
2016-10-11 00:06:22 -03:00
|
|
|
update_status();
|
2016-05-04 00:02:44 -03:00
|
|
|
}
|
|
|
|
}
|