Joystick.cc 18.8 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

#include "Joystick.h"
#include "QGC.h"
#include "AutoPilotPlugin.h"
#include "UAS.h"

#include <QSettings>

QGC_LOGGING_CATEGORY(JoystickLog, "JoystickLog")
19
QGC_LOGGING_CATEGORY(JoystickValuesLog, "JoystickValuesLog")
20 21 22

const char* Joystick::_settingsGroup =              "Joysticks";
const char* Joystick::_calibratedSettingsKey =      "Calibrated";
Don Gagne's avatar
Don Gagne committed
23
const char* Joystick::_buttonActionSettingsKey =    "ButtonActionName%1";
24
const char* Joystick::_throttleModeSettingsKey =    "ThrottleMode";
25
const char* Joystick::_exponentialSettingsKey =     "Exponential";
26 27 28 29 30 31 32 33

const char* Joystick::_rgFunctionSettingsKey[Joystick::maxFunction] = {
    "RollAxis",
    "PitchAxis",
    "YawAxis",
    "ThrottleAxis"
};

34
Joystick::Joystick(const QString& name, int axisCount, int buttonCount, int hatCount, MultiVehicleManager* multiVehicleManager)
Gregory Dymarek's avatar
Gregory Dymarek committed
35
    : _exitThread(false)
36 37 38
    , _name(name)
    , _axisCount(axisCount)
    , _buttonCount(buttonCount)
39 40 41
    , _hatCount(hatCount)
    , _hatButtonCount(4*hatCount)
    , _totalButtonCount(_buttonCount+_hatButtonCount)
Don Gagne's avatar
Don Gagne committed
42
    , _calibrationMode(CalibrationModeOff)
43 44 45
    , _rgAxisValues(NULL)
    , _rgCalibration(NULL)
    , _rgButtonValues(NULL)
46 47
    , _lastButtonBits(0)
    , _throttleMode(ThrottleModeCenterZero)
48
    , _exponential(false)
49 50
    , _activeVehicle(NULL)
    , _pollingStartedForCalibration(false)
51
    , _multiVehicleManager(multiVehicleManager)
52
{
53

54 55
    _rgAxisValues = new int[_axisCount];
    _rgCalibration = new Calibration_t[_axisCount];
56
    _rgButtonValues = new bool[_totalButtonCount];
57 58

    for (int i=0; i<_axisCount; i++) {
59 60
        _rgAxisValues[i] = 0;
    }
61
    for (int i=0; i<_totalButtonCount; i++) {
62 63
        _rgButtonValues[i] = false;
    }
64

65 66 67 68 69
    _loadSettings();
}

Joystick::~Joystick()
{
70 71 72
    delete _rgAxisValues;
    delete _rgCalibration;
    delete _rgButtonValues;
73 74 75 76 77
}

