JoystickInput.h 5.75 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*=====================================================================

PIXHAWK Micro Air Vehicle Flying Robotics Toolkit

(c) 2009, 2010 PIXHAWK PROJECT  <http://pixhawk.ethz.ch>

This file is part of the PIXHAWK project

    PIXHAWK is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PIXHAWK is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PIXHAWK. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Definition of joystick interface
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *   @author Andreas Romer <mavteam@student.ethz.ch>
 *
 */

#ifndef _JOYSTICKINPUT_H_
#define _JOYSTICKINPUT_H_

#include <QThread>
#include <QList>
38
#include <qmutex.h>
Lorenz Meier's avatar
Lorenz Meier committed
39 40 41
#ifdef Q_OS_MAC
#include <SDL.h>
#else
42
#include <SDL/SDL.h>
Lorenz Meier's avatar
Lorenz Meier committed
43
#endif
pixhawk's avatar
pixhawk committed
44 45 46 47 48 49 50 51 52 53 54 55

#include "UASInterface.h"

/**
 * @brief Joystick input
 */
class JoystickInput : public QThread
{
    Q_OBJECT

public:
    JoystickInput();
56
	~JoystickInput();
pixhawk's avatar
pixhawk committed
57
    void run();
58
    void shutdown();
pixhawk's avatar
pixhawk committed
59

Lorenz Meier's avatar
Lorenz Meier committed
60 61 62 63 64 65 66 67 68 69
    /**
     * @brief Load joystick settings
     */
    void loadSettings();

    /**
     * @brief Store joystick settings
     */
    void storeSettings();

70
    int getMappingThrottleAxis() const
Lorenz Meier's avatar
Lorenz Meier committed
71
    {
72
        return throttleAxis;
Lorenz Meier's avatar
Lorenz Meier committed
73 74
    }

75
    int getMappingRollAxis() const
Lorenz Meier's avatar
Lorenz Meier committed
76
    {
77
        return rollAxis;
Lorenz Meier's avatar
Lorenz Meier committed
78 79
    }

80
    int getMappingPitchAxis() const
Lorenz Meier's avatar
Lorenz Meier committed
81
    {
82
        return pitchAxis;
Lorenz Meier's avatar
Lorenz Meier committed
83 84
    }

85
    int getMappingYawAxis() const
Lorenz Meier's avatar
Lorenz Meier committed
86 87 88 89
    {
        return yawAxis;
    }

90 91
    int getJoystickNumButtons() const
    {
92
        return joystickNumButtons;
93 94
    }

95 96
    int getJoystickNumAxes() const
    {
97
        return joystickNumAxes;
98 99
    }

100 101 102 103 104
    int getJoystickID() const
    {
        return joystickID;
    }

105 106 107 108 109
    const QString& getName() const
    {
        return joystickName;
    }

110 111
    int getNumJoysticks() const
    {
112
        return numJoysticks;
113 114 115 116 117 118 119
    }

    QString getJoystickNameById(int id) const
    {
        return QString(SDL_JoystickName(id));
    }

120 121
    float getCurrentValueForAxis(int axisID);

pixhawk's avatar
pixhawk committed
122 123 124 125 126 127 128 129 130 131
    const double sdlJoystickMin;
    const double sdlJoystickMax;

protected:
    double calibrationPositive[10];
    double calibrationNegative[10];
    SDL_Joystick* joystick;
    UASInterface* uas;
    bool done;

132
    // Store the mapping between axis numbers and the roll/pitch/yaw/throttle configuration.
133
    // Value is one of JoystickAxis::JOYSTICK_AXIS_MAPPING.
134 135
    int rollAxis;
    int pitchAxis;
pixhawk's avatar
pixhawk committed
136
    int yawAxis;
137 138 139
    int throttleAxis;

