Skip to content

Commit 7e92ce3

Browse files
authored
Merge pull request #10103 from iNavFlight/MrD_Make-battery_capacity_unit-global
Make `battery_capacity_unit` a global setting
2 parents 864bb15 + c012de6 commit 7e92ce3

File tree

7 files changed

+34
-35
lines changed

7 files changed

+34
-35
lines changed

docs/Battery.md

-3
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ Up to 3 battery profiles are supported. You can select the battery profile from
201201
- `vbat_max_cell_voltage`
202202
- `vbat_warning_cell_voltage`
203203
- `vbat_min_cell_voltage`
204-
- `battery_capacity_unit`
205204
- `battery_capacity`
206205
- `battery_capacity_warning`
207206
- `battery_capacity_critical`
@@ -253,7 +252,6 @@ feature BAT_PROF_AUTOSWITCH
253252
battery_profile 1
254253
255254
set bat_cells = 3
256-
set battery_capacity_unit = MAH
257255
set battery_capacity = 2200
258256
set battery_capacity_warning = 440
259257
set battery_capacity_critical = 220
@@ -262,7 +260,6 @@ set battery_capacity_critical = 220
262260
battery_profile 2
263261
264262
set bat_cells = 4
265-
set battery_capacity_unit = MAH
266263
set battery_capacity = 1500
267264
set battery_capacity_warning = 300
268265
set battery_capacity_critical = 150