void Joystick::_loadSettings(void)
{
    QSettings   settings;
78

79 80
    settings.beginGroup(_settingsGroup);
    settings.beginGroup(_name);
81

82 83
    bool badSettings = false;
    bool convertOk;
84

85
    qCDebug(JoystickLog) << "_loadSettings " << _name;
86

87
    _calibrated = settings.value(_calibratedSettingsKey, false).toBool();
88
    _exponential = settings.value(_exponentialSettingsKey, false).toBool();
89

90 91
    _throttleMode = (ThrottleMode_t)settings.value(_throttleModeSettingsKey, ThrottleModeCenterZero).toInt(&convertOk);
    badSettings |= !convertOk;
92

93
    qCDebug(JoystickLog) << "_loadSettings calibrated:throttlemode:exponential:badsettings" << _calibrated << _throttleMode << _exponential << badSettings;
94

95 96 97 98
    QString minTpl  ("Axis%1Min");
    QString maxTpl  ("Axis%1Max");
    QString trimTpl ("Axis%1Trim");
    QString revTpl  ("Axis%1Rev");
99

100
    for (int axis=0; axis<_axisCount; axis++) {
101
        Calibration_t* calibration = &_rgCalibration[axis];
102

103 104
        calibration->center = settings.value(trimTpl.arg(axis), 0).toInt(&convertOk);
        badSettings |= !convertOk;
105

106 107
        calibration->min = settings.value(minTpl.arg(axis), -32768).toInt(&convertOk);
        badSettings |= !convertOk;
108

109
        calibration->max = settings.value(maxTpl.arg(axis), 32767).toInt(&convertOk);
110
        badSettings |= !convertOk;
111

112
        calibration->reversed = settings.value(revTpl.arg(axis), false).toBool();
113

114 115
        qCDebug(JoystickLog) << "_loadSettings axis:min:max:trim:reversed:badsettings" << axis << calibration->min << calibration->max << calibration->center << calibration->reversed << badSettings;
    }
116

117 118
    for (int function=0; function<maxFunction; function++) {
        int functionAxis;
119

120 121
        functionAxis = settings.value(_rgFunctionSettingsKey[function], -1).toInt(&convertOk);
        badSettings |= !convertOk || (functionAxis == -1);
122

123
        _rgFunctionAxis[function] = functionAxis;
124

125 126
        qCDebug(JoystickLog) << "_loadSettings function:axis:badsettings" << function << functionAxis << badSettings;
    }
127

Don Gagne's avatar
Don Gagne committed
128
    for (int button=0; button<_totalButtonCount; button++) {
129
        _rgButtonActions << settings.value(QString(_buttonActionSettingsKey).arg(button), QString()).toString();
Don Gagne's avatar
Don Gagne committed
130
        qCDebug(JoystickLog) << "_loadSettings button:action" << button << _rgButtonActions[button];
131
    }
132

133 134 135 136 137 138 139 140 141
    if (badSettings) {
        _calibrated = false;
        settings.setValue(_calibratedSettingsKey, false);
    }
}

void Joystick::_saveSettings(void)
{
    QSettings settings;
142

143 144
    settings.beginGroup(_settingsGroup);
    settings.beginGroup(_name);
145

146
    settings.setValue(_calibratedSettingsKey, _calibrated);
147
    settings.setValue(_exponentialSettingsKey, _exponential);
148
    settings.setValue(_throttleModeSettingsKey, _throttleMode);
149

150
    qCDebug(JoystickLog) << "_saveSettings calibrated:throttlemode" << _calibrated << _throttleMode;
151 152 153 154 155

    QString minTpl  ("Axis%1Min");
    QString maxTpl  ("Axis%1Max");
    QString trimTpl ("Axis%1Trim");
    QString revTpl  ("Axis%1Rev");
156

157
    for (int axis=0; axis<_axisCount; axis++) {
158
        Calibration_t* calibration = &_rgCalibration[axis];
159

160 161 162 163
        settings.setValue(trimTpl.arg(axis), calibration->center);
        settings.setValue(minTpl.arg(axis), calibration->min);
        settings.setValue(maxTpl.arg(axis), calibration->max);
        settings.setValue(revTpl.arg(axis), calibration->reversed);
164

165 166 167 168 169 170 171 172
        qCDebug(JoystickLog) << "_saveSettings name:axis:min:max:trim:reversed"
                                << _name
                                << axis
                                << calibration->min
                                << calibration->max
                                << calibration->center
                                << calibration->reversed;
    }
173

174 175 176 177
    for (int function=0; function<maxFunction; function++) {
        settings.setValue(_rgFunctionSettingsKey[function], _rgFunctionAxis[function]);
        qCDebug(JoystickLog) << "_saveSettings name:function:axis" << _name << function << _rgFunctionSettingsKey[function];
    }
178

Don Gagne's avatar
Don Gagne committed
179
    for (int button=0; button<_totalButtonCount; button++) {
180 181 182 183 184 185 186 187 188 189 190
        settings.setValue(QString(_buttonActionSettingsKey).arg(button), _rgButtonActions[button]);
        qCDebug(JoystickLog) << "_saveSettings button:action" << button << _rgButtonActions[button];
    }
}

