2011-09-08 22:29:39 -03:00
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
/********************************************************************************/
// Command Event Handlers
/********************************************************************************/
static void
2011-10-25 22:27:23 -03:00
handle_process_nav_cmd()
2011-09-08 22:29:39 -03:00
{
2012-08-22 03:17:55 -03:00
// set land_complete to false to stop us zeroing the throttle
land_complete = false;
// set takeoff_complete to true so we don't add extra evevator
// except in a takeoff
takeoff_complete = true;
2012-08-22 02:29:46 -03:00
gcs_send_text_fmt(PSTR("Executing nav command ID #%i"),next_nav_command.id);
2012-08-16 21:50:15 -03:00
switch(next_nav_command.id) {
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_NAV_TAKEOFF:
do_takeoff();
break;
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_NAV_WAYPOINT: // Navigate to Waypoint
do_nav_wp();
break;
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_NAV_LAND: // LAND to Waypoint
do_land();
break;
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_NAV_LOITER_UNLIM: // Loiter indefinitely
do_loiter_unlimited();
break;
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_NAV_LOITER_TURNS: // Loiter N Times
do_loiter_turns();
break;
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_NAV_LOITER_TIME:
do_loiter_time();
break;
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_NAV_RETURN_TO_LAUNCH:
do_RTL();
break;
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
default:
break;
}
2011-09-08 22:29:39 -03:00
}
static void
2011-10-25 22:27:23 -03:00
handle_process_condition_command()
2011-09-08 22:29:39 -03:00
{
2012-08-16 21:50:15 -03:00
gcs_send_text_fmt(PSTR("Executing command ID #%i"),next_nonnav_command.id);
switch(next_nonnav_command.id) {
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_CONDITION_DELAY:
do_wait_delay();
break;
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_CONDITION_DISTANCE:
do_within_distance();
break;
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_CONDITION_CHANGE_ALT:
do_change_alt();
break;
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
default:
break;
}
2011-09-08 22:29:39 -03:00
}
2011-10-25 22:27:23 -03:00
static void handle_process_do_command()
2011-09-08 22:29:39 -03:00
{
2012-08-16 21:50:15 -03:00
gcs_send_text_fmt(PSTR("Executing command ID #%i"),next_nonnav_command.id);
switch(next_nonnav_command.id) {
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_DO_JUMP:
do_jump();
break;
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_DO_CHANGE_SPEED:
do_change_speed();
break;
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_DO_SET_HOME:
do_set_home();
break;
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_DO_SET_SERVO:
2014-01-20 00:36:31 -04:00
ServoRelayEvents.do_set_servo(next_nonnav_command.p1, next_nonnav_command.alt);
2012-08-16 21:50:15 -03:00
break;
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_DO_SET_RELAY:
2014-01-20 00:36:31 -04:00
ServoRelayEvents.do_set_relay(next_nonnav_command.p1, next_nonnav_command.alt);
2012-08-16 21:50:15 -03:00
break;
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_DO_REPEAT_SERVO:
2014-01-20 00:36:31 -04:00
ServoRelayEvents.do_repeat_servo(next_nonnav_command.p1, next_nonnav_command.alt,
next_nonnav_command.lat, next_nonnav_command.lng);
2012-08-16 21:50:15 -03:00
break;
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_DO_REPEAT_RELAY:
2014-01-20 00:36:31 -04:00
ServoRelayEvents.do_repeat_relay(next_nonnav_command.p1, next_nonnav_command.alt,
next_nonnav_command.lat);
2012-08-16 21:50:15 -03:00
break;
2011-10-31 18:55:58 -03:00
2012-06-13 16:00:20 -03:00
#if CAMERA == ENABLED
2012-08-16 21:50:15 -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-06-13 16:00:20 -03:00
2012-08-16 21:50:15 -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-06-13 16:00:20 -03:00
2012-08-16 21:50:15 -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|
2013-04-01 23:08:23 -03:00
do_take_picture();
2012-08-16 21:50:15 -03:00
break;
2013-10-11 07:30:01 -03:00
case MAV_CMD_DO_SET_CAM_TRIGG_DIST:
camera.set_trigger_distance(next_nonnav_command.alt);
break;
2012-06-13 16:00:20 -03:00
#endif
2011-10-31 18:55:58 -03:00
#if MOUNT == ENABLED
2012-08-16 21:50:15 -03:00
// Sets the region of interest (ROI) for a sensor set or the
// vehicle itself. This can then be used by the vehicles control
// system to control the vehicle attitude and the attitude of various
// devices such as cameras.
// |Region of interest mode. (see MAV_ROI enum)| Waypoint index/ target ID. (see MAV_ROI enum)| ROI index (allows a vehicle to manage multiple cameras etc.)| Empty| x the location of the fixed ROI (see MAV_FRAME)| y| z|
case MAV_CMD_NAV_ROI:
#if 0
// send the command to the camera mount
camera_mount.set_roi_cmd(&command_nav_queue);
#else
gcs_send_text_P(SEVERITY_LOW, PSTR("DO_SET_ROI not supported"));
#endif
break;
2011-10-31 18:55:58 -03:00
2012-08-16 21:50:15 -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;
2011-10-31 18:55:58 -03:00
2012-08-16 21:50:15 -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;
2011-10-31 18:55:58 -03:00
#endif
2012-08-16 21:50:15 -03:00
}
2011-09-08 22:29:39 -03:00
}
static void handle_no_commands()
{
2012-08-16 21:50:15 -03:00
gcs_send_text_fmt(PSTR("Returning to Home"));
2014-01-03 07:33:40 -04:00
next_nav_command = rally_find_best_location(current_loc, home);
2012-08-16 21:50:15 -03:00
next_nav_command.id = MAV_CMD_NAV_LOITER_UNLIM;
nav_command_ID = MAV_CMD_NAV_LOITER_UNLIM;
non_nav_command_ID = WAIT_COMMAND;
handle_process_nav_cmd();
2011-09-08 22:29:39 -03:00
}
2013-04-15 01:41:15 -03:00
/*******************************************************************************
Verify command Handlers
Each type of mission element has a "verify" operation. The verify
operation returns true when the mission element has completed and we
should move onto the next mission element.
*******************************************************************************/
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
static bool verify_nav_command() // Returns true if command complete
2011-09-08 22:29:39 -03:00
{
2012-08-16 21:50:15 -03:00
switch(nav_command_ID) {
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_NAV_TAKEOFF:
return verify_takeoff();
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_NAV_LAND:
return verify_land();
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_NAV_WAYPOINT:
return verify_nav_wp();
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_NAV_LOITER_UNLIM:
return verify_loiter_unlim();
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_NAV_LOITER_TURNS:
return verify_loiter_turns();
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_NAV_LOITER_TIME:
return verify_loiter_time();
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
case MAV_CMD_NAV_RETURN_TO_LAUNCH:
return verify_RTL();
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
default:
gcs_send_text_P(SEVERITY_HIGH,PSTR("verify_nav: Invalid or no current Nav cmd"));
}
2012-08-12 07:53:44 -03:00
return false;
2011-09-08 22:29:39 -03:00
}
2012-08-16 21:50:15 -03:00
static bool verify_condition_command() // Returns true if command complete
2011-09-08 22:29:39 -03:00
{
2012-08-16 21:50:15 -03:00
switch(non_nav_command_ID) {
2011-09-08 22:29:39 -03:00
case NO_COMMAND:
break;
case MAV_CMD_CONDITION_DELAY:
return verify_wait_delay();
break;
case MAV_CMD_CONDITION_DISTANCE:
return verify_within_distance();
break;
case MAV_CMD_CONDITION_CHANGE_ALT:
return verify_change_alt();
break;
2012-07-19 03:07:34 -03:00
2011-10-27 16:43:19 -03:00
case WAIT_COMMAND:
return 0;
break;
2012-07-19 03:07:34 -03:00
2011-09-08 22:29:39 -03:00
default:
2011-10-25 22:27:23 -03:00
gcs_send_text_P(SEVERITY_HIGH,PSTR("verify_conditon: Invalid or no current Condition cmd"));
2011-09-08 22:29:39 -03:00
break;
2012-08-16 21:50:15 -03:00
}
2011-09-08 22:29:39 -03:00
return false;
}
/********************************************************************************/
// Nav (Must) commands
/********************************************************************************/
static void do_RTL(void)
{
2012-08-16 21:50:15 -03:00
control_mode = RTL;
2013-07-05 01:56:58 -03:00
prev_WP = current_loc;
2014-01-03 07:33:40 -04:00
next_WP = rally_find_best_location(current_loc, home);
2013-04-14 09:04:25 -03:00
if (g.loiter_radius < 0) {
2013-04-15 08:31:11 -03:00
loiter.direction = -1;
2013-04-14 09:04:25 -03:00
} else {
2013-04-15 08:31:11 -03:00
loiter.direction = 1;
2013-04-14 09:04:25 -03:00
}
2011-09-08 22:29:39 -03:00
2013-07-05 01:56:58 -03:00
setup_glide_slope();
2014-01-13 22:07:43 -04:00
if (should_log(MASK_LOG_MODE))
2012-08-16 21:50:15 -03:00
Log_Write_Mode(control_mode);
2011-09-08 22:29:39 -03:00
}
static void do_takeoff()
{
2012-08-16 21:50:15 -03:00
set_next_WP(&next_nav_command);
// pitch in deg, airspeed m/s, throttle %, track WP 1 or 0
takeoff_pitch_cd = (int)next_nav_command.p1 * 100;
2013-06-26 05:36:53 -03:00
takeoff_altitude_cm = next_nav_command.alt;
2014-01-03 07:33:40 -04:00
next_WP.lat = home.lat + 10;
next_WP.lng = home.lng + 10;
2012-08-16 21:50:15 -03:00
takeoff_complete = false; // set flag to use gps ground course during TO. IMU will be doing yaw drift correction
// Flag also used to override "on the ground" throttle disable
2011-09-08 22:29:39 -03:00
}
static void do_nav_wp()
{
2012-08-16 21:50:15 -03:00
set_next_WP(&next_nav_command);
2011-09-08 22:29:39 -03:00
}
static void do_land()
{
2012-08-16 21:50:15 -03:00
set_next_WP(&next_nav_command);
2011-09-08 22:29:39 -03:00
}
2013-03-26 08:58:54 -03:00
static void loiter_set_direction_wp(const struct Location *nav_command)
2013-01-13 09:29:53 -04:00
{
if (nav_command->options & MASK_OPTIONS_LOITER_DIRECTION) {
2013-04-15 08:31:11 -03:00
loiter.direction = -1;
2013-01-13 09:29:53 -04:00
} else {
2013-04-15 08:31:11 -03:00
loiter.direction = 1;
2013-01-13 09:29:53 -04:00
}
}
2011-09-08 22:29:39 -03:00
static void do_loiter_unlimited()
{
2012-08-16 21:50:15 -03:00
set_next_WP(&next_nav_command);
2013-01-13 09:29:53 -04:00
loiter_set_direction_wp(&next_nav_command);
2011-09-08 22:29:39 -03:00
}
static void do_loiter_turns()
{
2012-08-16 21:50:15 -03:00
set_next_WP(&next_nav_command);
2013-04-15 08:31:11 -03:00
loiter.total_cd = next_nav_command.p1 * 36000UL;
2013-01-13 09:29:53 -04:00
loiter_set_direction_wp(&next_nav_command);
2011-09-08 22:29:39 -03:00
}
static void do_loiter_time()
{
2012-08-16 21:50:15 -03:00
set_next_WP(&next_nav_command);
2013-04-15 08:39:14 -03:00
// we set start_time_ms when we reach the waypoint
loiter.start_time_ms = 0;
2013-04-15 08:31:11 -03:00
loiter.time_max_ms = next_nav_command.p1 * (uint32_t)1000; // units are seconds
2013-01-13 09:29:53 -04:00
loiter_set_direction_wp(&next_nav_command);
2011-09-08 22:29:39 -03:00
}
/********************************************************************************/
// Verify Nav (Must) commands
/********************************************************************************/
static bool verify_takeoff()
{
2012-08-16 21:50:15 -03:00
if (ahrs.yaw_initialised()) {
2013-10-03 09:01:43 -03:00
if (steer_state.hold_course_cd == -1) {
2012-08-16 21:50:15 -03:00
// save our current course to take off
2013-10-03 09:01:43 -03:00
steer_state.hold_course_cd = ahrs.yaw_sensor;
gcs_send_text_fmt(PSTR("Holding course %ld"), steer_state.hold_course_cd);
2012-08-16 21:50:15 -03:00
}
}
2011-09-08 22:29:39 -03:00
2013-10-03 09:01:43 -03:00
if (steer_state.hold_course_cd != -1) {
2013-04-11 21:25:46 -03:00
// call navigation controller for heading hold
2013-10-03 09:01:43 -03:00
nav_controller->update_heading_hold(steer_state.hold_course_cd);
2013-04-11 21:25:46 -03:00
} else {
nav_controller->update_level_flight();
2012-08-16 21:50:15 -03:00
}
2011-09-08 22:29:39 -03:00
2013-06-26 05:36:53 -03:00
// see if we have reached takeoff altitude
2013-06-26 07:48:45 -03:00
if (adjusted_altitude_cm() > takeoff_altitude_cm) {
2013-10-03 09:01:43 -03:00
steer_state.hold_course_cd = -1;
2012-08-16 21:50:15 -03:00
takeoff_complete = true;
2013-04-11 21:25:46 -03:00
next_WP = prev_WP = current_loc;
2012-08-16 21:50:15 -03:00
return true;
} else {
return false;
}
2011-09-08 22:29:39 -03:00
}
2012-02-14 17:44:33 -04:00
// we are executing a landing
2011-09-08 22:29:39 -03:00
static bool verify_land()
{
2012-08-16 21:50:15 -03:00
// we don't 'verify' landing in the sense that it never completes,
// so we don't verify command completion. Instead we use this to
// adjust final landing parameters
2012-02-14 17:44:33 -04:00
// Set land_complete if we are within 2 seconds distance or within
// 3 meters altitude of the landing point
2013-07-10 01:03:03 -03:00
if ((wp_distance <= (g.land_flare_sec*g_gps->ground_speed_cm*0.01f))
2012-09-19 03:22:53 -03:00
|| (adjusted_altitude_cm() <= next_WP.alt + g.land_flare_alt*100)) {
2011-09-08 22:29:39 -03:00
2012-08-16 21:50:15 -03:00
land_complete = true;
2012-02-14 17:49:38 -04:00
2013-10-03 09:01:43 -03:00
if (steer_state.hold_course_cd == -1) {
2012-08-15 05:11:16 -03:00
// we have just reached the threshold of to flare for landing.
2012-08-16 21:50:15 -03:00
// We now don't want to do any radical
// turns, as rolling could put the wings into the runway.
2013-10-03 09:01:43 -03:00
// To prevent further turns we set steer_state.hold_course_cd to the
2012-08-16 21:50:15 -03:00
// current heading. Previously we set this to
// crosstrack_bearing, but the xtrack bearing can easily
// be quite large at this point, and that could induce a
// sudden large roll correction which is very nasty at
// this point in the landing.
2013-10-03 09:01:43 -03:00
steer_state.hold_course_cd = ahrs.yaw_sensor;
gcs_send_text_fmt(PSTR("Land Complete - Hold course %ld"), steer_state.hold_course_cd);
2012-08-16 21:50:15 -03:00
}
2012-08-14 23:34:51 -03:00
2013-07-10 01:03:03 -03:00
if (g_gps->ground_speed_cm*0.01f < 3.0) {
2013-02-10 20:40:36 -04:00
// reload any airspeed or groundspeed parameters that may have
// been set for landing. We don't do this till ground
// speed drops below 3.0 m/s as otherwise we will change
// target speeds too early.
g.airspeed_cruise_cm.load();
g.min_gndspeed_cm.load();
2013-06-26 07:48:45 -03:00
aparm.throttle_cruise.load();
2013-02-10 20:40:36 -04:00
}
2012-08-16 21:50:15 -03:00
}
2011-09-08 22:29:39 -03:00
2013-10-03 09:01:43 -03:00
if (steer_state.hold_course_cd != -1) {
2012-08-16 21:50:15 -03:00
// recalc bearing error with hold_course;
2013-10-03 09:01:43 -03:00
nav_controller->update_heading_hold(steer_state.hold_course_cd);
2012-08-24 08:23:49 -03:00
} else {
2013-04-11 21:25:46 -03:00
nav_controller->update_waypoint(prev_WP, next_WP);
2012-08-16 21:50:15 -03:00
}
return false;
2011-09-08 22:29:39 -03:00
}
static bool verify_nav_wp()
{
2013-10-03 09:01:43 -03:00
steer_state.hold_course_cd = -1;
2013-04-11 21:25:46 -03:00
nav_controller->update_waypoint(prev_WP, next_WP);
2013-09-13 04:43:00 -03:00
// see if the user has specified a maximum distance to waypoint
if (g.waypoint_max_radius > 0 && wp_distance > (uint16_t)g.waypoint_max_radius) {
if (location_passed_point(current_loc, prev_WP, next_WP)) {
// this is needed to ensure completion of the waypoint
prev_WP = current_loc;
}
return false;
}
2013-04-11 21:25:46 -03:00
if (wp_distance <= nav_controller->turn_distance(g.waypoint_radius)) {
2012-08-16 21:50:15 -03:00
gcs_send_text_fmt(PSTR("Reached Waypoint #%i dist %um"),
2012-07-03 20:21:34 -03:00
(unsigned)nav_command_index,
2013-08-04 21:30:04 -03:00
(unsigned)get_distance(current_loc, next_WP));
2012-08-16 21:50:15 -03:00
return true;
2013-04-11 21:25:46 -03:00
}
2012-07-03 20:21:34 -03:00
// have we flown past the waypoint?
if (location_passed_point(current_loc, prev_WP, next_WP)) {
2012-08-16 21:50:15 -03:00
gcs_send_text_fmt(PSTR("Passed Waypoint #%i dist %um"),
2012-07-03 20:21:34 -03:00
(unsigned)nav_command_index,
2013-08-04 21:30:04 -03:00
(unsigned)get_distance(current_loc, next_WP));
2012-07-03 20:21:34 -03:00
return true;
}
2012-08-16 21:50:15 -03:00
return false;
2011-09-08 22:29:39 -03:00
}
static bool verify_loiter_unlim()
{
2012-08-16 21:50:15 -03:00
update_loiter();
return false;
2011-09-08 22:29:39 -03:00
}
static bool verify_loiter_time()
{
2012-08-16 21:50:15 -03:00
update_loiter();
2013-04-15 08:39:14 -03:00
if (loiter.start_time_ms == 0) {
if (nav_controller->reached_loiter_target()) {
// we've reached the target, start the timer
loiter.start_time_ms = millis();
}
} else if ((millis() - loiter.start_time_ms) > loiter.time_max_ms) {
2012-08-16 21:50:15 -03:00
gcs_send_text_P(SEVERITY_LOW,PSTR("verify_nav: LOITER time complete"));
return true;
}
return false;
2011-09-08 22:29:39 -03:00
}
static bool verify_loiter_turns()
{
2012-08-16 21:50:15 -03:00
update_loiter();
2013-04-15 08:31:11 -03:00
if (loiter.sum_cd > loiter.total_cd) {
loiter.total_cd = 0;
2012-08-16 21:50:15 -03:00
gcs_send_text_P(SEVERITY_LOW,PSTR("verify_nav: LOITER orbits complete"));
// clear the command queue;
return true;
}
return false;
2011-09-08 22:29:39 -03:00
}
static bool verify_RTL()
{
2013-04-11 21:25:46 -03:00
update_loiter();
if (wp_distance <= (uint32_t)max(g.waypoint_radius,0) ||
nav_controller->reached_loiter_target()) {
gcs_send_text_P(SEVERITY_LOW,PSTR("Reached home"));
return true;
} else {
2012-08-16 21:50:15 -03:00
return false;
2013-04-11 21:25:46 -03:00
}
2011-09-08 22:29:39 -03:00
}
/********************************************************************************/
// Condition (May) commands
/********************************************************************************/
static void do_wait_delay()
{
2012-08-16 21:50:15 -03:00
condition_start = millis();
condition_value = next_nonnav_command.lat * 1000; // convert to milliseconds
2011-09-08 22:29:39 -03:00
}
static void do_change_alt()
{
2012-08-16 21:50:15 -03:00
condition_rate = labs((int)next_nonnav_command.lat);
condition_value = next_nonnav_command.alt;
2012-09-19 03:22:53 -03:00
if (condition_value < adjusted_altitude_cm()) {
condition_rate = -condition_rate;
}
target_altitude_cm = adjusted_altitude_cm() + (condition_rate / 10); // Divide by ten for 10Hz update
2012-08-16 21:50:15 -03:00
next_WP.alt = condition_value; // For future nav calculations
offset_altitude_cm = 0; // For future nav calculations
2011-09-08 22:29:39 -03:00
}
static void do_within_distance()
{
2012-08-16 21:50:15 -03:00
condition_value = next_nonnav_command.lat;
2011-09-08 22:29:39 -03:00
}
/********************************************************************************/
// Verify Condition (May) commands
/********************************************************************************/
static bool verify_wait_delay()
{
2012-08-16 21:50:15 -03:00
if ((unsigned)(millis() - condition_start) > (unsigned)condition_value) {
condition_value = 0;
return true;
}
return false;
2011-09-08 22:29:39 -03:00
}
static bool verify_change_alt()
{
2012-09-19 03:22:53 -03:00
if( (condition_rate>=0 && adjusted_altitude_cm() >= condition_value) ||
(condition_rate<=0 && adjusted_altitude_cm() <= condition_value)) {
2012-08-16 21:50:15 -03:00
condition_value = 0;
return true;
}
target_altitude_cm += condition_rate / 10;
return false;
2011-09-08 22:29:39 -03:00
}
static bool verify_within_distance()
{
2013-01-26 04:04:12 -04:00
if (wp_distance < max(condition_value,0)) {
2012-08-16 21:50:15 -03:00
condition_value = 0;
return true;
}
return false;
2011-09-08 22:29:39 -03:00
}
/********************************************************************************/
// Do (Now) commands
/********************************************************************************/
static void do_loiter_at_location()
{
2013-04-14 09:04:25 -03:00
if (g.loiter_radius < 0) {
2013-04-15 08:31:11 -03:00
loiter.direction = -1;
2013-04-14 09:04:25 -03:00
} else {
2013-04-15 08:31:11 -03:00
loiter.direction = 1;
2013-04-14 09:04:25 -03:00
}
2012-08-16 21:50:15 -03:00
next_WP = current_loc;
2011-09-08 22:29:39 -03:00
}
static void do_jump()
{
2012-08-22 02:29:46 -03:00
if (next_nonnav_command.lat == 0) {
// the jump counter has reached zero - ignore
gcs_send_text_fmt(PSTR("Jumps left: 0 - skipping"));
return;
}
if (next_nonnav_command.p1 >= g.command_total) {
gcs_send_text_fmt(PSTR("Skipping invalid jump to %i"), next_nonnav_command.p1);
return;
}
2012-08-16 21:50:15 -03:00
struct Location temp;
2012-08-22 02:29:46 -03:00
temp = get_cmd_with_index(g.command_index);
2012-08-16 21:50:15 -03:00
gcs_send_text_fmt(PSTR("Jump to WP %u. Jumps left: %d"),
2012-08-14 03:54:08 -03:00
(unsigned)next_nonnav_command.p1,
(int)next_nonnav_command.lat);
2012-08-16 21:50:15 -03:00
if (next_nonnav_command.lat > 0) {
2012-08-22 02:29:46 -03:00
// Decrement repeat counter
temp.lat = next_nonnav_command.lat - 1;
set_cmd_with_index(temp, g.command_index);
}
2012-07-19 03:07:34 -03:00
2012-08-22 02:29:46 -03:00
nav_command_ID = NO_COMMAND;
next_nav_command.id = NO_COMMAND;
non_nav_command_ID = NO_COMMAND;
2011-10-25 22:27:23 -03:00
2012-08-27 23:01:12 -03:00
gcs_send_text_fmt(PSTR("setting command index: %i"), next_nonnav_command.p1);
g.command_index.set_and_save(next_nonnav_command.p1);
nav_command_index = next_nonnav_command.p1;
// Need to back "next_WP" up as it was set to the next waypoint following the jump
next_WP = prev_WP;
2012-08-22 02:29:46 -03:00
temp = get_cmd_with_index(g.command_index);
next_nav_command = temp;
nav_command_ID = next_nav_command.id;
non_nav_command_index = g.command_index;
non_nav_command_ID = WAIT_COMMAND;
2014-01-13 22:07:43 -04:00
if (should_log(MASK_LOG_CMD)) {
2012-08-22 02:29:46 -03:00
Log_Write_Cmd(g.command_index, &next_nav_command);
2012-08-16 21:50:15 -03:00
}
2012-08-27 23:01:12 -03:00
handle_process_nav_cmd();
2011-09-08 22:29:39 -03:00
}
static void do_change_speed()
{
2012-08-16 21:50:15 -03:00
switch (next_nonnav_command.p1)
{
case 0: // Airspeed
if (next_nonnav_command.alt > 0) {
g.airspeed_cruise_cm.set(next_nonnav_command.alt * 100);
gcs_send_text_fmt(PSTR("Set airspeed %u m/s"), (unsigned)next_nonnav_command.alt);
}
break;
case 1: // Ground speed
2012-09-12 00:09:32 -03:00
gcs_send_text_fmt(PSTR("Set groundspeed %u"), (unsigned)next_nonnav_command.alt);
2012-08-16 21:50:15 -03:00
g.min_gndspeed_cm.set(next_nonnav_command.alt * 100);
break;
}
2011-09-08 22:29:39 -03:00
2012-09-12 00:09:32 -03:00
if (next_nonnav_command.lat > 0) {
gcs_send_text_fmt(PSTR("Set throttle %u"), (unsigned)next_nonnav_command.lat);
2013-06-26 07:48:45 -03:00
aparm.throttle_cruise.set(next_nonnav_command.lat);
2012-09-12 00:09:32 -03:00
}
2011-09-08 22:29:39 -03:00
}
static void do_set_home()
{
2013-03-25 04:25:24 -03:00
if (next_nonnav_command.p1 == 1 && g_gps->status() == GPS::GPS_OK_FIX_3D) {
2012-08-16 21:50:15 -03:00
init_home();
} else {
2014-01-02 07:06:25 -04:00
ahrs.set_home(next_nonnav_command.lat, next_nonnav_command.lng, next_nonnav_command.alt);
2012-08-16 21:50:15 -03:00
home_is_set = true;
}
2011-09-08 22:29:39 -03:00
}
2013-04-01 23:08:23 -03:00
// do_take_picture - take a picture with the camera library
static void do_take_picture()
{
#if CAMERA == ENABLED
camera.trigger_pic();
2014-01-13 22:07:43 -04:00
if (should_log(MASK_LOG_CAMERA)) {
2013-07-09 23:23:36 -03:00
Log_Write_Camera();
}
2013-04-01 23:08:23 -03:00
#endif
}