2015-05-29 23:12:49 -03:00
# include "Copter.h"
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
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : start_command ( const AP_Mission : : Mission_Command & cmd )
2014-02-27 22:19:05 -04:00
{
// To-Do: logging when new commands start/end
2017-12-10 22:50:49 -04:00
if ( _copter . should_log ( MASK_LOG_CMD ) ) {
_copter . DataFlash . Log_Write_Mission_Cmd ( mission , 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
2016-11-17 01:19:22 -04:00
case MAV_CMD_NAV_PAYLOAD_PLACE : // 94 place at Waypoint
do_payload_place ( cmd ) ;
break ;
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-10-13 05:41:48 -03:00
case MAV_CMD_NAV_GUIDED_ENABLE : // 92 accept navigation commands from external nav computer
do_nav_guided_enable ( cmd ) ;
2014-05-23 02:29:08 -03:00
break ;
# endif
2016-04-12 03:10:09 -03:00
case MAV_CMD_NAV_DELAY : // 94 Delay the next navigation command
do_nav_delay ( cmd ) ;
break ;
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_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
2015-02-09 07:25:45 -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 :
2017-12-10 22:50:49 -04:00
_copter . 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 :
2017-12-10 22:50:49 -04:00
_copter . 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 :
2017-12-10 22:50:49 -04:00
_copter . ServoRelayEvents . do_repeat_servo ( cmd . content . repeat_servo . channel , cmd . content . repeat_servo . pwm ,
2014-03-18 23:14:06 -03:00
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 :
2017-12-10 22:50:49 -04:00
_copter . ServoRelayEvents . do_repeat_relay ( cmd . content . repeat_relay . num , cmd . content . repeat_relay . repeat_count ,
2014-03-18 23:14:06 -03:00
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 ;
2015-03-21 08:59:50 -03:00
case MAV_CMD_DO_MOUNT_CONTROL : // 205
// point the camera to a specified angle
do_mount_control ( cmd ) ;
break ;
2017-02-04 16:26:22 -04:00
case MAV_CMD_DO_FENCE_ENABLE :
# if AC_FENCE == ENABLED
if ( cmd . p1 = = 0 ) { //disable
copter . fence . enable ( false ) ;
2017-07-08 21:56:49 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Fence Disabled " ) ;
2017-02-04 16:26:22 -04:00
} else { //enable fence
copter . fence . enable ( true ) ;
2017-07-08 21:56:49 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Fence Enabled " ) ;
2017-02-04 16:26:22 -04:00
}
# endif //AC_FENCE == ENABLED
break ;
2015-03-21 08:59:50 -03:00
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)|
2015-03-29 15:49:22 -03:00
do_digicam_configure ( cmd ) ;
2012-08-21 23:19:50 -03:00
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|
2015-03-29 15:49:22 -03:00
do_digicam_control ( cmd ) ;
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 :
2017-12-10 22:50:49 -04:00
_copter . 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
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
2016-10-29 08:01:29 -03:00
# if GRIPPER_ENABLED == ENABLED
case MAV_CMD_DO_GRIPPER : // Mission command to control gripper
2014-09-17 08:31:30 -03:00
do_gripper ( cmd ) ;
break ;
# endif
2014-10-13 05:41:48 -03:00
# if NAV_GUIDED == ENABLED
case MAV_CMD_DO_GUIDED_LIMITS : // 220 accept guided mode limits
do_guided_limits ( cmd ) ;
break ;
# endif
2017-10-04 23:21:23 -03:00
case MAV_CMD_DO_WINCH : // Mission command to control winch
do_winch ( cmd ) ;
break ;
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
/********************************************************************************/
2015-07-20 04:08:00 -03:00
// verify_command_callback - callback function called from ap-mission at 10hz or higher when a command is being run
// we double check that the flight mode is AUTO to avoid the possibility of ap-mission triggering actions while we're not in AUTO mode
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : verify_command_callback ( const AP_Mission : : Mission_Command & cmd )
2015-07-20 04:08:00 -03:00
{
2017-12-10 22:50:49 -04:00
if ( _copter . flightmode = = & _copter . mode_auto ) {
2015-07-20 04:08:00 -03:00
bool cmd_complete = verify_command ( cmd ) ;
// send message to GCS
if ( cmd_complete ) {
2017-07-07 22:44:11 -03:00
gcs ( ) . send_mission_item_reached_message ( cmd . index ) ;
2015-07-20 04:08:00 -03:00
}
return cmd_complete ;
}
return false ;
}
2016-08-22 06:03:45 -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 .
Return true if we do not recognize the command so that we move on to the next command
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2015-07-20 04:08:00 -03:00
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : 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 ) {
//
// navigation commands
//
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_TAKEOFF :
2015-07-20 04:08:00 -03:00
return verify_takeoff ( ) ;
2011-02-24 01:56:59 -04:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_WAYPOINT :
2015-07-20 04:08:00 -03:00
return verify_nav_wp ( cmd ) ;
2012-07-24 23:02:54 -03:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_LAND :
2015-07-20 04:08:00 -03:00
return verify_land ( ) ;
2011-02-24 01:56:59 -04:00
2016-11-17 01:19:22 -04:00
case MAV_CMD_NAV_PAYLOAD_PLACE :
return verify_payload_place ( ) ;
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_LOITER_UNLIM :
2015-07-20 04:08:00 -03:00
return verify_loiter_unlimited ( ) ;
2011-03-13 03:25:38 -03:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_LOITER_TURNS :
2015-07-20 04:08:00 -03:00
return verify_circle ( cmd ) ;
2011-03-13 03:25:38 -03:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_LOITER_TIME :
2015-07-20 04:08:00 -03:00
return verify_loiter_time ( ) ;
2011-03-13 03:25:38 -03:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_NAV_RETURN_TO_LAUNCH :
2015-07-20 04:08:00 -03:00
return verify_RTL ( ) ;
2011-02-24 01:56:59 -04:00
2014-03-27 04:05:49 -03:00
case MAV_CMD_NAV_SPLINE_WAYPOINT :
2015-07-20 04:08:00 -03:00
return verify_spline_wp ( cmd ) ;
2014-03-27 04:05:49 -03:00
2014-05-23 02:29:08 -03:00
# if NAV_GUIDED == ENABLED
2014-10-13 05:41:48 -03:00
case MAV_CMD_NAV_GUIDED_ENABLE :
2015-07-20 04:08:00 -03:00
return verify_nav_guided_enable ( cmd ) ;
2014-05-23 02:29:08 -03:00
# endif
2016-04-12 03:10:09 -03:00
case MAV_CMD_NAV_DELAY :
return verify_nav_delay ( cmd ) ;
2014-02-27 22:19:05 -04:00
///
/// conditional commands
///
2012-08-21 23:19:50 -03:00
case MAV_CMD_CONDITION_DELAY :
2015-07-20 04:08:00 -03:00
return verify_wait_delay ( ) ;
2011-02-24 01:56:59 -04:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_CONDITION_DISTANCE :
2015-07-20 04:08:00 -03:00
return verify_within_distance ( ) ;
2011-02-24 01:56:59 -04:00
2012-08-21 23:19:50 -03:00
case MAV_CMD_CONDITION_YAW :
2015-07-20 04:08:00 -03:00
return verify_yaw ( ) ;
2011-03-13 03:25:38 -03:00
2016-08-22 06:03:45 -03:00
// do commands (always return true)
case MAV_CMD_DO_CHANGE_SPEED :
case MAV_CMD_DO_SET_HOME :
case MAV_CMD_DO_SET_SERVO :
case MAV_CMD_DO_SET_RELAY :
case MAV_CMD_DO_REPEAT_SERVO :
case MAV_CMD_DO_REPEAT_RELAY :
case MAV_CMD_DO_SET_ROI :
case MAV_CMD_DO_MOUNT_CONTROL :
case MAV_CMD_DO_CONTROL_VIDEO :
case MAV_CMD_DO_DIGICAM_CONFIGURE :
case MAV_CMD_DO_DIGICAM_CONTROL :
case MAV_CMD_DO_SET_CAM_TRIGG_DIST :
case MAV_CMD_DO_PARACHUTE : // assume parachute was released successfully
case MAV_CMD_DO_GRIPPER :
case MAV_CMD_DO_GUIDED_LIMITS :
2017-02-04 16:26:22 -04:00
case MAV_CMD_DO_FENCE_ENABLE :
2017-10-04 23:21:23 -03:00
case MAV_CMD_DO_WINCH :
2015-07-20 04:08:00 -03:00
return true ;
2014-04-03 05:53:27 -03:00
2012-08-21 23:19:50 -03:00
default :
2016-08-22 06:03:45 -03:00
// error message
2017-07-08 21:56:49 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_WARNING , " Skipping invalid cmd #%i " , cmd . id ) ;
2015-07-24 05:10:04 -03:00
// return true if we do not recognize the command so that we move on to the next command
2015-07-20 04:08:00 -03:00
return true ;
2012-08-21 23:19:50 -03:00
}
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
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : exit_mission ( )
2014-02-27 22:19:05 -04:00
{
2014-10-23 15:47:00 -03:00
// play a tone
AP_Notify : : events . mission_complete = 1 ;
2014-02-27 22:19:05 -04:00
// 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
2017-12-10 22:50:49 -04:00
if ( ! loiter_start ( ) ) {
2016-01-25 19:40:41 -04:00
set_mode ( LAND , MODE_REASON_MISSION_END ) ;
2014-02-27 22:19:05 -04:00
}
} else {
// if we've landed it's safe to disarm
2017-12-10 22:50:49 -04:00
_copter . init_disarm_motors ( ) ;
2014-02-27 22:19:05 -04:00
}
}
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
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : 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
2017-12-10 22:50:49 -04:00
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
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : 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
2017-12-10 22:50:49 -04:00
takeoff_start ( cmd . content . location ) ;
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
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : do_nav_wp ( const AP_Mission : : Mission_Command & cmd )
2011-03-13 03:25:38 -03:00
{
2016-05-12 04:33:50 -03:00
Location_Class target_loc ( cmd . content . location ) ;
2017-12-10 22:50:49 -04:00
const Location_Class & current_loc = _copter . current_loc ;
2016-05-12 04:33:50 -03:00
// use current lat, lon if zero
if ( target_loc . lat = = 0 & & target_loc . lng = = 0 ) {
target_loc . lat = current_loc . lat ;
target_loc . lng = current_loc . lng ;
}
// use current altitude if not provided
if ( target_loc . alt = = 0 ) {
// set to current altitude but in command's alt frame
int32_t curr_alt ;
if ( current_loc . get_alt_cm ( target_loc . get_alt_frame ( ) , curr_alt ) ) {
target_loc . set_alt_cm ( curr_alt , target_loc . get_alt_frame ( ) ) ;
} else {
// default to current altitude as alt-above-home
target_loc . set_alt_cm ( current_loc . alt , current_loc . get_alt_frame ( ) ) ;
}
}
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
2016-04-24 03:51:41 -03:00
loiter_time_max = cmd . p1 ;
2014-03-22 00:48:54 -03:00
2014-03-27 04:11:50 -03:00
// Set wp navigation target
2017-12-10 22:50:49 -04:00
wp_start ( target_loc ) ;
2015-08-16 16:10:23 -03:00
2014-03-27 04:11:50 -03:00
// if no delay set the waypoint as "fast"
if ( loiter_time_max = = 0 ) {
2017-01-09 03:31:26 -04:00
wp_nav - > set_fast_waypoint ( true ) ;
2013-05-08 12:18:36 -03:00
}
2011-03-13 03:25:38 -03:00
}
2016-11-17 01:19:22 -04:00
// terrain_adjusted_location: returns a Location with lat/lon from cmd
// and altitude from our current altitude adjusted for location
2017-12-10 22:50:49 -04:00
Location_Class Copter : : ModeAuto : : terrain_adjusted_location ( const AP_Mission : : Mission_Command & cmd ) const
2016-11-17 01:19:22 -04:00
{
// convert to location class
Location_Class target_loc ( cmd . content . location ) ;
2017-12-10 22:50:49 -04:00
const Location_Class & current_loc = _copter . current_loc ;
2016-11-17 01:19:22 -04:00
// decide if we will use terrain following
int32_t curr_terr_alt_cm , target_terr_alt_cm ;
if ( current_loc . get_alt_cm ( Location_Class : : ALT_FRAME_ABOVE_TERRAIN , curr_terr_alt_cm ) & &
target_loc . get_alt_cm ( Location_Class : : ALT_FRAME_ABOVE_TERRAIN , target_terr_alt_cm ) ) {
curr_terr_alt_cm = MAX ( curr_terr_alt_cm , 200 ) ;
// if using terrain, set target altitude to current altitude above terrain
target_loc . set_alt_cm ( curr_terr_alt_cm , Location_Class : : ALT_FRAME_ABOVE_TERRAIN ) ;
} else {
// set target altitude to current altitude above home
target_loc . set_alt_cm ( current_loc . alt , Location_Class : : ALT_FRAME_ABOVE_HOME ) ;
}
return target_loc ;
}
2012-12-09 02:50:50 -04:00
// do_land - initiate landing procedure
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : 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
2016-08-08 01:27:39 -03:00
land_state = LandStateType_FlyToLocation ;
2013-05-10 10:37:15 -03:00
2016-11-17 01:19:22 -04:00
Location_Class target_loc = terrain_adjusted_location ( cmd ) ;
2016-03-18 07:46:08 -03:00
2017-12-10 22:50:49 -04:00
wp_start ( target_loc ) ;
2013-02-18 01:58:24 -04:00
} else {
2013-05-10 10:37:15 -03:00
// set landing state
2016-08-08 01:27:39 -03:00
land_state = LandStateType_Descending ;
2012-12-10 10:38:43 -04:00
2014-01-24 22:37:58 -04:00
// initialise landing controller
2017-12-10 22:50:49 -04:00
land_start ( ) ;
2013-05-10 10:37:15 -03:00
}
2011-03-13 03:25:38 -03:00
}
2016-11-17 01:19:22 -04:00
// do_payload_place - initiate placing procedure
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : do_payload_place ( const AP_Mission : : Mission_Command & cmd )
2016-11-17 01:19:22 -04:00
{
// if location provided we fly to that location at current altitude
if ( cmd . content . location . lat ! = 0 | | cmd . content . location . lng ! = 0 ) {
// set state to fly to location
nav_payload_place . state = PayloadPlaceStateType_FlyToLocation ;
Location_Class target_loc = terrain_adjusted_location ( cmd ) ;
2017-12-10 22:50:49 -04:00
wp_start ( target_loc ) ;
2016-11-17 01:19:22 -04:00
} else {
nav_payload_place . state = PayloadPlaceStateType_Calibrating_Hover_Start ;
// initialise placing controller
2017-12-10 22:50:49 -04:00
payload_place_start ( ) ;
2016-11-17 01:19:22 -04:00
}
nav_payload_place . descend_max = cmd . p1 ;
}
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
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : do_loiter_unlimited ( const AP_Mission : : Mission_Command & cmd )
2011-03-13 03:25:38 -03:00
{
2016-03-09 06:26:18 -04:00
// convert back to location
Location_Class target_loc ( cmd . content . location ) ;
2017-12-10 22:50:49 -04:00
const Location_Class & current_loc = _copter . current_loc ;
2014-01-28 00:58:20 -04:00
2013-07-10 23:35:54 -03:00
// use current location if not provided
2016-03-09 06:26:18 -04:00
if ( target_loc . lat = = 0 & & target_loc . lng = = 0 ) {
// To-Do: make this simpler
Vector3f temp_pos ;
2017-01-09 03:31:26 -04:00
wp_nav - > get_wp_stopping_point_xy ( temp_pos ) ;
2016-05-12 06:16:58 -03:00
Location_Class temp_loc ( temp_pos ) ;
target_loc . lat = temp_loc . lat ;
target_loc . lng = temp_loc . lng ;
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
2016-03-09 06:26:18 -04:00
if ( target_loc . alt = = 0 ) {
// set to current altitude but in command's alt frame
int32_t curr_alt ;
if ( current_loc . get_alt_cm ( target_loc . get_alt_frame ( ) , curr_alt ) ) {
2016-04-28 07:53:30 -03:00
target_loc . set_alt_cm ( curr_alt , target_loc . get_alt_frame ( ) ) ;
2016-03-09 06:26:18 -04:00
} else {
// default to current altitude as alt-above-home
2016-04-28 07:53:30 -03:00
target_loc . set_alt_cm ( current_loc . alt , current_loc . get_alt_frame ( ) ) ;
2016-03-09 06:26:18 -04:00
}
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
2017-12-10 22:50:49 -04:00
wp_start ( target_loc ) ;
2013-02-18 01:58:24 -04:00
}
2013-02-25 04:50:56 -04:00
// do_circle - initiate moving in a circle
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : do_circle ( const AP_Mission : : Mission_Command & cmd )
2013-02-18 01:58:24 -04:00
{
2016-03-09 07:21:08 -04:00
Location_Class circle_center ( cmd . content . location ) ;
2017-12-10 22:50:49 -04:00
const Location_Class & current_loc = _copter . current_loc ;
2013-04-08 23:58:01 -03:00
2016-03-09 07:21:08 -04:00
// default lat/lon to current position if not provided
// To-Do: use stopping point or position_controller's target instead of current location to avoid jerk?
if ( circle_center . lat = = 0 & & circle_center . lng = = 0 ) {
circle_center . lat = current_loc . lat ;
circle_center . lng = current_loc . lng ;
2012-08-21 23:19:50 -03:00
}
2011-03-13 03:25:38 -03:00
2016-03-09 07:21:08 -04:00
// default target altitude to current altitude if not provided
if ( circle_center . alt = = 0 ) {
int32_t curr_alt ;
if ( current_loc . get_alt_cm ( circle_center . get_alt_frame ( ) , curr_alt ) ) {
// circle altitude uses frame from command
2016-04-28 07:53:30 -03:00
circle_center . set_alt_cm ( curr_alt , circle_center . get_alt_frame ( ) ) ;
2016-03-09 07:21:08 -04:00
} else {
// default to current altitude above origin
2016-04-28 07:53:30 -03:00
circle_center . set_alt_cm ( current_loc . alt , current_loc . get_alt_frame ( ) ) ;
2017-12-10 22:50:49 -04:00
_copter . Log_Write_Error ( ERROR_SUBSYSTEM_TERRAIN , ERROR_CODE_MISSING_TERRAIN_DATA ) ;
2016-03-09 07:21:08 -04:00
}
2012-08-21 23:19:50 -03:00
}
2013-02-25 04:50:56 -04:00
2016-03-09 07:21:08 -04:00
// calculate radius
uint8_t circle_radius_m = HIGHBYTE ( cmd . p1 ) ; // circle radius held in high byte of p1
2014-06-11 04:18:35 -03:00
2016-03-09 07:21:08 -04:00
// move to edge of circle (verify_circle) will ensure we begin circling once we reach the edge
2017-12-10 22:50:49 -04:00
circle_movetoedge_start ( circle_center , circle_radius_m ) ;
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
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : do_loiter_time ( const AP_Mission : : Mission_Command & cmd )
2011-03-13 03:25:38 -03:00
{
2016-03-09 06:26:18 -04:00
// re-use loiter unlimited
do_loiter_unlimited ( cmd ) ;
2013-04-08 23:58:01 -03:00
// setup loiter timer
loiter_time = 0 ;
2016-04-24 03:51:41 -03: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
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : do_spline_wp ( const AP_Mission : : Mission_Command & cmd )
2014-03-27 04:05:49 -03:00
{
2016-03-11 03:45:00 -04:00
Location_Class target_loc ( cmd . content . location ) ;
2017-12-10 22:50:49 -04:00
const Location_Class & current_loc = _copter . current_loc ;
2016-03-11 03:45:00 -04:00
// use current lat, lon if zero
if ( target_loc . lat = = 0 & & target_loc . lng = = 0 ) {
target_loc . lat = current_loc . lat ;
target_loc . lng = current_loc . lng ;
}
// use current altitude if not provided
if ( target_loc . alt = = 0 ) {
// set to current altitude but in command's alt frame
int32_t curr_alt ;
if ( current_loc . get_alt_cm ( target_loc . get_alt_frame ( ) , curr_alt ) ) {
2016-04-28 07:53:30 -03:00
target_loc . set_alt_cm ( curr_alt , target_loc . get_alt_frame ( ) ) ;
2016-03-11 03:45:00 -04:00
} else {
// default to current altitude as alt-above-home
2016-04-28 07:53:30 -03:00
target_loc . set_alt_cm ( current_loc . alt , current_loc . get_alt_frame ( ) ) ;
2016-03-11 03:45:00 -04:00
}
}
2014-03-27 04:05:49 -03:00
// 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
2016-04-24 03:51:41 -03:00
loiter_time_max = cmd . p1 ;
2014-03-27 04:05:49 -03:00
// 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 ;
// 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
2016-03-11 03:45:00 -04:00
Location_Class next_loc ;
2014-03-29 05:52:38 -03:00
if ( cmd . p1 = = 0 & & mission . get_next_nav_cmd ( cmd . index + 1 , temp_cmd ) ) {
2016-03-11 03:45:00 -04:00
next_loc = temp_cmd . content . location ;
// default lat, lon to first waypoint's lat, lon
if ( next_loc . lat = = 0 & & next_loc . lng = = 0 ) {
next_loc . lat = target_loc . lat ;
next_loc . lng = target_loc . lng ;
}
// default alt to first waypoint's alt but in next waypoint's alt frame
if ( next_loc . alt = = 0 ) {
int32_t next_alt ;
if ( target_loc . get_alt_cm ( next_loc . get_alt_frame ( ) , next_alt ) ) {
2016-04-28 07:53:30 -03:00
next_loc . set_alt_cm ( next_alt , next_loc . get_alt_frame ( ) ) ;
2016-03-11 03:45:00 -04:00
} else {
// default to first waypoints altitude
2016-04-28 07:53:30 -03:00
next_loc . set_alt_cm ( target_loc . alt , target_loc . get_alt_frame ( ) ) ;
2016-03-11 03:45:00 -04:00
}
}
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 ;
} else if ( temp_cmd . id = = MAV_CMD_NAV_SPLINE_WAYPOINT ) {
seg_end_type = AC_WPNav : : SEGMENT_END_SPLINE ;
}
}
// set spline navigation target
2017-12-10 22:50:49 -04:00
spline_start ( target_loc , stopped_at_start , seg_end_type , next_loc ) ;
2014-03-27 04:05:49 -03:00
}
2014-05-23 02:29:08 -03:00
# if NAV_GUIDED == ENABLED
2014-10-13 05:41:48 -03:00
// do_nav_guided_enable - initiate accepting commands from external nav computer
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : do_nav_guided_enable ( const AP_Mission : : Mission_Command & cmd )
2014-05-23 02:29:08 -03:00
{
2014-10-13 05:41:48 -03:00
if ( cmd . p1 > 0 ) {
// initialise guided limits
2017-12-10 22:50:49 -04:00
_copter . mode_guided . limit_init_time_and_pos ( ) ;
2014-05-23 02:29:08 -03:00
2014-10-13 05:41:48 -03:00
// set spline navigation target
2017-12-10 22:50:49 -04:00
nav_guided_start ( ) ;
2014-10-13 05:41:48 -03:00
}
2014-05-23 02:29:08 -03:00
}
# endif // NAV_GUIDED
2016-04-12 03:10:09 -03:00
// do_nav_delay - Delay the next navigation command
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : do_nav_delay ( const AP_Mission : : Mission_Command & cmd )
2016-04-12 03:10:09 -03:00
{
2016-05-04 09:19:26 -03:00
nav_delay_time_start = millis ( ) ;
2016-04-12 03:10:09 -03:00
if ( cmd . content . nav_delay . seconds > 0 ) {
// relative delay
2016-05-04 09:19:26 -03:00
nav_delay_time_max = cmd . content . nav_delay . seconds * 1000 ; // convert seconds to milliseconds
2016-04-12 03:10:09 -03:00
} else {
// absolute delay to utc time
2016-05-04 09:19:26 -03:00
nav_delay_time_max = hal . util - > get_time_utc ( cmd . content . nav_delay . hour_utc , cmd . content . nav_delay . min_utc , cmd . content . nav_delay . sec_utc , 0 ) ;
2016-04-12 03:10:09 -03:00
}
2017-07-08 21:56:49 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Delaying %u sec " , ( unsigned int ) ( nav_delay_time_max / 1000 ) ) ;
2016-04-12 03:10:09 -03:00
}
2014-05-23 02:29:08 -03:00
2014-04-03 05:53:27 -03:00
# if PARACHUTE == ENABLED
// do_parachute - configure or release parachute
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : do_parachute ( const AP_Mission : : Mission_Command & cmd )
2014-04-03 05:53:27 -03:00
{
switch ( cmd . p1 ) {
case PARACHUTE_DISABLE :
2017-12-10 22:50:49 -04:00
_copter . parachute . enabled ( false ) ;
2014-04-03 05:53:27 -03:00
Log_Write_Event ( DATA_PARACHUTE_DISABLED ) ;
break ;
case PARACHUTE_ENABLE :
2017-12-10 22:50:49 -04:00
_copter . parachute . enabled ( true ) ;
2014-04-03 05:53:27 -03:00
Log_Write_Event ( DATA_PARACHUTE_ENABLED ) ;
break ;
case PARACHUTE_RELEASE :
2017-12-10 22:50:49 -04:00
_copter . parachute_release ( ) ;
2014-04-03 05:53:27 -03:00
break ;
default :
2014-09-17 08:31:30 -03:00
// do nothing
break ;
}
}
# endif
2016-10-29 08:01:29 -03:00
# if GRIPPER_ENABLED == ENABLED
// do_gripper - control gripper
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : do_gripper ( const AP_Mission : : Mission_Command & cmd )
2014-09-17 08:31:30 -03:00
{
// Note: we ignore the gripper num parameter because we only support one gripper
switch ( cmd . content . gripper . action ) {
case GRIPPER_ACTION_RELEASE :
2016-10-29 08:01:29 -03:00
g2 . gripper . release ( ) ;
Log_Write_Event ( DATA_GRIPPER_RELEASE ) ;
2014-09-17 08:31:30 -03:00
break ;
case GRIPPER_ACTION_GRAB :
2016-10-29 08:01:29 -03:00
g2 . gripper . grab ( ) ;
Log_Write_Event ( DATA_GRIPPER_GRAB ) ;
2014-09-17 08:31:30 -03:00
break ;
default :
2014-04-03 05:53:27 -03:00
// do nothing
break ;
}
}
# endif
2014-10-13 05:41:48 -03:00
# if NAV_GUIDED == ENABLED
// do_guided_limits - pass guided limits to guided controller
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : do_guided_limits ( const AP_Mission : : Mission_Command & cmd )
2014-10-13 05:41:48 -03:00
{
2017-12-10 22:50:49 -04:00
_copter . mode_guided . limit_set (
cmd . p1 * 1000 , // convert seconds to ms
cmd . content . guided_limits . alt_min * 100.0f , // convert meters to cm
cmd . content . guided_limits . alt_max * 100.0f , // convert meters to cm
cmd . content . guided_limits . horiz_max * 100.0f ) ; // convert meters to cm
2014-10-13 05:41:48 -03:00
}
# endif
2017-10-04 23:21:23 -03:00
// control winch based on mission command
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : do_winch ( const AP_Mission : : Mission_Command & cmd )
2017-10-04 23:21:23 -03:00
{
// Note: we ignore the gripper num parameter because we only support one gripper
switch ( cmd . content . winch . action ) {
case WINCH_RELAXED :
g2 . winch . relax ( ) ;
Log_Write_Event ( DATA_WINCH_RELAXED ) ;
break ;
case WINCH_RELATIVE_LENGTH_CONTROL :
g2 . winch . release_length ( cmd . content . winch . release_length , cmd . content . winch . release_rate ) ;
Log_Write_Event ( DATA_WINCH_LENGTH_CONTROL ) ;
break ;
case WINCH_RATE_CONTROL :
g2 . winch . set_desired_rate ( cmd . content . winch . release_rate ) ;
Log_Write_Event ( DATA_WINCH_RATE_CONTROL ) ;
break ;
default :
// do nothing
break ;
}
}
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
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : verify_takeoff ( )
2011-03-02 22:32:50 -04:00
{
2013-04-08 23:58:01 -03:00
// have we reached our target altitude?
2017-01-09 03:31:26 -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
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : verify_land ( )
2012-01-29 02:00:05 -04:00
{
2013-05-10 10:37:15 -03:00
bool retval = false ;
2016-08-08 01:27:39 -03:00
switch ( land_state ) {
case LandStateType_FlyToLocation :
2013-05-10 10:37:15 -03:00
// check if we've reached the location
2017-01-09 03:31:26 -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
2017-01-09 03:31:26 -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
2017-12-10 22:50:49 -04:00
land_start ( dest ) ;
2013-05-10 10:37:15 -03:00
// advance to next state
2016-08-08 01:27:39 -03:00
land_state = LandStateType_Descending ;
2013-05-10 10:37:15 -03:00
}
break ;
2016-08-08 01:27:39 -03:00
case LandStateType_Descending :
2013-05-10 10:37:15 -03:00
// 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
}
2016-11-17 01:19:22 -04:00
# define NAV_PAYLOAD_PLACE_DEBUGGING 0
# if NAV_PAYLOAD_PLACE_DEBUGGING
# include <stdio.h>
# define debug(fmt, args ...) do {::fprintf(stderr,"%s:%d: " fmt "\n", __FUNCTION__, __LINE__, ## args); } while(0)
# else
# define debug(fmt, args ...)
# endif
// verify_payload_place - returns true if placing has been completed
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : verify_payload_place ( )
2016-11-17 01:19:22 -04:00
{
const uint16_t hover_throttle_calibrate_time = 2000 ; // milliseconds
const uint16_t descend_throttle_calibrate_time = 2000 ; // milliseconds
2016-12-20 00:00:44 -04:00
const float hover_throttle_placed_fraction = 0.7 ; // i.e. if throttle is less than 70% of hover we have placed
2016-11-17 01:19:22 -04:00
const float descent_throttle_placed_fraction = 0.9 ; // i.e. if throttle is less than 90% of descent throttle we have placed
const uint16_t placed_time = 500 ; // how long we have to be below a throttle threshold before considering placed
2017-01-09 03:31:26 -04:00
const float current_throttle_level = motors - > get_throttle ( ) ;
2016-11-17 01:19:22 -04:00
const uint32_t now = AP_HAL : : millis ( ) ;
2016-12-01 22:25:08 -04:00
// if we discover we've landed then immediately release the load:
if ( ap . land_complete ) {
switch ( nav_payload_place . state ) {
case PayloadPlaceStateType_FlyToLocation :
case PayloadPlaceStateType_Calibrating_Hover_Start :
case PayloadPlaceStateType_Calibrating_Hover :
case PayloadPlaceStateType_Descending_Start :
case PayloadPlaceStateType_Descending :
2017-07-08 21:56:49 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " NAV_PLACE: landed " ) ;
2016-12-01 22:25:08 -04:00
nav_payload_place . state = PayloadPlaceStateType_Releasing_Start ;
break ;
case PayloadPlaceStateType_Releasing_Start :
case PayloadPlaceStateType_Releasing :
case PayloadPlaceStateType_Released :
case PayloadPlaceStateType_Ascending_Start :
case PayloadPlaceStateType_Ascending :
case PayloadPlaceStateType_Done :
break ;
}
}
2016-11-17 01:19:22 -04:00
switch ( nav_payload_place . state ) {
case PayloadPlaceStateType_FlyToLocation :
2017-01-09 03:31:26 -04:00
if ( ! wp_nav - > reached_wp_destination ( ) ) {
2016-11-17 01:19:22 -04:00
return false ;
}
// we're there; set loiter target
nav_payload_place . state = PayloadPlaceStateType_Calibrating_Hover_Start ;
2017-08-22 14:28:10 -03:00
FALLTHROUGH ;
2016-11-17 01:19:22 -04:00
case PayloadPlaceStateType_Calibrating_Hover_Start :
// hover for 1 second to get an idea of what our hover
// throttle looks like
debug ( " Calibrate start " ) ;
nav_payload_place . hover_start_timestamp = now ;
nav_payload_place . state = PayloadPlaceStateType_Calibrating_Hover ;
2017-08-22 14:28:10 -03:00
FALLTHROUGH ;
2016-11-17 01:19:22 -04:00
case PayloadPlaceStateType_Calibrating_Hover : {
if ( now - nav_payload_place . hover_start_timestamp < hover_throttle_calibrate_time ) {
// still calibrating...
debug ( " Calibrate Timer: %d " , now - nav_payload_place . hover_start_timestamp ) ;
return false ;
}
// we have a valid calibration. Hopefully.
nav_payload_place . hover_throttle_level = current_throttle_level ;
2017-01-09 03:31:26 -04:00
const float hover_throttle_delta = fabsf ( nav_payload_place . hover_throttle_level - motors - > get_throttle_hover ( ) ) ;
2017-07-08 21:56:49 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " hover throttle delta: %f " , static_cast < double > ( hover_throttle_delta ) ) ;
2016-11-17 01:19:22 -04:00
nav_payload_place . state = PayloadPlaceStateType_Descending_Start ;
2016-12-12 08:35:33 -04:00
}
2017-08-22 14:28:10 -03:00
FALLTHROUGH ;
2016-11-17 01:19:22 -04:00
case PayloadPlaceStateType_Descending_Start :
nav_payload_place . descend_start_timestamp = now ;
nav_payload_place . descend_start_altitude = inertial_nav . get_altitude ( ) ;
nav_payload_place . descend_throttle_level = 0 ;
nav_payload_place . state = PayloadPlaceStateType_Descending ;
2017-08-22 14:28:10 -03:00
FALLTHROUGH ;
2016-11-17 01:19:22 -04:00
case PayloadPlaceStateType_Descending :
// make sure we don't descend too far:
debug ( " descended: %f cm (%f cm max) " , ( nav_payload_place . descend_start_altitude - inertial_nav . get_altitude ( ) ) , nav_payload_place . descend_max ) ;
if ( ! is_zero ( nav_payload_place . descend_max ) & &
nav_payload_place . descend_start_altitude - inertial_nav . get_altitude ( ) > nav_payload_place . descend_max ) {
nav_payload_place . state = PayloadPlaceStateType_Ascending ;
2017-07-08 21:56:49 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_WARNING , " Reached maximum descent " ) ;
2016-11-17 01:19:22 -04:00
return false ; // we'll do any cleanups required next time through the loop
}
// see if we've been descending long enough to calibrate a descend-throttle-level:
2016-12-06 07:01:02 -04:00
if ( is_zero ( nav_payload_place . descend_throttle_level ) & &
2016-11-17 01:19:22 -04:00
now - nav_payload_place . descend_start_timestamp > descend_throttle_calibrate_time ) {
nav_payload_place . descend_throttle_level = current_throttle_level ;
}
// watch the throttle to determine whether the load has been placed
// debug("hover ratio: %f descend ratio: %f\n", current_throttle_level/nav_payload_place.hover_throttle_level, ((nav_payload_place.descend_throttle_level == 0) ? -1.0f : current_throttle_level/nav_payload_place.descend_throttle_level));
if ( current_throttle_level / nav_payload_place . hover_throttle_level > hover_throttle_placed_fraction & &
2016-12-06 07:01:02 -04:00
( is_zero ( nav_payload_place . descend_throttle_level ) | |
2016-11-17 01:19:22 -04:00
current_throttle_level / nav_payload_place . descend_throttle_level > descent_throttle_placed_fraction ) ) {
// throttle is above both threshold ratios (or above hover threshold ration and descent threshold ratio not yet valid)
nav_payload_place . place_start_timestamp = 0 ;
return false ;
}
if ( nav_payload_place . place_start_timestamp = = 0 ) {
// we've only just now hit the correct throttle level
nav_payload_place . place_start_timestamp = now ;
return false ;
} else if ( now - nav_payload_place . place_start_timestamp < placed_time ) {
// keep going down....
debug ( " Place Timer: %d " , now - nav_payload_place . place_start_timestamp ) ;
return false ;
}
nav_payload_place . state = PayloadPlaceStateType_Releasing_Start ;
2017-08-22 14:28:10 -03:00
FALLTHROUGH ;
2016-11-17 01:19:22 -04:00
case PayloadPlaceStateType_Releasing_Start :
2017-01-01 21:03:47 -04:00
# if GRIPPER_ENABLED == ENABLED
2016-11-17 01:19:22 -04:00
if ( g2 . gripper . valid ( ) ) {
2017-07-08 21:56:49 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Releasing the gripper " ) ;
2016-11-17 01:19:22 -04:00
g2 . gripper . release ( ) ;
} else {
2017-07-08 21:56:49 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Gripper not valid " ) ;
2016-12-19 23:26:59 -04:00
nav_payload_place . state = PayloadPlaceStateType_Ascending_Start ;
2016-12-20 00:00:44 -04:00
break ;
2016-11-17 01:19:22 -04:00
}
2017-01-01 21:03:47 -04:00
# else
2017-07-08 21:56:49 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Gripper code disabled " ) ;
2017-01-01 21:03:47 -04:00
# endif
2016-11-17 01:19:22 -04:00
nav_payload_place . state = PayloadPlaceStateType_Releasing ;
2017-08-22 14:28:10 -03:00
FALLTHROUGH ;
2016-11-17 01:19:22 -04:00
case PayloadPlaceStateType_Releasing :
2017-01-01 21:03:47 -04:00
# if GRIPPER_ENABLED == ENABLED
2016-11-17 01:19:22 -04:00
if ( g2 . gripper . valid ( ) & & ! g2 . gripper . released ( ) ) {
return false ;
}
2017-01-01 21:03:47 -04:00
# endif
2016-11-17 01:19:22 -04:00
nav_payload_place . state = PayloadPlaceStateType_Released ;
2017-08-22 14:28:10 -03:00
FALLTHROUGH ;
2016-11-17 01:19:22 -04:00
case PayloadPlaceStateType_Released : {
nav_payload_place . state = PayloadPlaceStateType_Ascending_Start ;
2016-12-12 08:35:33 -04:00
}
2017-08-22 14:28:10 -03:00
FALLTHROUGH ;
2016-11-17 01:19:22 -04:00
case PayloadPlaceStateType_Ascending_Start : {
Location_Class target_loc = inertial_nav . get_position ( ) ;
target_loc . alt = nav_payload_place . descend_start_altitude ;
2017-12-10 22:50:49 -04:00
wp_start ( target_loc ) ;
2016-11-17 01:19:22 -04:00
nav_payload_place . state = PayloadPlaceStateType_Ascending ;
2016-12-12 08:35:33 -04:00
}
2017-08-22 14:28:10 -03:00
FALLTHROUGH ;
2016-11-17 01:19:22 -04:00
case PayloadPlaceStateType_Ascending :
2017-01-09 03:31:26 -04:00
if ( ! wp_nav - > reached_wp_destination ( ) ) {
2016-11-17 01:19:22 -04:00
return false ;
}
nav_payload_place . state = PayloadPlaceStateType_Done ;
2017-08-22 14:28:10 -03:00
FALLTHROUGH ;
2016-11-17 01:19:22 -04:00
case PayloadPlaceStateType_Done :
return true ;
default :
// this should never happen
// TO-DO: log an error
return true ;
}
// should never get here
return true ;
}
# undef debug
2012-12-09 02:50:50 -04:00
// verify_nav_wp - check if we have reached the next way point
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : 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
2017-01-09 03:31:26 -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
}
2014-10-23 15:47:00 -03:00
// play a tone
AP_Notify : : events . waypoint_complete = 1 ;
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 ) {
2017-07-08 21:56:49 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " 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
}
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : verify_loiter_unlimited ( )
2012-07-12 12:52:36 -03:00
{
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
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : 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
2017-01-09 03:31:26 -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
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : 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
2017-12-10 22:50:49 -04:00
if ( mode ( ) = = Auto_CircleMoveToEdge ) {
2017-01-09 03:31:26 -04:00
if ( wp_nav - > reached_wp_destination ( ) ) {
2017-12-10 22:50:49 -04:00
const Vector3f curr_pos = _copter . inertial_nav . get_position ( ) ;
Vector3f circle_center = _copter . pv_location_to_vector ( cmd . content . location ) ;
2014-04-16 04:23:24 -03:00
// set target altitude if not provided
2015-05-04 23:34:21 -03:00
if ( is_zero ( circle_center . z ) ) {
2014-04-16 04:23:24 -03:00
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
2017-12-10 22:50:49 -04:00
circle_start ( ) ;
2014-04-16 04:23:24 -03:00
}
return false ;
}
// check if we have completed circling
2017-01-09 03:31:26 -04:00
return fabsf ( circle_nav - > get_angle_total ( ) / M_2PI ) > = LOWBYTE ( cmd . p1 ) ;
2011-09-21 17:19:36 -03:00
}
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
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : verify_RTL ( )
2011-03-02 22:32:50 -04:00
{
2017-12-10 22:50:49 -04:00
return ( _copter . mode_rtl . state_complete ( ) & & ( _copter . mode_rtl . state ( ) = = RTL_FinalDescent | | _copter . mode_rtl . state ( ) = = RTL_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
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : verify_spline_wp ( const AP_Mission : : Mission_Command & cmd )
2014-03-27 04:05:49 -03:00
{
// check if we have reached the waypoint
2017-01-09 03:31:26 -04:00
if ( ! wp_nav - > reached_wp_destination ( ) ) {
2014-03-27 04:05:49 -03:00
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 ) {
2017-07-08 21:56:49 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Reached command #%i " , cmd . index ) ;
2014-03-27 04:05:49 -03:00
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
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : verify_nav_guided_enable ( const AP_Mission : : Mission_Command & cmd )
2014-05-23 02:29:08 -03:00
{
2014-10-13 05:41:48 -03:00
// if disabling guided mode then immediately return true so we move to next command
if ( cmd . p1 = = 0 ) {
2014-05-23 02:29:08 -03:00
return true ;
}
2014-10-13 05:41:48 -03:00
// check time and position limits
2017-12-10 22:50:49 -04:00
return _copter . mode_guided . limit_check ( ) ;
2014-05-23 02:29:08 -03:00
}
# endif // NAV_GUIDED
2016-04-12 03:10:09 -03:00
// verify_nav_delay - check if we have waited long enough
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : verify_nav_delay ( const AP_Mission : : Mission_Command & cmd )
2016-04-12 03:10:09 -03:00
{
2016-05-04 09:19:26 -03:00
if ( millis ( ) - nav_delay_time_start > ( uint32_t ) MAX ( nav_delay_time_max , 0 ) ) {
nav_delay_time_max = 0 ;
2016-04-12 03:10:09 -03:00
return true ;
}
return false ;
}
2014-05-23 02:29:08 -03:00
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
/********************************************************************************/
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : 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
}
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : 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
}
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : 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 ,
2014-09-26 00:20:40 -03:00
cmd . content . yaw . direction ,
2017-07-11 02:04:58 -03:00
cmd . content . yaw . relative_angle > 0 ) ;
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
/********************************************************************************/
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : verify_wait_delay ( )
2011-03-02 22:32:50 -04:00
{
2015-11-27 13:11:58 -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
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : verify_within_distance ( )
2011-03-02 22:32:50 -04:00
{
2017-12-10 22:50:49 -04:00
if ( wp_distance ( ) < ( 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
}
2012-12-08 01:23:32 -04:00
// verify_yaw - return true if we have reached the desired heading
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : 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
2017-12-10 22:50:49 -04:00
if ( labs ( wrap_180_cd ( ahrs . yaw_sensor - _copter . 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
2017-12-10 22:50:49 -04:00
bool Copter : : ModeAuto : : do_guided ( const AP_Mission : : Mission_Command & cmd )
2013-06-01 23:25:35 -03:00
{
2014-05-14 16:11:28 -03:00
// only process guided waypoint if we are in guided mode
2017-12-10 22:50:49 -04:00
if ( _copter . control_mode ! = GUIDED & & ! ( _copter . control_mode = = 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 :
2016-03-11 04:16:26 -04:00
{
2014-05-26 04:00:25 -03:00
// set wp_nav's destination
2016-03-11 04:16:26 -04:00
Location_Class dest ( cmd . content . location ) ;
2017-12-10 22:50:49 -04:00
return _copter . mode_guided . set_destination ( dest ) ;
2016-03-11 04:16:26 -04:00
}
2014-06-02 06:06:11 -03:00
2014-05-26 04:00:25 -03:00
case MAV_CMD_CONDITION_YAW :
do_yaw ( cmd ) ;
return true ;
default :
// reject unrecognised command
return false ;
}
2014-02-27 22:19:05 -04:00
return true ;
2013-06-01 23:25:35 -03:00
}
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : 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 ) {
2017-01-09 03:31:26 -04: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
}
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : do_set_home ( const AP_Mission : : Mission_Command & cmd )
2011-03-02 22:32:50 -04:00
{
2015-02-09 07:25:45 -04:00
if ( cmd . p1 = = 1 | | ( cmd . content . location . lat = = 0 & & cmd . content . location . lng = = 0 & & cmd . content . location . alt = = 0 ) ) {
2017-12-10 22:50:49 -04:00
_copter . set_home_to_current_location ( false ) ;
2012-08-21 23:19:50 -03:00
} else {
2017-12-10 22:50:49 -04:00
_copter . set_home ( cmd . content . location , false ) ;
2012-08-21 23:19:50 -03:00
}
2011-03-02 22:32:50 -04:00
}
2016-08-22 06:03:45 -03:00
// do_roi - starts actions required by MAV_CMD_DO_SET_ROI
2013-07-13 11:27:51 -03:00
// 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
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : do_roi ( const AP_Mission : : Mission_Command & cmd )
2012-07-24 23:02:54 -03:00
{
2017-12-10 22:50:49 -04:00
_copter . set_auto_yaw_roi ( cmd . content . location ) ;
2012-07-24 23:02:54 -03:00
}
2012-12-06 10:48:30 -04:00
2017-01-02 19:08:46 -04:00
# if CAMERA == ENABLED
2015-03-29 15:49:22 -03:00
// do_digicam_configure Send Digicam Configure message with the camera library
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : do_digicam_configure ( const AP_Mission : : Mission_Command & cmd )
2015-03-29 15:49:22 -03:00
{
2017-12-10 22:50:49 -04:00
_copter . camera . configure (
cmd . content . digicam_configure . shooting_mode ,
cmd . content . digicam_configure . shutter_speed ,
cmd . content . digicam_configure . aperture ,
cmd . content . digicam_configure . ISO ,
cmd . content . digicam_configure . exposure_type ,
cmd . content . digicam_configure . cmd_id ,
cmd . content . digicam_configure . engine_cutoff_time ) ;
2015-03-29 15:49:22 -03:00
}
// do_digicam_control Send Digicam Control message with the camera library
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : do_digicam_control ( const AP_Mission : : Mission_Command & cmd )
2015-03-29 15:49:22 -03:00
{
2017-12-10 22:50:49 -04:00
_copter . camera . control ( cmd . content . digicam_control . session ,
cmd . content . digicam_control . zoom_pos ,
cmd . content . digicam_control . zoom_step ,
cmd . content . digicam_control . focus_lock ,
cmd . content . digicam_control . shooting_cmd ,
cmd . content . digicam_control . cmd_id ) ;
2012-12-06 10:48:30 -04:00
}
2015-03-21 08:59:50 -03:00
2017-01-02 19:08:46 -04:00
# endif
2015-03-21 08:59:50 -03:00
// point the camera to a specified angle
2017-12-10 22:50:49 -04:00
void Copter : : ModeAuto : : do_mount_control ( const AP_Mission : : Mission_Command & cmd )
2015-03-21 08:59:50 -03:00
{
# if MOUNT == ENABLED
2017-12-22 16:29:23 -04:00
if ( ! _copter . camera_mount . has_pan_control ( ) ) {
_copter . set_auto_yaw_look_at_heading ( cmd . content . mount_control . yaw , 0.0f , 0 , 0 ) ;
}
2017-12-10 22:50:49 -04:00
_copter . camera_mount . set_angle_targets ( cmd . content . mount_control . roll , cmd . content . mount_control . pitch , cmd . content . mount_control . yaw ) ;
2015-03-21 08:59:50 -03:00
# endif
2016-01-19 01:26:14 -04:00
}