Joystick.h 13.3 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

Gus Grubba's avatar
Gus Grubba committed
10 11
/// @file
/// @brief  Joystick Controller
12

Gus Grubba's avatar
Gus Grubba committed
13
#pragma once
14 15 16 17 18 19

#include <QObject>
#include <QThread>

#include "QGCLoggingCategory.h"
#include "Vehicle.h"
20
#include "MultiVehicleManager.h"
21 22

Q_DECLARE_LOGGING_CATEGORY(JoystickLog)
23
Q_DECLARE_LOGGING_CATEGORY(JoystickValuesLog)
24

Gus Grubba's avatar
Gus Grubba committed
25
/// Action assigned to button
Gus Grubba's avatar
Gus Grubba committed
26
class AssignedButtonAction : public QObject {
Gus Grubba's avatar
Gus Grubba committed
27 28
    Q_OBJECT
public:
Gus Grubba's avatar
Gus Grubba committed
29
    AssignedButtonAction(QObject* parent, const QString name);
Gus Grubba's avatar
Gus Grubba committed
30
    QString action;
31
    QElapsedTimer buttonTime;
Gus Grubba's avatar
Gus Grubba committed
32
    bool    repeat = false;
Gus Grubba's avatar
Gus Grubba committed
33 34
};

Gus Grubba's avatar
Gus Grubba committed
35
/// Assignable Button Action
Gus Grubba's avatar
Gus Grubba committed
36
class AssignableButtonAction : public QObject {
Gus Grubba's avatar
Gus Grubba committed
37 38
    Q_OBJECT
public:
Gus Grubba's avatar
Gus Grubba committed
39
    AssignableButtonAction(QObject* parent, QString action_, bool canRepeat_ = false);
Gus Grubba's avatar
Gus Grubba committed
40 41
    Q_PROPERTY(QString  action      READ action     CONSTANT)
    Q_PROPERTY(bool     canRepeat   READ canRepeat  CONSTANT)
Gus Grubba's avatar
Gus Grubba committed
42 43
    QString action      () { return _action; }
    bool    canRepeat   () { return _repeat; }
Gus Grubba's avatar
Gus Grubba committed
44 45 46 47 48
private:
    QString _action;
    bool    _repeat = false;
};

Gus Grubba's avatar
Gus Grubba committed
49
/// Joystick Controller
50 51 52 53
class Joystick : public QThread
{
    Q_OBJECT
public:
54 55
    Joystick(const QString& name, int axisCount, int buttonCount, int hatCount, MultiVehicleManager* multiVehicleManager);

56
    ~Joystick();
57

58
    typedef struct Calibration_t {
59 60 61
        int     min;
        int     max;
        int     center;
62
        int     deadband;
63
        bool    reversed;
64 65 66 67 68 69
        Calibration_t()
            : min(-32767)
            , max(32767)
            , center(0)
            , deadband(0)
            , reversed(false) {}
70
    } Calibration_t;
71

72 73 74 75 76
    typedef enum {
        rollFunction,
        pitchFunction,
        yawFunction,
        throttleFunction,
Gus Grubba's avatar
Gus Grubba committed
77 78
        gimbalPitchFunction,
        gimbalYawFunction,
79 80
        maxFunction
    } AxisFunction_t;
81

82 83 84 85 86
    typedef enum {
        ThrottleModeCenterZero,
        ThrottleModeDownZero,
        ThrottleModeMax
    } ThrottleMode_t;
87

Gus Grubba's avatar
Gus Grubba committed
88 89 90 91 92
    Q_PROPERTY(QString  name                    READ name                   CONSTANT)
    Q_PROPERTY(bool     calibrated              MEMBER _calibrated          NOTIFY calibratedChanged)
    Q_PROPERTY(int      totalButtonCount        READ totalButtonCount       CONSTANT)
    Q_PROPERTY(int      axisCount               READ axisCount              CONSTANT)
    Q_PROPERTY(bool     requiresCalibration     READ requiresCalibration    CONSTANT)
Gus Grubba's avatar
Gus Grubba committed
93

Gus Grubba's avatar
Gus Grubba committed
94 95
    //-- Actions assigned to buttons
    Q_PROPERTY(QStringList buttonActions        READ buttonActions          NOTIFY buttonActionsChanged)
Gus Grubba's avatar
Gus Grubba committed
96

Gus Grubba's avatar
Gus Grubba committed
97
    //-- Actions that can be assigned to buttons
98 99
    Q_PROPERTY(QmlObjectListModel* assignableActions    READ assignableActions          NOTIFY      assignableActionsChanged)
    Q_PROPERTY(QStringList assignableActionTitles       READ assignableActionTitles     NOTIFY      assignableActionsChanged)
Gus Grubba's avatar
Gus Grubba committed
100 101 102 103
    Q_PROPERTY(QString  disabledActionName              READ disabledActionName         CONSTANT)

