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
2014-02-27 22:19:05 -04:00
// forward declarations to make compiler happy
static void do_takeoff(const AP_Mission::Mission_Command& cmd);
static void do_nav_wp(const AP_Mission::Mission_Command& cmd);
static void do_land(const AP_Mission::Mission_Command& cmd);
static void do_loiter_unlimited(const AP_Mission::Mission_Command& cmd);
static void do_circle(const AP_Mission::Mission_Command& cmd);
static void do_loiter_time(const AP_Mission::Mission_Command& cmd);
2014-03-27 04:05:49 -03:00
static void do_spline_wp(const AP_Mission::Mission_Command& cmd);
2014-05-23 02:29:08 -03:00
#if NAV_GUIDED == ENABLED
static void do_nav_guided(const AP_Mission::Mission_Command& cmd);
#endif
2014-02-27 22:19:05 -04:00
static void do_wait_delay(const AP_Mission::Mission_Command& cmd);
static void do_within_distance(const AP_Mission::Mission_Command& cmd);
static void do_change_alt(const AP_Mission::Mission_Command& cmd);
static void do_yaw(const AP_Mission::Mission_Command& cmd);
static void do_change_speed(const AP_Mission::Mission_Command& cmd);
static void do_set_home(const AP_Mission::Mission_Command& cmd);
static void do_roi(const AP_Mission::Mission_Command& cmd);
2014-04-03 05:53:27 -03:00
#if PARACHUTE == ENABLED
static void do_parachute(const AP_Mission::Mission_Command& cmd);
#endif
2014-02-27 22:19:05 -04:00
static bool verify_nav_wp(const AP_Mission::Mission_Command& cmd);
2014-04-16 04:23:24 -03:00
static bool verify_circle(const AP_Mission::Mission_Command& cmd);
2014-03-27 04:05:49 -03:00
static bool verify_spline_wp(const AP_Mission::Mission_Command& cmd);
2014-05-23 02:29:08 -03:00
#if NAV_GUIDED == ENABLED
static bool verify_nav_guided(const AP_Mission::Mission_Command& cmd);
#endif
2014-03-22 00:48:54 -03:00
static void auto_spline_start(const Vector3f& destination, bool stopped_at_start, AC_WPNav::spline_segment_end_type seg_end_type, const Vector3f& next_spline_destination);
2014-02-27 22:19:05 -04:00
// start_command - this function will be called when the ap_mission lib wishes to start a new command
static bool start_command(const AP_Mission::Mission_Command& cmd)
{
// To-Do: logging when new commands start/end
if (g.log_bitmask & MASK_LOG_CMD) {
2014-03-16 05:41:59 -03:00
Log_Write_Cmd(cmd);
2014-02-27 22:19:05 -04:00
}
2011-03-02 22:32:50 -04:00
2014-02-27 22:19:05 -04:00
switch(cmd.id) {
///
/// navigation commands
///
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_TAKEOFF: // 22
2014-02-27 22:19:05 -04:00
do_takeoff(cmd);
2012-08-21 23:19:50 -03:00
break;
2011-02-24 01:56:59 -04:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_WAYPOINT: // 16 Navigate to Waypoint
2014-02-27 22:19:05 -04:00
do_nav_wp(cmd);
2012-08-21 23:19:50 -03:00
break;
2011-02-24 01:56:59 -04:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_LAND: // 21 LAND to Waypoint
2014-02-27 22:19:05 -04:00
do_land(cmd);
2012-08-21 23:19:50 -03:00
break;
2011-02-24 01:56:59 -04:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_LOITER_UNLIM: // 17 Loiter indefinitely
2014-02-27 22:19:05 -04:00
do_loiter_unlimited(cmd);
2012-08-21 23:19:50 -03:00
break;
2011-03-13 03:25:38 -03:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_LOITER_TURNS: //18 Loiter N Times
2014-02-27 22:19:05 -04:00
do_circle(cmd);
2012-08-21 23:19:50 -03:00
break;
2011-03-13 03:25:38 -03:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_LOITER_TIME: // 19
2014-02-27 22:19:05 -04:00
do_loiter_time(cmd);
2012-08-21 23:19:50 -03:00
break;
2011-03-13 03:25:38 -03:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_RETURN_TO_LAUNCH: //20
do_RTL();
break;
2011-04-02 21:47:20 -03:00
2014-03-27 04:05:49 -03:00
case MAV_CMD_NAV_SPLINE_WAYPOINT: // 82 Navigate to Waypoint using spline
do_spline_wp(cmd);
break;
2014-05-23 02:29:08 -03:00
#if NAV_GUIDED == ENABLED
2014-07-24 07:38:00 -03:00
#ifdef MAV_CMD_NAV_GUIDED
case MAV_CMD_NAV_GUIDED: // 90 accept navigation commands from external nav computer
2014-05-23 02:29:08 -03:00
do_nav_guided(cmd);
break;
2014-07-24 07:38:00 -03:00
#endif
2014-05-23 02:29:08 -03:00
#endif
2014-02-27 22:19:05 -04:00
//
// conditional commands
//
2012-08-21 23:19:50 -03:00
case MAV_CMD_CONDITION_DELAY: // 112
2014-02-27 22:19:05 -04:00
do_wait_delay(cmd);
2012-08-21 23:19:50 -03:00
break;
2011-03-13 03:25:38 -03:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_CONDITION_DISTANCE: // 114
2014-02-27 22:19:05 -04:00
do_within_distance(cmd);
2012-08-21 23:19:50 -03:00
break;
2011-02-24 01:56:59 -04:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_CONDITION_CHANGE_ALT: // 113
2014-02-27 22:19:05 -04:00
do_change_alt(cmd);
2012-08-21 23:19:50 -03:00
break;
2011-02-24 01:56:59 -04:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_CONDITION_YAW: // 115
2014-02-27 22:19:05 -04:00
do_yaw(cmd);
2012-08-21 23:19:50 -03:00
break;
2011-02-24 01:56:59 -04:00
2014-02-27 22:19:05 -04:00
///
/// do commands
///
2012-08-21 23:19:50 -03:00
case MAV_CMD_DO_CHANGE_SPEED: // 178
2014-02-27 22:19:05 -04:00
do_change_speed(cmd);
2012-08-21 23:19:50 -03:00
break;
2011-03-13 03:25:38 -03:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_DO_SET_HOME: // 179
2014-02-27 22:19:05 -04:00
do_set_home(cmd);
2012-08-21 23:19:50 -03:00
break;
2011-02-24 01:56:59 -04:00
2014-01-20 01:05:31 -04:00
case MAV_CMD_DO_SET_SERVO:
2014-03-17 02:16:52 -03:00
ServoRelayEvents.do_set_servo(cmd.content.servo.channel, cmd.content.servo.pwm);
2012-08-21 23:19:50 -03:00
break;
2014-01-20 01:05:31 -04:00
case MAV_CMD_DO_SET_RELAY:
2014-03-17 02:16:52 -03:00
ServoRelayEvents.do_set_relay(cmd.content.relay.num, cmd.content.relay.state);
2012-08-21 23:19:50 -03:00
break;
2014-01-20 01:05:31 -04:00
case MAV_CMD_DO_REPEAT_SERVO:
2014-03-18 23:14:06 -03:00
ServoRelayEvents.do_repeat_servo(cmd.content.repeat_servo.channel, cmd.content.repeat_servo.pwm,
cmd.content.repeat_servo.repeat_count, cmd.content.repeat_servo.cycle_time * 1000.0f);
2012-08-21 23:19:50 -03:00
break;
2014-01-20 01:05:31 -04:00
case MAV_CMD_DO_REPEAT_RELAY:
2014-03-18 23:14:06 -03:00
ServoRelayEvents.do_repeat_relay(cmd.content.repeat_relay.num, cmd.content.repeat_relay.repeat_count,
cmd.content.repeat_relay.cycle_time * 1000.0f);
2012-08-21 23:19:50 -03:00
break;
2011-03-27 18:26:34 -03:00
2013-07-13 11:27:51 -03:00
case MAV_CMD_DO_SET_ROI: // 201
// point the copter and camera at a region of interest (ROI)
2014-02-27 22:19:05 -04:00
do_roi(cmd);
2013-07-13 11:27:51 -03:00
break;
2012-07-10 19:39:13 -03:00
#if CAMERA == ENABLED
2012-08-21 23:19:50 -03:00
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;
2012-07-10 19:39:13 -03:00
2012-08-21 23:19:50 -03:00
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;
2012-07-10 19:39:13 -03:00
2012-08-21 23:19:50 -03:00
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|
2012-12-06 10:48:30 -04:00
do_take_picture();
2012-08-21 23:19:50 -03:00
break;
2013-10-11 07:34:23 -03:00
case MAV_CMD_DO_SET_CAM_TRIGG_DIST:
2014-03-17 02:16:52 -03:00
camera.set_trigger_distance(cmd.content.cam_trigg_dist.meters);
2013-10-11 07:34:23 -03:00
break;
2012-07-10 19:39:13 -03:00
#endif
#if MOUNT == ENABLED
2012-08-21 23:19:50 -03:00
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;
2012-07-10 19:39:13 -03:00
2012-08-21 23:19:50 -03:00
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;
2012-07-10 19:39:13 -03:00
#endif
2012-07-24 23:02:54 -03:00
2014-04-03 05:53:27 -03:00
#if PARACHUTE == ENABLED
case MAV_CMD_DO_PARACHUTE: // Mission command to configure or release parachute
do_parachute(cmd);
break;
#endif
2012-08-21 23:19:50 -03:00
default:
// do nothing with unrecognized MAVLink messages
break;
}
2014-02-27 22:19:05 -04:00
// always return success
return true;
2011-02-24 01:56:59 -04:00
}
2011-03-13 03:25:38 -03:00
/********************************************************************************/
// Verify command Handlers
/********************************************************************************/
2014-02-27 22:19:05 -04:00
// verify_command - this will be called repeatedly by ap_mission lib to ensure the active commands are progressing
// should return true once the active navigation command completes successfully
// called at 10hz or higher
static bool verify_command(const AP_Mission::Mission_Command& cmd)
2011-03-02 22:32:50 -04:00
{
2014-02-27 22:19:05 -04:00
switch(cmd.id) {
2011-02-24 01:56:59 -04:00
2014-02-27 22:19:05 -04:00
//
// navigation commands
//
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_TAKEOFF:
return verify_takeoff();
break;
2011-02-24 01:56:59 -04:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_WAYPOINT:
2014-02-27 22:19:05 -04:00
return verify_nav_wp(cmd);
2012-08-21 23:19:50 -03:00
break;
2012-07-24 23:02:54 -03:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_LAND:
2012-11-23 02:57:49 -04:00
return verify_land();
2012-08-21 23:19:50 -03:00
break;
2011-02-24 01:56:59 -04:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_LOITER_UNLIM:
return verify_loiter_unlimited();
break;
2011-03-13 03:25:38 -03:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_LOITER_TURNS:
2014-04-16 04:23:24 -03:00
return verify_circle(cmd);
2012-08-21 23:19:50 -03:00
break;
2011-03-13 03:25:38 -03:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_LOITER_TIME:
return verify_loiter_time();
break;
2011-03-13 03:25:38 -03:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_RETURN_TO_LAUNCH:
return verify_RTL();
break;
2011-02-24 01:56:59 -04:00
2014-03-27 04:05:49 -03:00
case MAV_CMD_NAV_SPLINE_WAYPOINT:
return verify_spline_wp(cmd);
break;
2014-05-23 02:29:08 -03:00
#if NAV_GUIDED == ENABLED
2014-07-24 07:38:00 -03:00
#ifdef MAV_CMD_NAV_GUIDED
2014-05-23 02:29:08 -03:00
case MAV_CMD_NAV_GUIDED:
return verify_nav_guided(cmd);
break;
2014-07-24 07:38:00 -03:00
#endif
2014-05-23 02:29:08 -03:00
#endif
2014-02-27 22:19:05 -04:00
///
/// conditional commands
///
2012-08-21 23:19:50 -03:00
case MAV_CMD_CONDITION_DELAY:
return verify_wait_delay();
break;
2011-02-24 01:56:59 -04:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_CONDITION_DISTANCE:
return verify_within_distance();
break;
2011-02-24 01:56:59 -04:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_CONDITION_CHANGE_ALT:
return verify_change_alt();
break;
2011-02-24 01:56:59 -04:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_CONDITION_YAW:
return verify_yaw();
break;
2011-03-13 03:25:38 -03:00
2014-04-03 05:53:27 -03:00
#if PARACHUTE == ENABLED
case MAV_CMD_DO_PARACHUTE:
// assume parachute was released successfully
return true;
break;
#endif
2012-08-21 23:19:50 -03:00
default:
2014-02-27 22:19:05 -04:00
// return true if we do not recognise the command so that we move on to the next command
return true;
2012-08-21 23:19:50 -03:00
break;
}
2011-02-24 01:56:59 -04:00
}
2014-02-27 22:19:05 -04:00
// exit_mission - function that is called once the mission completes
static void exit_mission()
{
// if we are not on the ground switch to loiter or land
if(!ap.land_complete) {
// try to enter loiter but if that fails land
if (!set_mode(LOITER)) {
set_mode(LAND);
}
}else{
#if LAND_REQUIRE_MIN_THROTTLE_TO_DISARM == ENABLED
// disarm when the landing detector says we've landed and throttle is at minimum
if (g.rc_3.control_in == 0 || failsafe.radio) {
init_disarm_motors();
}
#else
// if we've landed it's safe to disarm
init_disarm_motors();
#endif
}
}
2011-03-02 22:32:50 -04:00
/********************************************************************************/
2011-04-26 02:15:25 -03:00
//
2011-03-02 22:32:50 -04:00
/********************************************************************************/
2012-11-29 08:08:19 -04:00
// do_RTL - start Return-to-Launch
2011-07-17 07:32:00 -03:00
static void do_RTL(void)
2011-03-13 03:25:38 -03:00
{
2014-01-27 23:21:45 -04:00
// start rtl in auto flight mode
auto_rtl_start();
2011-03-13 03:25:38 -03:00
}
2011-04-26 02:15:25 -03:00
/********************************************************************************/
// Nav (Must) commands
/********************************************************************************/
2012-12-09 02:50:50 -04:00
// do_takeoff - initiate takeoff navigation command
2014-02-27 22:19:05 -04:00
static void do_takeoff(const AP_Mission::Mission_Command& cmd)
2011-02-24 01:56:59 -04:00
{
2013-04-08 23:58:01 -03:00
// Set wp navigation target to safe altitude above current position
2014-02-27 22:19:05 -04:00
float takeoff_alt = cmd.content.location.alt;
2014-01-24 22:37:58 -04:00
takeoff_alt = max(takeoff_alt,current_loc.alt);
takeoff_alt = max(takeoff_alt,100.0f);
auto_takeoff_start(takeoff_alt);
2011-03-02 22:32:50 -04:00
}
2011-02-24 01:56:59 -04:00
2012-12-09 02:50:50 -04:00
// do_nav_wp - initiate move to next waypoint
2014-02-27 22:19:05 -04:00
static void do_nav_wp(const AP_Mission::Mission_Command& cmd)
2011-03-13 03:25:38 -03:00
{
2014-04-16 08:32:49 -03:00
const Vector3f &curr_pos = inertial_nav.get_position();
2014-02-27 22:19:05 -04:00
Vector3f local_pos = pv_location_to_vector(cmd.content.location);
2013-03-21 06:29:38 -03:00
2014-04-16 08:32:49 -03:00
// set target altitude to current altitude if not provided
if (cmd.content.location.alt == 0) {
local_pos.z = curr_pos.z;
}
// set lat/lon position to current position if not provided
if (cmd.content.location.lat == 0 && cmd.content.location.lng == 0) {
local_pos.x = curr_pos.x;
local_pos.y = curr_pos.y;
}
2013-04-08 23:58:01 -03:00
// this will be used to remember the time in millis after we reach or pass the WP.
2014-03-22 00:48:54 -03:00
loiter_time = 0;
2014-03-17 02:16:52 -03:00
// this is the delay, stored in seconds
2014-03-22 00:48:54 -03:00
loiter_time_max = abs(cmd.p1);
2014-03-27 04:11:50 -03:00
// Set wp navigation target
auto_wp_start(local_pos);
// if no delay set the waypoint as "fast"
if (loiter_time_max == 0 ) {
wp_nav.set_fast_waypoint(true);
2013-05-08 12:18:36 -03:00
}
2011-03-13 03:25:38 -03:00
}
2012-12-09 02:50:50 -04:00
// do_land - initiate landing procedure
2014-02-27 22:19:05 -04:00
static void do_land(const AP_Mission::Mission_Command& cmd)
2011-03-13 03:25:38 -03:00
{
2013-05-10 10:37:15 -03:00
// To-Do: check if we have already landed
// if location provided we fly to that location at current altitude
2014-02-27 22:19:05 -04:00
if (cmd.content.location.lat != 0 || cmd.content.location.lng != 0) {
2013-05-10 10:37:15 -03:00
// set state to fly to location
land_state = LAND_STATE_FLY_TO_LOCATION;
// calculate and set desired location above landing target
2014-02-27 22:19:05 -04:00
Vector3f pos = pv_location_to_vector(cmd.content.location);
2014-03-31 05:12:32 -03:00
pos.z = current_loc.alt;
2014-01-24 22:37:58 -04:00
auto_wp_start(pos);
2013-02-18 01:58:24 -04:00
}else{
2013-05-10 10:37:15 -03:00
// set landing state
land_state = LAND_STATE_DESCENDING;
2012-12-10 10:38:43 -04:00
2014-01-24 22:37:58 -04:00
// initialise landing controller
auto_land_start();
2013-05-10 10:37:15 -03:00
}
2011-03-13 03:25:38 -03:00
}
2013-02-04 11:45:31 -04:00
// do_loiter_unlimited - start loitering with no end conditions
2013-02-18 01:58:24 -04:00
// note: caller should set yaw_mode
2014-02-27 22:19:05 -04:00
static void do_loiter_unlimited(const AP_Mission::Mission_Command& cmd)
2011-03-13 03:25:38 -03:00
{
2013-07-10 23:35:54 -03:00
Vector3f target_pos;
2013-04-08 23:58:01 -03:00
// get current position
2013-07-10 23:35:54 -03:00
Vector3f curr_pos = inertial_nav.get_position();
2013-04-08 23:58:01 -03:00
2014-01-28 00:58:20 -04:00
// default to use position provided
2014-02-27 22:19:05 -04:00
target_pos = pv_location_to_vector(cmd.content.location);
2014-01-28 00:58:20 -04:00
2013-07-10 23:35:54 -03:00
// use current location if not provided
2014-02-27 22:19:05 -04:00
if(cmd.content.location.lat == 0 && cmd.content.location.lng == 0) {
2014-01-21 22:42:16 -04:00
wp_nav.get_wp_stopping_point_xy(target_pos);
2013-07-10 23:35:54 -03:00
}
2013-04-08 23:58:01 -03:00
// use current altitude if not provided
2014-01-28 00:58:20 -04:00
// To-Do: use z-axis stopping point instead of current alt
2014-02-27 22:19:05 -04:00
if( cmd.content.location.alt == 0 ) {
2013-07-10 23:35:54 -03:00
target_pos.z = curr_pos.z;
2013-02-04 11:45:31 -04:00
}
2013-04-08 23:58:01 -03:00
// start way point navigator and provide it the desired location
2014-01-27 23:21:45 -04:00
auto_wp_start(target_pos);
2013-02-18 01:58:24 -04:00
}
2013-02-25 04:50:56 -04:00
// do_circle - initiate moving in a circle
2014-02-27 22:19:05 -04:00
static void do_circle(const AP_Mission::Mission_Command& cmd)
2013-02-18 01:58:24 -04:00
{
2014-01-27 10:44:36 -04:00
Vector3f curr_pos = inertial_nav.get_position();
2014-02-27 22:19:05 -04:00
Vector3f circle_center = pv_location_to_vector(cmd.content.location);
2014-06-11 04:18:35 -03:00
uint8_t circle_radius_m = HIGHBYTE(cmd.p1); // circle radius held in high byte of p1
2014-04-16 04:23:24 -03:00
bool move_to_edge_required = false;
2013-04-08 23:58:01 -03:00
2014-01-27 10:44:36 -04:00
// set target altitude if not provided
2014-04-16 04:23:24 -03:00
if (cmd.content.location.alt == 0) {
2014-01-27 10:44:36 -04:00
circle_center.z = curr_pos.z;
2014-04-16 04:23:24 -03:00
} else {
move_to_edge_required = true;
2012-08-21 23:19:50 -03:00
}
2011-03-13 03:25:38 -03:00
2014-01-27 10:44:36 -04:00
// set lat/lon position if not provided
2014-04-16 04:23:24 -03:00
// To-Do: use previous command's destination if it was a straight line or spline waypoint command
2014-02-27 22:19:05 -04:00
if (cmd.content.location.lat == 0 && cmd.content.location.lng == 0) {
2014-01-27 10:44:36 -04:00
circle_center.x = curr_pos.x;
circle_center.y = curr_pos.y;
2014-04-16 04:23:24 -03:00
} else {
move_to_edge_required = true;
2012-08-21 23:19:50 -03:00
}
2013-02-25 04:50:56 -04:00
2014-04-16 04:23:24 -03:00
// set circle controller's center
circle_nav.set_center(circle_center);
2013-02-25 04:50:56 -04:00
2014-06-11 04:18:35 -03:00
// set circle radius
if (circle_radius_m != 0) {
circle_nav.set_radius((float)circle_radius_m * 100.0f);
}
2014-04-16 04:23:24 -03:00
// check if we need to move to edge of circle
if (move_to_edge_required) {
// move to edge of circle (verify_circle) will ensure we begin circling once we reach the edge
auto_circle_movetoedge_start();
} else {
// start circling
auto_circle_start();
}
2011-03-13 03:25:38 -03:00
}
2012-12-09 02:50:50 -04:00
// do_loiter_time - initiate loitering at a point for a given time period
2013-02-18 01:58:24 -04:00
// note: caller should set yaw_mode
2014-02-27 22:19:05 -04:00
static void do_loiter_time(const AP_Mission::Mission_Command& cmd)
2011-03-13 03:25:38 -03:00
{
2013-07-10 23:35:54 -03:00
Vector3f target_pos;
2013-04-08 23:58:01 -03:00
// get current position
2013-07-10 23:35:54 -03:00
Vector3f curr_pos = inertial_nav.get_position();
2013-04-08 23:58:01 -03:00
2014-01-28 00:58:20 -04:00
// default to use position provided
2014-02-27 22:19:05 -04:00
target_pos = pv_location_to_vector(cmd.content.location);
2014-01-28 00:58:20 -04:00
2013-07-10 23:35:54 -03:00
// use current location if not provided
2014-02-27 22:19:05 -04:00
if(cmd.content.location.lat == 0 && cmd.content.location.lng == 0) {
2014-01-21 22:42:16 -04:00
wp_nav.get_wp_stopping_point_xy(target_pos);
2013-07-10 23:35:54 -03:00
}
2013-04-08 23:58:01 -03:00
// use current altitude if not provided
2014-02-27 22:19:05 -04:00
if( cmd.content.location.alt == 0 ) {
2013-07-10 23:35:54 -03:00
target_pos.z = curr_pos.z;
2012-08-21 23:19:50 -03:00
}
2011-09-24 21:40:29 -03:00
2013-04-08 23:58:01 -03:00
// start way point navigator and provide it the desired location
2014-01-27 23:21:45 -04:00
auto_wp_start(target_pos);
2013-04-08 23:58:01 -03:00
// setup loiter timer
loiter_time = 0;
2014-03-03 01:38:32 -04:00
loiter_time_max = cmd.p1; // units are (seconds)
2011-03-13 03:25:38 -03:00
}
2014-03-27 04:05:49 -03:00
// do_spline_wp - initiate move to next waypoint
static void do_spline_wp(const AP_Mission::Mission_Command& cmd)
{
Vector3f local_pos = pv_location_to_vector(cmd.content.location);
// this will be used to remember the time in millis after we reach or pass the WP.
loiter_time = 0;
// this is the delay, stored in seconds
loiter_time_max = abs(cmd.p1);
// determine segment start and end type
bool stopped_at_start = true;
AC_WPNav::spline_segment_end_type seg_end_type = AC_WPNav::SEGMENT_END_STOP;
AP_Mission::Mission_Command temp_cmd;
2014-03-29 05:52:38 -03:00
Vector3f next_destination; // end of next segment
2014-03-27 04:05:49 -03:00
// if previous command was a wp_nav command with no delay set stopped_at_start to false
// To-Do: move processing of delay into wp-nav controller to allow it to determine the stopped_at_start value itself?
uint16_t prev_cmd_idx = mission.get_prev_nav_cmd_index();
if (prev_cmd_idx != AP_MISSION_CMD_INDEX_NONE) {
if (mission.read_cmd_from_storage(prev_cmd_idx, temp_cmd)) {
if ((temp_cmd.id == MAV_CMD_NAV_WAYPOINT || temp_cmd.id == MAV_CMD_NAV_SPLINE_WAYPOINT) && temp_cmd.p1 == 0) {
stopped_at_start = false;
}
}
}
// if there is no delay at the end of this segment get next nav command
2014-03-29 05:52:38 -03:00
if (cmd.p1 == 0 && mission.get_next_nav_cmd(cmd.index+1, temp_cmd)) {
2014-03-27 04:05:49 -03:00
// if the next nav command is a waypoint set end type to spline or straight
if (temp_cmd.id == MAV_CMD_NAV_WAYPOINT) {
seg_end_type = AC_WPNav::SEGMENT_END_STRAIGHT;
2014-03-29 05:52:38 -03:00
next_destination = pv_location_to_vector(temp_cmd.content.location);
2014-03-27 04:05:49 -03:00
}else if (temp_cmd.id == MAV_CMD_NAV_SPLINE_WAYPOINT) {
seg_end_type = AC_WPNav::SEGMENT_END_SPLINE;
2014-03-29 05:52:38 -03:00
next_destination = pv_location_to_vector(temp_cmd.content.location);
2014-03-27 04:05:49 -03:00
}
}
// set spline navigation target
2014-03-29 05:52:38 -03:00
auto_spline_start(local_pos, stopped_at_start, seg_end_type, next_destination);
2014-03-27 04:05:49 -03:00
}
2014-05-23 02:29:08 -03:00
#if NAV_GUIDED == ENABLED
// do_nav_guided - initiate accepting commands from exernal nav computer
static void do_nav_guided(const AP_Mission::Mission_Command& cmd)
{
// record start time so it can be compared vs timeout
nav_guided.start_time = millis();
// record start position so it can be compared vs horizontal limit
nav_guided.start_position = inertial_nav.get_position();
// set spline navigation target
auto_nav_guided_start();
}
#endif // NAV_GUIDED
2014-04-03 05:53:27 -03:00
#if PARACHUTE == ENABLED
// do_parachute - configure or release parachute
static void do_parachute(const AP_Mission::Mission_Command& cmd)
{
switch (cmd.p1) {
case PARACHUTE_DISABLE:
parachute.enabled(false);
Log_Write_Event(DATA_PARACHUTE_DISABLED);
break;
case PARACHUTE_ENABLE:
parachute.enabled(true);
Log_Write_Event(DATA_PARACHUTE_ENABLED);
break;
case PARACHUTE_RELEASE:
parachute_release();
break;
default:
// do nothing
break;
}
}
#endif
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
/********************************************************************************/
2012-12-09 02:50:50 -04:00
// verify_takeoff - check if we have completed the takeoff
2011-07-17 07:32:00 -03:00
static bool verify_takeoff()
2011-03-02 22:32:50 -04:00
{
2013-04-08 23:58:01 -03:00
// have we reached our target altitude?
2014-01-19 10:35:55 -04:00
return wp_nav.reached_wp_destination();
2011-03-02 22:32:50 -04:00
}
2012-11-23 02:57:49 -04:00
// verify_land - returns true if landing has been completed
static bool verify_land()
2012-01-29 02:00:05 -04:00
{
2013-05-10 10:37:15 -03:00
bool retval = false;
switch( land_state ) {
case LAND_STATE_FLY_TO_LOCATION:
// check if we've reached the location
2014-01-19 10:35:55 -04:00
if (wp_nav.reached_wp_destination()) {
2013-05-10 10:37:15 -03:00
// get destination so we can use it for loiter target
2014-01-21 22:42:16 -04:00
Vector3f dest = wp_nav.get_wp_destination();
2013-05-10 10:37:15 -03:00
2014-01-24 22:37:58 -04:00
// initialise landing controller
auto_land_start(dest);
2013-05-10 10:37:15 -03:00
// advance to next state
land_state = LAND_STATE_DESCENDING;
}
break;
case LAND_STATE_DESCENDING:
// rely on THROTTLE_LAND mode to correctly update landing status
retval = ap.land_complete;
break;
default:
// this should never happen
// TO-DO: log an error
retval = true;
break;
}
// true is returned if we've successfully landed
return retval;
2011-03-02 22:32:50 -04:00
}
2012-12-09 02:50:50 -04:00
// verify_nav_wp - check if we have reached the next way point
2014-02-27 22:19:05 -04:00
static bool verify_nav_wp(const AP_Mission::Mission_Command& cmd)
2011-03-02 22:32:50 -04:00
{
2013-04-08 23:58:01 -03:00
// check if we have reached the waypoint
2014-01-19 10:35:55 -04:00
if( !wp_nav.reached_wp_destination() ) {
2013-04-08 23:58:01 -03:00
return false;
2012-08-21 23:19:50 -03:00
}
2013-04-08 23:58:01 -03:00
// start timer if necessary
if(loiter_time == 0) {
loiter_time = millis();
2012-08-21 23:19:50 -03:00
}
2013-04-08 23:58:01 -03:00
// check if timer has run out
if (((millis() - loiter_time) / 1000) >= loiter_time_max) {
2014-02-27 22:19:05 -04:00
gcs_send_text_fmt(PSTR("Reached Command #%i"),cmd.index);
2012-08-21 23:19:50 -03:00
return true;
}else{
return false;
}
2011-03-02 22:32:50 -04:00
}
2012-07-12 12:52:36 -03:00
static bool verify_loiter_unlimited()
{
2012-08-21 23:19:50 -03:00
return false;
2012-07-12 12:52:36 -03:00
}
2011-03-02 22:32:50 -04:00
2012-12-09 02:50:50 -04:00
// verify_loiter_time - check if we have loitered long enough
2011-07-17 07:32:00 -03:00
static bool verify_loiter_time()
2011-03-02 22:32:50 -04:00
{
2013-04-08 23:58:01 -03:00
// return immediately if we haven't reached our destination
2014-01-19 10:35:55 -04:00
if (!wp_nav.reached_wp_destination()) {
2013-04-08 23:58:01 -03:00
return false;
2012-08-21 23:19:50 -03:00
}
2013-04-08 23:58:01 -03:00
// start our loiter timer
if( loiter_time == 0 ) {
2012-08-21 23:19:50 -03:00
loiter_time = millis();
}
2013-04-08 23:58:01 -03:00
// check if loiter timer has run out
2013-04-15 11:57:22 -03:00
return (((millis() - loiter_time) / 1000) >= loiter_time_max);
2011-03-02 22:32:50 -04:00
}
2013-02-25 04:50:56 -04:00
// verify_circle - check if we have circled the point enough
2014-04-16 04:23:24 -03:00
static bool verify_circle(const AP_Mission::Mission_Command& cmd)
2011-09-21 17:19:36 -03:00
{
2014-04-16 04:23:24 -03:00
// check if we've reached the edge
if (auto_mode == Auto_CircleMoveToEdge) {
if (wp_nav.reached_wp_destination()) {
Vector3f curr_pos = inertial_nav.get_position();
Vector3f circle_center = pv_location_to_vector(cmd.content.location);
// set target altitude if not provided
if (circle_center.z == 0) {
circle_center.z = curr_pos.z;
}
// set lat/lon position if not provided
if (cmd.content.location.lat == 0 && cmd.content.location.lng == 0) {
circle_center.x = curr_pos.x;
circle_center.y = curr_pos.y;
}
// start circling
auto_circle_start();
}
return false;
}
// check if we have completed circling
2014-06-11 04:18:35 -03:00
return fabsf(circle_nav.get_angle_total()/(2*M_PI)) >= (float)LOWBYTE(cmd.p1);
2011-09-21 17:19:36 -03:00
}
2014-01-25 04:24:56 -04:00
// externs to remove compiler warning
extern bool rtl_state_complete;
2012-11-29 08:08:19 -04:00
// verify_RTL - handles any state changes required to implement RTL
// do_RTL should have been called once first to initialise all variables
// returns true with RTL has completed successfully
2011-07-17 07:32:00 -03:00
static bool verify_RTL()
2011-03-02 22:32:50 -04:00
{
2014-01-25 04:24:56 -04:00
return (rtl_state_complete && (rtl_state == FinalDescent || rtl_state == Land));
2011-03-02 22:32:50 -04:00
}
2014-03-27 04:05:49 -03:00
// verify_spline_wp - check if we have reached the next way point using spline
static bool verify_spline_wp(const AP_Mission::Mission_Command& cmd)
{
// check if we have reached the waypoint
if( !wp_nav.reached_wp_destination() ) {
return false;
}
// start timer if necessary
if(loiter_time == 0) {
loiter_time = millis();
}
// check if timer has run out
if (((millis() - loiter_time) / 1000) >= loiter_time_max) {
gcs_send_text_fmt(PSTR("Reached Command #%i"),cmd.index);
return true;
}else{
return false;
}
}
2014-05-23 02:29:08 -03:00
#if NAV_GUIDED == ENABLED
// verify_nav_guided - check if we have breached any limits
static bool verify_nav_guided(const AP_Mission::Mission_Command& cmd)
{
// check if we have passed the timeout
if ((cmd.p1 > 0) && ((millis() - nav_guided.start_time) / 1000 >= cmd.p1)) {
return true;
}
// get current location
const Vector3f& curr_pos = inertial_nav.get_position();
// check if we have gone below min alt
if (cmd.content.nav_guided.alt_min != 0 && (curr_pos.z / 100) < cmd.content.nav_guided.alt_min) {
return true;
}
// check if we have gone above max alt
if (cmd.content.nav_guided.alt_max != 0 && (curr_pos.z / 100) > cmd.content.nav_guided.alt_max) {
return true;
}
// check if we have gone beyond horizontal limit
if (cmd.content.nav_guided.horiz_max != 0) {
float horiz_move = pv_get_horizontal_distance_cm(nav_guided.start_position, curr_pos) / 100;
if (horiz_move > cmd.content.nav_guided.horiz_max) {
return true;
}
}
// if we got here we should continue with the external nav controls
return false;
}
#endif // NAV_GUIDED
2011-03-02 22:32:50 -04:00
/********************************************************************************/
2011-04-03 18:11:14 -03:00
// Condition (May) commands
2011-03-02 22:32:50 -04:00
/********************************************************************************/
2014-02-27 22:19:05 -04:00
static void do_wait_delay(const AP_Mission::Mission_Command& cmd)
2011-03-13 03:25:38 -03:00
{
2012-08-21 23:19:50 -03:00
condition_start = millis();
2014-03-17 02:16:52 -03:00
condition_value = cmd.content.delay.seconds * 1000; // convert seconds to milliseconds
2011-03-13 03:25:38 -03:00
}
2014-02-27 22:19:05 -04:00
static void do_change_alt(const AP_Mission::Mission_Command& cmd)
2011-03-13 03:25:38 -03:00
{
2013-04-08 23:58:01 -03:00
// adjust target appropriately for each nav mode
2014-01-27 23:21:45 -04:00
if (control_mode == AUTO) {
switch (auto_mode) {
case Auto_TakeOff:
// To-Do: adjust waypoint target altitude to new provided altitude
2013-04-08 23:58:01 -03:00
break;
2014-01-27 23:21:45 -04:00
case Auto_WP:
2014-03-22 00:48:54 -03:00
case Auto_Spline:
2014-01-27 23:21:45 -04:00
// To-Do; reset origin to current location + stopping distance at new altitude
break;
case Auto_Land:
2014-02-03 09:11:13 -04:00
case Auto_RTL:
2014-01-27 23:21:45 -04:00
// ignore altitude
2013-04-08 23:58:01 -03:00
break;
2014-04-16 04:23:24 -03:00
case Auto_CircleMoveToEdge:
2014-01-27 23:21:45 -04:00
case Auto_Circle:
// move circle altitude up to target (we will need to store this target in circle class)
break;
2014-05-23 02:29:08 -03:00
case Auto_NavGuided:
// ignore altitude
break;
2014-01-27 23:21:45 -04:00
}
2013-04-08 23:58:01 -03:00
}
// To-Do: store desired altitude in a variable so that it can be verified later
2011-03-13 03:25:38 -03:00
}
2014-02-27 22:19:05 -04:00
static void do_within_distance(const AP_Mission::Mission_Command& cmd)
2011-03-13 03:25:38 -03:00
{
2014-03-17 02:16:52 -03:00
condition_value = cmd.content.distance.meters * 100;
2011-03-13 03:25:38 -03:00
}
2014-02-27 22:19:05 -04:00
static void do_yaw(const AP_Mission::Mission_Command& cmd)
2011-03-02 22:32:50 -04:00
{
2014-06-13 06:17:35 -03:00
set_auto_yaw_look_at_heading(
cmd.content.yaw.angle_deg,
cmd.content.yaw.turn_rate_dps,
cmd.content.yaw.relative_angle);
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
{
2013-01-26 22:27:43 -04:00
if (millis() - condition_start > (uint32_t)max(condition_value,0)) {
2012-08-21 23:19:50 -03:00
condition_value = 0;
return true;
}
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
{
2013-04-08 23:58:01 -03:00
// To-Do: use recorded target altitude to verify we have reached the target
return true;
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
{
2014-04-17 10:22:05 -03:00
// update distance calculation
calc_wp_distance();
2013-01-26 04:04:12 -04:00
if (wp_distance < max(condition_value,0)) {
2012-08-21 23:19:50 -03:00
condition_value = 0;
return true;
}
return false;
2011-03-02 22:32:50 -04:00
}
2012-12-08 01:23:32 -04:00
// verify_yaw - return true if we have reached the desired heading
2011-07-17 07:32:00 -03:00
static bool verify_yaw()
2011-03-02 22:32:50 -04:00
{
2014-04-17 04:14:18 -03:00
// set yaw mode if it has been changed (the waypoint controller often retakes control of yaw as it executes a new waypoint command)
if (auto_yaw_mode != AUTO_YAW_LOOK_AT_HEADING) {
set_auto_yaw_mode(AUTO_YAW_LOOK_AT_HEADING);
}
// check if we are within 2 degrees of the target heading
if (labs(wrap_180_cd(ahrs.yaw_sensor-yaw_look_at_heading)) <= 200) {
2012-08-21 23:19:50 -03:00
return true;
}else{
return false;
}
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
/********************************************************************************/
2013-06-01 23:25:35 -03:00
// do_guided - start guided mode
2014-02-27 22:19:05 -04:00
static bool do_guided(const AP_Mission::Mission_Command& cmd)
2013-06-01 23:25:35 -03:00
{
2014-06-02 06:06:11 -03:00
Vector3f pos_or_vel; // target location or velocity
2014-05-26 04:00:25 -03:00
2014-05-14 16:11:28 -03:00
// only process guided waypoint if we are in guided mode
2014-05-23 03:06:44 -03:00
if (control_mode != GUIDED && !(control_mode == AUTO && auto_mode == Auto_NavGuided)) {
2014-05-14 16:11:28 -03:00
return false;
2013-06-01 23:25:35 -03:00
}
2014-05-26 04:00:25 -03:00
// switch to handle different commands
switch (cmd.id) {
case MAV_CMD_NAV_WAYPOINT:
// set wp_nav's destination
2014-06-02 06:06:11 -03:00
pos_or_vel = pv_location_to_vector(cmd.content.location);
guided_set_destination(pos_or_vel);
return true;
break;
2014-07-24 07:38:00 -03:00
#ifdef MAV_CMD_NAV_VELOCITY
2014-06-02 06:06:11 -03:00
case MAV_CMD_NAV_VELOCITY:
// set target velocity
pos_or_vel.x = cmd.content.nav_velocity.x * 100.0f;
pos_or_vel.y = cmd.content.nav_velocity.y * 100.0f;
pos_or_vel.z = cmd.content.nav_velocity.z * 100.0f;
guided_set_velocity(pos_or_vel);
2014-05-26 04:00:25 -03:00
return true;
break;
2014-07-24 07:38:00 -03:00
#endif
2014-05-26 04:00:25 -03:00
case MAV_CMD_CONDITION_YAW:
do_yaw(cmd);
return true;
break;
default:
// reject unrecognised command
return false;
break;
}
2014-02-27 22:19:05 -04:00
return true;
2013-06-01 23:25:35 -03:00
}
2014-02-27 22:19:05 -04:00
static void do_change_speed(const AP_Mission::Mission_Command& cmd)
2011-03-13 03:25:38 -03:00
{
2014-03-18 23:14:06 -03:00
if (cmd.content.speed.target_ms > 0) {
2014-04-29 22:49:20 -03:00
wp_nav.set_speed_xy(cmd.content.speed.target_ms * 100.0f);
2014-03-18 23:14:06 -03:00
}
2011-03-13 03:25:38 -03:00
}
2014-02-27 22:19:05 -04:00
static void do_set_home(const AP_Mission::Mission_Command& cmd)
2011-03-02 22:32:50 -04:00
{
2014-03-03 01:38:32 -04:00
if(cmd.p1 == 1) {
2012-08-21 23:19:50 -03:00
init_home();
} else {
2014-03-31 04:07:46 -03:00
Location loc = cmd.content.location;
ahrs.set_home(loc);
2012-11-10 01:55:51 -04:00
set_home_is_set(true);
2012-08-21 23:19:50 -03:00
}
2011-03-02 22:32:50 -04:00
}
2013-07-13 11:27:51 -03:00
// do_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
// TO-DO: add support for other features of MAV_CMD_DO_SET_ROI including pointing at a given waypoint
2014-02-27 22:19:05 -04:00
static void do_roi(const AP_Mission::Mission_Command& cmd)
2012-07-24 23:02:54 -03:00
{
2014-07-04 03:40:12 -03:00
set_auto_yaw_roi(cmd.content.location);
2012-07-24 23:02:54 -03:00
}
2012-12-06 10:48:30 -04:00
// do_take_picture - take a picture with the camera library
static void do_take_picture()
{
2012-12-06 11:57:08 -04:00
#if CAMERA == ENABLED
2012-12-22 04:26:27 -04:00
camera.trigger_pic();
2012-12-06 11:57:08 -04:00
if (g.log_bitmask & MASK_LOG_CAMERA) {
2014-06-10 23:55:50 -03:00
DataFlash.Log_Write_Camera(ahrs, gps, current_loc);
2012-12-06 11:57:08 -04:00
}
#endif
2012-12-06 10:48:30 -04:00
}