/// Adjust the raw axis value to the -1:1 range given calibration information
float Joystick::_adjustRange(int value, Calibration_t calibration)
{
    float valueNormalized;
    float axisLength;
    float axisBasis;
191

192 193 194 195 196 197 198 199 200
    if (value > calibration.center) {
        axisBasis = 1.0f;
        valueNormalized = value - calibration.center;
        axisLength =  calibration.max - calibration.center;
    } else {
        axisBasis = -1.0f;
        valueNormalized = calibration.center - value;
        axisLength =  calibration.center - calibration.min;
    }
201

202
    float axisPercent = valueNormalized / axisLength;
203

204
    float correctedValue = axisBasis * axisPercent;
205

206 207 208
    if (calibration.reversed) {
        correctedValue *= -1.0f;
    }
209

210 211 212 213 214 215 216 217 218 219 220 221 222
#if 0
    qCDebug(JoystickLog) << "_adjustRange corrected:value:min:max:center:reversed:basis:normalized:length"
                            << correctedValue
                            << value
                            << calibration.min
                            << calibration.max
                            << calibration.center
                            << calibration.center
                            << axisBasis
                            << valueNormalized
                            << axisLength;
#endif

223
    return std::max(-1.0f, std::min(correctedValue, 1.0f));
224 225 226 227 228
}


