Skip to content

Commit b2fd58d

Browse files
committed
pbio/battery: Use a single averaging calculation.
The one in pbsys did not converge and we should do this only once to reduce code size. Fixes pybricks/support#2055
1 parent 78d4383 commit b2fd58d

File tree

2 files changed

+12
-52
lines changed

2 files changed

+12
-52
lines changed

lib/pbio/src/battery.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
#include <pbio/battery.h>
1515
#include <pbio/int_math.h>
1616

17+
#if !PBIO_CONFIG_MOTOR_PROCESS
18+
#error "PBIO_CONFIG_MOTOR_PROCESS must be enabled to continously update battery voltage."
19+
#endif
20+
1721
// Slow moving average battery voltage.
1822
static int32_t battery_voltage_avg_scaled;
1923

lib/pbio/sys/battery.c

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77
// TODO: need to handle battery pack switch and Li-ion batteries for Technic Hub and NXT
88

99
#include <pbdrv/battery.h>
10+
#include <pbio/battery.h>
1011
#include <pbdrv/charger.h>
1112
#include <pbdrv/config.h>
1213
#include <pbdrv/clock.h>
1314
#include <pbdrv/usb.h>
1415
#include <pbsys/status.h>
1516

16-
// period over which the battery voltage is averaged (in milliseconds)
17-
#define BATTERY_PERIOD_MS 2500
18-
1917
// These values are for Alkaline (AA/AAA) batteries
2018
#define BATTERY_OK_MV 6000 // 1.0V per cell
2119
#define BATTERY_LOW_MV 5400 // 0.9V per cell
@@ -27,46 +25,7 @@
2725
#define LIION_LOW_MV 6800 // 3.4V per cell
2826
#define LIION_CRITICAL_MV 6000 // 3.0V per cell
2927

30-
static uint32_t prev_poll_time;
31-
static uint16_t avg_battery_voltage;
32-
33-
#if PBDRV_CONFIG_BATTERY_ADC_TYPE == 1
34-
// special case to reduce code size on Move hub
35-
#define battery_critical_mv BATTERY_CRITICAL_MV
36-
#define battery_low_mv BATTERY_LOW_MV
37-
#define battery_ok_mv BATTERY_OK_MV
38-
#else
39-
static uint16_t battery_critical_mv;
40-
static uint16_t battery_low_mv;
41-
static uint16_t battery_ok_mv;
42-
#endif
43-
44-
/**
45-
* Initializes the system battery monitor.
46-
*/
4728
void pbsys_battery_init(void) {
48-
#if PBDRV_CONFIG_BATTERY_ADC_TYPE != 1
49-
pbdrv_battery_type_t type;
50-
if (pbdrv_battery_get_type(&type) == PBIO_SUCCESS && type == PBDRV_BATTERY_TYPE_LIION) {
51-
battery_critical_mv = LIION_CRITICAL_MV;
52-
battery_low_mv = LIION_LOW_MV;
53-
battery_ok_mv = LIION_OK_MV;
54-
} else {
55-
battery_critical_mv = BATTERY_CRITICAL_MV;
56-
battery_low_mv = BATTERY_LOW_MV;
57-
battery_ok_mv = BATTERY_OK_MV;
58-
}
59-
#endif
60-
61-
pbdrv_battery_get_voltage_now(&avg_battery_voltage);
62-
// This is mainly for the Technic Hub. It seems that the first battery voltage
63-
// read is always low and causes the hub to shut down because of low battery
64-
// voltage even though the battery isn't that low.
65-
if (avg_battery_voltage < battery_critical_mv) {
66-
avg_battery_voltage = battery_ok_mv;
67-
}
68-
69-
prev_poll_time = pbdrv_clock_get_ms();
7029
}
7130

7231
/**
@@ -75,18 +34,15 @@ void pbsys_battery_init(void) {
7534
* This is called periodically to update the current battery state.
7635
*/
7736
void pbsys_battery_poll(void) {
78-
uint32_t now;
79-
uint32_t poll_interval;
80-
uint16_t battery_voltage;
8137

82-
now = pbdrv_clock_get_ms();
83-
poll_interval = now - prev_poll_time;
84-
prev_poll_time = now;
38+
pbdrv_battery_type_t type;
39+
bool is_liion = pbdrv_battery_get_type(&type) == PBIO_SUCCESS && type == PBDRV_BATTERY_TYPE_LIION;
8540

86-
pbdrv_battery_get_voltage_now(&battery_voltage);
41+
uint32_t battery_critical_mv = is_liion ? LIION_CRITICAL_MV : BATTERY_CRITICAL_MV;
42+
uint32_t battery_low_mv = is_liion ? LIION_LOW_MV : BATTERY_LOW_MV;
43+
uint32_t battery_ok_mv = is_liion ? LIION_OK_MV : BATTERY_OK_MV;
8744

88-
avg_battery_voltage = (avg_battery_voltage * (BATTERY_PERIOD_MS - poll_interval)
89-
+ battery_voltage * poll_interval) / BATTERY_PERIOD_MS;
45+
uint32_t avg_battery_voltage = pbio_battery_get_average_voltage();
9046

9147
if (avg_battery_voltage <= battery_critical_mv) {
9248
pbsys_status_set(PBIO_PYBRICKS_STATUS_BATTERY_LOW_VOLTAGE_SHUTDOWN);
@@ -107,5 +63,5 @@ void pbsys_battery_poll(void) {
10763
* This is only valid on hubs with a built-in battery charger.
10864
*/
10965
bool pbsys_battery_is_full(void) {
110-
return avg_battery_voltage >= LIION_FULL_MV;
66+
return pbio_battery_get_average_voltage() >= LIION_FULL_MV;
11167
}

0 commit comments

Comments
 (0)