7import QGroundControl.Controls
8import QGroundControl.FactControls
10import "PX4ConfigRows.js" as ConfigRows
11import "PX4SensorsRows.js" as SensorsRows
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.
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
26 /// Emitted when a calibration starts, so the host page can show the calibration panel at the top
27 signal scrollToTopRequested
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)
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
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: ""
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
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
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)
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
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)
86 readonly property var _calibrations: ({
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.")
93 function _paramExists(name) {
94 return controller.parameterExists(-1, name)
97 function _fact(name) {
98 return controller.getParameterFact(-1, name, false)
101 function _rawValue(name) {
102 const fact = _fact(name)
103 return fact ? fact.rawValue : undefined
106 function _calibrationAllowed(row) {
107 const options = QGroundControl.corePlugin.options
108 switch (row.calibration) {
110 return options.showSensorCalibrationCompass && showSensorCalibrationCompass
112 return options.showSensorCalibrationGyro && showSensorCalibrationGyro
114 return options.showSensorCalibrationAccel && showSensorCalibrationAccel
116 return options.showSensorCalibrationLevel && showSensorCalibrationLevel
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
126 function _groupShown(group) {
127 return group.rows.some(row => SensorsRows.rowShown(row, _sensorsHaveFixedOrientation))
130 function _startCalibration(type) {
131 _largeVehicleResult = ""
132 if (type === "accel" || type === "compass" || type === "compassLargeVehicle") {
133 _showPanelReady(type === "accel" ? "accel" : "compass", type === "compassLargeVehicle")
136 _calibrationDialogType = type
137 preCalibrationDialogFactory.open({ title: _calibrations[type].title })
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") : ""
145 _panelStarting = false
146 _panelStartMessage = ""
147 calibrationPanel.largeVehicleOpen = largeVehicleOpen
148 _showCalibrationPanel = true
149 scrollToTopRequested()
152 function _startPanelCalibration() {
153 _largeVehicleResult = ""
154 _panelStarting = true
155 _panelStartMessage = ""
156 panelStartTimer.restart()
157 switch (_calibrationKind) {
159 controller.calibrateAccel()
162 controller.calibrateAirspeed()
165 controller.calibrateCompass()
170 // The vehicle starts the calibration within moments. It does not answer a request it refuses, such as when armed.
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.")
183 function _confirmReset() {
184 if (_calibrationBusy) {
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() })
193 QGCPalette { id: qgcPal; colorGroupEnabled: true }
195 SensorsComponentController {
198 onMagCalComplete: _root._rebootNeeded = true
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
205 onInlineCalibrationFinished: (success, message) => {
206 const cancelled = _root._inlineCancelled
207 _root._inlineCancelled = false
208 if (cancelled && !success) {
209 _root._inlineState = "idle"
210 _root._inlineMessage = ""
213 _root._inlineState = success ? "done" : "failed"
214 _root._inlineMessage = success ? "" : SensorsRows.inlineFailureText(message)
217 onWaitingForCancelChanged: {
218 if (controller.waitingForCancel) {
219 waitForCancelDialogFactory.open()
227 function onCalibrationActiveChanged() {
228 if (controller.calibrationActive) {
229 if (_root._inlineRunning) {
230 inlineRequestTimer.stop()
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
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()
252 _panelStarting = false
253 _showCalibrationPanel = false
255 _inlineState = "requested"
257 _inlineCancelled = false
258 inlineRequestTimer.restart()
259 switch (row.calibration) {
261 controller.calibrateLevel()
264 controller.calibrateGyro()
267 controller.calibrateBaro()
272 // The vehicle starts the calibration within moments. It does not answer a request it refuses, such as when armed.
274 id: inlineRequestTimer
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.")
285 function _calibrationKey(active) {
287 case SensorsComponentController.ActiveCalibrationCompass:
289 case SensorsComponentController.ActiveCalibrationAccel:
291 case SensorsComponentController.ActiveCalibrationAirspeed:
298 Component.onDestruction: globals.navigationBlockedReason = ""
300 QGCPopupDialogFactory {
301 id: waitForCancelDialogFactory
303 dialogComponent: waitForCancelDialogComponent
307 id: waitForCancelDialogComponent
309 QGCSimpleMessageDialog {
310 title: qsTr("Calibration Cancel")
311 text: qsTr("Waiting for Vehicle to response to Cancel. This may take a few seconds.")
317 function onWaitingForCancelChanged() {
318 if (!controller.waitingForCancel) {
326 QGCPopupDialogFactory {
327 id: preCalibrationDialogFactory
329 dialogComponent: preCalibrationDialogComponent
333 id: preCalibrationDialogComponent
337 buttons: Dialog.Cancel | Dialog.Ok
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()
347 spacing: ScreenTools.defaultFontPixelHeight
350 Layout.preferredWidth: ScreenTools.defaultFontPixelWidth * 50
351 wrapMode: Text.WordWrap
352 text: _root._calibrations[_root._calibrationDialogType].help
356 wrapMode: Text.WordWrap
357 text: qsTr("Click Ok to start calibration.")
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
375 anchors.margins: _margins
376 spacing: ScreenTools.defaultFontPixelWidth
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
387 Layout.fillWidth: true
388 text: _rowsNeedingCalibration.map(row => row.title).join(", ")
389 color: qgcPal.textSecondary
390 elide: Text.ElideRight
394 text: _largeVehicleResult
395 color: _largeVehicleResultSuccess ? qgcPal.successText : qgcPal.errorText
396 visible: _largeVehicleResult !== ""
400 text: qsTr("Reboot the vehicle before flight")
401 color: qgcPal.warningText
402 visible: _rebootNeeded
407 SensorsCalibrationPanel {
409 objectName: "sensors_calibrationPanel"
410 Layout.fillWidth: true
411 sensorsController: controller
412 kind: _calibrationKind
413 title: _calibrationTitle
415 starting: _panelStarting
416 startMessage: _panelStartMessage
418 vehicleKind: _vehicleKind
419 orientationFact: _sensorsHaveFixedOrientation ? null : _fact("SENS_BOARD_ROT")
420 largeVehicleMessage: _largeVehicleResult
421 largeVehicleSuccess: _largeVehicleResultSuccess
422 largeVehiclePending: controller.largeVehicleCalibrationPending
423 visible: _showCalibrationPanel
425 onStartRequested: _root._startPanelCalibration()
428 _root._dismissWhenFinished = true
429 controller.cancelCalibration()
433 _root._panelReady = false
434 _root._showCalibrationPanel = false
437 onDismissed: _root._showCalibrationPanel = false
440 if (_root._vehicle) {
441 _root._vehicle.rebootVehicle()
445 onLargeVehicleRequested: (heading) => {
446 _root._largeVehicleResult = ""
447 controller.calibrateCompassLargeVehicle(heading)
452 Layout.fillWidth: true
456 Layout.fillWidth: true
457 Layout.alignment: Qt.AlignTop
458 enabled: !(controller.calibrationActive && _showCalibrationPanel)
459 contentSpacing: ScreenTools.defaultFontPixelHeight * 0.5
463 title: qsTr("Calibration")
464 buttonObjectName: "sensors_tab_calibration"
467 model: _shownCalibrationRows
469 SensorsCalibrationRow {
471 showSeparator: index > 0
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
478 calibrateEnabled: !_root._calibrationBusy &&
479 SensorsRows.calibrationEnabled(row, _root._calibrationRows, _root._rawValue)
480 cancelEnabled: controller.calibrationActive && !controller.waitingForCancel
481 paramsEnabled: !_root._calibrationBusy
483 initialValues: _root._initialValues
485 required property var modelData
486 required property int index
488 onCalibrateClicked: {
490 _root._startInlineCalibration(row)
492 _root._startCalibration(row.calibration)
497 _root._inlineCancelled = true
498 controller.cancelCalibration()
501 onAlternateClicked: _root._startCalibration(row.alternate.type)
506 Layout.fillWidth: true
507 spacing: ScreenTools.defaultFontPixelHeight * 0.5
508 visible: _compassRows.length > 0
511 text: qsTr("Compasses")
519 objectName: "sensors_row_" + row.id
522 showSeparator: index > 0
523 enabled: !_root._calibrationBusy
525 required property var modelData
526 required property int index
528 readonly property var row: modelData
529 readonly property bool _external: SensorsRows.compassExternal(_root._rawValue(row.rotationParam))
531 // PX4 puts the rotation back to custom while a custom angle is not zero
533 target: _root._fact(compassRow.row.rotationParam)
535 function onRawValueChanged() {
536 // A calibration which sets the rotation is the firmware's own change
537 if (_root._calibrationBusy) {
540 SensorsRows.staleCustomAngles(compassRow.row, _root._rawValue).forEach(
541 param => _root._fact(param).rawValue = 0)
547 caption: qsTr("Device")
550 spacing: ScreenTools.defaultFontPixelWidth
553 text: controller.deviceDescription(_root._rawValue(compassRow.row.idParam))
554 font.family: ScreenTools.fixedFontFamily
558 text: compassRow._external ? qsTr("External") : qsTr("Internal")
564 model: compassRow.row.cells
569 initialValues: _root._initialValues
570 objectNamePrefix: "sensors"
571 visible: (!modelData.externalOnly || compassRow._external) && SensorsRows.cellShown(modelData, _root._rawValue)
572 enabled: !modelData.externalOnly || !_root._sensorsHaveFixedOrientation
574 required property var modelData
583 model: SensorsRows.tabGroups(_groups, false)
584 groupShown: _root._groupShown
585 objectNamePrefix: "sensors"
586 rowDelegate: paramRowComponent
591 title: qsTranslate("SetupPage", "Advanced")
592 buttonObjectName: "sensors_tab_advanced"
595 model: SensorsRows.tabGroups(_groups, true)
596 groupShown: _root._groupShown
597 objectNamePrefix: "sensors"
598 rowDelegate: paramRowComponent
602 objectName: "sensors_row_reset"
603 title: qsTr("Reset calibration")
607 text: qsTr("Clears the gyroscope, accelerometer and compass calibration and reboots the autopilot.")
610 cellsData: QGCCaptionCell {
612 objectName: "sensorsSetup_factoryReset"
613 text: qsTr("Factory Reset")
614 enabled: !_root._calibrationBusy
615 onClicked: _root._confirmReset()
623 id: paramRowComponent
627 objectName: "sensors_row_" + row.id
630 showSeparator: index > 0
631 visible: SensorsRows.rowShown(row, _root._sensorsHaveFixedOrientation)
632 enabled: !_root._calibrationBusy
634 required property var modelData
635 required property int index
637 readonly property var row: modelData
640 text: paramRow.row.description !== undefined ? paramRow.row.description : ""
643 cellsData: Repeater {
644 model: paramRow.row.cells
649 initialValues: _root._initialValues
650 objectNamePrefix: "sensors"
651 visible: SensorsRows.cellShown(modelData, _root._rawValue)
653 required property var modelData
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)