Joystick.h 10.5 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

#ifndef Joystick_H
#define Joystick_H

#include <QObject>
#include <QThread>

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

Q_DECLARE_LOGGING_CATEGORY(JoystickLog)
22
Q_DECLARE_LOGGING_CATEGORY(JoystickValuesLog)
23 24 25 26

class Joystick : public QThread
{
    Q_OBJECT
27

28
public:
29 30
    Joystick(const QString& name, int axisCount, int buttonCount, int hatCount, MultiVehicleManager* multiVehicleManager);

31
    ~Joystick();
32

33
    typedef struct Calibration_t {
34 35 36
        int     min;
        int     max;
        int     center;
37
        int     deadband;
38
        bool    reversed;
39 40 41 42 43 44
        Calibration_t()
            : min(-32767)
            , max(32767)
            , center(0)
            , deadband(0)
            , reversed(false) {}
45
    } Calibration_t;
46

47 48 49 50 51
    typedef enum {
        rollFunction,
        pitchFunction,
        yawFunction,
        throttleFunction,
Gus Grubba's avatar
Gus Grubba committed
52 53
        gimbalPitchFunction,
        gimbalYawFunction,
54 55
        maxFunction
    } AxisFunction_t;
56

57 58 59 60 61
    typedef enum {
        ThrottleModeCenterZero,
        ThrottleModeDownZero,
        ThrottleModeMax
    } ThrottleMode_t;
62

Gus Grubba's avatar
Gus Grubba committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    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)
    Q_PROPERTY(QStringList actions          READ actions                CONSTANT)
    Q_PROPERTY(QVariantList buttonActions   READ buttonActions          NOTIFY buttonActionsChanged)

    Q_PROPERTY(bool     gimbalEnabled       READ gimbalEnabled          WRITE setGimbalEnabled      NOTIFY gimbalEnabledChanged)
    Q_PROPERTY(int      throttleMode        READ throttleMode           WRITE setThrottleMode       NOTIFY throttleModeChanged)
    Q_PROPERTY(float    frequency           READ frequency              WRITE setFrequency          NOTIFY frequencyChanged)
    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    setButtonAction (int button, const QString& action);
    Q_INVOKABLE QString getButtonAction (int button);
81

82 83
    // Property accessors

Gus Grubba's avatar
Gus Grubba committed
84 85 86 87 88 89 90 91
    QString     name                () { return _name; }
    int         totalButtonCount    () { return _totalButtonCount; }
    int         axisCount           () { return _axisCount; }
    bool        gimbalEnabled       () { return _gimbalEnabled; }
    QStringList  actions            ();
    QVariantList buttonActions      ();

    void setGimbalEnabled           (bool set);
92

93
    /// Start the polling thread which will in turn emit joystick signals
94
    void startPolling(Vehicle* vehicle);
95
    void stopPolling(void);
96

97 98
    void setCalibration(int axis, Calibration_t& calibration);
    Calibration_t getCalibration(int axis);
99

100 101
    void setFunctionAxis(AxisFunction_t function, int axis);
    int getFunctionAxis(AxisFunction_t function);
102 103


104
/*
Jacob Walser's avatar
Jacob Walser committed
105
    // Joystick index used by sdl library
Patrick José Pereira's avatar
Patrick José Pereira committed
106
    // Settable because sdl library remaps indices after certain events
Jacob Walser's avatar
Jacob Walser committed
107 108
    virtual int index(void) = 0;
    virtual void setIndex(int index) = 0;
109
*/
110 111
	virtual bool requiresCalibration(void) { return true; }

112 113
    int throttleMode(void);
    void setThrottleMode(int mode);
114

115 116 117
    bool negativeThrust(void);
    void setNegativeThrust(bool allowNegative);

118 119
    float exponential(void);
    void setExponential(float expo);
120

121 122 123
    bool accumulator(void);
    void setAccumulator(bool accu);

Gregory Dymarek's avatar
Gregory Dymarek committed
124 125 126
    bool deadband(void);
    void setDeadband(bool accu);

127 128 129
    bool circleCorrection(void);
    void setCircleCorrection(bool circleCorrection);

130 131 132
    void setTXMode(int mode);
    int getTXMode(void) { return _transmitterMode; }

Don Gagne's avatar
Don Gagne committed
133
    /// Set the current calibration mode
134
    void setCalibrationMode(bool calibrating);
135

136 137 138
    float frequency();
    void setFrequency(float val);

139 140
signals:
    void calibratedChanged(bool calibrated);
141

142 143 144
    // The raw signals are only meant for use by calibration
    void rawAxisValueChanged(int index, int value);
    void rawButtonPressedChanged(int index, int pressed);
145

146
    void buttonActionsChanged(QVariantList actions);
147

148
    void throttleModeChanged(int mode);
149

150 151
    void negativeThrustChanged(bool allowNegative);

152
    void exponentialChanged(float exponential);
153

154 155
    void accumulatorChanged(bool accumulator);

156
    void enabledChanged(bool enabled);
157

158 159
    void circleCorrectionChanged(bool circleCorrection);

160
    /// Signal containing new joystick information
Gus Grubba's avatar
Gus Grubba committed
161 162 163 164
    ///     @param roll         Range is -1:1, negative meaning roll left, positive meaning roll right
    ///     @param pitch        Range i -1:1, negative meaning pitch down, positive meaning pitch up
    ///     @param yaw          Range is -1:1, negative meaning yaw left, positive meaning yaw right
    ///     @param throttle     Range is 0:1, 0 meaning no throttle, 1 meaning full throttle
