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_Logging.cpp
Views: 1798
1
#include "AP_BattMonitor_config.h"
2
#include <AP_Logger/AP_Logger_config.h>
3
4
#if AP_BATTERY_ENABLED && HAL_LOGGING_ENABLED
5
6
#include "AP_BattMonitor_Backend.h"
7
#include <AP_Logger/AP_Logger.h>
8
9
extern const AP_HAL::HAL& hal;
10
11
// Write BAT data packet(s)
12
void AP_BattMonitor_Backend::Log_Write_BAT(const uint8_t instance, const uint64_t time_us) const
13
{
14
bool has_curr = has_current();
15
uint8_t percent = -1;
16
IGNORE_RETURN(capacity_remaining_pct(percent));
17
18
float temperature;
19
int16_t temperature_cd = 0;
20
if (get_temperature(temperature)) {
21
temperature_cd = temperature * 100.0;
22
}
23
24
uint8_t soh_pct = 0;
25
IGNORE_RETURN(get_state_of_health_pct(soh_pct));
26
27
const struct log_BAT pkt{
28
LOG_PACKET_HEADER_INIT(LOG_BAT_MSG),
29
time_us : time_us,
30
instance : instance,
31
voltage : _state.voltage,
32
voltage_resting : _state.voltage_resting_estimate,
33
current_amps : has_curr ? _state.current_amps : AP::logger().quiet_nanf(),
34
current_total : has_curr ? _state.consumed_mah : AP::logger().quiet_nanf(),
35
consumed_wh : has_curr ? _state.consumed_wh : AP::logger().quiet_nanf(),
36
temperature : temperature_cd,
37
resistance : _state.resistance,
38
rem_percent : percent,
39
health : _state.healthy,
40
state_of_health_pct : soh_pct
41
};
42
AP::logger().WriteBlock(&pkt, sizeof(pkt));
43
}
44
45
// Write BCL data packet if has_cell_voltages
46
void AP_BattMonitor_Backend::Log_Write_BCL(const uint8_t instance, const uint64_t time_us) const
47
{
48
if (!has_cell_voltages()) {
49
return;
50
}
51
52
struct log_BCL cell_pkt{
53
LOG_PACKET_HEADER_INIT(LOG_BCL_MSG),
54
time_us : time_us,
55
instance : instance,
56
voltage : _state.voltage
57
};
58
59
// we pack the entire BCL message - we must have at least that
60
// many supported cells or the loop below will over-read
61
static_assert(ARRAY_SIZE(_state.cell_voltages.cells) >= ARRAY_SIZE(cell_pkt.cell_voltages), "must have at least ARRAY_SIZE(log_BCL.cell_voltages) cells");
62
63
for (uint8_t i = 0; i < ARRAY_SIZE(cell_pkt.cell_voltages); i++) {
64
cell_pkt.cell_voltages[i] = _state.cell_voltages.cells[i] + 1; // add 1mv
65
}
66
AP::logger().WriteBlock(&cell_pkt, sizeof(cell_pkt));
67
68
#if AP_BATT_MONITOR_CELLS_MAX > 12
69
if (_state.cell_voltages.cells[12] != UINT16_MAX || _state.cell_voltages.cells[13] != UINT16_MAX)
70
{
71
// @LoggerMessage: BCL2
72
// @Description: Battery cell voltage information
73
// @Field: TimeUS: Time since system startup
74
// @Field: Instance: battery instance number
75
// @Field: V13: thirteenth cell voltage
76
// @Field: V14: fourteenth cell voltage
77
AP::logger().WriteStreaming(
78
"BCL2",
79
"TimeUS,Instance,V13,V14",
80
"s#vv",
81
"F-CC",
82
"QBHH",
83
time_us,
84
instance,
85
_state.cell_voltages.cells[ARRAY_SIZE(cell_pkt.cell_voltages)+0] + 1, // add 1mv
86
_state.cell_voltages.cells[ARRAY_SIZE(cell_pkt.cell_voltages)+1] + 1 // add 1mv
87
);
88
}
89
#endif
90
}
91
92
#endif // AP_BATTERY_ENABLED && HAL_LOGGING_ENABLED
93
94