Copter: move poshold state variables into mode object

This was simply moving the members from the poshold struct into the
class then mechanically removing poshold. from in front of the member
access.
This commit is contained in:
Peter Barker 2019-04-23 11:48:41 +10:00 committed by Randy Mackay
parent 9f9531a790
commit f57a9f11cc
2 changed files with 180 additions and 182 deletions

View File

@ -909,6 +909,60 @@ private:
void poshold_roll_controller_to_pilot_override(); void poshold_roll_controller_to_pilot_override();
void poshold_pitch_controller_to_pilot_override(); void poshold_pitch_controller_to_pilot_override();
// PosHold states
enum PosHoldModeState {
PosHold_MotorStopped,
PosHold_Takeoff,
PosHold_Flying,
PosHold_Landed
};
// mission state enumeration
enum poshold_rp_mode {
POSHOLD_PILOT_OVERRIDE=0, // pilot is controlling this axis (i.e. roll or pitch)
POSHOLD_BRAKE, // this axis is braking towards zero
POSHOLD_BRAKE_READY_TO_LOITER, // this axis has completed braking and is ready to enter loiter mode (both modes must be this value before moving to next stage)
POSHOLD_BRAKE_TO_LOITER, // both vehicle's axis (roll and pitch) are transitioning from braking to loiter mode (braking and loiter controls are mixed)
POSHOLD_LOITER, // both vehicle axis are holding position
POSHOLD_CONTROLLER_TO_PILOT_OVERRIDE // pilot has input controls on this axis and this axis is transitioning to pilot override (other axis will transition to brake if no pilot input)
};
poshold_rp_mode roll_mode : 3; // roll mode: pilot override, brake or loiter
poshold_rp_mode pitch_mode : 3; // pitch mode: pilot override, brake or loiter
uint8_t braking_time_updated_roll : 1; // true once we have re-estimated the braking time. This is done once as the vehicle begins to flatten out after braking
uint8_t braking_time_updated_pitch : 1; // true once we have re-estimated the braking time. This is done once as the vehicle begins to flatten out after braking
// pilot input related variables
float pilot_roll; // pilot requested roll angle (filtered to slow returns to zero)
float pilot_pitch; // pilot requested roll angle (filtered to slow returns to zero)
// braking related variables
float brake_gain; // gain used during conversion of vehicle's velocity to lean angle during braking (calculated from brake_rate)
int16_t brake_roll; // target roll angle during braking periods
int16_t brake_pitch; // target pitch angle during braking periods
int16_t brake_timeout_roll; // number of cycles allowed for the braking to complete, this timeout will be updated at half-braking
int16_t brake_timeout_pitch; // number of cycles allowed for the braking to complete, this timeout will be updated at half-braking
int16_t brake_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_t brake_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_t brake_to_loiter_timer; // cycles to mix brake and loiter controls in POSHOLD_BRAKE_TO_LOITER
// loiter related variables
int16_t controller_to_pilot_timer_roll; // cycles to mix controller and pilot controls in POSHOLD_CONTROLLER_TO_PILOT
int16_t controller_to_pilot_timer_pitch; // cycles to mix controller and pilot controls in POSHOLD_CONTROLLER_TO_PILOT
int16_t controller_final_roll; // final roll angle from controller as we exit brake or loiter mode (used for mixing with pilot input)
int16_t controller_final_pitch; // final pitch angle from controller as we exit brake or loiter mode (used for mixing with pilot input)
// wind compensation related variables
Vector2f wind_comp_ef; // wind compensation in earth frame, filtered lean angles from position controller
int16_t wind_comp_roll; // roll angle to compensate for wind
int16_t wind_comp_pitch; // pitch angle to compensate for wind
uint16_t wind_comp_start_timer; // counter to delay start of wind compensation for a short time after loiter is engaged
int8_t wind_comp_timer; // counter to reduce wind comp roll/pitch lean angle calcs to 10hz
// final output
int16_t roll; // final roll angle sent to attitude controller
int16_t pitch; // final pitch angle sent to attitude controller
}; };

View File

