QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
PX4FlightModes.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Layouts
3
4import QGroundControl
5import QGroundControl.Controls
6import QGroundControl.FactControls
7
8import "PX4ConfigRows.js" as ConfigRows
9import "PX4FlightModeRows.js" as ModeRows
10
11SetupPage {
12 id: flightModesPage
13 pageComponent: pageComponent
14
15 property bool _showAssignedOnly: false
16 // Method picked while neither a mode channel nor mode buttons are set, so its control shows before it has a value
17 property string _chosenMethod: ""
18 // Parameter values when the page first showed them, so a reboot is only flagged after a change
19 property var _initialValues: Object.create(null)
20
21 readonly property var _vehicle: controller.vehicle
22 readonly property var _allRows: ModeRows.flightModeRows()
23 readonly property var _rows: ConfigRows.visibleRows(
24 _allRows,
25 {
26 "multiRotor": _vehicle ? _vehicle.multiRotor : false,
27 "fixedWing": _vehicle ? _vehicle.fixedWing : false,
28 "vtol": _vehicle ? _vehicle.vtol : false
29 },
30 function (name) { return controller.parameterExists(-1, name) })
31 readonly property var _groups: ConfigRows.groupRows(_rows)
32 readonly property int _shownCount: _rows.filter(row => _rowShown(row)).length
33
34 readonly property var _inputModeFact: _fact("COM_RC_IN_MODE")
35 readonly property var _modeChannelFact: _fact("RC_MAP_FLTMODE")
36 readonly property var _buttonMaskFact: _fact("RC_MAP_FLTM_BTN")
37 readonly property int _modeChannel: _modeChannelFact ? _modeChannelFact.rawValue : 0
38 readonly property int _buttonMask: _buttonMaskFact ? _buttonMaskFact.rawValue : 0
39 readonly property string _method: {
40 const method = controller.selectionMethod(_modeChannel, _buttonMask)
41 return (method === "none" && _chosenMethod !== "") ? _chosenMethod : method
42 }
43 readonly property var _buttonChannels: controller.buttonChannels(_buttonMask)
44 readonly property var _modeChannelValue: _method === "switch" ? _channelValue(_modeChannel) : undefined
45 readonly property int _activeSlot: {
46 if (_method === "switch") {
47 return _modeChannelValue !== undefined ? ModeRows.modeSlot(_modeChannelValue) : 0
48 }
49 return _method === "buttons" ? _lastButtonSlot : 0
50 }
51 property int _lastButtonSlot: 0
52
53 on_ButtonMaskChanged: _lastButtonSlot = 0
54 on_MethodChanged: _lastButtonSlot = 0
55
56 Connections {
57 target: controller
58
59 function onRcChannelValuesChanged() {
60 if (_method === "buttons") {
61 _lastButtonSlot = ModeRows.nextButtonSlot(_lastButtonSlot, _buttonChannels, _channelValue)
62 }
63 }
64 }
65 readonly property var _slotRanges: {
66 if (_method !== "switch" || _modeChannel <= 0) {
67 return []
68 }
69 const prefix = "RC" + _modeChannel
70 const min = _rawValue(prefix + "_MIN")
71 const trim = _rawValue(prefix + "_TRIM")
72 const max = _rawValue(prefix + "_MAX")
73 if (min === undefined || trim === undefined || max === undefined) {
74 return []
75 }
76 return ModeRows.slotPwmRanges(min, trim, max, _rawValue(prefix + "_REV") < 0)
77 }
78 readonly property var _conflicts: {
79 const assignments = [
80 { "name": qsTr("Roll"), "channel": _rawValue("RC_MAP_ROLL") },
81 { "name": qsTr("Pitch"), "channel": _rawValue("RC_MAP_PITCH") },
82 { "name": qsTr("Yaw"), "channel": _rawValue("RC_MAP_YAW") },
83 { "name": qsTr("Throttle"), "channel": _rawValue("RC_MAP_THROTTLE") }
84 ]
85 if (_method === "switch") {
86 assignments.push({ "name": qsTr("Mode channel"), "channel": _modeChannel })
87 } else if (_method === "buttons") {
88 // PX4 reads a mode button as pressed above 75% of its channel
89 _buttonChannels.forEach((channel, i) => assignments.push({ "name": qsTr("Mode button %1").arg(i + 1), "channel": channel,
90 "range": ModeRows.switchOnRange(0.75) }))
91 }
92 for (const row of _rows) {
93 if (row.kind === "switch") {
94 assignments.push({ "name": row.title, "channel": _rawValue(row.channelParam), "range": _switchOnRange(row) })
95 }
96 }
97 return ModeRows.channelConflicts(assignments)
98 }
99
100 function _fact(name) {
101 return controller.getParameterFact(-1, name, false)
102 }
103
104 function _rawValue(name) {
105 const fact = _fact(name)
106 return fact ? fact.rawValue : undefined
107 }
108
109 /// Normalized live value of an RC channel (1-based), undefined without RC input or calibration
110 function _channelValue(channel) {
111 const pwm = controller.rcChannelValues[channel - 1]
112 if (pwm === undefined || pwm < 0) {
113 return undefined
114 }
115 const prefix = "RC" + channel
116 const min = _rawValue(prefix + "_MIN")
117 const trim = _rawValue(prefix + "_TRIM")
118 const max = _rawValue(prefix + "_MAX")
119 if (min === undefined || trim === undefined || max === undefined) {
120 return undefined
121 }
122 return ModeRows.normalizedChannel(pwm, min, trim, max, _rawValue(prefix + "_REV") < 0)
123 }
124
125 /// Where a switch row is on along its channel, undefined for one without a threshold such as flaps
126 function _switchOnRange(row) {
127 const onThreshold = row.thresholdParam ? _rawValue(row.thresholdParam) : row.fixedThreshold
128 if (onThreshold === undefined) {
129 return undefined
130 }
131 const midThreshold = row.midThresholdParam ? _rawValue(row.midThresholdParam) : undefined
132 return ModeRows.switchOnRange(onThreshold, midThreshold)
133 }
134
135 /// "on", "mid", "off" or "" without live input for a switch row
136 function _switchPosition(row) {
137 const value = _channelValue(_rawValue(row.channelParam))
138 if (value === undefined) {
139 return ""
140 }
141 const onThreshold = row.thresholdParam ? _rawValue(row.thresholdParam) : row.fixedThreshold
142 if (onThreshold === undefined) {
143 return ""
144 }
145 const midThreshold = row.midThresholdParam ? _rawValue(row.midThresholdParam) : undefined
146 return ModeRows.switchPosition(value, onThreshold, midThreshold)
147 }
148
149 function _rowActive(row) {
150 return ConfigRows.isRowActive(row, _rawValue)
151 }
152
153 function _rowShown(row) {
154 return !_showAssignedOnly || !ConfigRows.isOptionalRow(row) || _rowActive(row)
155 }
156
157 function _groupShown(group) {
158 return group.rows.some(row => _rowShown(row))
159 }
160
161 function _modeChoices(fact) {
162 return controller.modeChoices(fact.name, fact.enumStrings, fact.enumValues, controller.externalModeNames, fact.rawValue)
163 }
164
165 /// Name of the mode a slot selects, empty when the slot is unassigned
166 function _slotModeName(slot) {
167 const fact = _fact("COM_FLTMODE" + slot)
168 if (!fact || fact.rawValue < 0 || fact.enumIndex < 0) {
169 return ""
170 }
171 return controller.modeName(fact.name, fact.enumStrings, fact.enumValues, controller.externalModeNames, fact.rawValue)
172 }
173
174 function _setMethod(method) {
175 _chosenMethod = method
176 for (const write of ModeRows.selectionWrites(method, _modeChannel, _buttonMask)) {
177 _fact(write.param).rawValue = write.value
178 }
179 }
180
181 function _slotRangeText(slot) {
182 const range = _slotRanges[slot - 1]
183 if (!range) {
184 return ""
185 }
186 if (range.low === null) {
187 return qsTr("up to %1 µs").arg(range.high)
188 }
189 if (range.high === null) {
190 return qsTr("%1 µs and up").arg(range.low)
191 }
192 return qsTr("%1 – %2 µs").arg(range.low).arg(range.high)
193 }
194
195 PX4FlightModesController {
196 id: controller
197 monitorRc: true
198 // Reboot-required settings are marked on the page instead
199 suppressRebootMessage: true
200 }
201
202 QGCPalette {
203 id: qgcPal
204 }
205
206 Component {
207 id: pageComponent
208
209 ColumnLayout {
210 width: Math.max(availableWidth, implicitWidth)
211 spacing: _margins
212
213 ConfigRowsPanel {
214 ConfigRowsFilterBar {
215 objectName: "flightModes_filter"
216 filterLabels: [ qsTr("All"), qsTr("Assigned") ]
217 filterIndex: _showAssignedOnly ? 1 : 0
218 summary: _showAssignedOnly ?
219 qsTr("Showing %1 of %2 settings.").arg(_shownCount).arg(_rows.length) :
220 qsTr("Showing all %1 settings.").arg(_rows.length)
221 onFilterActivated: (index) => { _showAssignedOnly = index === 1 }
222 }
223
224 QGCLabel {
225 objectName: "flightModes_inputSource"
226 Layout.fillWidth: true
227 wrapMode: Text.WordWrap
228 visible: _inputModeFact !== null
229 // 1: MAVLink only, 4: manual control disabled
230 color: _rcUnused ? qgcPal.warningText : qgcPal.textSecondary
231 text: _inputModeFact ?
232 (_rcUnused ?
233 qsTr("Manual control source: %1. RC input is not used, so the mode channel and switches have no effect.").arg(_inputModeFact.enumStringValue) :
234 qsTr("Manual control source: %1. The mode channel, mode buttons and switches act on RC input.").arg(_inputModeFact.enumStringValue)) :
235 ""
236
237 readonly property bool _rcUnused: _inputModeFact ? (_inputModeFact.rawValue === 1 || _inputModeFact.rawValue === 4) : false
238 }
239
240 Repeater {
241 model: _conflicts
242
243 Rectangle {
244 objectName: "flightModes_conflict_" + modelData.channel
245 Layout.fillWidth: true
246 implicitHeight: conflictLabel.implicitHeight + _margins * 2
247 color: "transparent"
248 border.color: qgcPal.warningText
249 radius: ScreenTools.defaultBorderRadius * 2
250
251 required property var modelData
252
253 QGCLabel {
254 id: conflictLabel
255 anchors.left: parent.left
256 anchors.right: parent.right
257 anchors.margins: ScreenTools.defaultFontPixelWidth * 1.5
258 anchors.verticalCenter: parent.verticalCenter
259 wrapMode: Text.WordWrap
260 color: qgcPal.warningText
261 text: qsTr("Channel %1: %2 can be on at the same time. Use separate channels, or thresholds that do not overlap.")
262 .arg(modelData.channel).arg(modelData.users.join(", "))
263 }
264 }
265 }
266
267 ConfigRowGroups {
268 model: _groups
269 groupShown: flightModesPage._groupShown
270
271 rowDelegate: ConfigRow {
272 id: rowLayout
273 objectName: "flightModes_row_" + row.id
274 title: row.title
275 params: row.params
276 active: _rowActive(row)
277 activeColor: _livePosition === "on" || _livePosition === "mid" ? qgcPal.colorYellow : qgcPal.colorGreen
278 showSeparator: index > 0
279 visible: _rowShown(row)
280
281 required property var modelData
282 required property int index
283
284 readonly property var row: modelData
285 readonly property bool _isSwitch: row.kind === "switch"
286 readonly property string _livePosition: _isSwitch && active ? _switchPosition(row) : ""
287
288 QGCChip {
289 text: qsTr("Cannot be undone until reboot")
290 textColor: qgcPal.warningText
291 visible: rowLayout.row.irreversible === true
292 }
293
294 ConfigNote {
295 text: rowLayout.row.description
296 }
297
298 cellsData: [
299 Repeater {
300 model: rowLayout.row.cells.filter(cell => !cell.customUi)
301
302 ConfigParamCell {
303 cell: modelData
304 factFor: flightModesPage._fact
305 initialValues: flightModesPage._initialValues
306 objectNamePrefix: "flightModes"
307 enumChoices: modelData.param === "COM_FLTMODE_BOOT" && fact ? _modeChoices(fact) : null
308 // An unassigned switch only needs its channel
309 visible: !rowLayout._isSwitch || rowLayout.active || modelData.param === rowLayout.row.channelParam
310
311 required property var modelData
312 }
313 },
314
315 Loader {
316 active: rowLayout.row.kind === "method"
317 visible: active
318
319 sourceComponent: QGCCaptionCell {
320 caption: qsTr("Method")
321
322 QGCRadioButtonBar {
323 objectName: "flightModes_method"
324 model: _buttonMaskFact ?
325 [ qsTr("Switch channel"), qsTr("Buttons"), qsTr("None") ] :
326 [ qsTr("Switch channel"), qsTr("None") ]
327 currentIndex: _methods.indexOf(_method)
328 onActivated: (index) => _setMethod(_methods[index])
329
330 readonly property var _methods: _buttonMaskFact ? [ "switch", "buttons", "none" ] : [ "switch", "none" ]
331 }
332 }
333 },
334
335 Loader {
336 active: rowLayout.row.kind === "method" && _method === "switch"
337 visible: active
338
339 sourceComponent: QGCCaptionCell {
340 caption: qsTr("Mode channel")
341
342 FactComboBox {
343 objectName: "flightModes_modeChannel"
344 fact: _modeChannelFact
345 indexModel: false
346 sizeToContents: true
347 }
348 }
349 },
350
351 Loader {
352 active: rowLayout.row.kind === "method" && _method === "buttons" && _buttonMaskFact !== null
353 visible: active
354
355 sourceComponent: QGCCaptionCell {
356 caption: qsTr("Mode button channels")
357
358 GridLayout {
359 columns: 6
360 columnSpacing: ScreenTools.defaultFontPixelWidth
361 rowSpacing: ScreenTools.defaultFontPixelHeight * 0.25
362
363 Repeater {
364 model: 18
365
366 FactBitMaskCheckBoxSlider {
367 objectName: "flightModes_button_" + (index + 1)
368 text: qsTr("Ch %1").arg(index + 1)
369 fact: _buttonMaskFact
370 bitMask: 1 << index
371
372 required property int index
373 }
374 }
375 }
376
377 ConfigNote {
378 Layout.preferredWidth: ScreenTools.defaultFontPixelWidth * 40
379 text: qsTr("The lowest channel selects slot 1, the next slot 2, and so on. PX4 uses up to six.")
380 }
381
382 ConfigNote {
383 objectName: "flightModes_ignoredButtons"
384 Layout.preferredWidth: ScreenTools.defaultFontPixelWidth * 40
385 color: qgcPal.warningText
386 text: {
387 const ignored = controller.ignoredButtonChannels(_buttonMask)
388 return ignored.length > 0 ?
389 qsTr("PX4 ignores %1, as only the lowest six channels select a slot.")
390 .arg(ignored.map(channel => qsTr("Ch %1").arg(channel)).join(", ")) :
391 ""
392 }
393 }
394 }
395 },
396
397 Loader {
398 active: rowLayout.row.kind === "slots" && _slotRanges.length > 0
399 visible: active
400
401 sourceComponent: QGCCaptionCell {
402 caption: _modeChannelValue !== undefined ?
403 qsTr("Channel %1 live").arg(_modeChannel) :
404 qsTr("Channel %1, no RC input").arg(_modeChannel)
405
406 Item {
407 id: slotStrip
408 implicitWidth: ScreenTools.defaultFontPixelWidth * 72
409 implicitHeight: ScreenTools.defaultFontPixelHeight * 2.5
410
411 readonly property real _slotWidth: width / ModeRows.modeSlotCount
412
413 Repeater {
414 model: ModeRows.modeSlotCount
415
416 Rectangle {
417 objectName: "flightModes_strip_" + (index + 1)
418 x: slotStrip._slotWidth * index
419 width: slotStrip._slotWidth
420 height: slotStrip.height
421 color: _active ? qgcPal.colorYellow : qgcPal.windowPanel
422 border.color: qgcPal.borderSubtle
423
424 required property int index
425
426 readonly property bool _active: _activeSlot === index + 1
427 readonly property string _modeName: _slotModeName(index + 1)
428
429 Column {
430 anchors.left: parent.left
431 anchors.right: parent.right
432 anchors.margins: ScreenTools.defaultFontPixelWidth / 2
433 anchors.verticalCenter: parent.verticalCenter
434
435 QGCLabel {
436 width: parent.width
437 horizontalAlignment: Text.AlignHCenter
438 text: index + 1
439 font.bold: _active
440 color: _active ? qgcPal.window : qgcPal.textSecondary
441 }
442
443 QGCLabel {
444 width: parent.width
445 horizontalAlignment: Text.AlignHCenter
446 elide: Text.ElideRight
447 font.pointSize: ScreenTools.smallFontPointSize
448 text: _modeName !== "" ? _modeName : qsTr("Unassigned")
449 color: _active ? qgcPal.window : (_modeName !== "" ? qgcPal.textPrimary : qgcPal.textSecondary)
450 }
451 }
452 }
453 }
454
455 Rectangle {
456 x: slotStrip.width * ModeRows.slotStripPosition(_modeChannelValue) - width / 2
457 width: ScreenTools.defaultFontPixelWidth / 3
458 height: slotStrip.height
459 color: qgcPal.textPrimary
460 visible: _modeChannelValue !== undefined
461 }
462 }
463 }
464 },
465
466 Loader {
467 active: rowLayout.row.kind === "slots"
468 visible: active
469
470 sourceComponent: QGCCaptionCell {
471 GridLayout {
472 columns: 4
473 columnSpacing: ScreenTools.defaultFontPixelWidth * 1.5
474 rowSpacing: ScreenTools.defaultFontPixelHeight * 0.25
475
476 Repeater {
477 model: ModeRows.modeSlotCount
478
479 QGCLabel {
480 Layout.row: index
481 Layout.column: 0
482 text: qsTr("Slot %1").arg(index + 1)
483 font.bold: _activeSlot === index + 1
484 color: _activeSlot === index + 1 ? qgcPal.colorYellow : qgcPal.textPrimary
485
486 required property int index
487 }
488 }
489
490 Repeater {
491 model: ModeRows.modeSlotCount
492
493 QGCLabel {
494 Layout.row: index
495 Layout.column: 1
496 font.family: ScreenTools.fixedFontFamily
497 font.pointSize: ScreenTools.smallFontPointSize
498 color: qgcPal.textSecondary
499 text: {
500 if (_method === "switch") {
501 return _slotRangeText(index + 1)
502 }
503 if (_method === "buttons") {
504 return index < _buttonChannels.length ? qsTr("Ch %1 button").arg(_buttonChannels[index]) : qsTr("No button")
505 }
506 return ""
507 }
508
509 required property int index
510 }
511 }
512
513 Repeater {
514 model: ModeRows.modeSlotCount
515
516 ConfigChoiceComboBox {
517 objectName: "flightModes_slot_" + (index + 1)
518 Layout.row: index
519 Layout.column: 2
520 Layout.fillWidth: true
521 fact: _fact("COM_FLTMODE" + (index + 1))
522 choices: fact ? _modeChoices(fact) : []
523
524 required property int index
525 }
526 }
527
528 Repeater {
529 model: ModeRows.modeSlotCount
530
531 QGCChip {
532 objectName: "flightModes_slotActive_" + (index + 1)
533 Layout.row: index
534 Layout.column: 3
535 // A released button leaves its mode selected until something else changes the mode
536 text: _method === "buttons" ? qsTr("Last pressed") : qsTr("Active")
537 textColor: qgcPal.colorYellow
538 opacity: _activeSlot === index + 1 ? 1 : 0
539
540 required property int index
541 }
542 }
543 }
544 }
545 },
546
547 Loader {
548 active: rowLayout._isSwitch && rowLayout.active
549 visible: active
550
551 sourceComponent: QGCCaptionCell {
552 caption: qsTr("Live")
553
554 QGCLabel {
555 objectName: "flightModes_live_" + rowLayout.row.id
556 color: rowLayout._livePosition === "on" || rowLayout._livePosition === "mid" ? qgcPal.colorYellow : qgcPal.textPrimary
557 text: {
558 const pwm = controller.rcChannelValues[_rawValue(rowLayout.row.channelParam) - 1]
559 if (pwm === undefined || pwm < 0) {
560 return qsTr("No RC input")
561 }
562 const positions = { "on": qsTr("On"), "mid": qsTr("Mid"), "off": qsTr("Off") }
563 const position = positions[rowLayout._livePosition]
564 return position ? qsTr("%1 · %2 µs").arg(position).arg(pwm) : qsTr("%1 µs").arg(pwm)
565 }
566 }
567 }
568 }
569 ]
570 }
571 }
572 }
573 }
574 }
575}