FlightModeConfig.cc 11.1 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
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2014 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/>.
 
 ======================================================================*/

24 25
#include "FlightModeConfig.h"

Don Gagne's avatar
Don Gagne committed
26 27 28
// We used the _rgModeInfo* arrays to populate the combo box choices. The numeric value
// is the flight mode value that corresponds to the label. We store that value in the
// combo box item data. There is a different set or each vehicle type.
29

Don Gagne's avatar
Don Gagne committed
30 31 32 33 34 35 36 37 38 39 40 41
const FlightModeConfig::FlightModeInfo_t FlightModeConfig::_rgModeInfoFixedWing[] = {
    { "Manual",     0 },
    { "Circle",     1 },
    { "Stabilize",  2 },
    { "Training",   3 },
    { "FBW A",      5 },
    { "FBW B",      6 },
    { "Auto",       10 },
    { "RTL",        11 },
    { "Loiter",     12 },
    { "Guided",     15 },
};
42

Don Gagne's avatar
Don Gagne committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
const FlightModeConfig::FlightModeInfo_t FlightModeConfig::_rgModeInfoRotor[] = {
    { "Stabilize",  0 },
    { "Acro",       1 },
    { "Alt Hold",   2 },
    { "Auto",       3 },
    { "Guided",     4 },
    { "Loiter",     5 },
    { "RTL",        6 },
    { "Circle",     7 },
    { "Position",   8 },
    { "Land",       9 },
    { "OF_Loiter",  10 },
    { "Toy A",      11 },
    { "Toy M",      12 },
};
58

Don Gagne's avatar
Don Gagne committed
59 60 61 62 63 64 65 66 67
const FlightModeConfig::FlightModeInfo_t FlightModeConfig::_rgModeInfoRover[] = {
    { "Manual",     0 },
    { "Learning",   2 },
    { "Steering",   3 },
    { "Hold",       4 },
    { "Auto",       10 },
    { "RTL",        11 },
    { "Guided",     15 },
};
68

Don Gagne's avatar
Don Gagne committed
69 70 71 72 73 74
// We use the _rgModeParam* arrays to store the parameter names for each selectable
// flight mode. The order of these corresponds to the combox box order as well. Array
// element 0, is the parameter for mode0ComboBox, array element 1 = mode1ComboBox and
// so on. The number of elements in the array also determines how many combo boxes we
// are going to need. Different vehicle types have different numbers of selectable
// flight modes.
75

Don Gagne's avatar
Don Gagne committed
76
const char* FlightModeConfig::_rgModeParamFixedWing[_cModes] = { "FLTMODE1", "FLTMODE2", "FLTMODE3", "FLTMODE4", "FLTMODE5", "FLTMODE6" };
77

Don Gagne's avatar
Don Gagne committed
78
const char* FlightModeConfig::_rgModeParamRotor[_cModes] = { "FLTMODE1", "FLTMODE2", "FLTMODE3", "FLTMODE4", "FLTMODE5", "FLTMODE6" };
79

Don Gagne's avatar
Don Gagne committed
80
const char* FlightModeConfig::_rgModeParamRover[_cModes] = { "MODE1", "MODE2", "MODE3", "MODE4", "MODE5", "MODE6" };
81

Don Gagne's avatar
Don Gagne committed
82 83
// Parameter which contains simple mode bitmask
const char* FlightModeConfig::_simpleModeBitMaskParam = "SIMPLE";
84

Don Gagne's avatar
Don Gagne committed
85 86 87 88
// Parameter which controls which RC channel mode switching is on.
// ArduCopter is hardcoded so no param
const char* FlightModeConfig::_modeSwitchRCChannelParamFixedWing = "FLTMODE_CH";
const char* FlightModeConfig::_modeSwitchRCChannelParamRover = "MODE_CH";
89

Don Gagne's avatar
Don Gagne committed
90 91
// PWM values which are the boundaries between each mode selection
const int FlightModeConfig::_rgModePWMBoundary[_cModes] = { 1230, 1360, 1490, 1620, 1749, 20000 };
92