@ -23,62 +23,6 @@
#define POSHOLD_STICK_RELEASE_SMOOTH_ANGLE 1800 // max angle required (in centi-degrees) after which the smooth stick release effect is applied #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 #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
// PosHold states
enum PosHoldModeState {
PosHold_MotorStopped,
PosHold_Takeoff,
PosHold_Flying,
PosHold_Landed
};
// mission state enumeration
enum poshold_rp_mode {
POSHOLD_PILOT_OVERRIDE=0, // pilot is controlling this axis (i.e. roll or pitch)
POSHOLD_BRAKE, // this axis is braking towards zero
POSHOLD_BRAKE_READY_TO_LOITER, // this axis has completed braking and is ready to enter loiter mode (both modes must be this value before moving to next stage)
POSHOLD_BRAKE_TO_LOITER, // both vehicle's axis (roll and pitch) are transitioning from braking to loiter mode (braking and loiter controls are mixed)
POSHOLD_LOITER, // both vehicle axis are holding position
POSHOLD_CONTROLLER_TO_PILOT_OVERRIDE // pilot has input controls on this axis and this axis is transitioning to pilot override (other axis will transition to brake if no pilot input)
};
static struct {
poshold_rp_mode roll_mode : 3; // roll mode: pilot override, brake or loiter
poshold_rp_mode pitch_mode : 3; // pitch mode: pilot override, brake or loiter
uint8_t braking_time_updated_roll : 1; // true once we have re-estimated the braking time. This is done once as the vehicle begins to flatten out after braking
uint8_t braking_time_updated_pitch : 1; // true once we have re-estimated the braking time. This is done once as the vehicle begins to flatten out after braking
// pilot input related variables
float pilot_roll; // pilot requested roll angle (filtered to slow returns to zero)
float pilot_pitch; // pilot requested roll angle (filtered to slow returns to zero)
// braking related variables
float brake_gain; // gain used during conversion of vehicle's velocity to lean angle during braking (calculated from brake_rate)
int16_t brake_roll; // target roll angle during braking periods
int16_t brake_pitch; // target pitch angle during braking periods
int16_t brake_timeout_roll; // number of cycles allowed for the braking to complete, this timeout will be updated at half-braking
int16_t brake_timeout_pitch; // number of cycles allowed for the braking to complete, this timeout will be updated at half-braking
int16_t brake_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_t brake_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_t brake_to_loiter_timer; // cycles to mix brake and loiter controls in POSHOLD_BRAKE_TO_LOITER
// loiter related variables
int16_t controller_to_pilot_timer_roll; // cycles to mix controller and pilot controls in POSHOLD_CONTROLLER_TO_PILOT
int16_t controller_to_pilot_timer_pitch; // cycles to mix controller and pilot controls in POSHOLD_CONTROLLER_TO_PILOT
int16_t controller_final_roll; // final roll angle from controller as we exit brake or loiter mode (used for mixing with pilot input)
int16_t controller_final_pitch; // final pitch angle from controller as we exit brake or loiter mode (used for mixing with pilot input)
// wind compensation related variables
Vector2f wind_comp_ef; // wind compensation in earth frame, filtered lean angles from position controller
int16_t wind_comp_roll; // roll angle to compensate for wind
int16_t wind_comp_pitch; // pitch angle to compensate for wind
uint16_t wind_comp_start_timer; // counter to delay start of wind compensation for a short time after loiter is engaged
int8_t wind_comp_timer; // counter to reduce wind comp roll/pitch lean angle calcs to 10hz
// final output
int16_t roll; // final roll angle sent to attitude controller
int16_t pitch; // final pitch angle sent to attitude controller
} poshold;
// poshold_init - initialise PosHold controller // poshold_init - initialise PosHold controller
bool Copter::ModePosHold::init(bool ignore_checks) bool Copter::ModePosHold::init(bool ignore_checks)
{ {
@ -93,20 +37,20 @@ bool Copter::ModePosHold::init(bool ignore_checks)
} }
// initialise lean angles to current attitude // initialise lean angles to current attitude
poshold.pilot_roll = 0; pilot_roll = 0;
poshold.pilot_pitch = 0; pilot_pitch = 0;
// compute brake_gain // compute brake_gain
poshold.brake_gain = (15.0f * (float)g.poshold_brake_rate + 95.0f) / 100.0f; brake_gain = (15.0f * (float)g.poshold_brake_rate + 95.0f) / 100.0f;
if (ap.land_complete) { if (ap.land_complete) {
// if landed begin in loiter mode // if landed begin in loiter mode
poshold.roll_mode = POSHOLD_LOITER; roll_mode = POSHOLD_LOITER;
poshold.pitch_mode = POSHOLD_LOITER; pitch_mode = POSHOLD_LOITER;
} else { } else {
// if not landed start in pilot override to avoid hard twitch // if not landed start in pilot override to avoid hard twitch
poshold.roll_mode = POSHOLD_PILOT_OVERRIDE; roll_mode = POSHOLD_PILOT_OVERRIDE;
poshold.pitch_mode = POSHOLD_PILOT_OVERRIDE; pitch_mode = POSHOLD_PILOT_OVERRIDE;
} }
// initialise loiter // initialise loiter
@ -114,10 +58,10 @@ bool Copter::ModePosHold::init(bool ignore_checks)
loiter_nav->init_target(); loiter_nav->init_target();
// initialise wind_comp each time PosHold is switched on // initialise wind_comp each time PosHold is switched on
poshold.wind_comp_ef.zero(); wind_comp_ef.zero();
poshold.wind_comp_roll = 0; wind_comp_roll = 0;
poshold.wind_comp_pitch = 0; wind_comp_pitch = 0;
poshold.wind_comp_timer = 0; wind_comp_timer = 0;
return true; return true;
} }
@ -171,8 +115,8 @@ void Copter::ModePosHold::run()
loiter_nav->update(); loiter_nav->update();
// set poshold state to pilot override // set poshold state to pilot override
poshold.roll_mode = POSHOLD_PILOT_OVERRIDE; roll_mode = POSHOLD_PILOT_OVERRIDE;
poshold.pitch_mode = POSHOLD_PILOT_OVERRIDE; pitch_mode = POSHOLD_PILOT_OVERRIDE;
break; break;
case AltHold_Takeoff: case AltHold_Takeoff:
@ -201,8 +145,8 @@ void Copter::ModePosHold::run()
pos_control->add_takeoff_climb_rate(takeoff_climb_rate, G_Dt); pos_control->add_takeoff_climb_rate(takeoff_climb_rate, G_Dt);
// set poshold state to pilot override // set poshold state to pilot override
poshold.roll_mode = POSHOLD_PILOT_OVERRIDE; roll_mode = POSHOLD_PILOT_OVERRIDE;
poshold.pitch_mode = POSHOLD_PILOT_OVERRIDE; pitch_mode = POSHOLD_PILOT_OVERRIDE;
break; break;
case AltHold_Landed_Ground_Idle: case AltHold_Landed_Ground_Idle:
@ -218,8 +162,8 @@ void Copter::ModePosHold::run()
pos_control->relax_alt_hold_controllers(0.0f); // forces throttle output to go to zero pos_control->relax_alt_hold_controllers(0.0f); // forces throttle output to go to zero
// set poshold state to pilot override // set poshold state to pilot override
poshold.roll_mode = POSHOLD_PILOT_OVERRIDE; roll_mode = POSHOLD_PILOT_OVERRIDE;
poshold.pitch_mode = POSHOLD_PILOT_OVERRIDE; pitch_mode = POSHOLD_PILOT_OVERRIDE;
break; break;
case AltHold_Flying: case AltHold_Flying:
@ -251,8 +195,8 @@ void Copter::ModePosHold::run()
float vel_right = -vel.x*ahrs.sin_yaw() + vel.y*ahrs.cos_yaw(); float vel_right = -vel.x*ahrs.sin_yaw() + vel.y*ahrs.cos_yaw();
// If not in LOITER, retrieve latest wind compensation lean angles related to current yaw // If not in LOITER, retrieve latest wind compensation lean angles related to current yaw
if (poshold.roll_mode != POSHOLD_LOITER || poshold.pitch_mode != POSHOLD_LOITER) { if (roll_mode != POSHOLD_LOITER || pitch_mode != POSHOLD_LOITER) {
poshold_get_wind_comp_lean_angles(poshold.wind_comp_roll, poshold.wind_comp_pitch); poshold_get_wind_comp_lean_angles(wind_comp_roll, wind_comp_pitch);
} }
// Roll state machine // Roll state machine
@ -260,61 +204,61 @@ void Copter::ModePosHold::run()
// 1. dealing with pilot input // 1. dealing with pilot input
// 2. calculating the final roll output to the attitude controller // 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 // 3. checking if the state (aka mode) should be changed and if 'yes' perform any required initialisation for the new state
switch (poshold.roll_mode) { switch (roll_mode) {
case POSHOLD_PILOT_OVERRIDE: case POSHOLD_PILOT_OVERRIDE:
// update pilot desired roll angle using latest radio input // 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 // this filters the input so that it returns to zero no faster than the brake-rate
poshold_update_pilot_lean_angle(poshold.pilot_roll, target_roll); poshold_update_pilot_lean_angle(pilot_roll, target_roll);
// switch to BRAKE mode for next iteration if no pilot input // switch to BRAKE mode for next iteration if no pilot input
if (is_zero(target_roll) && (fabsf(poshold.pilot_roll) < 2 * g.poshold_brake_rate)) { if (is_zero(target_roll) && (fabsf(pilot_roll) < 2 * g.poshold_brake_rate)) {
// initialise BRAKE mode // initialise BRAKE mode
poshold.roll_mode = POSHOLD_BRAKE; // Set brake roll mode roll_mode = POSHOLD_BRAKE; // Set brake roll mode
poshold.brake_roll = 0; // initialise braking angle to zero brake_roll = 0; // initialise braking angle to zero
poshold.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; // reset brake_angle_max so we can detect when vehicle begins to flatten out during braking
poshold.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.
poshold.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
} }
// final lean angle should be pilot input plus wind compensation // final lean angle should be pilot input plus wind compensation
poshold.roll = poshold.pilot_roll + poshold.wind_comp_roll; roll = pilot_roll + wind_comp_roll;
break; break;
case POSHOLD_BRAKE: case POSHOLD_BRAKE:
case POSHOLD_BRAKE_READY_TO_LOITER: case POSHOLD_BRAKE_READY_TO_LOITER:
// calculate brake_roll angle to counter-act velocity // calculate brake_roll angle to counter-act velocity
poshold_update_brake_angle_from_velocity(poshold.brake_roll, vel_right); poshold_update_brake_angle_from_velocity(brake_roll, vel_right);
// update braking time estimate // update braking time estimate
if (!poshold.braking_time_updated_roll) { if (!braking_time_updated_roll) {
// check if brake angle is increasing // check if brake angle is increasing
if (abs(poshold.brake_roll) >= poshold.brake_angle_max_roll) { if (abs(brake_roll) >= brake_angle_max_roll) {
poshold.brake_angle_max_roll = abs(poshold.brake_roll); brake_angle_max_roll = abs(brake_roll);
} else { } else {
// braking angle has started decreasing so re-estimate braking time // braking angle has started decreasing so re-estimate braking time
poshold.brake_timeout_roll = 1+(uint16_t)(LOOP_RATE_FACTOR*15L*(int32_t)(abs(poshold.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)(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.
poshold.braking_time_updated_roll = true; braking_time_updated_roll = true;
} }
} }
// if velocity is very low reduce braking time to 0.5seconds // if velocity is very low reduce braking time to 0.5seconds
if ((fabsf(vel_right) <= POSHOLD_SPEED_0) && (poshold.brake_timeout_roll > 50*LOOP_RATE_FACTOR)) { if ((fabsf(vel_right) <= POSHOLD_SPEED_0) && (brake_timeout_roll > 50*LOOP_RATE_FACTOR)) {
poshold.brake_timeout_roll = 50*LOOP_RATE_FACTOR; brake_timeout_roll = 50*LOOP_RATE_FACTOR;
} }
// reduce braking timer // reduce braking timer
if (poshold.brake_timeout_roll > 0) { if (brake_timeout_roll > 0) {
poshold.brake_timeout_roll--; brake_timeout_roll--;
} else { } else {
// indicate that we are ready to move to Loiter. // indicate that we are ready to move to Loiter.
// Loiter will only actually be engaged once both roll_mode and pitch_mode are changed to POSHOLD_BRAKE_READY_TO_LOITER // Loiter will only actually be engaged once both roll_mode and pitch_mode are changed to POSHOLD_BRAKE_READY_TO_LOITER
// logic for engaging loiter is handled below the roll and pitch mode switch statements // logic for engaging loiter is handled below the roll and pitch mode switch statements
poshold.roll_mode = POSHOLD_BRAKE_READY_TO_LOITER; roll_mode = POSHOLD_BRAKE_READY_TO_LOITER;
} }
// final lean angle is braking angle + wind compensation angle // final lean angle is braking angle + wind compensation angle
poshold.roll = poshold.brake_roll + poshold.wind_comp_roll; roll = brake_roll + wind_comp_roll;
// check for pilot input // check for pilot input
if (!is_zero(target_roll)) { if (!is_zero(target_roll)) {
@ -331,21 +275,21 @@ void Copter::ModePosHold::run()
case POSHOLD_CONTROLLER_TO_PILOT_OVERRIDE: case POSHOLD_CONTROLLER_TO_PILOT_OVERRIDE:
// update pilot desired roll angle using latest radio input // 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 // this filters the input so that it returns to zero no faster than the brake-rate
poshold_update_pilot_lean_angle(poshold.pilot_roll, target_roll); poshold_update_pilot_lean_angle(pilot_roll, target_roll);
// count-down loiter to pilot timer // count-down loiter to pilot timer
if (poshold.controller_to_pilot_timer_roll > 0) { if (controller_to_pilot_timer_roll > 0) {
poshold.controller_to_pilot_timer_roll--; controller_to_pilot_timer_roll--;
} else { } else {
// when timer runs out switch to full pilot override for next iteration // when timer runs out switch to full pilot override for next iteration
poshold.roll_mode = POSHOLD_PILOT_OVERRIDE; roll_mode = POSHOLD_PILOT_OVERRIDE;
} }
// calculate controller_to_pilot mix ratio // calculate controller_to_pilot mix ratio
controller_to_pilot_roll_mix = (float)poshold.controller_to_pilot_timer_roll / (float)POSHOLD_CONTROLLER_TO_PILOT_MIX_TIMER; controller_to_pilot_roll_mix = (float)controller_to_pilot_timer_roll / (float)POSHOLD_CONTROLLER_TO_PILOT_MIX_TIMER;
// mix final loiter lean angle and pilot desired lean angles // mix final loiter lean angle and pilot desired lean angles
poshold.roll = poshold_mix_controls(controller_to_pilot_roll_mix, poshold.controller_final_roll, poshold.pilot_roll + poshold.wind_comp_roll); roll = poshold_mix_controls(controller_to_pilot_roll_mix, controller_final_roll, pilot_roll + wind_comp_roll);
break; break;
} }
@ -354,61 +298,61 @@ void Copter::ModePosHold::run()
// 1. dealing with pilot input // 1. dealing with pilot input
// 2. calculating the final pitch output to the attitude contpitcher // 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 // 3. checking if the state (aka mode) should be changed and if 'yes' perform any required initialisation for the new state
switch (poshold.pitch_mode) { switch (pitch_mode) {
case POSHOLD_PILOT_OVERRIDE: case POSHOLD_PILOT_OVERRIDE:
// update pilot desired pitch angle using latest radio input // 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 // this filters the input so that it returns to zero no faster than the brake-rate
poshold_update_pilot_lean_angle(poshold.pilot_pitch, target_pitch); poshold_update_pilot_lean_angle(pilot_pitch, target_pitch);
// switch to BRAKE mode for next iteration if no pilot input // switch to BRAKE mode for next iteration if no pilot input
if (is_zero(target_pitch) && (fabsf(poshold.pilot_pitch) < 2 * g.poshold_brake_rate)) { if (is_zero(target_pitch) && (fabsf(pilot_pitch) < 2 * g.poshold_brake_rate)) {
// initialise BRAKE mode // initialise BRAKE mode
poshold.pitch_mode = POSHOLD_BRAKE; // set brake pitch mode pitch_mode = POSHOLD_BRAKE; // set brake pitch mode
poshold.brake_pitch = 0; // initialise braking angle to zero brake_pitch = 0; // initialise braking angle to zero
poshold.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; // reset brake_angle_max so we can detect when vehicle begins to flatten out during braking
poshold.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.
poshold.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
} }
// final lean angle should be pilot input plus wind compensation // final lean angle should be pilot input plus wind compensation
poshold.pitch = poshold.pilot_pitch + poshold.wind_comp_pitch; pitch = pilot_pitch + wind_comp_pitch;
break; break;
case POSHOLD_BRAKE: case POSHOLD_BRAKE:
case POSHOLD_BRAKE_READY_TO_LOITER: case POSHOLD_BRAKE_READY_TO_LOITER:
// calculate brake_pitch angle to counter-act velocity // calculate brake_pitch angle to counter-act velocity
poshold_update_brake_angle_from_velocity(poshold.brake_pitch, -vel_fw); poshold_update_brake_angle_from_velocity(brake_pitch, -vel_fw);
// update braking time estimate // update braking time estimate
if (!poshold.braking_time_updated_pitch) { if (!braking_time_updated_pitch) {
// check if brake angle is increasing // check if brake angle is increasing
if (abs(poshold.brake_pitch) >= poshold.brake_angle_max_pitch) { if (abs(brake_pitch) >= brake_angle_max_pitch) {
poshold.brake_angle_max_pitch = abs(poshold.brake_pitch); brake_angle_max_pitch = abs(brake_pitch);
} else { } else {
// braking angle has started decreasing so re-estimate braking time // braking angle has started decreasing so re-estimate braking time
poshold.brake_timeout_pitch = 1+(uint16_t)(LOOP_RATE_FACTOR*15L*(int32_t)(abs(poshold.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)(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.
poshold.braking_time_updated_pitch = true; braking_time_updated_pitch = true;
} }
} }
// if velocity is very low reduce braking time to 0.5seconds // if velocity is very low reduce braking time to 0.5seconds
if ((fabsf(vel_fw) <= POSHOLD_SPEED_0) && (poshold.brake_timeout_pitch > 50*LOOP_RATE_FACTOR)) { if ((fabsf(vel_fw) <= POSHOLD_SPEED_0) && (brake_timeout_pitch > 50*LOOP_RATE_FACTOR)) {
poshold.brake_timeout_pitch = 50*LOOP_RATE_FACTOR; brake_timeout_pitch = 50*LOOP_RATE_FACTOR;
} }
// reduce braking timer // reduce braking timer
if (poshold.brake_timeout_pitch > 0) { if (brake_timeout_pitch > 0) {
poshold.brake_timeout_pitch--; brake_timeout_pitch--;
} else { } else {
// indicate that we are ready to move to Loiter. // indicate that we are ready to move to Loiter.
// Loiter will only actually be engaged once both pitch_mode and pitch_mode are changed to POSHOLD_BRAKE_READY_TO_LOITER // Loiter will only actually be engaged once both pitch_mode and pitch_mode are changed to POSHOLD_BRAKE_READY_TO_LOITER
// logic for engaging loiter is handled below the pitch and pitch mode switch statements // logic for engaging loiter is handled below the pitch and pitch mode switch statements
poshold.pitch_mode = POSHOLD_BRAKE_READY_TO_LOITER; pitch_mode = POSHOLD_BRAKE_READY_TO_LOITER;
} }
// final lean angle is braking angle + wind compensation angle // final lean angle is braking angle + wind compensation angle
poshold.pitch = poshold.brake_pitch + poshold.wind_comp_pitch; pitch = brake_pitch + wind_comp_pitch;
// check for pilot input // check for pilot input
if (!is_zero(target_pitch)) { if (!is_zero(target_pitch)) {
@ -425,21 +369,21 @@ void Copter::ModePosHold::run()
case POSHOLD_CONTROLLER_TO_PILOT_OVERRIDE: case POSHOLD_CONTROLLER_TO_PILOT_OVERRIDE:
// update pilot desired pitch angle using latest radio input // 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 // this filters the input so that it returns to zero no faster than the brake-rate
poshold_update_pilot_lean_angle(poshold.pilot_pitch, target_pitch); poshold_update_pilot_lean_angle(pilot_pitch, target_pitch);
// count-down loiter to pilot timer // count-down loiter to pilot timer
if (poshold.controller_to_pilot_timer_pitch > 0) { if (controller_to_pilot_timer_pitch > 0) {
poshold.controller_to_pilot_timer_pitch--; controller_to_pilot_timer_pitch--;
} else { } else {
// when timer runs out switch to full pilot override for next iteration // when timer runs out switch to full pilot override for next iteration
poshold.pitch_mode = POSHOLD_PILOT_OVERRIDE; pitch_mode = POSHOLD_PILOT_OVERRIDE;
} }
// calculate controller_to_pilot mix ratio // calculate controller_to_pilot mix ratio
controller_to_pilot_pitch_mix = (float)poshold.controller_to_pilot_timer_pitch / (float)POSHOLD_CONTROLLER_TO_PILOT_MIX_TIMER; controller_to_pilot_pitch_mix = (float)controller_to_pilot_timer_pitch / (float)POSHOLD_CONTROLLER_TO_PILOT_MIX_TIMER;
// mix final loiter lean angle and pilot desired lean angles // mix final loiter lean angle and pilot desired lean angles
poshold.pitch = poshold_mix_controls(controller_to_pilot_pitch_mix, poshold.controller_final_pitch, poshold.pilot_pitch + poshold.wind_comp_pitch); pitch = poshold_mix_controls(controller_to_pilot_pitch_mix, controller_final_pitch, pilot_pitch + wind_comp_pitch);
break; break;
} }
@ -448,47 +392,47 @@ void Copter::ModePosHold::run()
// //
// switch into LOITER mode when both roll and pitch are ready // switch into LOITER mode when both roll and pitch are ready
if (poshold.roll_mode == POSHOLD_BRAKE_READY_TO_LOITER && poshold.pitch_mode == POSHOLD_BRAKE_READY_TO_LOITER) { if (roll_mode == POSHOLD_BRAKE_READY_TO_LOITER && pitch_mode == POSHOLD_BRAKE_READY_TO_LOITER) {
poshold.roll_mode = POSHOLD_BRAKE_TO_LOITER; roll_mode = POSHOLD_BRAKE_TO_LOITER;
poshold.pitch_mode = POSHOLD_BRAKE_TO_LOITER; pitch_mode = POSHOLD_BRAKE_TO_LOITER;
poshold.brake_to_loiter_timer = POSHOLD_BRAKE_TO_LOITER_TIMER; brake_to_loiter_timer = POSHOLD_BRAKE_TO_LOITER_TIMER;
// init loiter controller // init loiter controller
loiter_nav->init_target(inertial_nav.get_position()); loiter_nav->init_target(inertial_nav.get_position());
// set delay to start of wind compensation estimate updates // set delay to start of wind compensation estimate updates
poshold.wind_comp_start_timer = POSHOLD_WIND_COMP_START_TIMER; wind_comp_start_timer = POSHOLD_WIND_COMP_START_TIMER;
} }
// roll-mode is used as the combined roll+pitch mode when in BRAKE_TO_LOITER or LOITER modes // roll-mode is used as the combined roll+pitch mode when in BRAKE_TO_LOITER or LOITER modes
if (poshold.roll_mode == POSHOLD_BRAKE_TO_LOITER || poshold.roll_mode == POSHOLD_LOITER) { if (roll_mode == POSHOLD_BRAKE_TO_LOITER || roll_mode == POSHOLD_LOITER) {
// force pitch mode to be same as roll_mode just to keep it consistent (it's not actually used in these states) // force pitch mode to be same as roll_mode just to keep it consistent (it's not actually used in these states)
poshold.pitch_mode = poshold.roll_mode; pitch_mode = roll_mode;
// handle combined roll+pitch mode // handle combined roll+pitch mode
switch (poshold.roll_mode) { switch (roll_mode) {
case POSHOLD_BRAKE_TO_LOITER: case POSHOLD_BRAKE_TO_LOITER:
// reduce brake_to_loiter timer // reduce brake_to_loiter timer
if (poshold.brake_to_loiter_timer > 0) { if (brake_to_loiter_timer > 0) {
poshold.brake_to_loiter_timer--; brake_to_loiter_timer--;
} else { } else {
// progress to full loiter on next iteration // progress to full loiter on next iteration
poshold.roll_mode = POSHOLD_LOITER; roll_mode = POSHOLD_LOITER;
poshold.pitch_mode = POSHOLD_LOITER; pitch_mode = POSHOLD_LOITER;
} }
// calculate percentage mix of loiter and brake control // calculate percentage mix of loiter and brake control
brake_to_loiter_mix = (float)poshold.brake_to_loiter_timer / (float)POSHOLD_BRAKE_TO_LOITER_TIMER; brake_to_loiter_mix = (float)brake_to_loiter_timer / (float)POSHOLD_BRAKE_TO_LOITER_TIMER;
// calculate brake_roll and pitch angles to counter-act velocity // calculate brake_roll and pitch angles to counter-act velocity
poshold_update_brake_angle_from_velocity(poshold.brake_roll, vel_right); poshold_update_brake_angle_from_velocity(brake_roll, vel_right);
poshold_update_brake_angle_from_velocity(poshold.brake_pitch, -vel_fw); poshold_update_brake_angle_from_velocity(brake_pitch, -vel_fw);
// run loiter controller // run loiter controller
loiter_nav->update(); loiter_nav->update();
// calculate final roll and pitch output by mixing loiter and brake controls // calculate final roll and pitch output by mixing loiter and brake controls
poshold.roll = poshold_mix_controls(brake_to_loiter_mix, poshold.brake_roll + poshold.wind_comp_roll, loiter_nav->get_roll()); roll = poshold_mix_controls(brake_to_loiter_mix, brake_roll + wind_comp_roll, loiter_nav->get_roll());
poshold.pitch = poshold_mix_controls(brake_to_loiter_mix, poshold.brake_pitch + poshold.wind_comp_pitch, loiter_nav->get_pitch()); pitch = poshold_mix_controls(brake_to_loiter_mix, brake_pitch + wind_comp_pitch, loiter_nav->get_pitch());
// check for pilot input // check for pilot input
if (!is_zero(target_roll) || !is_zero(target_pitch)) { if (!is_zero(target_roll) || !is_zero(target_pitch)) {
@ -497,8 +441,8 @@ void Copter::ModePosHold::run()
// init transition to pilot override // init transition to pilot override
poshold_roll_controller_to_pilot_override(); poshold_roll_controller_to_pilot_override();
// 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)
// no need to reset poshold.brake_pitch here as wind comp has not been updated since last brake_pitch computation // no need to reset brake_pitch here as wind comp has not been updated since last brake_pitch computation
poshold.pitch_mode = POSHOLD_BRAKE_READY_TO_LOITER; pitch_mode = POSHOLD_BRAKE_READY_TO_LOITER;
} }
// 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)) {
@ -506,8 +450,8 @@ void Copter::ModePosHold::run()
poshold_pitch_controller_to_pilot_override(); poshold_pitch_controller_to_pilot_override();
if (is_zero(target_roll)) { if (is_zero(target_roll)) {
// switch roll-mode to brake (but ready to go back to loiter anytime) // switch roll-mode to brake (but ready to go back to loiter anytime)
// no need to reset poshold.brake_roll here as wind comp has not been updated since last brake_roll computation // no need to reset brake_roll here as wind comp has not been updated since last brake_roll computation
poshold.roll_mode = POSHOLD_BRAKE_READY_TO_LOITER; roll_mode = POSHOLD_BRAKE_READY_TO_LOITER;
} }
} }
} }
@ -518,8 +462,8 @@ void Copter::ModePosHold::run()
loiter_nav->update(); loiter_nav->update();
// set roll angle based on loiter controller outputs // set roll angle based on loiter controller outputs
poshold.roll = loiter_nav->get_roll(); roll = loiter_nav->get_roll();
poshold.pitch = loiter_nav->get_pitch(); pitch = loiter_nav->get_pitch();
// update wind compensation estimate // update wind compensation estimate
poshold_update_wind_comp_estimate(); poshold_update_wind_comp_estimate();
@ -531,9 +475,9 @@ void Copter::ModePosHold::run()
// init transition to pilot override // init transition to pilot override
poshold_roll_controller_to_pilot_override(); poshold_roll_controller_to_pilot_override();
// 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)
poshold.pitch_mode = POSHOLD_BRAKE_READY_TO_LOITER; pitch_mode = POSHOLD_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
poshold.brake_pitch = 0; brake_pitch = 0;
} }
// 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)) {
@ -541,8 +485,8 @@ void Copter::ModePosHold::run()
poshold_pitch_controller_to_pilot_override(); poshold_pitch_controller_to_pilot_override();
// 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)) {
poshold.roll_mode = POSHOLD_BRAKE_READY_TO_LOITER; roll_mode = POSHOLD_BRAKE_READY_TO_LOITER;
poshold.brake_roll = 0; brake_roll = 0;
} }
// 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)
} }
@ -557,11 +501,11 @@ void Copter::ModePosHold::run()
// constrain target pitch/roll angles // constrain target pitch/roll angles
float angle_max = copter.aparm.angle_max; float angle_max = copter.aparm.angle_max;
poshold.roll = constrain_int16(poshold.roll, -angle_max, angle_max); roll = constrain_int16(roll, -angle_max, angle_max);
poshold.pitch = constrain_int16(poshold.pitch, -angle_max, angle_max); pitch = constrain_int16(pitch, -angle_max, angle_max);
// call attitude controller // call attitude controller
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(poshold.roll, poshold.pitch, target_yaw_rate); attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(roll, pitch, target_yaw_rate);
// call z-axis position controller // call z-axis position controller
pos_control->update_z_controller(); pos_control->update_z_controller();
@ -611,9 +555,9 @@ void Copter::ModePosHold::poshold_update_brake_angle_from_velocity(int16_t &brak
// calculate velocity-only based lean angle // calculate velocity-only based lean angle
if (velocity >= 0) { if (velocity >= 0) {
lean_angle = -poshold.brake_gain * velocity * (1.0f+500.0f/(velocity+60.0f)); lean_angle = -brake_gain * velocity * (1.0f+500.0f/(velocity+60.0f));
} else { } else {
lean_angle = -poshold.brake_gain * velocity * (1.0f+500.0f/(-velocity+60.0f)); lean_angle = -brake_gain * velocity * (1.0f+500.0f/(-velocity+60.0f));
} }
// do not let lean_angle be too far from brake_angle // do not let lean_angle be too far from brake_angle
@ -628,8 +572,8 @@ void Copter::ModePosHold::poshold_update_brake_angle_from_velocity(int16_t &brak
void Copter::ModePosHold::poshold_update_wind_comp_estimate() void Copter::ModePosHold::poshold_update_wind_comp_estimate()
{ {
// check wind estimate start has not been delayed // check wind estimate start has not been delayed
if (poshold.wind_comp_start_timer > 0) { if (wind_comp_start_timer > 0) {
poshold.wind_comp_start_timer--; wind_comp_start_timer--;
return; return;
} }
@ -643,19 +587,19 @@ void Copter::ModePosHold::poshold_update_wind_comp_estimate()
const Vector3f& accel_target = pos_control->get_accel_target(); const Vector3f& accel_target = pos_control->get_accel_target();
// update wind compensation in earth-frame lean angles // update wind compensation in earth-frame lean angles
if (is_zero(poshold.wind_comp_ef.x)) { if (is_zero(wind_comp_ef.x)) {
// if wind compensation has not been initialised set it immediately to the pos controller's desired accel in north direction // if wind compensation has not been initialised set it immediately to the pos controller's desired accel in north direction
poshold.wind_comp_ef.x = accel_target.x; wind_comp_ef.x = accel_target.x;
} else { } else {
// low pass filter the position controller's lean angle output // low pass filter the position controller's lean angle output
poshold.wind_comp_ef.x = (1.0f-TC_WIND_COMP)*poshold.wind_comp_ef.x + TC_WIND_COMP*accel_target.x; wind_comp_ef.x = (1.0f-TC_WIND_COMP)*wind_comp_ef.x + TC_WIND_COMP*accel_target.x;
} }
if (is_zero(poshold.wind_comp_ef.y)) { if (is_zero(wind_comp_ef.y)) {
// if wind compensation has not been initialised set it immediately to the pos controller's desired accel in north direction // if wind compensation has not been initialised set it immediately to the pos controller's desired accel in north direction
poshold.wind_comp_ef.y = accel_target.y; wind_comp_ef.y = accel_target.y;
} else { } else {
// low pass filter the position controller's lean angle output // low pass filter the position controller's lean angle output
poshold.wind_comp_ef.y = (1.0f-TC_WIND_COMP)*poshold.wind_comp_ef.y + TC_WIND_COMP*accel_target.y; wind_comp_ef.y = (1.0f-TC_WIND_COMP)*wind_comp_ef.y + TC_WIND_COMP*accel_target.y;
} }
} }
@ -664,37 +608,37 @@ void Copter::ModePosHold::poshold_update_wind_comp_estimate()
void Copter::ModePosHold::poshold_get_wind_comp_lean_angles(int16_t &roll_angle, int16_t &pitch_angle) void Copter::ModePosHold::poshold_get_wind_comp_lean_angles(int16_t &roll_angle, int16_t &pitch_angle)
{ {
// reduce rate to 10hz // reduce rate to 10hz
poshold.wind_comp_timer++; wind_comp_timer++;
if (poshold.wind_comp_timer < POSHOLD_WIND_COMP_TIMER_10HZ) { if (wind_comp_timer < POSHOLD_WIND_COMP_TIMER_10HZ) {
return; return;
} }
poshold.wind_comp_timer = 0; wind_comp_timer = 0;
// convert earth frame desired accelerations to body frame roll and pitch lean angles // convert earth frame desired accelerations to body frame roll and pitch lean angles
roll_angle = atanf((-poshold.wind_comp_ef.x*ahrs.sin_yaw() + poshold.wind_comp_ef.y*ahrs.cos_yaw())/981)*(18000/M_PI); roll_angle = atanf((-wind_comp_ef.x*ahrs.sin_yaw() + wind_comp_ef.y*ahrs.cos_yaw())/981)*(18000/M_PI);
pitch_angle = atanf(-(poshold.wind_comp_ef.x*ahrs.cos_yaw() + poshold.wind_comp_ef.y*ahrs.sin_yaw())/981)*(18000/M_PI); pitch_angle = atanf(-(wind_comp_ef.x*ahrs.cos_yaw() + wind_comp_ef.y*ahrs.sin_yaw())/981)*(18000/M_PI);
} }
// poshold_roll_controller_to_pilot_override - initialises transition from a controller submode (brake or loiter) to a pilot override on roll axis // poshold_roll_controller_to_pilot_override - initialises transition from a controller submode (brake or loiter) to a pilot override on roll axis
void Copter::ModePosHold::poshold_roll_controller_to_pilot_override() void Copter::ModePosHold::poshold_roll_controller_to_pilot_override()
{ {
poshold.roll_mode = POSHOLD_CONTROLLER_TO_PILOT_OVERRIDE; roll_mode = POSHOLD_CONTROLLER_TO_PILOT_OVERRIDE;
poshold.controller_to_pilot_timer_roll = POSHOLD_CONTROLLER_TO_PILOT_MIX_TIMER; controller_to_pilot_timer_roll = POSHOLD_CONTROLLER_TO_PILOT_MIX_TIMER;
// 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
poshold.pilot_roll = 0; pilot_roll = 0;
// store final controller output for mixing with pilot input // store final controller output for mixing with pilot input
poshold.controller_final_roll = poshold.roll; controller_final_roll = roll;
} }
// poshold_pitch_controller_to_pilot_override - initialises transition from a controller submode (brake or loiter) to a pilot override on roll axis // poshold_pitch_controller_to_pilot_override - initialises transition from a controller submode (brake or loiter) to a pilot override on roll axis
void Copter::ModePosHold::poshold_pitch_controller_to_pilot_override() void Copter::ModePosHold::poshold_pitch_controller_to_pilot_override()
{ {
poshold.pitch_mode = POSHOLD_CONTROLLER_TO_PILOT_OVERRIDE; pitch_mode = POSHOLD_CONTROLLER_TO_PILOT_OVERRIDE;
poshold.controller_to_pilot_timer_pitch = POSHOLD_CONTROLLER_TO_PILOT_MIX_TIMER; controller_to_pilot_timer_pitch = POSHOLD_CONTROLLER_TO_PILOT_MIX_TIMER;
// initialise pilot_pitch 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_pitch 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
poshold.pilot_pitch = 0; pilot_pitch = 0;
// store final loiter outputs for mixing with pilot input // store final loiter outputs for mixing with pilot input
poshold.controller_final_pitch = poshold.pitch; controller_final_pitch = pitch;
} }
#endif #endif