APMFollowComponent.qml 25.3 KB
Newer Older
DonLakeFlyer's avatar
DonLakeFlyer committed
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
DonLakeFlyer's avatar
DonLakeFlyer committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/


import QtQuick          2.3
import QtQuick.Controls 1.2
import QtQuick.Dialogs  1.2
import QtQuick.Layouts  1.2

import QGroundControl               1.0
import QGroundControl.FactSystem    1.0
import QGroundControl.FactControls  1.0
import QGroundControl.Palette       1.0
import QGroundControl.Controls      1.0
import QGroundControl.ScreenTools   1.0
import QGroundControl.Controllers   1.0

SetupPage {
    id:             followPage
    pageComponent:  followPageComponent

    FactPanelController {
        id:         controller
    }

    Component {
        id: followPageComponent

        ColumnLayout {
            id:      flowLayout
            spacing: _margins

            property Fact _followEnabled:               controller.getParameterFact(-1, "FOLL_ENABLE")
            property bool _followParamsAvailable:       controller.parameterExists(-1, "FOLL_SYSID")
            property Fact _followDistanceMax:           controller.getParameterFact(-1, "FOLL_DIST_MAX", false /* reportMissing */)
            property Fact _followSysId:                 controller.getParameterFact(-1, "FOLL_SYSID", false /* reportMissing */)
            property Fact _followOffsetX:               controller.getParameterFact(-1, "FOLL_OFS_X", false /* reportMissing */)
            property Fact _followOffsetY:               controller.getParameterFact(-1, "FOLL_OFS_Y", false /* reportMissing */)
            property Fact _followOffsetZ:               controller.getParameterFact(-1, "FOLL_OFS_Z", false /* reportMissing */)
            property Fact _followOffsetType:            controller.getParameterFact(-1, "FOLL_OFS_TYPE", false /* reportMissing */)
            property Fact _followAltitudeType:          controller.getParameterFact(-1, "FOLL_ALT_TYPE", false /* reportMissing */)
            property Fact _followYawBehavior:           controller.getParameterFact(-1, "FOLL_YAW_BEHAVE", false /* reportMissing */)
            property int  _followComboMaintainIndex:    0
            property int  _followComboSpecifyIndex:     1
            property bool _followMaintain:              followPositionCombo.currentIndex === _followComboMaintainIndex
52
            property bool _supportedSetup:              true
53
            property bool _roverFirmware:               controller.roverFirmware
54 55
            property bool _showMainSetup:               _followEnabled.rawValue == 1 && _supportedSetup
            property bool _showOffsetsSetup:            _showMainSetup && !_followMaintain
DonLakeFlyer's avatar
DonLakeFlyer committed
56

57 58 59 60
            readonly property int _followYawBehaviorNone:           0
            readonly property int _followYawBehaviorFace:           1
            readonly property int _followYawBehaviorSame:           2
            readonly property int _followYawBehaviorFlight:         3
61
            readonly property int _followAltitudeTypeAbsolute:      0
62
            readonly property int _followAltitudeTypeRelative:      1
63
            readonly property int _followOffsetTypeRelative:        1
DonLakeFlyer's avatar
DonLakeFlyer committed
64 65 66

            Component.onCompleted: _setUIFromParams()

67 68 69 70 71 72 73 74 75 76 77 78 79 80
            function validateSupportedParamSetup() {
                var followSysIdOk = _followSysId.rawValue == QGroundControl.mavlinkSystemID
                var followOffsetOk = _followOffsetType.rawValue == _followOffsetTypeRelative
                var followAltOk = true
                var followYawOk = true
                if (!_roverFirmware) {
                    followAltOk = _followAltitudeType.rawValue == _followAltitudeTypeRelative
                    followYawOk = _followYawBehavior.rawValue == _followYawBehaviorNone || _followYawBehavior.rawValue == _followYawBehaviorFace || _followYawBehavior.rawValue == _followYawBehaviorFlight
                }
                _supportedSetup = followOffsetOk && followAltOk && followYawOk && followSysIdOk
                console.log("_supportedSetup", _supportedSetup, followSysIdOk, followOffsetOk, followAltOk, followYawOk)
                return _supportedSetup
            }

DonLakeFlyer's avatar
DonLakeFlyer committed
81
            function _setUIFromParams() {
82
                if (!_followParamsAvailable || !validateSupportedParamSetup()) {
DonLakeFlyer's avatar
DonLakeFlyer committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
                    return
                }

                if (_followOffsetX.rawValue == 0 && _followOffsetY.rawValue == 0 && _followOffsetZ.rawValue == 0) {
                    followPositionCombo.currentIndex =_followComboMaintainIndex
                    controller.distance.rawValue = 0
                    controller.angle.rawValue = 0
                    controller.height.rawValue = 0
                } else {
                    followPositionCombo.currentIndex =_followComboSpecifyIndex
                    var angleRadians = Math.atan2(_followOffsetX.rawValue, _followOffsetY.rawValue)
                    if (angleRadians == 0) {
                        controller.distance.rawValue = _followOffsetY.rawValue
                    } else {
                        controller.distance.rawValue = _followOffsetX.rawValue / Math.sin(angleRadians)
                    }
                    controller.angle.rawValue = _radiansToHeading(angleRadians)
                }
                controller.height.rawValue = -_followOffsetZ.rawValue
102
                if (!_roverFirmware) {
103 104 105 106 107 108 109 110 111
                    var comboIndex = -1
                    for (var i=0; i<pointVehicleCombo.rgValues.length; i++) {
                        if (pointVehicleCombo.rgValues[i] == _followYawBehavior.rawValue) {
                            comboIndex = i
                            break
                        }
                    }

                    pointVehicleCombo.currentIndex = comboIndex
112
                }
DonLakeFlyer's avatar
DonLakeFlyer committed
113 114 115
            }

            function _setFollowMeParamDefaults() {
116 117
                _followSysId.rawValue = QGroundControl.mavlinkSystemID
                _followOffsetType.rawValue = _followOffsetTypeRelative
118
                if (!_roverFirmware) {
119
                    _followAltitudeType.rawValue = _followAltitudeTypeRelative
120
                    _followYawBehavior.rawValue = _followYawBehaviorFace
121
                }
DonLakeFlyer's avatar
DonLakeFlyer committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

                controller.distance.value = controller.distance.defaultValue
                controller.angle.value = controller.angle.defaultValue
                controller.height.value = controller.height.defaultValue

                _setXYOffsetByAngleAndDistance(controller.angle.rawValue, controller.distance.rawValue)
                _followOffsetZ.rawValue = -controller.height.rawValue
                _setUIFromParams()
            }

            function _setXYOffsetByAngleAndDistance(headingAngleDegrees, distance) {
                if (distance == 0) {
                    _followOffsetX.rawValue = _followOffsetY.rawValue = 0
                } else {
                    var angleRadians = _headingToRadians(headingAngleDegrees)
                    if (angleRadians == 0) {
                        _followOffsetX.rawValue = 0
                        _followOffsetY.rawValue = distance
                    } else {
                        _followOffsetX.rawValue = Math.sin(angleRadians) * distance
                        _followOffsetY.rawValue = Math.cos(angleRadians) * distance
143
                        if (Math.abs(_followOffsetX.rawValue) < 0.0001) {
DonLakeFlyer's avatar
DonLakeFlyer committed
144 145
                            _followOffsetX.rawValue = 0
                        }
146
                        if (Math.abs(_followOffsetY.rawValue) < 0.0001) {
DonLakeFlyer's avatar
DonLakeFlyer committed
147 148 149 150 151 152 153
                            _followOffsetY.rawValue = 0
                        }
                    }
                }
            }

            function _radiansToHeading(radians) {
Remek Zajac's avatar
Remek Zajac committed
154
                var geometricAngle = QGroundControl.unitsConversion.radiansToDegrees(radians)
DonLakeFlyer's avatar
DonLakeFlyer committed
155 156 157 158 159 160 161 162 163 164 165
                var headingAngle = 90 - geometricAngle
                if (headingAngle < 0) {
                    headingAngle += 360
                } else if (headingAngle > 360) {
                    headingAngle -= 360
                }
                return headingAngle
            }

            function _headingToRadians(heading) {
                var geometricAngle = -(heading - 90)
Remek Zajac's avatar
Remek Zajac committed
166
                return QGroundControl.unitsConversion.degreesToRadians(geometricAngle)
DonLakeFlyer's avatar
DonLakeFlyer committed
167 168 169 170 171 172 173 174 175 176 177 178
            }

            APMFollowComponentController {
                id: controller

                onMissingParametersAvailable: {
                    _followDistanceMax =    controller.getParameterFact(-1, "FOLL_DIST_MAX")
                    _followSysId =          controller.getParameterFact(-1, "FOLL_SYSID")
                    _followOffsetX =        controller.getParameterFact(-1, "FOLL_OFS_X")
                    _followOffsetY =        controller.getParameterFact(-1, "FOLL_OFS_Y")
                    _followOffsetZ =        controller.getParameterFact(-1, "FOLL_OFS_Z")
                    _followOffsetType =     controller.getParameterFact(-1, "FOLL_OFS_TYPE")
179 180 181 182 183
                    if (!_roverFirmware) {
                        _followAltitudeType =   controller.getParameterFact(-1, "FOLL_ALT_TYPE")
                        _followYawBehavior =    controller.getParameterFact(-1, "FOLL_YAW_BEHAVE")
                    }

DonLakeFlyer's avatar
DonLakeFlyer committed
184 185
                    _followParamsAvailable = true
                    vehicleParamRefreshLabel.visible = false
186
                    validateSupportedParamSetup()
DonLakeFlyer's avatar
DonLakeFlyer committed
187 188 189 190 191 192 193
                }
            }

            QGCPalette { id: ggcPal; colorGroupEnabled: true }

            QGCCheckBox {
                text:       qsTr("Enable Follow Me")
194
                checked:    _followEnabled.rawValue == 1
DonLakeFlyer's avatar
DonLakeFlyer committed
195 196 197
                onClicked: {
                    if (checked) {
                        _followEnabled.rawValue = 1
198 199
                        var missingParameters = [ "FOLL_DIST_MAX", "FOLL_SYSID", "FOLL_OFS_X", "FOLL_OFS_Y", "FOLL_OFS_Z", "FOLL_OFS_TYPE"  ]
                        if (!_roverFirmware) {
200
                            missingParameters.push("FOLL_ALT_TYPE", "FOLL_YAW_BEHAVE")
201 202
                        }
                        controller.getMissingParameters(missingParameters)
DonLakeFlyer's avatar
DonLakeFlyer committed
203 204 205 206 207 208 209 210 211 212 213 214 215
                        vehicleParamRefreshLabel.visible = true
                    } else {
                        _followEnabled.rawValue = 0
                    }
                }
            }

            QGCLabel {
                id:         vehicleParamRefreshLabel
                text:       qsTr("Waiting for Vehicle to update")
                visible:    false
            }

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
            Column {
                width:      offsetSetupLayout.width
                spacing:    ScreenTools.defaultFontPixelWidth
                visible:    !_supportedSetup

                QGCLabel {
                    anchors.left:   parent.left
                    anchors.right:  parent.right
                    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.")
                    wrapMode:       Text.WordWrap
                    onWidthChanged: console.log('width', width)
                }

                QGCButton {
                    text:       qsTr("Reset To Supported Settings")
                    onClicked:  _setFollowMeParamDefaults()
                }
            }

DonLakeFlyer's avatar
DonLakeFlyer committed
235 236 237
            ColumnLayout {
                Layout.fillWidth:   true
                spacing:            ScreenTools.defaultFontPixelWidth
238
                visible:            _showMainSetup
DonLakeFlyer's avatar
DonLakeFlyer committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

                ColumnLayout {
                    Layout.fillWidth:   true
                    spacing:            ScreenTools.defaultFontPixelWidth

                    GridLayout {
                        Layout.fillWidth:   true
                        columns:            2

                        QGCLabel { text: qsTr("Vehicle Position") }
                        QGCComboBox {
                            id:                 followPositionCombo
                            Layout.fillWidth:   true
                            model:              [ qsTr("Maintain Current Offsets"), qsTr("Specify Offsets")]

                            onActivated: {
                                if (index == 0) {
                                    _followOffsetX.rawValue = _followOffsetY.rawValue = _followOffsetZ.rawValue = 0
                                    _setUIFromParams()
                                } else {
                                    _setFollowMeParamDefaults()
                                }
                            }
                        }

264 265 266 267
                        QGCLabel {
                            text:       qsTr("Point Vehicle")
                            visible:    !_roverFirmware
                        }
DonLakeFlyer's avatar
DonLakeFlyer committed
268 269 270
                        QGCComboBox {
                            id:                     pointVehicleCombo
                            Layout.fillWidth:       true
271
                            model:                  rgText
272
                            visible:                !_roverFirmware
273 274 275 276
                            onActivated:            _followYawBehavior.rawValue = rgValues[index]

                            property var rgText:    [ qsTr("Maintain current vehicle orientation"), qsTr("Point at ground station location"), qsTr("Same direction as ground station movement") ]
                            property var rgValues:  [ _followYawBehaviorNone, _followYawBehaviorFace, _followYawBehaviorFlight ]
DonLakeFlyer's avatar
DonLakeFlyer committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
                        }
                    }

                    GridLayout {
                        Layout.fillWidth:   true
                        columns:            4
                        visible:            !_followMaintain

                        QGCLabel {
                            Layout.columnSpan:  2
                            Layout.alignment:   Qt.AlignHCenter
                            text:               qsTr("Vehicle Offsets")
                        }

                        QGCLabel { text: qsTr("Angle") }
                        FactTextField {
                            fact:       controller.angle
                            onUpdated:  { console.log("updated"); _setXYOffsetByAngleAndDistance(controller.angle.rawValue, controller.distance.rawValue) }
                        }

                        QGCLabel { text: qsTr("Distance") }
                        FactTextField {
                            fact:       controller.distance
                            onUpdated:  _setXYOffsetByAngleAndDistance(controller.angle.rawValue, controller.distance.rawValue)
                        }

303 304 305 306 307
                        QGCLabel {
                            id:         heightLabel
                            text:       qsTr("Height")
                            visible:    !_roverFirmware && !_followMaintain
                        }
DonLakeFlyer's avatar
DonLakeFlyer committed
308 309
                        FactTextField {
                            fact:       controller.height
310
                            visible:    heightLabel.visible
DonLakeFlyer's avatar
DonLakeFlyer committed
311 312 313 314 315 316 317
                            onUpdated:  _followOffsetZ.rawValue = -controller.height.rawValue
                        }
                    }
                }
            }

            RowLayout {
318 319 320
                id:         offsetSetupLayout
                spacing:    ScreenTools.defaultFontPixelWidth * 2
                visible:    _showOffsetsSetup
DonLakeFlyer's avatar
DonLakeFlyer committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

                Item {
                    height: ScreenTools.defaultFontPixelWidth * 50
                    width:  height

                    Rectangle {
                        anchors.top:                parent.top
                        anchors.bottom:             parent.bottom
                        anchors.horizontalCenter:   parent.horizontalCenter
                        width:                      3
                        color:                      qgcPal.windowShade
                    }

                    Rectangle {
                        anchors.left:           parent.left
                        anchors.right:          parent.right
                        anchors.verticalCenter: parent.verticalCenter
                        height:                 3
                        color:                  qgcPal.windowShade
                    }

                    QGCLabel {
                        anchors.horizontalCenter:   parent.horizontalCenter
                        anchors.topMargin:          parent.height / 4
                        anchors.top:                parent.top
                        text:                       qsTr("Click in the graphic to change angle")
                        opacity:                    0.5
                    }

                    Image {
                        id:                 gcsIcon
                        anchors.centerIn:   parent
                        source:             "/res/QGCLogoArrow"
                        mipmap:             true
                        antialiasing:       true
                        fillMode:           Image.PreserveAspectFit
                        height:             ScreenTools.defaultFontPixelHeight * 2.5
                        sourceSize.height:  height
                    }

                    Item {
                        id:             vehicleHolder
                        anchors.fill:   parent

                        transform: Rotation {
                            origin.x:       vehicleHolder.width  / 2
                            origin.y:       vehicleHolder.height / 2
                            angle:          controller.angle.rawValue
                        }

                        Image {
                            id:                 vehicleIcon
                            anchors.top:        parent.top
                            anchors.horizontalCenter: parent.horizontalCenter
                            source:             controller.vehicle.vehicleImageOpaque
                            mipmap:             true
                            height:             ScreenTools.defaultFontPixelHeight * 2.5
                            sourceSize.height:  height
                            fillMode:           Image.PreserveAspectFit

                            transform: Rotation {
                                origin.x:       vehicleIcon.width  / 2
                                origin.y:       vehicleIcon.height / 2
384 385 386 387 388 389
                                angle:          _roverFirmware ? 0 :
                                                                 (_followYawBehavior.rawValue == _followYawBehaviorNone ?
                                                                      0 :
                                                                      (_followYawBehavior.rawValue == _followYawBehaviorFace ?
                                                                           180 :
                                                                           -controller.angle.rawValue))
DonLakeFlyer's avatar
DonLakeFlyer committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
                            }
                        }

                        Rectangle {
                            id:         distanceLine
                            x:          parent.width / 2
                            y:          vehicleIcon.height
                            height:     (parent.height / 2) - (vehicleIcon.height + (gcsIcon.height / 2))
                            width:      2
                            color:      qgcPal.text
                            opacity:    0.4

                            Rectangle {
                                anchors.top:                parent.top
                                anchors.horizontalCenter:   parent.horizontalCenter
                                width:                      ScreenTools.defaultFontPixelWidth * 2
                                height:                     2
                                color:                      qgcPal.text
                            }

                            Rectangle {
                                anchors.bottom:             parent.bottom
                                anchors.horizontalCenter:   parent.horizontalCenter
                                width:                      ScreenTools.defaultFontPixelWidth * 2
                                height:                     2
                                color:                      qgcPal.text
                            }
                        }

                        QGCLabel {
                            id:                 distanceLabel
                            anchors.centerIn:   distanceLine
Remek Zajac's avatar
Remek Zajac committed
422
                            text:               controller.distance.valueString + " " + QGroundControl.unitsConversion.appSettingsHorizontalDistanceUnitsString
DonLakeFlyer's avatar
DonLakeFlyer committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

                            transform: Rotation {
                                origin.x:       distanceLabel.width  / 2
                                origin.y:       distanceLabel.height / 2
                                angle:          -controller.angle.rawValue
                            }
                        }
                    }

                    MouseArea {
                        anchors.fill: parent

                        onClicked: {
                            // Translate x,y to centered
                            var x = mouse.x - (width / 2)
                            var y = (height - mouse.y) - (height / 2)
                            controller.angle.rawValue = _radiansToHeading(Math.atan2(y, x))
440
                            _setXYOffsetByAngleAndDistance(controller.angle.rawValue, controller.distance.rawValue)
DonLakeFlyer's avatar
DonLakeFlyer committed
441 442 443 444 445 446 447
                        }
                    }
                }

                ColumnLayout {
                    Layout.fillHeight:  true
                    spacing:            0
448
                    visible:            !_roverFirmware
DonLakeFlyer's avatar
DonLakeFlyer committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468

                    Image {
                        id:                 vehicleIconHeight
                        source:             controller.vehicle.vehicleImageOpaque
                        mipmap:             true
                        height:             ScreenTools.defaultFontPixelHeight * 2.5
                        sourceSize.height:  height
                        fillMode:           Image.PreserveAspectFit

                        transform: Rotation {
                            origin.x:       vehicleIconHeight.width  / 2
                            origin.y:       vehicleIconHeight.height / 2
                            angle:          65
                            axis { x: 1; y: 0; z: 0 }
                        }
                    }

                    Item {
                        Layout.alignment:   Qt.AlignHCenter
                        Layout.fillHeight:  true
469
                        width:              Math.max(ScreenTools.defaultFontPixelWidth * 2, heightValueLabel.width)
DonLakeFlyer's avatar
DonLakeFlyer committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497

                        Rectangle {
                            id:                         heightLine
                            anchors.top:                parent.top
                            anchors.bottom:             parent.bottom
                            anchors.horizontalCenter:   parent.horizontalCenter
                            width:                      2
                            color:                      qgcPal.text
                            opacity:                    0.4

                            Rectangle {
                                anchors.top:                parent.top
                                anchors.horizontalCenter:   parent.horizontalCenter
                                width:                      ScreenTools.defaultFontPixelWidth * 2
                                height:                     2
                                color:                      qgcPal.text
                            }

                            Rectangle {
                                anchors.bottom:             parent.bottom
                                anchors.horizontalCenter:   parent.horizontalCenter
                                width:                      ScreenTools.defaultFontPixelWidth * 2
                                height:                     2
                                color:                      qgcPal.text
                            }
                        }

                        QGCLabel {
498
                            id:                 heightValueLabel
DonLakeFlyer's avatar
DonLakeFlyer committed
499
                            anchors.centerIn:   parent
Remek Zajac's avatar
Remek Zajac committed
500
                            text:               controller.height.valueString + " " + QGroundControl.unitsConversion.appSettingsHorizontalDistanceUnitsString
DonLakeFlyer's avatar
DonLakeFlyer committed
501 502 503
                        }
                    }

504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
                    MissionItemIndexLabel {
                        id:                 launchIconHeight
                        Layout.alignment:   Qt.AlignHCenter
                        label:              qsTr("L")

                        transform: [
                            Scale {
                                origin.x:       launchIconHeight.width  / 2
                                origin.y:       launchIconHeight.height / 2
                                xScale:         1.5
                                yScale:         2.5

                            },
                            Rotation {
                                origin.x:       launchIconHeight.width  / 2
                                origin.y:       launchIconHeight.height / 2
                                angle:          75
                                axis { x: 1; y: 0; z: 0 }
                            } ]
DonLakeFlyer's avatar
DonLakeFlyer committed
523 524 525 526 527 528
                    }
                }
            }
        }
    }
}