void Joystick::run(void)
{
Gregory Dymarek's avatar
Gregory Dymarek committed
229
    _open();
230

231
    while (!_exitThread) {
Gregory Dymarek's avatar
Gregory Dymarek committed
232
    _update();
233 234 235

        // Update axes
        for (int axisIndex=0; axisIndex<_axisCount; axisIndex++) {
Gregory Dymarek's avatar
Gregory Dymarek committed
236
            int newAxisValue = _getAxis(axisIndex);
237 238 239 240
            // Calibration code requires signal to be emitted even if value hasn't changed
            _rgAxisValues[axisIndex] = newAxisValue;
            emit rawAxisValueChanged(axisIndex, newAxisValue);
        }
241

242 243
        // Update buttons
        for (int buttonIndex=0; buttonIndex<_buttonCount; buttonIndex++) {
Gregory Dymarek's avatar
Gregory Dymarek committed
244
            bool newButtonValue = _getButton(buttonIndex);
245 246 247 248 249
            if (newButtonValue != _rgButtonValues[buttonIndex]) {
                _rgButtonValues[buttonIndex] = newButtonValue;
                emit rawButtonPressedChanged(buttonIndex, newButtonValue);
            }
        }
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

        // Update hat - append hat buttons to the end of the normal button list
        int numHatButtons = 4;
        for (int hatIndex=0; hatIndex<_hatCount; hatIndex++) {
            for (int hatButtonIndex=0; hatButtonIndex<numHatButtons; hatButtonIndex++) {
                // Create new index value that includes the normal button list
                int rgButtonValueIndex = hatIndex*numHatButtons + hatButtonIndex + _buttonCount;
                // Get hat value from joystick
                bool newButtonValue = _getHat(hatIndex,hatButtonIndex);
                if (newButtonValue != _rgButtonValues[rgButtonValueIndex]) {
                    _rgButtonValues[rgButtonValueIndex] = newButtonValue;
                    emit rawButtonPressedChanged(rgButtonValueIndex, newButtonValue);
                }
            }
        }
265

Don Gagne's avatar
Don Gagne committed
266
        if (_calibrationMode != CalibrationModeCalibrating) {
267 268
            int     axis = _rgFunctionAxis[rollFunction];
            float   roll = _adjustRange(_rgAxisValues[axis], _rgCalibration[axis]);
269

270 271
                    axis = _rgFunctionAxis[pitchFunction];
            float   pitch = _adjustRange(_rgAxisValues[axis], _rgCalibration[axis]);
272

273 274
                    axis = _rgFunctionAxis[yawFunction];
            float   yaw = _adjustRange(_rgAxisValues[axis], _rgCalibration[axis]);
275

276 277 278
                    axis = _rgFunctionAxis[throttleFunction];
            float   throttle = _adjustRange(_rgAxisValues[axis], _rgCalibration[axis]);

279 280 281 282 283
            float roll_limited = std::max(static_cast<float>(-M_PI_4), std::min(roll, static_cast<float>(M_PI_4)));
            float pitch_limited = std::max(static_cast<float>(-M_PI_4), std::min(pitch, static_cast<float>(M_PI_4)));
            float yaw_limited = std::max(static_cast<float>(-M_PI_4), std::min(yaw, static_cast<float>(M_PI_4)));
            float throttle_limited = std::max(static_cast<float>(-M_PI_4), std::min(throttle, static_cast<float>(M_PI_4)));

284
            // Map from unit circle to linear range and limit
285 286 287 288
            roll =      std::max(-1.0f, std::min(tanf(asinf(roll_limited)), 1.0f));
            pitch =     std::max(-1.0f, std::min(tanf(asinf(pitch_limited)), 1.0f));
            yaw =       std::max(-1.0f, std::min(tanf(asinf(yaw_limited)), 1.0f));
            throttle =  std::max(-1.0f, std::min(tanf(asinf(throttle_limited)), 1.0f));
289
            
290 291 292 293 294 295 296 297 298 299 300
            if ( _exponential ) {
                // Exponential (0% to -50% range like most RC radios)
                // 0 for no exponential
                // -0.5 for strong exponential
                float expo = -0.35f;

                // Calculate new RPY with exponential applied
                roll =      -expo*powf(roll,3) + (1+expo)*roll;
                pitch =     -expo*powf(pitch,3) + (1+expo)*pitch;
                yaw =       -expo*powf(yaw,3) + (1+expo)*yaw;
            }
301

302
            // Adjust throttle to 0:1 range
303
            if (_throttleMode == ThrottleModeCenterZero && _activeVehicle->supportsThrottleModeCenterZero()) {
304
                throttle = std::max(0.0f, throttle);
305
            } else {
306 307
                throttle = (throttle + 1.0f) / 2.0f;
            }
308

309
            // Set up button pressed information
310

311
            // We only send the buttons the firmwware has reserved
312
            int reservedButtonCount = _activeVehicle->manualControlReservedButtonCount();
313
            if (reservedButtonCount == -1) {
314
                reservedButtonCount = _totalButtonCount;
315
            }
316

317 318
            quint16 newButtonBits = 0;      // New set of button which are down
            quint16 buttonPressedBits = 0;  // Buttons pressed for manualControl signal
319

320
            for (int buttonIndex=0; buttonIndex<_totalButtonCount; buttonIndex++) {
321
                quint16 buttonBit = 1 << buttonIndex;
322

323 324
                if (!_rgButtonValues[buttonIndex]) {
                    // Button up, just record it
325 326 327
                    newButtonBits |= buttonBit;
                } else {
                    if (_lastButtonBits & buttonBit) {
328
                        // Button was up last time through, but is now down which indicates a button press
329
                        qCDebug(JoystickLog) << "button triggered" << buttonIndex;
330

331 332
                        if (buttonIndex >= reservedButtonCount) {
                            // Button is above firmware reserved set
Don Gagne's avatar
Don Gagne committed
333 334 335
                            QString buttonAction =_rgButtonActions[buttonIndex];
                            if (!buttonAction.isEmpty()) {
                                _buttonAction(buttonAction);
336 337 338
                            }
                        }
                    }
339 340

                    // Mark the button as pressed as long as its pressed
341
                    buttonPressedBits |= buttonBit;
342 343
                }
            }
344

345
            _lastButtonBits = newButtonBits;
346

347
            qCDebug(JoystickValuesLog) << "name:roll:pitch:yaw:throttle" << name() << roll << -pitch << yaw << throttle;
348

349
            emit manualControl(roll, -pitch, yaw, throttle, buttonPressedBits, _activeVehicle->joystickMode());
350
        }
351

352 353 354
        // Sleep, update rate of joystick is approx. 25 Hz (1000 ms / 25 = 40 ms)
        QGC::SLEEP::msleep(40);
    }
355

Gregory Dymarek's avatar
Gregory Dymarek committed
356
    _close();
357 358
}

