2019-01-15 13:46:13 -04:00
|
|
|
#include "mode.h"
|
|
|
|
#include "Plane.h"
|
|
|
|
|
|
|
|
bool ModeLoiter::_enter()
|
|
|
|
{
|
|
|
|
plane.throttle_allows_nudging = true;
|
|
|
|
plane.auto_throttle_mode = true;
|
|
|
|
plane.auto_navigation_mode = true;
|
|
|
|
plane.do_loiter_at_location();
|
2019-06-05 17:24:15 -03:00
|
|
|
plane.loiter_angle_reset();
|
2019-01-15 13:46:13 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModeLoiter::update()
|
|
|
|
{
|
|
|
|
plane.calc_nav_roll();
|
|
|
|
plane.calc_nav_pitch();
|
|
|
|
plane.calc_throttle();
|
|
|
|
}
|
|
|
|
|
2019-06-07 12:08:28 -03:00
|
|
|
bool ModeLoiter::isHeadingLinedUp(const Location loiterCenterLoc, const Location targetLoc)
|
2019-06-05 15:25:40 -03:00
|
|
|
{
|
2020-01-28 00:14:43 -04:00
|
|
|
// Return true if current heading is aligned to vector to targetLoc.
|
|
|
|
// Tolerance is initially 10 degrees and grows at 10 degrees for each loiter circle completed.
|
|
|
|
|
2019-06-05 15:25:40 -03:00
|
|
|
const uint16_t loiterRadius = abs(plane.aparm.loiter_radius);
|
|
|
|
if (loiterCenterLoc.get_distance(targetLoc) < loiterRadius + loiterRadius*0.05) {
|
|
|
|
/* Whenever next waypoint is within the loiter radius plus 5%,
|
|
|
|
maintaining loiter would prevent us from ever pointing toward the next waypoint.
|
|
|
|
Hence break out of loiter immediately
|
|
|
|
*/
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-07 12:08:28 -03:00
|
|
|
// Bearing in centi-degrees
|
2019-06-05 15:25:40 -03:00
|
|
|
const int32_t bearing_cd = plane.current_loc.get_bearing_to(targetLoc);
|
2020-01-28 00:14:43 -04:00
|
|
|
return isHeadingLinedUp_cd(bearing_cd);
|
2019-06-07 12:08:28 -03:00
|
|
|
}
|
2019-06-05 15:25:40 -03:00
|
|
|
|
2019-06-07 12:08:28 -03:00
|
|
|
|
2020-01-28 00:14:43 -04:00
|
|
|
bool ModeLoiter::isHeadingLinedUp_cd(const int32_t bearing_cd)
|
2019-06-07 12:08:28 -03:00
|
|
|
{
|
2020-01-28 00:14:43 -04:00
|
|
|
// Return true if current heading is aligned to bearing_cd.
|
|
|
|
// Tolerance is initially 10 degrees and grows at 10 degrees for each loiter circle completed.
|
|
|
|
|
2019-06-05 15:25:40 -03:00
|
|
|
// get current heading.
|
2020-04-08 04:39:31 -03:00
|
|
|
const int32_t heading_cd = (wrap_360(degrees(plane.ahrs.groundspeed_vector().angle())))*100;
|
2019-06-05 15:25:40 -03:00
|
|
|
|
|
|
|
const int32_t heading_err_cd = wrap_180_cd(bearing_cd - heading_cd);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Check to see if the the plane is heading toward the land
|
|
|
|
waypoint. We use 20 degrees (+/-10 deg) of margin so that
|
|
|
|
we can handle 200 degrees/second of yaw.
|
|
|
|
|
|
|
|
After every full circle, extend acceptance criteria to ensure
|
|
|
|
aircraft will not loop forever in case high winds are forcing
|
|
|
|
it beyond 200 deg/sec when passing the desired exit course
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Use integer division to get discrete steps
|
2019-06-08 09:49:56 -03:00
|
|
|
const int32_t expanded_acceptance = 1000 * (labs(plane.loiter.sum_cd) / 36000);
|
2019-06-05 15:25:40 -03:00
|
|
|
|
|
|
|
if (labs(heading_err_cd) <= 1000 + expanded_acceptance) {
|
|
|
|
// Want to head in a straight line from _here_ to the next waypoint instead of center of loiter wp
|
|
|
|
|
|
|
|
// 0 to xtrack from center of waypoint, 1 to xtrack from tangent exit location
|
|
|
|
if (plane.next_WP_loc.loiter_xtrack) {
|
|
|
|
plane.next_WP_loc = plane.current_loc;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-29 12:01:34 -03:00
|
|
|
|
2020-08-19 04:51:00 -03:00
|
|
|
void ModeLoiter::navigate()
|
2020-07-29 12:01:34 -03:00
|
|
|
{
|
|
|
|
// Zero indicates to use WP_LOITER_RAD
|
|
|
|
plane.update_loiter(0);
|
|
|
|
}
|
|
|
|
|