5import QGroundControl.Controls
6import QGroundControl.FactControls
8import "PX4ConfigRows.js" as ConfigRows
9import "PX4FlightModeRows.js" as ModeRows
13 pageComponent: pageComponent
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)
21 readonly property var _vehicle: controller.vehicle
22 readonly property var _allRows: ModeRows.flightModeRows()
23 readonly property var _rows: ConfigRows.visibleRows(
26 "multiRotor": _vehicle ? _vehicle.multiRotor : false,
27 "fixedWing": _vehicle ? _vehicle.fixedWing : false,
28 "vtol": _vehicle ? _vehicle.vtol : false
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
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
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
49 return _method === "buttons" ? _lastButtonSlot : 0
51 property int _lastButtonSlot: 0
53 on_ButtonMaskChanged: _lastButtonSlot = 0
54 on_MethodChanged: _lastButtonSlot = 0
59 function onRcChannelValuesChanged() {
60 if (_method === "buttons") {
61 _lastButtonSlot = ModeRows.nextButtonSlot(_lastButtonSlot, _buttonChannels, _channelValue)
65 readonly property var _slotRanges: {
66 if (_method !== "switch" || _modeChannel <= 0) {
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) {
76 return ModeRows.slotPwmRanges(min, trim, max, _rawValue(prefix + "_REV") < 0)
78 readonly property var _conflicts: {
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") }
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) }))
92 for (const row of _rows) {
93 if (row.kind === "switch") {
94 assignments.push({ "name": row.title, "channel": _rawValue(row.channelParam), "range": _switchOnRange(row) })
97 return ModeRows.channelConflicts(assignments)
100 function _fact(name) {
101 return controller.getParameterFact(-1, name, false)
104 function _rawValue(name) {
105 const fact = _fact(name)
106 return fact ? fact.rawValue : undefined
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) {
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) {
122 return ModeRows.normalizedChannel(pwm, min, trim, max, _rawValue(prefix + "_REV") < 0)
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) {
131 const midThreshold = row.midThresholdParam ? _rawValue(row.midThresholdParam) : undefined
132 return ModeRows.switchOnRange(onThreshold, midThreshold)
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) {
141 const onThreshold = row.thresholdParam ? _rawValue(row.thresholdParam) : row.fixedThreshold
142 if (onThreshold === undefined) {
145 const midThreshold = row.midThresholdParam ? _rawValue(row.midThresholdParam) : undefined
146 return ModeRows.switchPosition(value, onThreshold, midThreshold)
149 function _rowActive(row) {
150 return ConfigRows.isRowActive(row, _rawValue)
153 function _rowShown(row) {
154 return !_showAssignedOnly || !ConfigRows.isOptionalRow(row) || _rowActive(row)
157 function _groupShown(group) {
158 return group.rows.some(row => _rowShown(row))
161 function _modeChoices(fact) {
162 return controller.modeChoices(fact.name, fact.enumStrings, fact.enumValues, controller.externalModeNames, fact.rawValue)
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) {
171 return controller.modeName(fact.name, fact.enumStrings, fact.enumValues, controller.externalModeNames, fact.rawValue)
174 function _setMethod(method) {
175 _chosenMethod = method
176 for (const write of ModeRows.selectionWrites(method, _modeChannel, _buttonMask)) {
177 _fact(write.param).rawValue = write.value
181 function _slotRangeText(slot) {
182 const range = _slotRanges[slot - 1]
186 if (range.low === null) {
187 return qsTr("up to %1 µs").arg(range.high)
189 if (range.high === null) {
190 return qsTr("%1 µs and up").arg(range.low)
192 return qsTr("%1 – %2 µs").arg(range.low).arg(range.high)
195 PX4FlightModesController {
198 // Reboot-required settings are marked on the page instead
199 suppressRebootMessage: true
210 width: Math.max(availableWidth, implicitWidth)
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 }
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 ?
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)) :
237 readonly property bool _rcUnused: _inputModeFact ? (_inputModeFact.rawValue === 1 || _inputModeFact.rawValue === 4) : false
244 objectName: "flightModes_conflict_" + modelData.channel
245 Layout.fillWidth: true
246 implicitHeight: conflictLabel.implicitHeight + _margins * 2
248 border.color: qgcPal.warningText
249 radius: ScreenTools.defaultBorderRadius * 2
251 required property var modelData
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(", "))
269 groupShown: flightModesPage._groupShown
271 rowDelegate: ConfigRow {
273 objectName: "flightModes_row_" + row.id
276 active: _rowActive(row)
277 activeColor: _livePosition === "on" || _livePosition === "mid" ? qgcPal.colorYellow : qgcPal.colorGreen
278 showSeparator: index > 0
279 visible: _rowShown(row)
281 required property var modelData
282 required property int index
284 readonly property var row: modelData
285 readonly property bool _isSwitch: row.kind === "switch"
286 readonly property string _livePosition: _isSwitch && active ? _switchPosition(row) : ""
289 text: qsTr("Cannot be undone until reboot")
290 textColor: qgcPal.warningText
291 visible: rowLayout.row.irreversible === true
295 text: rowLayout.row.description
300 model: rowLayout.row.cells.filter(cell => !cell.customUi)
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
311 required property var modelData
316 active: rowLayout.row.kind === "method"
319 sourceComponent: QGCCaptionCell {
320 caption: qsTr("Method")
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])
330 readonly property var _methods: _buttonMaskFact ? [ "switch", "buttons", "none" ] : [ "switch", "none" ]
336 active: rowLayout.row.kind === "method" && _method === "switch"
339 sourceComponent: QGCCaptionCell {
340 caption: qsTr("Mode channel")
343 objectName: "flightModes_modeChannel"
344 fact: _modeChannelFact
352 active: rowLayout.row.kind === "method" && _method === "buttons" && _buttonMaskFact !== null
355 sourceComponent: QGCCaptionCell {
356 caption: qsTr("Mode button channels")
360 columnSpacing: ScreenTools.defaultFontPixelWidth
361 rowSpacing: ScreenTools.defaultFontPixelHeight * 0.25
366 FactBitMaskCheckBoxSlider {
367 objectName: "flightModes_button_" + (index + 1)
368 text: qsTr("Ch %1").arg(index + 1)
369 fact: _buttonMaskFact
372 required property int index
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.")
383 objectName: "flightModes_ignoredButtons"
384 Layout.preferredWidth: ScreenTools.defaultFontPixelWidth * 40
385 color: qgcPal.warningText
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(", ")) :
398 active: rowLayout.row.kind === "slots" && _slotRanges.length > 0
401 sourceComponent: QGCCaptionCell {
402 caption: _modeChannelValue !== undefined ?
403 qsTr("Channel %1 live").arg(_modeChannel) :
404 qsTr("Channel %1, no RC input").arg(_modeChannel)
408 implicitWidth: ScreenTools.defaultFontPixelWidth * 72
409 implicitHeight: ScreenTools.defaultFontPixelHeight * 2.5
411 readonly property real _slotWidth: width / ModeRows.modeSlotCount
414 model: ModeRows.modeSlotCount
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
424 required property int index
426 readonly property bool _active: _activeSlot === index + 1
427 readonly property string _modeName: _slotModeName(index + 1)
430 anchors.left: parent.left
431 anchors.right: parent.right
432 anchors.margins: ScreenTools.defaultFontPixelWidth / 2
433 anchors.verticalCenter: parent.verticalCenter
437 horizontalAlignment: Text.AlignHCenter
440 color: _active ? qgcPal.window : qgcPal.textSecondary
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)
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
467 active: rowLayout.row.kind === "slots"
470 sourceComponent: QGCCaptionCell {
473 columnSpacing: ScreenTools.defaultFontPixelWidth * 1.5
474 rowSpacing: ScreenTools.defaultFontPixelHeight * 0.25
477 model: ModeRows.modeSlotCount
482 text: qsTr("Slot %1").arg(index + 1)
483 font.bold: _activeSlot === index + 1
484 color: _activeSlot === index + 1 ? qgcPal.colorYellow : qgcPal.textPrimary
486 required property int index
491 model: ModeRows.modeSlotCount
496 font.family: ScreenTools.fixedFontFamily
497 font.pointSize: ScreenTools.smallFontPointSize
498 color: qgcPal.textSecondary
500 if (_method === "switch") {
501 return _slotRangeText(index + 1)
503 if (_method === "buttons") {
504 return index < _buttonChannels.length ? qsTr("Ch %1 button").arg(_buttonChannels[index]) : qsTr("No button")
509 required property int index
514 model: ModeRows.modeSlotCount
516 ConfigChoiceComboBox {
517 objectName: "flightModes_slot_" + (index + 1)
520 Layout.fillWidth: true
521 fact: _fact("COM_FLTMODE" + (index + 1))
522 choices: fact ? _modeChoices(fact) : []
524 required property int index
529 model: ModeRows.modeSlotCount
532 objectName: "flightModes_slotActive_" + (index + 1)
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
540 required property int index
548 active: rowLayout._isSwitch && rowLayout.active
551 sourceComponent: QGCCaptionCell {
552 caption: qsTr("Live")
555 objectName: "flightModes_live_" + rowLayout.row.id
556 color: rowLayout._livePosition === "on" || rowLayout._livePosition === "mid" ? qgcPal.colorYellow : qgcPal.textPrimary
558 const pwm = controller.rcChannelValues[_rawValue(rowLayout.row.channelParam) - 1]
559 if (pwm === undefined || pwm < 0) {
560 return qsTr("No RC input")
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)