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-03-02 22:32:50 -04:00
|
|
|
/********************************************************************************/
|
|
|
|
// Command Event Handlers
|
|
|
|
/********************************************************************************/
|
2011-03-13 03:25:38 -03:00
|
|
|
void handle_process_must()
|
2011-02-24 01:56:59 -04:00
|
|
|
{
|
2011-03-02 22:32:50 -04:00
|
|
|
switch(next_command.id){
|
|
|
|
|
|
|
|
case MAV_CMD_NAV_TAKEOFF:
|
|
|
|
do_takeoff();
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_CMD_NAV_WAYPOINT: // Navigate to Waypoint
|
2011-03-02 22:32:50 -04:00
|
|
|
do_nav_wp();
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_CMD_NAV_LAND: // LAND to Waypoint
|
2011-03-02 22:32:50 -04:00
|
|
|
do_land();
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
case MAV_CMD_NAV_LOITER_UNLIM: // Loiter indefinitely
|
|
|
|
do_loiter_unlimited();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_CMD_NAV_LOITER_TURNS: // Loiter N Times
|
|
|
|
//do_loiter_turns();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_CMD_NAV_LOITER_TIME:
|
|
|
|
do_loiter_time();
|
|
|
|
break;
|
|
|
|
|
2011-02-24 01:56:59 -04:00
|
|
|
case MAV_CMD_NAV_RETURN_TO_LAUNCH:
|
2011-03-02 22:32:50 -04:00
|
|
|
do_RTL();
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
2011-04-02 21:47:20 -03:00
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
default:
|
|
|
|
break;
|
2011-02-24 01:56:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
void handle_process_may()
|
2011-02-24 01:56:59 -04:00
|
|
|
{
|
2011-03-02 22:32:50 -04:00
|
|
|
switch(next_command.id){
|
2011-02-24 01:56:59 -04:00
|
|
|
|
2011-03-02 22:32:50 -04:00
|
|
|
case MAV_CMD_CONDITION_DELAY:
|
2011-03-13 03:25:38 -03:00
|
|
|
do_wait_delay();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_CMD_CONDITION_DISTANCE:
|
|
|
|
do_within_distance();
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
|
|
|
|
2011-03-02 22:32:50 -04:00
|
|
|
case MAV_CMD_CONDITION_CHANGE_ALT:
|
|
|
|
do_change_alt();
|
|
|
|
break;
|
2011-02-24 01:56:59 -04:00
|
|
|
|
2011-03-09 21:06:27 -04:00
|
|
|
case MAV_CMD_CONDITION_YAW:
|
2011-03-02 22:32:50 -04:00
|
|
|
do_yaw();
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2011-03-02 22:32:50 -04:00
|
|
|
}
|
|
|
|
}
|
2011-02-24 01:56:59 -04:00
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
void handle_process_now()
|
2011-02-24 01:56:59 -04:00
|
|
|
{
|
2011-03-02 22:32:50 -04:00
|
|
|
switch(next_command.id){
|
2011-03-13 03:25:38 -03:00
|
|
|
|
|
|
|
case MAV_CMD_DO_JUMP:
|
|
|
|
do_jump();
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
case MAV_CMD_DO_CHANGE_SPEED:
|
|
|
|
//do_change_speed();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_CMD_DO_SET_HOME:
|
|
|
|
do_set_home();
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_CMD_DO_SET_SERVO:
|
2011-03-02 22:32:50 -04:00
|
|
|
do_set_servo();
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_CMD_DO_SET_RELAY:
|
2011-03-02 22:32:50 -04:00
|
|
|
do_set_relay();
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
2011-03-13 03:25:38 -03:00
|
|
|
|
|
|
|
case MAV_CMD_DO_REPEAT_SERVO:
|
|
|
|
do_repeat_servo();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_CMD_DO_REPEAT_RELAY:
|
|
|
|
do_repeat_relay();
|
|
|
|
break;
|
2011-03-27 18:26:34 -03:00
|
|
|
|
2011-05-08 15:32:33 -03:00
|
|
|
case MAV_CMD_DO_SET_ROI:
|
|
|
|
do_target_yaw();
|
2011-02-24 01:56:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
void handle_no_commands()
|
2011-02-24 01:56:59 -04:00
|
|
|
{
|
2011-04-25 02:12:59 -03:00
|
|
|
// we don't want to RTL. Maybe this will change in the future. RTL is kinda dangerous.
|
|
|
|
// use landing commands
|
|
|
|
/*
|
2011-03-02 22:32:50 -04:00
|
|
|
switch (control_mode){
|
|
|
|
default:
|
2011-04-03 18:11:14 -03:00
|
|
|
//set_mode(RTL);
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
2011-03-02 22:32:50 -04:00
|
|
|
}
|
2011-04-25 02:12:59 -03:00
|
|
|
return;
|
|
|
|
*/
|
2011-03-02 22:32:50 -04:00
|
|
|
}
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
/********************************************************************************/
|
|
|
|
// Verify command Handlers
|
|
|
|
/********************************************************************************/
|
|
|
|
|
2011-03-02 22:32:50 -04:00
|
|
|
bool verify_must()
|
|
|
|
{
|
2011-04-21 02:15:45 -03:00
|
|
|
//Serial.printf("vmust: %d\n", command_must_ID);
|
2011-04-03 18:11:14 -03:00
|
|
|
|
2011-03-02 22:32:50 -04:00
|
|
|
switch(command_must_ID) {
|
2011-02-24 01:56:59 -04:00
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
case MAV_CMD_NAV_TAKEOFF:
|
2011-03-02 22:32:50 -04:00
|
|
|
return verify_takeoff();
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_CMD_NAV_LAND:
|
2011-03-02 22:32:50 -04:00
|
|
|
return verify_land();
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
case MAV_CMD_NAV_WAYPOINT:
|
2011-03-02 22:32:50 -04:00
|
|
|
return verify_nav_wp();
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
case MAV_CMD_NAV_LOITER_UNLIM:
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_CMD_NAV_LOITER_TURNS:
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_CMD_NAV_LOITER_TIME:
|
|
|
|
return verify_loiter_time();
|
|
|
|
break;
|
|
|
|
|
2011-03-02 22:32:50 -04:00
|
|
|
case MAV_CMD_NAV_RETURN_TO_LAUNCH:
|
|
|
|
return verify_RTL();
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
|
|
|
|
2011-03-02 22:32:50 -04:00
|
|
|
default:
|
2011-03-19 07:14:15 -03:00
|
|
|
//gcs.send_text_P(SEVERITY_HIGH,PSTR("<verify_must: default> No current Must commands"));
|
2011-03-02 22:32:50 -04:00
|
|
|
return false;
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
2011-03-02 22:32:50 -04:00
|
|
|
}
|
|
|
|
}
|
2011-02-24 01:56:59 -04:00
|
|
|
|
2011-03-02 22:32:50 -04:00
|
|
|
bool verify_may()
|
|
|
|
{
|
|
|
|
switch(command_may_ID) {
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
case MAV_CMD_CONDITION_DELAY:
|
|
|
|
return verify_wait_delay();
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
case MAV_CMD_CONDITION_DISTANCE:
|
|
|
|
return verify_within_distance();
|
2011-03-02 22:32:50 -04:00
|
|
|
break;
|
2011-02-24 01:56:59 -04:00
|
|
|
|
2011-03-02 22:32:50 -04:00
|
|
|
case MAV_CMD_CONDITION_CHANGE_ALT:
|
|
|
|
return verify_change_alt();
|
|
|
|
break;
|
2011-02-24 01:56:59 -04:00
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
case MAV_CMD_CONDITION_YAW:
|
|
|
|
return verify_yaw();
|
|
|
|
break;
|
|
|
|
|
2011-02-24 01:56:59 -04:00
|
|
|
default:
|
2011-03-19 07:14:15 -03:00
|
|
|
//gcs.send_text_P(SEVERITY_HIGH,PSTR("<verify_must: default> No current May commands"));
|
2011-03-02 22:32:50 -04:00
|
|
|
return false;
|
2011-02-24 01:56:59 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-02 22:32:50 -04:00
|
|
|
/********************************************************************************/
|
2011-04-26 02:15:25 -03:00
|
|
|
//
|
2011-03-02 22:32:50 -04:00
|
|
|
/********************************************************************************/
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
void do_RTL(void)
|
|
|
|
{
|
2011-04-26 02:15:25 -03:00
|
|
|
// we need to change this.
|
|
|
|
wp_control = LOITER_MODE;
|
2011-04-03 18:11:14 -03:00
|
|
|
control_mode = LOITER;
|
|
|
|
Location temp = home;
|
|
|
|
temp.alt = read_alt_to_hold();
|
2011-03-13 03:25:38 -03:00
|
|
|
|
|
|
|
//so we know where we are navigating from
|
|
|
|
next_WP = current_loc;
|
|
|
|
|
|
|
|
// Loads WP from Memory
|
|
|
|
// --------------------
|
|
|
|
set_next_WP(&temp);
|
|
|
|
|
|
|
|
// output control mode to the ground station
|
|
|
|
gcs.send_message(MSG_HEARTBEAT);
|
|
|
|
|
2011-04-13 13:33:06 -03:00
|
|
|
//if (g.log_bitmask & MASK_LOG_MODE)
|
|
|
|
// Log_Write_Mode(control_mode);
|
2011-03-13 03:25:38 -03:00
|
|
|
}
|
|
|
|
|
2011-04-26 02:15:25 -03:00
|
|
|
/********************************************************************************/
|
|
|
|
// Nav (Must) commands
|
|
|
|
/********************************************************************************/
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
void do_takeoff()
|
2011-02-24 01:56:59 -04:00
|
|
|
{
|
2011-04-26 02:15:25 -03:00
|
|
|
wp_control = LOITER_MODE;
|
|
|
|
|
2011-04-27 02:08:24 -03:00
|
|
|
// Start with current location
|
2011-04-03 18:11:14 -03:00
|
|
|
Location temp = current_loc;
|
2011-04-25 02:12:59 -03:00
|
|
|
|
|
|
|
// next_command.alt is a relative altitude!!!
|
|
|
|
temp.alt = next_command.alt + home.alt;
|
2011-04-21 02:15:45 -03:00
|
|
|
|
2011-04-03 18:11:14 -03:00
|
|
|
takeoff_complete = false; // set flag to use g_gps ground course during TO. IMU will be doing yaw drift correction
|
|
|
|
|
2011-04-27 02:08:24 -03:00
|
|
|
// Set our waypoint
|
2011-03-02 22:32:50 -04:00
|
|
|
set_next_WP(&temp);
|
|
|
|
}
|
2011-02-24 01:56:59 -04:00
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
void do_nav_wp()
|
|
|
|
{
|
2011-04-26 02:15:25 -03:00
|
|
|
wp_control = WP_MODE;
|
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
// next_command.alt is a relative altitude!!!
|
|
|
|
next_command.alt += home.alt;
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
set_next_WP(&next_command);
|
2011-04-25 02:12:59 -03:00
|
|
|
|
|
|
|
// this is our bitmask to verify we have met all conditions to move on
|
2011-04-18 13:27:03 -03:00
|
|
|
wp_verify_byte = 0;
|
2011-04-25 02:12:59 -03:00
|
|
|
|
|
|
|
// this will be used to remember the time in millis after we reach or pass the WP.
|
2011-04-18 13:27:03 -03:00
|
|
|
loiter_time = 0;
|
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
// this is the delay, stored in seconds and expanded to millis
|
|
|
|
loiter_time_max = next_command.p1 * 1000;
|
|
|
|
|
|
|
|
// if we don't require an altitude minimum, we save this flag as passed (1)
|
2011-04-18 13:27:03 -03:00
|
|
|
if((next_WP.options & WP_OPTION_ALT_REQUIRED) == 0){
|
|
|
|
// we don't need to worry about it
|
|
|
|
wp_verify_byte |= NAV_ALTITUDE;
|
|
|
|
}
|
2011-03-13 03:25:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void do_land()
|
|
|
|
{
|
2011-04-26 02:15:25 -03:00
|
|
|
wp_control = LOITER_MODE;
|
|
|
|
|
2011-05-07 16:41:55 -03:00
|
|
|
//Serial.println("dlnd ");
|
2011-04-25 02:12:59 -03:00
|
|
|
|
|
|
|
// not really used right now, might be good for debugging
|
|
|
|
land_complete = false;
|
|
|
|
|
|
|
|
// A value that drives to 0 when the altitude doesn't change
|
2011-04-03 18:11:14 -03:00
|
|
|
velocity_land = 2000;
|
2011-04-25 02:12:59 -03:00
|
|
|
|
|
|
|
// used to limit decent rate
|
2011-04-20 02:37:05 -03:00
|
|
|
land_start = millis();
|
2011-03-13 03:25:38 -03:00
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
// used to limit decent rate
|
|
|
|
original_alt = current_loc.alt;
|
2011-03-13 03:25:38 -03:00
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
// hold at our current location
|
2011-04-20 02:37:05 -03:00
|
|
|
set_next_WP(¤t_loc);
|
2011-03-13 03:25:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void do_loiter_unlimited()
|
|
|
|
{
|
2011-04-26 02:15:25 -03:00
|
|
|
wp_control = LOITER_MODE;
|
|
|
|
|
2011-04-20 02:37:05 -03:00
|
|
|
//Serial.println("dloi ");
|
2011-04-03 18:11:14 -03:00
|
|
|
if(next_command.lat == 0)
|
|
|
|
set_next_WP(¤t_loc);
|
|
|
|
else
|
|
|
|
set_next_WP(&next_command);
|
2011-03-13 03:25:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void do_loiter_turns()
|
|
|
|
{
|
2011-04-25 02:12:59 -03:00
|
|
|
/*
|
2011-04-26 02:15:25 -03:00
|
|
|
wp_control = LOITER_MODE;
|
|
|
|
|
2011-04-03 18:11:14 -03:00
|
|
|
if(next_command.lat == 0)
|
|
|
|
set_next_WP(¤t_loc);
|
|
|
|
else
|
|
|
|
set_next_WP(&next_command);
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
loiter_total = next_command.p1 * 360;
|
2011-04-25 02:12:59 -03:00
|
|
|
*/
|
2011-03-13 03:25:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void do_loiter_time()
|
|
|
|
{
|
2011-04-25 02:12:59 -03:00
|
|
|
/*
|
2011-04-26 02:15:25 -03:00
|
|
|
wp_control = LOITER_MODE;
|
2011-04-03 18:11:14 -03:00
|
|
|
if(next_command.lat == 0)
|
|
|
|
set_next_WP(¤t_loc);
|
|
|
|
else
|
|
|
|
set_next_WP(&next_command);
|
|
|
|
|
|
|
|
loiter_time = millis();
|
|
|
|
loiter_time_max = next_command.p1 * 1000; // units are (seconds)
|
|
|
|
Serial.printf("dlt %ld, max %ld\n",loiter_time, loiter_time_max);
|
2011-04-25 02:12:59 -03:00
|
|
|
*/
|
2011-03-13 03:25:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************/
|
2011-04-03 18:11:14 -03:00
|
|
|
// Verify Nav (Must) commands
|
2011-03-13 03:25:38 -03:00
|
|
|
/********************************************************************************/
|
|
|
|
|
|
|
|
bool verify_takeoff()
|
2011-03-02 22:32:50 -04:00
|
|
|
{
|
2011-05-07 16:41:55 -03:00
|
|
|
//Serial.printf("vt c_alt:%ld, n_alt:%ld\n", current_loc.alt, next_WP.alt);
|
2011-04-16 17:43:47 -03:00
|
|
|
|
|
|
|
// wait until we are ready!
|
|
|
|
if(g.rc_3.control_in == 0)
|
|
|
|
return false;
|
|
|
|
|
2011-03-02 22:32:50 -04:00
|
|
|
if (current_loc.alt > next_WP.alt){
|
2011-05-07 16:41:55 -03:00
|
|
|
//Serial.println("Y");
|
2011-03-13 03:25:38 -03:00
|
|
|
takeoff_complete = true;
|
2011-03-02 22:32:50 -04:00
|
|
|
return true;
|
2011-04-25 02:12:59 -03:00
|
|
|
|
2011-03-02 22:32:50 -04:00
|
|
|
}else{
|
2011-04-25 02:12:59 -03:00
|
|
|
|
2011-05-07 16:41:55 -03:00
|
|
|
//Serial.println("N");
|
2011-03-02 22:32:50 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
bool verify_land()
|
2011-03-02 22:32:50 -04:00
|
|
|
{
|
2011-04-20 02:37:05 -03:00
|
|
|
// land at 1 meter per second
|
2011-04-25 02:12:59 -03:00
|
|
|
next_WP.alt = original_alt - ((millis() - land_start) / 5); // condition_value = our initial
|
2011-04-20 02:37:05 -03:00
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
velocity_land = ((old_alt - current_loc.alt) *.2) + (velocity_land * .8);
|
|
|
|
old_alt = current_loc.alt;
|
|
|
|
|
2011-04-03 18:11:14 -03:00
|
|
|
if(g.sonar_enabled){
|
2011-04-20 02:37:05 -03:00
|
|
|
// decide which sensor we're using
|
2011-04-08 16:13:31 -03:00
|
|
|
if(sonar_alt < 40){
|
2011-04-03 18:11:14 -03:00
|
|
|
land_complete = true;
|
2011-05-07 16:41:55 -03:00
|
|
|
//Serial.println("Y");
|
2011-04-25 02:12:59 -03:00
|
|
|
//return true;
|
2011-04-03 18:11:14 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-03 21:15:45 -03:00
|
|
|
if(velocity_land <= 0){
|
2011-04-03 18:11:14 -03:00
|
|
|
land_complete = true;
|
2011-04-25 02:12:59 -03:00
|
|
|
//return true;
|
2011-04-03 18:11:14 -03:00
|
|
|
}
|
2011-04-25 02:12:59 -03:00
|
|
|
//Serial.printf("N, %d\n", velocity_land);
|
2011-05-07 16:41:55 -03:00
|
|
|
//Serial.printf("N_alt, %ld\n", next_WP.alt);
|
2011-03-26 03:35:52 -03:00
|
|
|
|
|
|
|
//update_crosstrack();
|
2011-03-13 03:25:38 -03:00
|
|
|
return false;
|
2011-03-02 22:32:50 -04:00
|
|
|
}
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
bool verify_nav_wp()
|
2011-03-02 22:32:50 -04:00
|
|
|
{
|
|
|
|
update_crosstrack();
|
2011-04-08 16:13:31 -03:00
|
|
|
|
2011-04-18 13:27:03 -03:00
|
|
|
// Altitude checking
|
|
|
|
if(next_WP.options & WP_OPTION_ALT_REQUIRED){
|
|
|
|
// we desire a certain minimum altitude
|
|
|
|
if (current_loc.alt > next_WP.alt){
|
|
|
|
// we have reached that altitude
|
|
|
|
wp_verify_byte |= NAV_ALTITUDE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
// Did we pass the WP? // Distance checking
|
|
|
|
if((wp_distance <= g.waypoint_radius) || check_missed_wp()){
|
|
|
|
|
|
|
|
// if we have a distance calc error, wp_distance may be less than 0
|
|
|
|
if(wp_distance > 0){
|
|
|
|
wp_verify_byte |= NAV_LOCATION;
|
|
|
|
|
|
|
|
if(loiter_time == 0){
|
|
|
|
loiter_time = millis();
|
|
|
|
}
|
2011-04-18 13:27:03 -03:00
|
|
|
}
|
2011-04-08 16:13:31 -03:00
|
|
|
}
|
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
// Hold at Waypoint checking, we cant move on until this is OK
|
|
|
|
if(wp_verify_byte & NAV_LOCATION){
|
|
|
|
// we have reached our goal
|
2011-04-08 16:13:31 -03:00
|
|
|
|
2011-04-26 02:15:25 -03:00
|
|
|
// loiter at the WP
|
|
|
|
wp_control = LOITER_MODE;
|
|
|
|
|
2011-04-18 13:27:03 -03:00
|
|
|
if ((millis() - loiter_time) > loiter_time_max) {
|
|
|
|
wp_verify_byte |= NAV_DELAY;
|
|
|
|
//gcs.send_text_P(SEVERITY_LOW,PSTR("verify_must: LOITER time complete"));
|
|
|
|
//Serial.println("vlt done");
|
2011-04-08 16:13:31 -03:00
|
|
|
}
|
2011-03-02 22:32:50 -04:00
|
|
|
}
|
2011-03-13 03:25:38 -03:00
|
|
|
|
2011-05-07 16:41:55 -03:00
|
|
|
//if(wp_verify_byte >= 7){
|
|
|
|
if(wp_verify_byte & NAV_LOCATION){
|
2011-04-18 13:27:03 -03:00
|
|
|
char message[30];
|
2011-04-25 02:12:59 -03:00
|
|
|
sprintf(message,"Reached Command #%i",command_must_index);
|
2011-04-18 13:27:03 -03:00
|
|
|
gcs.send_text(SEVERITY_LOW,message);
|
2011-03-02 22:32:50 -04:00
|
|
|
return true;
|
2011-04-08 16:13:31 -03:00
|
|
|
}else{
|
|
|
|
return false;
|
2011-03-02 22:32:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
bool verify_loiter_unlim()
|
2011-03-02 22:32:50 -04:00
|
|
|
{
|
2011-03-13 03:25:38 -03:00
|
|
|
return false;
|
2011-03-02 22:32:50 -04:00
|
|
|
}
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
bool verify_loiter_time()
|
2011-03-02 22:32:50 -04:00
|
|
|
{
|
2011-04-03 21:15:45 -03:00
|
|
|
//Serial.printf("vlt %ld\n",(millis() - loiter_time));
|
2011-04-03 18:11:14 -03:00
|
|
|
|
|
|
|
if ((millis() - loiter_time) > loiter_time_max) { // scale loiter_time_max from (sec*10) to milliseconds
|
2011-03-19 07:14:15 -03:00
|
|
|
gcs.send_text_P(SEVERITY_LOW,PSTR("verify_must: LOITER time complete"));
|
2011-04-03 21:15:45 -03:00
|
|
|
//Serial.println("vlt done");
|
2011-03-02 22:32:50 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
bool verify_RTL()
|
2011-03-02 22:32:50 -04:00
|
|
|
{
|
|
|
|
if (wp_distance <= g.waypoint_radius) {
|
2011-03-19 07:14:15 -03:00
|
|
|
gcs.send_text_P(SEVERITY_LOW,PSTR("Reached home"));
|
2011-03-02 22:32:50 -04:00
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************/
|
2011-04-03 18:11:14 -03:00
|
|
|
// Condition (May) commands
|
2011-03-02 22:32:50 -04:00
|
|
|
/********************************************************************************/
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
void do_wait_delay()
|
|
|
|
{
|
2011-05-07 16:41:55 -03:00
|
|
|
//Serial.print("dwd ");
|
2011-03-13 03:25:38 -03:00
|
|
|
condition_start = millis();
|
2011-04-03 18:11:14 -03:00
|
|
|
condition_value = next_command.lat * 1000; // convert to milliseconds
|
|
|
|
Serial.println(condition_value,DEC);
|
2011-03-13 03:25:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void do_change_alt()
|
|
|
|
{
|
2011-04-03 18:11:14 -03:00
|
|
|
Location temp = next_WP;
|
2011-03-13 03:25:38 -03:00
|
|
|
condition_start = current_loc.alt;
|
|
|
|
condition_value = next_command.alt + home.alt;
|
2011-04-03 18:11:14 -03:00
|
|
|
temp.alt = condition_value;
|
2011-03-13 03:25:38 -03:00
|
|
|
set_next_WP(&temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_within_distance()
|
|
|
|
{
|
2011-04-03 18:11:14 -03:00
|
|
|
condition_value = next_command.lat;
|
2011-03-13 03:25:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void do_yaw()
|
2011-03-02 22:32:50 -04:00
|
|
|
{
|
2011-05-07 16:41:55 -03:00
|
|
|
//Serial.println("dyaw ");
|
|
|
|
yaw_tracking = MAV_ROI_NONE;
|
2011-03-02 22:32:50 -04:00
|
|
|
|
|
|
|
// target angle in degrees
|
|
|
|
command_yaw_start = nav_yaw; // current position
|
2011-04-03 18:11:14 -03:00
|
|
|
command_yaw_start_time = millis();
|
2011-03-02 22:32:50 -04:00
|
|
|
|
2011-04-03 18:11:14 -03:00
|
|
|
command_yaw_dir = next_command.p1; // 1 = clockwise, 0 = counterclockwise
|
|
|
|
command_yaw_relative = next_command.lng; // 1 = Relative, 0 = Absolute
|
2011-03-02 22:32:50 -04:00
|
|
|
|
2011-04-03 18:11:14 -03:00
|
|
|
command_yaw_speed = next_command.lat * 100; // ms * 100
|
2011-03-02 22:32:50 -04:00
|
|
|
|
|
|
|
|
2011-04-03 18:11:14 -03:00
|
|
|
// if unspecified go 60° a second
|
2011-03-02 22:32:50 -04:00
|
|
|
if(command_yaw_speed == 0)
|
2011-03-26 03:35:52 -03:00
|
|
|
command_yaw_speed = 6000;
|
2011-03-02 22:32:50 -04:00
|
|
|
|
2011-03-26 03:35:52 -03:00
|
|
|
// if unspecified go counterclockwise
|
2011-03-02 22:32:50 -04:00
|
|
|
if(command_yaw_dir == 0)
|
2011-03-26 03:35:52 -03:00
|
|
|
command_yaw_dir = -1;
|
|
|
|
|
|
|
|
if (command_yaw_relative){
|
|
|
|
// relative
|
2011-04-03 18:11:14 -03:00
|
|
|
//command_yaw_dir = (command_yaw_end > 0) ? 1 : -1;
|
|
|
|
//command_yaw_end += nav_yaw;
|
|
|
|
//command_yaw_end = wrap_360(command_yaw_end);
|
|
|
|
command_yaw_delta = next_command.alt * 100;
|
2011-03-02 22:32:50 -04:00
|
|
|
}else{
|
2011-03-26 03:35:52 -03:00
|
|
|
// absolute
|
2011-04-03 18:11:14 -03:00
|
|
|
command_yaw_end = next_command.alt * 100;
|
|
|
|
|
|
|
|
// calculate the delta travel in deg * 100
|
|
|
|
if(command_yaw_dir == 1){
|
|
|
|
if(command_yaw_start >= command_yaw_end){
|
|
|
|
command_yaw_delta = 36000 - (command_yaw_start - command_yaw_end);
|
|
|
|
}else{
|
|
|
|
command_yaw_delta = command_yaw_end - command_yaw_start;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if(command_yaw_start > command_yaw_end){
|
|
|
|
command_yaw_delta = command_yaw_start - command_yaw_end;
|
|
|
|
}else{
|
|
|
|
command_yaw_delta = 36000 + (command_yaw_start - command_yaw_end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
command_yaw_delta = wrap_360(command_yaw_delta);
|
2011-03-02 22:32:50 -04:00
|
|
|
}
|
2011-03-26 03:35:52 -03:00
|
|
|
|
2011-03-02 22:32:50 -04:00
|
|
|
|
|
|
|
// rate to turn deg per second - default is ten
|
2011-04-03 18:11:14 -03:00
|
|
|
command_yaw_time = (command_yaw_delta / command_yaw_speed) * 1000;
|
2011-03-02 22:32:50 -04:00
|
|
|
}
|
|
|
|
|
2011-03-26 03:35:52 -03:00
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
/********************************************************************************/
|
|
|
|
// Verify Condition (May) commands
|
|
|
|
/********************************************************************************/
|
|
|
|
|
|
|
|
bool verify_wait_delay()
|
2011-03-02 22:32:50 -04:00
|
|
|
{
|
2011-05-07 16:41:55 -03:00
|
|
|
//Serial.print("vwd");
|
2011-03-13 03:25:38 -03:00
|
|
|
if ((millis() - condition_start) > condition_value){
|
2011-05-07 16:41:55 -03:00
|
|
|
//Serial.println("y");
|
2011-04-03 18:11:14 -03:00
|
|
|
condition_value = 0;
|
2011-03-02 22:32:50 -04:00
|
|
|
return true;
|
|
|
|
}
|
2011-05-07 16:41:55 -03:00
|
|
|
//Serial.println("n");
|
2011-03-13 03:25:38 -03:00
|
|
|
return false;
|
2011-03-02 22:32:50 -04:00
|
|
|
}
|
2011-02-24 01:56:59 -04:00
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
bool verify_change_alt()
|
2011-03-02 22:32:50 -04:00
|
|
|
{
|
2011-03-13 03:25:38 -03:00
|
|
|
if (condition_start < next_WP.alt){
|
|
|
|
// we are going higer
|
|
|
|
if(current_loc.alt > next_WP.alt){
|
|
|
|
condition_value = 0;
|
|
|
|
return true;
|
|
|
|
}
|
2011-03-02 22:32:50 -04:00
|
|
|
}else{
|
2011-03-13 03:25:38 -03:00
|
|
|
// we are going lower
|
|
|
|
if(current_loc.alt < next_WP.alt){
|
|
|
|
condition_value = 0;
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-24 01:56:59 -04:00
|
|
|
}
|
2011-03-13 03:25:38 -03:00
|
|
|
return false;
|
2011-02-24 01:56:59 -04:00
|
|
|
}
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
bool verify_within_distance()
|
2011-03-02 22:32:50 -04:00
|
|
|
{
|
2011-03-13 03:25:38 -03:00
|
|
|
if (wp_distance < condition_value){
|
|
|
|
condition_value = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-03-02 22:32:50 -04:00
|
|
|
}
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
bool verify_yaw()
|
2011-03-02 22:32:50 -04:00
|
|
|
{
|
2011-05-07 16:41:55 -03:00
|
|
|
//Serial.print("vyaw ");
|
2011-04-03 18:11:14 -03:00
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
if((millis() - command_yaw_start_time) > command_yaw_time){
|
2011-03-26 03:35:52 -03:00
|
|
|
// time out
|
2011-04-03 18:11:14 -03:00
|
|
|
// make sure we hold at the final desired yaw angle
|
2011-03-13 03:25:38 -03:00
|
|
|
nav_yaw = command_yaw_end;
|
2011-05-07 16:41:55 -03:00
|
|
|
//Serial.println("Y");
|
2011-03-02 22:32:50 -04:00
|
|
|
return true;
|
2011-03-26 03:35:52 -03:00
|
|
|
|
2011-03-02 22:32:50 -04:00
|
|
|
}else{
|
2011-03-13 03:25:38 -03:00
|
|
|
// else we need to be at a certain place
|
|
|
|
// power is a ratio of the time : .5 = half done
|
|
|
|
float power = (float)(millis() - command_yaw_start_time) / (float)command_yaw_time;
|
2011-03-26 03:35:52 -03:00
|
|
|
|
2011-04-03 18:11:14 -03:00
|
|
|
nav_yaw = command_yaw_start + ((float)command_yaw_delta * power * command_yaw_dir);
|
|
|
|
nav_yaw = wrap_360(nav_yaw);
|
2011-05-07 16:41:55 -03:00
|
|
|
//Serial.printf("ny %ld\n",nav_yaw);
|
2011-03-02 22:32:50 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************/
|
2011-04-03 18:11:14 -03:00
|
|
|
// Do (Now) commands
|
2011-03-02 22:32:50 -04:00
|
|
|
/********************************************************************************/
|
|
|
|
|
2011-03-27 18:26:34 -03:00
|
|
|
void do_target_yaw()
|
|
|
|
{
|
2011-04-03 18:11:14 -03:00
|
|
|
yaw_tracking = next_command.p1;
|
2011-03-27 18:26:34 -03:00
|
|
|
|
2011-05-07 16:41:55 -03:00
|
|
|
if(yaw_tracking == MAV_ROI_LOCATION){
|
2011-04-03 18:11:14 -03:00
|
|
|
target_WP = next_command;
|
|
|
|
}
|
2011-03-27 18:26:34 -03:00
|
|
|
}
|
|
|
|
|
2011-03-13 03:25:38 -03:00
|
|
|
void do_loiter_at_location()
|
|
|
|
{
|
|
|
|
next_WP = current_loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_jump()
|
|
|
|
{
|
|
|
|
struct Location temp;
|
|
|
|
if(next_command.lat > 0) {
|
|
|
|
|
2011-04-03 18:11:14 -03:00
|
|
|
command_must_index = 0;
|
|
|
|
command_may_index = 0;
|
2011-04-25 02:12:59 -03:00
|
|
|
temp = get_command_with_index(g.waypoint_index);
|
2011-04-03 18:11:14 -03:00
|
|
|
temp.lat = next_command.lat - 1; // Decrement repeat counter
|
2011-03-13 03:25:38 -03:00
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
set_command_with_index(temp, g.waypoint_index);
|
2011-03-13 03:25:38 -03:00
|
|
|
g.waypoint_index.set_and_save(next_command.p1 - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_set_home()
|
2011-03-02 22:32:50 -04:00
|
|
|
{
|
2011-03-13 03:25:38 -03:00
|
|
|
if(next_command.p1 == 1) {
|
|
|
|
init_home();
|
|
|
|
} else {
|
2011-04-03 18:11:14 -03:00
|
|
|
home.id = MAV_CMD_NAV_WAYPOINT;
|
|
|
|
home.lng = next_command.lng; // Lon * 10**7
|
|
|
|
home.lat = next_command.lat; // Lat * 10**7
|
|
|
|
home.alt = max(next_command.alt, 0);
|
2011-03-13 03:25:38 -03:00
|
|
|
home_is_set = true;
|
|
|
|
}
|
2011-03-02 22:32:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void do_set_servo()
|
|
|
|
{
|
2011-03-13 03:25:38 -03:00
|
|
|
APM_RC.OutputCh(next_command.p1 - 1, next_command.alt);
|
2011-03-02 22:32:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void do_set_relay()
|
|
|
|
{
|
2011-03-13 03:25:38 -03:00
|
|
|
if (next_command.p1 == 1) {
|
2011-03-02 22:32:50 -04:00
|
|
|
relay_on();
|
2011-03-13 03:25:38 -03:00
|
|
|
} else if (next_command.p1 == 0) {
|
2011-03-02 22:32:50 -04:00
|
|
|
relay_off();
|
|
|
|
}else{
|
|
|
|
relay_toggle();
|
|
|
|
}
|
2011-03-13 03:25:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void do_repeat_servo()
|
|
|
|
{
|
|
|
|
event_id = next_command.p1 - 1;
|
|
|
|
|
|
|
|
if(next_command.p1 >= CH_5 + 1 && next_command.p1 <= CH_8 + 1) {
|
|
|
|
|
2011-04-03 18:11:14 -03:00
|
|
|
event_timer = 0;
|
|
|
|
event_delay = next_command.lng * 500.0; // /2 (half cycle time) * 1000 (convert to milliseconds)
|
|
|
|
event_repeat = next_command.lat * 2;
|
|
|
|
event_value = next_command.alt;
|
2011-03-13 03:25:38 -03:00
|
|
|
|
|
|
|
switch(next_command.p1) {
|
|
|
|
case CH_5:
|
|
|
|
event_undo_value = g.rc_5.radio_trim;
|
|
|
|
break;
|
|
|
|
case CH_6:
|
|
|
|
event_undo_value = g.rc_6.radio_trim;
|
|
|
|
break;
|
|
|
|
case CH_7:
|
|
|
|
event_undo_value = g.rc_7.radio_trim;
|
|
|
|
break;
|
|
|
|
case CH_8:
|
|
|
|
event_undo_value = g.rc_8.radio_trim;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
update_events();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_repeat_relay()
|
|
|
|
{
|
2011-04-03 18:11:14 -03:00
|
|
|
event_id = RELAY_TOGGLE;
|
|
|
|
event_timer = 0;
|
|
|
|
event_delay = next_command.lat * 500.0; // /2 (half cycle time) * 1000 (convert to milliseconds)
|
2011-03-13 03:25:38 -03:00
|
|
|
event_repeat = next_command.alt * 2;
|
|
|
|
update_events();
|
2011-03-19 07:14:15 -03:00
|
|
|
}
|