Skip to content

Commit

Permalink
vbat: use dedicated task
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Jul 6, 2024
1 parent ea9812f commit 773522f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
4 changes: 1 addition & 3 deletions src/core/tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
#include "rx/rx.h"

void util_task() {
// battery low logic
vbat_calc();

// check gestures
gestures();

Expand All @@ -45,6 +42,7 @@ FAST_RAM task_t tasks[TASK_MAX] = {
[TASK_IMU] = CREATE_TASK("IMU", TASK_MASK_ALWAYS, TASK_PRIORITY_REALTIME, imu_calc, 0),
[TASK_PID] = CREATE_TASK("PID", TASK_MASK_ALWAYS, TASK_PRIORITY_REALTIME, control, 0),
[TASK_RX] = CREATE_TASK("RX", TASK_MASK_ALWAYS, TASK_PRIORITY_REALTIME, rx_update, 0),
[TASK_VBAT] = CREATE_TASK("VBAT", TASK_MASK_ALWAYS, TASK_PRIORITY_HIGH, vbat_calc, 1000),
[TASK_UTIL] = CREATE_TASK("UTIL", TASK_MASK_ALWAYS, TASK_PRIORITY_HIGH, util_task, 0),
[TASK_BLACKBOX] = CREATE_TASK("BLACKBOX", TASK_MASK_ALWAYS, TASK_PRIORITY_MEDIUM, blackbox_update, 0),
[TASK_OSD] = CREATE_TASK("OSD", TASK_MASK_ALWAYS, TASK_PRIORITY_MEDIUM, osd_display, 8000),
Expand Down
1 change: 1 addition & 0 deletions src/core/tasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ typedef enum {
TASK_IMU,
TASK_PID,
TASK_RX,
TASK_VBAT,
TASK_UTIL,
TASK_BLACKBOX,
TASK_OSD,
Expand Down
10 changes: 5 additions & 5 deletions src/io/vbat.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void vbat_init() {
int count = 0;
while (count < 5000) {
state.vbat = adc_read(ADC_CHAN_VBAT);
lpf(&state.vbat_filtered, state.vbat, 0.9968f);
lpf(&state.vbat_filtered, state.vbat, lpfcalc(1, 500));
count++;
}

Expand Down Expand Up @@ -87,20 +87,20 @@ void vbat_calc() {

// read acd and scale based on processor voltage
state.ibat = adc_read(ADC_CHAN_IBAT);
lpf(&state.ibat_filtered, state.ibat, lpfcalc(1000, 5000e3));
lpf(&state.ibat_filtered, state.ibat, lpfcalc(1, 10000));

// li-ion battery model compensation time decay ( 18 seconds )
state.vbat = adc_read(ADC_CHAN_VBAT);
lpf(&state.vbat_filtered, state.vbat, 0.9968f);
lpf(&state.vbat_filtered_decay, state.vbat_filtered, lpfcalc(1000, 18000e3));
lpf(&state.vbat_filtered, state.vbat, lpfcalc(1, 500));
lpf(&state.vbat_filtered_decay, state.vbat_filtered, lpfcalc(1, 18000));

state.vbat_cell_avg = state.vbat_filtered_decay / (float)state.lipo_cell_count;

// average of all motors
// filter motorpwm so it has the same delay as the filtered voltage
// ( or they can use a single filter)
static float thrfilt = 0;
lpf(&thrfilt, state.thrsum, 0.9968f); // 0.5 sec at 1.6ms loop time
lpf(&thrfilt, state.thrsum, lpfcalc(1, 500));

const float tempvolt = state.vbat_filtered * (1.00f + CF1) - state.vbat_filtered_decay * (CF1);

Expand Down

0 comments on commit 773522f

Please sign in to comment.