APMFlightModesComponentController.cc 2.87 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
{
Don Gagne's avatar
Don Gagne committed
51
    Q_UNUSED(channelCount);
Don Gagne's avatar
Don Gagne committed
52

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

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