src/main/fc/fc_msp.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ static bool mspFcProcessOutCommand(uint16_t cmdMSP, sbuf_t *dst, mspPostProcessF
856856
sbufWriteU32(dst, currentBatteryProfile->capacity.value);
857857
sbufWriteU32(dst, currentBatteryProfile->capacity.warning);
858858
sbufWriteU32(dst, currentBatteryProfile->capacity.critical);
859-
sbufWriteU8(dst, currentBatteryProfile->capacity.unit);
859+
sbufWriteU8(dst, batteryMetersConfig()->capacity_unit);
860860
break;
861861

862862
case MSP2_INAV_MISC2:
@@ -895,7 +895,7 @@ static bool mspFcProcessOutCommand(uint16_t cmdMSP, sbuf_t *dst, mspPostProcessF
895895
sbufWriteU32(dst, currentBatteryProfile->capacity.value);
896896
sbufWriteU32(dst, currentBatteryProfile->capacity.warning);
897897
sbufWriteU32(dst, currentBatteryProfile->capacity.critical);
898-
sbufWriteU8(dst, currentBatteryProfile->capacity.unit);
898+
sbufWriteU8(dst, batteryMetersConfig()->capacity_unit);
899899
break;
900900

901901
#ifdef USE_GPS
@@ -2081,13 +2081,13 @@ static mspResult_e mspFcProcessInCommand(uint16_t cmdMSP, sbuf_t *src)
20812081
currentBatteryProfileMutable->capacity.value = sbufReadU32(src);
20822082
currentBatteryProfileMutable->capacity.warning = sbufReadU32(src);
20832083
currentBatteryProfileMutable->capacity.critical = sbufReadU32(src);
2084-
currentBatteryProfileMutable->capacity.unit = sbufReadU8(src);
2084+
batteryMetersConfigMutable()->capacity_unit = sbufReadU8(src);
20852085
if ((batteryMetersConfig()->voltageSource != BAT_VOLTAGE_RAW) && (batteryMetersConfig()->voltageSource != BAT_VOLTAGE_SAG_COMP)) {
20862086
batteryMetersConfigMutable()->voltageSource = BAT_VOLTAGE_RAW;
20872087
return MSP_RESULT_ERROR;
20882088
}
2089-
if ((currentBatteryProfile->capacity.unit != BAT_CAPACITY_UNIT_MAH) && (currentBatteryProfile->capacity.unit != BAT_CAPACITY_UNIT_MWH)) {
2090-
currentBatteryProfileMutable->capacity.unit = BAT_CAPACITY_UNIT_MAH;
2089+
if ((batteryMetersConfig()->capacity_unit != BAT_CAPACITY_UNIT_MAH) && (batteryMetersConfig()->capacity_unit != BAT_CAPACITY_UNIT_MWH)) {
2090+
batteryMetersConfigMutable()->capacity_unit = BAT_CAPACITY_UNIT_MAH;
20912091
return MSP_RESULT_ERROR;
20922092
}
20932093
} else
@@ -2120,13 +2120,13 @@ static mspResult_e mspFcProcessInCommand(uint16_t cmdMSP, sbuf_t *src)
21202120
currentBatteryProfileMutable->capacity.value = sbufReadU32(src);
21212121
currentBatteryProfileMutable->capacity.warning = sbufReadU32(src);
21222122
currentBatteryProfileMutable->capacity.critical = sbufReadU32(src);
2123-
currentBatteryProfileMutable->capacity.unit = sbufReadU8(src);
2123+
batteryMetersConfigMutable()->capacity_unit = sbufReadU8(src);
21242124
if ((batteryMetersConfig()->voltageSource != BAT_VOLTAGE_RAW) && (batteryMetersConfig()->voltageSource != BAT_VOLTAGE_SAG_COMP)) {
21252125
batteryMetersConfigMutable()->voltageSource = BAT_VOLTAGE_RAW;
21262126
return MSP_RESULT_ERROR;
21272127
}
2128-
if ((currentBatteryProfile->capacity.unit != BAT_CAPACITY_UNIT_MAH) && (currentBatteryProfile->capacity.unit != BAT_CAPACITY_UNIT_MWH)) {
2129-
currentBatteryProfileMutable->capacity.unit = BAT_CAPACITY_UNIT_MAH;
2128+
if ((batteryMetersConfig()->capacity_unit != BAT_CAPACITY_UNIT_MAH) && (batteryMetersConfig()->capacity_unit != BAT_CAPACITY_UNIT_MWH)) {
2129+
batteryMetersConfigMutable()->capacity_unit = BAT_CAPACITY_UNIT_MAH;
21302130
return MSP_RESULT_ERROR;
21312131
}
21322132
} else

src/main/fc/settings.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,12 @@ groups:
890890
condition: USE_ADC
891891
min: 0
892892
max: 65535
893+
- name: battery_capacity_unit
894+
description: "Unit used for `battery_capacity`, `battery_capacity_warning` and `battery_capacity_critical` [MAH/MWH] (milliAmpere hour / milliWatt hour)."
895+
default_value: "MAH"
896+
field: capacity_unit
897+
table: bat_capacity_unit
898+
type: uint8_t
893899
- name: current_meter_scale
894900
description: "This sets the output voltage to current scaling for the current sensor in 0.1 mV/A steps. 400 is 40mV/A such as the ACS756 sensor outputs. 183 is the setting for the uberdistro with a 0.25mOhm shunt."
895901
default_value: :target
@@ -996,12 +1002,6 @@ groups:
9961002
field: capacity.critical
9971003
min: 0
9981004
max: 4294967295
999-
- name: battery_capacity_unit
1000-
description: "Unit used for `battery_capacity`, `battery_capacity_warning` and `battery_capacity_critical` [MAH/MWH] (milliAmpere hour / milliWatt hour)."
1001-
default_value: "MAH"
1002-
field: capacity.unit
1003-
table: bat_capacity_unit
1004-
type: uint8_t
10051005
- name: controlrate_profile
10061006
description: "Control rate profile to switch to when the battery profile is selected, 0 to disable and keep the currently selected control rate profile"
10071007
default_value: 0

src/main/flight/rth_estimator.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ float calculateRemainingDistanceBeforeRTH(bool takeWindIntoAccount) {
222222

223223
// check requirements
224224
const bool areBatterySettingsOK = feature(FEATURE_VBAT) && feature(FEATURE_CURRENT_METER) && batteryWasFullWhenPluggedIn();
225-
const bool areRTHEstimatorSettingsOK = batteryMetersConfig()->cruise_power > 0 && currentBatteryProfile->capacity.unit == BAT_CAPACITY_UNIT_MWH &&currentBatteryProfile->capacity.value > 0 && navConfig()->fw.cruise_speed > 0;
225+
const bool areRTHEstimatorSettingsOK = batteryMetersConfig()->cruise_power > 0 && batteryMetersConfig()->capacity_unit == BAT_CAPACITY_UNIT_MWH && currentBatteryProfile->capacity.value > 0 && navConfig()->fw.cruise_speed > 0;
226226
const bool isNavigationOK = navigationPositionEstimateIsHealthy() && isImuHeadingValid();
227227

228228
if (!(areBatterySettingsOK && areRTHEstimatorSettingsOK && isNavigationOK)) {

src/main/io/osd.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,7 @@ static bool osdDrawSingleElement(uint8_t item)
17801780
tfp_sprintf(buff, " NA");
17811781
else if (!batteryWasFullWhenPluggedIn())
17821782
tfp_sprintf(buff, " NF");
1783-
else if (currentBatteryProfile->capacity.unit == BAT_CAPACITY_UNIT_MAH) {
1783+
else if (batteryMetersConfig()->capacity_unit == BAT_CAPACITY_UNIT_MAH) {
17841784
uint8_t mah_digits = osdConfig()->mAh_precision; // Initialize to config value
17851785

17861786
#ifndef DISABLE_MSP_DJI_COMPAT // IF DJICOMPAT is not supported, there's no need to check for it
@@ -1803,11 +1803,11 @@ static bool osdDrawSingleElement(uint8_t item)
18031803
buff[mah_digits + 1] = '\0';
18041804
unitsDrawn = true;
18051805
}
1806-
} else // currentBatteryProfile->capacity.unit == BAT_CAPACITY_UNIT_MWH
1806+
} else // batteryMetersConfig()->capacityUnit == BAT_CAPACITY_UNIT_MWH
18071807
osdFormatCentiNumber(buff + 1, getBatteryRemainingCapacity() / 10, 0, 2, 0, 3, false);
18081808

18091809
if (!unitsDrawn) {
1810-
buff[4] = currentBatteryProfile->capacity.unit == BAT_CAPACITY_UNIT_MAH ? SYM_MAH : SYM_WH;
1810+
buff[4] = batteryMetersConfig()->capacity_unit == BAT_CAPACITY_UNIT_MAH ? SYM_MAH : SYM_WH;
18111811
buff[5] = '\0';
18121812
}
18131813

src/main/sensors/battery.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static int32_t mWhDrawn = 0; // energy (milliWatt hours) draw
9797
batteryState_e batteryState;
9898
const batteryProfile_t *currentBatteryProfile;
9999

100-
PG_REGISTER_ARRAY_WITH_RESET_FN(batteryProfile_t, MAX_BATTERY_PROFILE_COUNT, batteryProfiles, PG_BATTERY_PROFILES, 2);
100+
PG_REGISTER_ARRAY_WITH_RESET_FN(batteryProfile_t, MAX_BATTERY_PROFILE_COUNT, batteryProfiles, PG_BATTERY_PROFILES, 3);
101101

102102
void pgResetFn_batteryProfiles(batteryProfile_t *instance)
103103
{
@@ -118,7 +118,6 @@ void pgResetFn_batteryProfiles(batteryProfile_t *instance)
118118
.value = SETTING_BATTERY_CAPACITY_DEFAULT,
119119
.warning = SETTING_BATTERY_CAPACITY_WARNING_DEFAULT,
120120
.critical = SETTING_BATTERY_CAPACITY_CRITICAL_DEFAULT,
121-
.unit = SETTING_BATTERY_CAPACITY_UNIT_DEFAULT,
122121
},
123122

124123
.controlRateProfile = 0,
@@ -167,7 +166,7 @@ void pgResetFn_batteryProfiles(batteryProfile_t *instance)
167166
}
168167
}
169168

170-
PG_REGISTER_WITH_RESET_TEMPLATE(batteryMetersConfig_t, batteryMetersConfig, PG_BATTERY_METERS_CONFIG, 1);
169+
PG_REGISTER_WITH_RESET_TEMPLATE(batteryMetersConfig_t, batteryMetersConfig, PG_BATTERY_METERS_CONFIG, 2);
171170

172171
PG_RESET_TEMPLATE(batteryMetersConfig_t, batteryMetersConfig,
173172

@@ -186,6 +185,8 @@ PG_RESET_TEMPLATE(batteryMetersConfig_t, batteryMetersConfig,
186185

187186
.voltageSource = SETTING_BAT_VOLTAGE_SRC_DEFAULT,
188187

188+
.capacity_unit = SETTING_BATTERY_CAPACITY_UNIT_DEFAULT,
189+
189190
.cruise_power = SETTING_CRUISE_POWER_DEFAULT,
190191
.idle_power = SETTING_IDLE_POWER_DEFAULT,
191192
.rth_energy_margin = SETTING_RTH_ENERGY_MARGIN_DEFAULT,
@@ -405,7 +406,7 @@ void batteryUpdate(timeUs_t timeDelta)
405406

406407
if ((currentBatteryProfile->capacity.value > 0) && batteryFullWhenPluggedIn) {
407408
uint32_t capacityDiffBetweenFullAndEmpty = currentBatteryProfile->capacity.value - currentBatteryProfile->capacity.critical;
408-
int32_t drawn = (currentBatteryProfile->capacity.unit == BAT_CAPACITY_UNIT_MWH ? mWhDrawn : mAhDrawn);
409+
int32_t drawn = (batteryMetersConfig()->capacity_unit == BAT_CAPACITY_UNIT_MWH ? mWhDrawn : mAhDrawn);
409410
batteryRemainingCapacity = (drawn > (int32_t)capacityDiffBetweenFullAndEmpty ? 0 : capacityDiffBetweenFullAndEmpty - drawn);
410411
}
411412

src/main/sensors/battery_config_structs.h

+11-10
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,18 @@ typedef struct batteryMetersConfig_s {
6161
#endif
6262

6363
struct {
64-
int16_t scale; // scale the current sensor output voltage to milliamps. Value in 1/10th mV/A
65-
int16_t offset; // offset of the current sensor in millivolt steps
66-
currentSensor_e type; // type of current meter used, either ADC or virtual
64+
int16_t scale; // scale the current sensor output voltage to milliamps. Value in 1/10th mV/A
65+
int16_t offset; // offset of the current sensor in millivolt steps
66+
currentSensor_e type; // type of current meter used, either ADC or virtual
6767
} current;
6868

6969
batVoltageSource_e voltageSource;
7070

71-
uint32_t cruise_power; // power drawn by the motor(s) at cruise throttle/speed (cW)
72-
uint16_t idle_power; // power drawn by the system when the motor(s) are stopped (cW)
73-
uint8_t rth_energy_margin; // Energy that should be left after RTH (%), used for remaining time/distance before RTH
71+
batCapacityUnit_e capacity_unit; // Describes unit of capacity.value, capacity.warning and capacity.critical
72+
73+
uint32_t cruise_power; // power drawn by the motor(s) at cruise throttle/speed (cW)
74+
uint16_t idle_power; // power drawn by the system when the motor(s) are stopped (cW)
75+
uint8_t rth_energy_margin; // Energy that should be left after RTH (%), used for remaining time/distance before RTH
7476

7577
float throttle_compensation_weight;
7678

@@ -90,10 +92,9 @@ typedef struct batteryProfile_s {
9092
#endif
9193

9294
struct {
93-
uint32_t value; // mAh or mWh (see capacity.unit)
94-
uint32_t warning; // mAh or mWh (see capacity.unit)
95-
uint32_t critical; // mAh or mWh (see capacity.unit)
96-
batCapacityUnit_e unit; // Describes unit of capacity.value, capacity.warning and capacity.critical
95+
uint32_t value; // mAh or mWh (see batteryMetersConfig()->capacity_unit)
96+
uint32_t warning; // mAh or mWh (see batteryMetersConfig()->capacity_unit)
97+
uint32_t critical; // mAh or mWh (see batteryMetersConfig()->capacity_unit)
9798
} capacity;
9899

99100
uint8_t controlRateProfile;

0 commit comments

Comments
 (0)