CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_BattMonitor/AP_BattMonitor.h
Views: 1798
1
#pragma once
2
3
#include "AP_BattMonitor_config.h"
4
5
#if AP_BATTERY_ENABLED
6
7
#include <AP_Common/AP_Common.h>
8
#include <AP_Param/AP_Param.h>
9
#include <AP_Math/AP_Math.h>
10
#include <AP_TemperatureSensor/AP_TemperatureSensor_config.h>
11
#include <GCS_MAVLink/GCS_MAVLink.h>
12
#include "AP_BattMonitor_Params.h"
13
14
// maximum number of battery monitors
15
#ifndef AP_BATT_MONITOR_MAX_INSTANCES
16
#define AP_BATT_MONITOR_MAX_INSTANCES 9
17
#endif
18
19
// first monitor is always the primary monitor
20
#define AP_BATT_PRIMARY_INSTANCE 0
21
22
#define AP_BATT_SERIAL_NUMBER_DEFAULT -1
23
24
#define AP_BATT_MONITOR_TIMEOUT 5000
25
26
#define AP_BATT_MONITOR_RES_EST_TC_1 0.5f
27
#define AP_BATT_MONITOR_RES_EST_TC_2 0.1f
28
29
#if BOARD_FLASH_SIZE > 1024
30
#define AP_BATT_MONITOR_CELLS_MAX 14
31
#else
32
#define AP_BATT_MONITOR_CELLS_MAX 12
33
#endif
34
35
// declare backend class
36
class AP_BattMonitor_Backend;
37
class AP_BattMonitor_Analog;
38
class AP_BattMonitor_SMBus;
39
class AP_BattMonitor_SMBus_Solo;
40
class AP_BattMonitor_SMBus_Generic;
41
class AP_BattMonitor_SMBus_Maxell;
42
class AP_BattMonitor_SMBus_Rotoye;
43
class AP_BattMonitor_DroneCAN;
44
class AP_BattMonitor_Generator;
45
class AP_BattMonitor_INA2XX;
46
class AP_BattMonitor_INA239;
47
class AP_BattMonitor_LTC2946;
48
class AP_BattMonitor_Torqeedo;
49
class AP_BattMonitor_FuelLevel_Analog;
50
class AP_BattMonitor_EFI;
51
class AP_BattMonitor_Scripting;
52
53
54
class AP_BattMonitor
55
{
56
friend class AP_BattMonitor_Backend;
57
friend class AP_BattMonitor_Analog;
58
friend class AP_BattMonitor_SMBus;
59
friend class AP_BattMonitor_SMBus_Solo;
60
friend class AP_BattMonitor_SMBus_Generic;
61
friend class AP_BattMonitor_SMBus_Maxell;
62
friend class AP_BattMonitor_SMBus_Rotoye;
63
friend class AP_BattMonitor_DroneCAN;
64
friend class AP_BattMonitor_Sum;
65
friend class AP_BattMonitor_FuelFlow;
66
friend class AP_BattMonitor_FuelLevel_PWM;
67
friend class AP_BattMonitor_Generator;
68
friend class AP_BattMonitor_EFI;
69
friend class AP_BattMonitor_INA2XX;
70
friend class AP_BattMonitor_INA239;
71
friend class AP_BattMonitor_LTC2946;
72
friend class AP_BattMonitor_AD7091R5;
73
friend class AP_BattMonitor_INA3221;
74
75
friend class AP_BattMonitor_Torqeedo;
76
friend class AP_BattMonitor_FuelLevel_Analog;
77
friend class AP_BattMonitor_Synthetic_Current;
78
friend class AP_BattMonitor_Scripting;
79
80
public:
81
82
// battery failsafes must be defined in levels of severity so that vehicles wont fall backwards
83
enum class Failsafe : uint8_t {
84
None = 0,
85
Unhealthy,
86
Low,
87
Critical
88
};
89
90
// Battery monitor driver types
91
enum class Type {
92
NONE = 0,
93
ANALOG_VOLTAGE_ONLY = 3,
94
ANALOG_VOLTAGE_AND_CURRENT = 4,
95
SOLO = 5,
96
BEBOP = 6,
97
SMBus_Generic = 7,
98
UAVCAN_BatteryInfo = 8,
99
BLHeliESC = 9,
100
Sum = 10,
101
FuelFlow = 11,
102
FuelLevel_PWM = 12,
103
SUI3 = 13,
104
SUI6 = 14,
105
NeoDesign = 15,
106
MAXELL = 16,
107
GENERATOR_ELEC = 17,
108
GENERATOR_FUEL = 18,
109
Rotoye = 19,
110
// 20 was MPPT_PacketDigital
111
INA2XX = 21,
112
LTC2946 = 22,
113
Torqeedo = 23,
114
FuelLevel_Analog = 24,
115
Analog_Volt_Synthetic_Current = 25,
116
INA239_SPI = 26,
117
EFI = 27,
118
AD7091R5 = 28,
119
Scripting = 29,
120
INA3221 = 30,
121
};
122
123
FUNCTOR_TYPEDEF(battery_failsafe_handler_fn_t, void, const char *, const int8_t);
124
125
AP_BattMonitor(uint32_t log_battery_bit, battery_failsafe_handler_fn_t battery_failsafe_handler_fn, const int8_t *failsafe_priorities);
126
127
/* Do not allow copies */
128
CLASS_NO_COPY(AP_BattMonitor);
129
130
static AP_BattMonitor *get_singleton() {
131
return _singleton;
132
}
133
134
// cell voltages in millivolts
135
struct cells {
136
uint16_t cells[AP_BATT_MONITOR_CELLS_MAX];
137
};
138
139
// The BattMonitor_State structure is filled in by the backend driver
140
struct BattMonitor_State {
141
cells cell_voltages; // battery cell voltages in millivolts, 10 cells matches the MAVLink spec
142
float voltage; // voltage in volts
143
float current_amps; // current in amperes
144
float consumed_mah; // total current draw in milliamp hours since start-up
145
float consumed_wh; // total energy consumed in Wh since start-up
146
uint32_t last_time_micros; // time when voltage and current was last read in microseconds
147
uint32_t low_voltage_start_ms; // time when voltage dropped below the minimum in milliseconds
148
uint32_t critical_voltage_start_ms; // critical voltage failsafe start timer in milliseconds
149
float temperature; // battery temperature in degrees Celsius
150
#if AP_TEMPERATURE_SENSOR_ENABLED
151
bool temperature_external_use;
152
float temperature_external; // battery temperature set by an external source in degrees Celsius
153
#endif
154
uint32_t temperature_time; // timestamp of the last received temperature message
155
float voltage_resting_estimate; // voltage with sag removed based on current and resistance estimate in Volt
156
float resistance; // resistance, in Ohms, calculated by comparing resting voltage vs in flight voltage
157
Failsafe failsafe; // stage failsafe the battery is in
158
bool healthy; // battery monitor is communicating correctly
159
uint32_t last_healthy_ms; // Time when monitor was last healthy
160
bool is_powering_off; // true when power button commands power off
161
bool powerOffNotified; // only send powering off notification once
162
uint32_t time_remaining; // remaining battery time
163
bool has_time_remaining; // time_remaining is only valid if this is true
164
uint8_t state_of_health_pct; // state of health (SOH) in percent
165
bool has_state_of_health_pct; // state_of_health_pct is only valid if this is true
166
uint8_t instance; // instance number of this backend
167
Type type; // allocated instance type
168
const struct AP_Param::GroupInfo *var_info;
169
};
170
171
static const struct AP_Param::GroupInfo *backend_var_info[AP_BATT_MONITOR_MAX_INSTANCES];
172
173
// Return the number of battery monitor instances
174
uint8_t num_instances(void) const { return _num_instances; }
175
176
// detect and initialise any available battery monitors
177
void init();
178
179
/// Read the battery voltage and current for all batteries. Should be called at 10hz
180
void read();
181
182
// healthy - returns true if monitor is functioning
183
bool healthy(uint8_t instance) const;
184
185
// return true if all configured battery monitors are healthy
186
bool healthy() const;
187
188
/// voltage - returns battery voltage in volts
189
float voltage(uint8_t instance) const;
190
float voltage() const { return voltage(AP_BATT_PRIMARY_INSTANCE); }
191
192
// voltage for a GCS, may be resistance compensated
193
float gcs_voltage(uint8_t instance) const;
194
float gcs_voltage(void) const { return gcs_voltage(AP_BATT_PRIMARY_INSTANCE); }
195
196
/// get voltage with sag removed (based on battery current draw and resistance)
197
/// this will always be greater than or equal to the raw voltage
198
float voltage_resting_estimate(uint8_t instance) const;
199
float voltage_resting_estimate() const { return voltage_resting_estimate(AP_BATT_PRIMARY_INSTANCE); }
200
201
/// current_amps - returns the instantaneous current draw in amperes
202
bool current_amps(float &current, const uint8_t instance = AP_BATT_PRIMARY_INSTANCE) const WARN_IF_UNUSED;
203
204
/// consumed_mah - returns total current drawn since start-up in milliampere.hours
205
bool consumed_mah(float &mah, const uint8_t instance = AP_BATT_PRIMARY_INSTANCE) const WARN_IF_UNUSED;
206
207
/// consumed_wh - returns total energy drawn since start-up in watt.hours
208
bool consumed_wh(float&wh, const uint8_t instance = AP_BATT_PRIMARY_INSTANCE) const WARN_IF_UNUSED;
209
210
/// capacity_remaining_pct - returns true if the percentage is valid and writes to percentage argument
211
virtual bool capacity_remaining_pct(uint8_t &percentage, uint8_t instance) const WARN_IF_UNUSED;
212
bool capacity_remaining_pct(uint8_t &percentage) const WARN_IF_UNUSED { return capacity_remaining_pct(percentage, AP_BATT_PRIMARY_INSTANCE); }
213
214
/// time_remaining - returns remaining battery time
215
bool time_remaining(uint32_t &seconds, const uint8_t instance = AP_BATT_PRIMARY_INSTANCE) const WARN_IF_UNUSED;
216
217
/// pack_capacity_mah - returns the capacity of the battery pack in mAh when the pack is full
218
int32_t pack_capacity_mah(uint8_t instance) const;
219
int32_t pack_capacity_mah() const { return pack_capacity_mah(AP_BATT_PRIMARY_INSTANCE); }
220
221
/// returns true if a battery failsafe has ever been triggered
222
bool has_failsafed(void) const { return _has_triggered_failsafe; };
223
224
/// returns the highest failsafe action that has been triggered
225
int8_t get_highest_failsafe_priority(void) const { return _highest_failsafe_priority; };
226
227
/// configured_type - returns battery monitor type as configured in parameters
228
enum Type configured_type(uint8_t instance) const {
229
return (Type)_params[instance]._type.get();
230
}
231
/// allocated_type - returns battery monitor type as allocated
232
enum Type allocated_type(uint8_t instance) const {
233
return state[instance].type;
234
}
235
236
/// get_serial_number - returns battery serial number
237
int32_t get_serial_number() const { return get_serial_number(AP_BATT_PRIMARY_INSTANCE); }
238
int32_t get_serial_number(uint8_t instance) const {
239
return _params[instance]._serial_number;
240
}
241
242
/// true when (voltage * current) > watt_max
243
bool overpower_detected() const;
244
bool overpower_detected(uint8_t instance) const;
245
246
// cell voltages in millivolts
247
bool has_cell_voltages() const { return has_cell_voltages(AP_BATT_PRIMARY_INSTANCE); }
248
bool has_cell_voltages(const uint8_t instance) const;
249
const cells &get_cell_voltages() const { return get_cell_voltages(AP_BATT_PRIMARY_INSTANCE); }
250
const cells &get_cell_voltages(const uint8_t instance) const;
251
252
// get once cell voltage (for scripting)
253
bool get_cell_voltage(uint8_t instance, uint8_t cell, float &voltage) const;
254
255
// temperature
256
bool get_temperature(float &temperature) const { return get_temperature(temperature, AP_BATT_PRIMARY_INSTANCE); }
257
bool get_temperature(float &temperature, const uint8_t instance) const;
258
#if AP_TEMPERATURE_SENSOR_ENABLED
259
bool set_temperature(const float temperature, const uint8_t instance);
260
bool set_temperature_by_serial_number(const float temperature, const int32_t serial_number);
261
#endif
262
263
// MPPT Control (Solar panels)
264
void MPPT_set_powered_state_to_all(const bool power_on);
265
void MPPT_set_powered_state(const uint8_t instance, const bool power_on);
266
267
bool option_is_set(uint8_t instance, AP_BattMonitor_Params::Options option) const;
268
269
// cycle count
270
bool get_cycle_count(uint8_t instance, uint16_t &cycles) const;
271
272
// get battery resistance estimate in ohms
273
float get_resistance() const { return get_resistance(AP_BATT_PRIMARY_INSTANCE); }
274
float get_resistance(uint8_t instance) const { return state[instance].resistance; }
275
276
// returns false if we fail arming checks, in which case the buffer will be populated with a failure message
277
bool arming_checks(size_t buflen, char *buffer) const;
278
279
// sends powering off mavlink broadcasts and sets notify flag
280
void checkPoweringOff(void);
281
282
// reset battery remaining percentage
283
bool reset_remaining_mask(uint16_t battery_mask, float percentage);
284
bool reset_remaining(uint8_t instance, float percentage) { return reset_remaining_mask(1U<<instance, percentage);}
285
286
// Returns mavlink charge state
287
MAV_BATTERY_CHARGE_STATE get_mavlink_charge_state(const uint8_t instance) const;
288
289
// Returns mavlink fault state
290
uint32_t get_mavlink_fault_bitmask(const uint8_t instance) const;
291
292
// return true if state of health (as a percentage) can be provided and fills in soh_pct argument
293
bool get_state_of_health_pct(uint8_t instance, uint8_t &soh_pct) const;
294
295
static const struct AP_Param::GroupInfo var_info[];
296
297
#if AP_BATTERY_SCRIPTING_ENABLED
298
bool handle_scripting(uint8_t idx, const struct BattMonitorScript_State &state);
299
#endif
300
301
protected:
302
303
/// parameters
304
AP_BattMonitor_Params _params[AP_BATT_MONITOR_MAX_INSTANCES];
305
306
private:
307
static AP_BattMonitor *_singleton;
308
309
BattMonitor_State state[AP_BATT_MONITOR_MAX_INSTANCES];
310
AP_BattMonitor_Backend *drivers[AP_BATT_MONITOR_MAX_INSTANCES];
311
uint32_t _log_battery_bit;
312
uint8_t _num_instances; /// number of monitors
313
314
void convert_dynamic_param_groups(uint8_t instance);
315
316
/// returns the failsafe state of the battery
317
Failsafe check_failsafe(const uint8_t instance);
318
void check_failsafes(void); // checks all batteries failsafes
319
320
battery_failsafe_handler_fn_t _battery_failsafe_handler_fn;
321
const int8_t *_failsafe_priorities; // array of failsafe priorities, sorted highest to lowest priority, -1 indicates no more entries
322
323
int8_t _highest_failsafe_priority; // highest selected failsafe action level (used to restrict what actions we move into)
324
bool _has_triggered_failsafe; // true after a battery failsafe has been triggered for the first time
325
326
};
327
328
namespace AP {
329
AP_BattMonitor &battery();
330
};
331
332
#endif // AP_BATTERY_ENABLED
333
334