    Q_PROPERTY(bool     gimbalEnabled           READ gimbalEnabled          WRITE setGimbalEnabled      NOTIFY gimbalEnabledChanged)
    Q_PROPERTY(int      throttleMode            READ throttleMode           WRITE setThrottleMode       NOTIFY throttleModeChanged)
Gus Grubba's avatar
Gus Grubba committed
104 105
    Q_PROPERTY(float    axisFrequency           READ axisFrequency          WRITE setAxisFrequency      NOTIFY axisFrequencyChanged)
    Q_PROPERTY(float    buttonFrequency         READ buttonFrequency        WRITE setButtonFrequency    NOTIFY buttonFrequencyChanged)
Gus Grubba's avatar
Gus Grubba committed
106 107 108 109 110 111
    Q_PROPERTY(bool     negativeThrust          READ negativeThrust         WRITE setNegativeThrust     NOTIFY negativeThrustChanged)
    Q_PROPERTY(float    exponential             READ exponential            WRITE setExponential        NOTIFY exponentialChanged)
    Q_PROPERTY(bool     accumulator             READ accumulator            WRITE setAccumulator        NOTIFY accumulatorChanged)
    Q_PROPERTY(bool     circleCorrection        READ circleCorrection       WRITE setCircleCorrection   NOTIFY circleCorrectionChanged)

    Q_INVOKABLE void    setButtonRepeat     (int button, bool repeat);
Gus Grubba's avatar
Gus Grubba committed
112
    Q_INVOKABLE bool    getButtonRepeat     (int button);
Gus Grubba's avatar
Gus Grubba committed
113 114
    Q_INVOKABLE void    setButtonAction     (int button, const QString& action);
    Q_INVOKABLE QString getButtonAction     (int button);
115

116 117
    // Property accessors

Gus Grubba's avatar
Gus Grubba committed
118 119 120 121
    QString     name                () { return _name; }
    int         totalButtonCount    () { return _totalButtonCount; }
    int         axisCount           () { return _axisCount; }
    bool        gimbalEnabled       () { return _gimbalEnabled; }
Gus Grubba's avatar
Gus Grubba committed
122 123 124 125 126
    QStringList buttonActions       ();

    QmlObjectListModel* assignableActions   () { return &_assignableButtonActions; }
    QStringList assignableActionTitles      () { return _availableActionTitles; }
    QString     disabledActionName          () { return _buttonActionNone; }
Gus Grubba's avatar
Gus Grubba committed
127 128