    // Cache information on the joystick instead of polling the SDL everytime.
140
    int numJoysticks; ///< Total number of joysticks detected by the SDL.
141
    QString joystickName;
142
    int joystickID;
143 144
    int joystickNumAxes;
    int joystickNumButtons;
145

146 147 148
    QList<float> joystickAxes; ///< The values of every axes during the last sample
    quint16 joystickButtons;   ///< The state of every button. Bitfield supporting 16 buttons with 1s indicating that the button is down.
    int xHat, yHat;            ///< The horizontal/vertical hat directions. Values are -1, 0, 1, with (-1,-1) indicating bottom-left.
pixhawk's avatar
pixhawk committed
149 150 151

    void init();

152
signals:
pixhawk's avatar
pixhawk committed
153 154 155 156

    /**
     * @brief Signal containing all joystick raw positions
     *
157 158 159 160
     * @param roll forward / pitch / x axis, front: 1, center: 0, back: -1. If the roll axis isn't defined, NaN is transmit instead.
     * @param pitch left / roll / y axis, left: -1, middle: 0, right: 1. If the roll axis isn't defined, NaN is transmit instead.
     * @param yaw turn axis, left-turn: -1, centered: 0, right-turn: 1. If the roll axis isn't defined, NaN is transmit instead.
     * @param throttle Throttle, -100%:-1.0, 0%: 0.0, 100%: 1.0. If the roll axis isn't defined, NaN is transmit instead.
pixhawk's avatar
pixhawk committed
161 162 163
     * @param xHat hat vector in forward-backward direction, +1 forward, 0 center, -1 backward
     * @param yHat hat vector in left-right direction, -1 left, 0 center, +1 right
     */
164
    void joystickChanged(double roll, double pitch, double yaw, double throttle, int xHat, int yHat, int buttons);
pixhawk's avatar
pixhawk committed
165 166

    /**
167
      * @brief Emit a new value for an axis
168
      *
169
      * @param value Value of the axis, between -1.0 and 1.0.
170
      */
171
    void axisValueChanged(int axis, float value);
pixhawk's avatar
pixhawk committed
172

173
    /**
174
      * @brief Joystick button has changed state from unpressed to pressed.
175 176
      * @param key index of the pressed key
      */
pixhawk's avatar
pixhawk committed
177 178
    void buttonPressed(int key);

179 180 181 182 183 184 185
    /**
      * @brief Joystick button has changed state from pressed to unpressed.
      *
      * @param key index of the released key
      */
    void buttonReleased(int key);

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    /**
      * @brief Hat (8-way switch on the top) has changed position
      *
      * Coordinate frame for joystick hat:
      *
      *    y
      *    ^
      *    |
      *    |
      *    0 ----> x
      *
      *
      * @param x vector in left-right direction
      * @param y vector in forward-backward direction
      */
pixhawk's avatar
pixhawk committed
201 202
    void hatDirectionChanged(int x, int y);

203
public slots:
pixhawk's avatar
pixhawk committed
204
    void setActiveUAS(UASInterface* uas);
205 206
    /** @brief Switch to a new joystick by ID number. */
    void setActiveJoystick(int id);
Lorenz Meier's avatar
Lorenz Meier committed
207

208
    void setMappingRollAxis(int mapping)
Lorenz Meier's avatar
Lorenz Meier committed
209
    {
210
        rollAxis = mapping;
Lorenz Meier's avatar
Lorenz Meier committed
211 212
    }

213
    void setMappingPitchAxis(int mapping)
Lorenz Meier's avatar
Lorenz Meier committed
214
    {
215
        pitchAxis = mapping;
Lorenz Meier's avatar
Lorenz Meier committed
216 217 218 219 220 221 222
    }

    void setMappingYawAxis(int mapping)
    {
        yawAxis = mapping;
    }

223
    void setMappingThrottleAxis(int mapping)
Lorenz Meier's avatar
Lorenz Meier committed
224
    {
225
        throttleAxis = mapping;
Lorenz Meier's avatar
Lorenz Meier committed
226
    }
pixhawk's avatar
pixhawk committed
227 228 229
};

#endif // _JOYSTICKINPUT_H_