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.
This commit is contained in:
mahima-yoga authored and Mahima Yoga committed 2026-08-20 15:07:42 +02:00
1 parent 768d1ce316
commit 0c835dcc17
8 files changed
+329 -1

No files matched your search

+2
View File
@@ -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.
@@ -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)
@@ -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);
}
@@ -37,7 +37,10 @@
* Performance model.
*/
#include <float.h>
#include <math.h>
#include <px4_platform_common/module_params.h>
#include <uORB/topics/fuel_tank_status.h>
#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<px4::params::FW_T_SINK_MIN>) _param_fw_t_sink_min,
(ParamFloat<px4::params::WEIGHT_BASE>) _param_weight_base,
(ParamFloat<px4::params::WEIGHT_GROSS>) _param_weight_gross,
(ParamFloat<px4::params::WEIGHT_FUEL>) _param_weight_fuel,
(ParamFloat<px4::params::FW_SERVICE_CEIL>) _param_service_ceiling,
(ParamFloat<px4::params::FW_THR_TRIM>) _param_fw_thr_trim,
(ParamFloat<px4::params::FW_THR_MAX>) _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_
@@ -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 <gtest/gtest.h>
#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)));
}
@@ -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
@@ -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;
@@ -48,6 +48,7 @@
#include <lib/npfg/AirspeedDirectionController.hpp>
#include <lib/tecs/TECS.hpp>
#include <lib/mathlib/mathlib.h>
#include <lib/mathlib/math/filter/AlphaFilter.hpp>
#include <lib/perf/perf_counter.h>
#include <px4_platform_common/px4_config.h>
#include <px4_platform_common/defines.h>
@@ -62,10 +63,12 @@
#include <uORB/PublicationMulti.hpp>
#include <uORB/Subscription.hpp>
#include <uORB/SubscriptionCallback.hpp>
#include <uORB/SubscriptionMultiArray.hpp>
#include <uORB/topics/airspeed_validated.h>
#include <uORB/topics/fixed_wing_lateral_setpoint.h>
#include <uORB/topics/fixed_wing_lateral_status.h>
#include <uORB/topics/fixed_wing_longitudinal_setpoint.h>
#include <uORB/topics/fuel_tank_status.h>
#include <uORB/topics/normalized_unsigned_setpoint.h>
#include <uORB/topics/flight_phase_estimation.h>
#include <uORB/topics/lateral_control_configuration.h>
@@ -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_s> _fuel_tank_status_subs{ORB_ID::fuel_tank_status};
uORB::Subscription _wind_sub{ORB_ID(wind)};
uORB::SubscriptionData<vehicle_control_mode_s> _control_mode_sub{ORB_ID(vehicle_control_mode)};
uORB::SubscriptionData<vehicle_air_data_s> _vehicle_air_data_sub{ORB_ID(vehicle_air_data)};
@@ -204,6 +208,10 @@ private:
float _load_factor_from_bank_angle{1.f};
SlewRate<float> _airspeed_slew_rate_controller;
AlphaFilter<float> _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;