    void setGimbalEnabled           (bool set);
129

130
    /// Start the polling thread which will in turn emit joystick signals
131
    void startPolling(Vehicle* vehicle);
132
    void stopPolling(void);
133

134 135
    void setCalibration(int axis, Calibration_t& calibration);
    Calibration_t getCalibration(int axis);
136

137 138
    void setFunctionAxis(AxisFunction_t function, int axis);
    int getFunctionAxis(AxisFunction_t function);
139 140


141
/*
Jacob Walser's avatar
Jacob Walser committed
142
    // Joystick index used by sdl library
Patrick José Pereira's avatar
Patrick José Pereira committed
143
    // Settable because sdl library remaps indices after certain events
Jacob Walser's avatar
Jacob Walser committed
144 145
    virtual int index(void) = 0;
    virtual void setIndex(int index) = 0;
146
*/
147 148
	virtual bool requiresCalibration(void) { return true; }

Gus Grubba's avatar
Gus Grubba committed
149 150
    int   throttleMode      ();
    void  setThrottleMode   (int mode);
151

Gus Grubba's avatar
Gus Grubba committed
152 153
    bool  negativeThrust    ();
    void  setNegativeThrust (bool allowNegative);
154

Gus Grubba's avatar
Gus Grubba committed
155
    float exponential       ();
Gus Grubba's avatar
Gus Grubba committed
156
    void  setExponential    (float expo);
157

Gus Grubba's avatar
Gus Grubba committed
158 159
    bool  accumulator       ();
    void  setAccumulator    (bool accu);
160

Gus Grubba's avatar
Gus Grubba committed
161 162
    bool  deadband          ();
    void  setDeadband       (bool accu);
Gregory Dymarek's avatar
Gregory Dymarek committed
163

Gus Grubba's avatar
Gus Grubba committed
164 165
    bool  circleCorrection  ();
    void  setCircleCorrection(bool circleCorrection);
166

Gus Grubba's avatar
Gus Grubba committed
167 168
    void  setTXMode         (int mode);
    int   getTXMode         () { return _transmitterMode; }
169

Don Gagne's avatar
Don Gagne committed
170
    /// Set the current calibration mode
Gus Grubba's avatar
Gus Grubba committed
171
    void  setCalibrationMode (bool calibrating);
172

Gus Grubba's avatar
Gus Grubba committed
173
    /// Get joystick message rate (in Hz)
Gus Grubba's avatar
Gus Grubba committed
174
    float axisFrequency     () { return _axisFrequency; }
Gus Grubba's avatar
Gus Grubba committed
175
    /// Set joystick message rate (in Hz)
Gus Grubba's avatar
Gus Grubba committed
176 177
    void  setAxisFrequency  (float val);

Gus Grubba's avatar
Gus Grubba committed
178
    /// Get joystick button repeat rate (in Hz)
Gus Grubba's avatar
Gus Grubba committed
179
    float buttonFrequency   () { return _buttonFrequency; }
Gus Grubba's avatar
Gus Grubba committed
180
    /// Set joystick button repeat rate (in Hz)
Gus Grubba's avatar
Gus Grubba committed
181
    void  setButtonFrequency(float val);
182

183 184
signals:
    // The raw signals are only meant for use by calibration
Gus Grubba's avatar
Gus Grubba committed
185 186 187 188
    void rawAxisValueChanged        (int index, int value);
    void rawButtonPressedChanged    (int index, int pressed);
    void calibratedChanged          (bool calibrated);
    void buttonActionsChanged       ();
189
    void assignableActionsChanged   ();
Gus Grubba's avatar
Gus Grubba committed
190 191 192 193 194 195
    void throttleModeChanged        (int mode);
    void negativeThrustChanged      (bool allowNegative);
    void exponentialChanged         (float exponential);
    void accumulatorChanged         (bool accumulator);
    void enabledChanged             (bool enabled);
    void circleCorrectionChanged    (bool circleCorrection);
196
    void axisValues                 (float roll, float pitch, float yaw, float throttle);
Gus Grubba's avatar
Gus Grubba committed
197 198 199
    void manualControlGimbal        (float gimbalPitch, float gimbalYaw);

