APMFlightModesComponentController.cc 6.43 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.
 *
 ****************************************************************************/

Don Gagne's avatar
Don Gagne committed
10 11 12 13 14 15 16

#include "APMFlightModesComponentController.h"
#include "QGCMAVLink.h"

#include <QVariant>
#include <QQmlProperty>

17 18
bool APMFlightModesComponentController::_typeRegistered = false;

19 20 21
const char* APMFlightModesComponentController::_simpleParamName =       "SIMPLE";
const char* APMFlightModesComponentController::_superSimpleParamName =  "SUPER_SIMPLE";

Don Gagne's avatar
Don Gagne committed
22
APMFlightModesComponentController::APMFlightModesComponentController(void)
23
    : _activeFlightMode     (0)
24 25
    , _channelCount         (Vehicle::cMaxRcChannels)
    , _simpleMode           (SimpleModeStandard)
26 27
    , _simpleModeFact       (parameterExists(-1, _simpleParamName)      ? getParameterFact(-1, _simpleParamName) : nullptr)
    , _superSimpleModeFact  (parameterExists(-1, _superSimpleParamName) ? getParameterFact(-1, _superSimpleParamName) : nullptr)
28
    , _simpleModesSupported (_simpleModeFact && _superSimpleModeFact)
Don Gagne's avatar
Don Gagne committed
29
{
30 31 32 33
    if (!_typeRegistered) {
        qmlRegisterUncreatableType<APMFlightModesComponentController>("QGroundControl.Controllers", 1, 0, "APMFlightModesComponentController", "Reference only");
    }

Don Gagne's avatar
Don Gagne committed
34 35
    _modeParamPrefix = _vehicle->rover() ? QStringLiteral("MODE") : QStringLiteral("FLTMODE");
    _modeChannelParam = _vehicle->rover() ? QStringLiteral("MODE_CH") : QStringLiteral("FLTMODE_CH");
Don Gagne's avatar
Don Gagne committed
36

37 38 39 40 41 42
    _simpleModeNames << tr("Off") << tr("Simple") << tr("Super-Simple") << tr("Custom");
    for (int i=0; i<_cFltModes; i++) {
        _simpleModeEnabled.append(QVariant(false));
        _superSimpleModeEnabled.append(QVariant(false));
    }

43
    if (_simpleModesSupported) {
44
        _setupSimpleModeEnabled();
45 46 47 48 49 50 51 52 53 54 55 56 57

        uint8_t simpleModeValue = static_cast<uint8_t>(_simpleModeFact->rawValue().toUInt());
        uint8_t superSimpleModeValue = static_cast<uint8_t>(_superSimpleModeFact->rawValue().toUInt());
        if (simpleModeValue == 0 && superSimpleModeValue == 0) {
            _simpleMode = SimpleModeStandard;
        } else if (simpleModeValue == _allSimpleBits && superSimpleModeValue == 0) {
            _simpleMode = SimpleModeSimple;
        } else if (simpleModeValue == 0 && superSimpleModeValue == _allSimpleBits) {
            _simpleMode = SimpleModeSuperSimple;
        } else {
            _simpleMode = SimpleModeCustom;
        }

58
        connect(this, &APMFlightModesComponentController::simpleModeChanged, this, &APMFlightModesComponentController::_updateSimpleParamsFromSimpleMode);
59 60
    }

Don Gagne's avatar
Don Gagne committed
61
    QStringList usedParams;
Don Gagne's avatar
Don Gagne committed
62 63 64
    for (int i=1; i<7; i++) {
        usedParams << QStringLiteral("%1%2").arg(_modeParamPrefix).arg(i);
    }
Don Gagne's avatar
Don Gagne committed
65 66 67 68
    if (!_allParametersExists(FactSystem::defaultComponentId, usedParams)) {
        return;
    }

69 70 71 72
    for (int i=0; i<_cChannelOptions; i++) {
        _rgChannelOptionEnabled.append(QVariant(false));
    }

Don Gagne's avatar
Don Gagne committed
73 74 75
    connect(_vehicle, &Vehicle::rcChannelsChanged, this, &APMFlightModesComponentController::_rcChannelsChanged);
}

