ACwing FC firmware
STM32WB55: attitude IEKF, altitude Kalman filter, flight core, protocol v2
Loading...
Searching...
No Matches
battery.c File Reference

Battery monitor (see battery.h). More...

#include "battery.h"
#include "proto_v2.h"
#include "spsc.h"
#include <math.h>
#include <string.h>
Include dependency graph for battery.c:

Go to the source code of this file.

Macros

#define DETECT_MS   500u /* battery present and stable before counting cells */
 
#define R_WIN_MS   100u /* window of the internal resistance estimate */
 
#define R_MIN_DI   0.5f /* A: load step needed for an estimate */
 
#define REST_I   0.3f /* A: below this the pack is "resting" */
 
#define SOC_REST_TAU   2.0f /* s */
 
#define SOC_V_TAU   10.0f /* s: voltage-only state of charge */
 
#define SOC_CORR_TAU   600.0f /* s: pull of the voltage on the coulomb counter */
 
#define I_SLOW_TAU   10.0f /* s: current used for the time left */
 

Functions

float batt_soc_from_ocv (float v)
 LiPo open-circuit voltage → state of charge 0…1 (exposed for the tests)
 
static float lpf (float y, float x, float dt, float tau)
 
static float clamp01 (float v)
 
void batt_init (const batt_cfg_t *cfg)
 
void batt_set_scales (float sv, float si)
 
void batt_get_cfg (batt_cfg_t *out)
 
const batt_state_t * batt_state (void)
 state as computed by the last step (same context)
 
void batt_read (batt_state_t *out)
 seqlock snapshot for other tasks; keeps *out on a race
 
static void publish (void)
 
static void lost_battery (void)
 
static void r_estimate (float v, float i, uint32_t dt)
 
static uint8_t level_of (float cv, const batt_limits_t *L, float hyst)
 
void batt_step (float pin_v, float cur_pin_v, uint32_t dt_ms, uint8_t armed, const batt_limits_t *L)
 one sample: pin voltages, time step, armed flag (flight-control context)
 

Variables

static batt_cfg_t s_cfg
 
static batt_state_t s
 
static uint32_t s_present_ms
 
static uint32_t s_absent_ms
 
static uint32_t s_below_ms [4]
 
static uint32_t s_recover_ms
 
static uint8_t s_have_anchor
 
static uint8_t s_have_f
 
static float s_soc_anchor
 
static float s_used_anchor
 
static float s_i_slow
 
static float s_wv
 
static float s_wi
 
static uint32_t s_wn
 
static uint32_t s_wms
 
static float s_pv
 
static float s_pi
 
static uint8_t s_have_pw
 
static volatile uint32_t s_seq
 
static batt_state_t s_pub
 
static const float k_ocv [21]
 

Detailed Description

Battery monitor (see battery.h).

Definition in file battery.c.

Macro Definition Documentation

◆ DETECT_MS

#define DETECT_MS   500u /* battery present and stable before counting cells */

Definition at line 11 of file battery.c.

◆ R_WIN_MS

#define R_WIN_MS   100u /* window of the internal resistance estimate */

Definition at line 12 of file battery.c.

◆ R_MIN_DI

#define R_MIN_DI   0.5f /* A: load step needed for an estimate */

Definition at line 13 of file battery.c.

◆ REST_I

#define REST_I   0.3f /* A: below this the pack is "resting" */

Definition at line 14 of file battery.c.

◆ SOC_REST_TAU

#define SOC_REST_TAU   2.0f /* s */

Definition at line 15 of file battery.c.

◆ SOC_V_TAU

#define SOC_V_TAU   10.0f /* s: voltage-only state of charge */

Definition at line 16 of file battery.c.

◆ SOC_CORR_TAU

#define SOC_CORR_TAU   600.0f /* s: pull of the voltage on the coulomb counter */

Definition at line 17 of file battery.c.

◆ I_SLOW_TAU

#define I_SLOW_TAU   10.0f /* s: current used for the time left */

Definition at line 18 of file battery.c.

Function Documentation

◆ batt_soc_from_ocv()

float batt_soc_from_ocv ( float  v)

LiPo open-circuit voltage → state of charge 0…1 (exposed for the tests)

Definition at line 41 of file battery.c.

References k_ocv.