    void gimbalEnabledChanged       ();
Gus Grubba's avatar
Gus Grubba committed
200 201
    void axisFrequencyChanged       ();
    void buttonFrequencyChanged     ();
Gus Grubba's avatar
Gus Grubba committed
202 203 204 205 206 207 208 209 210 211 212 213
    void startContinuousZoom        (int direction);
    void stopContinuousZoom         ();
    void stepZoom                   (int direction);
    void stepCamera                 (int direction);
    void stepStream                 (int direction);
    void triggerCamera              ();
    void startVideoRecord           ();
    void stopVideoRecord            ();
    void toggleVideoRecord          ();
    void gimbalPitchStep            (int direction);
    void gimbalYawStep              (int direction);
    void centerGimbal               ();
214
    void gimbalControlValue         (double pitch, double yaw);
Gus Grubba's avatar
Gus Grubba committed
215 216 217
    void setArmed                   (bool arm);
    void setVtolInFwdFlight         (bool set);
    void setFlightMode              (const QString& flightMode);
218
    void emergencyStop              ();
219

220
protected:
Gus Grubba's avatar
Gus Grubba committed
221 222
    void    _setDefaultCalibration  ();
    void    _saveSettings           ();
Gus Grubba's avatar
Gus Grubba committed
223
    void    _saveButtonSettings     ();
Gus Grubba's avatar
Gus Grubba committed
224 225
    void    _loadSettings           ();
    float   _adjustRange            (int value, Calibration_t calibration, bool withDeadbands);
226
    void    _executeButtonAction    (const QString& action, bool buttonDown);
Gus Grubba's avatar
Gus Grubba committed
227
    int     _findAssignableButtonAction(const QString& action);
Gus Grubba's avatar
Gus Grubba committed
228 229
    bool    _validAxis              (int axis);
    bool    _validButton            (int button);
Gus Grubba's avatar
Gus Grubba committed
230 231
    void    _handleAxis             ();
    void    _handleButtons          ();
232
    void    _buildActionList        (Vehicle* activeVehicle);
233

234 235 236 237 238
    void    _pitchStep              (int direction);
    void    _yawStep                (int direction);
    double  _localYaw       = 0.0;
    double  _localPitch     = 0.0;

239
private:
Gus Grubba's avatar
Gus Grubba committed
240 241 242
    virtual bool _open      ()          = 0;
    virtual void _close     ()          = 0;
    virtual bool _update    ()          = 0;
243

Gus Grubba's avatar
Gus Grubba committed
244 245 246
    virtual bool _getButton (int i)      = 0;
    virtual int  _getAxis   (int i)      = 0;
    virtual bool _getHat    (int hat,int i) = 0;
247

248
    void _updateTXModeSettingsKey(Vehicle* activeVehicle);
249 250 251
    int _mapFunctionMode(int mode, int function);
    void _remapAxes(int currentMode, int newMode, int (&newMapping)[maxFunction]);

252
    // Override from QThread
Gus Grubba's avatar
Gus Grubba committed
253
    virtual void run();
254

255
protected:
256

Gus Grubba's avatar
Gus Grubba committed
257 258 259 260 261 262 263 264
    enum {
        BUTTON_UP,
        BUTTON_DOWN,
        BUTTON_REPEAT
    };

    uint8_t*_rgButtonValues         = nullptr;

Gus Grubba's avatar
Gus Grubba committed
265 266 267 268 269 270 271 272 273 274
    bool    _exitThread             = false;    ///< true: signal thread to exit
    bool    _calibrationMode        = false;
    int*    _rgAxisValues           = nullptr;
    Calibration_t* _rgCalibration   = nullptr;
    ThrottleMode_t _throttleMode    = ThrottleModeDownZero;
    bool    _negativeThrust         = false;
    float   _exponential            = 0;
    bool    _accumulator            = false;
    bool    _deadband               = false;
    bool    _circleCorrection       = true;
Gus Grubba's avatar
Gus Grubba committed
275 276
    float   _axisFrequency          = 25.0f;
    float   _buttonFrequency        = 5.0f;
Gus Grubba's avatar
Gus Grubba committed
277
    Vehicle* _activeVehicle         = nullptr;
Gus Grubba's avatar
Gus Grubba committed
278
    bool    _gimbalEnabled          = false;
Gus Grubba's avatar
Gus Grubba committed
279 280

