Joystick.h 6.22 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 34 35 36
    typedef struct {
        int     min;
        int     max;
        int     center;
37
        int     deadband;
38 39
        bool    reversed;
    } Calibration_t;
40

41 42 43 44 45 46 47
    typedef enum {
        rollFunction,
        pitchFunction,
        yawFunction,
        throttleFunction,
        maxFunction
    } AxisFunction_t;
48

49 50 51 52 53
    typedef enum {
        ThrottleModeCenterZero,
        ThrottleModeDownZero,
        ThrottleModeMax
    } ThrottleMode_t;
54

55
    Q_PROPERTY(QString name READ name CONSTANT)
56

Don Gagne's avatar
Don Gagne committed
57
    Q_PROPERTY(bool calibrated MEMBER _calibrated NOTIFY calibratedChanged)
58

59
    Q_PROPERTY(int totalButtonCount  READ totalButtonCount    CONSTANT)
60
    Q_PROPERTY(int axisCount    READ axisCount      CONSTANT)
61

62
    Q_PROPERTY(QStringList actions READ actions CONSTANT)
63

64
    Q_PROPERTY(QVariantList buttonActions READ buttonActions NOTIFY buttonActionsChanged)
Don Gagne's avatar
Don Gagne committed
65 66
    Q_INVOKABLE void setButtonAction(int button, const QString& action);
    Q_INVOKABLE QString getButtonAction(int button);
67

68
    Q_PROPERTY(int throttleMode READ throttleMode WRITE setThrottleMode NOTIFY throttleModeChanged)
69 70
    Q_PROPERTY(bool exponential READ exponential WRITE setExponential NOTIFY exponentialChanged)
    Q_PROPERTY(bool accumulator READ accumulator WRITE setAccumulator NOTIFY accumulatorChanged)
71 72 73 74

    // Property accessors

    int axisCount(void) { return _axisCount; }
75
    int totalButtonCount(void) { return _totalButtonCount; }
76

77
    /// Start the polling thread which will in turn emit joystick signals
78
    void startPolling(Vehicle* vehicle);
79
    void stopPolling(void);
80

81 82
    void setCalibration(int axis, Calibration_t& calibration);
    Calibration_t getCalibration(int axis);
83

84 85
    void setFunctionAxis(AxisFunction_t function, int axis);
    int getFunctionAxis(AxisFunction_t function);
86

87 88
    QStringList actions(void);
    QVariantList buttonActions(void);
89

90
    QString name(void) { return _name; }
91

92 93
    int throttleMode(void);
    void setThrottleMode(int mode);
94

95 96 97
    bool exponential(void);
    void setExponential(bool expo);

98 99 100
    bool accumulator(void);
    void setAccumulator(bool accu);

Gregory Dymarek's avatar
Gregory Dymarek committed
101 102 103
    bool deadband(void);
    void setDeadband(bool accu);

Don Gagne's avatar
Don Gagne committed
104 105 106 107 108
    typedef enum {
        CalibrationModeOff,         // Not calibrating
        CalibrationModeMonitor,     // Monitors are active, continue to send to vehicle if already polling
        CalibrationModeCalibrating, // Calibrating, stop sending joystick to vehicle
    } CalibrationMode_t;
109

Don Gagne's avatar
Don Gagne committed
110 111
    /// Set the current calibration mode
    void startCalibrationMode(CalibrationMode_t mode);
112

Don Gagne's avatar
Don Gagne committed
113 114
    /// Clear the current calibration mode
    void stopCalibrationMode(CalibrationMode_t mode);
115

116 117
signals:
    void calibratedChanged(bool calibrated);
118

119 120 121
    // The raw signals are only meant for use by calibration
    void rawAxisValueChanged(int index, int value);
    void rawButtonPressedChanged(int index, int pressed);
122

123
    void buttonActionsChanged(QVariantList actions);
124

125
    void throttleModeChanged(int mode);
126

127 128
    void exponentialChanged(bool exponential);

129 130
    void accumulatorChanged(bool accumulator);

131
    void enabledChanged(bool enabled);
132

133 134 135 136 137 138 139
    /// 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);
140

141
    void buttonActionTriggered(int action);
142

143
protected:
144 145
    void _saveSettings(void);
    void _loadSettings(void);
146
    float _adjustRange(int value, Calibration_t calibration, bool withDeadbands);
Don Gagne's avatar
Don Gagne committed
147
    void _buttonAction(const QString& action);
148 149 150
    bool _validAxis(int axis);
    bool _validButton(int button);

151
private:
Gregory Dymarek's avatar
Gregory Dymarek committed
152 153 154
    virtual bool _open() = 0;
    virtual void _close() = 0;
    virtual bool _update() = 0;
155

Gregory Dymarek's avatar
Gregory Dymarek committed
156 157
    virtual bool _getButton(int i) = 0;
    virtual int _getAxis(int i) = 0;
158
    virtual uint8_t _getHat(int hat,int i) = 0;
159

160 161 162
    // Override from QThread
    virtual void run(void);

163
protected:
164

165
    bool    _exitThread;    ///< true: signal thread to exit
166

167 168 169 170
    QString _name;
    bool    _calibrated;
    int     _axisCount;
    int     _buttonCount;
171 172 173
    int     _hatCount;
    int     _hatButtonCount;
    int     _totalButtonCount;
174

Don Gagne's avatar
Don Gagne committed
175
    CalibrationMode_t   _calibrationMode;
176

177 178
    int*                _rgAxisValues;
    Calibration_t*      _rgCalibration;
179
    int                 _rgFunctionAxis[maxFunction];
180

181
    bool*               _rgButtonValues;
182
    QStringList         _rgButtonActions;
183
    quint16             _lastButtonBits;
184

185
    ThrottleMode_t      _throttleMode;
186

187
    bool                _exponential;
188
    bool                _accumulator;
Gregory Dymarek's avatar
Gregory Dymarek committed
189
    bool                _deadband;
190

191 192
    Vehicle*            _activeVehicle;
    bool                _pollingStartedForCalibration;
193 194

    MultiVehicleManager*    _multiVehicleManager;
195

196 197 198
private:
    static const char*  _rgFunctionSettingsKey[maxFunction];

199 200 201 202
    static const char* _settingsGroup;
    static const char* _calibratedSettingsKey;
    static const char* _buttonActionSettingsKey;
    static const char* _throttleModeSettingsKey;
203
    static const char* _exponentialSettingsKey;
204
    static const char* _accumulatorSettingsKey;
Gregory Dymarek's avatar
Gregory Dymarek committed
205
    static const char* _deadbandSettingsKey;
206
};
207

208
#endif