floatbrake_gain;// gain used during conversion of vehicle's velocity to lean angle during braking (calculated from brake_rate)
floatbrake_gain;// gain used during conversion of vehicle's velocity to lean angle during braking (calculated from brake_rate)
int16_tbrake_roll;// target roll angle during braking periods
floatbrake_roll;// target roll angle during braking periods
int16_tbrake_pitch;// target pitch angle during braking periods
floatbrake_pitch;// target pitch angle during braking periods
int16_tbrake_timeout_roll;// number of cycles allowed for the braking to complete, this timeout will be updated at half-braking
int16_tbrake_timeout_roll;// number of cycles allowed for the braking to complete, this timeout will be updated at half-braking
int16_tbrake_timeout_pitch;// number of cycles allowed for the braking to complete, this timeout will be updated at half-braking
int16_tbrake_timeout_pitch;// number of cycles allowed for the braking to complete, this timeout will be updated at half-braking
int16_tbrake_angle_max_roll;// maximum lean angle achieved during braking. Used to determine when the vehicle has begun to flatten out so that we can re-estimate the braking time
floatbrake_angle_max_roll;// maximum lean angle achieved during braking. Used to determine when the vehicle has begun to flatten out so that we can re-estimate the braking time
int16_tbrake_angle_max_pitch;// maximum lean angle achieved during braking Used to determine when the vehicle has begun to flatten out so that we can re-estimate the braking time
floatbrake_angle_max_pitch;// maximum lean angle achieved during braking Used to determine when the vehicle has begun to flatten out so that we can re-estimate the braking time
int16_tbrake_to_loiter_timer;// cycles to mix brake and loiter controls in POSHOLD_BRAKE_TO_LOITER
int16_tbrake_to_loiter_timer;// cycles to mix brake and loiter controls in POSHOLD_BRAKE_TO_LOITER
// loiter related variables
// loiter related variables
int16_tcontroller_to_pilot_timer_roll;// cycles to mix controller and pilot controls in POSHOLD_CONTROLLER_TO_PILOT
int16_tcontroller_to_pilot_timer_roll;// cycles to mix controller and pilot controls in POSHOLD_CONTROLLER_TO_PILOT
int16_tcontroller_to_pilot_timer_pitch;// cycles to mix controller and pilot controls in POSHOLD_CONTROLLER_TO_PILOT
int16_tcontroller_to_pilot_timer_pitch;// cycles to mix controller and pilot controls in POSHOLD_CONTROLLER_TO_PILOT
int16_tcontroller_final_roll;// final roll angle from controller as we exit brake or loiter mode (used for mixing with pilot input)
floatcontroller_final_roll;// final roll angle from controller as we exit brake or loiter mode (used for mixing with pilot input)
int16_tcontroller_final_pitch;// final pitch angle from controller as we exit brake or loiter mode (used for mixing with pilot input)
floatcontroller_final_pitch;// final pitch angle from controller as we exit brake or loiter mode (used for mixing with pilot input)
// wind compensation related variables
// wind compensation related variables
Vector2fwind_comp_ef;// wind compensation in earth frame, filtered lean angles from position controller
Vector2fwind_comp_ef;// wind compensation in earth frame, filtered lean angles from position controller
int16_twind_comp_roll;// roll angle to compensate for wind
floatwind_comp_roll;// roll angle to compensate for wind
int16_twind_comp_pitch;// pitch angle to compensate for wind
floatwind_comp_pitch;// pitch angle to compensate for wind
uint16_twind_comp_start_timer;// counter to delay start of wind compensation for a short time after loiter is engaged
uint16_twind_comp_start_timer;// counter to delay start of wind compensation for a short time after loiter is engaged
int8_twind_comp_timer;// counter to reduce wind comp roll/pitch lean angle calcs to 10hz
int8_twind_comp_timer;// counter to reduce wind comp roll/pitch lean angle calcs to 10hz
// final output
// final output
int16_troll;// final roll angle sent to attitude controller
floatroll;// final roll angle sent to attitude controller
int16_tpitch;// final pitch angle sent to attitude controller
floatpitch;// final pitch angle sent to attitude controller
brake_roll=0.0f;// initialise braking angle to zero
brake_angle_max_roll=0;// reset brake_angle_max so we can detect when vehicle begins to flatten out during braking
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_timeout_roll=POSHOLD_BRAKE_TIME_ESTIMATE_MAX;// number of cycles the brake will be applied, updated during braking mode.
braking_time_updated_roll=false;// flag the braking time can be re-estimated
braking_time_updated_roll=false;// flag the braking time can be re-estimated
}
}
@ -229,11 +229,11 @@ void ModePosHold::run()
// update braking time estimate
// update braking time estimate
if(!braking_time_updated_roll){
if(!braking_time_updated_roll){
// check if brake angle is increasing
// check if brake angle is increasing
if(abs(brake_roll)>=brake_angle_max_roll){
if(fabsf(brake_roll)>=brake_angle_max_roll){
brake_angle_max_roll=abs(brake_roll);
brake_angle_max_roll=fabsf(brake_roll);
}else{
}else{
// braking angle has started decreasing so re-estimate braking time
// braking angle has started decreasing so re-estimate braking time
brake_timeout_roll=1+(uint16_t)(LOOP_RATE_FACTOR*15L*(int32_t)(abs(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_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_pitch=0.0f;// initialise braking angle to zero
brake_angle_max_pitch=0;// reset brake_angle_max so we can detect when vehicle begins to flatten out during braking
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_timeout_pitch=POSHOLD_BRAKE_TIME_ESTIMATE_MAX;// number of cycles the brake will be applied, updated during braking mode.
braking_time_updated_pitch=false;// flag the braking time can be re-estimated
braking_time_updated_pitch=false;// flag the braking time can be re-estimated
}
}
@ -323,11 +323,11 @@ void ModePosHold::run()
// update braking time estimate
// update braking time estimate
if(!braking_time_updated_pitch){
if(!braking_time_updated_pitch){
// check if brake angle is increasing
// check if brake angle is increasing
if(abs(brake_pitch)>=brake_angle_max_pitch){
if(fabsf(brake_pitch)>=brake_angle_max_pitch){
brake_angle_max_pitch=abs(brake_pitch);
brake_angle_max_pitch=fabsf(brake_pitch);
}else{
}else{
// braking angle has started decreasing so re-estimate braking time
// braking angle has started decreasing so re-estimate braking time
brake_timeout_pitch=1+(uint16_t)(LOOP_RATE_FACTOR*15L*(int32_t)(abs(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_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.
braking_time_updated_pitch=true;
braking_time_updated_pitch=true;
}
}
}
}
@ -473,7 +473,7 @@ void ModePosHold::run()
// switch pitch-mode to brake (but ready to go back to loiter anytime)
// switch pitch-mode to brake (but ready to go back to loiter anytime)
pitch_mode=RPMode::BRAKE_READY_TO_LOITER;
pitch_mode=RPMode::BRAKE_READY_TO_LOITER;
// reset brake_pitch because wind_comp is now different and should give the compensation of the whole previous loiter angle
// reset brake_pitch because wind_comp is now different and should give the compensation of the whole previous loiter angle
brake_pitch=0;
brake_pitch=0.0f;
}
}
// if pitch input switch to pilot override for pitch
// if pitch input switch to pilot override for pitch
if(!is_zero(target_pitch)){
if(!is_zero(target_pitch)){
@ -482,7 +482,7 @@ void ModePosHold::run()
// if roll not overriden switch roll-mode to brake (but be ready to go back to loiter any time)
// if roll not overriden switch roll-mode to brake (but be ready to go back to loiter any time)
if(is_zero(target_roll)){
if(is_zero(target_roll)){
roll_mode=RPMode::BRAKE_READY_TO_LOITER;
roll_mode=RPMode::BRAKE_READY_TO_LOITER;
brake_roll=0;
brake_roll=0.0f;
}
}
// if roll not overridden switch roll-mode to brake (but be ready to go back to loiter any time)
// if roll not overridden switch roll-mode to brake (but be ready to go back to loiter any time)
// 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
// 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
pilot_roll=0;
pilot_roll=0.0f;
// store final controller output for mixing with pilot input
// store final controller output for mixing with pilot input
// 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
// 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
pilot_pitch=0;
pilot_pitch=0.0f;
// store final loiter outputs for mixing with pilot input
// store final loiter outputs for mixing with pilot input