ardupilot/ArduPlane/commands.cpp

149 lines
4.2 KiB
C++
Raw Normal View History

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
*/
2015-05-13 03:09:36 -03:00
#include "Plane.h"
/*
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
*/
2015-05-13 03:09:36 -03:00
void Plane::set_next_WP(const struct Location &loc)
{
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 {
// we should not try to cross-track for this waypoint
prev_WP_loc = current_loc;
// use cross-track for the next waypoint
auto_state.next_wp_crosstrack = true;
auto_state.crosstrack = false;
}
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;
// if lat and lon is zero, then use current lat/lon
// this allows a mission to contain a "loiter on the spot"
// command
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;
// additionally treat zero altitude as current altitude
if (next_WP_loc.alt == 0) {
next_WP_loc.alt = current_loc.alt;
next_WP_loc.relative_alt = false;
next_WP_loc.terrain_alt = false;
}
}
2014-03-04 00:54:46 -04:00
// convert relative alt to absolute alt
if (next_WP_loc.relative_alt) {
next_WP_loc.relative_alt = false;
next_WP_loc.alt += home.alt;
2014-03-04 00:54:46 -04: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
if (current_loc.past_interval_finish_line(prev_WP_loc, next_WP_loc)) {
gcs().send_text(MAV_SEVERITY_NOTICE, "Resetting previous waypoint");
prev_WP_loc = current_loc;
}
2012-08-21 23:19:51 -03:00
// used to control FBW and limit the rate of climb
// -----------------------------------------------
set_target_altitude_location(next_WP_loc);
2012-08-21 23:19:51 -03:00
// zero out our loiter vals to watch for missed waypoints
loiter_angle_reset();
2012-08-21 23:19:51 -03:00
setup_glide_slope();
setup_turn_angle();
}
2015-05-13 03:09:36 -03:00
void Plane::set_guided_WP(void)
{
if (aparm.loiter_radius < 0 || guided_WP_loc.loiter_ccw) {
loiter.direction = -1;
} else {
loiter.direction = 1;
}
2012-08-21 23:19:51 -03:00
// copy the current location into the OldWP slot
// ---------------------------------------
prev_WP_loc = current_loc;
2012-08-21 23:19:51 -03:00
// Load the next_WP slot
// ---------------------
next_WP_loc = guided_WP_loc;
2012-08-21 23:19:51 -03:00
// used to control FBW and limit the rate of climb
// -----------------------------------------------
set_target_altitude_current();
2012-08-21 23:19:51 -03:00
setup_glide_slope();
setup_turn_angle();
2012-08-21 23:19:51 -03:00
// disable crosstrack, head directly to the point
auto_state.crosstrack = false;
// reset loiter start time.
loiter.start_time_ms = 0;
// start in non-VTOL mode
auto_state.vtol_loiter = false;
loiter_angle_reset();
}
/*
update home location from GPS
this is called as long as we have 3D lock and the arming switch is
not pushed
*/
2015-05-13 03:09:36 -03:00
void Plane::update_home()
{
if (hal.util->was_watchdog_armed()) {
return;
}
if ((g2.home_reset_threshold == -1) ||
((g2.home_reset_threshold > 0) &&
(fabsf(barometer.get_altitude()) > g2.home_reset_threshold))) {
// 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
return;
}
if (ahrs.home_is_set() && !ahrs.home_is_locked()) {
Location loc;
if(ahrs.get_position(loc)) {
if (!AP::ahrs().set_home(loc)) {
// silently fail
}
}
}
barometer.update_calibration();
ahrs.resetHeightDatum();
}
2018-05-18 00:06:46 -03:00
bool Plane::set_home_persistently(const Location &loc)
2018-05-18 00:06:46 -03:00
{
if (hal.util->was_watchdog_armed()) {
return false;
}
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();
return true;
2018-05-18 00:06:46 -03:00
}