Don Gagne's avatar
Don Gagne committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
FlightModeConfig::FlightModeConfig(QWidget *parent) :
    AP2ConfigWidget(parent),
    _rgModeInfo(NULL),
    _cModeInfo(0),
    _rgModeParam(NULL),
    _simpleModeSupported(false),
    _modeSwitchRCChannel(_modeSwitchRCChannelInvalid)
{
    _ui.setupUi(this);
    
    // Find all the widgets we are going to programmatically control
    for (size_t i=0; i<_cModes; i++) {
        _rgLabel[i] = findChild<QLabel*>(QString("mode%1Label").arg(i));
        _rgCombo[i] = findChild<QComboBox*>(QString("mode%1ComboBox").arg(i));
        _rgSimpleModeCheckBox[i] = findChild<QCheckBox*>(QString("mode%1SimpleCheckBox").arg(i));
        _rgPWMLabel[i] = findChild<QLabel*>(QString("mode%1PWMLabel").arg(i));
        Q_ASSERT(_rgLabel[i]);
        Q_ASSERT(_rgCombo[i]);
        Q_ASSERT(_rgSimpleModeCheckBox[i]);
        Q_ASSERT(_rgPWMLabel[i]);
113
    }
Don Gagne's avatar
Don Gagne committed
114 115 116
    
    // Start disabled until we get a UAS
    setEnabled(false);
117

Don Gagne's avatar
Don Gagne committed
118 119 120
    connect(_ui.savePushButton, SIGNAL(clicked()), this, SLOT(saveButtonClicked()));
    initConnections();
}
121

Don Gagne's avatar
Don Gagne committed
122 123 124 125 126
void FlightModeConfig::activeUASSet(UASInterface *uas)
{
    // Clear the highlighting on PWM labels
    for (size_t i=0; i<_cModes; i++) {
        _rgPWMLabel[i]->setStyleSheet(QString(""));
127
    }
Don Gagne's avatar
Don Gagne committed
128 129 130

    // Disconnect from old UAS
    if (m_uas)
131
    {
Don Gagne's avatar
Don Gagne committed
132
        disconnect(m_uas, SIGNAL(remoteControlChannelRawChanged(int, float)), this, SLOT(remoteControlChannelRawChanged(int, float)));
133
    }
Don Gagne's avatar
Don Gagne committed
134 135 136 137 138 139
    
    // Connect to new UAS (if any)
    AP2ConfigWidget::activeUASSet(uas);
    if (!uas) {
        setEnabled(false);
        return;
140
    }
Don Gagne's avatar
Don Gagne committed
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 175 176 177 178 179 180
    connect(m_uas, SIGNAL(remoteControlChannelRawChanged(int, float)), this, SLOT(remoteControlChannelRawChanged(int, float)));
    setEnabled(true);
    
    // Select the correct set of Flight Modes and Flight Mode Parameters. If the rc channel
    // associated with the mode switch is settable through a param, it is invalid until
    // we get that param through.
    switch (m_uas->getSystemType()) {
        case MAV_TYPE_FIXED_WING:
            _rgModeInfo = &_rgModeInfoFixedWing[0];
            _cModeInfo = sizeof(_rgModeInfoFixedWing)/sizeof(_rgModeInfoFixedWing[0]);
            _rgModeParam = _rgModeParamFixedWing;
            _simpleModeSupported = false;
            _modeSwitchRCChannelParam = _modeSwitchRCChannelParamFixedWing;
            _modeSwitchRCChannel = _modeSwitchRCChannelInvalid;
            break;
        case MAV_TYPE_GROUND_ROVER:
            _rgModeInfo = &_rgModeInfoRover[0];
            _cModeInfo = sizeof(_rgModeInfoRover)/sizeof(_rgModeInfoRover[0]);
            _rgModeParam = _rgModeParamRover;
            _simpleModeSupported = false;
            _modeSwitchRCChannelParam = _modeSwitchRCChannelParamRover;
            _modeSwitchRCChannel = _modeSwitchRCChannelInvalid;
            break;
        case MAV_TYPE_QUADROTOR:
        case MAV_TYPE_TRICOPTER:
        case MAV_TYPE_HEXAROTOR:
        case MAV_TYPE_OCTOROTOR:
        case MAV_TYPE_HELICOPTER:
            _rgModeInfo = &_rgModeInfoRotor[0];
            _cModeInfo = sizeof(_rgModeInfoRotor)/sizeof(_rgModeInfoRotor[0]);
            _rgModeParam = _rgModeParamRotor;
            _simpleModeSupported = true;
            _modeSwitchRCChannelParam = NULL;
            _modeSwitchRCChannel = _modeSwitchRCChannelRotor; // Rotor is harcoded
            break;
        default:
            // We've gotten a mav type we can't handle, just disable to whole thing
            qDebug() << QString("Unknown System Type %1").arg(m_uas->getSystemType());
            setEnabled(false);
            return;
181
    }
Don Gagne's avatar
Don Gagne committed
182 183 184 185 186 187 188 189 190 191

    // Set up the combo boxes
    for (size_t i=0; i<_cModes; i++) {
        // Fill each combo box with the available flight modes
        for (size_t j=0; j<_cModeInfo; j++) {
            _rgCombo[i]->addItem(_rgModeInfo[j].label, QVariant(QChar((char)_rgModeInfo[j].value)));
        }
        
        // Not all vehicle types support simple mode, hide/show as appropriate
        _rgSimpleModeCheckBox[i]->setEnabled(_simpleModeSupported);
192 193 194
    }
}

