JoystickInput.h 5.19 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

60 61
    const QString& getName();

Lorenz Meier's avatar
Lorenz Meier committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    /**
     * @brief Load joystick settings
     */
    void loadSettings();

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

    int getMappingThrustAxis()
    {
        return thrustAxis;
    }

    int getMappingXAxis()
    {
        return xAxis;
    }

    int getMappingYAxis()
    {
        return yAxis;
    }

    int getMappingYawAxis()
    {
        return yawAxis;
    }

    int getMappingAutoButton()
    {
        return autoButtonMapping;
    }

    int getMappingManualButton()
    {
        return manualButtonMapping;
    }

    int getMappingStabilizeButton()
    {
        return stabilizeButtonMapping;
    }

pixhawk's avatar
pixhawk committed
107 108 109 110 111 112 113 114 115 116 117
    const double sdlJoystickMin;
    const double sdlJoystickMax;

protected:
    int defaultIndex;
    double calibrationPositive[10];
    double calibrationNegative[10];
    SDL_Joystick* joystick;
    UASInterface* uas;
    QList<int> uasButtonList;
    bool done;
118
	QMutex m_doneMutex;
pixhawk's avatar
pixhawk committed
119 120 121 122 123 124

    // Axis 3 is thrust (CALIBRATION!)
    int thrustAxis;
    int xAxis;
    int yAxis;
    int yawAxis;
Lorenz Meier's avatar
Lorenz Meier committed
125 126 127
    int autoButtonMapping;
    int manualButtonMapping;
    int stabilizeButtonMapping;
pixhawk's avatar
pixhawk committed
128
    SDL_Event event;
129
    QString joystickName;
pixhawk's avatar
pixhawk committed
130 131 132

    void init();

133
signals:
pixhawk's avatar
pixhawk committed
134 135 136 137 138 139 140 141 142 143 144

    /**
     * @brief Signal containing all joystick raw positions
     *
     * @param roll forward / pitch / x axis, front: 1, center: 0, back: -1
     * @param pitch left / roll / y axis, left: -1, middle: 0, right: 1
     * @param yaw turn axis, left-turn: -1, centered: 0, right-turn: 1
     * @param thrust Thrust, 0%: 0, 100%: 1
     * @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
     */
145
    void joystickChanged(double roll, double pitch, double yaw, double thrust, int xHat, int yHat, int buttons);
pixhawk's avatar
pixhawk committed
146 147 148 149 150 151 152 153

    /**
     * @brief Thrust lever of the joystick has changed
     *
     * @param thrust Thrust, 0%: 0, 100%: 1.0
     */
    void thrustChanged(int thrust);

154 155 156 157 158
    /**
      * @brief X-Axis / forward-backward axis has changed
      *
      * @param x forward / pitch / x axis, front: +1.0, center: 0.0, back: -1.0
      */
pixhawk's avatar
pixhawk committed
159 160
    void xChanged(int x);

161 162 163 164 165
    /**
      * @brief Y-Axis / left-right axis has changed
      *
      * @param y left / roll / y axis, left: -1.0, middle: 0.0, right: +1.0
      */
pixhawk's avatar
pixhawk committed
166 167
    void yChanged(int y);

168 169 170 171 172
    /**
      * @brief Yaw / left-right turn has changed
      *
      * @param yaw turn axis, left-turn: -1.0, middle: 0.0, right-turn: +1.0
      */
pixhawk's avatar
pixhawk committed
173 174
    void yawChanged(int yaw);

175 176 177 178 179
    /**
      * @brief Joystick button has been pressed
      *
      * @param key index of the pressed key
      */
pixhawk's avatar
pixhawk committed
180 181
    void buttonPressed(int key);

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    /**
      * @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
197 198
    void hatDirectionChanged(int x, int y);

199
public slots:
pixhawk's avatar
pixhawk committed
200
    void setActiveUAS(UASInterface* uas);
Lorenz Meier's avatar
Lorenz Meier committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    void setMappingThrustAxis(int mapping)
    {
        thrustAxis = mapping;
    }

    void setMappingXAxis(int mapping)
    {
        xAxis = mapping;
    }

    void setMappingYAxis(int mapping)
    {
        yAxis = mapping;
    }

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

    void setMappingAutoButton(int mapping)
    {
        autoButtonMapping = mapping;
    }

    void setMappingManualButton(int mapping)
    {
        manualButtonMapping = mapping;
    }

    void setMappingStabilizeButton(int mapping)
    {
        stabilizeButtonMapping = mapping;
    }
pixhawk's avatar
pixhawk committed
235 236 237
};

#endif // _JOYSTICKINPUT_H_