2015-05-29 23:12:49 -03:00
# include "Copter.h"
2014-02-03 04:57:31 -04:00
/*
2016-07-25 15:45:29 -03:00
* High level calls to set and update flight modes logic for individual
* flight modes is in control_acro . cpp , control_stabilize . cpp , etc
2014-02-03 04:57:31 -04:00
*/
2018-02-07 22:21:09 -04:00
/*
constructor for Mode object
*/
2019-05-09 23:18:49 -03:00
Mode : : Mode ( void ) :
2018-02-07 22:21:09 -04:00
g ( copter . g ) ,
g2 ( copter . g2 ) ,
wp_nav ( copter . wp_nav ) ,
2018-03-27 23:13:37 -03:00
loiter_nav ( copter . loiter_nav ) ,
2018-02-07 22:21:09 -04:00
pos_control ( copter . pos_control ) ,
inertial_nav ( copter . inertial_nav ) ,
ahrs ( copter . ahrs ) ,
attitude_control ( copter . attitude_control ) ,
motors ( copter . motors ) ,
channel_roll ( copter . channel_roll ) ,
channel_pitch ( copter . channel_pitch ) ,
channel_throttle ( copter . channel_throttle ) ,
channel_yaw ( copter . channel_yaw ) ,
2019-05-09 23:18:49 -03:00
G_Dt ( copter . G_Dt )
2018-02-07 22:21:09 -04:00
{ } ;
2019-05-09 23:18:49 -03:00
float Mode : : auto_takeoff_no_nav_alt_cm = 0 ;
2018-06-26 00:24:54 -03:00
2016-03-23 00:27:05 -03:00
// return the static controller object corresponding to supplied mode
2019-09-07 20:33:56 -03:00
Mode * Copter : : mode_from_mode_num ( const Mode : : Number mode )
2014-02-03 04:57:31 -04:00
{
2019-05-09 23:18:49 -03:00
Mode * ret = nullptr ;
2016-03-21 01:33:42 -03:00
2016-10-10 00:11:14 -03:00
switch ( mode ) {
2018-03-14 17:14:49 -03:00
# if MODE_ACRO_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : ACRO :
2017-12-11 01:43:27 -04:00
ret = & mode_acro ;
2014-02-03 04:57:31 -04:00
break ;
2018-03-14 17:14:49 -03:00
# endif
2014-02-03 04:57:31 -04:00
2019-09-07 20:33:56 -03:00
case Mode : : Number : : STABILIZE :
2017-12-11 01:43:27 -04:00
ret = & mode_stabilize ;
2014-02-03 04:57:31 -04:00
break ;
2019-09-07 20:33:56 -03:00
case Mode : : Number : : ALT_HOLD :
2017-12-11 01:43:27 -04:00
ret = & mode_althold ;
2014-02-03 04:57:31 -04:00
break ;
2018-02-21 22:06:07 -04:00
# if MODE_AUTO_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : AUTO :
2017-12-11 01:43:27 -04:00
ret = & mode_auto ;
2014-02-03 04:57:31 -04:00
break ;
2018-02-21 22:06:07 -04:00
# endif
2014-02-03 04:57:31 -04:00
2018-02-23 05:51:17 -04:00
# if MODE_CIRCLE_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : CIRCLE :
2017-12-11 01:43:27 -04:00
ret = & mode_circle ;
2014-02-03 04:57:31 -04:00
break ;
2018-02-23 05:51:17 -04:00
# endif
2014-02-03 04:57:31 -04:00
2018-02-22 01:23:35 -04:00
# if MODE_LOITER_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : LOITER :
2017-12-11 01:43:27 -04:00
ret = & mode_loiter ;
2014-02-03 04:57:31 -04:00
break ;
2018-02-22 01:23:35 -04:00
# endif
2014-02-03 04:57:31 -04:00
2018-02-23 00:09:07 -04:00
# if MODE_GUIDED_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : GUIDED :
2017-12-11 01:43:27 -04:00
ret = & mode_guided ;
2014-02-03 04:57:31 -04:00
break ;
2018-02-23 00:09:07 -04:00
# endif
2014-02-03 04:57:31 -04:00
2019-09-07 20:33:56 -03:00
case Mode : : Number : : LAND :
2017-12-11 01:43:27 -04:00
ret = & mode_land ;
2014-02-03 04:57:31 -04:00
break ;
2018-02-23 01:33:49 -04:00
# if MODE_RTL_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : RTL :
2017-12-11 01:43:27 -04:00
ret = & mode_rtl ;
2014-02-03 04:57:31 -04:00
break ;
2018-02-23 01:33:49 -04:00
# endif
2014-02-03 04:57:31 -04:00
2018-02-22 23:55:51 -04:00
# if MODE_DRIFT_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : DRIFT :
2017-12-11 01:43:27 -04:00
ret = & mode_drift ;
2014-02-03 04:57:31 -04:00
break ;
2018-02-22 23:55:51 -04:00
# endif
2014-02-03 04:57:31 -04:00
2018-02-22 23:48:02 -04:00
# if MODE_SPORT_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : SPORT :
2017-12-11 01:43:27 -04:00
ret = & mode_sport ;
2014-02-03 04:57:31 -04:00
break ;
2018-02-22 23:48:02 -04:00
# endif
2014-02-03 04:57:31 -04:00
2018-11-25 19:47:55 -04:00
# if MODE_FLIP_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : FLIP :
2017-12-11 01:43:27 -04:00
ret = & mode_flip ;
2014-02-03 04:57:31 -04:00
break ;
2018-11-25 19:47:55 -04:00
# endif
2014-02-03 04:57:31 -04:00
# if AUTOTUNE_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : AUTOTUNE :
2017-12-11 01:43:27 -04:00
ret = & mode_autotune ;
2014-02-03 04:57:31 -04:00
break ;
# endif
2018-02-21 23:45:02 -04:00
# if MODE_POSHOLD_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : POSHOLD :
2017-12-11 01:43:27 -04:00
ret = & mode_poshold ;
2014-04-04 11:14:30 -03:00
break ;
2018-02-21 23:45:02 -04:00
# endif
2014-04-04 11:14:30 -03:00
2018-02-23 01:40:29 -04:00
# if MODE_BRAKE_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : BRAKE :
2017-12-11 01:43:27 -04:00
ret = & mode_brake ;
2015-04-22 18:10:30 -03:00
break ;
2018-02-23 01:40:29 -04:00
# endif
2015-04-22 18:10:30 -03:00
2018-03-14 17:16:16 -03:00
# if MODE_THROW_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : THROW :
2017-12-11 01:43:27 -04:00
ret = & mode_throw ;
2015-12-18 05:46:56 -04:00
break ;
2018-03-14 17:16:16 -03:00
# endif
2015-12-18 05:46:56 -04:00
2018-02-16 10:21:55 -04:00
# if ADSB_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : AVOID_ADSB :
2017-12-11 01:43:27 -04:00
ret = & mode_avoid_adsb ;
2016-07-21 09:44:09 -03:00
break ;
2018-02-16 10:21:55 -04:00
# endif
2016-07-21 09:44:09 -03:00
2018-02-23 03:54:34 -04:00
# if MODE_GUIDED_NOGPS_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : GUIDED_NOGPS :
2017-12-11 01:43:27 -04:00
ret = & mode_guided_nogps ;
2016-08-01 05:44:12 -03:00
break ;
2018-02-23 03:54:34 -04:00
# endif
2016-08-01 05:44:12 -03:00
2018-02-21 23:58:28 -04:00
# if MODE_SMARTRTL_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : SMART_RTL :
2017-12-11 01:43:27 -04:00
ret = & mode_smartrtl ;
2017-07-26 14:14:40 -03:00
break ;
2018-02-21 23:58:28 -04:00
# endif
2017-07-26 14:14:40 -03:00
2018-01-18 02:12:29 -04:00
# if OPTFLOW == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : FLOWHOLD :
2019-05-09 23:18:49 -03:00
ret = ( Mode * ) g2 . mode_flowhold_ptr ;
2018-01-18 02:12:29 -04:00
break ;
# endif
2018-02-06 02:16:09 -04:00
# if MODE_FOLLOW_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : FOLLOW :
2018-02-05 22:44:05 -04:00
ret = & mode_follow ;
break ;
2018-02-06 02:16:09 -04:00
# endif
2018-02-05 22:44:05 -04:00
2018-09-07 01:23:33 -03:00
# if MODE_ZIGZAG_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
case Mode : : Number : : ZIGZAG :
2018-09-07 01:23:33 -03:00
ret = & mode_zigzag ;
break ;
# endif
2019-07-29 04:55:40 -03:00
# if MODE_SYSTEMID_ENABLED == ENABLED
case Mode : : Number : : SYSTEMID :
ret = ( Mode * ) g2 . mode_systemid_ptr ;
break ;
# endif
2019-11-28 16:21:07 -04:00
# if MODE_AUTOROTATE_ENABLED == ENABLED
case Mode : : Number : : AUTOROTATE :
ret = & mode_autorotate ;
break ;
# endif
2014-02-03 04:57:31 -04:00
default :
break ;
}
2016-03-23 00:27:05 -03:00
return ret ;
}
// set_mode - change flight mode and perform any necessary initialisation
// optional force parameter used to force the flight mode change (used only first time mode is set)
// returns true if mode was successfully set
// ACRO, STABILIZE, ALTHOLD, LAND, DRIFT and SPORT can always be set successfully but the return state of other flight modes should be checked and the caller should deal with failures appropriately
2019-10-17 00:49:22 -03:00
bool Copter : : set_mode ( Mode : : Number mode , ModeReason reason )
2016-03-23 00:27:05 -03:00
{
// return immediately if we are already in the desired mode
if ( mode = = control_mode ) {
control_mode_reason = reason ;
return true ;
}
2019-09-07 20:33:56 -03:00
Mode * new_flightmode = mode_from_mode_num ( ( Mode : : Number ) mode ) ;
2016-03-23 00:27:05 -03:00
if ( new_flightmode = = nullptr ) {
gcs ( ) . send_text ( MAV_SEVERITY_WARNING , " No such mode " ) ;
2019-03-24 22:31:46 -03:00
AP : : logger ( ) . Write_Error ( LogErrorSubsystem : : FLIGHT_MODE , LogErrorCode ( mode ) ) ;
2016-03-23 00:27:05 -03:00
return false ;
}
bool ignore_checks = ! motors - > armed ( ) ; // allow switching to any mode if disarmed. We rely on the arming check to perform
2017-09-19 22:18:49 -03:00
# if FRAME_CONFIG == HELI_FRAME
2016-03-23 00:27:05 -03:00
// do not allow helis to enter a non-manual throttle mode if the
// rotor runup is not complete
2019-11-28 16:21:07 -04:00
if ( ! ignore_checks & &
! new_flightmode - > has_manual_throttle ( ) & &
( motors - > get_spool_state ( ) = = AP_Motors : : SpoolState : : SPOOLING_UP | | motors - > get_spool_state ( ) = = AP_Motors : : SpoolState : : SPOOLING_DOWN ) ) {
# if MODE_AUTOROTATE_ENABLED == ENABLED
//if the mode being exited is the autorotation mode allow mode change despite rotor not being at
//full speed. This will reduce altitude loss on bail-outs back to non-manual throttle modes
bool in_autorotation_check = ( flightmode ! = & mode_autorotate | | new_flightmode ! = & mode_autorotate ) ;
# else
bool in_autorotation_check = false ;
# endif
if ( ! in_autorotation_check ) {
gcs ( ) . send_text ( MAV_SEVERITY_WARNING , " Flight mode change failed " ) ;
AP : : logger ( ) . Write_Error ( LogErrorSubsystem : : FLIGHT_MODE , LogErrorCode ( mode ) ) ;
return false ;
}
2016-03-23 00:27:05 -03:00
}
2019-11-28 16:21:07 -04:00
# if MODE_AUTOROTATE_ENABLED == ENABLED
// If changing to autorotate flight mode from a non-manual throttle mode, store the previous flight mode
// to exit back to it when interlock is re-engaged
prev_control_mode = control_mode ;
# endif
2017-09-19 22:18:49 -03:00
# endif
2016-01-26 20:08:45 -04:00
2019-03-06 01:31:05 -04:00
# if FRAME_CONFIG != HELI_FRAME
// ensure vehicle doesn't leap off the ground if a user switches
// into a manual throttle mode from a non-manual-throttle mode
// (e.g. user arms in guided, raises throttle to 1300 (not enough to
// trigger auto takeoff), then switches into manual):
2019-08-21 00:24:21 -03:00
bool user_throttle = new_flightmode - > has_manual_throttle ( ) ;
# if MODE_DRIFT_ENABLED == ENABLED
if ( new_flightmode = = & mode_drift ) {
user_throttle = true ;
}
# endif
2019-03-06 01:31:05 -04:00
if ( ! ignore_checks & &
ap . land_complete & &
2019-08-21 00:24:21 -03:00
user_throttle & &
2019-03-06 01:31:05 -04:00
! copter . flightmode - > has_manual_throttle ( ) & &
new_flightmode - > get_pilot_desired_throttle ( ) > copter . get_non_takeoff_throttle ( ) ) {
gcs ( ) . send_text ( MAV_SEVERITY_WARNING , " Mode change failed: throttle too high " ) ;
2019-03-26 23:19:07 -03:00
AP : : logger ( ) . Write_Error ( LogErrorSubsystem : : FLIGHT_MODE , LogErrorCode ( mode ) ) ;
2019-03-06 01:31:05 -04:00
return false ;
}
# endif
2019-02-27 23:56:36 -04:00
if ( ! ignore_checks & &
new_flightmode - > requires_GPS ( ) & &
! copter . position_ok ( ) ) {
gcs ( ) . send_text ( MAV_SEVERITY_WARNING , " Mode change failed: %s requires position " , new_flightmode - > name ( ) ) ;
2019-03-24 22:31:46 -03:00
AP : : logger ( ) . Write_Error ( LogErrorSubsystem : : FLIGHT_MODE , LogErrorCode ( mode ) ) ;
2019-02-27 23:56:36 -04:00
return false ;
}
2017-12-12 19:02:51 -04:00
if ( ! new_flightmode - > init ( ignore_checks ) ) {
gcs ( ) . send_text ( MAV_SEVERITY_WARNING , " Flight mode change failed " ) ;
2019-03-24 22:31:46 -03:00
AP : : logger ( ) . Write_Error ( LogErrorSubsystem : : FLIGHT_MODE , LogErrorCode ( mode ) ) ;
2017-12-12 19:02:51 -04:00
return false ;
}
2016-03-23 00:27:05 -03:00
// perform any cleanup required by previous flight mode
2017-12-05 19:52:54 -04:00
exit_mode ( flightmode , new_flightmode ) ;
2016-03-23 00:27:05 -03:00
// update flight mode
flightmode = new_flightmode ;
control_mode = mode ;
control_mode_reason = reason ;
2019-09-07 20:33:56 -03:00
logger . Write_Mode ( ( uint8_t ) control_mode , reason ) ;
2019-06-26 15:21:38 -03:00
gcs ( ) . send_message ( MSG_HEARTBEAT ) ;
2014-04-26 23:09:15 -03:00
2018-02-16 10:21:55 -04:00
# if ADSB_ENABLED == ENABLED
2019-09-07 20:33:56 -03:00
adsb . set_is_auto_mode ( ( mode = = Mode : : Number : : AUTO ) | | ( mode = = Mode : : Number : : RTL ) | | ( mode = = Mode : : Number : : GUIDED ) ) ;
2018-02-16 10:21:55 -04:00
# endif
2016-07-21 02:43:46 -03:00
2014-04-26 23:09:15 -03:00
# if AC_FENCE == ENABLED
2017-11-10 01:13:52 -04:00
// pilot requested flight mode change during a fence breach indicates pilot is attempting to manually recover
// this flight mode change could be automatic (i.e. fence, battery, GPS or GCS failsafe)
// but it should be harmless to disable the fence temporarily in these situations as well
2016-03-23 00:27:05 -03:00
fence . manual_recovery_start ( ) ;
2014-04-26 23:09:15 -03:00
# endif
2017-11-10 01:13:52 -04:00
2017-10-24 07:44:57 -03:00
# if CAMERA == ENABLED
2019-09-07 20:33:56 -03:00
camera . set_is_auto_mode ( control_mode = = Mode : : Number : : AUTO ) ;
2017-10-24 07:44:57 -03:00
# endif
2014-02-03 04:57:31 -04:00
2015-03-06 02:33:25 -04:00
// update notify object
2016-03-23 00:27:05 -03:00
notify_flight_mode ( ) ;
2015-03-06 02:33:25 -04:00
2016-03-23 00:27:05 -03:00
// return success
return true ;
2014-02-03 04:57:31 -04:00
}
2019-10-17 00:49:22 -03:00
bool Copter : : set_mode ( const uint8_t new_mode , const ModeReason reason )
{
static_assert ( sizeof ( Mode : : Number ) = = sizeof ( new_mode ) , " The new mode can't be mapped to the vehicles mode number " ) ;
# ifdef DISALLOW_GCS_MODE_CHANGE_DURING_RC_FAILSAFE
if ( reason = = ModeReason : : GCS_COMMAND & & copter . failsafe . radio ) {
// don't allow mode changes while in radio failsafe
return false ;
}
# endif
return copter . set_mode ( static_cast < Mode : : Number > ( new_mode ) , ModeReason : : GCS_COMMAND ) ;
}
2014-02-03 04:57:31 -04:00
// update_flight_mode - calls the appropriate attitude controllers based on flight mode
// called at 100hz or more
2015-05-29 23:12:49 -03:00
void Copter : : update_flight_mode ( )
2014-02-03 04:57:31 -04:00
{
2019-06-11 00:13:24 -03:00
surface_tracking . invalidate_for_logging ( ) ; // invalidate surface tracking alt, flight mode will set to true if used
2018-06-05 05:21:09 -03:00
2016-03-24 02:01:40 -03:00
flightmode - > run ( ) ;
2014-02-03 04:57:31 -04:00
}
// exit_mode - high level call to organise cleanup as a flight mode is exited
2019-05-09 23:18:49 -03:00
void Copter : : exit_mode ( Mode * & old_flightmode ,
Mode * & new_flightmode )
2014-02-03 04:57:31 -04:00
{
# if AUTOTUNE_ENABLED == ENABLED
2017-12-11 01:43:27 -04:00
if ( old_flightmode = = & mode_autotune ) {
mode_autotune . stop ( ) ;
2014-02-03 04:57:31 -04:00
}
# endif
2014-02-11 09:32:44 -04:00
2014-05-03 15:26:58 -03:00
// stop mission when we leave auto mode
2018-02-21 22:06:07 -04:00
# if MODE_AUTO_ENABLED == ENABLED
2017-12-11 01:43:27 -04:00
if ( old_flightmode = = & mode_auto ) {
2018-12-11 13:11:49 -04:00
if ( mode_auto . mission . state ( ) = = AP_Mission : : MISSION_RUNNING ) {
mode_auto . mission . stop ( ) ;
2014-05-15 04:11:17 -03:00
}
2014-06-09 16:02:28 -03:00
# if MOUNT == ENABLED
camera_mount . set_mode_to_default ( ) ;
# endif // MOUNT == ENABLED
2014-05-03 15:26:58 -03:00
}
2018-02-21 22:06:07 -04:00
# endif
2014-05-03 15:26:58 -03:00
2014-02-11 09:32:44 -04:00
// smooth throttle transition when switching from manual to automatic flight modes
2017-12-05 19:52:54 -04:00
if ( old_flightmode - > has_manual_throttle ( ) & & ! new_flightmode - > has_manual_throttle ( ) & & motors - > armed ( ) & & ! ap . land_complete ) {
2014-02-11 09:32:44 -04:00
// this assumes all manual flight modes use get_pilot_desired_throttle to translate pilot input to output throttle
2016-10-14 03:34:24 -03:00
set_accel_throttle_I_from_pilot_throttle ( ) ;
2014-02-11 09:32:44 -04:00
}
2015-04-30 03:52:32 -03:00
// cancel any takeoffs in progress
2017-12-12 06:34:49 -04:00
old_flightmode - > takeoff_stop ( ) ;
2015-04-30 03:52:32 -03:00
2018-02-21 23:58:28 -04:00
# if MODE_SMARTRTL_ENABLED == ENABLED
2017-09-08 23:45:31 -03:00
// call smart_rtl cleanup
2017-12-11 01:43:27 -04:00
if ( old_flightmode = = & mode_smartrtl ) {
mode_smartrtl . exit ( ) ;
2017-07-26 14:14:40 -03:00
}
2018-02-21 23:58:28 -04:00
# endif
2017-07-26 14:14:40 -03:00
2019-10-03 22:24:51 -03:00
# if MODE_FOLLOW_ENABLED == ENABLED
if ( old_flightmode = = & mode_follow ) {
mode_follow . exit ( ) ;
}
# endif
2014-07-03 18:07:04 -03:00
# if FRAME_CONFIG == HELI_FRAME
// firmly reset the flybar passthrough to false when exiting acro mode.
2017-12-11 01:43:27 -04:00
if ( old_flightmode = = & mode_acro ) {
2017-01-09 03:31:26 -04:00
attitude_control - > use_flybar_passthrough ( false , false ) ;
motors - > set_acro_tail ( false ) ;
2014-07-03 18:07:04 -03:00
}
2015-05-23 19:57:17 -03:00
2015-10-12 22:05:49 -03:00
// if we are changing from a mode that did not use manual throttle,
// stab col ramp value should be pre-loaded to the correct value to avoid a twitch
// heli_stab_col_ramp should really only be active switching between Stabilize and Acro modes
2017-12-05 19:52:54 -04:00
if ( ! old_flightmode - > has_manual_throttle ( ) ) {
2017-12-11 01:43:27 -04:00
if ( new_flightmode = = & mode_stabilize ) {
2015-10-12 22:05:49 -03:00
input_manager . set_stab_col_ramp ( 1.0 ) ;
2017-12-11 01:43:27 -04:00
} else if ( new_flightmode = = & mode_acro ) {
2015-10-12 22:05:49 -03:00
input_manager . set_stab_col_ramp ( 0.0 ) ;
}
}
2014-07-03 18:07:04 -03:00
# endif //HELI_FRAME
2014-02-03 04:57:31 -04:00
}
2016-03-20 22:19:11 -03:00
// notify_flight_mode - sets notify object based on current flight mode. Only used for OreoLED notify device
2016-03-24 02:01:40 -03:00
void Copter : : notify_flight_mode ( ) {
AP_Notify : : flags . autopilot_mode = flightmode - > is_autopilot ( ) ;
2019-09-07 20:33:56 -03:00
AP_Notify : : flags . flight_mode = ( uint8_t ) control_mode ;
2016-03-24 02:01:40 -03:00
notify . set_flight_mode_str ( flightmode - > name4 ( ) ) ;
2015-03-06 02:33:25 -04:00
}
2017-12-05 21:28:32 -04:00
2019-05-09 23:18:49 -03:00
void Mode : : update_navigation ( )
2017-12-05 21:28:32 -04:00
{
// run autopilot to make high level decisions about control modes
run_autopilot ( ) ;
}
2017-12-12 06:53:56 -04:00
2018-03-13 21:23:30 -03:00
// get_pilot_desired_angle - transform pilot's roll or pitch input into a desired lean angle
// returns desired angle in centi-degrees
2019-05-09 23:18:49 -03:00
void Mode : : get_pilot_desired_lean_angles ( float & roll_out , float & pitch_out , float angle_max , float angle_limit ) const
2018-03-13 21:23:30 -03:00
{
2019-09-13 21:56:36 -03:00
// throttle failsafe check
if ( copter . failsafe . radio | | ! copter . ap . rc_receiver_present ) {
roll_out = 0 ;
pitch_out = 0 ;
return ;
}
2018-03-18 08:06:54 -03:00
// fetch roll and pitch inputs
roll_out = channel_roll - > get_control_in ( ) ;
pitch_out = channel_pitch - > get_control_in ( ) ;
// limit max lean angle
2017-11-12 03:50:51 -04:00
angle_limit = constrain_float ( angle_limit , 1000.0f , angle_max ) ;
2018-03-13 21:23:30 -03:00
2018-03-18 08:06:54 -03:00
// scale roll and pitch inputs to ANGLE_MAX parameter range
2017-11-12 03:50:51 -04:00
float scaler = angle_max / ( float ) ROLL_PITCH_YAW_INPUT_MAX ;
2018-03-18 08:06:54 -03:00
roll_out * = scaler ;
pitch_out * = scaler ;
2018-03-13 21:23:30 -03:00
// do circular limit
2018-03-18 08:06:54 -03:00
float total_in = norm ( pitch_out , roll_out ) ;
2017-11-12 03:50:51 -04:00
if ( total_in > angle_limit ) {
float ratio = angle_limit / total_in ;
2018-03-18 08:06:54 -03:00
roll_out * = ratio ;
pitch_out * = ratio ;
2018-03-13 21:23:30 -03:00
}
// do lateral tilt to euler roll conversion
2018-03-18 08:06:54 -03:00
roll_out = ( 18000 / M_PI ) * atanf ( cosf ( pitch_out * ( M_PI / 18000 ) ) * tanf ( roll_out * ( M_PI / 18000 ) ) ) ;
2018-03-13 21:23:30 -03:00
2018-03-18 08:06:54 -03:00
// roll_out and pitch_out are returned
2018-03-13 21:23:30 -03:00
}
2019-05-09 23:18:49 -03:00
bool Mode : : _TakeOff : : triggered ( const float target_climb_rate ) const
2017-12-12 06:53:56 -04:00
{
2018-03-16 03:22:14 -03:00
if ( ! copter . ap . land_complete ) {
2017-12-12 06:53:56 -04:00
// can't take off if we're already flying
return false ;
}
if ( target_climb_rate < = 0.0f ) {
// can't takeoff unless we want to go up...
return false ;
}
2018-12-31 00:18:14 -04:00
2019-04-09 09:16:58 -03:00
if ( copter . motors - > get_spool_state ( ) ! = AP_Motors : : SpoolState : : THROTTLE_UNLIMITED ) {
2019-02-28 05:03:00 -04:00
// hold aircraft on the ground until rotor speed runup has finished
2017-12-12 06:53:56 -04:00
return false ;
}
2019-02-28 05:03:00 -04:00
2017-12-12 06:53:56 -04:00
return true ;
}
2016-11-17 01:46:52 -04:00
2019-05-09 23:18:49 -03:00
bool Mode : : is_disarmed_or_landed ( ) const
2019-04-08 05:15:57 -03:00
{
2019-05-09 23:18:49 -03:00
if ( ! motors - > armed ( ) | | ! copter . ap . auto_armed | | copter . ap . land_complete ) {
2019-04-08 05:15:57 -03:00
return true ;
}
return false ;
}
2019-05-09 23:18:49 -03:00
void Mode : : zero_throttle_and_relax_ac ( bool spool_up )
2016-11-17 01:46:52 -04:00
{
2019-01-08 06:43:51 -04:00
if ( spool_up ) {
2019-04-09 09:16:58 -03:00
motors - > set_desired_spool_state ( AP_Motors : : DesiredSpoolState : : THROTTLE_UNLIMITED ) ;
2019-01-08 06:43:51 -04:00
} else {
2019-04-09 09:16:58 -03:00
motors - > set_desired_spool_state ( AP_Motors : : DesiredSpoolState : : GROUND_IDLE ) ;
2019-01-08 06:43:51 -04:00
}
2017-06-26 05:48:04 -03:00
attitude_control - > input_euler_angle_roll_pitch_euler_rate_yaw ( 0.0f , 0.0f , 0.0f ) ;
2018-02-07 22:21:09 -04:00
attitude_control - > set_throttle_out ( 0.0f , false , copter . g . throttle_filt ) ;
2019-02-28 05:03:23 -04:00
}
2019-05-09 23:18:49 -03:00
void Mode : : zero_throttle_and_hold_attitude ( )
2019-02-28 05:03:23 -04:00
{
// run attitude controller
attitude_control - > input_rate_bf_roll_pitch_yaw ( 0.0f , 0.0f , 0.0f ) ;
attitude_control - > set_throttle_out ( 0.0f , false , copter . g . throttle_filt ) ;
}
2019-05-09 23:18:49 -03:00
void Mode : : make_safe_spool_down ( )
2019-02-28 05:03:23 -04:00
{
// command aircraft to initiate the shutdown process
2019-04-09 09:16:58 -03:00
motors - > set_desired_spool_state ( AP_Motors : : DesiredSpoolState : : GROUND_IDLE ) ;
switch ( motors - > get_spool_state ( ) ) {
2019-02-28 05:03:23 -04:00
2019-04-09 09:16:58 -03:00
case AP_Motors : : SpoolState : : SHUT_DOWN :
case AP_Motors : : SpoolState : : GROUND_IDLE :
2019-02-28 05:03:23 -04:00
// relax controllers during idle states
2020-12-20 17:40:58 -04:00
attitude_control - > reset_rate_controller_I_terms_smoothly ( ) ;
2019-02-28 05:03:23 -04:00
attitude_control - > set_yaw_target_to_current_heading ( ) ;
break ;
2019-04-09 09:16:58 -03:00
case AP_Motors : : SpoolState : : SPOOLING_UP :
case AP_Motors : : SpoolState : : THROTTLE_UNLIMITED :
case AP_Motors : : SpoolState : : SPOOLING_DOWN :
2019-02-28 05:03:23 -04:00
// while transitioning though active states continue to operate normally
break ;
}
pos_control - > relax_alt_hold_controllers ( 0.0f ) ; // forces throttle output to go to zero
pos_control - > update_z_controller ( ) ;
// we may need to move this out
attitude_control - > input_euler_angle_roll_pitch_euler_rate_yaw ( 0.0f , 0.0f , 0.0f ) ;
2016-11-17 01:46:52 -04:00
}
2018-02-07 22:21:09 -04:00
2018-03-22 07:13:58 -03:00
/*
get a height above ground estimate for landing
*/
2019-05-09 23:18:49 -03:00
int32_t Mode : : get_alt_above_ground_cm ( void )
2018-03-22 07:13:58 -03:00
{
int32_t alt_above_ground ;
if ( copter . rangefinder_alt_ok ( ) ) {
alt_above_ground = copter . rangefinder_state . alt_cm_filt . get ( ) ;
} else {
bool navigating = pos_control - > is_active_xy ( ) ;
2019-03-14 22:44:27 -03:00
if ( ! navigating | | ! copter . current_loc . get_alt_cm ( Location : : AltFrame : : ABOVE_TERRAIN , alt_above_ground ) ) {
2018-03-22 07:13:58 -03:00
alt_above_ground = copter . current_loc . alt ;
}
}
return alt_above_ground ;
}
2019-05-09 23:18:49 -03:00
void Mode : : land_run_vertical_control ( bool pause_descent )
2018-03-22 07:13:58 -03:00
{
2018-04-06 10:21:42 -03:00
# if PRECISION_LANDING == ENABLED
2018-03-22 07:13:58 -03:00
const bool navigating = pos_control - > is_active_xy ( ) ;
2019-05-09 23:18:49 -03:00
bool doing_precision_landing = ! copter . ap . land_repo_active & & copter . precland . target_acquired ( ) & & navigating ;
2018-03-22 07:13:58 -03:00
# else
bool doing_precision_landing = false ;
# endif
// compute desired velocity
const float precland_acceptable_error = 15.0f ;
const float precland_min_descent_speed = 10.0f ;
float cmb_rate = 0 ;
if ( ! pause_descent ) {
float max_land_descent_velocity ;
if ( g . land_speed_high > 0 ) {
max_land_descent_velocity = - g . land_speed_high ;
} else {
2018-09-20 02:45:02 -03:00
max_land_descent_velocity = pos_control - > get_max_speed_down ( ) ;
2018-03-22 07:13:58 -03:00
}
// Don't speed up for landing.
max_land_descent_velocity = MIN ( max_land_descent_velocity , - abs ( g . land_speed ) ) ;
// Compute a vertical velocity demand such that the vehicle approaches g2.land_alt_low. Without the below constraint, this would cause the vehicle to hover at g2.land_alt_low.
2019-02-25 23:07:55 -04:00
cmb_rate = AC_AttitudeControl : : sqrt_controller ( MAX ( g2 . land_alt_low , 100 ) - get_alt_above_ground_cm ( ) , pos_control - > get_pos_z_p ( ) . kP ( ) , pos_control - > get_max_accel_z ( ) , G_Dt ) ;
2018-03-22 07:13:58 -03:00
// Constrain the demanded vertical velocity so that it is between the configured maximum descent speed and the configured minimum descent speed.
cmb_rate = constrain_float ( cmb_rate , max_land_descent_velocity , - abs ( g . land_speed ) ) ;
if ( doing_precision_landing & & copter . rangefinder_alt_ok ( ) & & copter . rangefinder_state . alt_cm > 35.0f & & copter . rangefinder_state . alt_cm < 200.0f ) {
2019-02-04 14:12:44 -04:00
float max_descent_speed = abs ( g . land_speed ) * 0.5f ;
2018-03-22 07:13:58 -03:00
float land_slowdown = MAX ( 0.0f , pos_control - > get_horizontal_error ( ) * ( max_descent_speed / precland_acceptable_error ) ) ;
cmb_rate = MIN ( - precland_min_descent_speed , - max_descent_speed + land_slowdown ) ;
}
}
// update altitude target and call position controller
pos_control - > set_alt_target_from_climb_rate_ff ( cmb_rate , G_Dt , true ) ;
pos_control - > update_z_controller ( ) ;
}
2019-05-09 23:18:49 -03:00
void Mode : : land_run_horizontal_control ( )
2018-03-22 07:13:58 -03:00
{
float target_roll = 0.0f ;
float target_pitch = 0.0f ;
float target_yaw_rate = 0 ;
// relax loiter target if we might be landed
2019-05-09 23:18:49 -03:00
if ( copter . ap . land_complete_maybe ) {
2018-03-27 23:13:37 -03:00
loiter_nav - > soften_for_landing ( ) ;
2018-03-22 07:13:58 -03:00
}
// process pilot inputs
if ( ! copter . failsafe . radio ) {
2018-10-03 00:14:44 -03:00
if ( ( g . throttle_behavior & THR_BEHAVE_HIGH_THROTTLE_CANCELS_LAND ) ! = 0 & & copter . rc_throttle_control_in_filter . get ( ) > LAND_CANCEL_TRIGGER_THR ) {
2018-03-22 07:13:58 -03:00
copter . Log_Write_Event ( DATA_LAND_CANCELLED_BY_PILOT ) ;
// exit land if throttle is high
2019-10-17 00:49:22 -03:00
if ( ! set_mode ( Mode : : Number : : LOITER , ModeReason : : THROTTLE_LAND_ESCAPE ) ) {
set_mode ( Mode : : Number : : ALT_HOLD , ModeReason : : THROTTLE_LAND_ESCAPE ) ;
2018-03-22 07:13:58 -03:00
}
}
if ( g . land_repositioning ) {
// apply SIMPLE mode transform to pilot inputs
update_simple_mode ( ) ;
// convert pilot input to lean angles
2018-03-27 23:13:37 -03:00
get_pilot_desired_lean_angles ( target_roll , target_pitch , loiter_nav - > get_angle_max_cd ( ) , attitude_control - > get_althold_lean_angle_max ( ) ) ;
2018-03-22 07:13:58 -03:00
2019-03-25 20:57:55 -03:00
// record if pilot has overridden roll or pitch
2018-03-22 07:13:58 -03:00
if ( ! is_zero ( target_roll ) | | ! is_zero ( target_pitch ) ) {
2019-05-09 23:18:49 -03:00
if ( ! copter . ap . land_repo_active ) {
2018-11-12 06:31:22 -04:00
copter . Log_Write_Event ( DATA_LAND_REPO_ACTIVE ) ;
}
2019-05-09 23:18:49 -03:00
copter . ap . land_repo_active = true ;
2018-03-22 07:13:58 -03:00
}
}
// get pilot's desired yaw rate
target_yaw_rate = get_pilot_desired_yaw_rate ( channel_yaw - > get_control_in ( ) ) ;
2018-05-30 14:01:52 -03:00
if ( ! is_zero ( target_yaw_rate ) ) {
auto_yaw . set_mode ( AUTO_YAW_HOLD ) ;
}
2018-03-22 07:13:58 -03:00
}
# if PRECISION_LANDING == ENABLED
2019-05-09 23:18:49 -03:00
bool doing_precision_landing = ! copter . ap . land_repo_active & & copter . precland . target_acquired ( ) ;
2018-03-22 07:13:58 -03:00
// run precision landing
if ( doing_precision_landing ) {
Vector2f target_pos , target_vel_rel ;
2018-10-03 00:14:44 -03:00
if ( ! copter . precland . get_target_position_cm ( target_pos ) ) {
2018-03-22 07:13:58 -03:00
target_pos . x = inertial_nav . get_position ( ) . x ;
target_pos . y = inertial_nav . get_position ( ) . y ;
}
2018-10-03 00:14:44 -03:00
if ( ! copter . precland . get_target_velocity_relative_cms ( target_vel_rel ) ) {
2018-03-22 07:13:58 -03:00
target_vel_rel . x = - inertial_nav . get_velocity ( ) . x ;
target_vel_rel . y = - inertial_nav . get_velocity ( ) . y ;
}
pos_control - > set_xy_target ( target_pos . x , target_pos . y ) ;
pos_control - > override_vehicle_velocity_xy ( - target_vel_rel ) ;
}
# endif
// process roll, pitch inputs
2018-03-27 23:13:37 -03:00
loiter_nav - > set_pilot_desired_acceleration ( target_roll , target_pitch , G_Dt ) ;
2018-03-22 07:13:58 -03:00
// run loiter controller
2018-09-05 03:47:50 -03:00
loiter_nav - > update ( ) ;
2018-03-22 07:13:58 -03:00
2019-04-04 18:05:23 -03:00
float nav_roll = loiter_nav - > get_roll ( ) ;
float nav_pitch = loiter_nav - > get_pitch ( ) ;
2018-03-22 07:13:58 -03:00
if ( g2 . wp_navalt_min > 0 ) {
// user has requested an altitude below which navigation
// attitude is limited. This is used to prevent commanded roll
// over on landing, which particularly affects helicopters if
// there is any position estimate drift after touchdown. We
// limit attitude to 7 degrees below this limit and linearly
// interpolate for 1m above that
2019-02-25 23:07:55 -04:00
float attitude_limit_cd = linear_interpolate ( 700 , copter . aparm . angle_max , get_alt_above_ground_cm ( ) ,
2018-03-22 07:13:58 -03:00
g2 . wp_navalt_min * 100U , ( g2 . wp_navalt_min + 1 ) * 100U ) ;
float total_angle_cd = norm ( nav_roll , nav_pitch ) ;
if ( total_angle_cd > attitude_limit_cd ) {
float ratio = attitude_limit_cd / total_angle_cd ;
nav_roll * = ratio ;
nav_pitch * = ratio ;
// tell position controller we are applying an external limit
pos_control - > set_limit_accel_xy ( ) ;
}
}
// call attitude controller
2018-05-30 14:01:52 -03:00
if ( auto_yaw . mode ( ) = = AUTO_YAW_HOLD ) {
// roll & pitch from waypoint controller, yaw rate from pilot
attitude_control - > input_euler_angle_roll_pitch_euler_rate_yaw ( nav_roll , nav_pitch , target_yaw_rate ) ;
} else {
// roll, pitch from waypoint controller, yaw heading from auto_heading()
attitude_control - > input_euler_angle_roll_pitch_yaw ( nav_roll , nav_pitch , auto_yaw . yaw ( ) , true ) ;
}
2018-03-22 07:13:58 -03:00
}
2019-05-09 23:18:49 -03:00
float Mode : : throttle_hover ( ) const
2019-03-26 07:09:12 -03:00
{
return motors - > get_throttle_hover ( ) ;
}
// transform pilot's manual throttle input to make hover throttle mid stick
// used only for manual throttle modes
// thr_mid should be in the range 0 to 1
// returns throttle output 0 to 1
2019-05-09 23:18:49 -03:00
float Mode : : get_pilot_desired_throttle ( ) const
2019-03-26 07:09:12 -03:00
{
const float thr_mid = throttle_hover ( ) ;
int16_t throttle_control = channel_throttle - > get_control_in ( ) ;
int16_t mid_stick = copter . get_throttle_mid ( ) ;
// protect against unlikely divide by zero
if ( mid_stick < = 0 ) {
mid_stick = 500 ;
}
// ensure reasonable throttle values
throttle_control = constrain_int16 ( throttle_control , 0 , 1000 ) ;
// calculate normalised throttle input
float throttle_in ;
if ( throttle_control < mid_stick ) {
throttle_in = ( ( float ) throttle_control ) * 0.5f / ( float ) mid_stick ;
} else {
2019-03-26 21:55:18 -03:00
throttle_in = 0.5f + ( ( float ) ( throttle_control - mid_stick ) ) * 0.5f / ( float ) ( 1000 - mid_stick ) ;
2019-03-26 07:09:12 -03:00
}
2019-04-04 02:41:10 -03:00
const float expo = constrain_float ( - ( thr_mid - 0.5f ) / 0.375f , - 0.5f , 1.0f ) ;
2019-03-26 07:09:12 -03:00
// calculate the output throttle using the given expo function
float throttle_out = throttle_in * ( 1.0f - expo ) + expo * throttle_in * throttle_in * throttle_in ;
return throttle_out ;
}
2019-05-09 23:18:49 -03:00
Mode : : AltHoldModeState Mode : : get_alt_hold_state ( float target_climb_rate_cms )
2019-02-28 05:03:23 -04:00
{
// Alt Hold State Machine Determination
if ( ! motors - > armed ( ) ) {
// the aircraft should moved to a shut down state
2019-04-09 09:16:58 -03:00
motors - > set_desired_spool_state ( AP_Motors : : DesiredSpoolState : : SHUT_DOWN ) ;
2019-02-28 05:03:23 -04:00
// transition through states as aircraft spools down
2019-04-09 09:16:58 -03:00
switch ( motors - > get_spool_state ( ) ) {
2019-02-28 05:03:23 -04:00
2019-04-09 09:16:58 -03:00
case AP_Motors : : SpoolState : : SHUT_DOWN :
2019-02-28 05:03:23 -04:00
return AltHold_MotorStopped ;
2019-04-09 09:16:58 -03:00
case AP_Motors : : SpoolState : : GROUND_IDLE :
2019-02-28 05:03:23 -04:00
return AltHold_Landed_Ground_Idle ;
default :
return AltHold_Landed_Pre_Takeoff ;
}
} else if ( takeoff . running ( ) | | takeoff . triggered ( target_climb_rate_cms ) ) {
// the aircraft is currently landed or taking off, asking for a positive climb rate and in THROTTLE_UNLIMITED
// the aircraft should progress through the take off procedure
return AltHold_Takeoff ;
2019-05-09 23:18:49 -03:00
} else if ( ! copter . ap . auto_armed | | copter . ap . land_complete ) {
2019-02-28 05:03:23 -04:00
// the aircraft is armed and landed
2019-05-09 23:18:49 -03:00
if ( target_climb_rate_cms < 0.0f & & ! copter . ap . using_interlock ) {
2019-02-28 05:03:23 -04:00
// the aircraft should move to a ground idle state
2019-04-09 09:16:58 -03:00
motors - > set_desired_spool_state ( AP_Motors : : DesiredSpoolState : : GROUND_IDLE ) ;
2019-02-28 05:03:23 -04:00
} else {
// the aircraft should prepare for imminent take off
2019-04-09 09:16:58 -03:00
motors - > set_desired_spool_state ( AP_Motors : : DesiredSpoolState : : THROTTLE_UNLIMITED ) ;
2019-02-28 05:03:23 -04:00
}
2019-04-09 09:16:58 -03:00
if ( motors - > get_spool_state ( ) = = AP_Motors : : SpoolState : : GROUND_IDLE ) {
2019-02-28 05:03:23 -04:00
// the aircraft is waiting in ground idle
return AltHold_Landed_Ground_Idle ;
} else {
// the aircraft can leave the ground at any time
return AltHold_Landed_Pre_Takeoff ;
}
} else {
// the aircraft is in a flying state
2019-04-09 09:16:58 -03:00
motors - > set_desired_spool_state ( AP_Motors : : DesiredSpoolState : : THROTTLE_UNLIMITED ) ;
2019-02-28 05:03:23 -04:00
return AltHold_Flying ;
}
}
2018-02-07 22:21:09 -04:00
// pass-through functions to reduce code churn on conversion;
// these are candidates for moving into the Mode base
// class.
2019-05-09 23:18:49 -03:00
float Mode : : get_pilot_desired_yaw_rate ( int16_t stick_angle )
2018-02-07 22:21:09 -04:00
{
return copter . get_pilot_desired_yaw_rate ( stick_angle ) ;
}
2019-05-09 23:18:49 -03:00
float Mode : : get_pilot_desired_climb_rate ( float throttle_control )
2018-02-07 22:21:09 -04:00
{
return copter . get_pilot_desired_climb_rate ( throttle_control ) ;
}
2019-05-09 23:18:49 -03:00
float Mode : : get_non_takeoff_throttle ( )
2018-02-07 22:21:09 -04:00
{
return copter . get_non_takeoff_throttle ( ) ;
}
2019-05-09 23:18:49 -03:00
void Mode : : update_simple_mode ( void ) {
2018-02-07 22:21:09 -04:00
copter . update_simple_mode ( ) ;
}
2019-10-17 00:49:22 -03:00
bool Mode : : set_mode ( Mode : : Number mode , ModeReason reason )
2018-02-07 22:21:09 -04:00
{
return copter . set_mode ( mode , reason ) ;
}
2019-05-09 23:18:49 -03:00
void Mode : : set_land_complete ( bool b )
2018-02-07 22:21:09 -04:00
{
return copter . set_land_complete ( b ) ;
}
2019-05-09 23:18:49 -03:00
GCS_Copter & Mode : : gcs ( )
2018-02-07 22:21:09 -04:00
{
return copter . gcs ( ) ;
}
2019-05-09 23:18:49 -03:00
void Mode : : Log_Write_Event ( Log_Event id )
2018-02-07 22:21:09 -04:00
{
2019-02-01 07:30:00 -04:00
return copter . logger . Write_Event ( id ) ;
2018-02-07 22:21:09 -04:00
}
2019-05-09 23:18:49 -03:00
void Mode : : set_throttle_takeoff ( )
2018-02-07 22:21:09 -04:00
{
return copter . set_throttle_takeoff ( ) ;
}
2019-05-09 23:18:49 -03:00
float Mode : : get_avoidance_adjusted_climbrate ( float target_rate )
2018-02-07 22:21:09 -04:00
{
return copter . get_avoidance_adjusted_climbrate ( target_rate ) ;
}
2019-05-09 23:18:49 -03:00
uint16_t Mode : : get_pilot_speed_dn ( )
2018-02-07 22:21:09 -04:00
{
return copter . get_pilot_speed_dn ( ) ;
}