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-11-16 04:16:24 -04:00
static void process_nav_command()
2011-02-24 01:56:59 -04:00
{
2011-11-16 04:16:24 -04:00
switch(command_nav_queue.id){
2011-03-02 22:32:50 -04:00
2011-11-09 16:07:15 -04:00
case MAV_CMD_NAV_TAKEOFF: // 22
2011-03-02 22:32:50 -04:00
do_takeoff();
2011-02-24 01:56:59 -04:00
break;
2011-11-09 16:07:15 -04:00
case MAV_CMD_NAV_WAYPOINT: // 16 Navigate to Waypoint
2011-03-02 22:32:50 -04:00
do_nav_wp();
2011-02-24 01:56:59 -04:00
break;
2011-11-09 16:07:15 -04:00
case MAV_CMD_NAV_LAND: // 21 LAND to Waypoint
2012-01-29 02:00:05 -04:00
yaw_mode = YAW_HOLD;
2011-03-02 22:32:50 -04:00
do_land();
2011-02-24 01:56:59 -04:00
break;
2011-11-09 16:07:15 -04:00
case MAV_CMD_NAV_LOITER_UNLIM: // 17 Loiter indefinitely
2011-03-13 03:25:38 -03:00
do_loiter_unlimited();
break;
2011-11-09 16:07:15 -04:00
case MAV_CMD_NAV_LOITER_TURNS: //18 Loiter N Times
2011-09-24 21:40:29 -03:00
do_loiter_turns();
2011-03-13 03:25:38 -03:00
break;
2011-05-14 23:02:09 -03:00
case MAV_CMD_NAV_LOITER_TIME: // 19
2011-03-13 03:25:38 -03:00
do_loiter_time();
break;
2011-11-09 16:07:15 -04:00
case MAV_CMD_NAV_RETURN_TO_LAUNCH: //20
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
2012-07-24 23:02:54 -03:00
// point the copter and camera at a region of interest (ROI)
case MAV_CMD_NAV_ROI: // 80
do_nav_roi();
break;
2011-03-13 03:25:38 -03:00
default:
break;
2011-02-24 01:56:59 -04:00
}
2011-05-31 02:29:06 -03:00
2011-02-24 01:56:59 -04:00
}
2011-11-16 04:16:24 -04:00
static void process_cond_command()
2011-02-24 01:56:59 -04:00
{
2011-11-16 04:16:24 -04:00
switch(command_cond_queue.id){
2011-02-24 01:56:59 -04:00
2011-11-09 16:07:15 -04:00
case MAV_CMD_CONDITION_DELAY: // 112
2011-03-13 03:25:38 -03:00
do_wait_delay();
break;
2011-11-09 16:07:15 -04:00
case MAV_CMD_CONDITION_DISTANCE: // 114
2011-03-13 03:25:38 -03:00
do_within_distance();
2011-02-24 01:56:59 -04:00
break;
2011-11-09 16:07:15 -04:00
case MAV_CMD_CONDITION_CHANGE_ALT: // 113
2011-03-02 22:32:50 -04:00
do_change_alt();
break;
2011-02-24 01:56:59 -04:00
2011-11-09 16:07:15 -04:00
case MAV_CMD_CONDITION_YAW: // 115
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-11-16 04:16:24 -04:00
static void process_now_command()
2011-02-24 01:56:59 -04:00
{
2011-11-16 04:16:24 -04:00
switch(command_cond_queue.id){
2011-03-13 03:25:38 -03:00
2011-11-09 16:07:15 -04:00
case MAV_CMD_DO_JUMP: // 177
2011-03-13 03:25:38 -03:00
do_jump();
2011-02-24 01:56:59 -04:00
break;
2011-11-09 16:07:15 -04:00
case MAV_CMD_DO_CHANGE_SPEED: // 178
2011-09-24 13:49:11 -03:00
do_change_speed();
2011-03-13 03:25:38 -03:00
break;
2011-11-09 16:07:15 -04:00
case MAV_CMD_DO_SET_HOME: // 179
2011-03-13 03:25:38 -03:00
do_set_home();
2011-02-24 01:56:59 -04:00
break;
2011-11-09 16:07:15 -04:00
case MAV_CMD_DO_SET_SERVO: // 183
2011-03-02 22:32:50 -04:00
do_set_servo();
2011-02-24 01:56:59 -04:00
break;
2011-11-09 16:07:15 -04:00
case MAV_CMD_DO_SET_RELAY: // 181
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
2011-11-09 16:07:15 -04:00
case MAV_CMD_DO_REPEAT_SERVO: // 184
2011-03-13 03:25:38 -03:00
do_repeat_servo();
break;
2011-11-09 16:07:15 -04:00
case MAV_CMD_DO_REPEAT_RELAY: // 182
2011-03-13 03:25:38 -03:00
do_repeat_relay();
break;
2011-03-27 18:26:34 -03:00
2012-07-10 19:39:13 -03:00
#if CAMERA == ENABLED
case MAV_CMD_DO_CONTROL_VIDEO: // Control on-board camera capturing. |Camera ID (-1 for all)| Transmission: 0: disabled, 1: enabled compressed, 2: enabled raw| Transmission mode: 0: video stream, >0: single images every n seconds (decimal)| Recording: 0: disabled, 1: enabled compressed, 2: enabled raw| Empty| Empty| Empty|
break;
case MAV_CMD_DO_DIGICAM_CONFIGURE: // Mission command to configure an on-board camera controller system. |Modes: P, TV, AV, M, Etc| Shutter speed: Divisor number for one second| Aperture: F stop number| ISO number e.g. 80, 100, 200, Etc| Exposure type enumerator| Command Identity| Main engine cut-off time before camera trigger in seconds/10 (0 means no cut-off)|
break;
case MAV_CMD_DO_DIGICAM_CONTROL: // Mission command to control an on-board camera controller system. |Session control e.g. show/hide lens| Zoom's absolute position| Zooming step value to offset zoom from the current position| Focus Locking, Unlocking or Re-locking| Shooting Command| Command Identity| Empty|
break;
#endif
#if MOUNT == ENABLED
case MAV_CMD_DO_MOUNT_CONFIGURE: // Mission command to configure a camera mount |Mount operation mode (see MAV_CONFIGURE_MOUNT_MODE enum)| stabilize roll? (1 = yes, 0 = no)| stabilize pitch? (1 = yes, 0 = no)| stabilize yaw? (1 = yes, 0 = no)| Empty| Empty| Empty|
camera_mount.configure_cmd();
break;
case MAV_CMD_DO_MOUNT_CONTROL: // Mission command to control a camera mount |pitch(deg*100) or lat, depending on mount mode.| roll(deg*100) or lon depending on mount mode| yaw(deg*100) or alt (in cm) depending on mount mode| Empty| Empty| Empty| Empty|
camera_mount.control_cmd();
break;
#endif
2012-07-24 23:02:54 -03:00
default:
// do nothing with unrecognized MAVLink messages
break;
2011-02-24 01:56:59 -04:00
}
}
2011-03-13 03:25:38 -03:00
/********************************************************************************/
// Verify command Handlers
/********************************************************************************/
2011-07-17 07:32:00 -03:00
static bool verify_must()
2011-03-02 22:32:50 -04:00
{
2011-11-16 04:16:24 -04:00
switch(command_nav_queue.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;
2012-07-24 23:02:54 -03:00
case MAV_CMD_NAV_WAYPOINT:
return verify_nav_wp();
break;
2011-02-24 01:56:59 -04:00
case MAV_CMD_NAV_LAND:
2012-01-29 02:00:05 -04:00
if(g.sonar_enabled == true){
return verify_land_sonar();
}else{
return verify_land_baro();
}
2011-02-24 01:56:59 -04:00
break;
2011-03-13 03:25:38 -03:00
case MAV_CMD_NAV_LOITER_UNLIM:
2012-07-12 12:52:36 -03:00
return verify_loiter_unlimited();
2011-03-13 03:25:38 -03:00
break;
case MAV_CMD_NAV_LOITER_TURNS:
2011-09-21 17:19:36 -03:00
return verify_loiter_turns();
2011-03-13 03:25:38 -03:00
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;
2012-07-24 23:02:54 -03:00
case MAV_CMD_NAV_ROI: // 80
return verify_nav_roi();
break;
2011-03-02 22:32:50 -04:00
default:
2011-10-11 06:12:37 -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-07-17 07:32:00 -03:00
static bool verify_may()
2011-03-02 22:32:50 -04:00
{
2011-11-16 04:16:24 -04:00
switch(command_cond_queue.id) {
2011-03-02 22:32:50 -04:00
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-10-11 06:12:37 -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-07-17 07:32:00 -03:00
static void do_RTL(void)
2011-03-13 03:25:38 -03:00
{
2011-09-24 13:49:11 -03:00
// TODO: Altitude option from mission planner
2011-04-03 18:11:14 -03:00
Location temp = home;
2012-06-30 19:34:55 -03:00
temp.alt = get_RTL_alt();
2011-03-13 03:25:38 -03:00
//so we know where we are navigating from
2011-09-04 21:15:36 -03:00
// --------------------------------------
2012-01-11 03:33:49 -04:00
next_WP = current_loc;
2011-03-13 03:25:38 -03:00
// Loads WP from Memory
// --------------------
set_next_WP(&temp);
2012-02-19 17:12:23 -04:00
// We want to come home and stop on a dime
slow_wp = true;
2011-03-13 03:25:38 -03:00
// output control mode to the ground station
2011-09-04 21:15:36 -03:00
// -----------------------------------------
2011-10-11 06:12:37 -03:00
gcs_send_message(MSG_HEARTBEAT);
2011-03-13 03:25:38 -03:00
}
2011-04-26 02:15:25 -03:00
/********************************************************************************/
// Nav (Must) commands
/********************************************************************************/
2011-07-17 07:32:00 -03:00
static void do_takeoff()
2011-02-24 01:56:59 -04:00
{
2011-09-07 01:44:01 -03:00
wp_control = LOITER_MODE;
2011-04-26 02:15:25 -03:00
2011-04-27 02:08:24 -03:00
// Start with current location
2011-09-07 01:44:01 -03:00
Location temp = current_loc;
2011-04-25 02:12:59 -03:00
2012-07-03 21:19:19 -03:00
// alt is always relative
temp.alt = command_nav_queue.alt;
2011-04-21 02:15:45 -03:00
2012-02-11 02:54:29 -04:00
// prevent flips
reset_I_all();
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-07-17 07:32:00 -03:00
static void do_nav_wp()
2011-03-13 03:25:38 -03:00
{
2011-04-26 02:15:25 -03:00
wp_control = WP_MODE;
2012-07-19 13:48:54 -03:00
slow_wp = false;
2011-04-26 02:15:25 -03:00
2011-11-16 04:16:24 -04:00
set_next_WP(&command_nav_queue);
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
2011-11-16 04:16:24 -04:00
loiter_time_max = command_nav_queue.p1 * 1000;
2011-04-25 02:12:59 -03:00
2012-07-03 21:19:19 -03:00
if((next_WP.options & WP_OPTION_ALT_REQUIRED) == false){
2011-04-18 13:27:03 -03:00
wp_verify_byte |= NAV_ALTITUDE;
}
2011-03-13 03:25:38 -03:00
}
2011-07-17 07:32:00 -03:00
static void do_land()
2011-03-13 03:25:38 -03:00
{
2011-04-26 02:15:25 -03:00
wp_control = LOITER_MODE;
2012-01-29 02:00:05 -04:00
// just to make sure
2011-04-25 02:12:59 -03:00
land_complete = false;
2012-01-29 02:00:05 -04:00
// landing boost lowers the main throttle to mimmick
// the effect of a user's hand
landing_boost = 0;
// A counter that goes up if our climb rate stalls out.
ground_detector = 0;
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);
2012-01-11 03:33:49 -04:00
2012-07-19 02:41:45 -03:00
// Set a new target altitude
set_new_altitude(0);
2011-03-13 03:25:38 -03:00
}
2011-07-17 07:32:00 -03:00
static void do_loiter_unlimited()
2011-03-13 03:25:38 -03:00
{
2011-04-26 02:15:25 -03:00
wp_control = LOITER_MODE;
2011-04-20 02:37:05 -03:00
//Serial.println("dloi ");
2012-07-19 02:41:45 -03:00
if(command_nav_queue.lat == 0){
2011-04-03 18:11:14 -03:00
set_next_WP(¤t_loc);
2012-07-19 02:41:45 -03:00
wp_control = LOITER_MODE;
}else{
2011-11-16 04:16:24 -04:00
set_next_WP(&command_nav_queue);
2012-07-19 02:41:45 -03:00
wp_control = WP_MODE;
}
2011-03-13 03:25:38 -03:00
}
2011-07-17 07:32:00 -03:00
static void do_loiter_turns()
2011-03-13 03:25:38 -03:00
{
2011-10-11 06:23:42 -03:00
wp_control = CIRCLE_MODE;
2011-04-26 02:15:25 -03:00
2011-12-15 20:10:43 -04:00
if(command_nav_queue.lat == 0){
// allow user to specify just the altitude
if(command_nav_queue.alt > 0){
current_loc.alt = command_nav_queue.alt;
}
2011-04-03 18:11:14 -03:00
set_next_WP(¤t_loc);
2011-12-15 20:10:43 -04:00
}else{
2011-11-16 04:16:24 -04:00
set_next_WP(&command_nav_queue);
2011-12-15 20:10:43 -04:00
}
2011-04-03 18:11:14 -03:00
2012-07-12 13:22:20 -03:00
circle_WP = next_WP;
2011-11-16 04:16:24 -04:00
loiter_total = command_nav_queue.p1 * 360;
2011-09-21 17:19:36 -03:00
loiter_sum = 0;
2011-11-16 04:16:24 -04:00
old_target_bearing = target_bearing;
2012-01-14 15:21:50 -04:00
circle_angle = target_bearing + 18000;
circle_angle = wrap_360(circle_angle);
circle_angle *= RADX100;
2011-03-13 03:25:38 -03:00
}
2011-07-17 07:32:00 -03:00
static void do_loiter_time()
2011-03-13 03:25:38 -03:00
{
2011-11-16 04:16:24 -04:00
if(command_nav_queue.lat == 0){
2011-09-24 21:40:29 -03:00
wp_control = LOITER_MODE;
loiter_time = millis();
set_next_WP(¤t_loc);
}else{
wp_control = WP_MODE;
2011-11-16 04:16:24 -04:00
set_next_WP(&command_nav_queue);
2011-09-24 21:40:29 -03:00
}
2011-11-16 04:16:24 -04:00
loiter_time_max = command_nav_queue.p1 * 1000; // units are (seconds)
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
/********************************************************************************/
2011-07-17 07:32:00 -03:00
static bool verify_takeoff()
2011-03-02 22:32:50 -04:00
{
2011-04-16 17:43:47 -03:00
// wait until we are ready!
2011-05-14 23:02:09 -03:00
if(g.rc_3.control_in == 0){
2011-04-16 17:43:47 -03:00
return false;
2011-05-14 23:02:09 -03:00
}
2011-12-26 19:15:31 -04:00
// are we above our target altitude?
2012-08-09 20:46:40 -03:00
return (current_loc.alt > next_WP.alt);
2011-03-02 22:32:50 -04:00
}
2012-01-20 14:08:27 -04:00
// called at 10hz
2012-01-29 02:00:05 -04:00
static bool verify_land_sonar()
2011-03-02 22:32:50 -04:00
{
2012-01-29 02:00:05 -04:00
if(current_loc.alt > 300){
wp_control = LOITER_MODE;
ground_detector = 0;
2012-01-11 03:33:49 -04:00
}else{
2012-01-29 02:00:05 -04:00
// begin to pull down on the throttle
landing_boost++;
2012-02-08 09:46:32 -04:00
landing_boost = min(landing_boost, 40);
2012-01-04 03:29:28 -04:00
}
2011-03-13 03:25:38 -03:00
2012-01-29 02:00:05 -04:00
if(current_loc.alt < 200 ){
2012-01-29 02:35:57 -04:00
wp_control = NO_NAV_MODE;
2012-01-29 02:00:05 -04:00
}
2012-01-20 14:08:27 -04:00
2012-01-29 02:00:05 -04:00
if(current_loc.alt < 150 ){
2012-02-08 09:46:32 -04:00
// if we are low or don't seem to be decending much, increment ground detector
2012-07-19 02:45:31 -03:00
if(current_loc.alt < 80 || abs(climb_rate) < 20) {
2012-02-08 09:46:32 -04:00
landing_boost++; // reduce the throttle at twice the normal rate
2012-05-30 13:34:31 -03:00
if(ground_detector < 30) {
2012-05-30 13:36:16 -03:00
ground_detector++;
2012-05-30 13:34:31 -03:00
}else if (ground_detector == 30){
2012-01-29 02:00:05 -04:00
land_complete = true;
2012-02-09 01:26:48 -04:00
if(g.rc_3.control_in == 0){
2012-05-30 13:44:59 -03:00
ground_detector++;
2012-02-09 01:26:48 -04:00
init_disarm_motors();
}
2012-01-29 02:00:05 -04:00
return true;
}
}
}
return false;
}
2012-01-20 14:08:27 -04:00
2012-01-29 02:00:05 -04:00
static bool verify_land_baro()
{
if(current_loc.alt > 300){
wp_control = LOITER_MODE;
ground_detector = 0;
2012-01-13 02:27:51 -04:00
}else{
2012-01-29 02:00:05 -04:00
// begin to pull down on the throttle
landing_boost++;
landing_boost = min(landing_boost, 40);
}
2012-01-29 02:35:57 -04:00
2012-07-19 02:45:31 -03:00
if(current_loc.alt < 100 ){
2012-01-29 02:00:05 -04:00
wp_control = NO_NAV_MODE;
2011-12-15 02:04:11 -04:00
}
2012-07-19 02:45:31 -03:00
if(current_loc.alt < 200 ){
if(abs(climb_rate) < 40) {
2012-01-29 02:35:57 -04:00
landing_boost++;
2012-05-30 13:34:31 -03:00
if(ground_detector < 30) {
2012-05-30 13:36:16 -03:00
ground_detector++;
2012-05-30 13:34:31 -03:00
}else if (ground_detector == 30){
2012-01-29 02:00:05 -04:00
land_complete = true;
2012-02-09 01:26:48 -04:00
if(g.rc_3.control_in == 0){
2012-05-30 13:44:59 -03:00
ground_detector++;
2012-02-09 01:26:48 -04:00
init_disarm_motors();
}
2012-01-29 02:00:05 -04:00
return true;
}
}
2011-04-03 18:11:14 -03:00
}
2011-03-13 03:25:38 -03:00
return false;
2011-03-02 22:32:50 -04:00
}
2011-07-17 07:32:00 -03:00
static bool verify_nav_wp()
2011-03-02 22:32:50 -04:00
{
2011-04-18 13:27:03 -03:00
// Altitude checking
2012-07-03 21:19:19 -03:00
if(next_WP.options & WP_OPTION_ALT_REQUIRED){
2011-04-18 13:27:03 -03:00
// we desire a certain minimum altitude
2012-07-03 21:19:19 -03:00
if(alt_change_flag == REACHED_ALT){
2012-01-11 03:33:49 -04:00
2011-04-18 13:27:03 -03:00
// 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
2012-08-10 14:02:32 -03:00
if((wp_distance <= (waypoint_radius * 100)) || check_missed_wp()){
2011-04-25 02:12:59 -03:00
// 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;
2011-10-11 06:12:37 -03:00
//gcs_send_text_P(SEVERITY_LOW,PSTR("verify_must: LOITER time complete"));
2011-04-18 13:27:03 -03:00
//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-14 23:02:09 -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-11-16 04:16:24 -04:00
sprintf(message,"Reached Command #%i",command_nav_index);
2011-10-11 06:12:37 -03:00
gcs_send_text(SEVERITY_LOW,message);
2011-05-12 18:37:38 -03:00
wp_verify_byte = 0;
2012-05-22 10:57:38 -03:00
copter_leds_nav_blink = 15; // Cause the CopterLEDs to blink three times to indicate waypoint reached
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
}
}
2012-07-12 12:52:36 -03:00
static bool verify_loiter_unlimited()
{
if(wp_control == WP_MODE && wp_distance <= (g.waypoint_radius * 100)){
// switch to position hold
wp_control = LOITER_MODE;
}
return false;
}
2011-03-02 22:32:50 -04:00
2011-07-17 07:32:00 -03:00
static bool verify_loiter_time()
2011-03-02 22:32:50 -04:00
{
2011-09-24 21:40:29 -03:00
if(wp_control == LOITER_MODE){
if ((millis() - loiter_time) > loiter_time_max) {
return true;
}
}
2012-07-10 21:16:57 -03:00
if(wp_control == WP_MODE && wp_distance <= (g.waypoint_radius * 100)){
2011-09-24 21:40:29 -03:00
// reset our loiter time
loiter_time = millis();
// switch to position hold
wp_control = LOITER_MODE;
2011-03-02 22:32:50 -04:00
}
return false;
}
2011-09-21 17:19:36 -03:00
static bool verify_loiter_turns()
{
2011-11-16 04:21:12 -04:00
//Serial.printf("loiter_sum: %d \n", loiter_sum);
2011-09-21 17:19:36 -03:00
// have we rotated around the center enough times?
// -----------------------------------------------
2011-10-13 00:59:12 -03:00
if(abs(loiter_sum) > loiter_total) {
2011-09-21 17:19:36 -03:00
loiter_total = 0;
loiter_sum = 0;
//gcs_send_text_P(SEVERITY_LOW,PSTR("verify_must: LOITER orbits complete"));
// clear the command queue;
return true;
}
return false;
}
2011-07-17 07:32:00 -03:00
static bool verify_RTL()
2011-03-02 22:32:50 -04:00
{
2011-11-08 01:09:29 -04:00
wp_control = WP_MODE;
2011-12-07 01:03:56 -04:00
// Did we pass the WP? // Distance checking
2012-07-10 21:16:57 -03:00
if((wp_distance <= (g.waypoint_radius * 100)) || check_missed_wp()){
2011-11-08 01:09:29 -04:00
wp_control = LOITER_MODE;
2011-10-11 06:12:37 -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-07-17 07:32:00 -03:00
static void do_wait_delay()
2011-03-13 03:25:38 -03:00
{
2011-05-07 16:41:55 -03:00
//Serial.print("dwd ");
2011-03-13 03:25:38 -03:00
condition_start = millis();
2011-11-16 04:16:24 -04:00
condition_value = command_cond_queue.lat * 1000; // convert to milliseconds
2011-10-02 15:36:23 -03:00
//Serial.println(condition_value,DEC);
2011-03-13 03:25:38 -03:00
}
2011-07-17 07:32:00 -03:00
static void do_change_alt()
2011-03-13 03:25:38 -03:00
{
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;
2012-01-10 00:23:37 -04:00
//condition_value = command_cond_queue.alt;
2011-11-16 04:16:24 -04:00
temp.alt = command_cond_queue.alt;
2011-03-13 03:25:38 -03:00
set_next_WP(&temp);
}
2011-07-17 07:32:00 -03:00
static void do_within_distance()
2011-03-13 03:25:38 -03:00
{
2012-01-22 02:06:35 -04:00
condition_value = command_cond_queue.lat * 100;
2011-03-13 03:25:38 -03:00
}
2011-07-17 07:32:00 -03:00
static 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-11-16 04:16:24 -04:00
command_yaw_dir = command_cond_queue.p1; // 1 = clockwise, 0 = counterclockwise
command_yaw_speed = command_cond_queue.lat * 100; // ms * 100
command_yaw_relative = command_cond_queue.lng; // 1 = Relative, 0 = Absolute
2011-03-02 22:32:50 -04:00
2012-08-07 10:43:10 -03:00
// if unspecified turn at 30° per second
2011-03-02 22:32:50 -04:00
if(command_yaw_speed == 0)
2011-05-31 02:29:06 -03:00
command_yaw_speed = 3000;
2011-03-02 22:32:50 -04:00
2012-08-07 10:43:10 -03:00
// ensure direction is valid, if invalid default to counter clockwise
if(command_yaw_dir > 1)
command_yaw_dir = 0; // 0 = counter clockwise, 1 = clockwise
2011-03-26 03:35:52 -03:00
2012-08-07 10:43:10 -03:00
if(command_yaw_relative == 1){
2011-03-26 03:35:52 -03:00
// relative
2011-11-16 04:16:24 -04:00
command_yaw_delta = command_cond_queue.alt * 100;
2012-08-07 10:43:10 -03:00
if(command_yaw_dir == 0){ // 0 = counter clockwise, 1 = clockwise
command_yaw_end = command_yaw_start - command_yaw_delta;
}else{
command_yaw_end = command_yaw_start + command_yaw_delta;
}
command_yaw_end = wrap_360(command_yaw_end);
2011-03-02 22:32:50 -04:00
}else{
2011-03-26 03:35:52 -03:00
// absolute
2011-11-16 04:16:24 -04:00
command_yaw_end = command_cond_queue.alt * 100;
2011-04-03 18:11:14 -03:00
// calculate the delta travel in deg * 100
2012-08-07 10:43:10 -03:00
if(command_yaw_dir == 0){ // 0 = counter clockwise, 1 = clockwise
2011-04-03 18:11:14 -03:00
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);
}
2012-08-07 10:43:10 -03:00
}else{
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;
}
2011-04-03 18:11:14 -03:00
}
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
/********************************************************************************/
2011-07-17 07:32:00 -03:00
static bool verify_wait_delay()
2011-03-02 22:32:50 -04:00
{
2011-05-07 16:41:55 -03:00
//Serial.print("vwd");
2012-01-10 00:23:37 -04:00
if ((unsigned)(millis() - condition_start) > (unsigned)condition_value){
2011-05-07 16:41:55 -03:00
//Serial.println("y");
2012-01-10 00:23:37 -04: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-07-17 07:32:00 -03:00
static bool verify_change_alt()
2011-03-02 22:32:50 -04:00
{
2011-11-16 04:21:12 -04:00
//Serial.printf("change_alt, ca:%d, na:%d\n", (int)current_loc.alt, (int)next_WP.alt);
2012-01-11 03:33:49 -04:00
if ((int32_t)condition_start < next_WP.alt){
2011-03-13 03:25:38 -03:00
// we are going higer
if(current_loc.alt > next_WP.alt){
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){
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-07-17 07:32:00 -03:00
static bool verify_within_distance()
2011-03-02 22:32:50 -04:00
{
2011-11-16 04:21:12 -04:00
//Serial.printf("cond dist :%d\n", (int)condition_value);
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-07-17 07:32:00 -03:00
static bool verify_yaw()
2011-03-02 22:32:50 -04:00
{
2011-11-16 04:21:12 -04:00
//Serial.printf("vyaw %d\n", (int)(nav_yaw/100));
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
2012-01-21 02:33:45 -04:00
nav_yaw = command_yaw_end;
2011-09-21 03:20:33 -03:00
auto_yaw = nav_yaw;
2012-08-07 10:43:10 -03:00
// TO-DO: there's still a problem with Condition_yaw, it will do it two times(probably more) sometimes, if it hasn't reached the next waypoint yet.
// it should only do it one time so there should be code here to prevent another Condition_Yaw.
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
2012-08-07 10:43:10 -03:00
if(command_yaw_dir == 0) { // 0 = counter clockwise, 1 = clockwise
nav_yaw = command_yaw_start - ((float)command_yaw_delta * power );
}else{
nav_yaw = command_yaw_start + ((float)command_yaw_delta * power );
}
2011-04-03 18:11:14 -03:00
nav_yaw = wrap_360(nav_yaw);
2011-09-21 02:59:23 -03:00
auto_yaw = 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;
}
}
2012-07-24 23:02:54 -03:00
// verify_nav_roi - verifies that actions required by MAV_CMD_NAV_ROI have completed
// we assume the camera command has been successfully implemented by the do_nav_roi command
// so all we need to check is whether we needed to yaw the copter (due to the mount type) and
// whether that yaw has completed
// TO-DO: add support for other features of MAV_NAV_ROI including pointing at a given waypoint
static bool verify_nav_roi()
{
#if MOUNT == ENABLED
// check if mount type requires us to rotate the quad
if( camera_mount.get_mount_type() == AP_Mount::k_tilt_roll ) {
// ensure yaw has gotten to within 2 degrees of the target
2012-08-16 05:51:09 -03:00
if( labs(wrap_180(ahrs.yaw_sensor-auto_yaw)) <= 200 ) {
2012-07-24 23:02:54 -03:00
nav_yaw = auto_yaw; // ensure target yaw for YAW_HOLD is our desired yaw
return true;
}else{
return false;
}
}else{
// if no rotation required, assume the camera instruction was implemented immediately
return true;
}
#else
// if we have no camera mount simply check we've reached the desired yaw
// ensure yaw has gotten to within 2 degrees of the target
if( abs(wrap_180(ahrs.yaw_sensor-auto_yaw)) <= 200 ) {
nav_yaw = auto_yaw; // ensure target yaw for YAW_HOLD is our desired yaw
return true;
}else{
return false;
}
#endif
}
2011-03-02 22:32:50 -04:00
/********************************************************************************/
2011-04-03 18:11:14 -03:00
// Do (Now) commands
2011-03-02 22:32:50 -04:00
/********************************************************************************/
2011-09-24 13:49:11 -03:00
static void do_change_speed()
{
2011-11-16 04:16:24 -04:00
g.waypoint_speed_max = command_cond_queue.p1 * 100;
2011-09-24 13:49:11 -03:00
}
2011-07-17 07:32:00 -03:00
static void do_target_yaw()
2011-03-27 18:26:34 -03:00
{
2011-11-16 04:16:24 -04:00
yaw_tracking = command_cond_queue.p1;
2011-03-27 18:26:34 -03:00
2011-05-07 16:41:55 -03:00
if(yaw_tracking == MAV_ROI_LOCATION){
2011-11-16 04:16:24 -04:00
target_WP = command_cond_queue;
2011-04-03 18:11:14 -03:00
}
2011-03-27 18:26:34 -03:00
}
2011-07-17 07:32:00 -03:00
static void do_loiter_at_location()
2011-03-13 03:25:38 -03:00
{
next_WP = current_loc;
}
2011-07-17 07:32:00 -03:00
static void do_jump()
2011-03-13 03:25:38 -03:00
{
2012-01-03 14:21:52 -04:00
// Used to track the state of the jump command in Mission scripting
// -10 is a value that means the register is unused
// when in use, it contains the current remaining jumps
static int8_t jump = -10; // used to track loops in jump command
2011-11-17 01:28:56 -04:00
//Serial.printf("do Jump: %d\n", jump);
2011-09-24 21:40:29 -03:00
if(jump == -10){
2011-11-17 01:28:56 -04:00
//Serial.printf("Fresh Jump\n");
// we use a locally stored index for jump
2011-11-16 04:16:24 -04:00
jump = command_cond_queue.lat;
2011-09-24 21:40:29 -03:00
}
2011-11-17 01:28:56 -04:00
//Serial.printf("Jumps left: %d\n",jump);
2011-03-13 03:25:38 -03:00
2011-09-24 21:40:29 -03:00
if(jump > 0) {
2011-11-17 01:28:56 -04:00
//Serial.printf("Do Jump to %d\n",command_cond_queue.p1);
2011-09-24 21:40:29 -03:00
jump--;
2011-11-17 01:28:56 -04:00
change_command(command_cond_queue.p1);
2011-09-24 21:40:29 -03:00
} else if (jump == 0){
2011-11-17 01:28:56 -04:00
//Serial.printf("Did last jump\n");
2011-09-24 21:40:29 -03:00
// we're done, move along
2011-11-17 01:28:56 -04:00
jump = -11;
2011-09-24 21:40:29 -03:00
} else if (jump == -1) {
2011-11-17 01:28:56 -04:00
//Serial.printf("jumpForever\n");
2011-09-24 21:40:29 -03:00
// repeat forever
2011-11-17 01:28:56 -04:00
change_command(command_cond_queue.p1);
2011-03-13 03:25:38 -03:00
}
}
2011-07-17 07:32:00 -03:00
static void do_set_home()
2011-03-02 22:32:50 -04:00
{
2011-11-16 04:16:24 -04:00
if(command_cond_queue.p1 == 1) {
2011-03-13 03:25:38 -03:00
init_home();
} else {
2011-04-03 18:11:14 -03:00
home.id = MAV_CMD_NAV_WAYPOINT;
2011-11-16 04:16:24 -04:00
home.lng = command_cond_queue.lng; // Lon * 10**7
home.lat = command_cond_queue.lat; // Lat * 10**7
2012-07-10 14:58:26 -03:00
home.alt = 0;
2011-03-13 03:25:38 -03:00
home_is_set = true;
}
2011-03-02 22:32:50 -04:00
}
2011-07-17 07:32:00 -03:00
static void do_set_servo()
2011-03-02 22:32:50 -04:00
{
2011-11-16 04:16:24 -04:00
APM_RC.OutputCh(command_cond_queue.p1 - 1, command_cond_queue.alt);
2011-03-02 22:32:50 -04:00
}
2011-07-17 07:32:00 -03:00
static void do_set_relay()
2011-03-02 22:32:50 -04:00
{
2011-11-16 04:16:24 -04:00
if (command_cond_queue.p1 == 1) {
2011-10-02 09:16:31 -03:00
relay.on();
2011-11-16 04:16:24 -04:00
} else if (command_cond_queue.p1 == 0) {
2011-10-02 09:16:31 -03:00
relay.off();
2011-03-02 22:32:50 -04:00
}else{
2011-10-02 09:16:31 -03:00
relay.toggle();
2011-03-02 22:32:50 -04:00
}
2011-03-13 03:25:38 -03:00
}
2011-07-17 07:32:00 -03:00
static void do_repeat_servo()
2011-03-13 03:25:38 -03:00
{
2011-11-16 04:16:24 -04:00
event_id = command_cond_queue.p1 - 1;
2011-03-13 03:25:38 -03:00
2011-11-16 04:16:24 -04:00
if(command_cond_queue.p1 >= CH_5 + 1 && command_cond_queue.p1 <= CH_8 + 1) {
2011-03-13 03:25:38 -03:00
2011-04-03 18:11:14 -03:00
event_timer = 0;
2011-11-16 04:16:24 -04:00
event_value = command_cond_queue.alt;
event_repeat = command_cond_queue.lat * 2;
event_delay = command_cond_queue.lng * 500.0; // /2 (half cycle time) * 1000 (convert to milliseconds)
2011-03-13 03:25:38 -03:00
2011-11-16 04:16:24 -04:00
switch(command_cond_queue.p1) {
2011-03-13 03:25:38 -03:00
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();
}
}
2011-07-17 07:32:00 -03:00
static void do_repeat_relay()
2011-03-13 03:25:38 -03:00
{
2011-04-03 18:11:14 -03:00
event_id = RELAY_TOGGLE;
event_timer = 0;
2011-11-16 04:16:24 -04:00
event_delay = command_cond_queue.lat * 500.0; // /2 (half cycle time) * 1000 (convert to milliseconds)
event_repeat = command_cond_queue.alt * 2;
2011-03-13 03:25:38 -03:00
update_events();
2011-03-19 07:14:15 -03:00
}
2012-07-24 23:02:54 -03:00
// do_nav_roi - starts actions required by MAV_CMD_NAV_ROI
// this involves either moving the camera to point at the ROI (region of interest)
// and possibly rotating the copter to point at the ROI if our mount type does not support a yaw feature
// Note: the ROI should already be in the command_nav_queue global variable
// TO-DO: add support for other features of MAV_NAV_ROI including pointing at a given waypoint
static void do_nav_roi()
{
#if MOUNT == ENABLED
// check if mount type requires us to rotate the quad
if( camera_mount.get_mount_type() == AP_Mount::k_tilt_roll ) {
yaw_tracking = MAV_ROI_LOCATION;
target_WP = command_nav_queue;
2012-08-07 03:04:02 -03:00
auto_yaw = get_bearing_cd(¤t_loc, &target_WP);
2012-07-24 23:02:54 -03:00
}
// send the command to the camera mount
camera_mount.set_roi_cmd(&command_nav_queue);
// TO-DO: expand handling of the do_nav_roi to support all modes of the MAVLink. Currently we only handle mode 4 (see below)
// 0: do nothing
// 1: point at next waypoint
// 2: point at a waypoint taken from WP# parameter (2nd parameter?)
// 3: point at a location given by alt, lon, lat parameters
// 4: point at a target given a target id (can't be implmented)
#else
// if we have no camera mount simply rotate the quad
yaw_tracking = MAV_ROI_LOCATION;
target_WP = command_nav_queue;
2012-08-07 03:04:02 -03:00
auto_yaw = get_bearing_cd(¤t_loc, &target_WP);
2012-07-24 23:02:54 -03:00
#endif
}