Referenced by batt_step().

Here is the caller graph for this function:

◆ lpf()

static float lpf ( float  y,
float  x,
float  dt,
float  tau 
)
static

Definition at line 54 of file battery.c.

Referenced by batt_step().

Here is the caller graph for this function:

◆ clamp01()

static float clamp01 ( float  v)
static

Definition at line 55 of file battery.c.

Referenced by batt_step().

Here is the caller graph for this function:

◆ batt_init()

void batt_init ( const batt_cfg_t *  cfg)

Definition at line 57 of file battery.c.

References BATT_R_CELL_DEFAULT, batt_state_t::r_cell, batt_state_t::remain_s, s, s_absent_ms, s_below_ms, s_cfg, s_have_anchor, s_have_f, s_have_pw, s_i_slow, s_present_ms, s_pub, s_recover_ms, s_seq, s_soc_anchor, s_used_anchor, s_wi, s_wms, s_wn, s_wv, batt_cfg_t::scale_i, batt_cfg_t::scale_v, and batt_cfg_t::sens_v_per_a.

Referenced by batt_task_init().

Here is the caller graph for this function:

◆ batt_set_scales()

void batt_set_scales ( float  sv,
float  si 
)

Definition at line 75 of file battery.c.

References s_cfg, batt_cfg_t::scale_i, and batt_cfg_t::scale_v.

Referenced by batt_task_step().

Here is the caller graph for this function:

◆ batt_get_cfg()

void batt_get_cfg ( batt_cfg_t *  out)

Definition at line 81 of file battery.c.

References s_cfg.

Referenced by batt_task_step().

Here is the caller graph for this function:

◆ batt_state()

const batt_state_t * batt_state ( void  )

state as computed by the last step (same context)

Definition at line 82 of file battery.c.

References s.

Referenced by batt_task_step().

Here is the caller graph for this function:

◆ batt_read()

void batt_read ( batt_state_t *  out)

seqlock snapshot for other tasks; keeps *out on a race

Definition at line 84 of file battery.c.

References s_pub, s_seq, and SPSC_BARRIER.

Referenced by batt_now().

Here is the caller graph for this function:

◆ publish()

static void publish ( void  )
static

Definition at line 96 of file battery.c.

References s, s_pub, s_seq, and SPSC_BARRIER.

Referenced by batt_step().

Here is the caller graph for this function:

◆ lost_battery()

static void lost_battery ( void  )
static

Definition at line 105 of file battery.c.

References batt_state_t::cell_rest_v, batt_state_t::cell_v, batt_state_t::cells, batt_state_t::level, batt_state_t::pct, batt_state_t::present, PV2_BATT_OK, batt_state_t::remain_s, s, s_below_ms, s_have_anchor, s_have_pw, s_present_ms, s_recover_ms, and batt_state_t::soc.

Referenced by batt_step().

Here is the caller graph for this function:

◆ r_estimate()

static void r_estimate ( float  v,
float  i,
uint32_t  dt 
)
static

Definition at line 121 of file battery.c.

References batt_state_t::cells, batt_state_t::r_cell, R_MIN_DI, R_WIN_MS, s, s_have_pw, s_pi, s_pv, s_wi, s_wms, s_wn, and s_wv.

Referenced by batt_step().

Here is the caller graph for this function:

◆ level_of()

static uint8_t level_of ( float  cv,
const batt_limits_t *  L,
float  hyst 
)
static

Definition at line 137 of file battery.c.

References PV2_BATT_CRITICAL, PV2_BATT_EMERGENCY, PV2_BATT_LOW, and PV2_BATT_OK.

Referenced by batt_step().

Here is the caller graph for this function:

◆ batt_step()

void batt_step ( float  pin_v,
float  cur_pin_v,
uint32_t  dt_ms,
uint8_t  armed,
const batt_limits_t *  L 
)

one sample: pin voltages, time step, armed flag (flight-control context)

Definition at line 145 of file battery.c.

