2012-04-30 04:17:14 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
/* Functions in this file:
|
|
|
|
void init_commands()
|
|
|
|
struct Location get_cmd_with_index(int i)
|
|
|
|
void set_cmd_with_index(struct Location temp, int i)
|
|
|
|
void increment_cmd_index()
|
|
|
|
void decrement_cmd_index()
|
|
|
|
long read_alt_to_hold()
|
|
|
|
void set_next_WP(struct Location *wp)
|
|
|
|
void set_guided_WP(void)
|
|
|
|
void init_home()
|
2012-05-14 12:47:08 -03:00
|
|
|
void restart_nav()
|
2012-04-30 04:17:14 -03:00
|
|
|
************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void init_commands()
|
|
|
|
{
|
|
|
|
g.command_index.set_and_save(0);
|
|
|
|
nav_command_ID = NO_COMMAND;
|
|
|
|
non_nav_command_ID = NO_COMMAND;
|
|
|
|
next_nav_command.id = CMD_BLANK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getters
|
|
|
|
// -------
|
|
|
|
static struct Location get_cmd_with_index(int i)
|
|
|
|
{
|
|
|
|
struct Location temp;
|
2012-12-18 07:44:12 -04:00
|
|
|
uint16_t mem;
|
2012-04-30 04:17:14 -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-18 07:44:12 -04:00
|
|
|
temp.id = hal.storage->read_byte(mem);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
mem++;
|
2012-12-18 07:44:12 -04:00
|
|
|
temp.options = hal.storage->read_byte(mem);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
mem++;
|
2012-12-18 07:44:12 -04:00
|
|
|
temp.p1 = hal.storage->read_byte(mem);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
mem++;
|
2012-12-18 07:44:12 -04:00
|
|
|
temp.alt = (long)hal.storage->read_dword(mem);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
mem += 4;
|
2012-12-18 07:44:12 -04:00
|
|
|
temp.lat = (long)hal.storage->read_dword(mem);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
mem += 4;
|
2012-12-18 07:44:12 -04:00
|
|
|
temp.lng = (long)hal.storage->read_dword(mem);
|
2012-04-30 04:17:14 -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){
|
|
|
|
temp.alt += home.alt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setters
|
|
|
|
// -------
|
|
|
|
static void set_cmd_with_index(struct Location temp, int i)
|
|
|
|
{
|
2012-12-18 22:35:13 -04:00
|
|
|
i = constrain_int16(i, 0, g.command_total.get());
|
2012-12-18 07:44:12 -04:00
|
|
|
uint16_t mem = WP_START_BYTE + (i * WP_SIZE);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
// Set altitude options bitmask
|
|
|
|
// XXX What is this trying to do?
|
2013-04-23 19:32:01 -03:00
|
|
|
if ((temp.options & MASK_OPTIONS_RELATIVE_ALT) && i != 0){
|
2012-04-30 04:17:14 -03:00
|
|
|
temp.options = MASK_OPTIONS_RELATIVE_ALT;
|
|
|
|
} else {
|
|
|
|
temp.options = 0;
|
|
|
|
}
|
|
|
|
|
2012-12-18 07:44:12 -04:00
|
|
|
hal.storage->write_byte(mem, temp.id);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
2012-12-18 07:44:12 -04:00
|
|
|
mem++;
|
|
|
|
hal.storage->write_byte(mem, temp.options);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
mem++;
|
2012-12-18 07:44:12 -04:00
|
|
|
hal.storage->write_byte(mem, temp.p1);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
mem++;
|
2012-12-18 07:44:12 -04:00
|
|
|
hal.storage->write_dword(mem, temp.alt);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
mem += 4;
|
2012-12-18 07:44:12 -04:00
|
|
|
hal.storage->write_dword(mem, temp.lat);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
mem += 4;
|
2012-12-18 07:44:12 -04:00
|
|
|
hal.storage->write_dword(mem, temp.lng);
|
2012-04-30 04:17:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
This function stores waypoint commands
|
|
|
|
It looks to see what the next command type is and finds the last command.
|
|
|
|
*/
|
2013-03-26 08:58:54 -03:00
|
|
|
static void set_next_WP(const struct Location *wp)
|
2012-04-30 04:17:14 -03:00
|
|
|
{
|
|
|
|
// copy the current WP into the OldWP slot
|
|
|
|
// ---------------------------------------
|
|
|
|
prev_WP = next_WP;
|
|
|
|
|
|
|
|
// Load the next_WP slot
|
|
|
|
// ---------------------
|
|
|
|
next_WP = *wp;
|
|
|
|
|
2012-11-18 01:07:50 -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 (location_passed_point(current_loc, prev_WP, next_WP)) {
|
|
|
|
gcs_send_text_P(SEVERITY_LOW, PSTR("Resetting prev_WP"));
|
|
|
|
prev_WP = current_loc;
|
|
|
|
}
|
|
|
|
|
2012-04-30 04:17:14 -03:00
|
|
|
// this is handy for the groundstation
|
2013-08-04 21:18:29 -03:00
|
|
|
wp_totalDistance = get_distance(current_loc, next_WP);
|
2012-04-30 04:17:14 -03:00
|
|
|
wp_distance = wp_totalDistance;
|
2013-08-04 21:18:29 -03:00
|
|
|
target_bearing = get_bearing_cd(current_loc, next_WP);
|
2012-04-30 04:17:14 -03:00
|
|
|
nav_bearing = target_bearing;
|
|
|
|
|
|
|
|
// set a new crosstrack bearing
|
|
|
|
// ----------------------------
|
|
|
|
reset_crosstrack();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_guided_WP(void)
|
|
|
|
{
|
|
|
|
// copy the current location into the OldWP slot
|
|
|
|
// ---------------------------------------
|
|
|
|
prev_WP = current_loc;
|
|
|
|
|
|
|
|
// Load the next_WP slot
|
|
|
|
// ---------------------
|
|
|
|
next_WP = guided_WP;
|
|
|
|
|
|
|
|
// this is handy for the groundstation
|
2013-08-04 21:18:29 -03:00
|
|
|
wp_totalDistance = get_distance(current_loc, next_WP);
|
2012-04-30 04:17:14 -03:00
|
|
|
wp_distance = wp_totalDistance;
|
2013-08-04 21:18:29 -03:00
|
|
|
target_bearing = get_bearing_cd(current_loc, next_WP);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
// set a new crosstrack bearing
|
|
|
|
// ----------------------------
|
|
|
|
reset_crosstrack();
|
|
|
|
}
|
|
|
|
|
|
|
|
// run this at setup on the ground
|
|
|
|
// -------------------------------
|
|
|
|
void init_home()
|
|
|
|
{
|
2012-11-28 07:44:03 -04:00
|
|
|
if (!have_position) {
|
|
|
|
// we need position information
|
|
|
|
return;
|
|
|
|
}
|
2012-04-30 04:17:14 -03:00
|
|
|
|
2012-11-28 07:44:03 -04:00
|
|
|
gcs_send_text_P(SEVERITY_LOW, PSTR("init home"));
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
home.id = MAV_CMD_NAV_WAYPOINT;
|
|
|
|
|
|
|
|
home.lng = g_gps->longitude; // Lon * 10**7
|
|
|
|
home.lat = g_gps->latitude; // Lat * 10**7
|
2013-07-10 01:02:40 -03:00
|
|
|
gps_base_alt = max(g_gps->altitude_cm, 0);
|
|
|
|
home.alt = g_gps->altitude_cm;
|
2012-04-30 04:17:14 -03:00
|
|
|
home_is_set = true;
|
|
|
|
|
|
|
|
// Save Home to EEPROM - Command 0
|
|
|
|
// -------------------
|
|
|
|
set_cmd_with_index(home, 0);
|
|
|
|
|
|
|
|
// Save prev loc
|
|
|
|
// -------------
|
|
|
|
next_WP = prev_WP = home;
|
|
|
|
|
|
|
|
// Load home for a default guided_WP
|
|
|
|
// -------------
|
|
|
|
guided_WP = home;
|
|
|
|
}
|
|
|
|
|
2012-05-14 12:47:08 -03:00
|
|
|
static void restart_nav()
|
|
|
|
{
|
2013-02-07 19:21:30 -04:00
|
|
|
g.pidNavSteer.reset_I();
|
|
|
|
g.pidSpeedThrottle.reset_I();
|
2012-11-28 07:44:03 -04:00
|
|
|
prev_WP = current_loc;
|
|
|
|
nav_command_ID = NO_COMMAND;
|
|
|
|
nav_command_index = 0;
|
|
|
|
process_next_command();
|
2012-05-14 12:47:08 -03:00
|
|
|
}
|