2015-05-29 23:12:49 -03:00
# include "Copter.h"
2019-02-25 10:32:52 -04:00
# if MODE_POSHOLD_ENABLED == ENABLED
2014-04-05 11:20:39 -03:00
/*
2016-07-25 15:45:29 -03:00
* Init and run calls for PosHold flight mode
2014-07-11 02:06:53 -03:00
* PosHold tries to improve upon regular loiter by mixing the pilot input with the loiter controller
2014-04-05 11:20:39 -03:00
*/
2015-05-04 19:12:19 -03:00
# define POSHOLD_SPEED_0 10 // speed below which it is always safe to switch to loiter
2014-04-13 02:33:31 -03:00
2015-05-04 19:12:19 -03:00
// 400hz loop update rate
# define POSHOLD_BRAKE_TIME_ESTIMATE_MAX (600*4) // max number of cycles the brake will be applied before we switch to loiter
# define POSHOLD_BRAKE_TO_LOITER_TIMER (150*4) // Number of cycles to transition from brake mode to loiter mode. Must be lower than POSHOLD_LOITER_STAB_TIMER
# define POSHOLD_WIND_COMP_START_TIMER (150*4) // Number of cycles to start wind compensation update after loiter is engaged
# define POSHOLD_CONTROLLER_TO_PILOT_MIX_TIMER (50*4) // Set it from 100 to 200, the number of centiseconds loiter and manual commands are mixed to make a smooth transition.
# define POSHOLD_SMOOTH_RATE_FACTOR 0.0125f // filter applied to pilot's roll/pitch input as it returns to center. A lower number will cause the roll/pitch to return to zero more slowly if the brake_rate is also low.
# define POSHOLD_WIND_COMP_TIMER_10HZ 40 // counter value used to reduce wind compensation to 10hz
# define LOOP_RATE_FACTOR 4 // used to adapt PosHold params to loop_rate
# define TC_WIND_COMP 0.0025f // Time constant for poshold_update_wind_comp_estimate()
2014-04-13 02:33:31 -03:00
// definitions that are independent of main loop rate
2015-05-04 19:12:19 -03:00
# define POSHOLD_STICK_RELEASE_SMOOTH_ANGLE 1800 // max angle required (in centi-degrees) after which the smooth stick release effect is applied
# define POSHOLD_WIND_COMP_ESTIMATE_SPEED_MAX 10 // wind compensation estimates will only run when velocity is at or below this speed in cm/s
2020-09-08 09:12:53 -03:00
# define POSHOLD_WIND_COMP_LEAN_PCT_MAX 0.6666f // wind compensation no more than 2/3rds of angle max to ensure pilot can always override
2014-04-10 03:55:54 -03:00
2014-07-11 02:06:53 -03:00
// poshold_init - initialise PosHold controller
2019-05-09 23:18:49 -03:00
bool ModePosHold : : init ( bool ignore_checks )
2014-04-04 11:14:07 -03:00
{
2021-05-19 11:07:38 -03:00
// set vertical speed and acceleration limits
2021-05-11 01:42:02 -03:00
pos_control - > set_max_speed_accel_z ( - get_pilot_speed_dn ( ) , g . pilot_speed_up , g . pilot_accel_z ) ;
2014-04-04 11:14:07 -03:00
2021-05-19 11:07:38 -03:00
// initialise the vertical position controller
2017-01-09 03:31:26 -04:00
if ( ! pos_control - > is_active_z ( ) ) {
2021-05-11 01:42:02 -03:00
pos_control - > init_z_controller ( ) ;
2016-10-14 09:28:32 -03:00
}
2014-04-30 04:29:32 -03:00
2014-04-11 05:16:40 -03:00
// initialise lean angles to current attitude
2019-07-07 10:13:43 -03:00
pilot_roll = 0.0f ;
pilot_pitch = 0.0f ;
2014-04-11 05:16:40 -03:00
2014-04-10 03:55:54 -03:00
// compute brake_gain
2019-04-08 21:58:11 -03:00
brake . gain = ( 15.0f * ( float ) g . poshold_brake_rate + 95.0f ) / 100.0f ;
2014-04-05 11:20:39 -03:00
2019-05-09 23:18:49 -03:00
if ( copter . ap . land_complete ) {
2014-04-11 05:16:40 -03:00
// if landed begin in loiter mode
2019-04-22 23:14:35 -03:00
roll_mode = RPMode : : LOITER ;
pitch_mode = RPMode : : LOITER ;
2018-12-28 06:32:57 -04:00
} else {
2014-04-11 05:16:40 -03:00
// if not landed start in pilot override to avoid hard twitch
2019-04-22 23:14:35 -03:00
roll_mode = RPMode : : PILOT_OVERRIDE ;
pitch_mode = RPMode : : PILOT_OVERRIDE ;
2014-04-04 11:14:07 -03:00
}
2014-04-10 03:55:54 -03:00
2019-02-28 05:03:23 -04:00
// initialise loiter
loiter_nav - > clear_pilot_desired_acceleration ( ) ;
loiter_nav - > init_target ( ) ;
2014-07-11 02:06:53 -03:00
// initialise wind_comp each time PosHold is switched on
2020-11-05 21:42:17 -04:00
init_wind_comp_estimate ( ) ;
2014-04-10 03:55:54 -03:00
return true ;
2014-04-04 11:14:07 -03:00
}
2014-07-11 02:06:53 -03:00
// poshold_run - runs the PosHold controller
2014-04-04 11:14:07 -03:00
// should be called at 100hz or more
2019-05-09 23:18:49 -03:00
void ModePosHold : : run ( )
2014-04-04 11:14:07 -03:00
{
2014-04-23 00:39:46 -03:00
float controller_to_pilot_roll_mix ; // mix of controller and pilot controls. 0 = fully last controller controls, 1 = fully pilot controls
float controller_to_pilot_pitch_mix ; // mix of controller and pilot controls. 0 = fully last controller controls, 1 = fully pilot controls
2014-04-05 11:20:39 -03:00
const Vector3f & vel = inertial_nav . get_velocity ( ) ;
2014-04-04 11:14:07 -03:00
2021-05-19 11:07:38 -03:00
// set vertical speed and acceleration limits
2021-05-11 01:42:02 -03:00
pos_control - > set_max_speed_accel_z ( - get_pilot_speed_dn ( ) , g . pilot_speed_up , g . pilot_accel_z ) ;
2018-09-18 05:06:24 -03:00
loiter_nav - > clear_pilot_desired_acceleration ( ) ;
2015-10-19 21:05:10 -03:00
2019-02-28 05:03:23 -04:00
// apply SIMPLE mode transform to pilot inputs
update_simple_mode ( ) ;
// convert pilot input to lean angles
float target_roll , target_pitch ;
get_pilot_desired_lean_angles ( target_roll , target_pitch , copter . aparm . angle_max , attitude_control - > get_althold_lean_angle_max ( ) ) ;
// get pilot's desired yaw rate
float target_yaw_rate = get_pilot_desired_yaw_rate ( channel_yaw - > get_control_in ( ) ) ;
// get pilot desired climb rate (for alt-hold mode and take-off)
float target_climb_rate = get_pilot_desired_climb_rate ( channel_throttle - > get_control_in ( ) ) ;
target_climb_rate = constrain_float ( target_climb_rate , - get_pilot_speed_dn ( ) , g . pilot_speed_up ) ;
// relax loiter target if we might be landed
2019-05-09 23:18:49 -03:00
if ( copter . ap . land_complete_maybe ) {
2019-02-28 05:03:23 -04:00
loiter_nav - > soften_for_landing ( ) ;
2014-04-04 11:14:07 -03:00
}
2019-02-28 05:03:23 -04:00
// Pos Hold State Machine Determination
AltHoldModeState poshold_state = get_alt_hold_state ( target_climb_rate ) ;
2014-04-04 11:14:07 -03:00
2019-02-28 05:03:23 -04:00
// state machine
switch ( poshold_state ) {
2014-04-04 11:14:07 -03:00
2019-02-28 05:03:23 -04:00
case AltHold_MotorStopped :
attitude_control - > reset_rate_controller_I_terms ( ) ;
2021-05-24 10:42:19 -03:00
attitude_control - > reset_yaw_target_and_rate ( ) ;
2021-05-19 11:07:38 -03:00
pos_control - > relax_z_controller ( 0.0f ) ; // forces throttle output to decay to zero
2019-07-18 08:06:09 -03:00
loiter_nav - > clear_pilot_desired_acceleration ( ) ;
2019-02-28 05:03:23 -04:00
loiter_nav - > init_target ( ) ;
2021-03-15 15:43:45 -03:00
loiter_nav - > update ( false ) ;
2015-04-30 03:06:55 -03:00
2019-02-28 05:03:23 -04:00
// set poshold state to pilot override
2019-04-22 23:14:35 -03:00
roll_mode = RPMode : : PILOT_OVERRIDE ;
pitch_mode = RPMode : : PILOT_OVERRIDE ;
2020-11-05 21:42:17 -04:00
// initialise wind compensation estimate
init_wind_comp_estimate ( ) ;
2019-02-28 05:03:23 -04:00
break ;
2014-04-04 11:14:07 -03:00
2019-02-28 05:03:23 -04:00
case AltHold_Takeoff :
// initiate take-off
if ( ! takeoff . running ( ) ) {
takeoff . start ( constrain_float ( g . pilot_takeoff_alt , 0.0f , 1000.0f ) ) ;
2014-04-04 11:14:07 -03:00
}
2019-02-28 05:03:23 -04:00
// get avoidance adjusted climb rate
target_climb_rate = get_avoidance_adjusted_climbrate ( target_climb_rate ) ;
2021-05-19 11:07:38 -03:00
// set position controller targets adjusted for pilot input
2021-05-13 02:40:05 -03:00
takeoff . do_pilot_takeoff ( target_climb_rate ) ;
2019-02-28 05:03:23 -04:00
// init and update loiter although pilot is controlling lean angles
2019-07-18 08:06:09 -03:00
loiter_nav - > clear_pilot_desired_acceleration ( ) ;
2018-03-27 23:13:37 -03:00
loiter_nav - > init_target ( ) ;
2021-03-15 15:43:45 -03:00
loiter_nav - > update ( false ) ;
2019-02-28 05:03:23 -04:00
// set poshold state to pilot override
2019-04-22 23:14:35 -03:00
roll_mode = RPMode : : PILOT_OVERRIDE ;
pitch_mode = RPMode : : PILOT_OVERRIDE ;
2019-02-28 05:03:23 -04:00
break ;
case AltHold_Landed_Ground_Idle :
2019-07-18 08:06:09 -03:00
loiter_nav - > clear_pilot_desired_acceleration ( ) ;
2019-02-28 05:03:23 -04:00
loiter_nav - > init_target ( ) ;
2021-03-15 15:43:45 -03:00
loiter_nav - > update ( false ) ;
2021-05-24 10:42:19 -03:00
attitude_control - > reset_yaw_target_and_rate ( ) ;
2020-11-05 21:42:17 -04:00
init_wind_comp_estimate ( ) ;
2020-01-09 05:12:06 -04:00
FALLTHROUGH ;
2019-02-28 05:03:23 -04:00
case AltHold_Landed_Pre_Takeoff :
2020-12-20 17:40:58 -04:00
attitude_control - > reset_rate_controller_I_terms_smoothly ( ) ;
2021-05-19 11:07:38 -03:00
pos_control - > relax_z_controller ( 0.0f ) ; // forces throttle output to decay to zero
2014-04-05 11:20:39 -03:00
2019-02-28 05:03:23 -04:00
// set poshold state to pilot override
2019-04-22 23:14:35 -03:00
roll_mode = RPMode : : PILOT_OVERRIDE ;
pitch_mode = RPMode : : PILOT_OVERRIDE ;
2019-02-28 05:03:23 -04:00
break ;
2014-04-05 11:20:39 -03:00
2019-02-28 05:03:23 -04:00
case AltHold_Flying :
2019-04-09 09:16:58 -03:00
motors - > set_desired_spool_state ( AP_Motors : : DesiredSpoolState : : THROTTLE_UNLIMITED ) ;
2014-04-11 05:16:40 -03:00
2019-02-28 05:03:23 -04:00
// adjust climb rate using rangefinder
if ( copter . rangefinder_alt_ok ( ) ) {
// if rangefinder is ok, use surface tracking
2019-06-11 00:13:24 -03:00
target_climb_rate = copter . surface_tracking . adjust_climb_rate ( target_climb_rate ) ;
2019-02-28 05:03:23 -04:00
}
2014-04-05 11:20:39 -03:00
2019-02-28 05:03:23 -04:00
// get avoidance adjusted climb rate
target_climb_rate = get_avoidance_adjusted_climbrate ( target_climb_rate ) ;
2014-04-10 03:55:54 -03:00
2021-05-11 01:42:02 -03:00
pos_control - > set_pos_target_z_from_climb_rate_cm ( target_climb_rate , false ) ;
2019-02-28 05:03:23 -04:00
break ;
}
2014-04-04 11:14:07 -03:00
2019-02-28 05:03:23 -04:00
// poshold specific behaviour to calculate desired roll, pitch angles
// convert inertial nav earth-frame velocities to body-frame
// To-Do: move this to AP_Math (or perhaps we already have a function to do this)
float vel_fw = vel . x * ahrs . cos_yaw ( ) + vel . y * ahrs . sin_yaw ( ) ;
float vel_right = - vel . x * ahrs . sin_yaw ( ) + vel . y * ahrs . cos_yaw ( ) ;
2014-04-23 00:39:46 -03:00
2019-02-28 05:03:23 -04:00
// If not in LOITER, retrieve latest wind compensation lean angles related to current yaw
2019-04-22 23:14:35 -03:00
if ( roll_mode ! = RPMode : : LOITER | | pitch_mode ! = RPMode : : LOITER ) {
2019-04-22 22:54:48 -03:00
get_wind_comp_lean_angles ( wind_comp_roll , wind_comp_pitch ) ;
2019-02-28 05:03:23 -04:00
}
2014-04-10 03:55:54 -03:00
2019-02-28 05:03:23 -04:00
// Roll state machine
// Each state (aka mode) is responsible for:
// 1. dealing with pilot input
// 2. calculating the final roll output to the attitude controller
// 3. checking if the state (aka mode) should be changed and if 'yes' perform any required initialisation for the new state
2019-04-22 22:48:41 -03:00
switch ( roll_mode ) {
2019-02-28 05:03:23 -04:00
2019-04-22 23:14:35 -03:00
case RPMode : : PILOT_OVERRIDE :
2019-02-28 05:03:23 -04:00
// update pilot desired roll angle using latest radio input
// this filters the input so that it returns to zero no faster than the brake-rate
2019-04-22 22:54:48 -03:00
update_pilot_lean_angle ( pilot_roll , target_roll ) ;
2019-02-28 05:03:23 -04:00
// switch to BRAKE mode for next iteration if no pilot input
2019-04-22 22:48:41 -03:00
if ( is_zero ( target_roll ) & & ( fabsf ( pilot_roll ) < 2 * g . poshold_brake_rate ) ) {
2019-02-28 05:03:23 -04:00
// initialise BRAKE mode
2019-04-22 23:14:35 -03:00
roll_mode = RPMode : : BRAKE ; // Set brake roll mode
2019-04-08 21:58:11 -03:00
brake . roll = 0.0f ; // initialise braking angle to zero
brake . angle_max_roll = 0.0f ; // reset brake_angle_max so we can detect when vehicle begins to flatten out during braking
brake . timeout_roll = POSHOLD_BRAKE_TIME_ESTIMATE_MAX ; // number of cycles the brake will be applied, updated during braking mode.
brake . time_updated_roll = false ; // flag the braking time can be re-estimated
2019-02-28 05:03:23 -04:00
}
// final lean angle should be pilot input plus wind compensation
2019-04-22 22:48:41 -03:00
roll = pilot_roll + wind_comp_roll ;
2019-02-28 05:03:23 -04:00
break ;
2014-04-10 03:55:54 -03:00
2019-04-22 23:14:35 -03:00
case RPMode : : BRAKE :
case RPMode : : BRAKE_READY_TO_LOITER :
2019-04-08 21:58:11 -03:00
// calculate brake.roll angle to counter-act velocity
update_brake_angle_from_velocity ( brake . roll , vel_right ) ;
2014-04-11 05:16:40 -03:00
2019-02-28 05:03:23 -04:00
// update braking time estimate
2019-04-08 21:58:11 -03:00
if ( ! brake . time_updated_roll ) {
2019-02-28 05:03:23 -04:00
// check if brake angle is increasing
2019-04-08 21:58:11 -03:00
if ( fabsf ( brake . roll ) > = brake . angle_max_roll ) {
brake . angle_max_roll = fabsf ( brake . roll ) ;
2014-04-11 05:16:40 -03:00
} else {
2019-02-28 05:03:23 -04:00
// braking angle has started decreasing so re-estimate braking time
2019-04-08 21:58:11 -03:00
brake . timeout_roll = 1 + ( uint16_t ) ( LOOP_RATE_FACTOR * 15L * ( int32_t ) ( fabsf ( brake . roll ) ) / ( 10L * ( int32_t ) g . poshold_brake_rate ) ) ; // the 1.2 (12/10) factor has to be tuned in flight, here it means 120% of the "normal" time.
brake . time_updated_roll = true ;
2014-04-11 05:16:40 -03:00
}
2019-02-28 05:03:23 -04:00
}
2014-04-11 05:16:40 -03:00
2019-02-28 05:03:23 -04:00
// if velocity is very low reduce braking time to 0.5seconds
2019-04-08 21:58:11 -03:00
if ( ( fabsf ( vel_right ) < = POSHOLD_SPEED_0 ) & & ( brake . timeout_roll > 50 * LOOP_RATE_FACTOR ) ) {
brake . timeout_roll = 50 * LOOP_RATE_FACTOR ;
2019-02-28 05:03:23 -04:00
}
2014-04-11 05:16:40 -03:00
2019-02-28 05:03:23 -04:00
// reduce braking timer
2019-04-08 21:58:11 -03:00
if ( brake . timeout_roll > 0 ) {
brake . timeout_roll - - ;
2019-02-28 05:03:23 -04:00
} else {
// indicate that we are ready to move to Loiter.
2019-04-22 23:14:35 -03:00
// Loiter will only actually be engaged once both roll_mode and pitch_mode are changed to RPMode::BRAKE_READY_TO_LOITER
2019-02-28 05:03:23 -04:00
// logic for engaging loiter is handled below the roll and pitch mode switch statements
2019-04-22 23:14:35 -03:00
roll_mode = RPMode : : BRAKE_READY_TO_LOITER ;
2019-02-28 05:03:23 -04:00
}
2014-04-04 11:14:07 -03:00
2019-02-28 05:03:23 -04:00
// final lean angle is braking angle + wind compensation angle
2019-04-08 21:58:11 -03:00
roll = brake . roll + wind_comp_roll ;
2014-04-11 05:16:40 -03:00
2019-02-28 05:03:23 -04:00
// check for pilot input
if ( ! is_zero ( target_roll ) ) {
// init transition to pilot override
2019-04-22 22:54:48 -03:00
roll_controller_to_pilot_override ( ) ;
2019-02-28 05:03:23 -04:00
}
break ;
2019-04-22 23:14:35 -03:00
case RPMode : : BRAKE_TO_LOITER :
case RPMode : : LOITER :
2019-02-28 05:03:23 -04:00
// these modes are combined roll-pitch modes and are handled below
break ;
2019-04-22 23:14:35 -03:00
case RPMode : : CONTROLLER_TO_PILOT_OVERRIDE :
2019-02-28 05:03:23 -04:00
// update pilot desired roll angle using latest radio input
// this filters the input so that it returns to zero no faster than the brake-rate
2019-04-22 22:54:48 -03:00
update_pilot_lean_angle ( pilot_roll , target_roll ) ;
2019-02-28 05:03:23 -04:00
// count-down loiter to pilot timer
2019-04-22 22:48:41 -03:00
if ( controller_to_pilot_timer_roll > 0 ) {
controller_to_pilot_timer_roll - - ;
2019-02-28 05:03:23 -04:00
} else {
// when timer runs out switch to full pilot override for next iteration
2019-04-22 23:14:35 -03:00
roll_mode = RPMode : : PILOT_OVERRIDE ;
2019-02-28 05:03:23 -04:00
}
2014-04-10 03:55:54 -03:00
2019-02-28 05:03:23 -04:00
// calculate controller_to_pilot mix ratio
2019-04-22 22:48:41 -03:00
controller_to_pilot_roll_mix = ( float ) controller_to_pilot_timer_roll / ( float ) POSHOLD_CONTROLLER_TO_PILOT_MIX_TIMER ;
2014-04-11 05:16:40 -03:00
2019-02-28 05:03:23 -04:00
// mix final loiter lean angle and pilot desired lean angles
2019-04-22 22:54:48 -03:00
roll = mix_controls ( controller_to_pilot_roll_mix , controller_final_roll , pilot_roll + wind_comp_roll ) ;
2019-02-28 05:03:23 -04:00
break ;
}
2014-04-11 05:16:40 -03:00
2019-02-28 05:03:23 -04:00
// Pitch state machine
// Each state (aka mode) is responsible for:
// 1. dealing with pilot input
// 2. calculating the final pitch output to the attitude contpitcher
// 3. checking if the state (aka mode) should be changed and if 'yes' perform any required initialisation for the new state
2019-04-22 22:48:41 -03:00
switch ( pitch_mode ) {
2019-02-28 05:03:23 -04:00
2019-04-22 23:14:35 -03:00
case RPMode : : PILOT_OVERRIDE :
2019-02-28 05:03:23 -04:00
// update pilot desired pitch angle using latest radio input
// this filters the input so that it returns to zero no faster than the brake-rate
2019-04-22 22:54:48 -03:00
update_pilot_lean_angle ( pilot_pitch , target_pitch ) ;
2019-02-28 05:03:23 -04:00
// switch to BRAKE mode for next iteration if no pilot input
2019-04-22 22:48:41 -03:00
if ( is_zero ( target_pitch ) & & ( fabsf ( pilot_pitch ) < 2 * g . poshold_brake_rate ) ) {
2019-02-28 05:03:23 -04:00
// initialise BRAKE mode
2019-04-22 23:14:35 -03:00
pitch_mode = RPMode : : BRAKE ; // set brake pitch mode
2019-04-08 21:58:11 -03:00
brake . pitch = 0.0f ; // initialise braking angle to zero
brake . angle_max_pitch = 0.0f ; // reset brake_angle_max so we can detect when vehicle begins to flatten out during braking
brake . timeout_pitch = POSHOLD_BRAKE_TIME_ESTIMATE_MAX ; // number of cycles the brake will be applied, updated during braking mode.
brake . time_updated_pitch = false ; // flag the braking time can be re-estimated
2019-02-28 05:03:23 -04:00
}
// final lean angle should be pilot input plus wind compensation
2019-04-22 22:48:41 -03:00
pitch = pilot_pitch + wind_comp_pitch ;
2019-02-28 05:03:23 -04:00
break ;
2019-04-22 23:14:35 -03:00
case RPMode : : BRAKE :
case RPMode : : BRAKE_READY_TO_LOITER :
2019-02-28 05:03:23 -04:00
// calculate brake_pitch angle to counter-act velocity
2019-04-08 21:58:11 -03:00
update_brake_angle_from_velocity ( brake . pitch , - vel_fw ) ;
2014-04-11 05:16:40 -03:00
2019-02-28 05:03:23 -04:00
// update braking time estimate
2019-04-08 21:58:11 -03:00
if ( ! brake . time_updated_pitch ) {
2019-02-28 05:03:23 -04:00
// check if brake angle is increasing
2019-04-08 21:58:11 -03:00
if ( fabsf ( brake . pitch ) > = brake . angle_max_pitch ) {
brake . angle_max_pitch = fabsf ( brake . pitch ) ;
2014-04-11 05:16:40 -03:00
} else {
2019-02-28 05:03:23 -04:00
// braking angle has started decreasing so re-estimate braking time
2019-04-08 21:58:11 -03:00
brake . timeout_pitch = 1 + ( uint16_t ) ( LOOP_RATE_FACTOR * 15L * ( int32_t ) ( fabsf ( brake . pitch ) ) / ( 10L * ( int32_t ) g . poshold_brake_rate ) ) ; // the 1.2 (12/10) factor has to be tuned in flight, here it means 120% of the "normal" time.
brake . time_updated_pitch = true ;
2014-04-11 05:16:40 -03:00
}
2019-02-28 05:03:23 -04:00
}
2014-04-11 05:16:40 -03:00
2019-02-28 05:03:23 -04:00
// if velocity is very low reduce braking time to 0.5seconds
2019-04-08 21:58:11 -03:00
if ( ( fabsf ( vel_fw ) < = POSHOLD_SPEED_0 ) & & ( brake . timeout_pitch > 50 * LOOP_RATE_FACTOR ) ) {
brake . timeout_pitch = 50 * LOOP_RATE_FACTOR ;
2019-02-28 05:03:23 -04:00
}
2014-04-23 00:39:46 -03:00
2019-02-28 05:03:23 -04:00
// reduce braking timer
2019-04-08 21:58:11 -03:00
if ( brake . timeout_pitch > 0 ) {
brake . timeout_pitch - - ;
2019-02-28 05:03:23 -04:00
} else {
// indicate that we are ready to move to Loiter.
2019-04-22 23:14:35 -03:00
// Loiter will only actually be engaged once both pitch_mode and pitch_mode are changed to RPMode::BRAKE_READY_TO_LOITER
2019-02-28 05:03:23 -04:00
// logic for engaging loiter is handled below the pitch and pitch mode switch statements
2019-04-22 23:14:35 -03:00
pitch_mode = RPMode : : BRAKE_READY_TO_LOITER ;
2019-02-28 05:03:23 -04:00
}
2014-04-10 03:55:54 -03:00
2019-02-28 05:03:23 -04:00
// final lean angle is braking angle + wind compensation angle
2019-04-08 21:58:11 -03:00
pitch = brake . pitch + wind_comp_pitch ;
2014-04-10 03:55:54 -03:00
2019-02-28 05:03:23 -04:00
// check for pilot input
if ( ! is_zero ( target_pitch ) ) {
// init transition to pilot override
2019-04-22 22:54:48 -03:00
pitch_controller_to_pilot_override ( ) ;
2019-02-28 05:03:23 -04:00
}
break ;
2019-04-22 23:14:35 -03:00
case RPMode : : BRAKE_TO_LOITER :
case RPMode : : LOITER :
2019-02-28 05:03:23 -04:00
// these modes are combined pitch-pitch modes and are handled below
break ;
2019-04-22 23:14:35 -03:00
case RPMode : : CONTROLLER_TO_PILOT_OVERRIDE :
2019-02-28 05:03:23 -04:00
// update pilot desired pitch angle using latest radio input
// this filters the input so that it returns to zero no faster than the brake-rate
2019-04-22 22:54:48 -03:00
update_pilot_lean_angle ( pilot_pitch , target_pitch ) ;
2019-02-28 05:03:23 -04:00
// count-down loiter to pilot timer
2019-04-22 22:48:41 -03:00
if ( controller_to_pilot_timer_pitch > 0 ) {
controller_to_pilot_timer_pitch - - ;
2019-02-28 05:03:23 -04:00
} else {
// when timer runs out switch to full pilot override for next iteration
2019-04-22 23:14:35 -03:00
pitch_mode = RPMode : : PILOT_OVERRIDE ;
2019-02-28 05:03:23 -04:00
}
2014-04-11 05:16:40 -03:00
2019-02-28 05:03:23 -04:00
// calculate controller_to_pilot mix ratio
2019-04-22 22:48:41 -03:00
controller_to_pilot_pitch_mix = ( float ) controller_to_pilot_timer_pitch / ( float ) POSHOLD_CONTROLLER_TO_PILOT_MIX_TIMER ;
2019-02-28 05:03:23 -04:00
// mix final loiter lean angle and pilot desired lean angles
2019-04-22 22:54:48 -03:00
pitch = mix_controls ( controller_to_pilot_pitch_mix , controller_final_pitch , pilot_pitch + wind_comp_pitch ) ;
2019-02-28 05:03:23 -04:00
break ;
}
//
2019-04-22 23:14:35 -03:00
// Shared roll & pitch states (RPMode::BRAKE_TO_LOITER and RPMode::LOITER)
2019-02-28 05:03:23 -04:00
//
// switch into LOITER mode when both roll and pitch are ready
2019-04-22 23:14:35 -03:00
if ( roll_mode = = RPMode : : BRAKE_READY_TO_LOITER & & pitch_mode = = RPMode : : BRAKE_READY_TO_LOITER ) {
roll_mode = RPMode : : BRAKE_TO_LOITER ;
pitch_mode = RPMode : : BRAKE_TO_LOITER ;
2019-04-08 21:58:11 -03:00
brake . to_loiter_timer = POSHOLD_BRAKE_TO_LOITER_TIMER ;
2019-02-28 05:03:23 -04:00
// init loiter controller
loiter_nav - > init_target ( inertial_nav . get_position ( ) ) ;
// set delay to start of wind compensation estimate updates
2019-04-22 22:48:41 -03:00
wind_comp_start_timer = POSHOLD_WIND_COMP_START_TIMER ;
2019-02-28 05:03:23 -04:00
}
// roll-mode is used as the combined roll+pitch mode when in BRAKE_TO_LOITER or LOITER modes
2019-04-22 23:14:35 -03:00
if ( roll_mode = = RPMode : : BRAKE_TO_LOITER | | roll_mode = = RPMode : : LOITER ) {
2019-02-28 05:03:23 -04:00
// force pitch mode to be same as roll_mode just to keep it consistent (it's not actually used in these states)
2019-04-22 22:48:41 -03:00
pitch_mode = roll_mode ;
2019-02-28 05:03:23 -04:00
// handle combined roll+pitch mode
2019-04-22 22:48:41 -03:00
switch ( roll_mode ) {
2019-04-08 21:58:11 -03:00
case RPMode : : BRAKE_TO_LOITER : {
2019-02-28 05:03:23 -04:00
// reduce brake_to_loiter timer
2019-04-08 21:58:11 -03:00
if ( brake . to_loiter_timer > 0 ) {
brake . to_loiter_timer - - ;
2014-04-04 11:14:07 -03:00
} else {
2019-02-28 05:03:23 -04:00
// progress to full loiter on next iteration
2019-04-22 23:14:35 -03:00
roll_mode = RPMode : : LOITER ;
pitch_mode = RPMode : : LOITER ;
2014-04-04 11:14:07 -03:00
}
2014-04-10 03:55:54 -03:00
2019-04-08 21:58:11 -03:00
// mix of brake and loiter controls. 0 = fully brake
// controls, 1 = fully loiter controls
const float brake_to_loiter_mix = ( float ) brake . to_loiter_timer / ( float ) POSHOLD_BRAKE_TO_LOITER_TIMER ;
2014-04-11 05:16:40 -03:00
2019-04-08 21:58:11 -03:00
// calculate brake.roll and pitch angles to counter-act velocity
update_brake_angle_from_velocity ( brake . roll , vel_right ) ;
update_brake_angle_from_velocity ( brake . pitch , - vel_fw ) ;
2014-04-05 11:20:39 -03:00
2019-02-28 05:03:23 -04:00
// run loiter controller
2021-03-15 15:43:45 -03:00
loiter_nav - > update ( false ) ;
2016-01-17 23:53:16 -04:00
2019-02-28 05:03:23 -04:00
// calculate final roll and pitch output by mixing loiter and brake controls
2019-04-08 21:58:11 -03:00
roll = mix_controls ( brake_to_loiter_mix , brake . roll + wind_comp_roll , loiter_nav - > get_roll ( ) ) ;
pitch = mix_controls ( brake_to_loiter_mix , brake . pitch + wind_comp_pitch , loiter_nav - > get_pitch ( ) ) ;
2014-04-11 05:16:40 -03:00
2019-02-28 05:03:23 -04:00
// check for pilot input
if ( ! is_zero ( target_roll ) | | ! is_zero ( target_pitch ) ) {
// if roll input switch to pilot override for roll
if ( ! is_zero ( target_roll ) ) {
// init transition to pilot override
2019-04-22 22:54:48 -03:00
roll_controller_to_pilot_override ( ) ;
2019-02-28 05:03:23 -04:00
// switch pitch-mode to brake (but ready to go back to loiter anytime)
2019-04-08 21:58:11 -03:00
// no need to reset brake.pitch here as wind comp has not been updated since last brake.pitch computation
2019-04-22 23:14:35 -03:00
pitch_mode = RPMode : : BRAKE_READY_TO_LOITER ;
2014-04-11 05:16:40 -03:00
}
2019-02-28 05:03:23 -04:00
// if pitch input switch to pilot override for pitch
if ( ! is_zero ( target_pitch ) ) {
// init transition to pilot override
2019-04-22 22:54:48 -03:00
pitch_controller_to_pilot_override ( ) ;
2019-02-28 05:03:23 -04:00
if ( is_zero ( target_roll ) ) {
// switch roll-mode to brake (but ready to go back to loiter anytime)
2019-04-08 21:58:11 -03:00
// no need to reset brake.roll here as wind comp has not been updated since last brake.roll computation
2019-04-22 23:14:35 -03:00
roll_mode = RPMode : : BRAKE_READY_TO_LOITER ;
2014-04-11 05:16:40 -03:00
}
}
2019-02-28 05:03:23 -04:00
}
break ;
2019-04-08 21:58:11 -03:00
}
2019-04-22 23:14:35 -03:00
case RPMode : : LOITER :
2019-02-28 05:03:23 -04:00
// run loiter controller
2021-03-15 15:43:45 -03:00
loiter_nav - > update ( false ) ;
2019-02-28 05:03:23 -04:00
// set roll angle based on loiter controller outputs
2019-04-22 22:48:41 -03:00
roll = loiter_nav - > get_roll ( ) ;
pitch = loiter_nav - > get_pitch ( ) ;
2019-02-28 05:03:23 -04:00
// update wind compensation estimate
2019-04-22 22:54:48 -03:00
update_wind_comp_estimate ( ) ;
2019-02-28 05:03:23 -04:00
// check for pilot input
if ( ! is_zero ( target_roll ) | | ! is_zero ( target_pitch ) ) {
// if roll input switch to pilot override for roll
if ( ! is_zero ( target_roll ) ) {
// init transition to pilot override
2019-04-22 22:54:48 -03:00
roll_controller_to_pilot_override ( ) ;
2019-02-28 05:03:23 -04:00
// switch pitch-mode to brake (but ready to go back to loiter anytime)
2019-04-22 23:14:35 -03:00
pitch_mode = RPMode : : BRAKE_READY_TO_LOITER ;
2019-04-08 21:58:11 -03:00
// reset brake.pitch because wind_comp is now different and should give the compensation of the whole previous loiter angle
brake . pitch = 0.0f ;
2019-02-28 05:03:23 -04:00
}
// if pitch input switch to pilot override for pitch
if ( ! is_zero ( target_pitch ) ) {
// init transition to pilot override
2019-04-22 22:54:48 -03:00
pitch_controller_to_pilot_override ( ) ;
2019-02-28 05:03:23 -04:00
// if roll not overriden switch roll-mode to brake (but be ready to go back to loiter any time)
if ( is_zero ( target_roll ) ) {
2019-04-22 23:14:35 -03:00
roll_mode = RPMode : : BRAKE_READY_TO_LOITER ;
2019-04-08 21:58:11 -03:00
brake . roll = 0.0f ;
2014-04-11 05:16:40 -03:00
}
2019-03-25 20:57:55 -03:00
// if roll not overridden switch roll-mode to brake (but be ready to go back to loiter any time)
2014-04-11 05:16:40 -03:00
}
2019-02-28 05:03:23 -04:00
}
break ;
2014-04-11 05:16:40 -03:00
2019-02-28 05:03:23 -04:00
default :
// do nothing for uncombined roll and pitch modes
break ;
2014-04-11 05:16:40 -03:00
}
2019-02-28 05:03:23 -04:00
}
2014-04-05 11:20:39 -03:00
2019-02-28 05:03:23 -04:00
// constrain target pitch/roll angles
float angle_max = copter . aparm . angle_max ;
2019-07-07 10:13:43 -03:00
roll = constrain_float ( roll , - angle_max , angle_max ) ;
pitch = constrain_float ( pitch , - angle_max , angle_max ) ;
2014-04-04 11:14:07 -03:00
2019-02-28 05:03:23 -04:00
// call attitude controller
2019-04-22 22:48:41 -03:00
attitude_control - > input_euler_angle_roll_pitch_euler_rate_yaw ( roll , pitch , target_yaw_rate ) ;
2017-01-05 02:24:02 -04:00
2021-05-19 11:07:38 -03:00
// run the vertical position controller and set output throttle
2019-02-28 05:03:23 -04:00
pos_control - > update_z_controller ( ) ;
2014-04-10 03:55:54 -03:00
}
2014-07-11 02:06:53 -03:00
// poshold_update_pilot_lean_angle - update the pilot's filtered lean angle with the latest raw input received
2019-05-09 23:18:49 -03:00
void ModePosHold : : update_pilot_lean_angle ( float & lean_angle_filtered , float & lean_angle_raw )
2014-04-11 05:16:40 -03:00
{
// if raw input is large or reversing the vehicle's lean angle immediately set the fitlered angle to the new raw angle
2015-05-15 12:53:29 -03:00
if ( ( lean_angle_filtered > 0 & & lean_angle_raw < 0 ) | | ( lean_angle_filtered < 0 & & lean_angle_raw > 0 ) | | ( fabsf ( lean_angle_raw ) > POSHOLD_STICK_RELEASE_SMOOTH_ANGLE ) ) {
2014-04-11 05:16:40 -03:00
lean_angle_filtered = lean_angle_raw ;
} else {
// lean_angle_raw must be pulling lean_angle_filtered towards zero, smooth the decrease
if ( lean_angle_filtered > 0 ) {
2014-04-23 00:32:08 -03:00
// reduce the filtered lean angle at 5% or the brake rate (whichever is faster).
2015-11-27 13:11:58 -04:00
lean_angle_filtered - = MAX ( ( float ) lean_angle_filtered * POSHOLD_SMOOTH_RATE_FACTOR , MAX ( 1 , g . poshold_brake_rate / LOOP_RATE_FACTOR ) ) ;
2014-04-11 05:16:40 -03:00
// do not let the filtered angle fall below the pilot's input lean angle.
// the above line pulls the filtered angle down and the below line acts as a catch
2015-11-27 13:11:58 -04:00
lean_angle_filtered = MAX ( lean_angle_filtered , lean_angle_raw ) ;
2014-04-11 05:16:40 -03:00
} else {
2015-11-27 13:11:58 -04:00
lean_angle_filtered + = MAX ( - ( float ) lean_angle_filtered * POSHOLD_SMOOTH_RATE_FACTOR , MAX ( 1 , g . poshold_brake_rate / LOOP_RATE_FACTOR ) ) ;
lean_angle_filtered = MIN ( lean_angle_filtered , lean_angle_raw ) ;
2014-04-11 05:16:40 -03:00
}
}
}
2019-04-22 22:54:48 -03:00
// mix_controls - mixes two controls based on the mix_ratio
2014-04-13 01:47:47 -03:00
// mix_ratio of 1 = use first_control completely, 0 = use second_control completely, 0.5 = mix evenly
2019-07-07 10:13:43 -03:00
float ModePosHold : : mix_controls ( float mix_ratio , float first_control , float second_control )
2014-04-13 01:47:47 -03:00
{
mix_ratio = constrain_float ( mix_ratio , 0.0f , 1.0f ) ;
2019-07-07 10:13:43 -03:00
return mix_ratio * first_control + ( 1.0f - mix_ratio ) * second_control ;
2014-04-13 01:47:47 -03:00
}
2019-04-22 22:54:48 -03:00
// update_brake_angle_from_velocity - updates the brake_angle based on the vehicle's velocity and brake_gain
2014-07-11 02:06:53 -03:00
// brake_angle is slewed with the wpnav.poshold_brake_rate and constrained by the wpnav.poshold_braking_angle_max
2014-04-11 05:16:40 -03:00
// velocity is assumed to be in the same direction as lean angle so for pitch you should provide the velocity backwards (i.e. -ve forward velocity)
2019-07-07 10:13:43 -03:00
void ModePosHold : : update_brake_angle_from_velocity ( float & brake_angle , float velocity )
2014-04-11 05:16:40 -03:00
{
float lean_angle ;
2019-07-07 10:13:43 -03:00
float brake_rate = g . poshold_brake_rate ;
2014-04-13 02:33:31 -03:00
2019-07-07 10:13:43 -03:00
brake_rate / = 4.0f ;
if ( brake_rate < = 1.0f ) {
brake_rate = 1.0f ;
2014-04-13 02:33:31 -03:00
}
2014-04-11 05:16:40 -03:00
// calculate velocity-only based lean angle
if ( velocity > = 0 ) {
2019-04-08 21:58:11 -03:00
lean_angle = - brake . gain * velocity * ( 1.0f + 500.0f / ( velocity + 60.0f ) ) ;
2014-04-11 05:16:40 -03:00
} else {
2019-04-08 21:58:11 -03:00
lean_angle = - brake . gain * velocity * ( 1.0f + 500.0f / ( - velocity + 60.0f ) ) ;
2014-04-11 05:16:40 -03:00
}
// do not let lean_angle be too far from brake_angle
2019-07-07 10:13:43 -03:00
brake_angle = constrain_float ( lean_angle , brake_angle - brake_rate , brake_angle + brake_rate ) ;
2014-04-11 05:16:40 -03:00
// constrain final brake_angle
2019-07-07 10:13:43 -03:00
brake_angle = constrain_float ( brake_angle , - ( float ) g . poshold_brake_angle_max , ( float ) g . poshold_brake_angle_max ) ;
2014-04-11 05:16:40 -03:00
}
2020-11-05 21:42:17 -04:00
// initialise wind compensation estimate back to zero
void ModePosHold : : init_wind_comp_estimate ( )
{
wind_comp_ef . zero ( ) ;
wind_comp_timer = 0 ;
wind_comp_roll = 0.0f ;
wind_comp_pitch = 0.0f ;
}
2019-04-22 22:54:48 -03:00
// update_wind_comp_estimate - updates wind compensation estimate
2014-04-10 03:55:54 -03:00
// should be called at the maximum loop rate when loiter is engaged
2019-05-09 23:18:49 -03:00
void ModePosHold : : update_wind_comp_estimate ( )
2014-04-10 03:55:54 -03:00
{
2014-04-13 02:33:31 -03:00
// check wind estimate start has not been delayed
2019-04-22 22:48:41 -03:00
if ( wind_comp_start_timer > 0 ) {
wind_comp_start_timer - - ;
2014-04-13 02:33:31 -03:00
return ;
}
// check horizontal velocity is low
2017-10-13 00:42:09 -03:00
if ( inertial_nav . get_speed_xy ( ) > POSHOLD_WIND_COMP_ESTIMATE_SPEED_MAX ) {
2014-04-10 03:55:54 -03:00
return ;
}
2014-04-11 05:16:40 -03:00
// get position controller accel target
2021-05-11 01:42:02 -03:00
const Vector3f & accel_target = pos_control - > get_accel_target_cmss ( ) ;
2014-04-11 05:16:40 -03:00
2014-04-10 03:55:54 -03:00
// update wind compensation in earth-frame lean angles
2019-04-22 22:48:41 -03:00
if ( is_zero ( wind_comp_ef . x ) ) {
2014-04-10 03:55:54 -03:00
// if wind compensation has not been initialised set it immediately to the pos controller's desired accel in north direction
2019-04-22 22:48:41 -03:00
wind_comp_ef . x = accel_target . x ;
2014-04-10 03:55:54 -03:00
} else {
// low pass filter the position controller's lean angle output
2019-04-22 22:48:41 -03:00
wind_comp_ef . x = ( 1.0f - TC_WIND_COMP ) * wind_comp_ef . x + TC_WIND_COMP * accel_target . x ;
2014-04-10 03:55:54 -03:00
}
2019-04-22 22:48:41 -03:00
if ( is_zero ( wind_comp_ef . y ) ) {
2014-04-10 03:55:54 -03:00
// if wind compensation has not been initialised set it immediately to the pos controller's desired accel in north direction
2019-04-22 22:48:41 -03:00
wind_comp_ef . y = accel_target . y ;
2014-04-10 03:55:54 -03:00
} else {
// low pass filter the position controller's lean angle output
2019-04-22 22:48:41 -03:00
wind_comp_ef . y = ( 1.0f - TC_WIND_COMP ) * wind_comp_ef . y + TC_WIND_COMP * accel_target . y ;
2014-04-10 03:55:54 -03:00
}
2020-09-08 09:12:53 -03:00
// limit acceleration
const float accel_lim_cmss = tanf ( radians ( POSHOLD_WIND_COMP_LEAN_PCT_MAX * copter . aparm . angle_max * 0.01f ) ) * 981.0f ;
const float wind_comp_ef_len = wind_comp_ef . length ( ) ;
if ( ! is_zero ( accel_lim_cmss ) & & ( wind_comp_ef_len > accel_lim_cmss ) ) {
wind_comp_ef * = accel_lim_cmss / wind_comp_ef_len ;
}
2014-04-10 03:55:54 -03:00
}
2019-04-22 22:54:48 -03:00
// get_wind_comp_lean_angles - retrieve wind compensation angles in body frame roll and pitch angles
2014-04-16 09:18:01 -03:00
// should be called at the maximum loop rate
2019-07-07 10:13:43 -03:00
void ModePosHold : : get_wind_comp_lean_angles ( float & roll_angle , float & pitch_angle )
2014-04-10 03:55:54 -03:00
{
2014-04-16 09:18:01 -03:00
// reduce rate to 10hz
2019-04-22 22:48:41 -03:00
wind_comp_timer + + ;
if ( wind_comp_timer < POSHOLD_WIND_COMP_TIMER_10HZ ) {
2014-04-16 09:18:01 -03:00
return ;
}
2019-04-22 22:48:41 -03:00
wind_comp_timer = 0 ;
2014-04-16 09:18:01 -03:00
2014-04-10 03:55:54 -03:00
// convert earth frame desired accelerations to body frame roll and pitch lean angles
2020-03-15 00:26:05 -03:00
roll_angle = atanf ( ( - wind_comp_ef . x * ahrs . sin_yaw ( ) + wind_comp_ef . y * ahrs . cos_yaw ( ) ) / ( GRAVITY_MSS * 100 ) ) * ( 18000.0f / M_PI ) ;
pitch_angle = atanf ( - ( wind_comp_ef . x * ahrs . cos_yaw ( ) + wind_comp_ef . y * ahrs . sin_yaw ( ) ) / ( GRAVITY_MSS * 100 ) ) * ( 18000.0f / M_PI ) ;
2014-04-04 11:14:07 -03:00
}
2014-04-23 00:39:46 -03:00
2019-04-22 22:54:48 -03:00
// roll_controller_to_pilot_override - initialises transition from a controller submode (brake or loiter) to a pilot override on roll axis
2019-05-09 23:18:49 -03:00
void ModePosHold : : roll_controller_to_pilot_override ( )
2014-04-23 00:39:46 -03:00
{
2019-04-22 23:14:35 -03:00
roll_mode = RPMode : : CONTROLLER_TO_PILOT_OVERRIDE ;
2019-04-22 22:48:41 -03:00
controller_to_pilot_timer_roll = POSHOLD_CONTROLLER_TO_PILOT_MIX_TIMER ;
2014-07-11 02:06:53 -03:00
// initialise pilot_roll to 0, wind_comp will be updated to compensate and poshold_update_pilot_lean_angle function shall not smooth this transition at next iteration. so 0 is the right value
2019-07-07 10:13:43 -03:00
pilot_roll = 0.0f ;
2014-04-23 00:39:46 -03:00
// store final controller output for mixing with pilot input
2019-04-22 22:48:41 -03:00
controller_final_roll = roll ;
2014-04-23 00:39:46 -03:00
}
2019-04-22 22:54:48 -03:00
// pitch_controller_to_pilot_override - initialises transition from a controller submode (brake or loiter) to a pilot override on roll axis
2019-05-09 23:18:49 -03:00
void ModePosHold : : pitch_controller_to_pilot_override ( )
2014-04-23 00:39:46 -03:00
{
2019-04-22 23:14:35 -03:00
pitch_mode = RPMode : : CONTROLLER_TO_PILOT_OVERRIDE ;
2019-04-22 22:48:41 -03:00
controller_to_pilot_timer_pitch = POSHOLD_CONTROLLER_TO_PILOT_MIX_TIMER ;
2019-04-22 22:54:48 -03:00
// initialise pilot_pitch to 0, wind_comp will be updated to compensate and update_pilot_lean_angle function shall not smooth this transition at next iteration. so 0 is the right value
2019-07-07 10:13:43 -03:00
pilot_pitch = 0.0f ;
2014-04-23 00:39:46 -03:00
// store final loiter outputs for mixing with pilot input
2019-04-22 22:48:41 -03:00
controller_final_pitch = pitch ;
2014-04-23 00:39:46 -03:00
}
2019-02-25 10:32:52 -04:00
# endif