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-07-17 07:32:00 -03:00
|
|
|
static void change_command(uint8_t index)
|
2011-05-11 03:10:06 -03:00
|
|
|
{
|
|
|
|
struct Location temp = get_command_with_index(index);
|
|
|
|
|
2011-05-12 18:37:38 -03:00
|
|
|
if (temp.id > MAV_CMD_NAV_LAST ){
|
2011-05-11 03:10:06 -03:00
|
|
|
gcs.send_text_P(SEVERITY_LOW,PSTR("error: non-Nav cmd"));
|
|
|
|
} else {
|
|
|
|
command_must_index = NO_COMMAND;
|
|
|
|
next_command.id = NO_COMMAND;
|
|
|
|
g.waypoint_index.set_and_save(index - 1);
|
|
|
|
update_commands();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-14 23:02:09 -03:00
|
|
|
// called by 10 Hz Medium loop
|
|
|
|
// ---------------------------
|
2011-07-17 07:32:00 -03:00
|
|
|
static void update_commands(void)
|
2011-05-14 23:02:09 -03:00
|
|
|
{
|
|
|
|
// fill command queue with a new command if available
|
|
|
|
if(next_command.id == NO_COMMAND){
|
|
|
|
|
|
|
|
// fetch next command if the next command queue is empty
|
|
|
|
// -----------------------------------------------------
|
|
|
|
if (g.waypoint_index < g.waypoint_total) {
|
|
|
|
// only if we have a cmd stored in EEPROM
|
|
|
|
next_command = get_command_with_index(g.waypoint_index + 1);
|
|
|
|
Serial.printf("queue CMD %d\n", next_command.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are we out of must commands and the queue is empty?
|
|
|
|
if(next_command.id == NO_COMMAND && command_must_index == NO_COMMAND){
|
|
|
|
// if no commands were available from EEPROM
|
|
|
|
// And we have no nav commands
|
|
|
|
// --------------------------------------------
|
|
|
|
if (command_must_ID == NO_COMMAND){
|
|
|
|
gcs.send_text_P(SEVERITY_LOW,PSTR("out of commands!"));
|
|
|
|
handle_no_commands();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check to see if we need to act on our command queue
|
|
|
|
if (process_next_command()){
|
|
|
|
Serial.printf("did PNC: %d\n", next_command.id);
|
|
|
|
|
|
|
|
// We acted on the queue - let's debug that
|
|
|
|
// ----------------------------------------
|
|
|
|
print_wp(&next_command, g.waypoint_index);
|
|
|
|
|
|
|
|
// invalidate command queue so a new one is loaded
|
|
|
|
// -----------------------------------------------
|
|
|
|
clear_command_queue();
|
|
|
|
|
|
|
|
// make sure we load the next command index
|
|
|
|
// ----------------------------------------
|
|
|
|
increment_WP_index();
|
2011-04-25 02:12:59 -03:00
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
// called with GPS navigation update - not constantly
|
2011-07-17 07:32:00 -03:00
|
|
|
static void verify_commands(void)
|
2011-03-02 22:32:50 -04:00
|
|
|
{
|
|
|
|
if(verify_must()){
|
2011-05-14 23:02:09 -03:00
|
|
|
Serial.printf("verified must cmd %d\n" , command_must_index);
|
2011-03-02 22:32:50 -04:00
|
|
|
command_must_index = NO_COMMAND;
|
2011-05-16 03:23:21 -03:00
|
|
|
// reset rate controlled nav
|
|
|
|
g.pid_nav_wp.reset_I();
|
|
|
|
|
2011-05-14 23:02:09 -03:00
|
|
|
}else{
|
|
|
|
Serial.printf("verified must false %d\n" , command_must_index);
|
2011-03-02 22:32:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(verify_may()){
|
2011-05-14 23:02:09 -03:00
|
|
|
Serial.printf("verified may cmd %d\n" , command_may_index);
|
2011-03-02 22:32:50 -04:00
|
|
|
command_may_index = NO_COMMAND;
|
2011-03-13 03:25:38 -03:00
|
|
|
command_may_ID = NO_COMMAND;
|
2011-03-02 22:32:50 -04:00
|
|
|
}
|
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static bool
|
2011-05-14 01:30:42 -03:00
|
|
|
process_next_command()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2011-03-13 03:25:38 -03:00
|
|
|
// these are Navigation/Must commands
|
2010-12-19 12:40:33 -04:00
|
|
|
// ---------------------------------
|
2011-04-25 02:12:59 -03:00
|
|
|
if (command_must_index == NO_COMMAND){ // no current command loaded
|
2011-02-18 23:59:58 -04:00
|
|
|
if (next_command.id < MAV_CMD_NAV_LAST ){
|
2011-04-25 02:12:59 -03:00
|
|
|
|
|
|
|
// we remember the index of our mission here
|
2011-05-14 23:02:09 -03:00
|
|
|
command_must_index = g.waypoint_index + 1;
|
2011-04-25 02:12:59 -03:00
|
|
|
|
|
|
|
// Save CMD to Log
|
2011-02-18 23:59:58 -04:00
|
|
|
if (g.log_bitmask & MASK_LOG_CMD)
|
2011-07-02 19:44:59 -03:00
|
|
|
Log_Write_Cmd(g.waypoint_index + 1, &next_command);
|
2011-04-25 02:12:59 -03:00
|
|
|
|
|
|
|
// Act on the new command
|
2010-12-19 12:40:33 -04:00
|
|
|
process_must();
|
2011-05-14 01:30:42 -03:00
|
|
|
return true;
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
}
|
2011-02-18 23:59:58 -04:00
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
// these are Condition/May commands
|
2010-12-19 12:40:33 -04:00
|
|
|
// ----------------------
|
2011-05-14 01:30:42 -03:00
|
|
|
if (command_may_index == NO_COMMAND){
|
2011-02-18 23:59:58 -04:00
|
|
|
if (next_command.id > MAV_CMD_NAV_LAST && next_command.id < MAV_CMD_CONDITION_LAST ){
|
2011-04-25 02:12:59 -03:00
|
|
|
|
|
|
|
// we remember the index of our mission here
|
2011-05-14 23:02:09 -03:00
|
|
|
command_may_index = g.waypoint_index + 1;
|
|
|
|
|
|
|
|
//SendDebug("MSG <pnc> new may ");
|
|
|
|
//SendDebugln(next_command.id,DEC);
|
2011-04-25 02:12:59 -03:00
|
|
|
//Serial.print("new command_may_index ");
|
|
|
|
//Serial.println(command_may_index,DEC);
|
|
|
|
|
|
|
|
// Save CMD to Log
|
2011-02-17 03:09:13 -04:00
|
|
|
if (g.log_bitmask & MASK_LOG_CMD)
|
2011-07-02 19:44:59 -03:00
|
|
|
Log_Write_Cmd(g.waypoint_index + 1, &next_command);
|
2011-04-25 02:12:59 -03:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
process_may();
|
2011-05-14 01:30:42 -03:00
|
|
|
return true;
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
2011-02-18 23:59:58 -04:00
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
// these are Do/Now commands
|
|
|
|
// ---------------------------
|
|
|
|
if (next_command.id > MAV_CMD_CONDITION_LAST){
|
2011-05-14 23:02:09 -03:00
|
|
|
//SendDebug("MSG <pnc> new now ");
|
|
|
|
//SendDebugln(next_command.id,DEC);
|
2011-04-03 18:11:14 -03:00
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
if (g.log_bitmask & MASK_LOG_CMD)
|
2011-07-02 19:44:59 -03:00
|
|
|
Log_Write_Cmd(g.waypoint_index + 1, &next_command);
|
2011-03-13 03:25:38 -03:00
|
|
|
process_now();
|
2011-05-14 01:30:42 -03:00
|
|
|
return true;
|
2011-03-13 03:25:38 -03:00
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
2011-05-14 01:30:42 -03:00
|
|
|
// we did not need any new commands
|
|
|
|
return false;
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
2011-03-02 22:32:50 -04:00
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
/**************************************************/
|
|
|
|
// These functions implement the commands.
|
|
|
|
/**************************************************/
|
2011-07-17 07:32:00 -03:00
|
|
|
static void process_must()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2011-04-03 18:11:14 -03:00
|
|
|
//gcs.send_text_P(SEVERITY_LOW,PSTR("New cmd: <process_must>"));
|
|
|
|
//gcs.send_message(MSG_COMMAND_LIST, g.waypoint_index);
|
2011-04-21 02:15:45 -03:00
|
|
|
//Serial.printf("pmst %d\n", (int)next_command.id);
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
// clear May indexes to force loading of more commands
|
|
|
|
// existing May commands are tossed.
|
2011-03-13 03:25:38 -03:00
|
|
|
command_may_index = NO_COMMAND;
|
|
|
|
command_may_ID = NO_COMMAND;
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
// remember our command ID
|
2011-03-13 03:25:38 -03:00
|
|
|
command_must_ID = next_command.id;
|
2011-04-25 02:12:59 -03:00
|
|
|
|
|
|
|
// implements the Flight Logic
|
2011-03-13 03:25:38 -03:00
|
|
|
handle_process_must();
|
2011-02-18 23:59:58 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void process_may()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2011-04-03 18:11:14 -03:00
|
|
|
//gcs.send_text_P(SEVERITY_LOW,PSTR("<process_may>"));
|
|
|
|
//gcs.send_message(MSG_COMMAND_LIST, g.waypoint_index);
|
|
|
|
Serial.print("pmay");
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
command_may_ID = next_command.id;
|
2011-03-02 22:32:50 -04:00
|
|
|
handle_process_may();
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void process_now()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2011-04-03 18:11:14 -03:00
|
|
|
Serial.print("pnow");
|
2011-03-02 22:32:50 -04:00
|
|
|
handle_process_now();
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|