5/// Manages access to ArduPilot battery parameters for a given battery index.
6/// ArduPilot battery parameter prefixes: BATT_, BATT2_, ..., BATT9_, BATTA_, ..., BATTG_
8 required property var controller
9 required property int batteryIndex
11 readonly property string _prefix: prefixForIndex(batteryIndex)
13 /// Returns the parameter prefix for a 0-based battery index.
14 /// 0: "BATT_", 1: "BATT2_", ..., 8: "BATT9_", 9: "BATTA_", ..., 15: "BATTG_"
15 function prefixForIndex(index) {
20 return "BATT" + (index + 1) + "_"
22 return "BATT" + String.fromCharCode(65 + index - 9) + "_"
25 /// Returns a display label for a 0-based battery index.
26 /// 0: "1", 1: "2", ..., 8: "9", 9: "A", ..., 15: "G"
27 function labelForIndex(index) {
29 return String(index + 1)
31 return String.fromCharCode(65 + index - 9)
34 /// Returns the number of battery slots whose MONITOR parameter exists.
35 function getBatteryCount() {
36 for (let i = 0; i < 16; i++) {
37 if (!controller.parameterExists(-1, prefixForIndex(i) + "MONITOR")) {
44 /// Returns how many batteries have MONITOR enabled (rawValue != 0).
45 function getEnabledBatteryCount() {
47 let total = getBatteryCount()
48 for (let i = 0; i < total; i++) {
49 let mon = controller.getParameterFact(-1, prefixForIndex(i) + "MONITOR", false)
50 if (mon && mon.rawValue !== 0) count++
55 property Fact battMonitor: controller.getParameterFact(-1, _prefix + "MONITOR")
56 property Fact battCapacity: controller.getParameterFact(-1, _prefix + "CAPACITY", false)
57 property Fact battArmVolt: controller.getParameterFact(-1, _prefix + "ARM_VOLT", false)
58 property Fact battAmpPerVolt: controller.getParameterFact(-1, _prefix + "AMP_PERVLT", false)
59 property Fact battAmpOffset: controller.getParameterFact(-1, _prefix + "AMP_OFFSET", false)
60 property Fact battVoltMult: controller.getParameterFact(-1, _prefix + "VOLT_MULT", false)
62 // Failsafe parameters
63 property Fact fsLowAct: controller.getParameterFact(-1, _prefix + "FS_LOW_ACT", false)
64 property Fact fsCritAct: controller.getParameterFact(-1, _prefix + "FS_CRT_ACT", false)
65 property Fact lowMah: controller.getParameterFact(-1, _prefix + "LOW_MAH", false)
66 property Fact critMah: controller.getParameterFact(-1, _prefix + "CRT_MAH", false)
67 property Fact lowVolt: controller.getParameterFact(-1, _prefix + "LOW_VOLT", false)
68 property Fact critVolt: controller.getParameterFact(-1, _prefix + "CRT_VOLT", false)
70 property bool monitorEnabled: battMonitor.rawValue !== 0
71 property bool paramsAvailable: controller.parameterExists(-1, _prefix + "CAPACITY")
72 property bool showReboot: monitorEnabled && !paramsAvailable
74 // ArduPilot BattMonitor::Type enum values for analog sensor detection
75 readonly property int _monitorAnalogVoltageOnly: 3
76 readonly property int _monitorAnalogVoltageAndCurrent: 4
77 readonly property int _monitorAnalogVoltageSyntheticCurrent: 25
78 readonly property int _monitorAnalogCurrentOnly: 31
80 function _isMonitorTypeAnyOf(types) {
81 return monitorEnabled && types.includes(battMonitor.rawValue)
84 readonly property bool hasVoltageSensor: _isMonitorTypeAnyOf([_monitorAnalogVoltageOnly, _monitorAnalogVoltageAndCurrent, _monitorAnalogVoltageSyntheticCurrent])
85 readonly property bool hasCurrentSensor: _isMonitorTypeAnyOf([_monitorAnalogVoltageAndCurrent, _monitorAnalogCurrentOnly])