JoystickConfigController.h 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27


/// @file
///     @brief Radio Config Qml Controller
///     @author Don Gagne <don@thegagnes.com

#ifndef JoystickConfigController_H
#define JoystickConfigController_H

#include <QTimer>

#include "FactPanelController.h"
#include "QGCLoggingCategory.h"
#include "Joystick.h"

Q_DECLARE_LOGGING_CATEGORY(JoystickConfigControllerLog)

class RadioConfigest;
28
class JoystickManager;
29 30 31 32 33 34 35 36 37 38

namespace Ui {
    class JoystickConfigController;
}


class JoystickConfigController : public FactPanelController
{
    Q_OBJECT
    
39
    //friend class RadioConfigTest; ///< This allows our unit test to access internal information needed.
40 41 42 43 44 45 46 47 48 49
    
public:
    JoystickConfigController(void);
    ~JoystickConfigController();
    
    Q_PROPERTY(QQuickItem* statusText   MEMBER _statusText)
    Q_PROPERTY(QQuickItem* cancelButton MEMBER _cancelButton)
    Q_PROPERTY(QQuickItem* nextButton   MEMBER _nextButton)
    Q_PROPERTY(QQuickItem* skipButton   MEMBER _skipButton)
    
Gus Grubba's avatar
Gus Grubba committed
50 51 52 53
    Q_PROPERTY(bool rollAxisMapped              READ rollAxisMapped             NOTIFY rollAxisMappedChanged)
    Q_PROPERTY(bool pitchAxisMapped             READ pitchAxisMapped            NOTIFY pitchAxisMappedChanged)
    Q_PROPERTY(bool yawAxisMapped               READ yawAxisMapped              NOTIFY yawAxisMappedChanged)
    Q_PROPERTY(bool throttleAxisMapped          READ throttleAxisMapped         NOTIFY throttleAxisMappedChanged)
Gregory Dymarek's avatar
Gregory Dymarek committed
54

Gus Grubba's avatar
Gus Grubba committed
55 56 57 58 59 60 61 62 63 64 65 66
    Q_PROPERTY(bool gimbalPitchAxisMapped       READ gimbalPitchAxisMapped      NOTIFY gimbalPitchAxisMappedChanged)
    Q_PROPERTY(bool gimbalYawAxisMapped         READ gimbalYawAxisMapped        NOTIFY gimbalYawAxisMappedChanged)

    Q_PROPERTY(int  rollAxisReversed            READ rollAxisReversed           NOTIFY rollAxisReversedChanged)
    Q_PROPERTY(int  pitchAxisReversed           READ pitchAxisReversed          NOTIFY pitchAxisReversedChanged)
    Q_PROPERTY(int  yawAxisReversed             READ yawAxisReversed            NOTIFY yawAxisReversedChanged)
    Q_PROPERTY(int  throttleAxisReversed        READ throttleAxisReversed       NOTIFY throttleAxisReversedChanged)

    Q_PROPERTY(int  gimbalPitchAxisReversed     READ gimbalPitchAxisReversed    NOTIFY gimbalPitchAxisReversedChanged)
    Q_PROPERTY(int  gimbalYawAxisReversed       READ gimbalYawAxisReversed      NOTIFY gimbalYawAxisReversedChanged)

    Q_PROPERTY(bool deadbandToggle              READ getDeadbandToggle          WRITE setDeadbandToggle    NOTIFY deadbandToggled)
67

68
    Q_PROPERTY(int transmitterMode READ transmitterMode WRITE setTransmitterMode NOTIFY transmitterModeChanged)
69
    Q_PROPERTY(QString imageHelp MEMBER _imageHelp NOTIFY imageHelpChanged)
70
    Q_PROPERTY(bool calibrating READ calibrating NOTIFY calibratingChanged)
71
    
Gus Grubba's avatar
Gus Grubba committed
72 73 74 75 76
    Q_INVOKABLE void cancelButtonClicked    ();
    Q_INVOKABLE void skipButtonClicked      ();
    Q_INVOKABLE void nextButtonClicked      ();
    Q_INVOKABLE void start                  ();
    Q_INVOKABLE void setDeadbandValue       (int axis, int value);
Gregory Dymarek's avatar
Gregory Dymarek committed
77

Gus Grubba's avatar
Gus Grubba committed
78 79 80 81 82 83
    bool rollAxisMapped                     () { return _rgFunctionAxisMapping[Joystick::rollFunction]          != _axisNoAxis; }
    bool pitchAxisMapped                    () { return _rgFunctionAxisMapping[Joystick::pitchFunction]         != _axisNoAxis; }
    bool yawAxisMapped                      () { return _rgFunctionAxisMapping[Joystick::yawFunction]           != _axisNoAxis; }
    bool throttleAxisMapped                 () { return _rgFunctionAxisMapping[Joystick::throttleFunction]      != _axisNoAxis; }
    bool gimbalPitchAxisMapped              () { return _rgFunctionAxisMapping[Joystick::gimbalPitchFunction]   != _axisNoAxis; }
    bool gimbalYawAxisMapped                () { return _rgFunctionAxisMapping[Joystick::gimbalYawFunction]     != _axisNoAxis; }
84

Gus Grubba's avatar
Gus Grubba committed
85 86 87 88 89 90
    bool rollAxisReversed                   ();
    bool pitchAxisReversed                  ();
    bool yawAxisReversed                    ();
    bool throttleAxisReversed               ();
    bool gimbalPitchAxisReversed            ();
    bool gimbalYawAxisReversed              ();
91

Gus Grubba's avatar
Gus Grubba committed
92 93
    bool getDeadbandToggle                  ();
    void setDeadbandToggle                  (bool);
94

Gus Grubba's avatar
Gus Grubba committed
95 96 97 98 99 100
    int  axisCount                          () { return _axisCount; }

