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

Battery monitor: voltage, current, consumed charge, cell count, state of charge, flight time left and the warning level used by the battery failsafes (HANDOVER_FW.md §4 POWER, §6, §9 item 1, §10.6). More...

#include <stdint.h>
Include dependency graph for battery.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  batt_cfg_t
 
struct  batt_state_t
 published state More...
 
struct  batt_limits_t
 thresholds and settings (from the parameters) More...
 

Macros

#define BATT_CELL_DETECT_V   4.35f
 max cell voltage used to count cells

 
#define BATT_PRESENT_V   2.5f
 pack above this = battery connected

 
#define BATT_ABSENT_V   1.5f
 
#define BATT_LEVEL_MS   2000u
 below a threshold this long -> level

 
#define BATT_RECOVER_MS   2000u
 disarmed, above threshold + hyst

 
#define BATT_RECOVER_HYST   0.05f
 V/cell

 
#define BATT_RESERVE_SOC   0.20f
 charge kept for landing (remain)

 
#define BATT_R_CELL_DEFAULT   0.040f
 Ω per cell before any estimate

 

Functions

void batt_init (const batt_cfg_t *cfg)
 
void batt_set_scales (float scale_v, float scale_i)
 
void batt_get_cfg (batt_cfg_t *out)
 
void batt_step (float pin_v, float cur_pin_v, uint32_t dt_ms, uint8_t armed, const batt_limits_t *lim)
 one sample: pin voltages, time step, armed flag (flight-control context)
 
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
 
float batt_soc_from_ocv (float cell_v)
 LiPo open-circuit voltage → state of charge 0…1 (exposed for the tests)
 

Detailed Description

Battery monitor: voltage, current, consumed charge, cell count, state of charge, flight time left and the warning level used by the battery failsafes (HANDOVER_FW.md §4 POWER, §6, §9 item 1, §10.6).

Inputs are the voltages at the two ADC pins (the board binding converts the counts with VREFINT). The module runs in the flight-control context (ImuTask, 50 Hz) and publishes a snapshot that any task can read.

  • Voltage. vbat = pin × scaleV / 10 (scaleV 110 = the 100k/10k divider of the ACwing FC). BATTERY calibration corrects scaleV.
  • Current. I = pin / sens × scaleI, sens 0.1 V/A on the ACwing ESC 1S (TSC213 + 2 mΩ). BATTERY calibration corrects scaleI from the charge the charger put back.
  • Cells. Parameter battCells, or detected once per battery connection (disarmed): ceil(vbat / 4.35 V), 1…6.
  • Warning level. From the sagged (under load) cell voltage, low-pass filtered: a level is entered after the cell voltage stayed below its threshold for 2 s; it is left only when disarmed (or the battery is replaced), so a level reached in flight does not flicker when the throttle is reduced.
  • State of charge. Rest voltage V + I·R (R estimated on line from the load steps) through a LiPo OCV curve, anchored while the battery rests, then coulomb counting when the capacity (battCapacity) is known.

Host-testable: no HAL, no RTOS.

Definition in file battery.h.

Macro Definition Documentation

◆ BATT_CELL_DETECT_V

#define BATT_CELL_DETECT_V   4.35f

max cell voltage used to count cells

Definition at line 34 of file battery.h.

◆ BATT_PRESENT_V

#define BATT_PRESENT_V   2.5f

pack above this = battery connected

Definition at line 35 of file battery.h.

◆ BATT_ABSENT_V

#define BATT_ABSENT_V   1.5f

Definition at line 36 of file battery.h.

◆ BATT_LEVEL_MS

#define BATT_LEVEL_MS   2000u

below a threshold this long -> level

Definition at line 37 of file battery.h.

◆ BATT_RECOVER_MS

#define BATT_RECOVER_MS   2000u

disarmed, above threshold + hyst

Definition at line 38 of file battery.h.

◆ BATT_RECOVER_HYST

#define BATT_RECOVER_HYST   0.05f

V/cell

Definition at line 39 of file battery.h.

◆ BATT_RESERVE_SOC

#define BATT_RESERVE_SOC   0.20f

charge kept for landing (remain)

Definition at line 40 of file battery.h.

◆ BATT_R_CELL_DEFAULT

#define BATT_R_CELL_DEFAULT   0.040f

Ω per cell before any estimate

Definition at line 41 of file battery.h.

Function Documentation

◆ 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  scale_v,
float  scale_i 
)

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_step()

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

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:

◆ 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:

◆ batt_soc_from_ocv()

float batt_soc_from_ocv ( float  cell_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: