QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
APMFollowComponent.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Controls
3import QtQuick.Dialogs
4import QtQuick.Layouts
5
6import QGroundControl
7import QGroundControl.FactControls
8import QGroundControl.Controls
9import QGroundControl.PlanView
10
11SetupPage {
12 id: followPage
13 pageComponent: followPageComponent
14
15 FactPanelController {
16 id: controller
17 }
18
19 Component {
20 id: followPageComponent
21
22 ColumnLayout {
23 id: flowLayout
24 spacing: _margins
25
26 property Fact _followEnabled: controller.getParameterFact(-1, "FOLL_ENABLE")
27 property bool _followParamsAvailable: controller.parameterExists(-1, "FOLL_SYSID")
28 property Fact _followDistanceMax: controller.getParameterFact(-1, "FOLL_DIST_MAX", false /* reportMissing */)
29 property Fact _followSysId: controller.getParameterFact(-1, "FOLL_SYSID", false /* reportMissing */)
30 property Fact _followOffsetX: controller.getParameterFact(-1, "FOLL_OFS_X", false /* reportMissing */)
31 property Fact _followOffsetY: controller.getParameterFact(-1, "FOLL_OFS_Y", false /* reportMissing */)
32 property Fact _followOffsetZ: controller.getParameterFact(-1, "FOLL_OFS_Z", false /* reportMissing */)
33 property Fact _followOffsetType: controller.getParameterFact(-1, "FOLL_OFS_TYPE", false /* reportMissing */)
34 property Fact _followAltitudeType: controller.getParameterFact(-1, "FOLL_ALT_TYPE", false /* reportMissing */)
35 property Fact _followYawBehavior: controller.getParameterFact(-1, "FOLL_YAW_BEHAVE", false /* reportMissing */)
36 property int _followComboMaintainIndex: 0
37 property int _followComboSpecifyIndex: 1
38 property bool _followMaintain: followPositionCombo.currentIndex === _followComboMaintainIndex
39 property bool _supportedSetup: true
40 property bool _roverFirmware: controller.roverFirmware
41 property bool _showMainSetup: _followEnabled.rawValue == 1 && _supportedSetup
42 property bool _showOffsetsSetup: _showMainSetup && !_followMaintain
43 property real _textFieldWidth: ScreenTools.defaultFontPixelWidth * 15
44 property real _comboWidth: ScreenTools.defaultFontPixelWidth * 30
45
46 readonly property int _followYawBehaviorNone: 0
47 readonly property int _followYawBehaviorFace: 1
48 readonly property int _followYawBehaviorSame: 2
49 readonly property int _followYawBehaviorFlight: 3
50 readonly property int _followAltitudeTypeAbsolute: 0
51 readonly property int _followAltitudeTypeRelative: 1
52 readonly property int _followOffsetTypeRelative: 1
53
54 Component.onCompleted: _setUIFromParams()
55
56 function validateSupportedParamSetup() {
57 let followSysIdOk = _followSysId.rawValue == QGroundControl.settingsManager.mavlinkSettings.gcsMavlinkSystemID.rawValue
58 let followOffsetOk = _followOffsetType.rawValue == _followOffsetTypeRelative
59 let followAltOk = true
60 let followYawOk = true
61 if (!_roverFirmware) {
62 followAltOk = _followAltitudeType.rawValue == _followAltitudeTypeRelative
63 followYawOk = _followYawBehavior.rawValue == _followYawBehaviorNone || _followYawBehavior.rawValue == _followYawBehaviorFace || _followYawBehavior.rawValue == _followYawBehaviorFlight
64 }
65 _supportedSetup = followOffsetOk && followAltOk && followYawOk && followSysIdOk
66 console.log("_supportedSetup", _supportedSetup, followSysIdOk, followOffsetOk, followAltOk, followYawOk)
67 return _supportedSetup
68 }
69
70 function _setUIFromParams() {
71 if (!_followParamsAvailable || !validateSupportedParamSetup()) {
72 return
73 }
74
75 if (_followOffsetX.rawValue == 0 && _followOffsetY.rawValue == 0 && _followOffsetZ.rawValue == 0) {
76 followPositionCombo.currentIndex =_followComboMaintainIndex
77 controller.distance.rawValue = 0
78 controller.angle.rawValue = 0
79 controller.height.rawValue = 0
80 } else {
81 followPositionCombo.currentIndex =_followComboSpecifyIndex
82 let angleRadians = Math.atan2(_followOffsetX.rawValue, _followOffsetY.rawValue)
83 if (angleRadians == 0) {
84 controller.distance.rawValue = _followOffsetY.rawValue
85 } else {
86 controller.distance.rawValue = _followOffsetX.rawValue / Math.sin(angleRadians)
87 }
88 controller.angle.rawValue = _radiansToHeading(angleRadians)
89 }
90 controller.height.rawValue = -_followOffsetZ.rawValue
91 if (!_roverFirmware) {
92 let comboIndex = -1
93 for (let i=0; i<pointVehicleCombo.rgValues.length; i++) {
94 if (pointVehicleCombo.rgValues[i] == _followYawBehavior.rawValue) {
95 comboIndex = i
96 break
97 }
98 }
99
100 pointVehicleCombo.currentIndex = comboIndex
101 }
102 }
103
104 function _setFollowMeParamDefaults() {
105 _followSysId.rawValue = QGroundControl.settingsManager.mavlinkSettings.gcsMavlinkSystemID.rawValue
106 _followOffsetType.rawValue = _followOffsetTypeRelative
107 if (!_roverFirmware) {
108 _followAltitudeType.rawValue = _followAltitudeTypeRelative
109 _followYawBehavior.rawValue = _followYawBehaviorFace
110 }
111
112 controller.distance.value = controller.distance.defaultValue
113 controller.angle.value = controller.angle.defaultValue
114 controller.height.value = controller.height.defaultValue
115
116 _setXYOffsetByAngleAndDistance(controller.angle.rawValue, controller.distance.rawValue)
117 _followOffsetZ.rawValue = -controller.height.rawValue
118 _setUIFromParams()
119 }
120
121 function _setXYOffsetByAngleAndDistance(headingAngleDegrees, distance) {
122 if (distance == 0) {
123 _followOffsetX.rawValue = _followOffsetY.rawValue = 0
124 } else {
125 let angleRadians = _headingToRadians(headingAngleDegrees)
126 if (angleRadians == 0) {
127 _followOffsetX.rawValue = 0
128 _followOffsetY.rawValue = distance
129 } else {
130 _followOffsetX.rawValue = Math.sin(angleRadians) * distance
131 _followOffsetY.rawValue = Math.cos(angleRadians) * distance
132 if (Math.abs(_followOffsetX.rawValue) < 0.0001) {
133 _followOffsetX.rawValue = 0
134 }
135 if (Math.abs(_followOffsetY.rawValue) < 0.0001) {
136 _followOffsetY.rawValue = 0
137 }
138 }
139 }
140 }
141
142 function _radiansToHeading(radians) {
143 let geometricAngle = QGroundControl.unitsConversion.radiansToDegrees(radians)
144 let headingAngle = 90 - geometricAngle
145 if (headingAngle < 0) {
146 headingAngle += 360
147 } else if (headingAngle > 360) {
148 headingAngle -= 360
149 }
150 return headingAngle
151 }
152
153 function _headingToRadians(heading) {
154 let geometricAngle = -(heading - 90)
155 return QGroundControl.unitsConversion.degreesToRadians(geometricAngle)
156 }
157
158 function _distanceLabelText() {
159 return QGroundControl.unitsConversion.metersToAppSettingsHorizontalDistanceUnitsString(controller.distance.rawValue)
160 }
161
162 function _heightLabelText() {
163 return QGroundControl.unitsConversion.metersToAppSettingsVerticalDistanceUnitsString(controller.height.rawValue)
164 }
165
166 APMFollowComponentController {
167 id: controller
168
169 onMissingParametersAvailable: {
170 _followDistanceMax = controller.getParameterFact(-1, "FOLL_DIST_MAX")
171 _followSysId = controller.getParameterFact(-1, "FOLL_SYSID")
172 _followOffsetX = controller.getParameterFact(-1, "FOLL_OFS_X")
173 _followOffsetY = controller.getParameterFact(-1, "FOLL_OFS_Y")
174 _followOffsetZ = controller.getParameterFact(-1, "FOLL_OFS_Z")
175 _followOffsetType = controller.getParameterFact(-1, "FOLL_OFS_TYPE")
176 if (!_roverFirmware) {
177 _followAltitudeType = controller.getParameterFact(-1, "FOLL_ALT_TYPE")
178 _followYawBehavior = controller.getParameterFact(-1, "FOLL_YAW_BEHAVE")
179 }
180
181 _followParamsAvailable = true
182 vehicleParamRefreshLabel.visible = false
183 validateSupportedParamSetup()
184 }
185 }
186
187 QGCPalette { id: ggcPal; colorGroupEnabled: true }
188
189 QGCCheckBox {
190 text: qsTr("Enable Follow Me")
191 checked: _followEnabled.rawValue == 1
192 onClicked: {
193 if (checked) {
194 _followEnabled.rawValue = 1
195 let missingParameters = [ "FOLL_DIST_MAX", "FOLL_SYSID", "FOLL_OFS_X", "FOLL_OFS_Y", "FOLL_OFS_Z", "FOLL_OFS_TYPE" ]
196 if (!_roverFirmware) {
197 missingParameters.push("FOLL_ALT_TYPE", "FOLL_YAW_BEHAVE")
198 }
199 controller.getMissingParameters(missingParameters)
200 vehicleParamRefreshLabel.visible = true
201 } else {
202 _followEnabled.rawValue = 0
203 }
204 }
205 }
206
207 QGCLabel {
208 id: vehicleParamRefreshLabel
209 text: qsTr("Waiting for Vehicle to update")
210 visible: false
211 }
212
213 Column {
214 width: offsetSetupLayout.width
215 spacing: ScreenTools.defaultFontPixelWidth
216 visible: !_supportedSetup
217
218 QGCLabel {
219 anchors.left: parent.left
220 anchors.right: parent.right
221 text: qsTr("The vehicle parameters required for follow me are currently set in a way which is not supported. Using follow with this setup may lead to unpredictable/hazardous results.")
222 wrapMode: Text.WordWrap
223 }
224
225 QGCButton {
226 text: qsTr("Reset To Supported Settings")
227 onClicked: _setFollowMeParamDefaults()
228 }
229 }
230
231 ColumnLayout {
232 Layout.fillWidth: true
233 spacing: ScreenTools.defaultFontPixelWidth
234 visible: _showMainSetup
235
236 QGCGroupBox {
237 Layout.fillWidth: false
238 heading: qsTr("Follow Me Settings")
239
240 GridLayout {
241 columns: 2
242 rowSpacing: ScreenTools.defaultFontPixelWidth
243 columnSpacing: ScreenTools.defaultFontPixelWidth
244
245 QGCLabel { text: qsTr("Vehicle Position") }
246 QGCComboBox {
247 id: followPositionCombo
248 sizeToContents: true
249 Layout.maximumWidth: _comboWidth
250 model: [ qsTr("Maintain Current Offsets"), qsTr("Specify Offsets")]
251
252 onActivated: (index) => {
253 if (index == 0) {
254 _followOffsetX.rawValue = _followOffsetY.rawValue = _followOffsetZ.rawValue = 0
255 _setUIFromParams()
256 } else {
257 _setFollowMeParamDefaults()
258 }
259 }
260 }
261
262 QGCLabel {
263 text: qsTr("Point Vehicle")
264 visible: !_roverFirmware
265 }
266 QGCComboBox {
267 id: pointVehicleCombo
268 sizeToContents: true
269 Layout.maximumWidth: _comboWidth
270 model: rgText
271 visible: !_roverFirmware
272 onActivated: (index) => { _followYawBehavior.rawValue = rgValues[index] }
273
274 property var rgText: [ qsTr("Maintain current vehicle orientation"), qsTr("Point at ground station location"), qsTr("Same direction as ground station movement") ]
275 property var rgValues: [ _followYawBehaviorNone, _followYawBehaviorFace, _followYawBehaviorFlight ]
276 }
277
278 QGCLabel {
279 Layout.columnSpan: 2
280 Layout.alignment: Qt.AlignHCenter
281 text: qsTr("Vehicle Offsets")
282 visible: !_followMaintain
283 }
284
285 QGCLabel {
286 text: qsTr("Angle")
287 visible: !_followMaintain
288 }
289 FactTextField {
290 Layout.preferredWidth: _textFieldWidth
291 visible: !_followMaintain
292 fact: controller.angle
293 onUpdated: _setXYOffsetByAngleAndDistance(controller.angle.rawValue, controller.distance.rawValue)
294 }
295
296 QGCLabel {
297 text: qsTr("Distance")
298 visible: !_followMaintain
299 }
300 FactTextField {
301 Layout.preferredWidth: _textFieldWidth
302 visible: !_followMaintain
303 fact: controller.distance
304 onUpdated: _setXYOffsetByAngleAndDistance(controller.angle.rawValue, controller.distance.rawValue)
305 }
306
307 QGCLabel {
308 text: qsTr("Height")
309 visible: !_roverFirmware && !_followMaintain
310 }
311 FactTextField {
312 Layout.preferredWidth: _textFieldWidth
313 visible: !_roverFirmware && !_followMaintain
314 fact: controller.height
315 onUpdated: _followOffsetZ.rawValue = -controller.height.rawValue
316 }
317 } // GridLayout
318 } // QGCGroupBox
319 }
320
321 RowLayout {
322 id: offsetSetupLayout
323 spacing: ScreenTools.defaultFontPixelWidth * 2
324 visible: _showOffsetsSetup
325
326 Item {
327 height: ScreenTools.defaultFontPixelWidth * 50
328 width: height
329
330 Rectangle {
331 anchors.top: parent.top
332 anchors.bottom: parent.bottom
333 anchors.horizontalCenter: parent.horizontalCenter
334 width: 3
335 color: qgcPal.windowCard
336 }
337
338 Rectangle {
339 anchors.left: parent.left
340 anchors.right: parent.right
341 anchors.verticalCenter: parent.verticalCenter
342 height: 3
343 color: qgcPal.windowCard
344 }
345
346 QGCLabel {
347 anchors.horizontalCenter: parent.horizontalCenter
348 anchors.topMargin: parent.height / 4
349 anchors.top: parent.top
350 text: qsTr("Click in the graphic to change angle")
351 opacity: 0.5
352 }
353
354 Image {
355 id: gcsIcon
356 anchors.centerIn: parent
357 source: "/res/QGCLogoArrow.svg"
358 mipmap: true
359 antialiasing: true
360 fillMode: Image.PreserveAspectFit
361 height: ScreenTools.defaultFontPixelHeight * 2.5
362 sourceSize.height: height
363 }
364
365 Item {
366 id: vehicleHolder
367 anchors.fill: parent
368
369 transform: Rotation {
370 origin.x: vehicleHolder.width / 2
371 origin.y: vehicleHolder.height / 2
372 angle: controller.angle.rawValue
373 }
374
375 Image {
376 id: vehicleIcon
377 anchors.top: parent.top
378 anchors.horizontalCenter: parent.horizontalCenter
379 source: controller.vehicle.vehicleImageOpaque
380 mipmap: true
381 height: ScreenTools.defaultFontPixelHeight * 2.5
382 sourceSize.height: height
383 fillMode: Image.PreserveAspectFit
384
385 transform: Rotation {
386 origin.x: vehicleIcon.width / 2
387 origin.y: vehicleIcon.height / 2
388 angle: _roverFirmware || !_followYawBehavior ? 0 :
389 (_followYawBehavior.rawValue == _followYawBehaviorNone ?
390 0 :
391 (_followYawBehavior.rawValue == _followYawBehaviorFace ?
392 180 :
393 -controller.angle.rawValue))
394 }
395 }
396
397 Rectangle {
398 id: distanceLine
399 x: parent.width / 2
400 y: vehicleIcon.height
401 height: (parent.height / 2) - (vehicleIcon.height + (gcsIcon.height / 2))
402 width: 2
403 color: qgcPal.textPrimary
404 opacity: 0.4
405
406 Rectangle {
407 anchors.top: parent.top
408 anchors.horizontalCenter: parent.horizontalCenter
409 width: ScreenTools.defaultFontPixelWidth * 2
410 height: 2
411 color: qgcPal.textPrimary
412 }
413
414 Rectangle {
415 anchors.bottom: parent.bottom
416 anchors.horizontalCenter: parent.horizontalCenter
417 width: ScreenTools.defaultFontPixelWidth * 2
418 height: 2
419 color: qgcPal.textPrimary
420 }
421 }
422
423 QGCLabel {
424 id: distanceLabel
425 anchors.centerIn: distanceLine
426 text: _distanceLabelText()
427
428 transform: Rotation {
429 origin.x: distanceLabel.width / 2
430 origin.y: distanceLabel.height / 2
431 angle: -controller.angle.rawValue
432 }
433 }
434 }
435
436 MouseArea {
437 anchors.fill: parent
438
439 onClicked: (mouse) => {
440 // Translate x,y to centered
441 let x = mouse.x - (width / 2)
442 let y = (height - mouse.y) - (height / 2)
443 controller.angle.rawValue = _radiansToHeading(Math.atan2(y, x))
444 _setXYOffsetByAngleAndDistance(controller.angle.rawValue, controller.distance.rawValue)
445 }
446 }
447 }
448
449 ColumnLayout {
450 Layout.fillHeight: true
451 spacing: 0
452 visible: !_roverFirmware
453
454 Image {
455 id: vehicleIconHeight
456 source: controller.vehicle.vehicleImageOpaque
457 mipmap: true
458 height: ScreenTools.defaultFontPixelHeight * 2.5
459 sourceSize.height: height
460 fillMode: Image.PreserveAspectFit
461
462 transform: Rotation {
463 origin.x: vehicleIconHeight.width / 2
464 origin.y: vehicleIconHeight.height / 2
465 angle: 65
466 axis { x: 1; y: 0; z: 0 }
467 }
468 }
469
470 Item {
471 Layout.alignment: Qt.AlignHCenter
472 Layout.fillHeight: true
473 width: Math.max(ScreenTools.defaultFontPixelWidth * 2, heightValueLabel.width)
474
475 Rectangle {
476 id: heightLine
477 anchors.top: parent.top
478 anchors.bottom: parent.bottom
479 anchors.horizontalCenter: parent.horizontalCenter
480 width: 2
481 color: qgcPal.textPrimary
482 opacity: 0.4
483
484 Rectangle {
485 anchors.top: parent.top
486 anchors.horizontalCenter: parent.horizontalCenter
487 width: ScreenTools.defaultFontPixelWidth * 2
488 height: 2
489 color: qgcPal.textPrimary
490 }
491
492 Rectangle {
493 anchors.bottom: parent.bottom
494 anchors.horizontalCenter: parent.horizontalCenter
495 width: ScreenTools.defaultFontPixelWidth * 2
496 height: 2
497 color: qgcPal.textPrimary
498 }
499 }
500
501 QGCLabel {
502 id: heightValueLabel
503 anchors.centerIn: parent
504 text: _heightLabelText()
505 }
506 }
507
508 MissionItemIndexLabel {
509 id: launchIconHeight
510 Layout.alignment: Qt.AlignHCenter
511 label: qsTr("L")
512
513 transform: [
514 Scale {
515 origin.x: launchIconHeight.width / 2
516 origin.y: launchIconHeight.height / 2
517 xScale: 1.5
518 yScale: 2.5
519
520 },
521 Rotation {
522 origin.x: launchIconHeight.width / 2
523 origin.y: launchIconHeight.height / 2
524 angle: 75
525 axis { x: 1; y: 0; z: 0 }
526 } ]
527 }
528 }
529 }
530
531 Connections {
532 target: QGroundControl.unitsConversion
533
534 function onUnitsChanged() {
535 distanceLabel.text = Qt.binding(_distanceLabelText)
536 heightValueLabel.text = Qt.binding(_heightLabelText)
537 }
538 }
539 }
540 }
541}