    bool    _pollingStartedForCalibration = false;
281

282 283 284 285
    QString _name;
    bool    _calibrated;
    int     _axisCount;
    int     _buttonCount;
286 287 288
    int     _hatCount;
    int     _hatButtonCount;
    int     _totalButtonCount;
289

290
    static int          _transmitterMode;
Gus Grubba's avatar
Gus Grubba committed
291
    int                 _rgFunctionAxis[maxFunction] = {};
292
    QElapsedTimer       _axisTime;
293

Gus Grubba's avatar
Gus Grubba committed
294 295 296 297
    QmlObjectListModel              _assignableButtonActions;
    QList<AssignedButtonAction*>    _buttonActionArray;
    QStringList                     _availableActionTitles;
    MultiVehicleManager*            _multiVehicleManager = nullptr;
298

299

300 301 302
private:
    static const char*  _rgFunctionSettingsKey[maxFunction];

303 304
    static const char* _settingsGroup;
    static const char* _calibratedSettingsKey;
Gus Grubba's avatar
Gus Grubba committed
305 306
    static const char* _buttonActionNameKey;
    static const char* _buttonActionRepeatKey;
307
    static const char* _throttleModeSettingsKey;
308
    static const char* _exponentialSettingsKey;
309
    static const char* _accumulatorSettingsKey;
Gregory Dymarek's avatar
Gregory Dymarek committed
310
    static const char* _deadbandSettingsKey;
311
    static const char* _circleCorrectionSettingsKey;
Gus Grubba's avatar
Gus Grubba committed
312 313
    static const char* _axisFrequencySettingsKey;
    static const char* _buttonFrequencySettingsKey;
314
    static const char* _txModeSettingsKey;
315 316 317 318 319
    static const char* _fixedWingTXModeSettingsKey;
    static const char* _multiRotorTXModeSettingsKey;
    static const char* _roverTXModeSettingsKey;
    static const char* _vtolTXModeSettingsKey;
    static const char* _submarineTXModeSettingsKey;
Gus Grubba's avatar
Gus Grubba committed
320
    static const char* _gimbalSettingsKey;
321

Gus Grubba's avatar
Gus Grubba committed
322
    static const char* _buttonActionNone;
323 324
    static const char* _buttonActionArm;
    static const char* _buttonActionDisarm;
Gus Grubba's avatar
Gus Grubba committed
325
    static const char* _buttonActionToggleArm;
326 327
    static const char* _buttonActionVTOLFixedWing;
    static const char* _buttonActionVTOLMultiRotor;
Gus Grubba's avatar
Gus Grubba committed
328 329 330 331
    static const char* _buttonActionStepZoomIn;
    static const char* _buttonActionStepZoomOut;
    static const char* _buttonActionContinuousZoomIn;
    static const char* _buttonActionContinuousZoomOut;
332 333 334 335
    static const char* _buttonActionNextStream;
    static const char* _buttonActionPreviousStream;
    static const char* _buttonActionNextCamera;
    static const char* _buttonActionPreviousCamera;
Gus Grubba's avatar
Gus Grubba committed
336 337 338 339
    static const char* _buttonActionTriggerCamera;
    static const char* _buttonActionStartVideoRecord;
    static const char* _buttonActionStopVideoRecord;
    static const char* _buttonActionToggleVideoRecord;
Gus Grubba's avatar
Gus Grubba committed
340 341 342 343 344
    static const char* _buttonActionGimbalDown;
    static const char* _buttonActionGimbalUp;
    static const char* _buttonActionGimbalLeft;
    static const char* _buttonActionGimbalRight;
    static const char* _buttonActionGimbalCenter;
345
    static const char* _buttonActionEmergencyStop;
346

347 348
private slots:
    void _activeVehicleChanged(Vehicle* activeVehicle);
349
};