359
void Joystick::startPolling(Vehicle* vehicle)
360
{
361 362 363 364 365 366
    if (vehicle) {

        // If a vehicle is connected, disconnect it
        if (_activeVehicle) {
            UAS* uas = _activeVehicle->uas();
            disconnect(this, &Joystick::manualControl, uas, &UAS::setExternalControlSetpoint);
367
        }
368 369

        // Always set up the new vehicle
370
        _activeVehicle = vehicle;
371 372 373 374 375 376 377 378 379 380 381 382 383 384

        // Only connect the new vehicle if it wants joystick data
        if (vehicle->joystickEnabled()) {
            _pollingStartedForCalibration = false;

            UAS* uas = _activeVehicle->uas();
            connect(this, &Joystick::manualControl, uas, &UAS::setExternalControlSetpoint);
            // FIXME: ****
            //connect(this, &Joystick::buttonActionTriggered, uas, &UAS::triggerAction);
        }
    }


    if (!isRunning()) {
385 386
        _exitThread = false;
        start();
387 388 389 390 391
    }
}

void Joystick::stopPolling(void)
{
392
    if (isRunning()) {
393 394 395 396 397 398

        if (_activeVehicle && _activeVehicle->joystickEnabled()) {
            UAS* uas = _activeVehicle->uas();

            disconnect(this, &Joystick::manualControl,          uas, &UAS::setExternalControlSetpoint);
        }
Don Gagne's avatar
Don Gagne committed
399 400
        // FIXME: ****
        //disconnect(this, &Joystick::buttonActionTriggered,  uas, &UAS::triggerAction);
401

402 403
        _exitThread = true;
        }
404 405 406 407
}

void Joystick::setCalibration(int axis, Calibration_t& calibration)
{
408
    if (!_validAxis(axis)) {
409 410 411
        qCWarning(JoystickLog) << "Invalid axis index" << axis;
        return;
    }
412

413 414 415 416 417 418 419 420
    _calibrated = true;
    _rgCalibration[axis] = calibration;
    _saveSettings();
    emit calibratedChanged(_calibrated);
}

Joystick::Calibration_t Joystick::getCalibration(int axis)
{
421
    if (!_validAxis(axis)) {
422 423
        qCWarning(JoystickLog) << "Invalid axis index" << axis;
    }
424

425 426 427 428 429
    return _rgCalibration[axis];
}

void Joystick::setFunctionAxis(AxisFunction_t function, int axis)
{
430
    if (!_validAxis(axis)) {
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
        qCWarning(JoystickLog) << "Invalid axis index" << axis;
        return;
    }

    _calibrated = true;
    _rgFunctionAxis[function] = axis;
    _saveSettings();
    emit calibratedChanged(_calibrated);
}

int Joystick::getFunctionAxis(AxisFunction_t function)
{
    if (function < 0 || function >= maxFunction) {
        qCWarning(JoystickLog) << "Invalid function" << function;
    }

    return _rgFunctionAxis[function];
}

QStringList Joystick::actions(void)
{
    QStringList list;
Don Gagne's avatar
Don Gagne committed
453 454

    list << "Arm" << "Disarm";
455 456 457 458

    if (_activeVehicle) {
        list << _activeVehicle->flightModes();
    }
459

460 461 462
    return list;
}

Don Gagne's avatar
Don Gagne committed
463
void Joystick::setButtonAction(int button, const QString& action)
464
{
465
    if (!_validButton(button)) {
466 467 468
        qCWarning(JoystickLog) << "Invalid button index" << button;
        return;
    }
469

Don Gagne's avatar
Don Gagne committed
470
    qDebug() << "setButtonAction" << action;
471

472 473 474 475 476
    _rgButtonActions[button] = action;
    _saveSettings();
    emit buttonActionsChanged(buttonActions());
}

