APMFlightModesComponentController.cc 2.76 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 37 38 39 40 41
{
    QStringList usedParams;
    usedParams << "FLTMODE1" << "FLTMODE2" << "FLTMODE3" << "FLTMODE4" << "FLTMODE5" << "FLTMODE6";
    if (!_allParametersExists(FactSystem::defaultComponentId, usedParams)) {
        return;
    }

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

Don Gagne's avatar
Don Gagne committed
47 48
/// Connected to Vehicle::rcChannelsChanged signal
void APMFlightModesComponentController::_rcChannelsChanged(int channelCount, int pwmValues[Vehicle::cMaxRcChannels])
Don Gagne's avatar
Don Gagne committed
49
{
Don Gagne's avatar
Don Gagne committed
50
    Q_UNUSED(channelCount);
Don Gagne's avatar
Don Gagne committed
51

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

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