    int  transmitterMode                    () { return _transmitterMode; }
    void setTransmitterMode                 (int mode);

    bool calibrating                        () { return _currentStep != -1; }
101 102
    
signals:
Gus Grubba's avatar
Gus Grubba committed
103 104
    void axisValueChanged                   (int axis, int value);
    void axisDeadbandChanged                (int axis, int value);
Gregory Dymarek's avatar
Gregory Dymarek committed
105

Gus Grubba's avatar
Gus Grubba committed
106 107 108 109 110 111
    void rollAxisMappedChanged              (bool mapped);
    void pitchAxisMappedChanged             (bool mapped);
    void yawAxisMappedChanged               (bool mapped);
    void throttleAxisMappedChanged          (bool mapped);
    void gimbalPitchAxisMappedChanged       (bool mapped);
    void gimbalYawAxisMappedChanged         (bool mapped);
Gregory Dymarek's avatar
Gregory Dymarek committed
112

Gus Grubba's avatar
Gus Grubba committed
113 114 115 116 117 118
    void rollAxisReversedChanged            (bool reversed);
    void pitchAxisReversedChanged           (bool reversed);
    void yawAxisReversedChanged             (bool reversed);
    void throttleAxisReversedChanged        (bool reversed);
    void gimbalPitchAxisReversedChanged     (bool reversed);
    void gimbalYawAxisReversedChanged       (bool reversed);
119

Gus Grubba's avatar
Gus Grubba committed
120
    void deadbandToggled                    (bool value);
121
    
Gus Grubba's avatar
Gus Grubba committed
122 123 124
    void imageHelpChanged                   (QString source);
    void transmitterModeChanged             (int mode);
    void calibratingChanged                 ();
125 126
    
    // @brief Signalled when in unit test mode and a message box should be displayed by the next button
Gus Grubba's avatar
Gus Grubba committed
127
    void nextButtonMessageBoxDisplayed      ();
128 129

private slots:
130
    void _activeJoystickChanged(Joystick* joystick);
131
    void _axisValueChanged(int axis, int value);
Gregory Dymarek's avatar
Gregory Dymarek committed
132
    void _axisDeadbandChanged(int axis, int value);
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
   
private:
    /// @brief The states of the calibration state machine.
    enum calStates {
        calStateAxisWait,
        calStateBegin,
        calStateIdentify,
        calStateMinMax,
        calStateCenterThrottle,
        calStateDetectInversion,
        calStateTrims,
        calStateSave
    };
    
    typedef void (JoystickConfigController::*inputFn)(Joystick::AxisFunction_t function, int axis, int value);
    typedef void (JoystickConfigController::*buttonFn)(void);
    struct stateMachineEntry {
        Joystick::AxisFunction_t    function;
        const char*                 instructions;
        const char*                 image;
        inputFn                     rcInputFn;
        buttonFn                    nextFn;
        buttonFn                    skipFn;
Gus Grubba's avatar
Gus Grubba committed
156
        int                         channelID;
157 158 159 160 161 162 163 164 165
    };
    
    /// @brief A set of information associated with a radio axis.
    struct AxisInfo {
        Joystick::AxisFunction_t    function;   ///< Function mapped to this axis, Joystick::maxFunction for none
        bool                        reversed;   ///< true: axis is reverse, false: not reversed
        int                         axisMin;    ///< Minimum axis value
        int                         axisMax;    ///< Maximum axis value
        int                         axisTrim;   ///< Trim position
166
        int                         deadband;   ///< Deadband
167 168
    };
    
Gus Grubba's avatar
Gus Grubba committed
169
    Joystick* _activeJoystick = nullptr;
170
    
Gus Grubba's avatar
Gus Grubba committed
171 172
    int _transmitterMode    = 2;
    int _currentStep        = -1;  ///< Current step of state machine
173 174 175
    
