FlightModesComponentController.cc 6.91 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/*=====================================================================
 
 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/>.
 
 ======================================================================*/

/// @file
///     @author Don Gagne <don@thegagnes.com>

#include "FlightModesComponentController.h"
#include "QGCMAVLink.h"
#include "UASManager.h"
#include "AutoPilotPluginManager.h"

#include <QVariant>
#include <QQmlProperty>

FlightModesComponentController::FlightModesComponentController(QObject* parent) :
    QObject(parent),
    _liveRCValues(false),
    _validConfiguration(false),
    _channelCount(18),
    _autoPilotPlugin(NULL)
{
    _uas = UASManager::instance()->getActiveUAS();
    Q_ASSERT(_uas);
    
    _autoPilotPlugin = AutoPilotPluginManager::instance()->getInstanceForAutoPilotPlugin(_uas);
    Q_ASSERT(_autoPilotPlugin);
47
    Q_ASSERT(_autoPilotPlugin->pluginReady());
Don Gagne's avatar
Don Gagne committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

    _initRcValues();
    _validateConfiguration();
}

FlightModesComponentController::~FlightModesComponentController()
{
    setSendLiveRCSwitchRanges(false);
}


void FlightModesComponentController::_initRcValues(void)
{
    for (int i=0; i<_chanMax; i++) {
        _rcValues << 1.0;
    }
}

/// This will look for parameter settings which would cause the config to not run correctly.
/// It will set _validConfiguration and _configurationErrors as needed.
void FlightModesComponentController::_validateConfiguration(void)
{
    _validConfiguration = true;
    
72 73 74
    _channelCount = _autoPilotPlugin->parameterExists("RC_CHAN_CNT") ?
                        _autoPilotPlugin->getParameterFact("RC_CHAN_CNT")->value().toInt() :
                        _chanMax;
Don Gagne's avatar
Don Gagne committed
75 76 77 78
    if (_channelCount <= 0 || _channelCount > _chanMax) {
        // Parameter exists, but has not yet been set or is invalid. Use default
        _channelCount = _chanMax;
    }
Don Gagne's avatar
Don Gagne committed
79 80 81 82 83 84 85 86 87 88
    
    // Make sure switches are valid and within channel range
    
    QStringList switchParams, switchNames;
    QList<int> switchMappings;
    
    switchParams << "RC_MAP_MODE_SW" << "RC_MAP_RETURN_SW" << "RC_MAP_LOITER_SW" << "RC_MAP_POSCTL_SW";
    switchNames << "Mode Switch" << "Return Switch" << "Loiter Switch" << "PosCtl Switch";
    
    for(int i=0; i<switchParams.count(); i++) {
89
        int map = _autoPilotPlugin->getParameterFact(switchParams[i])->value().toInt();
Don Gagne's avatar
Don Gagne committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        switchMappings << map;
        
        if (map < 0 || map > _channelCount) {
            _validConfiguration = false;
            _configurationErrors += QString("%1 is set to %2. Mapping must between 0 and %3 (inclusive).\n").arg(switchNames[i]).arg(map).arg(_channelCount);
        }
    }
    
    // Make sure switches are not mapped to attitude control channels
    
    QStringList attitudeParams, attitudeNames;
    
    attitudeParams << "RC_MAP_THROTTLE" << "RC_MAP_YAW" << "RC_MAP_PITCH" << "RC_MAP_ROLL" << "RC_MAP_FLAPS" << "RC_MAP_AUX1" << "RC_MAP_AUX2";
    attitudeNames << "Throttle" << "Yaw" << "Pitch" << "Roll" << "Flaps" << "Aux1" << "Aux2";

    for (int i=0; i<attitudeParams.count(); i++) {
106
        int map = _autoPilotPlugin->getParameterFact(attitudeParams[i])->value().toInt();
Don Gagne's avatar
Don Gagne committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

        for (int j=0; j<switchParams.count(); j++) {
            if (map != 0 && map == switchMappings[j]) {
                _validConfiguration = false;
                _configurationErrors += QString("%1 is set to same channel as %2.\n").arg(switchNames[j]).arg(attitudeNames[i]);
            }
        }
    }
}

void FlightModesComponentController::setSendLiveRCSwitchRanges(bool start)
{
    if (start) {
        
        // We need to know min and max for channel in order to calculate percentage range
        for (int i=0; i<_chanMax; i++) {
            QString rcMinParam, rcMaxParam, rcRevParam;
            
            rcMinParam = QString("RC%1_MIN").arg(i+1);
            rcMaxParam = QString("RC%1_MAX").arg(i+1);
            rcRevParam = QString("RC%1_REV").arg(i+1);
            
            QVariant value;
            
131 132
            _rgRCMin[i] = _autoPilotPlugin->getParameterFact(rcMinParam)->value().toInt();
            _rgRCMax[i] = _autoPilotPlugin->getParameterFact(rcMaxParam)->value().toInt();
Don Gagne's avatar
Don Gagne committed
133
            
134
            float floatReversed = _autoPilotPlugin->getParameterFact(rcRevParam)->value().toFloat();
Don Gagne's avatar
Don Gagne committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
            _rgRCReversed[i] = floatReversed == -1.0f;
        }
        
        _uas->startRadioControlCalibration();
        connect(_uas, &UASInterface::remoteControlChannelRawChanged, this, &FlightModesComponentController::_remoteControlChannelRawChanged);
    } else {
        disconnect(_uas, &UASInterface::remoteControlChannelRawChanged, this, &FlightModesComponentController::_remoteControlChannelRawChanged);
        _uas->endRadioControlCalibration();
        _initRcValues();
        emit switchLiveRangeChanged();
    }
}

/// @brief This routine is called whenever a raw value for an RC channel changes.
///     @param chan RC channel on which signal is coming from (0-based)
///     @param fval Current value for channel
void FlightModesComponentController::_remoteControlChannelRawChanged(int chan, float fval)
{
    Q_ASSERT(chan >= 0 && chan <= _chanMax);
    
    if (fval < _rgRCMin[chan]) {
        fval= _rgRCMin[chan];
    }
    if (fval > _rgRCMax[chan]) {
        fval= _rgRCMax[chan];
    }
    
    float percentRange = (fval - _rgRCMin[chan]) / (float)(_rgRCMax[chan] - _rgRCMin[chan]);
    if (_rgRCReversed[chan]) {
        percentRange = 1.0 - percentRange;
    }
    
    _rcValues[chan] = percentRange;
    emit switchLiveRangeChanged();
}

double FlightModesComponentController::_switchLiveRange(const QString& param)
{
    QVariant value;
    
175
    int channel = _autoPilotPlugin->getParameterFact(param)->value().toInt();
Don Gagne's avatar
Don Gagne committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    if (channel == 0) {
        return 1.0;
    } else {
        return _rcValues[channel - 1];
    }
}

double FlightModesComponentController::modeSwitchLiveRange(void)
{
    return _switchLiveRange("RC_MAP_MODE_SW");
}

double FlightModesComponentController::returnSwitchLiveRange(void)
{
    return _switchLiveRange("RC_MAP_RETURN_SW");
}

double FlightModesComponentController::loiterSwitchLiveRange(void)
{
    return _switchLiveRange("RC_MAP_LOITER_SW");
}

double FlightModesComponentController::posCtlSwitchLiveRange(void)
{
    return _switchLiveRange("RC_MAP_POSCTL_SW");
}