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_Scripting.cpp
Views: 1798
1
#include "AP_BattMonitor_config.h"
2
3
#if AP_BATTERY_SCRIPTING_ENABLED
4
5
#include "AP_BattMonitor_Scripting.h"
6
7
#define AP_BATT_MONITOR_SCRIPTING_TIMEOUT_US 5000000
8
9
bool AP_BattMonitor_Scripting::capacity_remaining_pct(uint8_t &percentage) const
10
{
11
if (internal_state.capacity_remaining_pct != UINT8_MAX) {
12
percentage = internal_state.capacity_remaining_pct;
13
return true;
14
}
15
// Fall back to default implementation
16
return AP_BattMonitor_Backend::capacity_remaining_pct(percentage);
17
}
18
19
bool AP_BattMonitor_Scripting::get_cycle_count(uint16_t &cycles) const
20
{
21
if (internal_state.cycle_count == UINT16_MAX) {
22
return false;
23
}
24
cycles = internal_state.cycle_count;
25
return true;
26
}
27
28
// Called by frontend to update the state. Called at 10Hz
29
void AP_BattMonitor_Scripting::read()
30
{
31
WITH_SEMAPHORE(sem);
32
33
// Check for timeout, to prevent a faulty script from appearing healthy
34
if (last_update_us == 0 || AP_HAL::micros() - last_update_us > AP_BATT_MONITOR_SCRIPTING_TIMEOUT_US) {
35
_state.healthy = false;
36
return;
37
}
38
39
if (_state.last_time_micros == last_update_us) {
40
// No new data
41
return;
42
}
43
44
/*
45
the script can fill in voltages up to 32 cells, for mavlink reporting
46
the extra cell voltages get distributed over the max of 14 for mavlink
47
*/
48
for (uint8_t i = 0; i < MIN(AP_BATT_MONITOR_CELLS_MAX,internal_state.cell_count); i++) {
49
_state.cell_voltages.cells[i] = internal_state.cell_voltages[i];
50
}
51
_state.voltage = internal_state.voltage;
52
if (!isnan(internal_state.current_amps)) {
53
_state.current_amps = internal_state.current_amps;
54
}
55
if (!isnan(internal_state.consumed_mah)) {
56
_state.consumed_mah = internal_state.consumed_mah;
57
}
58
// Overide integrated consumed energy with script value if it has been set
59
if (!isnan(internal_state.consumed_wh)) {
60
_state.consumed_wh = internal_state.consumed_wh;
61
}
62
if (!isnan(internal_state.temperature)) {
63
_state.temperature = internal_state.temperature;
64
}
65
66
_state.healthy = internal_state.healthy;
67
68
// Update the timestamp (has to be done after the consumed_mah calculation)
69
_state.last_time_micros = last_update_us;
70
}
71
72
bool AP_BattMonitor_Scripting::handle_scripting(const BattMonitorScript_State &battmon_state)
73
{
74
WITH_SEMAPHORE(sem);
75
internal_state = battmon_state;
76
const uint32_t now_us = AP_HAL::micros();
77
uint32_t dt_us = now_us - last_update_us;
78
if (last_update_us != 0 && !isnan(internal_state.current_amps) && isnan(internal_state.consumed_mah)) {
79
AP_BattMonitor_Backend::update_consumed(_state, dt_us);
80
}
81
last_update_us = now_us;
82
return true;
83
}
84
85
#endif // AP_BATTERY_SCRIPTING_ENABLED
86
87