Skip to content

Commit dd652d0

Browse files
committed
feat(battery): enable use of BAT_n_I_OVERWRITE for all battery estimation sources
Signed-off-by: Silvan <silvan@auterion.com>
1 parent 197a1a6 commit dd652d0

File tree

6 files changed

+38
-52
lines changed

6 files changed

+38
-52
lines changed

src/lib/battery/battery.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ Battery::Battery(int index, ModuleParams *parent, const int sample_interval_us,
8787
snprintf(param_name, sizeof(param_name), "BAT%d_SOURCE", _index);
8888
_param_handles.source = param_find(param_name);
8989

90+
snprintf(param_name, sizeof(param_name), "BAT%d_I_OVERWRITE", _index);
91+
_param_handles.i_overwrite = param_find(param_name);
92+
9093
_param_handles.low_thr = param_find("BAT_LOW_THR");
9194
_param_handles.crit_thr = param_find("BAT_CRIT_THR");
9295
_param_handles.emergen_thr = param_find("BAT_EMERGEN_THR");
@@ -103,7 +106,13 @@ void Battery::updateVoltage(const float voltage_v)
103106

104107
void Battery::updateCurrent(const float current_a)
105108
{
106-
_current_a = current_a;
109+
// Overwrite the measured current if current overwrite is defined and vehicle is unarmed
110+
if (!_armed && _params.i_overwrite > FLT_EPSILON) {
111+
_current_a = _params.i_overwrite;
112+
113+
} else {
114+
_current_a = current_a;
115+
}
107116
}
108117

109118
void Battery::updateTemperature(const float temperature_c)
@@ -144,6 +153,14 @@ void Battery::updateBatteryStatus(const hrt_abstime &timestamp)
144153
if (_connected && _battery_initialized) {
145154
_warning = determineWarning(_state_of_charge);
146155
}
156+
157+
if (_vehicle_status_sub.updated()) {
158+
vehicle_status_s vehicle_status;
159+
160+
if (_vehicle_status_sub.copy(&vehicle_status)) {
161+
_armed = (vehicle_status.arming_state == vehicle_status_s::ARMING_STATE_ARMED);
162+
}
163+
}
147164
}
148165

149166
battery_status_s Battery::getBatteryStatus()
@@ -359,7 +376,6 @@ float Battery::computeRemainingTime(float current_a)
359376
vehicle_status_s vehicle_status;
360377

361378
if (_vehicle_status_sub.copy(&vehicle_status)) {
362-
_armed = (vehicle_status.arming_state == vehicle_status_s::ARMING_STATE_ARMED);
363379

364380
if (vehicle_status.vehicle_type == vehicle_status_s::VEHICLE_TYPE_FIXED_WING && !_vehicle_status_is_fw) {
365381
reset_current_avg_filter = true;
@@ -412,6 +428,7 @@ void Battery::updateParams()
412428
param_get(_param_handles.crit_thr, &_params.crit_thr);
413429
param_get(_param_handles.emergen_thr, &_params.emergen_thr);
414430
param_get(_param_handles.bat_avrg_current, &_params.bat_avrg_current);
431+
param_get(_param_handles.i_overwrite, &_params.i_overwrite);
415432

416433
float capacity{0.f};
417434
param_get(_param_handles.capacity, &capacity);

src/lib/battery/battery.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class Battery : public ModuleParams
147147
param_t emergen_thr;
148148
param_t source;
149149
param_t bat_avrg_current;
150+
param_t i_overwrite;
150151
} _param_handles{};
151152

152153
struct {
@@ -159,6 +160,7 @@ class Battery : public ModuleParams
159160
float emergen_thr;
160161
int32_t source;
161162
float bat_avrg_current;
163+
float i_overwrite;
162164
} _params{};
163165

164166
const int _index;

src/lib/battery/module.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,23 @@ parameters:
126126
instance_start: 1
127127
default: [0, -1, -1]
128128

129+
BAT${i}_I_OVERWRITE:
130+
description:
131+
short: Battery ${i} idle current overwrite
132+
long: |
133+
This parameter allows to overwrite the current measured during
134+
idle (unarmed) state with a user-defined constant value (expressed in amperes).
135+
When the system is armed, the measured current is used. This is useful
136+
because on certain ESCs current measurements are inaccurate in case of no load.
137+
Negative values are ignored and will cause the measured current to be used.
138+
The default value of 0 disables the overwrite, in which case the measured value
139+
is always used.
140+
type: float
141+
decimal: 2
142+
num_instances: *max_num_config_instances
143+
instance_start: 1
144+
default: [0, 0, 0]
145+
129146
BAT_LOW_THR:
130147
description:
131148
short: Low threshold.

src/modules/battery_status/analog_battery.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ AnalogBattery::AnalogBattery(int index, ModuleParams *parent, const int sample_i
6767
snprintf(param_name, sizeof(param_name), "BAT%d_V_CHANNEL", index);
6868
_analog_param_handles.v_channel = param_find(param_name);
6969

70-
snprintf(param_name, sizeof(param_name), "BAT%d_I_OVERWRITE", index);
71-
_analog_param_handles.i_overwrite = param_find(param_name);
72-
7370
snprintf(param_name, sizeof(param_name), "BAT%d_V_FILT", index);
7471
_analog_param_handles.v_filt = param_find(param_name);
7572

@@ -102,15 +99,6 @@ AnalogBattery::updateBatteryStatusADC(hrt_abstime timestamp, float voltage_raw,
10299
}
103100
}
104101

105-
// Overwrite the measured current if current overwrite is defined and vehicle is unarmed
106-
if (_analog_params.i_overwrite > 0) {
107-
updateTopics();
108-
109-
if (_arming_state == vehicle_status_s::ARMING_STATE_DISARMED) {
110-
current_a = _analog_params.i_overwrite;
111-
}
112-
}
113-
114102
Battery::setConnected(connected);
115103
Battery::updateVoltage(voltage_v);
116104
Battery::updateCurrent(current_a);
@@ -153,7 +141,6 @@ AnalogBattery::updateParams()
153141
param_get(_analog_param_handles.v_div, &_analog_params.v_div);
154142
param_get(_analog_param_handles.a_per_v, &_analog_params.a_per_v);
155143
param_get(_analog_param_handles.v_channel, &_analog_params.v_channel);
156-
param_get(_analog_param_handles.i_overwrite, &_analog_params.i_overwrite);
157144
param_get(_analog_param_handles.v_offs_cur, &_analog_params.v_offs_cur);
158145
param_get(_analog_param_handles.v_filt, &_analog_params.v_filt);
159146
param_get(_analog_param_handles.i_filt, &_analog_params.i_filt);
@@ -168,12 +155,3 @@ AnalogBattery::updateParams()
168155

169156
Battery::updateParams();
170157
}
171-
172-
void AnalogBattery::updateTopics()
173-
{
174-
vehicle_status_s vehicle_status;
175-
176-
if (_vehicle_status_sub.update(&vehicle_status)) {
177-
_arming_state = vehicle_status.arming_state;
178-
}
179-
}

