APMFlightModesComponentController.cc 3.18 KB
Newer Older
Don Gagne's avatar
Don Gagne 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
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009, 2015 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL 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.
 
 QGROUNDCONTROL 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 QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

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

#include <QVariant>
#include <QQmlProperty>

APMFlightModesComponentController::APMFlightModesComponentController(void)
Don Gagne's avatar
Don Gagne committed
32 33 34
    : _activeFlightMode(0)
    , _channelCount(Vehicle::cMaxRcChannels)
    , _fixedWing(_vehicle->vehicleType() == MAV_TYPE_FIXED_WING)
Don Gagne's avatar
Don Gagne committed
35 36
{
    QStringList usedParams;
37 38
    usedParams << QStringLiteral("FLTMODE1") << QStringLiteral("FLTMODE2") << QStringLiteral("FLTMODE3")
               << QStringLiteral("FLTMODE4") << QStringLiteral("FLTMODE5") << QStringLiteral("FLTMODE6");
Don Gagne's avatar
Don Gagne committed
39 40 41 42
    if (!_allParametersExists(FactSystem::defaultComponentId, usedParams)) {
        return;
    }

Don Gagne's avatar
Don Gagne committed
43
    _rgChannelOptionEnabled << QVariant(false) << QVariant(false) << QVariant(false) << QVariant(false) << QVariant(false) << QVariant(false);
Don Gagne's avatar
Don Gagne committed
44 45 46 47
    
    connect(_vehicle, &Vehicle::rcChannelsChanged, this, &APMFlightModesComponentController::_rcChannelsChanged);
}

Don Gagne's avatar
Don Gagne committed
48 49
/// Connected to Vehicle::rcChannelsChanged signal
void APMFlightModesComponentController::_rcChannelsChanged(int channelCount, int pwmValues[Vehicle::cMaxRcChannels])
Don Gagne's avatar
Don Gagne committed
50
{
51 52 53 54 55 56 57
    int flightModeChannel = 4;

    if (parameterExists(FactSystem::defaultComponentId, QStringLiteral("FLTMODE_CH"))) {
        flightModeChannel = getParameterFact(FactSystem::defaultComponentId, QStringLiteral("FLTMODE_CH"))->rawValue().toInt() - 1;
    }

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

Don Gagne's avatar
Don Gagne committed
61
    _activeFlightMode = 0;
62
    int channelValue = pwmValues[flightModeChannel];
Don Gagne's avatar
Don Gagne committed
63
    if (channelValue != -1) {
Don Gagne's avatar
Don Gagne committed
64
        bool found = false;
Don Gagne's avatar
Don Gagne committed
65 66 67 68
        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
69
                found = true;
Don Gagne's avatar
Don Gagne committed
70
                break;
Don Gagne's avatar
Don Gagne committed
71 72
            }
        }
Don Gagne's avatar
Don Gagne committed
73 74 75
        if (!found) {
            _activeFlightMode = 6;
        }
Don Gagne's avatar
Don Gagne committed
76
    }
Don Gagne's avatar
Don Gagne committed
77
    emit activeFlightModeChanged(_activeFlightMode);
Don Gagne's avatar
Don Gagne committed
78

Don Gagne's avatar
Don Gagne committed
79 80
    for (int i=0; i<6; i++) {
        _rgChannelOptionEnabled[i] = QVariant(false);
Don Gagne's avatar
Don Gagne committed
81
        channelValue = pwmValues[i+6];
Don Gagne's avatar
Don Gagne committed
82 83 84
        if (channelValue != -1 && channelValue > 1800) {
            _rgChannelOptionEnabled[i] = QVariant(true);
        }
Don Gagne's avatar
Don Gagne committed
85
    }
Don Gagne's avatar
Don Gagne committed
86
    emit channelOptionEnabledChanged();
Don Gagne's avatar
Don Gagne committed
87
}