6import QGroundControl.Controls
7import QGroundControl.FactControls
9import "PX4SensorsOrientation.js" as Orientation
10import "PX4SensorsRows.js" as SensorsRows
12/// The calibration which has the page's attention: the ready screen before it starts, then its progress, and its result.
13/// An accelerometer or compass calibration shows the sides of the vehicle, the others show the log. sensorsController is the
14/// page's SensorsComponentController, and the page handles the signals.
18 required property var sensorsController
20 /// "accel", "compass" or "airspeed"
21 property string kind: ""
22 property string title: ""
23 /// True until the calibration is started, so the panel shows what to do first
24 property bool ready: false
25 /// Start was clicked and the vehicle has not answered yet
26 property bool starting: false
27 /// Why the vehicle did not start the calibration, empty if it did
28 property string startMessage: ""
29 /// SensorsRows.gpsReadiness(), for the large vehicle calibration
30 property var gps: SensorsRows.gpsReadiness(0, 0, NaN)
31 /// The autopilot orientation fact, which has to be set before a calibration. Null when it is fixed.
32 property var orientationFact: null
33 property string vehicleKind: ""
34 property alias largeVehicleOpen: largeVehicleForm.open
35 property string largeVehicleMessage: ""
36 property bool largeVehicleSuccess: false
37 property bool largeVehiclePending: false
38 property bool logOpen: false
40 /// Start, or retry after a failure
42 signal cancelRequested
43 /// Cancel before the calibration started, or Close after a failure
45 /// Done after a success
47 signal rebootRequested
48 signal largeVehicleRequested(real heading)
50 readonly property bool _orientationKind: kind === "accel" || kind === "compass"
51 readonly property string _kindName: kind === "accel" ? qsTr("Accelerometer") : kind === "compass" ? qsTr("Compass") : title
52 readonly property string _phase: {
56 if (sensorsController.calibrationActive) {
57 return sensorsController.waitingForCancel ? "cancelling" : "running"
59 if (sensorsController.calibrationResult === SensorsComponentController.CalibrationResultSuccess) {
62 if (sensorsController.calibrationResult === SensorsComponentController.CalibrationResultFailed) {
68 // The sides which the calibration uses and where each is, read again when the controller reports a change
69 property int _revision: 0
70 property string _current: "" ///< The side the vehicle was last on
71 property string _lastDone: "" ///< The side which was finished last, until another side is detected
72 readonly property var _states: _readStates(_revision)
73 readonly property var _sideIds: Orientation.sideIds.filter(side => _states[side].used)
74 readonly property var _doneSides: _sideIds.filter(side => _states[side].state === 3)
75 readonly property string _detected: {
76 const active = _sideIds.filter(side => _states[side].state === 2)
77 return active.length > 0 ? active[0] : ""
79 readonly property string _suggested: {
80 const unused = Orientation.sideIds.filter(side => _sideIds.indexOf(side) < 0)
81 return Orientation.suggestNext(_current, _doneSides.concat(unused))
83 readonly property var _callout: _makeCallout()
85 function _readStates(revision) {
87 for (const side of Orientation.sideIds) {
89 used: _phase === "ready" || sensorsController[Orientation.visibleProperty(side)],
90 state: _phase === "ready" ? 0 : sensorsController[Orientation.stateProperty(side)]
96 function _sideState(side) {
97 return _states[side].state
100 function _failureText() {
101 const text = sensorsController.calibrationFailureText
102 const known = SensorsRows.inlineFailureText(text)
103 if (kind === "airspeed") {
104 // The log below has the firmware's reason when it is not one which is recognized
105 return known !== text ? known : qsTr("The calibration did not finish. Keep the sensor out of the wind, blow across it when asked, then try again.")
107 if (known !== text && text !== "") {
110 return kind === "compass" ? qsTr("The readings were not consistent. Move away from metal and magnets, then try again.") :
111 qsTr("The readings could not be used. Keep the vehicle still on each side, then try again.")
114 // kind is "plain", "acc", "ok" or "err"
115 function _makeCallout() {
119 return { kind: "plain", icon: "…", big: qsTr("Starting…"), sub: qsTr("Waiting for the vehicle.") }
121 if (startMessage !== "") {
122 return { kind: "err", icon: "!", big: qsTr("Could not start"), sub: startMessage }
124 if (!_orientationKind) {
125 return { kind: "plain", icon: "▶", big: qsTr("Ready to calibrate"), sub: "" }
127 return { kind: "plain", icon: "", big: qsTr("Ready to calibrate"),
128 sub: kind === "accel" ? qsTr("Put the vehicle on each of its sides, in any order, and hold it still on each.") :
129 qsTr("Move away from metal and magnets. On each side, turn the vehicle slowly until the side is marked done.") }
131 return { kind: "plain", icon: "…", big: qsTr("Waiting for the vehicle to cancel"), sub: qsTr("This can take a few seconds.") }
133 return { kind: "ok", icon: "✓", big: qsTr("%1 calibrated").arg(_kindName),
134 sub: kind === "compass" ? qsTr("Reboot the vehicle before flight so it takes effect.") : qsTr("The result is saved on the vehicle.") }
136 return { kind: "err", icon: "!", big: qsTr("Calibration failed"), sub: _failureText() }
139 if (!_orientationKind) {
140 return { kind: "acc", icon: "…", big: qsTr("Calibrating %1").arg(_kindName.toLowerCase()), sub: qsTr("Follow the instructions in the log.") }
142 if (_detected !== "") {
143 return kind === "compass" ? { kind: "acc", icon: "↻", big: qsTr("Rotate the vehicle slowly"), sub: qsTr("Keep turning until the side is marked done.") } :
144 { kind: "acc", icon: "✋", big: qsTr("Hold still"), sub: qsTr("Do not move it until the side is marked done.") }
146 if (sensorsController.sideAlreadyDone) {
147 return { kind: "acc", icon: "!", big: qsTr("That side is already done"), sub: qsTr("Use a side that is not done yet.") }
149 if (_lastDone !== "") {
150 return { kind: "ok", icon: "✓", big: qsTr("%1 done").arg(Orientation.sideName(_lastDone)),
151 sub: _suggested !== "" ? qsTr("Now place it on another side.") : "" }
153 return { kind: "acc", icon: "→", big: qsTr("Next: %1").arg(Orientation.sideName(_suggested)), sub: "" }
156 function _sideObjectName(side) {
158 level: "sensorsCal_downSide",
159 flipped: "sensorsCal_upsideDownSide",
160 nose: "sensorsCal_noseDownSide",
161 tail: "sensorsCal_tailDownSide",
162 left: "sensorsCal_leftSide",
163 right: "sensorsCal_rightSide"
167 function _refresh() {
168 const wasDone = _doneSides
170 if (_detected !== "") {
174 const newlyDone = _doneSides.filter(side => wasDone.indexOf(side) < 0)
175 if (newlyDone.length > 0) {
176 _lastDone = newlyDone[newlyDone.length - 1]
180 QGCPalette { id: qgcPal }
183 target: control.sensorsController
185 function onOrientationCalSidesStateChanged() { control._refresh() }
186 function onOrientationCalSidesVisibleChanged() { control._revision++ }
188 // A new calibration, or the ready screen after one ends, starts again from level
189 function onCalibrationActiveChanged() {
190 control._current = ""
191 control._lastDone = ""
195 function onCalibrationResultChanged() {
197 if (control.sensorsController.calibrationResult === SensorsComponentController.CalibrationResultFailed) {
198 control.logOpen = true
204 Layout.fillWidth: true
205 spacing: ScreenTools.defaultFontPixelWidth
208 implicitWidth: ScreenTools.defaultFontPixelHeight * 0.5
209 implicitHeight: implicitWidth
210 radius: implicitWidth / 2
211 color: control._phase === "done" ? qgcPal.colorGreen :
212 control._phase === "failed" ? qgcPal.colorRed :
213 (control._phase === "running" || control._phase === "cancelling") ? qgcPal.accent : "transparent"
214 border.color: color === "transparent" ? qgcPal.textSecondary : color
218 objectName: "sensorsCal_title"
219 text: control._kindName
221 font.capitalization: Font.AllUppercase
222 font.letterSpacing: ScreenTools.defaultFontPixelWidth * 0.1
226 objectName: "sensorsCal_counter"
227 textColor: control._phase === "done" ? qgcPal.successText : control._phase === "failed" ? qgcPal.errorText :
228 (control._phase === "running" || control._phase === "cancelling") ? qgcPal.accent : qgcPal.textSecondary
229 visible: control._orientationKind
231 switch (control._phase) {
233 return qsTr("Calibrated")
235 return qsTr("Failed")
237 return qsTr("%1 sides").arg(control._sideIds.length)
239 return qsTr("%1 of %2 sides done").arg(control._doneSides.length).arg(control._sideIds.length)
243 Item { Layout.fillWidth: true }
246 objectName: control._phase === "running" || control._phase === "cancelling" ? "sensorsSetup_cancelCalibration" : "sensorsSetup_closeCalibration"
247 text: control._phase === "cancelling" ? qsTr("Cancelling…") : control._phase === "failed" ? qsTr("Close") : qsTr("Cancel")
248 enabled: control._phase !== "cancelling" && !control.starting
249 visible: control._phase !== "done"
250 onClicked: control._phase === "running" ? control.cancelRequested() : control.closeRequested()
254 // One segment for each side. The firmware does not report progress within a side.
256 Layout.fillWidth: true
257 spacing: ScreenTools.defaultFontPixelWidth * 0.5
258 visible: control._orientationKind
261 model: control._sideIds
266 required property string modelData
267 readonly property int sideState: control._sideState(modelData)
269 objectName: "sensorsCal_segment_" + modelData
270 Layout.fillWidth: true
271 implicitHeight: ScreenTools.defaultFontPixelHeight * 0.5
273 color: sideState === 3 ? qgcPal.colorGreen : sideState === 2 ? qgcPal.accent : qgcPal.windowInset
274 border.color: sideState === 3 ? qgcPal.colorGreen : sideState === 2 ? qgcPal.accent : qgcPal.borderSubtle
280 objectName: "sensorsCal_progress"
281 Layout.fillWidth: true
282 value: control.sensorsController.calibrationProgress
283 visible: !control._orientationKind
289 objectName: "sensorsCal_callout"
290 Layout.fillWidth: true
291 implicitHeight: calloutLayout.implicitHeight + ScreenTools.defaultFontPixelHeight
292 radius: ScreenTools.defaultBorderRadius
293 color: control._callout.kind === "ok" ? Qt.alpha(qgcPal.colorGreen, 0.12) :
294 control._callout.kind === "err" ? Qt.alpha(qgcPal.colorRed, 0.12) :
295 control._callout.kind === "acc" ? qgcPal.accentSubtle : qgcPal.windowCard
296 border.color: control._callout.kind === "ok" ? qgcPal.colorGreen :
297 control._callout.kind === "err" ? qgcPal.colorRed :
298 control._callout.kind === "acc" ? qgcPal.accent : qgcPal.borderSubtle
303 anchors.margins: ScreenTools.defaultFontPixelHeight * 0.5
304 spacing: ScreenTools.defaultFontPixelWidth * 1.5
307 implicitWidth: ScreenTools.defaultFontPixelHeight * 2
308 implicitHeight: implicitWidth
309 radius: implicitWidth / 2
310 color: control._callout.kind === "ok" ? qgcPal.colorGreen : control._callout.kind === "err" ? qgcPal.colorRed :
311 control._callout.kind === "acc" ? qgcPal.accent : qgcPal.windowInset
312 border.color: qgcPal.borderSubtle
313 visible: control._callout.icon !== ""
316 anchors.centerIn: parent
317 text: control._callout.icon
319 color: control._callout.kind === "plain" ? qgcPal.textSecondary : qgcPal.window
324 Layout.fillWidth: true
328 objectName: "sensorsCal_calloutTitle"
329 Layout.fillWidth: true
330 text: control._callout.big
331 font.pointSize: ScreenTools.mediumFontPointSize
333 wrapMode: Text.WordWrap
334 color: control._callout.kind === "ok" ? qgcPal.successText : control._callout.kind === "err" ? qgcPal.errorText : qgcPal.textPrimary
338 objectName: "sensorsCal_calloutText"
339 Layout.fillWidth: true
340 text: control._callout.sub
341 color: qgcPal.textSecondary
342 wrapMode: Text.WordWrap
348 objectName: "sensorsSetup_startCalibration"
351 visible: control._phase === "ready"
352 enabled: !control.starting && !control.largeVehiclePending
353 onClicked: control.startRequested()
357 objectName: "sensorsSetup_retryCalibration"
360 visible: control._phase === "failed"
361 onClicked: control.startRequested()
365 objectName: "sensorsSetup_rebootVehicle"
366 text: qsTr("Reboot vehicle")
368 visible: control._phase === "done" && control.kind === "compass"
369 onClicked: control.rebootRequested()
373 objectName: "sensorsSetup_dismissCalibration"
375 primary: control.kind !== "compass"
376 visible: control._phase === "done"
377 onClicked: control.dismissed()
383 Layout.fillWidth: true
384 spacing: ScreenTools.defaultFontPixelWidth
385 visible: control._orientationKind
387 SensorsCalibrationStage {
388 vehicleKind: control.vehicleKind
389 Layout.fillWidth: true
390 Layout.fillHeight: true
391 Layout.preferredWidth: ScreenTools.defaultFontPixelWidth * 40
393 if (control._phase === "done" || control._phase === "failed") {
396 return control._detected !== "" ? control._detected : (control._suggested !== "" ? control._suggested : "level")
398 rotating: control.kind === "compass" && control._detected !== "" && control._phase === "running"
400 if (control._phase !== "running" || control._detected === "") {
403 return control.kind === "compass" ? qsTr("Rotate") : qsTr("Hold still")
405 active: control._detected !== ""
406 mark: control._phase === "done" ? "ok" : control._phase === "failed" ? "error" : ""
408 if (control._phase === "ready") {
409 return qsTr("Start here")
411 if (control._phase !== "running") {
414 return control._detected !== "" ? qsTr("Detected") : qsTr("Suggested")
417 if (control._phase === "done") {
418 return qsTr("All sides done")
420 if (control._phase === "failed") {
421 return qsTr("Calibration incomplete")
423 return Orientation.sideName(control._detected !== "" ? control._detected : control._suggested)
428 Layout.fillWidth: true
429 Layout.preferredWidth: ScreenTools.defaultFontPixelWidth * 34
430 Layout.alignment: Qt.AlignTop
431 spacing: ScreenTools.defaultFontPixelHeight * 0.3
434 model: control._sideIds
437 required property string modelData
439 objectName: control._sideObjectName(modelData)
441 calState: control._sideState(modelData)
442 suggested: control._phase !== "done" && control._phase !== "failed" && control._detected === "" && modelData === control._suggested
449 objectName: "sensorsCal_orientationRow"
450 Layout.fillWidth: true
451 spacing: ScreenTools.defaultFontPixelWidth
452 visible: control._phase === "ready" && control.orientationFact !== null
453 enabled: !control.starting && !control.largeVehiclePending
456 text: qsTr("Autopilot orientation")
461 fact: control.orientationFact
465 text: qsTr("Set this before calibrating.")
466 color: qgcPal.textSecondary
470 SensorsLargeVehicleForm {
472 Layout.fillWidth: true
474 message: control.largeVehicleMessage
475 success: control.largeVehicleSuccess
476 pending: control.largeVehiclePending
477 visible: control.kind === "compass" && control._phase === "ready"
478 enabled: !control.starting
480 onCalibrateRequested: (heading) => control.largeVehicleRequested(heading)
481 onRebootRequested: control.rebootRequested()
485 Layout.fillWidth: true
486 spacing: ScreenTools.defaultFontPixelWidth
487 visible: control._phase !== "ready" && (!control._orientationKind || control._phase === "failed")
490 objectName: "sensorsCal_logToggle"
491 text: (control.logOpen ? qsTr("Hide log") + " ▾" : qsTr("Show log") + " ▸")
495 onTapped: control.logOpen = !control.logOpen
500 Layout.fillWidth: true
501 text: control.sensorsController.calibrationLog.split("\n").pop()
502 font.family: ScreenTools.fixedFontFamily
503 font.pointSize: ScreenTools.smallFontPointSize
504 color: qgcPal.textSecondary
505 elide: Text.ElideRight
506 visible: !control.logOpen
511 Layout.fillWidth: true
512 Layout.preferredHeight: ScreenTools.defaultFontPixelHeight * 9
513 visible: control._phase !== "ready" && (!control._orientationKind || control._phase === "failed") && control.logOpen
516 objectName: "sensorsCal_log"
518 text: control.sensorsController.calibrationLog
519 font.family: ScreenTools.fixedFontFamily
520 font.pointSize: ScreenTools.smallFontPointSize
521 color: qgcPal.textPrimary
522 wrapMode: TextEdit.Wrap
524 background: Rectangle {
525 color: qgcPal.windowInset
526 border.color: qgcPal.borderSubtle
527 radius: ScreenTools.defaultBorderRadius