diff --git a/src/lib/tecs/TECS.cpp b/src/lib/tecs/TECS.cpp index d19dd58a5ce..1839a1448e9 100644 --- a/src/lib/tecs/TECS.cpp +++ b/src/lib/tecs/TECS.cpp @@ -245,8 +245,8 @@ void TECSControl::initialize(const Setpoint &setpoint, const Input &input, const specific_energy_rate, param, flag, weight)}; ControlValues seb_rate{_calcPitchControlSebRate(weight, pitch_specific_energy_rate)}; - _pitch_setpoint = _calcPitchControlOutput(input, seb_rate, - specific_energy_rate.spe_rate.setpoint + specific_energy_rate.ske_rate.setpoint, param); + _pitch_setpoint = _calcPitchControlOutput(input, seb_rate, _calcPitchControlSteRate(pitch_specific_energy_rate), + weight, param); ControlValues ste_rate{_calcThrottleControlSteRate(limit, specific_energy_rate, param)}; resetIfFinite(_ste_rate_error_filter, _getControlError(ste_rate), 0.f); @@ -460,8 +460,8 @@ void TECSControl::_calcPitchControl(float dt, const Setpoint &setpoint, const In ControlValues seb_rate{_calcPitchControlSebRate(weight, pitch_specific_energy_rates)}; _calcPitchControlUpdate(dt, input, seb_rate, param); - const float pitch_setpoint{_calcPitchControlOutput(input, seb_rate, - specific_energy_rates.spe_rate.setpoint + specific_energy_rates.ske_rate.setpoint, param)}; + const float pitch_setpoint{_calcPitchControlOutput(input, seb_rate, _calcPitchControlSteRate(pitch_specific_energy_rates), + weight, param)}; // Comply with the specified vertical acceleration limit by applying a pitch rate limit // NOTE: at zero airspeed, the pitch increment is unbounded @@ -516,8 +516,8 @@ void TECSControl::_calcPitchControlUpdate(float dt, const Input &input, const Co if (param.integrator_gain_pitch > FLT_EPSILON) { // Calculate pitch integrator input term: the balance rate error is a balance rate increment at the current throttle - float pitch_integ_input = _sebRateToClimbAngle(_getControlError(seb_rate) * param.integrator_gain_pitch, 0.f, input, - param); + float pitch_integ_input = _sebRateToClimbAngle(_getControlError(seb_rate) * param.integrator_gain_pitch, 0.f, 0.f, + input, param); // Guard against NaN integrator input - discard bad input sample (e.g., exiting offboard velocity mode) if (!PX4_ISFINITE(pitch_integ_input)) { @@ -545,25 +545,36 @@ void TECSControl::_calcPitchControlUpdate(float dt, const Input &input, const Co } } -float TECSControl::_sebRateToClimbAngle(float seb_rate, float ste_rate, const Input &input, const Param ¶m) +float TECSControl::_sebRateToClimbAngle(float seb_rate, float ste_rate, float speed_weight, const Input &input, + const Param ¶m) { // The balance rate depends not only on the climb angle but also on total energy rate. - // Working from definitions, with climb angle γ (sin linearised as usual here): + // Working from definitions, with climb angle γ (sin linearised as usual here) and speed weight w: // STE' = total energy rate from the throttle (ideally the STE rate setpoint) // SPE' = g V γ // SKE' = STE' - SPE' - // SEB' = SPE' - SKE' = 2 SPE' - STE' = 2 g V γ - STE' + // SEB' = (2 - w) SPE' - w SKE' = 2 SPE' - w STE' = 2 g V γ - w STE' // Solving for γ: - // γ = (SEB' + STE') / (2 g V) + // γ = (SEB' + w STE') / (2 g V) + // The pitch then flies the potential energy rate of the balance rate setpoint for any weight. The weight only + // shifts how the pitch feedback weights altitude against airspeed errors. // max() returns its second argument for a NaN first one, so a NaN airspeed falls back to the minimum airspeed const float tas = max(input.tas, param.tas_min, FLT_EPSILON); - return (seb_rate + ste_rate) / (2.f * tas * CONSTANTS_ONE_G); + return (seb_rate + speed_weight * ste_rate) / (2.f * tas * CONSTANTS_ONE_G); +} + +float TECSControl::_calcPitchControlSteRate(const SpecificEnergyRates &pitch_specific_energy_rates) +{ + // The total energy rate of the pair the pitch balances, not the throttle's setpoint: with the airspeed setpoint + // capped for the pitch between trim and the setpoint, the throttle's acceleration demand above trim is not the + // pitch's to fly. With the throttle's setpoint the pitch would pitch up by the weighted acceleration demand there. + return pitch_specific_energy_rates.spe_rate.setpoint + pitch_specific_energy_rates.ske_rate.setpoint; } float TECSControl::_calcPitchControlOutput(const Input &input, const ControlValues &seb_rate, float ste_rate_setpoint, - const Param ¶m) const + const SpecificEnergyWeighting &weight, const Param ¶m) const { // Calculate a specific energy correction that doesn't include the integrator contribution. The setpoint is fed // forward in full: together with the total energy rate it converts to the potential energy rate the pitch flies, @@ -575,7 +586,8 @@ float TECSControl::_calcPitchControlOutput(const Input &input, const ControlValu // a) The climb angle follows pitch angle with a lag that is small enough not to destabilise the control loop. // b) The offset between climb angle and pitch angle (angle of attack) is constant and equal to the level-flight // pitch, excluding the effect of pitch transients due to control action or turbulence. - float pitch_setpoint_unc = _sebRateToClimbAngle(SEB_rate_correction, ste_rate_setpoint, input, param) + float pitch_setpoint_unc = _sebRateToClimbAngle(SEB_rate_correction, ste_rate_setpoint, weight.ske_weighting, input, + param) + _pitch_integ_state + param.level_flight_pitch; diff --git a/src/lib/tecs/TECS.hpp b/src/lib/tecs/TECS.hpp index 5107470a9c4..bfe8b63769c 100644 --- a/src/lib/tecs/TECS.hpp +++ b/src/lib/tecs/TECS.hpp @@ -557,7 +557,8 @@ private: * @brief Climb angle that realises a specific energy balance rate. * * The balance rate depends on the throttle as well as on the climb angle, SEB rate = 2 g V gamma - STE rate, - * so the total energy rate the throttle flies has to be added back: gamma = (SEB rate + STE rate) / (2 g V). + * so the total energy rate the throttle flies has to be added back. With the speed weight w the balance rate is + * SEB rate = 2 g V gamma - w STE rate, so gamma = (SEB rate + w STE rate) / (2 g V). * For a balance rate increment at unchanged throttle pass a zero total energy rate. * * Uses the filtered true airspeed, which is trim airspeed without a valid measurement, floored at the minimum @@ -565,11 +566,21 @@ private: * * @param seb_rate is the specific energy balance rate to realise in [m²/s³]. * @param ste_rate is the specific total energy rate the throttle flies in [m²/s³]. + * @param speed_weight is the speed weight w of the balance rate, the kinetic energy weighting [0, 2]. * @param input is the current input measurement of the UAS. * @param param is the control parameters. * @return climb angle in [rad]. */ - static float _sebRateToClimbAngle(float seb_rate, float ste_rate, const Input &input, const Param ¶m); + static float _sebRateToClimbAngle(float seb_rate, float ste_rate, float speed_weight, const Input &input, + const Param ¶m); + + /** + * @brief Specific total energy rate the pitch assumes the throttle flies. + * + * @param pitch_specific_energy_rates is the specific energy rates of the pitch loop. + * @return specific total energy rate in [m²/s³]. + */ + static float _calcPitchControlSteRate(const SpecificEnergyRates &pitch_specific_energy_rates); /** * @brief Calculate the pitch control output function. @@ -577,11 +588,12 @@ private: * @param input is the current input measurement of the UAS. * @param seb_rate is the specific energy balance rate in [m²/s³]. * @param ste_rate_setpoint is the specific total energy rate setpoint the throttle flies in [m²/s³]. + * @param weight is the speed and altitude weighting of the balance rate. * @param param is the control parameters. * @return absolute pitch setpoint [rad]. */ float _calcPitchControlOutput(const Input &input, const ControlValues &seb_rate, float ste_rate_setpoint, - const Param ¶m) const; + const SpecificEnergyWeighting &weight, const Param ¶m) const; /** * @brief Update controlled throttle setpoint. diff --git a/src/lib/tecs/TECSControlClosedLoopTest.cpp b/src/lib/tecs/TECSControlClosedLoopTest.cpp index 7857fb73624..4852aa206da 100644 --- a/src/lib/tecs/TECSControlClosedLoopTest.cpp +++ b/src/lib/tecs/TECSControlClosedLoopTest.cpp @@ -443,6 +443,36 @@ TEST_F(TECSControlClosedLoopTest, AltitudeStepUp) EXPECT_NEAR(_state.h, _alt_sp, TRACKING_ERROR_TOL); } +// Below speed weight 1 the pitch weights altitude errors more than airspeed errors, but flies the same climb angle + +TEST_F(TECSControlClosedLoopTest, AltitudeStepUpLowSpeedWeight) +{ + _params.pitch_speed_weight = 0.5f; + + run(10.f); + + setAltitudeSetpoint(ALT_INIT + ALT_STEP); + run(200.f); + + EXPECT_LT(_sim_stats.max_airspeed_error, MAX_NUMERICAL_ERR) << "peak at t=" << _sim_stats.t_max_airspeed_error << " s"; + + EXPECT_NEAR(_state.h, _alt_sp, TRACKING_ERROR_TOL); +} + +TEST_F(TECSControlClosedLoopTest, AltitudeStepDownLowSpeedWeight) +{ + _params.pitch_speed_weight = 0.5f; + + run(10.f); + + setAltitudeSetpoint(ALT_INIT - ALT_STEP); + run(200.f); + + EXPECT_LT(_sim_stats.max_airspeed_error, MAX_NUMERICAL_ERR) << "peak at t=" << _sim_stats.t_max_airspeed_error << " s"; + + EXPECT_NEAR(_state.h, _alt_sp, TRACKING_ERROR_TOL); +} + // --- Plant pitch-to-flight-path offset (model mismatch) ----------------------- // // The plant flies level at pitch_cmd = 10 deg instead of 0 (e.g. incorrect FW_PSP_OFF). @@ -500,8 +530,12 @@ TEST_F(TECSControlClosedLoopTest, AltitudeStepUpDefaultTuningPitchOffset) // // Speed weight 2 is the glider setting: the pitch flies airspeed only, so it tracks an airspeed setpoint with no // propulsion at all. +// +// Disabled: at speed weight 2 the climb angle follows entirely from the total energy rate the throttle delivers, +// gamma = (SEB rate + 2 STE rate) / (2 g V). The pitch takes it from the setpoint, which with a failed throttle is +// never delivered, so the pitch flies the altitude demand. Needs the pitch to assume gliding at speed weight 2. -TEST_F(TECSControlClosedLoopTest, GlideSpeedWeightTwoAirspeedStep) +TEST_F(TECSControlClosedLoopTest, DISABLED_GlideSpeedWeightTwoAirspeedStep) { _params.pitch_speed_weight = 2.f; @@ -599,3 +633,33 @@ TEST_F(TECSControlClosedLoopTest, AirspeedlessSteadyStateCruise) EXPECT_LT(_sim_stats.max_throttle_integ_norm, MAX_NUMERICAL_ERR); EXPECT_LT(_sim_stats.max_pitch_integ_norm, MAX_NUMERICAL_ERR); } + +// Without airspeed the speed weight is 0. The pitch flies the demanded climb angle and the throttle feedforward funds +// it, so the SEB rate error stays zero and the altitude error decays first order with FW_T_ALT_TC, while the open loop +// airspeed does not move. As in AltitudeStepDown, the pitch must follow the raw step as fast as the throttle and the +// demanded sink rate must not exceed FW_T_SINK_MIN. + +class TECSControlClosedLoopAirspeedlessTest : public TECSControlClosedLoopTest +{ +protected: + void checkAltitudeStep(float step) + { + disableAirspeed(); + run(10.f); + + setAltitudeSetpoint(_state.h + step); + const float t_step = _t; + + for (int i = 0; i < 30; i++) { + run(1.f); + const float expected_error = step * expf(-(_t - t_step) * _params.altitude_error_gain); + EXPECT_NEAR(_alt_sp - _state.h, expected_error, MAX_NUMERICAL_ERR_OPENLOOP) << "at t=" << _t << " s"; + } + + EXPECT_LT(_sim_stats.max_airspeed_error, MAX_NUMERICAL_ERR_OPENLOOP) << "peak at t=" << _sim_stats.t_max_airspeed_error << " s"; + EXPECT_LT(_sim_stats.max_pitch_integ_norm, MAX_NUMERICAL_ERR_OPENLOOP); + } +}; + +TEST_F(TECSControlClosedLoopAirspeedlessTest, AirspeedlessAltitudeStepUp) { checkAltitudeStep(ALT_STEP); } +TEST_F(TECSControlClosedLoopAirspeedlessTest, AirspeedlessAltitudeStepDown) { checkAltitudeStep(-ALT_STEP); }