References BATT_ABSENT_V, BATT_CELL_DETECT_V, BATT_LEVEL_MS, BATT_PRESENT_V, BATT_RECOVER_HYST, BATT_RECOVER_MS, BATT_RESERVE_SOC, batt_soc_from_ocv(), batt_state_t::cell_rest_v, batt_state_t::cell_v, batt_state_t::cells, clamp01(), batt_state_t::current, DETECT_MS, batt_cfg_t::has_current, I_SLOW_TAU, L, batt_state_t::level, level_of(), lost_battery(), lpf(), n, batt_state_t::pct, batt_state_t::present, publish(), PV2_BATT_OK, batt_state_t::r_cell, r_estimate(), batt_state_t::remain_s, REST_I, s, s_absent_ms, s_below_ms, s_cfg, s_have_anchor, s_have_f, s_i_slow, s_present_ms, s_recover_ms, s_soc_anchor, s_used_anchor, batt_cfg_t::scale_i, batt_cfg_t::scale_v, batt_cfg_t::sens_v_per_a, batt_state_t::soc, SOC_CORR_TAU, SOC_REST_TAU, SOC_V_TAU, batt_state_t::used_mah, batt_state_t::vbat, and batt_state_t::vbat_raw.

Referenced by batt_task_step().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ s_cfg

batt_cfg_t s_cfg
static

Definition at line 20 of file battery.c.

Referenced by batt_get_cfg(), batt_init(), batt_set_scales(), and batt_step().

◆ s

◆ s_present_ms

uint32_t s_present_ms
static

Definition at line 22 of file battery.c.

Referenced by batt_init(), batt_step(), and lost_battery().

◆ s_absent_ms

uint32_t s_absent_ms
static

Definition at line 22 of file battery.c.

Referenced by batt_init(), and batt_step().

◆ s_below_ms

uint32_t s_below_ms[4]
static

Definition at line 23 of file battery.c.

Referenced by batt_init(), batt_step(), and lost_battery().

◆ s_recover_ms

uint32_t s_recover_ms
static

Definition at line 23 of file battery.c.

Referenced by batt_init(), batt_step(), and lost_battery().

◆ s_have_anchor

uint8_t s_have_anchor
static

Definition at line 24 of file battery.c.

Referenced by batt_init(), batt_step(), and lost_battery().

◆ s_have_f

uint8_t s_have_f
static

Definition at line 24 of file battery.c.

Referenced by batt_init(), and batt_step().

◆ s_soc_anchor

float s_soc_anchor
static

Definition at line 25 of file battery.c.

Referenced by batt_init(), and batt_step().

◆ s_used_anchor

float s_used_anchor
static

Definition at line 25 of file battery.c.

Referenced by batt_init(), and batt_step().

◆ s_i_slow

float s_i_slow
static

Definition at line 25 of file battery.c.

Referenced by batt_init(), and batt_step().

◆ s_wv

float s_wv
static

Definition at line 27 of file battery.c.

Referenced by batt_init(), and r_estimate().

◆ s_wi

float s_wi
static

Definition at line 27 of file battery.c.

Referenced by batt_init(), and r_estimate().

◆ s_wn

uint32_t s_wn
static

Definition at line 28 of file battery.c.

Referenced by batt_init(), and r_estimate().

◆ s_wms

uint32_t s_wms
static

Definition at line 28 of file battery.c.

Referenced by batt_init(), and r_estimate().

◆ s_pv

float s_pv
static

Definition at line 29 of file battery.c.

Referenced by r_estimate().

◆ s_pi

float s_pi
static

Definition at line 29 of file battery.c.

Referenced by r_estimate().

◆ s_have_pw

uint8_t s_have_pw
static

Definition at line 30 of file battery.c.

Referenced by batt_init(), lost_battery(), and r_estimate().

◆ s_seq

volatile uint32_t s_seq
static

Definition at line 32 of file battery.c.

Referenced by batt_init(), batt_read(), and publish().

◆ s_pub

batt_state_t s_pub
static

Definition at line 33 of file battery.c.

Referenced by batt_init(), batt_read(), and publish().

◆ k_ocv

const float k_ocv[21]
static
Initial value:
= {
3.27f, 3.61f, 3.69f, 3.71f, 3.73f, 3.75f, 3.77f, 3.79f, 3.80f, 3.82f, 3.84f,
3.85f, 3.87f, 3.91f, 3.95f, 3.98f, 4.02f, 4.08f, 4.11f, 4.15f, 4.20f
}

Definition at line 36 of file battery.c.

Referenced by batt_soc_from_ocv().