Don Gagne's avatar
Don Gagne committed
477
QString Joystick::getButtonAction(int button)
478
{
479
    if (!_validButton(button)) {
480 481
        qCWarning(JoystickLog) << "Invalid button index" << button;
    }
482

483 484 485 486 487 488
    return _rgButtonActions[button];
}

QVariantList Joystick::buttonActions(void)
{
    QVariantList list;
489

Don Gagne's avatar
Don Gagne committed
490
    for (int button=0; button<_totalButtonCount; button++) {
491 492
        list += QVariant::fromValue(_rgButtonActions[button]);
    }
493

494 495 496 497 498 499 500 501 502 503 504 505 506 507
    return list;
}

int Joystick::throttleMode(void)
{
    return _throttleMode;
}

void Joystick::setThrottleMode(int mode)
{
    if (mode < 0 || mode >= ThrottleModeMax) {
        qCWarning(JoystickLog) << "Invalid throttle mode" << mode;
        return;
    }
508

509 510 511 512 513
    _throttleMode = (ThrottleMode_t)mode;
    _saveSettings();
    emit throttleModeChanged(_throttleMode);
}

514 515 516 517 518 519 520 521 522 523 524 525 526
bool Joystick::exponential(void)
{
    return _exponential;
}

void Joystick::setExponential(bool expo)
{
    _exponential = expo;

    _saveSettings();
    emit exponentialChanged(_exponential);
}

Don Gagne's avatar
Don Gagne committed
527
void Joystick::startCalibrationMode(CalibrationMode_t mode)
528
{
Don Gagne's avatar
Don Gagne committed
529 530 531 532
    if (mode == CalibrationModeOff) {
        qWarning() << "Incorrect mode CalibrationModeOff";
        return;
    }
533

Don Gagne's avatar
Don Gagne committed
534
    _calibrationMode = mode;
535

536 537
    if (!isRunning()) {
        _pollingStartedForCalibration = true;
538
        startPolling(_multiVehicleManager->activeVehicle());
539
    }
540 541
}

Don Gagne's avatar
Don Gagne committed
542
void Joystick::stopCalibrationMode(CalibrationMode_t mode)
543
{
Don Gagne's avatar
Don Gagne committed
544 545 546 547
    if (mode == CalibrationModeOff) {
        qWarning() << "Incorrect mode: CalibrationModeOff";
        return;
    }
548

Don Gagne's avatar
Don Gagne committed
549 550 551 552
    if (mode == CalibrationModeCalibrating) {
        _calibrationMode = CalibrationModeMonitor;
    } else {
        _calibrationMode = CalibrationModeOff;
553 554 555
        if (_pollingStartedForCalibration) {
            stopPolling();
        }
556 557
    }
}
558

Don Gagne's avatar
Don Gagne committed
559 560
void Joystick::_buttonAction(const QString& action)
{
561 562 563 564
    if (!_activeVehicle || !_activeVehicle->joystickEnabled()) {
        return;
    }

Don Gagne's avatar
Don Gagne committed
565 566 567 568
    if (action == "Arm") {
        _activeVehicle->setArmed(true);
    } else if (action == "Disarm") {
        _activeVehicle->setArmed(false);
569 570
    } else if (_activeVehicle->flightModes().contains(action)) {
        _activeVehicle->setFlightMode(action);
Don Gagne's avatar
Don Gagne committed
571 572 573 574 575
    } else {
        qCDebug(JoystickLog) << "_buttonAction unknown action:" << action;
    }
}

576 577 578 579 580 581 582
bool Joystick::_validAxis(int axis)
{
    return axis >= 0 && axis < _axisCount;
}

bool Joystick::_validButton(int button)
{
583
    return button >= 0 && button < _totalButtonCount;
584 585
}