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);
|
2018-08-29 22:14:31 -03:00
|
|
|
|
|
|
|
// set target to the closest rally point or home
|
|
|
|
#if AP_RALLY == ENABLED
|
|
|
|
set_desired_location(rover.g2.rally.calc_best_rally_or_home_location(rover.current_loc, ahrs.get_home().alt));
|
|
|
|
#else
|
2017-08-03 05:14:52 -03:00
|
|
|
// set destination
|
2018-08-29 22:14:31 -03:00
|
|
|
set_desired_location(rover.home);
|
|
|
|
#endif
|
|
|
|
|
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
|
2018-08-08 02:18:13 -03:00
|
|
|
calc_steering_to_waypoint(_reached_destination ? rover.current_loc :_origin, _destination, _reversed);
|
|
|
|
calc_throttle(calc_reduced_speed_for_turn_or_distance(_reversed ? -_desired_speed : _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
|
|
|
}
|
|
|
|
}
|