165
    ///     @param mode     See Vehicle::JoystickMode_t enum
Gus Grubba's avatar
Gus Grubba committed
166 167
    void manualControl          (float roll, float pitch, float yaw, float throttle, quint16 buttons, int joystickMmode);
    void manualControlGimbal    (float gimbalPitch, float gimbalYaw);
168

169
    void buttonActionTriggered(int action);
170

Gus Grubba's avatar
Gus Grubba committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    void gimbalEnabledChanged   ();
    void frequencyChanged       ();
    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           ();
    void setArmed               (bool arm);
    void setVtolInFwdFlight     (bool set);
    void setFlightMode          (const QString& flightMode);
186

187
protected:
Gus Grubba's avatar
Gus Grubba committed
188 189 190 191 192 193 194
    void    _setDefaultCalibration  ();
    void    _saveSettings           ();
    void    _loadSettings           ();
    float   _adjustRange            (int value, Calibration_t calibration, bool withDeadbands);
    void    _buttonAction           (const QString& action);
    bool    _validAxis              (int axis);
    bool    _validButton            (int button);
195

196
private:
Gus Grubba's avatar
Gus Grubba committed
197 198 199
    virtual bool _open      ()          = 0;
    virtual void _close     ()          = 0;
    virtual bool _update    ()          = 0;
200

Gus Grubba's avatar
Gus Grubba committed
201 202 203
    virtual bool _getButton (int i)      = 0;
    virtual int  _getAxis   (int i)      = 0;
    virtual bool _getHat    (int hat,int i) = 0;
204

205
    void _updateTXModeSettingsKey(Vehicle* activeVehicle);
206 207 208
    int _mapFunctionMode(int mode, int function);
    void _remapAxes(int currentMode, int newMode, int (&newMapping)[maxFunction]);

209
    // Override from QThread
Gus Grubba's avatar
Gus Grubba committed
210
    virtual void run();
211

212
protected:
213

Gus Grubba's avatar
Gus Grubba committed
214 215 216 217 218 219 220 221
    enum {
        BUTTON_UP,
        BUTTON_DOWN,
        BUTTON_REPEAT
    };

    uint8_t*_rgButtonValues         = nullptr;

Gus Grubba's avatar
Gus Grubba committed
222 223 224 225 226 227 228 229 230 231 232 233
    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;
    float   _frequency              = 25.0f;
    Vehicle* _activeVehicle         = nullptr;
Gus Grubba's avatar
Gus Grubba committed
234
    bool    _gimbalEnabled          = false;
Gus Grubba's avatar
Gus Grubba committed
235 236

    bool    _pollingStartedForCalibration = false;
237

238 239 240 241
    QString _name;
    bool    _calibrated;
    int     _axisCount;
    int     _buttonCount;
242 243 244
    int     _hatCount;
    int     _hatButtonCount;
    int     _totalButtonCount;
245

246
    static int          _transmitterMode;
247

Gus Grubba's avatar
Gus Grubba committed
248
    int                 _rgFunctionAxis[maxFunction] = {};
249 250

    QStringList         _rgButtonActions;
251

Gus Grubba's avatar
Gus Grubba committed
252
    MultiVehicleManager* _multiVehicleManager = nullptr;
253

254 255 256
private:
    static const char*  _rgFunctionSettingsKey[maxFunction];

257 258 259 260
    static const char* _settingsGroup;
    static const char* _calibratedSettingsKey;
    static const char* _buttonActionSettingsKey;
    static const char* _throttleModeSettingsKey;
261
    static const char* _exponentialSettingsKey;
262
    static const char* _accumulatorSettingsKey;
Gregory Dymarek's avatar
Gregory Dymarek committed
263
    static const char* _deadbandSettingsKey;
264
    static const char* _circleCorrectionSettingsKey;
265
    static const char* _frequencySettingsKey;
266
    static const char* _txModeSettingsKey;
267 268 269 270 271
    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
272
    static const char* _gimbalSettingsKey;
273

274 275
    static const char* _buttonActionArm;
    static const char* _buttonActionDisarm;
Gus Grubba's avatar
Gus Grubba committed
276
    static const char* _buttonActionToggleArm;
277 278
    static const char* _buttonActionVTOLFixedWing;
    static const char* _buttonActionVTOLMultiRotor;
279 280 281 282 283 284
    static const char* _buttonActionZoomIn;
    static const char* _buttonActionZoomOut;
    static const char* _buttonActionNextStream;
    static const char* _buttonActionPreviousStream;
    static const char* _buttonActionNextCamera;
    static const char* _buttonActionPreviousCamera;
Gus Grubba's avatar
Gus Grubba committed
285 286 287 288
    static const char* _buttonActionTriggerCamera;
    static const char* _buttonActionStartVideoRecord;
    static const char* _buttonActionStopVideoRecord;
    static const char* _buttonActionToggleVideoRecord;
Gus Grubba's avatar
Gus Grubba committed
289 290 291 292 293
    static const char* _buttonActionGimbalDown;
    static const char* _buttonActionGimbalUp;
    static const char* _buttonActionGimbalLeft;
    static const char* _buttonActionGimbalRight;
    static const char* _buttonActionGimbalCenter;
294

295 296
private slots:
    void _activeVehicleChanged(Vehicle* activeVehicle);
297
};
298

299
#endif