2011-09-08 22:29:39 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
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
|
|
|
|
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
|
|
|
*/
|
2014-03-17 04:45:45 -03:00
|
|
|
static void set_next_WP(const struct Location& loc)
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2012-08-21 23:19:51 -03:00
|
|
|
// copy the current WP into the OldWP slot
|
|
|
|
// ---------------------------------------
|
2014-03-16 01:53:10 -03:00
|
|
|
prev_WP_loc = next_WP_loc;
|
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;
|
|
|
|
next_WP_loc.flags.relative_alt = false;
|
2014-07-24 21:23:12 -03:00
|
|
|
next_WP_loc.flags.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
|
2014-03-16 01:53:10 -03:00
|
|
|
if (next_WP_loc.flags.relative_alt) {
|
|
|
|
next_WP_loc.flags.relative_alt = false;
|
|
|
|
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
|
2014-03-16 01:53:10 -03:00
|
|
|
if (location_passed_point(current_loc, prev_WP_loc, next_WP_loc)) {
|
2012-07-21 07:32:07 -03:00
|
|
|
gcs_send_text_P(SEVERITY_LOW, PSTR("Resetting prev_WP"));
|
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
|
|
|
// 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
|
|
|
|
|
|
|
// 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();
|
2012-08-21 23:19:51 -03:00
|
|
|
|
2013-04-11 21:25:46 -03:00
|
|
|
loiter_angle_reset();
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void set_guided_WP(void)
|
|
|
|
{
|
2013-04-21 07:52:50 -03:00
|
|
|
if (g.loiter_radius < 0) {
|
|
|
|
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
|
|
|
|
// ---------------------
|
2014-03-16 01:53:10 -03:00
|
|
|
next_WP_loc = guided_WP_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
|
|
|
|
2013-04-11 21:25:46 -03:00
|
|
|
loiter_angle_reset();
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// run this at setup on the ground
|
|
|
|
// -------------------------------
|
2013-10-05 05:44:44 -03:00
|
|
|
static void init_home()
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2012-08-21 23:19:51 -03:00
|
|
|
gcs_send_text_P(SEVERITY_LOW, PSTR("init home"));
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2014-03-28 16:52:48 -03:00
|
|
|
ahrs.set_home(gps.location());
|
2012-08-21 23:19:51 -03:00
|
|
|
home_is_set = true;
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2014-01-03 07:33:40 -04:00
|
|
|
gcs_send_text_fmt(PSTR("gps alt: %lu"), (unsigned long)home.alt);
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2014-03-03 09:50:45 -04:00
|
|
|
// Save Home to EEPROM
|
|
|
|
mission.write_home_to_storage();
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
// Save prev loc
|
|
|
|
// -------------
|
2014-03-16 01:53:10 -03:00
|
|
|
next_WP_loc = prev_WP_loc = home;
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
// Load home for a default guided_WP
|
|
|
|
// -------------
|
2014-03-16 01:53:10 -03:00
|
|
|
guided_WP_loc = home;
|
|
|
|
guided_WP_loc.alt += g.RTL_altitude_cm;
|
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
|
|
|
|
*/
|
|
|
|
static void update_home()
|
|
|
|
{
|
2014-03-28 16:52:48 -03:00
|
|
|
ahrs.set_home(gps.location());
|
2013-10-05 05:44:44 -03:00
|
|
|
barometer.update_calibration();
|
|
|
|
}
|