QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
SensorsCalibrationPanel.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Controls
3import QtQuick.Layouts
4
5import QGroundControl
6import QGroundControl.Controls
7import QGroundControl.FactControls
8
9import "PX4SensorsOrientation.js" as Orientation
10import "PX4SensorsRows.js" as SensorsRows
11
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.
15ConfigRowsPanel {
16 id: control
17
18 required property var sensorsController
19
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
39
40 /// Start, or retry after a failure
41 signal startRequested
42 signal cancelRequested
43 /// Cancel before the calibration started, or Close after a failure
44 signal closeRequested
45 /// Done after a success
46 signal dismissed
47 signal rebootRequested
48 signal largeVehicleRequested(real heading)
49
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: {
53 if (ready) {
54 return "ready"
55 }
56 if (sensorsController.calibrationActive) {
57 return sensorsController.waitingForCancel ? "cancelling" : "running"
58 }
59 if (sensorsController.calibrationResult === SensorsComponentController.CalibrationResultSuccess) {
60 return "done"
61 }
62 if (sensorsController.calibrationResult === SensorsComponentController.CalibrationResultFailed) {
63 return "failed"
64 }
65 return "ready"
66 }
67
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] : ""
78 }
79 readonly property string _suggested: {
80 const unused = Orientation.sideIds.filter(side => _sideIds.indexOf(side) < 0)
81 return Orientation.suggestNext(_current, _doneSides.concat(unused))
82 }
83 readonly property var _callout: _makeCallout()
84
85 function _readStates(revision) {
86 const states = {}
87 for (const side of Orientation.sideIds) {
88 states[side] = {
89 used: _phase === "ready" || sensorsController[Orientation.visibleProperty(side)],
90 state: _phase === "ready" ? 0 : sensorsController[Orientation.stateProperty(side)]
91 }
92 }
93 return states
94 }
95
96 function _sideState(side) {
97 return _states[side].state
98 }
99
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.")
106 }
107 if (known !== text && text !== "") {
108 return known
109 }
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.")
112 }
113
114 // kind is "plain", "acc", "ok" or "err"
115 function _makeCallout() {
116 switch (_phase) {
117 case "ready":
118 if (starting) {
119 return { kind: "plain", icon: "…", big: qsTr("Starting…"), sub: qsTr("Waiting for the vehicle.") }
120 }
121 if (startMessage !== "") {
122 return { kind: "err", icon: "!", big: qsTr("Could not start"), sub: startMessage }
123 }
124 if (!_orientationKind) {
125 return { kind: "plain", icon: "▶", big: qsTr("Ready to calibrate"), sub: "" }
126 }
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.") }
130 case "cancelling":
131 return { kind: "plain", icon: "…", big: qsTr("Waiting for the vehicle to cancel"), sub: qsTr("This can take a few seconds.") }
132 case "done":
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.") }
135 case "failed":
136 return { kind: "err", icon: "!", big: qsTr("Calibration failed"), sub: _failureText() }
137 }
138
139 if (!_orientationKind) {
140 return { kind: "acc", icon: "…", big: qsTr("Calibrating %1").arg(_kindName.toLowerCase()), sub: qsTr("Follow the instructions in the log.") }
141 }
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.") }
145 }
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.") }
148 }
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.") : "" }
152 }
153 return { kind: "acc", icon: "→", big: qsTr("Next: %1").arg(Orientation.sideName(_suggested)), sub: "" }
154 }
155
156 function _sideObjectName(side) {
157 return {
158 level: "sensorsCal_downSide",
159 flipped: "sensorsCal_upsideDownSide",
160 nose: "sensorsCal_noseDownSide",
161 tail: "sensorsCal_tailDownSide",
162 left: "sensorsCal_leftSide",
163 right: "sensorsCal_rightSide"
164 }[side]
165 }
166
167 function _refresh() {
168 const wasDone = _doneSides
169 _revision++
170 if (_detected !== "") {
171 _current = _detected
172 _lastDone = ""
173 }
174 const newlyDone = _doneSides.filter(side => wasDone.indexOf(side) < 0)
175 if (newlyDone.length > 0) {
176 _lastDone = newlyDone[newlyDone.length - 1]
177 }
178 }
179
180 QGCPalette { id: qgcPal }
181
182 Connections {
183 target: control.sensorsController
184
185 function onOrientationCalSidesStateChanged() { control._refresh() }
186 function onOrientationCalSidesVisibleChanged() { control._revision++ }
187
188 // A new calibration, or the ready screen after one ends, starts again from level
189 function onCalibrationActiveChanged() {
190 control._current = ""
191 control._lastDone = ""
192 control._revision++
193 }
194
195 function onCalibrationResultChanged() {
196 control._revision++
197 if (control.sensorsController.calibrationResult === SensorsComponentController.CalibrationResultFailed) {
198 control.logOpen = true
199 }
200 }
201 }
202
203 RowLayout {
204 Layout.fillWidth: true
205 spacing: ScreenTools.defaultFontPixelWidth
206
207 Rectangle {
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
215 }
216
217 QGCLabel {
218 objectName: "sensorsCal_title"
219 text: control._kindName
220 font.bold: true
221 font.capitalization: Font.AllUppercase
222 font.letterSpacing: ScreenTools.defaultFontPixelWidth * 0.1
223 }
224
225 QGCChip {
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
230 text: {
231 switch (control._phase) {
232 case "done":
233 return qsTr("Calibrated")
234 case "failed":
235 return qsTr("Failed")
236 case "ready":
237 return qsTr("%1 sides").arg(control._sideIds.length)
238 }
239 return qsTr("%1 of %2 sides done").arg(control._doneSides.length).arg(control._sideIds.length)
240 }
241 }
242
243 Item { Layout.fillWidth: true }
244
245 QGCButton {
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()
251 }
252 }
253
254 // One segment for each side. The firmware does not report progress within a side.
255 RowLayout {
256 Layout.fillWidth: true
257 spacing: ScreenTools.defaultFontPixelWidth * 0.5
258 visible: control._orientationKind
259
260 Repeater {
261 model: control._sideIds
262
263 Rectangle {
264 id: segment
265
266 required property string modelData
267 readonly property int sideState: control._sideState(modelData)
268
269 objectName: "sensorsCal_segment_" + modelData
270 Layout.fillWidth: true
271 implicitHeight: ScreenTools.defaultFontPixelHeight * 0.5
272 radius: height / 2
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
275 }
276 }
277 }
278
279 ProgressBar {
280 objectName: "sensorsCal_progress"
281 Layout.fillWidth: true
282 value: control.sensorsController.calibrationProgress
283 visible: !control._orientationKind
284 }
285
286 Rectangle {
287 id: callout
288
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
299
300 RowLayout {
301 id: calloutLayout
302 anchors.fill: parent
303 anchors.margins: ScreenTools.defaultFontPixelHeight * 0.5
304 spacing: ScreenTools.defaultFontPixelWidth * 1.5
305
306 Rectangle {
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 !== ""
314
315 QGCLabel {
316 anchors.centerIn: parent
317 text: control._callout.icon
318 font.bold: true
319 color: control._callout.kind === "plain" ? qgcPal.textSecondary : qgcPal.window
320 }
321 }
322
323 ColumnLayout {
324 Layout.fillWidth: true
325 spacing: 0
326
327 QGCLabel {
328 objectName: "sensorsCal_calloutTitle"
329 Layout.fillWidth: true
330 text: control._callout.big
331 font.pointSize: ScreenTools.mediumFontPointSize
332 font.bold: true
333 wrapMode: Text.WordWrap
334 color: control._callout.kind === "ok" ? qgcPal.successText : control._callout.kind === "err" ? qgcPal.errorText : qgcPal.textPrimary
335 }
336
337 QGCLabel {
338 objectName: "sensorsCal_calloutText"
339 Layout.fillWidth: true
340 text: control._callout.sub
341 color: qgcPal.textSecondary
342 wrapMode: Text.WordWrap
343 visible: text !== ""
344 }
345 }
346
347 QGCButton {
348 objectName: "sensorsSetup_startCalibration"
349 text: qsTr("Start")
350 primary: true
351 visible: control._phase === "ready"
352 enabled: !control.starting && !control.largeVehiclePending
353 onClicked: control.startRequested()
354 }
355
356 QGCButton {
357 objectName: "sensorsSetup_retryCalibration"
358 text: qsTr("Retry")
359 primary: true
360 visible: control._phase === "failed"
361 onClicked: control.startRequested()
362 }
363
364 QGCButton {
365 objectName: "sensorsSetup_rebootVehicle"
366 text: qsTr("Reboot vehicle")
367 primary: true
368 visible: control._phase === "done" && control.kind === "compass"
369 onClicked: control.rebootRequested()
370 }
371
372 QGCButton {
373 objectName: "sensorsSetup_dismissCalibration"
374 text: qsTr("Done")
375 primary: control.kind !== "compass"
376 visible: control._phase === "done"
377 onClicked: control.dismissed()
378 }
379 }
380 }
381
382 RowLayout {
383 Layout.fillWidth: true
384 spacing: ScreenTools.defaultFontPixelWidth
385 visible: control._orientationKind
386
387 SensorsCalibrationStage {
388 vehicleKind: control.vehicleKind
389 Layout.fillWidth: true
390 Layout.fillHeight: true
391 Layout.preferredWidth: ScreenTools.defaultFontPixelWidth * 40
392 side: {
393 if (control._phase === "done" || control._phase === "failed") {
394 return "level"
395 }
396 return control._detected !== "" ? control._detected : (control._suggested !== "" ? control._suggested : "level")
397 }
398 rotating: control.kind === "compass" && control._detected !== "" && control._phase === "running"
399 instruction: {
400 if (control._phase !== "running" || control._detected === "") {
401 return ""
402 }
403 return control.kind === "compass" ? qsTr("Rotate") : qsTr("Hold still")
404 }
405 active: control._detected !== ""
406 mark: control._phase === "done" ? "ok" : control._phase === "failed" ? "error" : ""
407 tag: {
408 if (control._phase === "ready") {
409 return qsTr("Start here")
410 }
411 if (control._phase !== "running") {
412 return ""
413 }
414 return control._detected !== "" ? qsTr("Detected") : qsTr("Suggested")
415 }
416 caption: {
417 if (control._phase === "done") {
418 return qsTr("All sides done")
419 }
420 if (control._phase === "failed") {
421 return qsTr("Calibration incomplete")
422 }
423 return Orientation.sideName(control._detected !== "" ? control._detected : control._suggested)
424 }
425 }
426
427 ColumnLayout {
428 Layout.fillWidth: true
429 Layout.preferredWidth: ScreenTools.defaultFontPixelWidth * 34
430 Layout.alignment: Qt.AlignTop
431 spacing: ScreenTools.defaultFontPixelHeight * 0.3
432
433 Repeater {
434 model: control._sideIds
435
436 SensorsSideRow {
437 required property string modelData
438
439 objectName: control._sideObjectName(modelData)
440 side: modelData
441 calState: control._sideState(modelData)
442 suggested: control._phase !== "done" && control._phase !== "failed" && control._detected === "" && modelData === control._suggested
443 }
444 }
445 }
446 }
447
448 RowLayout {
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
454
455 QGCLabel {
456 text: qsTr("Autopilot orientation")
457 }
458
459 FactComboBox {
460 sizeToContents: true
461 fact: control.orientationFact
462 }
463
464 QGCLabel {
465 text: qsTr("Set this before calibrating.")
466 color: qgcPal.textSecondary
467 }
468 }
469
470 SensorsLargeVehicleForm {
471 id: largeVehicleForm
472 Layout.fillWidth: true
473 gps: control.gps
474 message: control.largeVehicleMessage
475 success: control.largeVehicleSuccess
476 pending: control.largeVehiclePending
477 visible: control.kind === "compass" && control._phase === "ready"
478 enabled: !control.starting
479
480 onCalibrateRequested: (heading) => control.largeVehicleRequested(heading)
481 onRebootRequested: control.rebootRequested()
482 }
483
484 RowLayout {
485 Layout.fillWidth: true
486 spacing: ScreenTools.defaultFontPixelWidth
487 visible: control._phase !== "ready" && (!control._orientationKind || control._phase === "failed")
488
489 QGCLabel {
490 objectName: "sensorsCal_logToggle"
491 text: (control.logOpen ? qsTr("Hide log") + " ▾" : qsTr("Show log") + " ▸")
492 color: qgcPal.accent
493
494 TapHandler {
495 onTapped: control.logOpen = !control.logOpen
496 }
497 }
498
499 QGCLabel {
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
507 }
508 }
509
510 ScrollView {
511 Layout.fillWidth: true
512 Layout.preferredHeight: ScreenTools.defaultFontPixelHeight * 9
513 visible: control._phase !== "ready" && (!control._orientationKind || control._phase === "failed") && control.logOpen
514
515 TextArea {
516 objectName: "sensorsCal_log"
517 readOnly: true
518 text: control.sensorsController.calibrationLog
519 font.family: ScreenTools.fixedFontFamily
520 font.pointSize: ScreenTools.smallFontPointSize
521 color: qgcPal.textPrimary
522 wrapMode: TextEdit.Wrap
523
524 background: Rectangle {
525 color: qgcPal.windowInset
526 border.color: qgcPal.borderSubtle
527 radius: ScreenTools.defaultBorderRadius
528 }
529 }
530 }
531}