QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
SensorsSetup.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Controls
3import QtQuick.Dialogs
4import QtQuick.Layouts
5
6import QGroundControl
7import QGroundControl.Controls
8import QGroundControl.FactControls
9
10import "PX4ConfigRows.js" as ConfigRows
11import "PX4SensorsRows.js" as SensorsRows
12
13/// Page for sensor calibration, sensor orientation and sensor options. This control is used within the SensorsComponent
14/// control and can also be used standalone for custom UIs. When using standalone you can use the various show* bools to
15/// show/hide what you want.
16ColumnLayout {
17 id: _root
18 spacing: _margins
19
20 property bool showSensorCalibrationCompass: true ///< true: Show the compass calibration
21 property bool showSensorCalibrationGyro: true ///< true: Show the gyroscope calibration
22 property bool showSensorCalibrationAccel: true ///< true: Show the accelerometer calibration
23 property bool showSensorCalibrationLevel: true ///< true: Show the level horizon calibration
24 property bool showSensorCalibrationAirspeed: true ///< true: Show the airspeed calibration
25
26 /// Emitted when a calibration starts, so the host page can show the calibration panel at the top
27 signal scrollToTopRequested
28
29 // Parameter values when the page first showed them, so a reboot is only flagged after a change
30 property var _initialValues: Object.create(null)
31
32 // Calibrations which start from a dialog: the airspeed calibration
33 property string _calibrationDialogType: ""
34 property string _calibrationTitle: ""
35 property bool _showCalibrationPanel: false
36 property bool _dismissWhenFinished: false
37 property bool _rebootNeeded: false
38 property string _largeVehicleResult: ""
39 property bool _largeVehicleResultSuccess: false
40
41 // The accelerometer and compass calibrations start from the panel: "accel" or "compass" while it waits for Start
42 property string _calibrationKind: ""
43 property bool _panelReady: false
44 property bool _panelStarting: false
45 property string _panelStartMessage: ""
46
47 // A barometer or level horizon calibration runs in its row: "requested" until the vehicle starts it, then "done" or "failed"
48 property string _inlineId: ""
49 property string _inlineState: "idle"
50 property string _inlineMessage: ""
51 property bool _inlineCancelled: false
52
53 readonly property bool _inlineRunning: _inlineState === "requested" ||
54 controller.activeCalibration === SensorsComponentController.ActiveCalibrationBaro ||
55 controller.activeCalibration === SensorsComponentController.ActiveCalibrationLevel ||
56 controller.activeCalibration === SensorsComponentController.ActiveCalibrationGyro
57 // A calibration is running or has been asked for and the vehicle has not answered yet
58 readonly property bool _calibrationBusy: controller.calibrationActive || _inlineRunning || _panelStarting ||
59 controller.largeVehicleCalibrationPending
60
61 // Leaving the page destroys the controller, which loses the result of a calibration that has been asked for
62 on_CalibrationBusyChanged: globals.navigationBlockedReason = _calibrationBusy ? qsTr("Complete or cancel the current calibration first") : ""
63 readonly property var _gps: SensorsRows.gpsReadiness(
64 _vehicle ? _vehicle.gps.lock.rawValue : 0,
65 _vehicle ? _vehicle.gps.count.rawValue : 0,
66 _vehicle ? _vehicle.gps.verticalAccuracy.rawValue : NaN)
67
68 readonly property real _margins: ScreenTools.defaultFontPixelHeight * 0.5
69 readonly property bool _sensorsHaveFixedOrientation: QGroundControl.corePlugin.options.sensorsHaveFixedOrientation
70 readonly property var _vehicle: controller.vehicle
71 readonly property string _vehicleKind: _vehicle ? (_vehicle.multiRotor ? "multiRotor" : _vehicle.vtol ? "vtol" : (_vehicle.fixedWing || _vehicle.airship) ? "fixedWing" : _vehicle.rover ? "rover" : "") : ""
72 readonly property var _vehicleKinds: ({
73 "multiRotor": _vehicle ? _vehicle.multiRotor : false,
74 "fixedWing": _vehicle ? (_vehicle.fixedWing || _vehicle.airship) : false,
75 "vtol": _vehicle ? _vehicle.vtol : false
76 })
77 readonly property var _calibrationRows: SensorsRows.calibrationRows(_vehicleKinds, _paramExists)
78 readonly property var _shownCalibrationRows: _calibrationRows.filter(
79 row => SensorsRows.calibrationRowShown(row, _rawValue) && _calibrationAllowed(row))
80 readonly property var _rowsNeedingCalibration: SensorsRows.rowsNeedingCalibration(_shownCalibrationRows, _rawValue)
81 readonly property var _compassRows: SensorsRows.compassRows(_rawValue, _paramExists)
82 readonly property var _devices: SensorsRows.hardwareDevices(_rawValue, _paramExists)
83 readonly property var _rows: ConfigRows.visibleRows(SensorsRows.sensorRows(), _vehicleKinds, _paramExists)
84 readonly property var _groups: ConfigRows.groupRows(_rows)
85
86 readonly property var _calibrations: ({
87 "airspeed": {
88 "title": qsTr("Calibrate Airspeed"),
89 "help": qsTr("For Airspeed calibration you will need to keep your airspeed sensor out of any wind and then blow across the sensor. Do not touch the sensor or obstruct any holes during the calibration.")
90 }
91 })
92
93 function _paramExists(name) {
94 return controller.parameterExists(-1, name)
95 }
96
97 function _fact(name) {
98 return controller.getParameterFact(-1, name, false)
99 }
100
101 function _rawValue(name) {
102 const fact = _fact(name)
103 return fact ? fact.rawValue : undefined
104 }
105
106 function _calibrationAllowed(row) {
107 const options = QGroundControl.corePlugin.options
108 switch (row.calibration) {
109 case "compass":
110 return options.showSensorCalibrationCompass && showSensorCalibrationCompass
111 case "gyro":
112 return options.showSensorCalibrationGyro && showSensorCalibrationGyro
113 case "accel":
114 return options.showSensorCalibrationAccel && showSensorCalibrationAccel
115 case "level":
116 return options.showSensorCalibrationLevel && showSensorCalibrationLevel
117 case "airspeed":
118 // The component knows which parameters say there is an airspeed sensor on each firmware version
119 return vehicleComponent && vehicleComponent.airspeedCalSupported &&
120 options.showSensorCalibrationAirspeed && showSensorCalibrationAirspeed
121 default:
122 return true
123 }
124 }
125
126 function _groupShown(group) {
127 return group.rows.some(row => SensorsRows.rowShown(row, _sensorsHaveFixedOrientation))
128 }
129
130 function _startCalibration(type) {
131 _largeVehicleResult = ""
132 if (type === "accel" || type === "compass" || type === "compassLargeVehicle") {
133 _showPanelReady(type === "accel" ? "accel" : "compass", type === "compassLargeVehicle")
134 return
135 }
136 _calibrationDialogType = type
137 preCalibrationDialogFactory.open({ title: _calibrations[type].title })
138 }
139
140 // The panel shows what to do first, and the calibration starts from its Start button
141 function _showPanelReady(kind, largeVehicleOpen) {
142 _calibrationKind = kind
143 _calibrationTitle = kind === "airspeed" ? qsTr("Airspeed") : ""
144 _panelReady = true
145 _panelStarting = false
146 _panelStartMessage = ""
147 calibrationPanel.largeVehicleOpen = largeVehicleOpen
148 _showCalibrationPanel = true
149 scrollToTopRequested()
150 }
151
152 function _startPanelCalibration() {
153 _largeVehicleResult = ""
154 _panelStarting = true
155 _panelStartMessage = ""
156 panelStartTimer.restart()
157 switch (_calibrationKind) {
158 case "accel":
159 controller.calibrateAccel()
160 break
161 case "airspeed":
162 controller.calibrateAirspeed()
163 break
164 default:
165 controller.calibrateCompass()
166 break
167 }
168 }
169
170 // The vehicle starts the calibration within moments. It does not answer a request it refuses, such as when armed.
171 Timer {
172 id: panelStartTimer
173 interval: 4000
174
175 onTriggered: {
176 if (_root._panelStarting) {
177 _root._panelStarting = false
178 _root._panelStartMessage = qsTr("The vehicle did not start the calibration. It may be busy or armed.")
179 }
180 }
181 }
182
183 function _confirmReset() {
184 if (_calibrationBusy) {
185 return
186 }
187 QGroundControl.showMessageDialog(_root, qsTr("Reset sensor calibration"),
188 qsTr("This clears the gyroscope, accelerometer and compass calibration and reboots the autopilot. Are you sure?"),
189 Dialog.Yes | Dialog.Cancel,
190 function () { controller.resetFactoryParameters() })
191 }
192
193 QGCPalette { id: qgcPal; colorGroupEnabled: true }
194
195 SensorsComponentController {
196 id: controller
197
198 onMagCalComplete: _root._rebootNeeded = true
199
200 onLargeVehicleCalibrationFinished: (success, message) => {
201 _root._largeVehicleResultSuccess = success
202 _root._largeVehicleResult = success ? qsTr("Offsets set. They are saved when the vehicle disarms or reboots.") : message
203 }
204
205 onInlineCalibrationFinished: (success, message) => {
206 const cancelled = _root._inlineCancelled
207 _root._inlineCancelled = false
208 if (cancelled && !success) {
209 _root._inlineState = "idle"
210 _root._inlineMessage = ""
211 return
212 }
213 _root._inlineState = success ? "done" : "failed"
214 _root._inlineMessage = success ? "" : SensorsRows.inlineFailureText(message)
215 }
216
217 onWaitingForCancelChanged: {
218 if (controller.waitingForCancel) {
219 waitForCancelDialogFactory.open()
220 }
221 }
222 }
223
224 Connections {
225 target: controller
226
227 function onCalibrationActiveChanged() {
228 if (controller.calibrationActive) {
229 if (_root._inlineRunning) {
230 inlineRequestTimer.stop()
231 return
232 }
233 panelStartTimer.stop()
234 _root._panelReady = false
235 _root._panelStarting = false
236 _root._calibrationKind = _calibrationKey(controller.activeCalibration)
237 _root._calibrationTitle = _root._calibrationKind === "airspeed" ? qsTr("Airspeed") : ""
238 calibrationPanel.logOpen = _root._calibrationKind === "airspeed"
239 _root._showCalibrationPanel = true
240 _root.scrollToTopRequested()
241 } else if (_root._dismissWhenFinished) {
242 _root._dismissWhenFinished = false
243 _root._showCalibrationPanel = false
244 }
245 }
246 }
247
248 function _startInlineCalibration(row) {
249 // A panel left open from another calibration would stay up and disable this page while this one runs
250 panelStartTimer.stop()
251 _panelReady = false
252 _panelStarting = false
253 _showCalibrationPanel = false
254 _inlineId = row.id
255 _inlineState = "requested"
256 _inlineMessage = ""
257 _inlineCancelled = false
258 inlineRequestTimer.restart()
259 switch (row.calibration) {
260 case "level":
261 controller.calibrateLevel()
262 break
263 case "gyro":
264 controller.calibrateGyro()
265 break
266 default:
267 controller.calibrateBaro()
268 break
269 }
270 }
271
272 // The vehicle starts the calibration within moments. It does not answer a request it refuses, such as when armed.
273 Timer {
274 id: inlineRequestTimer
275 interval: 4000
276
277 onTriggered: {
278 if (_root._inlineState === "requested") {
279 _root._inlineState = "failed"
280 _root._inlineMessage = qsTr("The vehicle did not start the calibration. It may be busy or armed.")
281 }
282 }
283 }
284
285 function _calibrationKey(active) {
286 switch (active) {
287 case SensorsComponentController.ActiveCalibrationCompass:
288 return "compass"
289 case SensorsComponentController.ActiveCalibrationAccel:
290 return "accel"
291 case SensorsComponentController.ActiveCalibrationAirspeed:
292 return "airspeed"
293 default:
294 return ""
295 }
296 }
297
298 Component.onDestruction: globals.navigationBlockedReason = ""
299
300 QGCPopupDialogFactory {
301 id: waitForCancelDialogFactory
302
303 dialogComponent: waitForCancelDialogComponent
304 }
305
306 Component {
307 id: waitForCancelDialogComponent
308
309 QGCSimpleMessageDialog {
310 title: qsTr("Calibration Cancel")
311 text: qsTr("Waiting for Vehicle to response to Cancel. This may take a few seconds.")
312 buttons: 0
313
314 Connections {
315 target: controller
316
317 function onWaitingForCancelChanged() {
318 if (!controller.waitingForCancel) {
319 close()
320 }
321 }
322 }
323 }
324 }
325
326 QGCPopupDialogFactory {
327 id: preCalibrationDialogFactory
328
329 dialogComponent: preCalibrationDialogComponent
330 }
331
332 Component {
333 id: preCalibrationDialogComponent
334
335 QGCPopupDialog {
336 id: dialog
337 buttons: Dialog.Cancel | Dialog.Ok
338
339 onAccepted: {
340 // The panel is shown first, so a request the vehicle does not answer is explained there and can be
341 // retried. It also holds the pending state, so no other start can be made until the vehicle answers.
342 _root._showPanelReady("airspeed", false)
343 _root._startPanelCalibration()
344 }
345
346 ColumnLayout {
347 spacing: ScreenTools.defaultFontPixelHeight
348
349 QGCLabel {
350 Layout.preferredWidth: ScreenTools.defaultFontPixelWidth * 50
351 wrapMode: Text.WordWrap
352 text: _root._calibrations[_root._calibrationDialogType].help
353 }
354
355 QGCLabel {
356 wrapMode: Text.WordWrap
357 text: qsTr("Click Ok to start calibration.")
358 }
359 }
360 }
361 }
362
363 Rectangle {
364 objectName: "sensors_statusBanner"
365 Layout.fillWidth: true
366 implicitHeight: bannerLayout.implicitHeight + _margins * 2
367 color: qgcPal.windowPanel
368 border.color: _rowsNeedingCalibration.length > 0 ? qgcPal.warningText : qgcPal.borderSubtle
369 radius: ScreenTools.defaultBorderRadius * 2
370 visible: !_showCalibrationPanel
371
372 RowLayout {
373 id: bannerLayout
374 anchors.fill: parent
375 anchors.margins: _margins
376 spacing: ScreenTools.defaultFontPixelWidth
377
378 QGCLabel {
379 text: _rowsNeedingCalibration.length > 0 ?
380 qsTr("%n sensor(s) need calibration before flight", "", _rowsNeedingCalibration.length) :
381 qsTr("Sensors are ready")
382 color: _rowsNeedingCalibration.length > 0 ? qgcPal.warningText : qgcPal.textPrimary
383 font.bold: true
384 }
385
386 QGCLabel {
387 Layout.fillWidth: true
388 text: _rowsNeedingCalibration.map(row => row.title).join(", ")
389 color: qgcPal.textSecondary
390 elide: Text.ElideRight
391 }
392
393 QGCLabel {
394 text: _largeVehicleResult
395 color: _largeVehicleResultSuccess ? qgcPal.successText : qgcPal.errorText
396 visible: _largeVehicleResult !== ""
397 }
398
399 QGCLabel {
400 text: qsTr("Reboot the vehicle before flight")
401 color: qgcPal.warningText
402 visible: _rebootNeeded
403 }
404 }
405 }
406
407 SensorsCalibrationPanel {
408 id: calibrationPanel
409 objectName: "sensors_calibrationPanel"
410 Layout.fillWidth: true
411 sensorsController: controller
412 kind: _calibrationKind
413 title: _calibrationTitle
414 ready: _panelReady
415 starting: _panelStarting
416 startMessage: _panelStartMessage
417 gps: _gps
418 vehicleKind: _vehicleKind
419 orientationFact: _sensorsHaveFixedOrientation ? null : _fact("SENS_BOARD_ROT")
420 largeVehicleMessage: _largeVehicleResult
421 largeVehicleSuccess: _largeVehicleResultSuccess
422 largeVehiclePending: controller.largeVehicleCalibrationPending
423 visible: _showCalibrationPanel
424
425 onStartRequested: _root._startPanelCalibration()
426
427 onCancelRequested: {
428 _root._dismissWhenFinished = true
429 controller.cancelCalibration()
430 }
431
432 onCloseRequested: {
433 _root._panelReady = false
434 _root._showCalibrationPanel = false
435 }
436
437 onDismissed: _root._showCalibrationPanel = false
438
439 onRebootRequested: {
440 if (_root._vehicle) {
441 _root._vehicle.rebootVehicle()
442 }
443 }
444
445 onLargeVehicleRequested: (heading) => {
446 _root._largeVehicleResult = ""
447 controller.calibrateCompassLargeVehicle(heading)
448 }
449 }
450
451 RowLayout {
452 Layout.fillWidth: true
453 spacing: _margins
454
455 QGCGroupBox {
456 Layout.fillWidth: true
457 Layout.alignment: Qt.AlignTop
458 enabled: !(controller.calibrationActive && _showCalibrationPanel)
459 contentSpacing: ScreenTools.defaultFontPixelHeight * 0.5
460 showDividers: false
461
462 QGCGroupBoxTab {
463 title: qsTr("Calibration")
464 buttonObjectName: "sensors_tab_calibration"
465
466 Repeater {
467 model: _shownCalibrationRows
468
469 SensorsCalibrationRow {
470 id: calibrationRow
471 showSeparator: index > 0
472 row: modelData
473 calibrationState: SensorsRows.calibrationState(row, _root._rawValue)
474 inlineState: row.id === _root._inlineId ? (_root._inlineRunning ? "running" : _root._inlineState) : "idle"
475 inlineMessage: _root._inlineMessage
476 progress: controller.calibrationProgress
477 gps: _root._gps
478 calibrateEnabled: !_root._calibrationBusy &&
479 SensorsRows.calibrationEnabled(row, _root._calibrationRows, _root._rawValue)
480 cancelEnabled: controller.calibrationActive && !controller.waitingForCancel
481 paramsEnabled: !_root._calibrationBusy
482 factFor: _root._fact
483 initialValues: _root._initialValues
484
485 required property var modelData
486 required property int index
487
488 onCalibrateClicked: {
489 if (row.inline) {
490 _root._startInlineCalibration(row)
491 } else {
492 _root._startCalibration(row.calibration)
493 }
494 }
495
496 onCancelClicked: {
497 _root._inlineCancelled = true
498 controller.cancelCalibration()
499 }
500
501 onAlternateClicked: _root._startCalibration(row.alternate.type)
502 }
503 }
504
505 ColumnLayout {
506 Layout.fillWidth: true
507 spacing: ScreenTools.defaultFontPixelHeight * 0.5
508 visible: _compassRows.length > 0
509
510 ConfigGroupHeader {
511 text: qsTr("Compasses")
512 }
513
514 Repeater {
515 model: _compassRows
516
517 ConfigRow {
518 id: compassRow
519 objectName: "sensors_row_" + row.id
520 title: row.title
521 params: row.params
522 showSeparator: index > 0
523 enabled: !_root._calibrationBusy
524
525 required property var modelData
526 required property int index
527
528 readonly property var row: modelData
529 readonly property bool _external: SensorsRows.compassExternal(_root._rawValue(row.rotationParam))
530
531 // PX4 puts the rotation back to custom while a custom angle is not zero
532 Connections {
533 target: _root._fact(compassRow.row.rotationParam)
534
535 function onRawValueChanged() {
536 // A calibration which sets the rotation is the firmware's own change
537 if (_root._calibrationBusy) {
538 return
539 }
540 SensorsRows.staleCustomAngles(compassRow.row, _root._rawValue).forEach(
541 param => _root._fact(param).rawValue = 0)
542 }
543 }
544
545 cellsData: [
546 QGCCaptionCell {
547 caption: qsTr("Device")
548
549 RowLayout {
550 spacing: ScreenTools.defaultFontPixelWidth
551
552 QGCLabel {
553 text: controller.deviceDescription(_root._rawValue(compassRow.row.idParam))
554 font.family: ScreenTools.fixedFontFamily
555 }
556
557 QGCChip {
558 text: compassRow._external ? qsTr("External") : qsTr("Internal")
559 }
560 }
561 },
562
563 Repeater {
564 model: compassRow.row.cells
565
566 ConfigParamCell {
567 cell: modelData
568 factFor: _root._fact
569 initialValues: _root._initialValues
570 objectNamePrefix: "sensors"
571 visible: (!modelData.externalOnly || compassRow._external) && SensorsRows.cellShown(modelData, _root._rawValue)
572 enabled: !modelData.externalOnly || !_root._sensorsHaveFixedOrientation
573
574 required property var modelData
575 }
576 }
577 ]
578 }
579 }
580 }
581
582 ConfigRowGroups {
583 model: SensorsRows.tabGroups(_groups, false)
584 groupShown: _root._groupShown
585 objectNamePrefix: "sensors"
586 rowDelegate: paramRowComponent
587 }
588 }
589
590 QGCGroupBoxTab {
591 title: qsTranslate("SetupPage", "Advanced")
592 buttonObjectName: "sensors_tab_advanced"
593
594 ConfigRowGroups {
595 model: SensorsRows.tabGroups(_groups, true)
596 groupShown: _root._groupShown
597 objectNamePrefix: "sensors"
598 rowDelegate: paramRowComponent
599 }
600
601 ConfigRow {
602 objectName: "sensors_row_reset"
603 title: qsTr("Reset calibration")
604 showSeparator: true
605
606 ConfigNote {
607 text: qsTr("Clears the gyroscope, accelerometer and compass calibration and reboots the autopilot.")
608 }
609
610 cellsData: QGCCaptionCell {
611 QGCButton {
612 objectName: "sensorsSetup_factoryReset"
613 text: qsTr("Factory Reset")
614 enabled: !_root._calibrationBusy
615 onClicked: _root._confirmReset()
616 }
617 }
618 }
619 }
620 }
621
622 Component {
623 id: paramRowComponent
624
625 ConfigRow {
626 id: paramRow
627 objectName: "sensors_row_" + row.id
628 title: row.title
629 params: row.params
630 showSeparator: index > 0
631 visible: SensorsRows.rowShown(row, _root._sensorsHaveFixedOrientation)
632 enabled: !_root._calibrationBusy
633
634 required property var modelData
635 required property int index
636
637 readonly property var row: modelData
638
639 ConfigNote {
640 text: paramRow.row.description !== undefined ? paramRow.row.description : ""
641 }
642
643 cellsData: Repeater {
644 model: paramRow.row.cells
645
646 ConfigParamCell {
647 cell: modelData
648 factFor: _root._fact
649 initialValues: _root._initialValues
650 objectNamePrefix: "sensors"
651 visible: SensorsRows.cellShown(modelData, _root._rawValue)
652
653 required property var modelData
654 }
655 }
656 }
657 }
658
659 SensorsLiveCard {
660 Layout.preferredWidth: ScreenTools.defaultFontPixelWidth * 36
661 Layout.alignment: Qt.AlignTop
662 visible: _root.width > ScreenTools.defaultFontPixelWidth * 110
663 vehicle: _root._vehicle
664 vehicleKind: _root._vehicleKind
665 devices: _root._devices
666 deviceName: function (param) {
667 const value = _root._rawValue(param)
668 return value === undefined ? "" : controller.deviceDescription(value)
669 }
670 }
671 }
672}