src/modules/battery_status/analog_battery.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
#include <battery/battery.h>
3737
#include <lib/mathlib/math/filter/AlphaFilter.hpp>
3838
#include <parameters/param.h>
39-
#include <uORB/topics/vehicle_status.h>
40-
4139
class AnalogBattery : public Battery
4240
{
4341
public:
@@ -78,7 +76,6 @@ class AnalogBattery : public Battery
7876
param_t v_div;
7977
param_t a_per_v;
8078
param_t v_channel;
81-
param_t i_overwrite;
8279
param_t v_filt;
8380
param_t i_filt;
8481
} _analog_param_handles;
@@ -88,7 +85,6 @@ class AnalogBattery : public Battery
8885
float v_div;
8986
float a_per_v;
9087
int32_t v_channel;
91-
float i_overwrite;
9288
float v_filt;
9389
float i_filt;
9490
} _analog_params;
@@ -99,12 +95,7 @@ class AnalogBattery : public Battery
9995

10096
static constexpr int V_CHANNEL_DISABLED = -2;
10197

102-
uORB::Subscription _vehicle_status_sub{ORB_ID(vehicle_status)};
103-
uint8_t _arming_state{0};
104-
10598
hrt_abstime _last_timestamp{0};
10699
AlphaFilter<float> _voltage_filter{};
107100
AlphaFilter<float> _current_filter{};
108-
109-
void updateTopics();
110101
};

src/modules/battery_status/module.yaml

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,6 @@ parameters:
5151
instance_start: 1
5252
default: [-1, -1]
5353

54-
BAT${i}_I_OVERWRITE:
55-
description:
56-
short: Battery ${i} idle current overwrite
57-
long: |
58-
This parameter allows to overwrite the current measured during
59-
idle (unarmed) state with a user-defined constant value (expressed in amperes).
60-
When the system is armed, the measured current is used. This is useful
61-
because on certain ESCs current measurements are inaccurate in case of no load.
62-
Negative values are ignored and will cause the measured current to be used.
63-
The default value of 0 disables the overwrite, in which case the measured value
64-
is always used.
65-
66-
type: float
67-
decimal: 8
68-
reboot_required: true
69-
num_instances: *max_num_config_instances
70-
instance_start: 1
71-
default: [0, 0]
72-
7354
BAT${i}_V_FILT:
7455
description:
7556
short: Battery ${i} voltage filter time constant

0 commit comments

Comments
 (0)