    const struct stateMachineEntry* _getStateMachineEntry(int step);
    
Gus Grubba's avatar
Gus Grubba committed
176 177
    void _advanceState          ();
    void _setupCurrentState     ();
178
    
Gus Grubba's avatar
Gus Grubba committed
179
    bool _validAxis             (int axis);
180

181 182 183 184 185
    void _inputCenterWaitBegin  (Joystick::AxisFunction_t function, int axis, int value);
    void _inputStickDetect      (Joystick::AxisFunction_t function, int axis, int value);
    void _inputStickMin         (Joystick::AxisFunction_t function, int axis, int value);
    void _inputCenterWait       (Joystick::AxisFunction_t function, int axis, int value);
    
Gus Grubba's avatar
Gus Grubba committed
186
    void _switchDetect          (Joystick::AxisFunction_t function, int axis, int value, bool moveToNextStep);
187
    
Gus Grubba's avatar
Gus Grubba committed
188 189 190
    void _saveFlapsDown         ();
    void _skipFlaps             ();
    void _saveAllTrims          ();
191
    
Gus Grubba's avatar
Gus Grubba committed
192
    bool _stickSettleComplete   (int axis, int value);
193
    
Gus Grubba's avatar
Gus Grubba committed
194 195 196 197
    void _validateCalibration   ();
    void _writeCalibration      ();
    void _resetInternalCalibrationValues();
    void _setInternalCalibrationValuesFromSettings();
198
    
Gus Grubba's avatar
Gus Grubba committed
199 200 201
    void _startCalibration      ();
    void _stopCalibration       ();
    void _calSave               ();
202

Gus Grubba's avatar
Gus Grubba committed
203
    void _calSaveCurrentValues  ();
204 205 206
    
    void _setHelpImage(const char* imageFile);
    
Gus Grubba's avatar
Gus Grubba committed
207
    void _signalAllAttitudeValueChanges();
208 209 210
    
    // Member variables

211
    static const char* _imageFileMode1Dir;
212
    static const char* _imageFileMode2Dir;
213 214
    static const char* _imageFileMode3Dir;
    static const char* _imageFileMode4Dir;
215 216 217 218 219 220 221 222 223 224 225 226 227
    static const char* _imageFilePrefix;
    static const char* _imageCenter;
    static const char* _imageThrottleUp;
    static const char* _imageThrottleDown;
    static const char* _imageYawLeft;
    static const char* _imageYawRight;
    static const char* _imageRollLeft;
    static const char* _imageRollRight;
    static const char* _imagePitchUp;
    static const char* _imagePitchDown;
    
    int _rgFunctionAxisMapping[Joystick::maxFunction]; ///< Maps from joystick function to axis index. _axisMax indicates axis not set for this function.

Gus Grubba's avatar
Gus Grubba committed
228 229 230 231 232 233 234 235
    static const int _attitudeControls  = 5;
    
    int                 _axisCount      = 0;        ///< Number of actual joystick axes available
    static const int    _axisNoAxis     = -1;       ///< Signals no axis set
    static const int    _axisMinimum    = 4;        ///< Minimum numner of joystick axes required to run PX4
    struct AxisInfo*    _rgAxisInfo     = nullptr;  ///< Information associated with each axis
    int*                _axisValueSave  = nullptr;  ///< Saved values prior to detecting axis movement
    int*                _axisRawValue   = nullptr;  ///< Current set of raw axis values
236

Gus Grubba's avatar
Gus Grubba committed
237 238 239 240 241 242
    enum calStates _calState = calStateAxisWait;    ///< Current calibration state

    int     _calStateCurrentAxis;                   ///< Current axis being worked on in calStateIdentify and calStateDetectInversion
    bool    _calStateAxisComplete;                  ///< Work associated with current axis is complete
    int     _calStateIdentifyOldMapping;            ///< Previous mapping for axis being currently identified
    int     _calStateReverseOldMapping;             ///< Previous mapping for axis being currently used to detect inversion
243 244 245 246 247 248 249 250 251
    
    static const int _calCenterPoint;
    static const int _calValidMinValue;
    static const int _calValidMaxValue;
    static const int _calDefaultMinValue;
    static const int _calDefaultMaxValue;
    static const int _calRoughCenterDelta;
    static const int _calMoveDelta;
    static const int _calSettleDelta;
252
    static const int _calMinDelta;    
253 254 255 256 257 258 259
    
    int     _stickDetectAxis;
    int     _stickDetectInitialValue;
    int     _stickDetectValue;
    bool    _stickDetectSettleStarted;
    QTime   _stickDetectSettleElapsed;
    static const int _stickDetectSettleMSecs;
Gus Grubba's avatar
Gus Grubba committed
260 261 262 263 264

    QQuickItem* _statusText     = nullptr;
    QQuickItem* _cancelButton   = nullptr;
    QQuickItem* _nextButton     = nullptr;
    QQuickItem* _skipButton     = nullptr;
265 266
    
    QString _imageHelp;
267 268

    JoystickManager*    _joystickManager;
269 270 271
};

#endif // JoystickConfigController_H