Joystick.h 8.53 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 52 53
    typedef enum {
        rollFunction,
        pitchFunction,
        yawFunction,
        throttleFunction,
        maxFunction
    } AxisFunction_t;
54

55 56 57 58 59
    typedef enum {
        ThrottleModeCenterZero,
        ThrottleModeDownZero,
        ThrottleModeMax
    } ThrottleMode_t;
60

61
    Q_PROPERTY(QString name READ name CONSTANT)
62

Don Gagne's avatar
Don Gagne committed
63
    Q_PROPERTY(bool calibrated MEMBER _calibrated NOTIFY calibratedChanged)
64

65
    Q_PROPERTY(int totalButtonCount  READ totalButtonCount    CONSTANT)
66
    Q_PROPERTY(int axisCount    READ axisCount      CONSTANT)
67

68
    Q_PROPERTY(QStringList actions READ actions CONSTANT)
69

70
    Q_PROPERTY(QVariantList buttonActions READ buttonActions NOTIFY buttonActionsChanged)
Don Gagne's avatar
Don Gagne committed
71 72
    Q_INVOKABLE void setButtonAction(int button, const QString& action);
    Q_INVOKABLE QString getButtonAction(int button);
73

74
    Q_PROPERTY(int throttleMode READ throttleMode WRITE setThrottleMode NOTIFY throttleModeChanged)
75
    Q_PROPERTY(bool negativeThrust READ negativeThrust WRITE setNegativeThrust NOTIFY negativeThrustChanged)
76
    Q_PROPERTY(float exponential READ exponential WRITE setExponential NOTIFY exponentialChanged)
77
    Q_PROPERTY(bool accumulator READ accumulator WRITE setAccumulator NOTIFY accumulatorChanged)
78 79
    Q_PROPERTY(bool requiresCalibration READ requiresCalibration CONSTANT)
    Q_PROPERTY(bool circleCorrection READ circleCorrection WRITE setCircleCorrection NOTIFY circleCorrectionChanged)
80
    Q_PROPERTY(float frequency READ frequency WRITE setFrequency NOTIFY frequencyChanged)
81

82 83 84
    // Property accessors

    int axisCount(void) { return _axisCount; }
85
    int totalButtonCount(void) { return _totalButtonCount; }
86

87
    /// Start the polling thread which will in turn emit joystick signals
88
    void startPolling(Vehicle* vehicle);
89
    void stopPolling(void);
90

91 92
    void setCalibration(int axis, Calibration_t& calibration);
    Calibration_t getCalibration(int axis);
93

94 95
    void setFunctionAxis(AxisFunction_t function, int axis);
    int getFunctionAxis(AxisFunction_t function);
96

97 98
    QStringList actions(void);
    QVariantList buttonActions(void);
99

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

109 110
    int throttleMode(void);
    void setThrottleMode(int mode);
111

112 113 114
    bool negativeThrust(void);
    void setNegativeThrust(bool allowNegative);

115 116
    float exponential(void);
    void setExponential(float expo);
117

118 119 120
    bool accumulator(void);
    void setAccumulator(bool accu);

Gregory Dymarek's avatar
Gregory Dymarek committed
121 122 123
    bool deadband(void);
    void setDeadband(bool accu);

124 125 126
    bool circleCorrection(void);
    void setCircleCorrection(bool circleCorrection);

127 128 129
    void setTXMode(int mode);
    int getTXMode(void) { return _transmitterMode; }

Don Gagne's avatar
Don Gagne committed
130
    /// Set the current calibration mode
131
    void setCalibrationMode(bool calibrating);
132

133 134 135
    float frequency();
    void setFrequency(float val);

136 137
signals:
    void calibratedChanged(bool calibrated);
138

139 140 141
    // The raw signals are only meant for use by calibration
    void rawAxisValueChanged(int index, int value);
    void rawButtonPressedChanged(int index, int pressed);
142

143
    void buttonActionsChanged(QVariantList actions);
144

145
    void throttleModeChanged(int mode);
146

147 148
    void negativeThrustChanged(bool allowNegative);

149
    void exponentialChanged(float exponential);
150

151 152
    void accumulatorChanged(bool accumulator);

153
    void enabledChanged(bool enabled);
154

155 156
    void circleCorrectionChanged(bool circleCorrection);

