Skip to content

Commit ebf140f

Browse files
committed
pbio/battery: Use own process.
This used to be mixed in with the motor process to save a little bit of space, but it is clearer to have the averaging process be inside the battery module. This will allow us to stop the motor process on shutdown without stopping the battery process.
1 parent 148a93e commit ebf140f

File tree

4 files changed

+47
-59
lines changed

4 files changed

+47
-59
lines changed

lib/pbio/include/pbio/battery.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
#if PBIO_CONFIG_BATTERY
2828

2929
/** @cond INTERNAL */
30-
pbio_error_t pbio_battery_init(void);
31-
pbio_error_t pbio_battery_update(void);
30+
void pbio_battery_init(void);
3231
/** @endcond */
3332
int32_t pbio_battery_get_average_voltage(void);
3433
int32_t pbio_battery_get_duty_from_voltage(int32_t voltage);
@@ -37,12 +36,7 @@ int32_t pbio_battery_get_voltage_from_duty_pct(int32_t duty);
3736

3837
#else // PBIO_CONFIG_BATTERY
3938

40-
static inline pbio_error_t pbio_battery_init(void) {
41-
return PBIO_ERROR_NOT_SUPPORTED;
42-
}
43-
44-
static inline pbio_error_t pbio_battery_update(void) {
45-
return PBIO_ERROR_NOT_SUPPORTED;
39+
static inline void pbio_battery_init(void) {
4640
}
4741

4842
static inline int32_t pbio_battery_get_average_voltage(void) {

lib/pbio/src/battery.c

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
#include <pbdrv/battery.h>
1414
#include <pbio/battery.h>
1515
#include <pbio/int_math.h>
16-
17-
#if !PBIO_CONFIG_MOTOR_PROCESS
18-
#error "PBIO_CONFIG_MOTOR_PROCESS must be enabled to continously update battery voltage."
19-
#endif
16+
#include <pbio/os.h>
2017

2118
// Slow moving average battery voltage.
2219
static int32_t battery_voltage_avg_scaled;
@@ -25,46 +22,6 @@ static int32_t battery_voltage_avg_scaled;
2522
// to reduce rounding errors in the moving average.
2623
#define SCALE (1024)
2724

28-
/**
29-
* Initializes battery voltage state to first measurement.
30-
*
31-
* @return Error code.
32-
*/
33-
pbio_error_t pbio_battery_init(void) {
34-
35-
// Get battery voltage.
36-
uint16_t battery_voltage_now_mv;
37-
pbio_error_t err = pbdrv_battery_get_voltage_now(&battery_voltage_now_mv);
38-
if (err != PBIO_SUCCESS) {
39-
return err;
40-
}
41-
42-
// Initialize average voltage.
43-
battery_voltage_avg_scaled = (int32_t)battery_voltage_now_mv * SCALE;
44-
45-
return PBIO_SUCCESS;
46-
}
47-
48-
/**
49-
* Updates the average voltage using a new measurement.
50-
*
51-
* @return Error code.
52-
*/
53-
pbio_error_t pbio_battery_update(void) {
54-
55-
// Get battery voltage.
56-
uint16_t battery_voltage_now_mv;
57-
pbio_error_t err = pbdrv_battery_get_voltage_now(&battery_voltage_now_mv);
58-
if (err != PBIO_SUCCESS) {
59-
return err;
60-
}
61-
62-
// Update moving average.
63-
battery_voltage_avg_scaled = (battery_voltage_avg_scaled * 127 + ((int32_t)battery_voltage_now_mv) * SCALE) / 128;
64-
65-
return PBIO_SUCCESS;
66-
}
67-
6825
/**
6926
* Gets the moving average battery voltage.
7027
*
@@ -116,4 +73,46 @@ int32_t pbio_battery_get_voltage_from_duty_pct(int32_t duty) {
11673
return duty * (battery_voltage_avg_scaled / SCALE) / 100;
11774
}
11875

76+
static pbio_os_process_t pbio_battery_process;
77+
78+
static pbio_error_t pbio_battery_process_thread(pbio_os_state_t *state, void *context) {
79+
80+
static pbio_os_timer_t timer;
81+
82+
PBIO_OS_ASYNC_BEGIN(state);
83+
84+
pbio_os_timer_set(&timer, PBIO_CONFIG_CONTROL_LOOP_TIME_MS);
85+
86+
for (;;) {
87+
uint16_t battery_voltage_now_mv;
88+
pbdrv_battery_get_voltage_now(&battery_voltage_now_mv);
89+
// Returned error is ignored.
90+
91+
// Update moving average.
92+
battery_voltage_avg_scaled = (battery_voltage_avg_scaled * 127 + ((int32_t)battery_voltage_now_mv) * SCALE) / 128;
93+
94+
pbio_os_timer_extend(&timer);
95+
PBIO_OS_AWAIT_UNTIL(state, pbio_os_timer_is_expired(&timer));
96+
}
97+
98+
// Unreachable.
99+
PBIO_OS_ASYNC_END(PBIO_ERROR_FAILED);
100+
}
101+
102+
/**
103+
* Initializes battery voltage state to first measurement.
104+
*/
105+
void pbio_battery_init(void) {
106+
107+
// Get battery voltage.
108+
uint16_t battery_voltage_now_mv;
109+
pbdrv_battery_get_voltage_now(&battery_voltage_now_mv);
110+
// Returned error is ignored.
111+
112+
// Initialize average upscaled voltage.
113+
battery_voltage_avg_scaled = (int32_t)battery_voltage_now_mv * SCALE;
114+
115+
pbio_os_process_start(&pbio_battery_process, pbio_battery_process_thread, NULL);
116+
}
117+
119118
#endif // PBIO_CONFIG_BATTERY

lib/pbio/src/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <pbdrv/sound.h>
1212

13+
#include <pbio/battery.h>
1314
#include <pbio/imu.h>
1415
#include <pbio/motor_process.h>
1516
#include <pbio/port_interface.h>
@@ -27,6 +28,7 @@
2728
*/
2829
void pbio_init(bool start_processes) {
2930

31+
pbio_battery_init();
3032
pbio_imu_init();
3133

3234
if (!start_processes) {

lib/pbio/src/motor_process.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include <pbdrv/clock.h>
55

6-
#include <pbio/battery.h>
76
#include <pbio/control.h>
87
#include <pbio/drivebase.h>
98
#include <pbio/servo.h>
@@ -23,13 +22,7 @@ static pbio_error_t pbio_motor_process_thread(pbio_os_state_t *state, void *cont
2322
timer.start = pbdrv_clock_get_ms() - PBIO_CONFIG_CONTROL_LOOP_TIME_MS;
2423
timer.duration = PBIO_CONFIG_CONTROL_LOOP_TIME_MS;
2524

26-
// Initialize battery voltage.
27-
pbio_battery_init();
28-
2925
for (;;) {
30-
// Update battery voltage.
31-
pbio_battery_update();
32-
3326
// Update drivebase
3427
pbio_drivebase_update_all();
3528

0 commit comments

Comments
 (0)