Don Gagne's avatar
Don Gagne committed
195
void FlightModeConfig::saveButtonClicked(void)
196
{
Don Gagne's avatar
Don Gagne committed
197 198 199 200 201 202 203 204 205 206 207 208 209
    // Save the current setting for each flight mode slot
    for (size_t i=0; i<_cModes; i++) {
        m_uas->getParamManager()->setParameter(1, _rgModeParam[i], _rgCombo[i]->itemData(_rgCombo[i]->currentIndex()));
        QVariant var =_rgCombo[i]->itemData(_rgCombo[i]->currentIndex());
    }
    
    // Save Simple Mode bit mask if supported
    if (_simpleModeSupported) {
        int bitMask = 0;
        for (size_t i=0; i<_cModes; i++) {
            if (_rgSimpleModeCheckBox[i]->isChecked()) {
                bitMask |= 1 << i;
            }
210
        }
Don Gagne's avatar
Don Gagne committed
211
        m_uas->getParamManager()->setParameter(1, _simpleModeBitMaskParam, QVariant(QChar(bitMask)));
212 213 214
    }
}

Don Gagne's avatar
Don Gagne committed
215 216 217
// Higlights the PWM label for currently selected mode according the mode switch
// rc channel value.
void FlightModeConfig::remoteControlChannelRawChanged(int chan, float val)
218
{
Don Gagne's avatar
Don Gagne committed
219 220 221 222
    qDebug() << chan << val << _modeSwitchRCChannel;
    // Until we get the _modeSwitchRCChannel value from a parameter it will be set
    // to -1, which is an invalid channel thus the labels won't update
    if (chan == _modeSwitchRCChannel)
223
    {
Don Gagne's avatar
Don Gagne committed
224 225 226 227 228 229 230 231
        qDebug() << chan << val;
        size_t highlightIndex;
        
        for (size_t i=0; i<_cModes; i++) {
            if (val < _rgModePWMBoundary[i]) {
                highlightIndex = i;
                break;
            }
232
        }
Don Gagne's avatar
Don Gagne committed
233 234 235 236 237 238 239 240 241
        
        for (size_t i=0; i<_cModes; i++) {
            QString styleSheet;
            if (i == highlightIndex) {
                styleSheet = "background-color: rgb(0, 255, 0);color: rgb(0, 0, 0);";
            } else {
                styleSheet = "";
            }
            _rgPWMLabel[i]->setStyleSheet(styleSheet);
242 243
        }
    }
Don Gagne's avatar
Don Gagne committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
}

void FlightModeConfig::parameterChanged(int uas, int component, QString parameterName, QVariant value)
{
    Q_UNUSED(uas);
    Q_UNUSED(component);
    
    int iValue = value.toInt();
    
    if (parameterName == _modeSwitchRCChannelParam) {
        _modeSwitchRCChannel = iValue - 1;  // 1-based in params
    } else if (parameterName == _simpleModeBitMaskParam) {
        if (_simpleModeSupported) {
            for (size_t i=0; i<_cModes; i++) {
                _rgSimpleModeCheckBox[i]->setCheckState((iValue & (1 << i)) ? Qt::Checked : Qt::Unchecked);
            }
        } else {
            qDebug() << "Received simple mode parameter on non simple mode vehicle type";
262
        }
Don Gagne's avatar
Don Gagne committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    } else {
        // Loop over the flight mode params looking for a match
        for (size_t i=0; i<_cModes; i++) {
            if (parameterName == _rgModeParam[i]) {
                // We found a match, i is now the index of the combo box which displays that mode slot
                // Loop over the mode info till we find the matching value, this tells us which row in the
                // combo box to select.
                QComboBox* combo = _rgCombo[i];
                for (size_t j=0; j<_cModeInfo; j++) {
                    if (_rgModeInfo[j].value == iValue) {
                        combo->setCurrentIndex(j);
                        return;
                    }
                }
                
                // We should have been able to find the flight mode value. If we didn't, we have been passed a
                // flight mode that we don't understand. Possibly a newer firmware version we are not yet up
                // to date with. In this case, add a new item to the combo box to represent it.
                qDebug() << QString("Unknown flight mode value %1").arg(iValue);
                combo->addItem(QString("%1 - Unknown").arg(iValue), QVariant(iValue));
                combo->setCurrentIndex(combo->count() - 1);
            }
285 286 287
        }
    }
}