157 158 159 160 161 162 163
    /// Signal containing new joystick information
    ///     @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
    ///     @param mode     See Vehicle::JoystickMode_t enum
    void manualControl(float roll, float pitch, float yaw, float throttle, quint16 buttons, int joystickMmode);
164

165
    void buttonActionTriggered(int action);
166

167 168 169 170
    void frequencyChanged   ();
    void stepZoom           (int direction);
    void stepCamera         (int direction);
    void stepStream         (int direction);
171

172
protected:
173 174 175 176 177 178 179
    void    _setDefaultCalibration(void);
    void    _saveSettings(void);
    void    _loadSettings(void);
    float   _adjustRange(int value, Calibration_t calibration, bool withDeadbands);
    void    _buttonAction(const QString& action);
    bool    _validAxis(int axis);
    bool    _validButton(int button);
180

181
private:
Gregory Dymarek's avatar
Gregory Dymarek committed
182 183 184
    virtual bool _open() = 0;
    virtual void _close() = 0;
    virtual bool _update() = 0;
185

Gregory Dymarek's avatar
Gregory Dymarek committed
186 187
    virtual bool _getButton(int i) = 0;
    virtual int _getAxis(int i) = 0;
188
    virtual uint8_t _getHat(int hat,int i) = 0;
189

190
    void _updateTXModeSettingsKey(Vehicle* activeVehicle);
191 192 193
    int _mapFunctionMode(int mode, int function);
    void _remapAxes(int currentMode, int newMode, int (&newMapping)[maxFunction]);

194 195 196
    // Override from QThread
    virtual void run(void);

197
protected:
198

199
    bool    _exitThread;    ///< true: signal thread to exit
200

201 202 203 204
    QString _name;
    bool    _calibrated;
    int     _axisCount;
    int     _buttonCount;
205 206 207
    int     _hatCount;
    int     _hatButtonCount;
    int     _totalButtonCount;
208

209
    static int          _transmitterMode;
210
    bool                _calibrationMode;
211

212 213
    int*                _rgAxisValues;
    Calibration_t*      _rgCalibration;
214
    int                 _rgFunctionAxis[maxFunction];
215

216
    bool*               _rgButtonValues;
217
    QStringList         _rgButtonActions;
218
    quint16             _lastButtonBits;
219

220
    ThrottleMode_t      _throttleMode;
221

222 223
    bool                _negativeThrust;

224
    float                _exponential;
225
    bool                _accumulator;
Gregory Dymarek's avatar
Gregory Dymarek committed
226
    bool                _deadband;
227
    bool                _circleCorrection;
228
    float               _frequency;
229

230 231
    Vehicle*            _activeVehicle;
    bool                _pollingStartedForCalibration;
232 233

    MultiVehicleManager*    _multiVehicleManager;
234

235 236 237
private:
    static const char*  _rgFunctionSettingsKey[maxFunction];

238 239 240 241
    static const char* _settingsGroup;
    static const char* _calibratedSettingsKey;
    static const char* _buttonActionSettingsKey;
    static const char* _throttleModeSettingsKey;
242
    static const char* _exponentialSettingsKey;
243
    static const char* _accumulatorSettingsKey;
Gregory Dymarek's avatar
Gregory Dymarek committed
244
    static const char* _deadbandSettingsKey;
245
    static const char* _circleCorrectionSettingsKey;
246
    static const char* _frequencySettingsKey;
247
    static const char* _txModeSettingsKey;
248 249 250 251 252 253
    static const char* _fixedWingTXModeSettingsKey;
    static const char* _multiRotorTXModeSettingsKey;
    static const char* _roverTXModeSettingsKey;
    static const char* _vtolTXModeSettingsKey;
    static const char* _submarineTXModeSettingsKey;

254 255 256 257
    static const char* _buttonActionArm;
    static const char* _buttonActionDisarm;
    static const char* _buttonActionVTOLFixedWing;
    static const char* _buttonActionVTOLMultiRotor;
258 259 260 261 262 263
    static const char* _buttonActionZoomIn;
    static const char* _buttonActionZoomOut;
    static const char* _buttonActionNextStream;
    static const char* _buttonActionPreviousStream;
    static const char* _buttonActionNextCamera;
    static const char* _buttonActionPreviousCamera;
264

265 266
private slots:
    void _activeVehicleChanged(Vehicle* activeVehicle);
267
};
268

269
#endif