2012-02-13 17:53:54 -04:00
|
|
|
/*
|
2012-08-21 23:19:51 -03:00
|
|
|
* logic for dealing with the current command in the mission and home location
|
2012-02-13 17:53:54 -04:00
|
|
|
*/
|
2011-10-25 22:27:23 -03:00
|
|
|
|
2015-05-13 03:09:36 -03:00
|
|
|
#include "Plane.h"
|
|
|
|
|
2011-09-08 22:29:39 -03:00
|
|
|
/*
|
2014-03-17 04:45:45 -03:00
|
|
|
* set_next_WP - sets the target location the vehicle should fly to
|
2012-08-21 23:19:51 -03:00
|
|
|
*/
|
2023-02-02 18:58:40 -04:00
|
|
|
void Plane::set_next_WP(const Location &loc)
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2017-11-21 15:41:45 -04:00
|
|
|
if (auto_state.next_wp_crosstrack) {
|
|
|
|
// copy the current WP into the OldWP slot
|
|
|
|
prev_WP_loc = next_WP_loc;
|
|
|
|
auto_state.crosstrack = true;
|
|
|
|
} else {
|
2014-08-04 07:39:15 -03:00
|
|
|
// we should not try to cross-track for this waypoint
|
|
|
|
prev_WP_loc = current_loc;
|
|
|
|
// use cross-track for the next waypoint
|
2017-11-21 15:41:45 -04:00
|
|
|
auto_state.next_wp_crosstrack = true;
|
|
|
|
auto_state.crosstrack = false;
|
2014-08-04 07:39:15 -03:00
|
|
|
}
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
// Load the next_WP slot
|
|
|
|
// ---------------------
|
2014-03-17 04:45:45 -03:00
|
|
|
next_WP_loc = loc;
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-13 20:40:23 -03:00
|
|
|
// if lat and lon is zero, then use current lat/lon
|
|
|
|
// this allows a mission to contain a "loiter on the spot"
|
|
|
|
// command
|
2014-03-16 01:53:10 -03:00
|
|
|
if (next_WP_loc.lat == 0 && next_WP_loc.lng == 0) {
|
|
|
|
next_WP_loc.lat = current_loc.lat;
|
|
|
|
next_WP_loc.lng = current_loc.lng;
|
2012-08-13 20:40:23 -03:00
|
|
|
// additionally treat zero altitude as current altitude
|
2014-03-16 01:53:10 -03:00
|
|
|
if (next_WP_loc.alt == 0) {
|
|
|
|
next_WP_loc.alt = current_loc.alt;
|
2019-01-02 00:46:09 -04:00
|
|
|
next_WP_loc.relative_alt = false;
|
|
|
|
next_WP_loc.terrain_alt = false;
|
2012-08-13 20:40:23 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-04 00:54:46 -04:00
|
|
|
// convert relative alt to absolute alt
|
2019-01-02 00:46:09 -04:00
|
|
|
if (next_WP_loc.relative_alt) {
|
|
|
|
next_WP_loc.relative_alt = false;
|
2014-03-16 01:53:10 -03:00
|
|
|
next_WP_loc.alt += home.alt;
|
2014-03-04 00:54:46 -04:00
|
|
|
}
|
2012-08-13 20:40:23 -03:00
|
|
|
|
2012-07-21 07:32:07 -03:00
|
|
|
// are we already past the waypoint? This happens when we jump
|
|
|
|
// waypoints, and it can cause us to skip a waypoint. If we are
|
|
|
|
// past the waypoint when we start on a leg, then use the current
|
|
|
|
// location as the previous waypoint, to prevent immediately
|
|
|
|
// considering the waypoint complete
|
2019-04-12 05:23:04 -03:00
|
|
|
if (current_loc.past_interval_finish_line(prev_WP_loc, next_WP_loc)) {
|
2014-03-16 01:53:10 -03:00
|
|
|
prev_WP_loc = current_loc;
|
2012-07-21 07:32:07 -03:00
|
|
|
}
|
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
// zero out our loiter vals to watch for missed waypoints
|
2013-04-11 21:25:46 -03:00
|
|
|
loiter_angle_reset();
|
2012-08-21 23:19:51 -03:00
|
|
|
|
2013-07-05 01:56:58 -03:00
|
|
|
setup_glide_slope();
|
2014-07-24 03:21:30 -03:00
|
|
|
setup_turn_angle();
|
2022-12-06 02:00:18 -04:00
|
|
|
|
|
|
|
// update plane.target_altitude straight away, or if we are too
|
|
|
|
// close to out loiter point we may decide we are at the correct
|
|
|
|
// altitude before updating it (this is based on scheduler table
|
|
|
|
// ordering, where we navigate() before we
|
|
|
|
// adjust_altitude_target(), and navigate() uses values updated in
|
|
|
|
// adjust_altitude_target()
|
|
|
|
adjust_altitude_target();
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
2022-02-09 19:44:58 -04:00
|
|
|
void Plane::set_guided_WP(const Location &loc)
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2022-02-09 19:44:58 -04:00
|
|
|
if (aparm.loiter_radius < 0 || loc.loiter_ccw) {
|
2013-04-21 07:52:50 -03:00
|
|
|
loiter.direction = -1;
|
|
|
|
} else {
|
|
|
|
loiter.direction = 1;
|
|
|
|
}
|
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
// copy the current location into the OldWP slot
|
|
|
|
// ---------------------------------------
|
2014-03-16 01:53:10 -03:00
|
|
|
prev_WP_loc = current_loc;
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
|
|
// Load the next_WP slot
|
|
|
|
// ---------------------
|
2022-02-09 19:44:58 -04:00
|
|
|
next_WP_loc = loc;
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
|
|
// used to control FBW and limit the rate of climb
|
|
|
|
// -----------------------------------------------
|
2014-07-24 03:21:30 -03:00
|
|
|
set_target_altitude_current();
|
2012-08-21 23:19:51 -03:00
|
|
|
|
2013-07-05 01:56:58 -03:00
|
|
|
setup_glide_slope();
|
2014-07-24 03:21:30 -03:00
|
|
|
setup_turn_angle();
|
2012-08-21 23:19:51 -03:00
|
|
|
|
2017-11-21 15:44:04 -04:00
|
|
|
// disable crosstrack, head directly to the point
|
|
|
|
auto_state.crosstrack = false;
|
|
|
|
|
2016-05-05 22:28:26 -03:00
|
|
|
// reset loiter start time.
|
|
|
|
loiter.start_time_ms = 0;
|
|
|
|
|
|
|
|
// start in non-VTOL mode
|
2016-05-11 02:57:41 -03:00
|
|
|
auto_state.vtol_loiter = false;
|
2016-05-05 22:28:26 -03:00
|
|
|
|
2013-04-11 21:25:46 -03:00
|
|
|
loiter_angle_reset();
|
2021-09-16 19:46:03 -03:00
|
|
|
|
|
|
|
#if HAL_QUADPLANE_ENABLED
|
|
|
|
// cancel pending takeoff
|
|
|
|
quadplane.guided_takeoff = false;
|
|
|
|
#endif
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
2013-10-05 05:44:44 -03:00
|
|
|
/*
|
|
|
|
update home location from GPS
|
|
|
|
this is called as long as we have 3D lock and the arming switch is
|
|
|
|
not pushed
|
2023-02-16 18:03:22 -04:00
|
|
|
|
|
|
|
returns true if home is changed
|
2013-10-05 05:44:44 -03:00
|
|
|
*/
|
2023-02-16 18:03:22 -04:00
|
|
|
bool Plane::update_home()
|
2013-10-05 05:44:44 -03:00
|
|
|
{
|
2019-04-21 22:26:55 -03:00
|
|
|
if (hal.util->was_watchdog_armed()) {
|
2023-02-16 18:03:22 -04:00
|
|
|
return false;
|
2019-04-21 22:26:55 -03:00
|
|
|
}
|
2017-08-28 19:20:13 -03:00
|
|
|
if ((g2.home_reset_threshold == -1) ||
|
|
|
|
((g2.home_reset_threshold > 0) &&
|
|
|
|
(fabsf(barometer.get_altitude()) > g2.home_reset_threshold))) {
|
2016-07-19 21:05:12 -03:00
|
|
|
// don't auto-update if we have changed barometer altitude
|
|
|
|
// significantly. This allows us to cope with slow baro drift
|
|
|
|
// but not re-do home and the baro if we have changed height
|
|
|
|
// significantly
|
2023-02-16 18:03:22 -04:00
|
|
|
return false;
|
2016-07-19 21:05:12 -03:00
|
|
|
}
|
2023-02-16 18:03:22 -04:00
|
|
|
bool ret = false;
|
|
|
|
if (ahrs.home_is_set() && !ahrs.home_is_locked() && gps.status() >= AP_GPS::GPS_OK_FIX_3D) {
|
2017-02-27 15:14:29 -04:00
|
|
|
Location loc;
|
2023-02-16 18:03:22 -04:00
|
|
|
if (ahrs.get_location(loc)) {
|
2019-06-28 01:11:38 -03:00
|
|
|
// we take the altitude directly from the GPS as we are
|
|
|
|
// about to reset the baro calibration. We can't use AHRS
|
|
|
|
// altitude or we can end up perpetuating a bias in
|
|
|
|
// altitude, as AHRS alt depends on home alt, which means
|
|
|
|
// we would have a circular dependency
|
|
|
|
loc.alt = gps.location().alt;
|
2023-02-16 18:03:22 -04:00
|
|
|
ret = AP::ahrs().set_home(loc);
|
2017-02-27 15:14:29 -04:00
|
|
|
}
|
2015-02-20 19:14:18 -04:00
|
|
|
}
|
2023-02-16 18:03:22 -04:00
|
|
|
|
|
|
|
// even if home is not updated we do a baro reset to stop baro
|
|
|
|
// drift errors while disarmed
|
2013-10-05 05:44:44 -03:00
|
|
|
barometer.update_calibration();
|
2019-02-07 19:15:47 -04:00
|
|
|
ahrs.resetHeightDatum();
|
2023-02-16 18:03:22 -04:00
|
|
|
|
|
|
|
update_current_loc();
|
|
|
|
|
|
|
|
return ret;
|
2013-10-05 05:44:44 -03:00
|
|
|
}
|
2018-05-18 00:06:46 -03:00
|
|
|
|
2018-05-29 20:47:50 -03:00
|
|
|
bool Plane::set_home_persistently(const Location &loc)
|
2018-05-18 00:06:46 -03:00
|
|
|
{
|
2019-04-21 22:26:55 -03:00
|
|
|
if (hal.util->was_watchdog_armed()) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-29 20:47:50 -03:00
|
|
|
if (!AP::ahrs().set_home(loc)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-18 00:06:46 -03:00
|
|
|
|
|
|
|
// Save Home to EEPROM
|
|
|
|
mission.write_home_to_storage();
|
2018-05-29 20:47:50 -03:00
|
|
|
|
|
|
|
return true;
|
2018-05-18 00:06:46 -03:00
|
|
|
}
|