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 APMFollowComponentController {
159 id: controller
160
161 onMissingParametersAvailable: {
162 _followDistanceMax = controller.getParameterFact(-1, "FOLL_DIST_MAX")
163 _followSysId = controller.getParameterFact(-1, "FOLL_SYSID")
164 _followOffsetX = controller.getParameterFact(-1, "FOLL_OFS_X")
165 _followOffsetY = controller.getParameterFact(-1, "FOLL_OFS_Y")
166 _followOffsetZ = controller.getParameterFact(-1, "FOLL_OFS_Z")
167 _followOffsetType = controller.getParameterFact(-1, "FOLL_OFS_TYPE")
168 if (!_roverFirmware) {
169 _followAltitudeType = controller.getParameterFact(-1, "FOLL_ALT_TYPE")
170 _followYawBehavior = controller.getParameterFact(-1, "FOLL_YAW_BEHAVE")
171 }
172
173 _followParamsAvailable = true
174 vehicleParamRefreshLabel.visible = false
175 validateSupportedParamSetup()
176 }
177 }
178
179 QGCPalette { id: ggcPal; colorGroupEnabled: true }
180
181 QGCCheckBox {
182 text: qsTr("Enable Follow Me")
183 checked: _followEnabled.rawValue == 1
184 onClicked: {
185 if (checked) {
186 _followEnabled.rawValue = 1
187 let missingParameters = [ "FOLL_DIST_MAX", "FOLL_SYSID", "FOLL_OFS_X", "FOLL_OFS_Y", "FOLL_OFS_Z", "FOLL_OFS_TYPE" ]
188 if (!_roverFirmware) {
189 missingParameters.push("FOLL_ALT_TYPE", "FOLL_YAW_BEHAVE")
190 }
191 controller.getMissingParameters(missingParameters)
192 vehicleParamRefreshLabel.visible = true
193 } else {
194 _followEnabled.rawValue = 0
195 }
196 }
197 }
198
199 QGCLabel {
200 id: vehicleParamRefreshLabel
201 text: qsTr("Waiting for Vehicle to update")
202 visible: false
203 }
204
205 Column {
206 width: offsetSetupLayout.width
207 spacing: ScreenTools.defaultFontPixelWidth
208 visible: !_supportedSetup
209
210 QGCLabel {
211 anchors.left: parent.left
212 anchors.right: parent.right
213 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.")
214 wrapMode: Text.WordWrap
215 }
216
217 QGCButton {
218 text: qsTr("Reset To Supported Settings")
219 onClicked: _setFollowMeParamDefaults()
220 }
221 }
222
223 ColumnLayout {
224 Layout.fillWidth: true
225 spacing: ScreenTools.defaultFontPixelWidth
226 visible: _showMainSetup
227
228 QGCGroupBox {
229 title: qsTr("Follow Me Settings")
230
231 GridLayout {
232 columns: 2
233 rowSpacing: ScreenTools.defaultFontPixelWidth
234 columnSpacing: ScreenTools.defaultFontPixelWidth
235
236 QGCLabel { text: qsTr("Vehicle Position") }
237 QGCComboBox {
238 id: followPositionCombo
239 sizeToContents: true
240 Layout.maximumWidth: _comboWidth
241 model: [ qsTr("Maintain Current Offsets"), qsTr("Specify Offsets")]
242
243 onActivated: (index) => {
244 if (index == 0) {
245 _followOffsetX.rawValue = _followOffsetY.rawValue = _followOffsetZ.rawValue = 0
246 _setUIFromParams()
247 } else {
248 _setFollowMeParamDefaults()
249 }
250 }
251 }
252
253 QGCLabel {
254 text: qsTr("Point Vehicle")
255 visible: !_roverFirmware
256 }
257 QGCComboBox {
258 id: pointVehicleCombo
259 sizeToContents: true
260 Layout.maximumWidth: _comboWidth
261 model: rgText
262 visible: !_roverFirmware
263 onActivated: (index) => { _followYawBehavior.rawValue = rgValues[index] }
264
265 property var rgText: [ qsTr("Maintain current vehicle orientation"), qsTr("Point at ground station location"), qsTr("Same direction as ground station movement") ]
266 property var rgValues: [ _followYawBehaviorNone, _followYawBehaviorFace, _followYawBehaviorFlight ]
267 }
268
269 QGCLabel {
270 Layout.columnSpan: 2
271 Layout.alignment: Qt.AlignHCenter
272 text: qsTr("Vehicle Offsets")
273 visible: !_followMaintain
274 }
275
276 QGCLabel {
277 text: qsTr("Angle")
278 visible: !_followMaintain
279 }
280 FactTextField {
281 Layout.preferredWidth: _textFieldWidth
282 visible: !_followMaintain
283 fact: controller.angle
284 onUpdated: _setXYOffsetByAngleAndDistance(controller.angle.rawValue, controller.distance.rawValue)
285 }
286
287 QGCLabel {
288 text: qsTr("Distance")
289 visible: !_followMaintain
290 }
291 FactTextField {
292 Layout.preferredWidth: _textFieldWidth
293 visible: !_followMaintain
294 fact: controller.distance
295 onUpdated: _setXYOffsetByAngleAndDistance(controller.angle.rawValue, controller.distance.rawValue)
296 }
297
298 QGCLabel {
299 text: qsTr("Height")
300 visible: !_roverFirmware && !_followMaintain
301 }
302 FactTextField {
303 Layout.preferredWidth: _textFieldWidth
304 visible: !_roverFirmware && !_followMaintain
305 fact: controller.height
306 onUpdated: _followOffsetZ.rawValue = -controller.height.rawValue
307 }
308 } // GridLayout
309 } // QGCGroupBox
310 }
311
312 RowLayout {
313 id: offsetSetupLayout
314 spacing: ScreenTools.defaultFontPixelWidth * 2
315 visible: _showOffsetsSetup
316
317 Item {
318 height: ScreenTools.defaultFontPixelWidth * 50
319 width: height
320
321 Rectangle {
322 anchors.top: parent.top
323 anchors.bottom: parent.bottom
324 anchors.horizontalCenter: parent.horizontalCenter
325 width: 3
326 color: qgcPal.windowShade
327 }
328
329 Rectangle {
330 anchors.left: parent.left
331 anchors.right: parent.right
332 anchors.verticalCenter: parent.verticalCenter
333 height: 3
334 color: qgcPal.windowShade
335 }
336
337 QGCLabel {
338 anchors.horizontalCenter: parent.horizontalCenter
339 anchors.topMargin: parent.height / 4
340 anchors.top: parent.top
341 text: qsTr("Click in the graphic to change angle")
342 opacity: 0.5
343 }
344
345 Image {
346 id: gcsIcon
347 anchors.centerIn: parent
348 source: "/res/QGCLogoArrow.svg"
349 mipmap: true
350 antialiasing: true
351 fillMode: Image.PreserveAspectFit
352 height: ScreenTools.defaultFontPixelHeight * 2.5
353 sourceSize.height: height
354 }
355
356 Item {
357 id: vehicleHolder
358 anchors.fill: parent
359
360 transform: Rotation {
361 origin.x: vehicleHolder.width / 2
362 origin.y: vehicleHolder.height / 2
363 angle: controller.angle.rawValue
364 }
365
366 Image {
367 id: vehicleIcon
368 anchors.top: parent.top
369 anchors.horizontalCenter: parent.horizontalCenter
370 source: controller.vehicle.vehicleImageOpaque
371 mipmap: true
372 height: ScreenTools.defaultFontPixelHeight * 2.5
373 sourceSize.height: height
374 fillMode: Image.PreserveAspectFit
375
376 transform: Rotation {
377 origin.x: vehicleIcon.width / 2
378 origin.y: vehicleIcon.height / 2
379 angle: _roverFirmware || !_followYawBehavior ? 0 :
380 (_followYawBehavior.rawValue == _followYawBehaviorNone ?
381 0 :
382 (_followYawBehavior.rawValue == _followYawBehaviorFace ?
383 180 :
384 -controller.angle.rawValue))
385 }
386 }
387
388 Rectangle {
389 id: distanceLine
390 x: parent.width / 2
391 y: vehicleIcon.height
392 height: (parent.height / 2) - (vehicleIcon.height + (gcsIcon.height / 2))
393 width: 2
394 color: qgcPal.text
395 opacity: 0.4
396
397 Rectangle {
398 anchors.top: parent.top
399 anchors.horizontalCenter: parent.horizontalCenter
400 width: ScreenTools.defaultFontPixelWidth * 2
401 height: 2
402 color: qgcPal.text
403 }
404
405 Rectangle {
406 anchors.bottom: parent.bottom
407 anchors.horizontalCenter: parent.horizontalCenter
408 width: ScreenTools.defaultFontPixelWidth * 2
409 height: 2
410 color: qgcPal.text
411 }
412 }
413
414 QGCLabel {
415 id: distanceLabel
416 anchors.centerIn: distanceLine
417 text: controller.distance.valueString + " " + QGroundControl.unitsConversion.appSettingsHorizontalDistanceUnitsString
418
419 transform: Rotation {
420 origin.x: distanceLabel.width / 2
421 origin.y: distanceLabel.height / 2
422 angle: -controller.angle.rawValue
423 }
424 }
425 }
426
427 MouseArea {
428 anchors.fill: parent
429
430 onClicked: (mouse) => {
431 // Translate x,y to centered
432 let x = mouse.x - (width / 2)
433 let y = (height - mouse.y) - (height / 2)
434 controller.angle.rawValue = _radiansToHeading(Math.atan2(y, x))
435 _setXYOffsetByAngleAndDistance(controller.angle.rawValue, controller.distance.rawValue)
436 }
437 }
438 }
439
440 ColumnLayout {
441 Layout.fillHeight: true
442 spacing: 0
443 visible: !_roverFirmware
444
445 Image {
446 id: vehicleIconHeight
447 source: controller.vehicle.vehicleImageOpaque
448 mipmap: true
449 height: ScreenTools.defaultFontPixelHeight * 2.5
450 sourceSize.height: height
451 fillMode: Image.PreserveAspectFit
452
453 transform: Rotation {
454 origin.x: vehicleIconHeight.width / 2
455 origin.y: vehicleIconHeight.height / 2
456 angle: 65
457 axis { x: 1; y: 0; z: 0 }
458 }
459 }
460
461 Item {
462 Layout.alignment: Qt.AlignHCenter
463 Layout.fillHeight: true
464 width: Math.max(ScreenTools.defaultFontPixelWidth * 2, heightValueLabel.width)
465
466 Rectangle {
467 id: heightLine
468 anchors.top: parent.top
469 anchors.bottom: parent.bottom
470 anchors.horizontalCenter: parent.horizontalCenter
471 width: 2
472 color: qgcPal.text
473 opacity: 0.4
474
475 Rectangle {
476 anchors.top: parent.top
477 anchors.horizontalCenter: parent.horizontalCenter
478 width: ScreenTools.defaultFontPixelWidth * 2
479 height: 2
480 color: qgcPal.text
481 }
482
483 Rectangle {
484 anchors.bottom: parent.bottom
485 anchors.horizontalCenter: parent.horizontalCenter
486 width: ScreenTools.defaultFontPixelWidth * 2
487 height: 2
488 color: qgcPal.text
489 }
490 }
491
492 QGCLabel {
493 id: heightValueLabel
494 anchors.centerIn: parent
495 text: controller.height.valueString + " " + QGroundControl.unitsConversion.appSettingsHorizontalDistanceUnitsString
496 }
497 }
498
499 MissionItemIndexLabel {
500 id: launchIconHeight
501 Layout.alignment: Qt.AlignHCenter
502 label: qsTr("L")
503
504 transform: [
505 Scale {
506 origin.x: launchIconHeight.width / 2
507 origin.y: launchIconHeight.height / 2
508 xScale: 1.5
509 yScale: 2.5
510
511 },
512 Rotation {
513 origin.x: launchIconHeight.width / 2
514 origin.y: launchIconHeight.height / 2
515 angle: 75
516 axis { x: 1; y: 0; z: 0 }
517 } ]
518 }
519 }
520 }
521 }
522 }
523}