MultiRotorMotorDisplay.qml 8.69 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import QtQuick 2.2

import QGroundControl.Palette       1.0
import QGroundControl.ScreenTools   1.0

Item {
    id: motorRoot

    property int    motorCount: 4       // Number of motors on vehicle
    property bool   xConfig:    true    // true: X configuration, false: Plus configuration
    property bool   coaxial:    true    // true: motors on top bottom of same arm, false: motors only on top of arm

    property bool   _unsupportedConfig: motorCount == 3 || (motorCount == 6 && coaxial) // Tricopters NYI
    property var    _qgcPal:            QGCPalette { colorGroupEnabled: enabled }
    property real   _rotorRadius:       motorRoot.height / 8
    property int    _motorsPerSide:     motorCount / (coaxial ? 2 : 1)

dogmaphobic's avatar
dogmaphobic committed
18 19
    readonly property string    _cwColor:               "#15ce15"   // Green
    readonly property string    _ccwColor:              "#1283e0"   // Blue
Don Gagne's avatar
Don Gagne committed
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    readonly property var       _motorColors4Plus:      [ _ccwColor, _cwColor ]
    readonly property var       _motorColors4X:         [ _cwColor, _ccwColor ]
    readonly property var       _motorColors6:          [ _cwColor, _ccwColor ]
    readonly property var       _motorColors8:          [ _cwColor, _ccwColor ]
    readonly property var       _motorColors4Top:       [ _cwColor, _ccwColor ]
    readonly property var       _motorColors4Bottom:    [ _ccwColor, _cwColor ]

    readonly property var       _motorNumbers4Plus:     [ 1, 4, 2, 3 ]
    readonly property var       _motorNumbers4X:        [ 4, 2, 3, 1 ]
    readonly property var       _motorNumbers6Plus:     [ 6, 2, 3, 5, 1, 4 ]
    readonly property var       _motorNumbers6X:        [ 1, 4, 6, 2, 3, 5 ]
    readonly property var       _motorNumbers8:         [ 8, 4, 2, 6, 7, 5, 1, 3 ]
    readonly property var       _motorNumbers4Top:      [ 4, 3, 2, 1 ]
    readonly property var       _motorNumbers4Bottom:   [ 7, 8, 5, 6 ]

    Component.onCompleted: {
        if (coaxial) {
            topMotors.motorNumbers = _motorNumbers4Top
            bottomMotors.motorNumbers = _motorNumbers4Bottom
        } else {
            switch (motorCount) {
            case 4:
                topMotors.motorNumbers = xConfig ? _motorNumbers4X : _motorNumbers4Plus
                topMotors.motorColors = xConfig ? _motorColors4X : _motorColors4Plus
                break
            case 6:
                topMotors.motorNumbers = xConfig ? _motorNumbers6X : _motorNumbers6Plus
                topMotors.motorColors = _motorColors6
                break
            default:
            case 8:
                topMotors.motorNumbers = _motorNumbers8
                topMotors.motorColors = _motorColors8
                break
            }
            bottomMotors.motorNumbers = _motorNumbers8
        }
    }

    Component {
        id: motorDisplayComponent

        Repeater {
            id:     motorRepeater
            model: _motorsPerSide

            Item {
                x:      motorRepeater.width / 2 + _armXCenter - rotor.radius
                y:      motorRepeater.height / 2 + _armYCenter - rotor.radius
                width:  _rotorRadius * 2
                height: _rotorRadius * 2

                property real _armOffsetRadians:        ((2 * Math.PI) / _motorsPerSide)
                property real _armOffsetIndexRadians:   (_armOffsetRadians  * index) + ((xConfig && _motorsPerSide != 6) || (!xConfig && _motorsPerSide == 6) ? _armOffsetRadians / 2 : 0)
                property real _armLength:               (motorRepeater.height / 2) - (_rotorRadius * (xConfig && _motorsPerSide == 4 ? 0 : 1))
                property real _armXCenter:              Math.cos(_armOffsetIndexRadians) * _armLength // adjacent = cos * hypotenuse
                property real _armYCenter:              Math.sin(_armOffsetIndexRadians) * _armLength // opposite = sin * hypotenuse

dogmaphobic's avatar
dogmaphobic committed
78 79
                Rectangle {
                    id:             rotor
Don Gagne's avatar
Don Gagne committed
80 81 82 83 84
                    anchors.fill:   parent
                    radius:         _rotorRadius
                    color:          motorColors[index & 1]
                    opacity:        topCoaxial ? 0.65 : 1.0
                    border.color:   topCoaxial ? "black" : color
dogmaphobic's avatar
dogmaphobic committed
85
                    antialiasing:   true
Don Gagne's avatar
Don Gagne committed
86
                    readonly property bool topCoaxial: topMotors && coaxial
dogmaphobic's avatar
dogmaphobic committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
                    //-- Top Directional Arrow
                    QGCColoredImage {
                        color:                      _qgcPal.globalTheme === QGCPalette.Light ? "black" : "white"
                        height:                     parent.height * 0.2
                        width:                      height
                        sourceSize.height:          height
                        mipmap:                     true
                        fillMode:                   Image.PreserveAspectFit
                        source:                     (index & 1) ? "/qmlimages/ArrowCW.svg" : "/qmlimages/ArrowCCW.svg"
                        anchors.top:                parent.top
                        anchors.topMargin:          height * -0.5
                        anchors.horizontalCenter:   parent.horizontalCenter
                    }
                    //-- Bottom Directional Arrow
                    QGCColoredImage {
                        color:                      _qgcPal.globalTheme === QGCPalette.Light ? "black" : "white"
                        height:                     parent.height * 0.2
                        width:                      height
                        sourceSize.height:          height
                        mipmap:                     true
                        fillMode:                   Image.PreserveAspectFit
                        source:                     (index & 1) ? "/qmlimages/ArrowCCW.svg" : "/qmlimages/ArrowCW.svg"
                        anchors.bottom:             parent.bottom
                        anchors.bottomMargin:       height * -0.5
                        anchors.horizontalCenter:   parent.horizontalCenter
                    }
                    transform: [
                        Rotation {
                            origin.x:           rotor.width  / 2
                            origin.y:           rotor.height / 2
                            angle:              (index & 1) ? 45 : -45
                        }]
Don Gagne's avatar
Don Gagne committed
119 120 121 122 123
                }

                Rectangle {
                    anchors.horizontalCenter:   parent.horizontalCenter
                    anchors.verticalCenter:     parent.verticalCenter
dogmaphobic's avatar
dogmaphobic committed
124 125 126 127 128 129
                    width:                      radius * 2
                    height:                     radius * 2
                    radius:                     ScreenTools.defaultFontPixelHeight * 0.666
                    color:                      Qt.rgba(1,1,1,1)
                    border.color:               Qt.rgba(0,0,0,0.75)
                    antialiasing:               true
Don Gagne's avatar
Don Gagne committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

                    QGCLabel {
                        anchors.fill:               parent
                        verticalAlignment:          Text.AlignVCenter
                        horizontalAlignment:        Text.AlignHCenter
                        text:                       motorNumbers[index]
                        color:                      parent.border.color
                    }
                }
            } // Item
        } // Repeater
    } // Component - MotorDisplayComponent

    Item {
        anchors.fill:   parent
        visible:        !_unsupportedConfig

        Loader {
            id:                 bottomMotors
            anchors.topMargin:  ScreenTools.defaultFontPixelHeight
            anchors.fill:       parent
            sourceComponent:    motorDisplayComponent
            visible:            coaxial

            property bool   topMotors:      false
            property var    motorNumbers:   _motorNumbers8
            property var    motorColors:    _motorColors4Bottom
        }

        Loader {
            id:                     topMotors
            anchors.fill:           parent
            anchors.bottomMargin:   coaxial ? ScreenTools.defaultFontPixelHeight : 0
            sourceComponent:        motorDisplayComponent

            property bool   topMotors:      true
            property var    motorNumbers:   _motorNumbers8
            property var    motorColors:    _motorColors4Top
        }

dogmaphobic's avatar
dogmaphobic committed
170 171 172 173 174 175 176 177 178
        QGCColoredImage {
            color:                      _qgcPal.text
            height:                     parent.height * 0.5
            width:                      height * 0.55
            sourceSize.height:          height
            mipmap:                     true
            fillMode:                   Image.PreserveAspectFit
            source:                     "/qmlimages/ArrowDirection.svg"
            anchors.centerIn:           parent
Don Gagne's avatar
Don Gagne committed
179
        }
dogmaphobic's avatar
dogmaphobic committed
180

Don Gagne's avatar
Don Gagne committed
181 182
    } // Item
} // Item