Skip to content
APMFlightModesComponentController.cc 6.5 KiB
Newer Older
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
 * (c) 2009-2020 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

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

#include <QVariant>
#include <QQmlProperty>

DonLakeFlyer's avatar
 
DonLakeFlyer committed
bool APMFlightModesComponentController::_typeRegistered = false;

DonLakeFlyer's avatar
 
DonLakeFlyer committed
const char* APMFlightModesComponentController::_simpleParamName =       "SIMPLE";
const char* APMFlightModesComponentController::_superSimpleParamName =  "SUPER_SIMPLE";

Don Gagne's avatar
Don Gagne committed
APMFlightModesComponentController::APMFlightModesComponentController(void)
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    : _activeFlightMode     (0)
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    , _channelCount         (Vehicle::cMaxRcChannels)
    , _simpleMode           (SimpleModeStandard)
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    , _simpleModeFact       (parameterExists(-1, _simpleParamName)      ? getParameterFact(-1, _simpleParamName) : nullptr)
    , _superSimpleModeFact  (parameterExists(-1, _superSimpleParamName) ? getParameterFact(-1, _superSimpleParamName) : nullptr)
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    , _simpleModesSupported (_simpleModeFact && _superSimpleModeFact)
Don Gagne's avatar
Don Gagne committed
{
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    if (!_typeRegistered) {
        qmlRegisterUncreatableType<APMFlightModesComponentController>("QGroundControl.Controllers", 1, 0, "APMFlightModesComponentController", "Reference only");
    }

Don Gagne's avatar
 
Don Gagne committed
    bool arduRoverFirmware = parameterExists(-1, QStringLiteral("MODE1"));
    _modeParamPrefix = arduRoverFirmware ? QStringLiteral("MODE") : QStringLiteral("FLTMODE");
    _modeChannelParam = arduRoverFirmware ? QStringLiteral("MODE_CH") : QStringLiteral("FLTMODE_CH");
Don Gagne's avatar
Don Gagne committed

DonLakeFlyer's avatar
 
DonLakeFlyer committed
    _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));
    }

DonLakeFlyer's avatar
 
DonLakeFlyer committed
    if (_simpleModesSupported) {
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        _setupSimpleModeEnabled();
DonLakeFlyer's avatar
 
DonLakeFlyer committed

        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;
        }

DonLakeFlyer's avatar
 
DonLakeFlyer committed
        connect(this, &APMFlightModesComponentController::simpleModeChanged, this, &APMFlightModesComponentController::_updateSimpleParamsFromSimpleMode);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    }

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

DonLakeFlyer's avatar
 
DonLakeFlyer committed
    for (int i=0; i<_cChannelOptions; i++) {
        _rgChannelOptionEnabled.append(QVariant(false));
    }

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

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

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

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

Don Gagne's avatar
Don Gagne committed
    _activeFlightMode = 0;
    int channelValue = pwmValues[flightModeChannel];
Don Gagne's avatar
Don Gagne committed
    if (channelValue != -1) {
Don Gagne's avatar
Don Gagne committed
        bool found = false;
Don Gagne's avatar
Don Gagne committed
        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
                found = true;
Don Gagne's avatar
Don Gagne committed
                break;
Don Gagne's avatar
Don Gagne committed
            }
        }
Don Gagne's avatar
Don Gagne committed
        if (!found) {
            _activeFlightMode = 6;
        }
Don Gagne's avatar
Don Gagne committed
    }
Don Gagne's avatar
Don Gagne committed
    emit activeFlightModeChanged(_activeFlightMode);
Don Gagne's avatar
Don Gagne committed

DonLakeFlyer's avatar
 
DonLakeFlyer committed
    for (int i=0; i<_cChannelOptions; i++) {
Don Gagne's avatar
Don Gagne committed
        _rgChannelOptionEnabled[i] = QVariant(false);
Don Gagne's avatar
Don Gagne committed
        channelValue = pwmValues[i+6];
Don Gagne's avatar
Don Gagne committed
            _rgChannelOptionEnabled[i] = QVariant(true);
        }
Don Gagne's avatar
Don Gagne committed
    }
Don Gagne's avatar
Don Gagne committed
    emit channelOptionEnabledChanged();
Don Gagne's avatar
Don Gagne committed
}
DonLakeFlyer's avatar
 
DonLakeFlyer committed

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

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

DonLakeFlyer's avatar
 
DonLakeFlyer committed
    for (int i=0; i<_cFltModes; i++) {
        _simpleModeEnabled[i] =         false;
        _superSimpleModeEnabled[i] =    false;
    }
    emit simpleModeEnabledChanged();
    emit superSimpleModeEnabledChanged();

DonLakeFlyer's avatar
 
DonLakeFlyer committed
    _simpleModeFact->setRawValue(newSimpleModeValue);
    _superSimpleModeFact->setRawValue(newSuperSimpleModeValue);
}

void APMFlightModesComponentController::setSimpleMode(int fltModeIndex, bool enabled)
{
    if (fltModeIndex < _cFltModes) {
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        uint8_t mode = static_cast<uint8_t>(_simpleModeFact->rawValue().toInt());
        if (enabled) {
            mode |= 1 << fltModeIndex;
        } else {
            mode &= ~(1 << fltModeIndex);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        }
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        _simpleModeFact->setRawValue(mode);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    }
}

void APMFlightModesComponentController::setSuperSimpleMode(int fltModeIndex, bool enabled)
{
    if (fltModeIndex < _cFltModes) {
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        uint8_t mode = static_cast<uint8_t>(_superSimpleModeFact->rawValue().toInt());
        if (enabled) {
            mode |= 1 << fltModeIndex;
        } else {
            mode &= ~(1 << fltModeIndex);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        }
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        _superSimpleModeFact->setRawValue(mode);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    }
}

DonLakeFlyer's avatar
 
DonLakeFlyer committed
void APMFlightModesComponentController::_setupSimpleModeEnabled(void)
DonLakeFlyer's avatar
 
DonLakeFlyer committed
{
    uint8_t simpleMode =        static_cast<uint8_t>(_simpleModeFact->rawValue().toUInt());
    uint8_t superSimpleMode =   static_cast<uint8_t>(_superSimpleModeFact->rawValue().toUInt());

DonLakeFlyer's avatar
 
DonLakeFlyer committed
    for (int i=0; i<_cFltModes; i++) {
        uint8_t bitSet = static_cast<uint8_t>(1 << i);
        _simpleModeEnabled[i] = !!(simpleMode & bitSet);
        _superSimpleModeEnabled[i] = !!(superSimpleMode & bitSet);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    }

    emit simpleModeEnabledChanged();
    emit superSimpleModeEnabledChanged();
}