Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/AP_Periph/battery_bms.h
9688 views
1
#pragma once
2
3
#if AP_PERIPH_BATTERY_BMS_ENABLED
4
5
class BatteryBMS {
6
public:
7
friend class AP_Periph_FW;
8
9
// main update function
10
void update(void);
11
12
private:
13
14
// configure gpio pins. returns true once configured
15
bool configured();
16
17
// check and handle button press events
18
void handle_button_press();
19
20
// request display of battery SOC percentage using LEDs
21
void request_display_percentage();
22
23
// display battery SOC percentage using LEDs
24
void display_percentage();
25
26
// get battery SOC percentage (0-100). returns true on success
27
bool get_percentage(uint8_t &percentage);
28
29
// set LED pattern based on 8-bit bitmask
30
void set_led_pattern(uint8_t pattern);
31
32
// update the LEDs. called regularly from update()
33
void update_led_state(void);
34
35
// BMS state machine variables
36
enum class BmsState : uint8_t {
37
IDLE = 0,
38
POWERING_ON,
39
POWERED_ON,
40
POWERING_OFF,
41
POWERED_OFF
42
};
43
44
// request change in bms state. the only valid inputs are POWERED_ON and POWERING_OFF
45
// return true on success
46
bool request_bms_state(BmsState new_state);
47
48
// update bms state. transitions bms_state to req_bms_state
49
void update_bms_state();
50
51
// configuration variables
52
bool config_complete; // true once configuration has been completed
53
54
// button handling variables
55
struct {
56
bool startup_complete; // true once startup delay has completed
57
bool pressed_prev; // true if button was pressed during previous iteration
58
uint32_t pressed_start_ms; // system time that button was first detected as pressed
59
bool short_press_handled; // true once a short press has been detected and handled
60
bool long_press_handled; // true once a long pressed has been detected and handled
61
} button;
62
static const uint32_t BUTTON_SHORT_PRESS_THRESHOLD_MS = 10; // 10 ms for short press
63
static const uint32_t BUTTON_LONG_PRESS_THRESHOLD_MS = 1000; // 1 second for long press
64
static const uint32_t BUTTON_STARTUP_DELAY_MS = 2000; // ignore button presses for first 2 seconds after startup
65
66
// BMS state machine variables
67
BmsState bms_state, req_bms_state; // current and requested BMS states
68
uint32_t bms_last_update_ms; // system time of last state machine update
69
uint8_t bms_transition_counter; // transition counter also used for animation
70
71
// LED display variables
72
uint32_t led_last_update_ms; // system time of last LED update. used to rate limit LED updates to 20hz
73
uint32_t led_display_soc_start_ms; // system time that SOC display started. 0 if not displaying SOC
74
uint8_t led_charging_animation_step; // LED charging animation step
75
static const uint32_t LED_UPDATE_INTERVAL_MS = 50; // update LEDs at 20hz
76
static const uint32_t LED_DISPLAY_SOC_DURATION_MS = 1000; // Display SOC percentage for 1 second
77
static const uint8_t led_gpios[]; // GPIO pins used for BMS LEDs
78
};
79
80
#endif // AP_PERIPH_BATTERY_BMS_ENABLED
81
82
83