From 0c835dcc175e0942504f65fb27cc47d24f657297 Mon Sep 17 00:00:00 2001 From: mahima-yoga Date: Thu, 20 Aug 2026 15:03:56 +0200 Subject: [PATCH] feat(fw_performance_model): scale vehicle weight by remaining fuel Add a WEIGHT_FUEL parameter for the mass of a full fuel load. When set, the weight used for trim throttle, min/stall airspeed, and climb/sink rate scaling is reduced by the burned fuel mass derived from fuel_tank_status (filtered to reject sloshing). Disabled by default. --- msg/TecsStatus.msg | 2 + src/lib/fw_performance_model/CMakeLists.txt | 3 + .../fw_performance_model/PerformanceModel.cpp | 32 +++- .../fw_performance_model/PerformanceModel.hpp | 29 +++ .../PerformanceModelTest.cpp | 181 ++++++++++++++++++ .../performance_model_params.yaml | 13 ++ .../FwLateralLongitudinalControl.cpp | 60 ++++++ .../FwLateralLongitudinalControl.hpp | 10 + 8 files changed, 329 insertions(+), 1 deletion(-) create mode 100644 src/lib/fw_performance_model/PerformanceModelTest.cpp diff --git a/msg/TecsStatus.msg b/msg/TecsStatus.msg index ed62b70da3c..9b16ea16671 100644 --- a/msg/TecsStatus.msg +++ b/msg/TecsStatus.msg @@ -29,3 +29,5 @@ float32 throttle_trim # estimated throttle value [0,1] required to fly level a float32 underspeed_ratio # 0: no underspeed, 1: maximal underspeed. Controller takes measures to avoid stall proportional to ratio if >0. float32 fast_descend_ratio # value indicating if fast descend mode is enabled with ramp up and ramp down [0-1] + +float32 weight_ratio # [-] Ratio of estimated vehicle weight to WEIGHT_BASE. Accounts for burned fuel if fuel-based weight compensation is enabled. diff --git a/src/lib/fw_performance_model/CMakeLists.txt b/src/lib/fw_performance_model/CMakeLists.txt index c93557ded7f..cc1021157df 100644 --- a/src/lib/fw_performance_model/CMakeLists.txt +++ b/src/lib/fw_performance_model/CMakeLists.txt @@ -34,4 +34,7 @@ px4_add_library(performance_model PerformanceModel.cpp ) +target_link_libraries(performance_model PRIVATE atmosphere geo) set_property(GLOBAL APPEND PROPERTY PX4_MODULE_CONFIG_FILES ${CMAKE_CURRENT_SOURCE_DIR}/performance_model_params.yaml) + +px4_add_functional_gtest(SRC PerformanceModelTest.cpp LINKLIBS performance_model) diff --git a/src/lib/fw_performance_model/PerformanceModel.cpp b/src/lib/fw_performance_model/PerformanceModel.cpp index 3d3b36c8cea..7eb39d90fb8 100644 --- a/src/lib/fw_performance_model/PerformanceModel.cpp +++ b/src/lib/fw_performance_model/PerformanceModel.cpp @@ -58,12 +58,42 @@ PerformanceModel::PerformanceModel(): ModuleParams(nullptr) { updateParams(); } +void PerformanceModel::setFuelFractionRemaining(float fuel_fraction_remaining) +{ + if (PX4_ISFINITE(fuel_fraction_remaining)) { + _fuel_fraction_remaining = math::constrain(fuel_fraction_remaining, 0.f, 1.f); + } +} + +float PerformanceModel::getFuelFractionRemaining(const fuel_tank_status_s &fuel_tank_status) +{ + float fraction = NAN; + + if (fuel_tank_status.percent_remaining <= 100) { + fraction = fuel_tank_status.percent_remaining * 0.01f; + + } else if (PX4_ISFINITE(fuel_tank_status.remaining_fuel) && fuel_tank_status.maximum_fuel_capacity > FLT_EPSILON) { + fraction = fuel_tank_status.remaining_fuel / fuel_tank_status.maximum_fuel_capacity; + + } else if (PX4_ISFINITE(fuel_tank_status.consumed_fuel) && fuel_tank_status.maximum_fuel_capacity > FLT_EPSILON) { + fraction = 1.f - fuel_tank_status.consumed_fuel / fuel_tank_status.maximum_fuel_capacity; + } + + return PX4_ISFINITE(fraction) ? math::constrain(fraction, 0.f, 1.f) : NAN; +} + float PerformanceModel::getWeightRatio() const { float weight_factor = 1.0f; if (_param_weight_base.get() > FLT_EPSILON && _param_weight_gross.get() > FLT_EPSILON) { - weight_factor = math::constrain(_param_weight_gross.get() / _param_weight_base.get(), kMinWeightRatio, + float weight = _param_weight_gross.get(); + + if (_param_weight_fuel.get() > FLT_EPSILON && PX4_ISFINITE(_fuel_fraction_remaining)) { + weight -= (1.f - _fuel_fraction_remaining) * _param_weight_fuel.get(); + } + + weight_factor = math::constrain(weight / _param_weight_base.get(), kMinWeightRatio, kMaxWeightRatio); } diff --git a/src/lib/fw_performance_model/PerformanceModel.hpp b/src/lib/fw_performance_model/PerformanceModel.hpp index 07a10e8621c..123cef528e7 100644 --- a/src/lib/fw_performance_model/PerformanceModel.hpp +++ b/src/lib/fw_performance_model/PerformanceModel.hpp @@ -37,7 +37,10 @@ * Performance model. */ +#include +#include #include +#include #ifndef PX4_SRC_MODULES_FW_POS_CONTROL_PERFORMANCEMODEL_H_ #define PX4_SRC_MODULES_FW_POS_CONTROL_PERFORMANCEMODEL_H_ @@ -70,6 +73,29 @@ public: */ float getWeightRatio() const; + /** + * Set the fraction of fuel remaining, used to reduce the vehicle weight by the burned fuel mass + * if fuel-based weight compensation is enabled (WEIGHT_FUEL > 0). + * Non-finite input is ignored and the last known fuel fraction is kept. + * @param fuel_fraction_remaining fraction of fuel remaining in range [0,1] + */ + void setFuelFractionRemaining(float fuel_fraction_remaining); + + /** + * Check if fuel-based weight compensation is enabled. + * @return true if WEIGHT_FUEL is set to a valid fuel weight + */ + bool isFuelCompensationEnabled() const { return _param_weight_fuel.get() > FLT_EPSILON; } + + /** + * Extract the fraction of fuel remaining from a fuel tank status message, preferring the + * remaining percentage over the measured remaining fuel over the consumed fuel. + * The consumed fuel fallback assumes the tank was full at boot. + * @param fuel_tank_status fuel tank status message + * @return fraction of fuel remaining in range [0,1], NAN if not available + */ + static float getFuelFractionRemaining(const fuel_tank_status_s &fuel_tank_status); + /** * Get the trim throttle for the current airspeed setpoint as well as air density and weight. * @param throttle_min minimum throttle in range [0,1] @@ -131,6 +157,7 @@ private: (ParamFloat) _param_fw_t_sink_min, (ParamFloat) _param_weight_base, (ParamFloat) _param_weight_gross, + (ParamFloat) _param_weight_fuel, (ParamFloat) _param_service_ceiling, (ParamFloat) _param_fw_thr_trim, (ParamFloat) _param_fw_thr_max, @@ -150,6 +177,8 @@ private: static float sanitiseAirDensity(float air_density); + + float _fuel_fraction_remaining{NAN}; ///< fraction of fuel remaining in range [0,1], NAN if unknown }; #endif //PX4_SRC_MODULES_FW_POS_CONTROL_PERFORMANCEMODEL_H_ diff --git a/src/lib/fw_performance_model/PerformanceModelTest.cpp b/src/lib/fw_performance_model/PerformanceModelTest.cpp new file mode 100644 index 00000000000..4b86c43c84f --- /dev/null +++ b/src/lib/fw_performance_model/PerformanceModelTest.cpp @@ -0,0 +1,181 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#include +#include "PerformanceModel.hpp" + +// to run: make tests TESTFILTER=PerformanceModel + +class PerformanceModelTest : public ::testing::Test +{ +public: + void SetUp() override + { + param_control_autosave(false); + param_reset_all(); + } + + void setWeightParams(float base, float gross, float fuel) + { + param_set(param_handle(px4::params::WEIGHT_BASE), &base); + param_set(param_handle(px4::params::WEIGHT_GROSS), &gross); + param_set(param_handle(px4::params::WEIGHT_FUEL), &fuel); + _model.updateParameters(); + } + + PerformanceModel _model; +}; + +TEST_F(PerformanceModelTest, weightRatioDefaultsToOne) +{ + // GIVEN: default parameters (weight compensation disabled) + // THEN: the weight ratio is 1 + EXPECT_FLOAT_EQ(_model.getWeightRatio(), 1.0f); +} + +TEST_F(PerformanceModelTest, weightRatioWithoutFuelCompensation) +{ + // GIVEN: base and gross weight set, fuel compensation disabled + setWeightParams(10.0f, 12.0f, -1.0f); + + // THEN: the weight ratio is gross/base, independent of any fuel fraction + EXPECT_FLOAT_EQ(_model.getWeightRatio(), 1.2f); + + _model.setFuelFractionRemaining(0.5f); + EXPECT_FLOAT_EQ(_model.getWeightRatio(), 1.2f); +} + +TEST_F(PerformanceModelTest, weightRatioScalesWithFuelFraction) +{ + // GIVEN: base and gross weight set, 4kg full fuel load + setWeightParams(10.0f, 12.0f, 4.0f); + + // THEN: with unknown fuel fraction the full-tank gross weight is assumed + EXPECT_FLOAT_EQ(_model.getWeightRatio(), 1.2f); + + // AND: the weight ratio scales linearly with the burned fuel mass + _model.setFuelFractionRemaining(1.0f); + EXPECT_FLOAT_EQ(_model.getWeightRatio(), 1.2f); + + _model.setFuelFractionRemaining(0.5f); + EXPECT_FLOAT_EQ(_model.getWeightRatio(), 1.0f); + + _model.setFuelFractionRemaining(0.0f); + EXPECT_FLOAT_EQ(_model.getWeightRatio(), 0.8f); +} + +TEST_F(PerformanceModelTest, fuelFractionKeptOnNonFiniteInput) +{ + // GIVEN: base and gross weight set, 4kg full fuel load, and a known fuel fraction + setWeightParams(10.0f, 12.0f, 4.0f); + _model.setFuelFractionRemaining(0.5f); + EXPECT_FLOAT_EQ(_model.getWeightRatio(), 1.0f); + + // WHEN: the fuel fraction input becomes non-finite (e.g. sensor failure) + _model.setFuelFractionRemaining(NAN); + + // THEN: the last known fuel fraction is kept + EXPECT_FLOAT_EQ(_model.getWeightRatio(), 1.0f); +} + +TEST_F(PerformanceModelTest, fuelFractionIsConstrained) +{ + // GIVEN: base and gross weight set, 4kg full fuel load + setWeightParams(10.0f, 12.0f, 4.0f); + + // THEN: out-of-range fuel fractions are constrained to [0, 1] + _model.setFuelFractionRemaining(-0.5f); + EXPECT_FLOAT_EQ(_model.getWeightRatio(), 0.8f); + + _model.setFuelFractionRemaining(1.5f); + EXPECT_FLOAT_EQ(_model.getWeightRatio(), 1.2f); +} + +TEST_F(PerformanceModelTest, weightRatioIsConstrained) +{ + // GIVEN: a fuel weight larger than the gross weight (misconfiguration) + setWeightParams(10.0f, 12.0f, 12.0f); + + // THEN: the weight ratio is constrained to its lower bound + _model.setFuelFractionRemaining(0.0f); + EXPECT_FLOAT_EQ(_model.getWeightRatio(), 0.5f); +} + +TEST_F(PerformanceModelTest, fuelFractionFromTankStatusFallbackChain) +{ + // GIVEN: a fuel tank status with no measurements + fuel_tank_status_s status{}; + status.maximum_fuel_capacity = NAN; + status.remaining_fuel = NAN; + status.consumed_fuel = NAN; + status.percent_remaining = UINT8_MAX; + + // THEN: no fuel fraction is available + EXPECT_TRUE(std::isnan(PerformanceModel::getFuelFractionRemaining(status))); + + // AND: the consumed fuel is the last fallback, assuming a full tank at boot + status.maximum_fuel_capacity = 2000.f; + status.consumed_fuel = 500.f; + EXPECT_FLOAT_EQ(PerformanceModel::getFuelFractionRemaining(status), 0.75f); + + // AND: the measured remaining fuel takes precedence over the consumed fuel + status.remaining_fuel = 1000.f; + EXPECT_FLOAT_EQ(PerformanceModel::getFuelFractionRemaining(status), 0.5f); + + // AND: a valid remaining percentage takes precedence over everything else + status.percent_remaining = 40; + EXPECT_FLOAT_EQ(PerformanceModel::getFuelFractionRemaining(status), 0.4f); +} + +TEST_F(PerformanceModelTest, fuelFractionFromTankStatusIsConstrained) +{ + // GIVEN: a remaining fuel measurement exceeding the maximum capacity + fuel_tank_status_s status{}; + status.maximum_fuel_capacity = 2000.f; + status.remaining_fuel = 3000.f; + status.consumed_fuel = NAN; + status.percent_remaining = UINT8_MAX; + + // THEN: the fuel fraction is constrained to 1 + EXPECT_FLOAT_EQ(PerformanceModel::getFuelFractionRemaining(status), 1.0f); + + // AND: more consumed fuel than the maximum capacity results in a fraction of 0 + status.remaining_fuel = NAN; + status.consumed_fuel = 3000.f; + EXPECT_FLOAT_EQ(PerformanceModel::getFuelFractionRemaining(status), 0.0f); + + // AND: a remaining fuel measurement without a valid maximum capacity is unusable + status.maximum_fuel_capacity = 0.f; + status.remaining_fuel = 1000.f; + EXPECT_TRUE(std::isnan(PerformanceModel::getFuelFractionRemaining(status))); +} diff --git a/src/lib/fw_performance_model/performance_model_params.yaml b/src/lib/fw_performance_model/performance_model_params.yaml index f218b597fae..025c6f14707 100644 --- a/src/lib/fw_performance_model/performance_model_params.yaml +++ b/src/lib/fw_performance_model/performance_model_params.yaml @@ -71,6 +71,19 @@ parameters: This is the actual weight of the vehicle at any time. This value will differ from WEIGHT_BASE in case weight was added or removed from the base weight. Examples are the addition of payloads or larger batteries. A zero or negative value disables trim throttle and minimum airspeed compensation based on weight. + If fuel-based weight compensation is enabled (WEIGHT_FUEL), set this to the takeoff weight with a full fuel load. + type: float + default: -1.0 + unit: kg + decimal: 1 + increment: 0.1 + WEIGHT_FUEL: + description: + short: Mass of a full fuel load + long: |- + If set together with WEIGHT_BASE and WEIGHT_GROSS, the weight used for + performance scaling is reduced by the burned fuel reported in the fuel tank status. + Only a single fuel tank is supported. A zero or negative value disables fuel-based weight compensation. type: float default: -1.0 unit: kg diff --git a/src/modules/fw_lateral_longitudinal_control/FwLateralLongitudinalControl.cpp b/src/modules/fw_lateral_longitudinal_control/FwLateralLongitudinalControl.cpp index c816cd3db74..045fb6cb227 100644 --- a/src/modules/fw_lateral_longitudinal_control/FwLateralLongitudinalControl.cpp +++ b/src/modules/fw_lateral_longitudinal_control/FwLateralLongitudinalControl.cpp @@ -66,6 +66,12 @@ static constexpr float ROLL_WARNING_CAN_RUN_THRESHOLD = 0.9f; // [m/s/s] slew rate limit for airspeed setpoint changes static constexpr float ASPD_SP_SLEW_RATE = 1.f; +// [s] time constant of the fuel fraction filter, needs to be slow enough to reject fuel sloshing +static constexpr float FUEL_FRACTION_FILTER_TIME_CONST = 30.f; + +// [s] maximum time step used to advance the fuel fraction filter on a new sample +static constexpr float FUEL_FRACTION_FILTER_MAX_DT = 10.f; + FwLateralLongitudinalControl::FwLateralLongitudinalControl(bool is_vtol) : ModuleParams(nullptr), WorkItem(MODULE_NAME, px4::wq_configurations::nav_and_controllers), @@ -76,6 +82,9 @@ FwLateralLongitudinalControl::FwLateralLongitudinalControl(bool is_vtol) : _tecs_status_pub.advertise(); _flight_phase_estimation_pub.advertise(); _fixed_wing_lateral_status_pub.advertise(); + + _fuel_fraction_filter.setParameters(0.f, FUEL_FRACTION_FILTER_TIME_CONST); + parameters_update(); _airspeed_slew_rate_controller.setSlewRate(ASPD_SP_SLEW_RATE); @@ -160,6 +169,8 @@ void FwLateralLongitudinalControl::Run() _tecs.set_min_sink_rate(_performance_model.getMinimumSinkRate(_air_density)); } + updateFuelState(); + if (_vehicle_landed_sub.updated()) { vehicle_land_detected_s landed{}; _vehicle_landed_sub.copy(&landed); @@ -443,6 +454,54 @@ FwLateralLongitudinalControl::tecs_update_pitch_throttle(const float control_int return flight_phase_estimation_s::FLIGHT_PHASE_UNKNOWN; } +void FwLateralLongitudinalControl::updateFuelState() +{ + fuel_tank_status_s fuel_tank_status; + + for (auto &sub : _fuel_tank_status_subs) { + if (!sub.update(&fuel_tank_status)) { + continue; + } + + // only the tank with id 0 is used for weight compensation, multi-tank systems + // are expected to publish an aggregated total as tank 0 + if (fuel_tank_status.fuel_tank_id != 0) { + if (!_ignored_fuel_tank_reported && _performance_model.isFuelCompensationEnabled()) { + PX4_WARN("Ignoring fuel tank status with tank id %d, only tank 0 is used for weight compensation", + fuel_tank_status.fuel_tank_id); + _ignored_fuel_tank_reported = true; + } + + continue; + } + + const float fuel_fraction_remaining = PerformanceModel::getFuelFractionRemaining(fuel_tank_status); + + if (!PX4_ISFINITE(fuel_fraction_remaining)) { + continue; + } + + if (_time_last_fuel_fraction_update == 0) { + _fuel_fraction_filter.reset(fuel_fraction_remaining); + + } else if (fuel_tank_status.timestamp > _time_last_fuel_fraction_update) { + const float dt = math::min((fuel_tank_status.timestamp - _time_last_fuel_fraction_update) * 1e-6f, + FUEL_FRACTION_FILTER_MAX_DT); + _fuel_fraction_filter.update(fuel_fraction_remaining, dt); + + } else { + // out-dated sample + continue; + } + + _time_last_fuel_fraction_update = fuel_tank_status.timestamp; + + _performance_model.setFuelFractionRemaining(_fuel_fraction_filter.getState()); + + _tecs.set_equivalent_airspeed_trim(_performance_model.getCalibratedTrimAirspeed()); + } +} + void FwLateralLongitudinalControl::tecs_status_publish(float alt_sp, float equivalent_airspeed_sp, float true_airspeed_derivative_raw, float throttle_trim, hrt_abstime timestamp) @@ -478,6 +537,7 @@ FwLateralLongitudinalControl::tecs_status_publish(float alt_sp, float equivalent tecs_status.throttle_trim = throttle_trim; tecs_status.underspeed_ratio = _tecs.get_underspeed_ratio(); tecs_status.fast_descend_ratio = debug_output.fast_descend; + tecs_status.weight_ratio = _performance_model.getWeightRatio(); tecs_status.timestamp = timestamp; diff --git a/src/modules/fw_lateral_longitudinal_control/FwLateralLongitudinalControl.hpp b/src/modules/fw_lateral_longitudinal_control/FwLateralLongitudinalControl.hpp index 9cf728efc8a..c2ccd9baa03 100644 --- a/src/modules/fw_lateral_longitudinal_control/FwLateralLongitudinalControl.hpp +++ b/src/modules/fw_lateral_longitudinal_control/FwLateralLongitudinalControl.hpp @@ -48,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -62,10 +63,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include @@ -114,6 +117,7 @@ private: uORB::Subscription _airspeed_validated_sub{ORB_ID(airspeed_validated)}; uORB::Subscription _flaps_setpoint_sub{ORB_ID(flaps_setpoint)}; + uORB::SubscriptionMultiArray _fuel_tank_status_subs{ORB_ID::fuel_tank_status}; uORB::Subscription _wind_sub{ORB_ID(wind)}; uORB::SubscriptionData _control_mode_sub{ORB_ID(vehicle_control_mode)}; uORB::SubscriptionData _vehicle_air_data_sub{ORB_ID(vehicle_air_data)}; @@ -204,6 +208,10 @@ private: float _load_factor_from_bank_angle{1.f}; SlewRate _airspeed_slew_rate_controller; + AlphaFilter _fuel_fraction_filter; + hrt_abstime _time_last_fuel_fraction_update{0}; + bool _ignored_fuel_tank_reported{false}; ///< true if the warning about an ignored fuel tank id was already sent + perf_counter_t _loop_perf; // loop performance counter PerformanceModel _performance_model; @@ -235,6 +243,8 @@ private: void updateWind(hrt_abstime now); + void updateFuelState(); + void updateTECSAltitudeTimeConstant(const bool is_low_height, const float dt); bool checkLowHeightConditions() const;