2011-03-19 07:20:11 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2011-03-08 08:20:48 -04:00
|
|
|
|
2011-05-11 03:10:06 -03:00
|
|
|
// For changing active command mid-mission
|
|
|
|
//----------------------------------------
|
2011-11-05 01:41:51 -03:00
|
|
|
static void change_command(uint8_t cmd_index)
|
2011-05-11 03:10:06 -03:00
|
|
|
{
|
2012-11-21 02:08:03 -04:00
|
|
|
//cliSerial->printf("change_command: %d\n",cmd_index );
|
2012-08-16 21:50:03 -03:00
|
|
|
// limit range
|
|
|
|
cmd_index = min(g.command_total - 1, cmd_index);
|
2011-11-16 04:16:34 -04:00
|
|
|
|
2012-08-16 21:50:03 -03:00
|
|
|
// load command
|
|
|
|
struct Location temp = get_cmd_with_index(cmd_index);
|
2011-05-11 03:10:06 -03:00
|
|
|
|
2012-11-21 02:08:03 -04:00
|
|
|
//cliSerial->printf("loading cmd: %d with id:%d\n", cmd_index, temp.id);
|
2011-11-17 01:28:56 -04:00
|
|
|
|
2012-08-16 21:50:03 -03:00
|
|
|
// verify it's a nav command
|
|
|
|
if(temp.id > MAV_CMD_NAV_LAST) {
|
|
|
|
//gcs_send_text_P(SEVERITY_LOW,PSTR("error: non-Nav cmd"));
|
2011-11-16 04:16:34 -04:00
|
|
|
|
2012-08-16 21:50:03 -03:00
|
|
|
}else{
|
|
|
|
// clear out command queue
|
|
|
|
init_commands();
|
2011-11-28 14:31:12 -04:00
|
|
|
|
2012-08-16 21:50:03 -03:00
|
|
|
// copy command to the queue
|
|
|
|
command_nav_queue = temp;
|
|
|
|
command_nav_index = cmd_index;
|
|
|
|
execute_nav_command();
|
|
|
|
}
|
2011-05-11 03:10:06 -03:00
|
|
|
}
|
|
|
|
|
2012-12-09 02:50:50 -04:00
|
|
|
// update_commands - initiates new navigation commands if we have completed the previous command
|
2011-11-05 01:41:51 -03:00
|
|
|
// called by 10 Hz loop
|
2011-11-28 14:31:12 -04:00
|
|
|
static void update_commands()
|
2011-05-14 23:02:09 -03:00
|
|
|
{
|
2012-11-21 02:08:03 -04:00
|
|
|
//cliSerial->printf("update_commands: %d\n",increment );
|
2012-08-16 21:50:03 -03:00
|
|
|
// A: if we do not have any commands there is nothing to do
|
|
|
|
// B: We have completed the mission, don't redo the mission
|
|
|
|
// XXX debug
|
|
|
|
//uint8_t tmp = g.command_index.get();
|
2012-11-21 02:08:03 -04:00
|
|
|
//cliSerial->printf("command_index %u \n", tmp);
|
2012-08-16 21:50:03 -03:00
|
|
|
|
2013-05-24 06:50:11 -03:00
|
|
|
if(g.command_total <= 1)
|
2012-08-16 21:50:03 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
if(command_nav_queue.id == NO_COMMAND) {
|
|
|
|
// Our queue is empty
|
|
|
|
// fill command queue with a new command if available, or exit mission
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
|
|
|
|
// find next nav command
|
|
|
|
int16_t tmp_index;
|
|
|
|
|
|
|
|
if(command_nav_index < g.command_total) {
|
|
|
|
|
|
|
|
// what is the next index for a nav command?
|
|
|
|
tmp_index = find_next_nav_index(command_nav_index + 1);
|
|
|
|
|
|
|
|
if(tmp_index == -1) {
|
|
|
|
exit_mission();
|
|
|
|
return;
|
|
|
|
}else{
|
|
|
|
command_nav_index = tmp_index;
|
|
|
|
command_nav_queue = get_cmd_with_index(command_nav_index);
|
|
|
|
execute_nav_command();
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
// we are out of commands
|
|
|
|
exit_mission();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(command_cond_queue.id == NO_COMMAND) {
|
|
|
|
// Our queue is empty
|
|
|
|
// fill command queue with a new command if available, or do nothing
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
|
|
|
|
// no nav commands completed yet
|
|
|
|
if(prev_nav_index == NO_COMMAND)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(command_cond_index >= command_nav_index) {
|
|
|
|
// don't process the fututre
|
|
|
|
return;
|
|
|
|
|
|
|
|
}else if(command_cond_index == NO_COMMAND) {
|
|
|
|
// start from scratch
|
|
|
|
// look at command after the most recent completed nav
|
|
|
|
command_cond_index = prev_nav_index + 1;
|
|
|
|
|
|
|
|
}else{
|
|
|
|
// we've completed 1 cond, look at next command for another
|
|
|
|
command_cond_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(command_cond_index < (g.command_total -2)) {
|
|
|
|
// we're OK to load a new command (last command must be a nav command)
|
|
|
|
command_cond_queue = get_cmd_with_index(command_cond_index);
|
|
|
|
|
|
|
|
if(command_cond_queue.id > MAV_CMD_CONDITION_LAST) {
|
|
|
|
// this is a do now command
|
|
|
|
process_now_command();
|
|
|
|
|
|
|
|
// clear command queue
|
|
|
|
command_cond_queue.id = NO_COMMAND;
|
|
|
|
|
|
|
|
}else if(command_cond_queue.id > MAV_CMD_NAV_LAST) {
|
|
|
|
// this is a conditional command
|
|
|
|
process_cond_command();
|
|
|
|
|
|
|
|
}else{
|
|
|
|
// this is a nav command, don't process
|
|
|
|
// clear the command conditional queue and index
|
|
|
|
prev_nav_index = NO_COMMAND;
|
|
|
|
command_cond_index = NO_COMMAND;
|
|
|
|
command_cond_queue.id = NO_COMMAND;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
2011-03-02 22:32:50 -04:00
|
|
|
|
2012-12-09 02:50:50 -04:00
|
|
|
// execute_nav_command - performs minor initialisation and logging before next navigation command in the queue is executed
|
2011-11-28 14:31:12 -04:00
|
|
|
static void execute_nav_command(void)
|
|
|
|
{
|
2012-08-16 21:50:03 -03:00
|
|
|
// This is what we report to MAVLINK
|
|
|
|
g.command_index = command_nav_index;
|
2011-11-28 14:31:12 -04:00
|
|
|
|
2012-08-16 21:50:03 -03:00
|
|
|
// Save CMD to Log
|
|
|
|
if(g.log_bitmask & MASK_LOG_CMD)
|
|
|
|
Log_Write_Cmd(g.command_index, &command_nav_queue);
|
2011-11-28 14:31:12 -04:00
|
|
|
|
2012-08-16 21:50:03 -03:00
|
|
|
// clear navigation prameters
|
|
|
|
reset_nav_params();
|
2012-01-03 14:22:36 -04:00
|
|
|
|
2012-08-16 21:50:03 -03:00
|
|
|
// Act on the new command
|
|
|
|
process_nav_command();
|
2011-11-28 14:31:12 -04:00
|
|
|
|
2012-08-16 21:50:03 -03:00
|
|
|
// clear May indexes to force loading of more commands
|
|
|
|
// existing May commands are tossed.
|
|
|
|
command_cond_index = NO_COMMAND;
|
2011-11-28 14:31:12 -04:00
|
|
|
}
|
|
|
|
|
2012-12-09 02:50:50 -04:00
|
|
|
// verify_commands - high level function to check if navigation and conditional commands have completed
|
2011-11-16 04:16:34 -04:00
|
|
|
static void verify_commands(void)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2013-04-24 08:59:49 -03:00
|
|
|
// check if navigation command completed
|
2013-05-10 00:06:29 -03:00
|
|
|
if(verify_nav_command()) {
|
2013-04-24 08:59:49 -03:00
|
|
|
// clear navigation command queue so next command can be loaded
|
2012-08-16 21:50:03 -03:00
|
|
|
command_nav_queue.id = NO_COMMAND;
|
2011-04-25 02:12:59 -03:00
|
|
|
|
2012-08-16 21:50:03 -03:00
|
|
|
// store our most recent executed nav command
|
2013-04-24 08:59:49 -03:00
|
|
|
prev_nav_index = command_nav_index;
|
2011-02-18 23:59:58 -04:00
|
|
|
|
2012-08-16 21:50:03 -03:00
|
|
|
// Wipe existing conditionals
|
2013-04-24 08:59:49 -03:00
|
|
|
command_cond_index = NO_COMMAND;
|
2012-08-16 21:50:03 -03:00
|
|
|
command_cond_queue.id = NO_COMMAND;
|
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2013-04-24 08:59:49 -03:00
|
|
|
// check if conditional command completed
|
2013-05-10 00:06:29 -03:00
|
|
|
if(verify_cond_command()) {
|
2013-04-24 08:59:49 -03:00
|
|
|
// clear conditional command queue so next command can be loaded
|
2012-08-16 21:50:03 -03:00
|
|
|
command_cond_queue.id = NO_COMMAND;
|
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
2012-08-09 20:47:29 -03:00
|
|
|
|
|
|
|
// Finds the next navgation command in EEPROM
|
|
|
|
static int16_t find_next_nav_index(int16_t search_index)
|
|
|
|
{
|
2012-08-16 21:50:03 -03:00
|
|
|
Location tmp;
|
2012-09-13 03:02:55 -03:00
|
|
|
while(search_index < g.command_total) {
|
2012-08-16 21:50:03 -03:00
|
|
|
tmp = get_cmd_with_index(search_index);
|
|
|
|
if(tmp.id <= MAV_CMD_NAV_LAST) {
|
|
|
|
return search_index;
|
|
|
|
}else{
|
|
|
|
search_index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
2012-08-09 20:47:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void exit_mission()
|
|
|
|
{
|
2012-08-16 21:50:03 -03:00
|
|
|
// we are out of commands
|
|
|
|
g.command_index = 255;
|
|
|
|
|
|
|
|
// if we are on the ground, enter stabilize, else Land
|
2012-11-10 01:55:51 -04:00
|
|
|
if(ap.land_complete) {
|
2012-08-16 21:50:03 -03:00
|
|
|
// we will disarm the motors after landing.
|
|
|
|
}else{
|
|
|
|
// If the approach altitude is valid (above 1m), do approach, else land
|
2012-11-29 08:08:19 -04:00
|
|
|
if(g.rtl_alt_final == 0) {
|
2012-08-16 21:50:03 -03:00
|
|
|
set_mode(LAND);
|
|
|
|
}else{
|
|
|
|
set_mode(LOITER);
|
|
|
|
}
|
|
|
|
}
|
2012-08-09 20:47:29 -03:00
|
|
|
|
|
|
|
}
|
|
|
|
|