QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
PX4BatteryComponent.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Layouts
3
4import QGroundControl
5import QGroundControl.Controls
6import QGroundControl.FactControls
7
8import "PX4BatteryRows.js" as BatteryRows
9import "PX4ConfigRows.js" as ConfigRows
10
11SetupPage {
12 id: batteryPage
13 pageComponent: pageComponent
14
15 property bool _showInUseOnly: false
16 property bool _filterChosen: false
17 // Parameter values when the page first showed them, so a reboot is only flagged after a change
18 property var _initialValues: Object.create(null)
19
20 readonly property var _vehicle: controller.vehicle
21 readonly property bool _estimatorOverwrite: BatteryRows.overwriteInEstimator(_vehicle ? _vehicle.firmwareMajorVersion : -1,
22 _vehicle ? _vehicle.firmwareMinorVersion : -1)
23 readonly property bool _loading: controller.pendingParams.length > 0
24 readonly property var _allRows: BatteryRows.batteryRows()
25 readonly property var _rows: {
26 // Rebuilt only when the vehicle adds a parameter. Each rebuild recreates the row controls, and a read the
27 // firmware cannot answer changes nothing visible.
28 controller.addedParamCount
29 return BatteryRows.visibleRows(_allRows, name => controller.parameterReady(name))
30 }
31 readonly property var _groups: ConfigRows.groupRows(_rows)
32 readonly property var _reportedTelemetry: {
33 const reported = []
34 const batteries = _vehicle ? _vehicle.batteries : null
35 for (let i = 0; batteries && i < batteries.count; i++) {
36 reported.push(batteries.get(i))
37 }
38 return reported
39 }
40 readonly property var _otherTelemetry: {
41 const batteries = _rows.filter(row => row.instance !== undefined)
42 .map(row => ({ "instance": row.instance, "inputs": _rowInputs(row) }))
43 const unclaimed = BatteryRows.unclaimedTelemetryIds(_reportedTelemetry.map(battery => battery.id.rawValue), batteries)
44 return _reportedTelemetry.filter(battery => unclaimed.indexOf(battery.id.rawValue) >= 0)
45 }
46 readonly property var _filterableRows: _rows.filter(row => !row.shared)
47 readonly property int _inUseCount: _filterableRows.filter(row => _rowActive(row)).length
48
49 function _fact(name) {
50 return controller.getParameterFact(-1, name, false)
51 }
52
53 function _rawValue(name) {
54 const fact = _fact(name)
55 return fact ? fact.rawValue : undefined
56 }
57
58 function _rowActive(row) {
59 return BatteryRows.isRowActive(row, _rawValue)
60 }
61
62 function _rowShown(row) {
63 return !_showInUseOnly || row.shared === true || _rowActive(row)
64 }
65
66 function _groupShown(group) {
67 return group.rows.some(row => _rowShown(row))
68 }
69
70 function _hasShownRowAbove(row) {
71 const group = _groups.find(candidate => candidate.rows.some(other => other.id === row.id))
72 return !!group && ConfigRows.hasShownRowAbove(group.rows, row, _rowShown)
73 }
74
75 function _rowInputs(row) {
76 return row.sourceParam ? BatteryRows.batteryInputs(_rawValue(row.sourceParam), _rawValue("UAVCAN_SUB_BAT"),
77 _rawValue("UAVCAN_ENABLE"), _estimatorOverwrite) : []
78 }
79
80 function _sourceDescription(inputs) {
81 if (inputs.indexOf("canFiltered") >= 0) {
82 // Without a source, the page cannot tell whether a DroneCAN battery uses this slot, so it points nowhere
83 return inputs.indexOf("external") >= 0 ?
84 qsTr("DroneCAN data filtered through the battery estimate below. Its live values are listed under Other Reported Batteries once the vehicle reports it.") :
85 qsTr("DroneCAN data filtered through the battery estimate below.")
86 }
87 if (inputs.indexOf("external") >= 0) {
88 return qsTr("Reported by MAVLink, DroneCAN or a driver, which estimates the charge itself. Its live values are listed under Other Reported Batteries once the vehicle reports it.")
89 }
90 if (inputs.indexOf("escs") >= 0) {
91 return qsTr("Voltage and current from ESC telemetry.")
92 }
93 if (inputs.indexOf("powerModule") >= 0) {
94 return qsTr("Analog power module or I2C power monitor. The analog settings only apply to the board's ADC inputs.")
95 }
96 return qsTr("Not monitored.")
97 }
98
99 function _batteryTelemetry(telemetryId) {
100 if (telemetryId < 0) {
101 return null
102 }
103 for (const battery of _reportedTelemetry) {
104 if (battery.id.rawValue === telemetryId) {
105 return battery
106 }
107 }
108 return null
109 }
110
111 function _telemetryText(battery) {
112 return [battery.voltage, battery.percentRemaining, battery.current]
113 .map(fact => fact.valueString + " " + fact.units)
114 .join(" ")
115 }
116
117 // Reads pendingParams from the controller, not _loading, which may not have updated yet when pendingParams changes
118 function _chooseFilter() {
119 if (!_filterChosen && controller.pendingParams.length === 0) {
120 _filterChosen = true
121 _showInUseOnly = _inUseCount > 0
122 }
123 }
124
125 PX4BatteryController {
126 id: controller
127 // Reboot-required settings are marked on the page instead
128 suppressRebootMessage: true
129
130 // Opening on what is in use hides the spare battery slots and unused power monitor drivers
131 onPendingParamsChanged: _chooseFilter()
132 }
133
134 QGCPalette {
135 id: qgcPal
136 }
137
138 QGCPopupDialogFactory {
139 id: voltageDividerDialogFactory
140 dialogComponent: voltageDividerDialogComponent
141 }
142
143 Component {
144 id: voltageDividerDialogComponent
145
146 CalcVoltageDividerDialog { }
147 }
148
149 QGCPopupDialogFactory {
150 id: ampsPerVoltDialogFactory
151 dialogComponent: ampsPerVoltDialogComponent
152 }
153
154 Component {
155 id: ampsPerVoltDialogComponent
156
157 CalcAmpsPerVoltDialog { }
158 }
159
160 Component {
161 id: pageComponent
162
163 ColumnLayout {
164 width: Math.max(availableWidth, implicitWidth)
165 spacing: _margins
166
167 ConfigRowsPanel {
168 ConfigRowsFilterBar {
169 objectName: "battery_filter"
170 filterLabels: [ qsTr("All"), qsTr("In Use") ]
171 filterIndex: _showInUseOnly ? 1 : 0
172 summary: {
173 const total = _filterableRows.length
174 if (_showInUseOnly) {
175 return total === 1 ?
176 qsTr("Showing %1 of 1 battery or power monitor.").arg(_inUseCount) :
177 qsTr("Showing %1 of %2 batteries and power monitors.").arg(_inUseCount).arg(total)
178 }
179 return total === 1 ?
180 qsTr("Showing the only battery or power monitor.") :
181 qsTr("Showing all %1 batteries and power monitors.").arg(total)
182 }
183 onFilterActivated: (index) => {
184 _filterChosen = true
185 _showInUseOnly = index === 1
186 }
187
188 QGCLabel {
189 objectName: "battery_loading"
190 color: qgcPal.textSecondary
191 text: qsTr("Loading settings from the vehicle...")
192 visible: _loading
193 }
194 }
195
196 ConfigRowGroups {
197 model: _groups
198 groupShown: batteryPage._groupShown
199
200 rowDelegate: ConfigRow {
201 id: rowLayout
202 objectName: "battery_row_" + row.id
203 title: row.title
204 params: BatteryRows.usedParams(row, _inputs)
205 active: _rowActive(row)
206 showSeparator: _hasShownRowAbove(row)
207 visible: _rowShown(row)
208
209 required property var modelData
210 required property int index
211
212 readonly property var row: modelData
213 readonly property bool _isBattery: row.instance !== undefined
214 readonly property var _inputs: _rowInputs(row)
215 readonly property var _telemetry: _isBattery ? _batteryTelemetry(BatteryRows.telemetryId(row.instance, _inputs)) : null
216 readonly property string _warning: _isBattery ? BatteryRows.sourceWarning(row.instance, _rawValue(row.sourceParam)) : ""
217
218 QGCLabel {
219 objectName: "battery_telemetry_" + rowLayout.row.id
220 text: rowLayout._telemetry ? _telemetryText(rowLayout._telemetry) : ""
221 font.family: ScreenTools.fixedFontFamily
222 visible: rowLayout._telemetry !== null
223 }
224
225 ConfigNote {
226 text: rowLayout._isBattery ? _sourceDescription(rowLayout._inputs) : rowLayout.row.description
227 }
228
229 ConfigNote {
230 color: qgcPal.warningText
231 text: rowLayout._warning
232 }
233
234 cellsData: Repeater {
235 model: rowLayout.row.cells
236
237 ConfigParamCell {
238 id: paramCell
239 cell: modelData
240 factFor: batteryPage._fact
241 initialValues: batteryPage._initialValues
242 objectNamePrefix: "battery"
243 numberTrailing: modelData.calc !== undefined ? calcButtonComponent : null
244 visible: BatteryRows.cellUsed(modelData, rowLayout._inputs)
245
246 required property var modelData
247
248 Component {
249 id: calcButtonComponent
250
251 QGCButton {
252 objectName: "battery_calc_" + paramCell.modelData.param
253 text: qsTr("Calculate")
254 // Calculating scales the current value, so it needs a set value rather than the board default
255 enabled: rowLayout._telemetry !== null && paramCell.fact !== null && paramCell.fact.rawValue > 0
256 onClicked: {
257 const factory = paramCell.modelData.calc === "voltageDivider" ?
258 voltageDividerDialogFactory : ampsPerVoltDialogFactory
259 factory.open({ "batteryIndex": rowLayout.row.instance })
260 }
261 }
262 }
263 }
264 }
265 }
266 }
267
268 // Reported under an id that does not identify a battery row above
269 ColumnLayout {
270 Layout.fillWidth: true
271 spacing: _margins
272 visible: _otherTelemetry.length > 0
273
274 ConfigGroupHeader {
275 text: qsTr("Other Reported Batteries")
276 }
277
278 ConfigNote {
279 // A wrapped label reports its unwrapped width as its implicit width, which would widen the page
280 Layout.preferredWidth: 0
281 text: qsTr("Reported batteries the page cannot match to a battery above. A battery set to External can report under any ID, so it appears here.")
282 }
283
284 Repeater {
285 model: _otherTelemetry
286
287 ConfigRow {
288 id: otherRow
289 objectName: "battery_other_" + modelData.id.rawValue
290 title: qsTr("ID %1").arg(modelData.id.rawValue)
291 active: !isNaN(otherRow.modelData.voltage.rawValue)
292 showSeparator: index > 0
293
294 required property var modelData
295 required property int index
296
297 cellsData: Repeater {
298 model: [
299 { "caption": qsTr("Voltage"), "fact": otherRow.modelData.voltage },
300 { "caption": qsTr("Charge"), "fact": otherRow.modelData.percentRemaining },
301 { "caption": qsTr("Current"), "fact": otherRow.modelData.current }
302 ]
303
304 QGCCaptionCell {
305 required property var modelData
306
307 caption: modelData.caption
308
309 QGCLabel {
310 text: modelData.fact.valueString + " " + modelData.fact.units
311 font.family: ScreenTools.fixedFontFamily
312 }
313 }
314 }
315 }
316 }
317 }
318 }
319 }
320 }
321
322 Component.onCompleted: {
323 controller.requestParameters(BatteryRows.allParams(_allRows))
324 _chooseFilter()
325 }
326}