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
|
|
|
static void init_commands()
|
|
|
|
{
|
2011-10-25 22:27:23 -03:00
|
|
|
g.command_index.set_and_save(0);
|
2012-08-21 23:19:51 -03:00
|
|
|
nav_command_ID = NO_COMMAND;
|
|
|
|
non_nav_command_ID = NO_COMMAND;
|
|
|
|
next_nav_command.id = CMD_BLANK;
|
2012-09-22 03:18:32 -03:00
|
|
|
nav_command_index = 0;
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void update_auto()
|
|
|
|
{
|
2012-08-21 23:19:51 -03:00
|
|
|
if (g.command_index >= g.command_total) {
|
|
|
|
handle_no_commands();
|
|
|
|
if(g.command_total == 0) {
|
|
|
|
next_WP.lat = home.lat + 1000; // so we don't have bad calcs
|
|
|
|
next_WP.lng = home.lng + 1000; // so we don't have bad calcs
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(g.command_index != 0) {
|
|
|
|
g.command_index = nav_command_index;
|
|
|
|
nav_command_index--;
|
|
|
|
}
|
|
|
|
nav_command_ID = NO_COMMAND;
|
|
|
|
non_nav_command_ID = NO_COMMAND;
|
|
|
|
next_nav_command.id = CMD_BLANK;
|
|
|
|
process_next_command();
|
|
|
|
}
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// this is only used by an air-start
|
|
|
|
static void reload_commands_airstart()
|
|
|
|
{
|
2012-08-21 23:19:51 -03:00
|
|
|
init_commands();
|
|
|
|
decrement_cmd_index();
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
2012-09-09 20:40:29 -03:00
|
|
|
/*
|
|
|
|
fetch a mission item from EEPROM
|
|
|
|
*/
|
|
|
|
static struct Location get_cmd_with_index_raw(int16_t i)
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2012-08-21 23:19:51 -03:00
|
|
|
struct Location temp;
|
2012-09-07 21:14:32 -03:00
|
|
|
uint16_t mem;
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
|
|
// Find out proper location in memory by using the start_byte position + the index
|
|
|
|
// --------------------------------------------------------------------------------
|
|
|
|
if (i > g.command_total) {
|
|
|
|
memset(&temp, 0, sizeof(temp));
|
|
|
|
temp.id = CMD_BLANK;
|
|
|
|
}else{
|
|
|
|
// read WP position
|
|
|
|
mem = (WP_START_BYTE) + (i * WP_SIZE);
|
2012-12-12 17:58:28 -04:00
|
|
|
temp.id = hal.storage->read_byte(mem);
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
|
|
mem++;
|
2012-12-12 17:58:28 -04:00
|
|
|
temp.options = hal.storage->read_byte(mem);
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
|
|
mem++;
|
2012-12-12 17:58:28 -04:00
|
|
|
temp.p1 = hal.storage->read_byte(mem);
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
|
|
mem++;
|
2012-12-12 17:58:28 -04:00
|
|
|
temp.alt = hal.storage->read_dword(mem);
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
|
|
mem += 4;
|
2012-12-12 17:58:28 -04:00
|
|
|
temp.lat = hal.storage->read_dword(mem);
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
|
|
mem += 4;
|
2012-12-12 17:58:28 -04:00
|
|
|
temp.lng = hal.storage->read_dword(mem);
|
2012-08-21 23:19:51 -03:00
|
|
|
}
|
|
|
|
|
2012-09-09 20:40:29 -03:00
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
fetch a mission item from EEPROM. Adjust altitude to be absolute
|
|
|
|
*/
|
|
|
|
static struct Location get_cmd_with_index(int16_t i)
|
|
|
|
{
|
|
|
|
struct Location temp;
|
|
|
|
|
|
|
|
temp = get_cmd_with_index_raw(i);
|
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
// Add on home altitude if we are a nav command (or other command with altitude) and stored alt is relative
|
|
|
|
if ((temp.id < MAV_CMD_NAV_LAST || temp.id == MAV_CMD_CONDITION_CHANGE_ALT) &&
|
|
|
|
(temp.options & MASK_OPTIONS_RELATIVE_ALT) &&
|
2012-10-11 01:03:52 -03:00
|
|
|
(temp.lat != 0 || temp.lng != 0 || temp.alt != 0)) {
|
2012-08-21 23:19:51 -03:00
|
|
|
temp.alt += home.alt;
|
|
|
|
}
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
return temp;
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setters
|
|
|
|
// -------
|
2012-08-18 06:26:13 -03:00
|
|
|
static void set_cmd_with_index(struct Location temp, int16_t i)
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2012-12-18 22:36:00 -04:00
|
|
|
i = constrain_int16(i, 0, g.command_total.get());
|
2012-12-12 17:58:28 -04:00
|
|
|
uint16_t mem = WP_START_BYTE + (i * WP_SIZE);
|
2012-08-21 23:19:51 -03:00
|
|
|
|
2013-01-13 09:29:53 -04:00
|
|
|
// force home wp to absolute height
|
|
|
|
if (i == 0) {
|
|
|
|
temp.options &= ~(MASK_OPTIONS_RELATIVE_ALT);
|
2012-08-21 23:19:51 -03:00
|
|
|
}
|
2013-01-13 09:29:53 -04:00
|
|
|
// zero unused bits
|
2013-02-04 19:33:10 -04:00
|
|
|
temp.options &= (MASK_OPTIONS_RELATIVE_ALT | MASK_OPTIONS_LOITER_DIRECTION);
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-12-12 17:58:28 -04:00
|
|
|
hal.storage->write_byte(mem, temp.id);
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
mem++;
|
2012-12-12 17:58:28 -04:00
|
|
|
hal.storage->write_byte(mem, temp.options);
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
mem++;
|
2012-12-12 17:58:28 -04:00
|
|
|
hal.storage->write_byte(mem, temp.p1);
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
mem++;
|
2012-12-12 17:58:28 -04:00
|
|
|
hal.storage->write_dword(mem, temp.alt);
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
mem += 4;
|
2012-12-12 17:58:28 -04:00
|
|
|
hal.storage->write_dword(mem, temp.lat);
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
mem += 4;
|
2012-12-12 17:58:28 -04:00
|
|
|
hal.storage->write_dword(mem, temp.lng);
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
2011-10-25 22:27:23 -03:00
|
|
|
static void decrement_cmd_index()
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2011-10-25 22:27:23 -03:00
|
|
|
if (g.command_index > 0) {
|
|
|
|
g.command_index.set_and_save(g.command_index - 1);
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-18 06:26:13 -03:00
|
|
|
static int32_t read_alt_to_hold()
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2012-08-21 23:19:51 -03:00
|
|
|
if (g.RTL_altitude_cm < 0) {
|
|
|
|
return current_loc.alt;
|
2012-08-07 03:05:51 -03:00
|
|
|
}
|
|
|
|
return g.RTL_altitude_cm + home.alt;
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-08-21 23:19:51 -03:00
|
|
|
* This function stores waypoint commands
|
|
|
|
* It looks to see what the next command type is and finds the last command.
|
|
|
|
*/
|
2011-09-08 22:29:39 -03:00
|
|
|
static void set_next_WP(struct Location *wp)
|
|
|
|
{
|
2012-08-21 23:19:51 -03:00
|
|
|
// copy the current WP into the OldWP slot
|
|
|
|
// ---------------------------------------
|
|
|
|
prev_WP = next_WP;
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
// Load the next_WP slot
|
|
|
|
// ---------------------
|
|
|
|
next_WP = *wp;
|
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
|
|
|
|
if (next_WP.lat == 0 && next_WP.lng == 0) {
|
|
|
|
next_WP.lat = current_loc.lat;
|
|
|
|
next_WP.lng = current_loc.lng;
|
|
|
|
// additionally treat zero altitude as current altitude
|
|
|
|
if (next_WP.alt == 0) {
|
|
|
|
next_WP.alt = current_loc.alt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
if (location_passed_point(current_loc, prev_WP, next_WP)) {
|
|
|
|
gcs_send_text_P(SEVERITY_LOW, PSTR("Resetting prev_WP"));
|
|
|
|
prev_WP = current_loc;
|
|
|
|
}
|
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
// used to control FBW and limit the rate of climb
|
|
|
|
// -----------------------------------------------
|
|
|
|
target_altitude_cm = current_loc.alt;
|
|
|
|
|
2013-02-13 04:32:44 -04:00
|
|
|
if (prev_WP.id != MAV_CMD_NAV_TAKEOFF &&
|
|
|
|
prev_WP.alt != home.alt &&
|
|
|
|
(next_WP.id == MAV_CMD_NAV_WAYPOINT || next_WP.id == MAV_CMD_NAV_LAND)) {
|
2012-08-21 23:19:51 -03:00
|
|
|
offset_altitude_cm = next_WP.alt - prev_WP.alt;
|
2013-02-13 04:32:44 -04:00
|
|
|
} else {
|
|
|
|
offset_altitude_cm = 0;
|
|
|
|
}
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
|
|
// zero out our loiter vals to watch for missed waypoints
|
|
|
|
loiter_delta = 0;
|
|
|
|
loiter_sum = 0;
|
|
|
|
loiter_total = 0;
|
|
|
|
|
|
|
|
// this is handy for the groundstation
|
|
|
|
wp_totalDistance = get_distance(¤t_loc, &next_WP);
|
|
|
|
wp_distance = wp_totalDistance;
|
|
|
|
target_bearing_cd = get_bearing_cd(¤t_loc, &next_WP);
|
|
|
|
nav_bearing_cd = target_bearing_cd;
|
|
|
|
|
|
|
|
// to check if we have missed the WP
|
|
|
|
// ----------------------------
|
|
|
|
old_target_bearing_cd = target_bearing_cd;
|
|
|
|
|
|
|
|
// set a new crosstrack bearing
|
|
|
|
// ----------------------------
|
|
|
|
reset_crosstrack();
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void set_guided_WP(void)
|
|
|
|
{
|
2012-08-21 23:19:51 -03:00
|
|
|
// copy the current location into the OldWP slot
|
|
|
|
// ---------------------------------------
|
|
|
|
prev_WP = current_loc;
|
|
|
|
|
|
|
|
// Load the next_WP slot
|
|
|
|
// ---------------------
|
|
|
|
next_WP = guided_WP;
|
|
|
|
|
|
|
|
// used to control FBW and limit the rate of climb
|
|
|
|
// -----------------------------------------------
|
|
|
|
target_altitude_cm = current_loc.alt;
|
|
|
|
offset_altitude_cm = next_WP.alt - prev_WP.alt;
|
|
|
|
|
|
|
|
// this is handy for the groundstation
|
|
|
|
wp_totalDistance = get_distance(¤t_loc, &next_WP);
|
|
|
|
wp_distance = wp_totalDistance;
|
|
|
|
target_bearing_cd = get_bearing_cd(¤t_loc, &next_WP);
|
|
|
|
|
|
|
|
// to check if we have missed the WP
|
|
|
|
// ----------------------------
|
|
|
|
old_target_bearing_cd = target_bearing_cd;
|
|
|
|
|
|
|
|
// set a new crosstrack bearing
|
|
|
|
// ----------------------------
|
|
|
|
reset_crosstrack();
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// run this at setup on the ground
|
|
|
|
// -------------------------------
|
|
|
|
void init_home()
|
|
|
|
{
|
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
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
// block until we get a good fix
|
|
|
|
// -----------------------------
|
|
|
|
while (!g_gps->new_data || !g_gps->fix) {
|
|
|
|
g_gps->update();
|
2012-08-29 20:54:20 -03:00
|
|
|
#if HIL_MODE != HIL_MODE_DISABLED
|
2012-12-03 20:13:33 -04:00
|
|
|
// update hil gps so we have new_data
|
|
|
|
gcs_update();
|
2012-08-29 20:54:20 -03:00
|
|
|
#endif
|
2012-08-21 23:19:51 -03:00
|
|
|
}
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
home.id = MAV_CMD_NAV_WAYPOINT;
|
|
|
|
home.lng = g_gps->longitude; // Lon * 10**7
|
|
|
|
home.lat = g_gps->latitude; // Lat * 10**7
|
|
|
|
home.alt = max(g_gps->altitude, 0);
|
|
|
|
home_is_set = true;
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2011-09-18 05:59:59 -03:00
|
|
|
gcs_send_text_fmt(PSTR("gps alt: %lu"), (unsigned long)home.alt);
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
// Save Home to EEPROM - Command 0
|
|
|
|
// -------------------
|
|
|
|
set_cmd_with_index(home, 0);
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
// Save prev loc
|
|
|
|
// -------------
|
|
|
|
next_WP = prev_WP = home;
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
// Load home for a default guided_WP
|
|
|
|
// -------------
|
|
|
|
guided_WP = home;
|
|
|
|
guided_WP.alt += g.RTL_altitude_cm;
|
2011-09-08 22:29:39 -03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|