mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-12-02 22:40:27 +08:00
fix(battery): improve motor failure handling and reset logic for current averaging
This commit is contained in:
1 parent
0a0b8e30f0
commit
4e5059791e
3 files changed
+110
-24
No files matched your search
@@ -109,7 +109,8 @@ public:
|
||||
}
|
||||
|
||||
// feed a current alternating by +-noise_a, updating the current average every sample like the drivers do
|
||||
static void fly(Battery &battery, float current_a, hrt_abstime duration, hrt_abstime ×tamp, float noise_a = 0.f)
|
||||
static void fly(Battery &battery, float current_a, hrt_abstime duration, hrt_abstime ×tamp, float noise_a = 0.f,
|
||||
hrt_abstime sample_interval = 20_ms)
|
||||
{
|
||||
const hrt_abstime end = timestamp + duration;
|
||||
float noise_sign = 1.f;
|
||||
@@ -121,7 +122,7 @@ public:
|
||||
noise_sign = -noise_sign;
|
||||
battery.updateBatteryStatus(timestamp);
|
||||
battery.getBatteryStatus();
|
||||
timestamp += 20_ms;
|
||||
timestamp += sample_interval;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -179,7 +180,7 @@ TEST_F(BatteryTest, KnownCellCountEstimatesCharge)
|
||||
EXPECT_EQ(empty_status.warning, battery_status_s::WARNING_EMERGENCY);
|
||||
}
|
||||
|
||||
// a motor failure in flight holds the current average through the spike, then averages the new load over a growing window
|
||||
// a motor failure in flight holds the current average through the spike, then sets it to the mean of the new load
|
||||
TEST_F(BatteryTest, MotorFailureAveragesNewLoad)
|
||||
{
|
||||
int32_t n_cells = 3;
|
||||
@@ -196,7 +197,10 @@ TEST_F(BatteryTest, MotorFailureAveragesNewLoad)
|
||||
fly(battery, 60.f, 1900_ms, timestamp);
|
||||
EXPECT_NEAR(battery.getCurrentAverage(), average_before, 0.1f);
|
||||
|
||||
fly(battery, 20.f, 2_s, timestamp, 5.f);
|
||||
fly(battery, 20.f, 1900_ms, timestamp, 5.f);
|
||||
EXPECT_NEAR(battery.getCurrentAverage(), average_before, 0.1f);
|
||||
|
||||
fly(battery, 20.f, 300_ms, timestamp, 5.f);
|
||||
EXPECT_NEAR(battery.getCurrentAverage(), 20.f, 0.5f);
|
||||
}
|
||||
|
||||
@@ -213,8 +217,8 @@ TEST_F(BatteryTest, MotorFailureKeepsAverageBeforeAsMinimum)
|
||||
const float average_before = battery.getCurrentAverage();
|
||||
|
||||
publishMotorFailureMask(1);
|
||||
fly(battery, 5.f, 10_s, timestamp);
|
||||
EXPECT_NEAR(battery.getCurrentAverage(), average_before, 0.01f);
|
||||
fly(battery, 5.f, 4100_ms, timestamp);
|
||||
EXPECT_NEAR(battery.getCurrentAverage(), average_before, 0.05f);
|
||||
}
|
||||
|
||||
// a failure flag that clears and comes back doesn't restart the hold
|
||||
@@ -234,7 +238,7 @@ TEST_F(BatteryTest, MotorFailureFlappingDoesNotRestart)
|
||||
fly(battery, 20.f, 100_ms, timestamp);
|
||||
publishMotorFailureMask(1);
|
||||
fly(battery, 60.f, 1_s, timestamp);
|
||||
// still averaging: one second at 60 A after 1.1 s at 20 A
|
||||
// set to the mean of the window: 1.1 s at 20 A and 0.9 s at 60 A
|
||||
EXPECT_GT(battery.getCurrentAverage(), 30.f);
|
||||
}
|
||||
|
||||
@@ -292,7 +296,7 @@ TEST_F(BatteryTest, MotorFailurePresentBeforeArmingIsIgnored)
|
||||
EXPECT_LT(battery.getCurrentAverage(), 17.f);
|
||||
}
|
||||
|
||||
// after a back-transition the hover load is averaged over a growing window, starting from no less than the configured prior
|
||||
// after a back-transition the average is set to the mean hover load, no less than the configured prior
|
||||
TEST_F(BatteryTest, BackTransitionAveragesHoverLoad)
|
||||
{
|
||||
int32_t n_cells = 3;
|
||||
@@ -312,7 +316,7 @@ TEST_F(BatteryTest, BackTransitionAveragesHoverLoad)
|
||||
fly(battery, 60.f, 1900_ms, timestamp);
|
||||
EXPECT_NEAR(battery.getCurrentAverage(), 15.f, 0.01f);
|
||||
|
||||
fly(battery, 40.f, 2_s, timestamp, 5.f);
|
||||
fly(battery, 40.f, 2200_ms, timestamp, 5.f);
|
||||
EXPECT_NEAR(battery.getCurrentAverage(), 40.f, 0.5f);
|
||||
}
|
||||
|
||||
@@ -330,8 +334,8 @@ TEST_F(BatteryTest, BackTransitionKeepsConfiguredPriorAsMinimum)
|
||||
fly(battery, 10.f, 5_s, timestamp);
|
||||
|
||||
publishArmed(true);
|
||||
fly(battery, 8.f, 10_s, timestamp);
|
||||
EXPECT_NEAR(battery.getCurrentAverage(), 15.f, 0.01f);
|
||||
fly(battery, 8.f, 4100_ms, timestamp);
|
||||
EXPECT_NEAR(battery.getCurrentAverage(), 15.f, 0.05f);
|
||||
}
|
||||
|
||||
// a fixed-wing average above the configured prior is kept as minimum after a back-transition
|
||||
@@ -351,8 +355,8 @@ TEST_F(BatteryTest, BackTransitionKeepsFixedWingAverageAsMinimum)
|
||||
ASSERT_GT(fixed_wing_average, 18.f);
|
||||
|
||||
publishArmed(true);
|
||||
fly(battery, 8.f, 10_s, timestamp);
|
||||
EXPECT_NEAR(battery.getCurrentAverage(), fixed_wing_average, 0.01f);
|
||||
fly(battery, 8.f, 4100_ms, timestamp);
|
||||
EXPECT_NEAR(battery.getCurrentAverage(), fixed_wing_average, 0.05f);
|
||||
}
|
||||
|
||||
// disarming ends the reset, after re-arming the average filters normally again
|
||||
@@ -378,7 +382,7 @@ TEST_F(BatteryTest, MotorFailureResetEndsOnDisarm)
|
||||
EXPECT_LT(battery.getCurrentAverage(), average_before + 3.f);
|
||||
}
|
||||
|
||||
// once the averaging window reaches the filter time constant, the average filters normally without a minimum
|
||||
// once the average is set, it filters normally again without a minimum
|
||||
TEST_F(BatteryTest, MotorFailureResetHandsOverToFilter)
|
||||
{
|
||||
int32_t n_cells = 3;
|
||||
@@ -391,10 +395,10 @@ TEST_F(BatteryTest, MotorFailureResetHandsOverToFilter)
|
||||
const float average_before = battery.getCurrentAverage();
|
||||
|
||||
publishMotorFailureMask(1);
|
||||
fly(battery, 20.f, 53_s, timestamp);
|
||||
fly(battery, 20.f, 4100_ms, timestamp);
|
||||
EXPECT_NEAR(battery.getCurrentAverage(), 20.f, 0.1f);
|
||||
|
||||
// filter: about 7.4 A, continued averaging: held at the average before
|
||||
// filter: about 7.4 A, still resetting: held at 20 A
|
||||
fly(battery, 2.f, 60_s, timestamp);
|
||||
EXPECT_LT(battery.getCurrentAverage(), average_before - 1.f);
|
||||
}
|
||||
@@ -418,3 +422,82 @@ TEST_F(BatteryTest, MotorFailureHoldIgnoresInvalidCurrent)
|
||||
fly(battery, 60.f, 1900_ms, timestamp);
|
||||
EXPECT_NEAR(battery.getCurrentAverage(), average_before, 0.1f);
|
||||
}
|
||||
|
||||
// a second motor failure in the same flight averages only the load after it
|
||||
TEST_F(BatteryTest, SecondMotorFailureAveragesItsOwnWindow)
|
||||
{
|
||||
int32_t n_cells = 3;
|
||||
param_set(param_find("BAT1_N_CELLS"), &n_cells);
|
||||
|
||||
Battery battery(1, nullptr, 20_ms, battery_status_s::SOURCE_POWER_MODULE);
|
||||
hrt_abstime timestamp = 1_s;
|
||||
publishArmed(true);
|
||||
fly(battery, 10.f, 10_s, timestamp);
|
||||
|
||||
publishMotorFailureMask(1);
|
||||
fly(battery, 30.f, 10_s, timestamp);
|
||||
ASSERT_NEAR(battery.getCurrentAverage(), 30.f, 0.5f);
|
||||
|
||||
publishMotorFailureMask(3);
|
||||
fly(battery, 50.f, 4200_ms, timestamp);
|
||||
EXPECT_NEAR(battery.getCurrentAverage(), 50.f, 0.5f);
|
||||
}
|
||||
|
||||
// the window mean is weighted by time, so a changing sample rate doesn't shift it towards the faster sampled current
|
||||
TEST_F(BatteryTest, MotorFailureWindowMeanIsTimeWeighted)
|
||||
{
|
||||
int32_t n_cells = 3;
|
||||
param_set(param_find("BAT1_N_CELLS"), &n_cells);
|
||||
|
||||
Battery battery(1, nullptr, 20_ms, battery_status_s::SOURCE_POWER_MODULE);
|
||||
hrt_abstime timestamp = 1_s;
|
||||
publishArmed(true);
|
||||
fly(battery, 10.f, 10_s, timestamp);
|
||||
|
||||
publishMotorFailureMask(1);
|
||||
fly(battery, 60.f, 2_s, timestamp);
|
||||
fly(battery, 20.f, 1_s, timestamp, 0.f, 10_ms);
|
||||
fly(battery, 40.f, 1200_ms, timestamp, 0.f, 100_ms);
|
||||
// counting samples instead would give about 22 A
|
||||
EXPECT_NEAR(battery.getCurrentAverage(), 30.f, 1.5f);
|
||||
}
|
||||
|
||||
// a sample after lost samples accounts for the whole gap, like the discharged charge, and the window still ends on time
|
||||
TEST_F(BatteryTest, MotorFailureWindowMeanCoversLostSamples)
|
||||
{
|
||||
int32_t n_cells = 3;
|
||||
param_set(param_find("BAT1_N_CELLS"), &n_cells);
|
||||
|
||||
Battery battery(1, nullptr, 20_ms, battery_status_s::SOURCE_POWER_MODULE);
|
||||
hrt_abstime timestamp = 1_s;
|
||||
publishArmed(true);
|
||||
fly(battery, 10.f, 10_s, timestamp);
|
||||
|
||||
publishMotorFailureMask(1);
|
||||
fly(battery, 60.f, 2_s, timestamp);
|
||||
fly(battery, 20.f, 500_ms, timestamp);
|
||||
timestamp += 800_ms;
|
||||
fly(battery, 40.f, 800_ms, timestamp);
|
||||
// 0.5 s at 20 A, 1.5 s at 40 A including the gap; counting samples instead would give about 32 A
|
||||
EXPECT_NEAR(battery.getCurrentAverage(), 35.f, 1.f);
|
||||
}
|
||||
|
||||
// a sample interval across the end of the hold only counts with its part inside the window
|
||||
TEST_F(BatteryTest, MotorFailureWindowStartsWithinSampleInterval)
|
||||
{
|
||||
int32_t n_cells = 3;
|
||||
param_set(param_find("BAT1_N_CELLS"), &n_cells);
|
||||
|
||||
Battery battery(1, nullptr, 20_ms, battery_status_s::SOURCE_POWER_MODULE);
|
||||
hrt_abstime timestamp = 1_s;
|
||||
publishArmed(true);
|
||||
fly(battery, 10.f, 10_s, timestamp);
|
||||
|
||||
publishMotorFailureMask(1);
|
||||
fly(battery, 40.f, 2100_ms, timestamp, 0.f, 700_ms);
|
||||
// this interval ends 0.12 s after the hold
|
||||
fly(battery, 100.f, 700_ms, timestamp, 0.f, 700_ms);
|
||||
fly(battery, 40.f, 2100_ms, timestamp, 0.f, 700_ms);
|
||||
// (0.12 s * 100 A + 2.1 s * 40 A) / 2.22 s, counting the whole interval instead would give about 69 A
|
||||
EXPECT_NEAR(battery.getCurrentAverage(), 43.2f, 1.f);
|
||||
}
|
||||
@@ -463,6 +463,7 @@ void Battery::resetTimeRemainingEstimate(float min_current_a)
|
||||
{
|
||||
_time_since_reset_s = 0.f;
|
||||
_reset_min_current_a = min_current_a;
|
||||
_reset_charge_as = 0.f;
|
||||
_current_average_filter_a.reset(fmaxf(_current_average_filter_a.getState(), min_current_a));
|
||||
}
|
||||
|
||||
@@ -473,17 +474,17 @@ void Battery::updateCurrentAverage(float current_a)
|
||||
}
|
||||
|
||||
if (PX4_ISFINITE(_time_since_reset_s)) {
|
||||
// after a reset, hold the average through the current spike, then use the cumulative mean of the samples since
|
||||
// until its window reaches the filter time constant, where it continues seamlessly, never below the given minimum
|
||||
// after a reset, hold the average through the load spike, then set it once to the mean current of a short window,
|
||||
// never below the given minimum, and continue filtering from there
|
||||
_time_since_reset_s += _dt;
|
||||
const float averaging_time_s = _time_since_reset_s - RESET_HOLD_TIME_S;
|
||||
|
||||
if (averaging_time_s > FLT_EPSILON) {
|
||||
const float alpha = math::min(_dt / averaging_time_s, 1.f);
|
||||
const float state = _current_average_filter_a.getState();
|
||||
_current_average_filter_a.reset(fmaxf(state + alpha * (fmaxf(current_a, 0.f) - state), _reset_min_current_a));
|
||||
if (_time_since_reset_s > RESET_HOLD_TIME_S) {
|
||||
// only the part of this sample's interval that lies in the window
|
||||
const float window_time_s = _time_since_reset_s - RESET_HOLD_TIME_S;
|
||||
_reset_charge_as += fmaxf(current_a, 0.f) * math::min(_dt, window_time_s);
|
||||
|
||||
if (averaging_time_s >= CURRENT_AVERAGE_TIME_CONSTANT_S) {
|
||||
if (window_time_s >= RESET_AVERAGING_TIME_S) {
|
||||
_current_average_filter_a.reset(fmaxf(_reset_charge_as / window_time_s, _reset_min_current_a));
|
||||
_time_since_reset_s = NAN;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,6 +214,7 @@ private:
|
||||
uint16_t _motor_failure_mask{0};
|
||||
float _time_since_reset_s{NAN}; ///< time since the last time-remaining reset, NAN if none is active
|
||||
float _reset_min_current_a{0.f}; ///< lower bound for the current average until the reset is completed
|
||||
float _reset_charge_as{0.f}; ///< [As] charge drawn in the averaging window of the reset
|
||||
hrt_abstime _last_unconnected_timestamp{0};
|
||||
|
||||
// Internal Resistance estimation
|
||||
@@ -242,4 +243,5 @@ private:
|
||||
static constexpr float BAT_TEMP_MAX = 100.0f;
|
||||
static constexpr float CURRENT_AVERAGE_TIME_CONSTANT_S = 50.f;
|
||||
static constexpr float RESET_HOLD_TIME_S = 2.f; ///< [s] hold the current average through the load spike following the event
|
||||
static constexpr float RESET_AVERAGING_TIME_S = 2.f; ///< [s] window after the hold whose mean current the average is reset to
|
||||
};
|
||||
Reference in new issue
Block a user