Don Gagne's avatar
Don Gagne committed
76 77
/// Connected to Vehicle::rcChannelsChanged signal
void APMFlightModesComponentController::_rcChannelsChanged(int channelCount, int pwmValues[Vehicle::cMaxRcChannels])
Don Gagne's avatar
Don Gagne committed
78
{
79 80
    int flightModeChannel = 4;

Don Gagne's avatar
Don Gagne committed
81 82
    if (parameterExists(FactSystem::defaultComponentId, _modeChannelParam)) {
        flightModeChannel = getParameterFact(FactSystem::defaultComponentId, _modeChannelParam)->rawValue().toInt() - 1;
83 84 85
    }

    if (flightModeChannel >= channelCount) {
86 87
        return;
    }
Don Gagne's avatar
Don Gagne committed
88

Don Gagne's avatar
Don Gagne committed
89
    _activeFlightMode = 0;
90
    int channelValue = pwmValues[flightModeChannel];
Don Gagne's avatar
Don Gagne committed
91
    if (channelValue != -1) {
Don Gagne's avatar
Don Gagne committed
92
        bool found = false;
Don Gagne's avatar
Don Gagne committed
93 94 95 96
        int rgThreshold[] = { 1230, 1360, 1490, 1620, 1749 };
        for (int i=0; i<5; i++) {
            if (channelValue <= rgThreshold[i]) {
                _activeFlightMode = i + 1;
Don Gagne's avatar
Don Gagne committed
97
                found = true;
Don Gagne's avatar
Don Gagne committed
98
                break;
Don Gagne's avatar
Don Gagne committed
99 100
            }
        }
Don Gagne's avatar
Don Gagne committed
101 102 103
        if (!found) {
            _activeFlightMode = 6;
        }
Don Gagne's avatar
Don Gagne committed
104
    }
Don Gagne's avatar
Don Gagne committed
105
    emit activeFlightModeChanged(_activeFlightMode);
Don Gagne's avatar
Don Gagne committed
106

107
    for (int i=0; i<_cChannelOptions; i++) {
Don Gagne's avatar
Don Gagne committed
108
        _rgChannelOptionEnabled[i] = QVariant(false);
Don Gagne's avatar
Don Gagne committed
109
        channelValue = pwmValues[i+6];
110
        if (channelValue > 1800) {
Don Gagne's avatar
Don Gagne committed
111 112
            _rgChannelOptionEnabled[i] = QVariant(true);
        }
Don Gagne's avatar
Don Gagne committed
113
    }
Don Gagne's avatar
Don Gagne committed
114
    emit channelOptionEnabledChanged();
Don Gagne's avatar
Don Gagne committed
115
}
116 117 118 119 120 121 122 123 124 125 126 127

void APMFlightModesComponentController::_updateSimpleParamsFromSimpleMode(void)
{
    int newSimpleModeValue = 0;
    int newSuperSimpleModeValue = 0;

    if (_simpleMode == SimpleModeSimple) {
        newSimpleModeValue = _allSimpleBits;
    } else if (_simpleMode == SimpleModeSuperSimple) {
        newSuperSimpleModeValue = _allSimpleBits;
    }

128 129 130 131 132 133 134
    for (int i=0; i<_cFltModes; i++) {
        _simpleModeEnabled[i] =         false;
        _superSimpleModeEnabled[i] =    false;
    }
    emit simpleModeEnabledChanged();
    emit superSimpleModeEnabledChanged();

135 136 137 138 139 140 141
    _simpleModeFact->setRawValue(newSimpleModeValue);
    _superSimpleModeFact->setRawValue(newSuperSimpleModeValue);
}

void APMFlightModesComponentController::setSimpleMode(int fltModeIndex, bool enabled)
{
    if (fltModeIndex < _cFltModes) {
142 143 144 145 146
        uint8_t mode = static_cast<uint8_t>(_simpleModeFact->rawValue().toInt());
        if (enabled) {
            mode |= 1 << fltModeIndex;
        } else {
            mode &= ~(1 << fltModeIndex);
147
        }
148
        _simpleModeFact->setRawValue(mode);
149 150 151 152 153 154
    }
}

void APMFlightModesComponentController::setSuperSimpleMode(int fltModeIndex, bool enabled)
{
    if (fltModeIndex < _cFltModes) {
155 156 157 158 159
        uint8_t mode = static_cast<uint8_t>(_superSimpleModeFact->rawValue().toInt());
        if (enabled) {
            mode |= 1 << fltModeIndex;
        } else {
            mode &= ~(1 << fltModeIndex);
160
        }
161
        _superSimpleModeFact->setRawValue(mode);
162 163 164
    }
}

165
void APMFlightModesComponentController::_setupSimpleModeEnabled(void)
166 167 168 169
{
    uint8_t simpleMode =        static_cast<uint8_t>(_simpleModeFact->rawValue().toUInt());
    uint8_t superSimpleMode =   static_cast<uint8_t>(_superSimpleModeFact->rawValue().toUInt());

170 171 172 173
    for (int i=0; i<_cFltModes; i++) {
        uint8_t bitSet = static_cast<uint8_t>(1 << i);
        _simpleModeEnabled[i] = !!(simpleMode & bitSet);
        _superSimpleModeEnabled[i] = !!(superSimpleMode & bitSet);
174 175 176 177 178
    }

    emit simpleModeEnabledChanged();
    emit superSimpleModeEnabledChanged();
}