2017-07-18 23:17:45 -03:00
|
|
|
#include "mode.h"
|
|
|
|
#include "Rover.h"
|
|
|
|
|
|
|
|
bool ModeRTL::_enter()
|
|
|
|
{
|
2017-08-03 05:14:52 -03:00
|
|
|
// refuse RTL if home has not been set
|
2018-03-15 22:49:06 -03:00
|
|
|
if (!AP::ahrs().home_is_set()) {
|
2017-08-03 05:14:52 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-05 21:41:28 -04:00
|
|
|
// initialise waypoint speed
|
|
|
|
set_desired_speed_to_default(true);
|
|
|
|
|
2017-08-03 05:14:52 -03:00
|
|
|
// set destination
|
|
|
|
set_desired_location(rover.home);
|
|
|
|
|
2017-08-03 03:19:57 -03:00
|
|
|
// RTL never reverses
|
|
|
|
rover.set_reverse(false);
|
|
|
|
|
2017-07-18 23:17:45 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModeRTL::update()
|
|
|
|
{
|
2018-02-05 02:39:48 -04:00
|
|
|
// calculate distance to home
|
|
|
|
_distance_to_destination = get_distance(rover.current_loc, _destination);
|
|
|
|
const bool near_wp = _distance_to_destination <= rover.g.waypoint_radius;
|
|
|
|
// check if we've reached the destination
|
|
|
|
if (!_reached_destination && (near_wp || location_passed_point(rover.current_loc, _origin, _destination))) {
|
|
|
|
// trigger reached
|
|
|
|
_reached_destination = true;
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "Reached destination");
|
|
|
|
}
|
|
|
|
// determine if we should keep navigating
|
|
|
|
if (!_reached_destination || (rover.is_boat() && !near_wp)) {
|
2017-08-03 05:14:52 -03:00
|
|
|
// continue driving towards destination
|
2017-12-06 22:43:13 -04:00
|
|
|
calc_steering_to_waypoint(_reached_destination ? rover.current_loc :_origin, _destination);
|
2018-04-21 03:45:12 -03:00
|
|
|
calc_throttle(calc_reduced_speed_for_turn_or_distance(_desired_speed), true, false);
|
2017-08-03 05:14:52 -03:00
|
|
|
} else {
|
|
|
|
// we've reached destination so stop
|
2017-08-10 00:06:43 -03:00
|
|
|
stop_vehicle();
|
2017-07-18 23:17:45 -03:00
|
|
|
}
|
|
|
|
}
|