APMFlightModesComponentController.cc 2.54 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 17 18

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

#include <QVariant>
#include <QQmlProperty>

APMFlightModesComponentController::APMFlightModesComponentController(void)
Don Gagne's avatar
Don Gagne committed
19 20
    : _activeFlightMode(0)
    , _channelCount(Vehicle::cMaxRcChannels)
Don Gagne's avatar
Don Gagne committed
21
{
Don Gagne's avatar
Don Gagne committed
22 23 24
    _modeParamPrefix = _vehicle->rover() ? "MODE" : "FLTMODE";
    _modeChannelParam = _vehicle->rover() ? "MODE_CH" : "FLTMODE_CH";

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

Don Gagne's avatar
Don Gagne committed
33
    _rgChannelOptionEnabled << QVariant(false) << QVariant(false) << QVariant(false) << QVariant(false) << QVariant(false) << QVariant(false);
Don Gagne's avatar
Don Gagne committed
34 35 36 37
    
    connect(_vehicle, &Vehicle::rcChannelsChanged, this, &APMFlightModesComponentController::_rcChannelsChanged);
}

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

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

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

Don Gagne's avatar
Don Gagne committed
51
    _activeFlightMode = 0;
52
    int channelValue = pwmValues[flightModeChannel];
Don Gagne's avatar
Don Gagne committed
53
    if (channelValue != -1) {
Don Gagne's avatar
Don Gagne committed
54
        bool found = false;
Don Gagne's avatar
Don Gagne committed
55 56 57 58
        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
59
                found = true;
Don Gagne's avatar
Don Gagne committed
60
                break;
Don Gagne's avatar
Don Gagne committed
61 62
            }
        }
Don Gagne's avatar
Don Gagne committed
63 64 65
        if (!found) {
            _activeFlightMode = 6;
        }
Don Gagne's avatar
Don Gagne committed
66
    }
Don Gagne's avatar
Don Gagne committed
67
    emit activeFlightModeChanged(_activeFlightMode);
Don Gagne's avatar
Don Gagne committed
68

Don Gagne's avatar
Don Gagne committed
69 70
    for (int i=0; i<6; i++) {
        _rgChannelOptionEnabled[i] = QVariant(false);
Don Gagne's avatar
Don Gagne committed
71
        channelValue = pwmValues[i+6];
Don Gagne's avatar
Don Gagne committed
72 73 74
        if (channelValue != -1 && channelValue > 1800) {
            _rgChannelOptionEnabled[i] = QVariant(true);
        }
Don Gagne's avatar
Don Gagne committed
75
    }
Don Gagne's avatar
Don Gagne committed
76
    emit channelOptionEnabledChanged();
Don Gagne's avatar
Don Gagne committed
77
}