QGCHilFlightGearConfiguration.cc 5.55 KB
Newer Older
1 2 3 4
#include "QGCHilFlightGearConfiguration.h"

#include "MainWindow.h"

Don Gagne's avatar
Don Gagne committed
5 6 7 8 9 10 11 12 13 14 15
// Various settings groups and keys
const char* QGCHilFlightGearConfiguration::_settingsGroup =                 "QGC_HILCONFIG_FLIGHTGEAR";
const char* QGCHilFlightGearConfiguration::_mavSettingsSubGroupFixedWing =  "FIXED_WING";
const char* QGCHilFlightGearConfiguration::_mavSettingsSubGroupQuadRotor =  "QUADROTOR";
const char* QGCHilFlightGearConfiguration::_aircraftKey =                   "AIRCRAFT";
const char* QGCHilFlightGearConfiguration::_optionsKey =                    "OPTIONS";
const char* QGCHilFlightGearConfiguration::_barometerOffsetKey =            "BARO_OFFSET";
const char* QGCHilFlightGearConfiguration::_sensorHilKey =                  "SENSOR_HIL";

// Default set of optional command line parameters. If FlightGear won't run HIL without it it should go into
// the QGCFlightGearLink code instead.
Don Gagne's avatar
Don Gagne committed
16
const char* QGCHilFlightGearConfiguration::_defaultOptions = "--roll=0 --pitch=0 --vc=0 --heading=300 --timeofday=noon --disable-hud-3d --disable-fullscreen --geometry=400x300 --disable-anti-alias-hud --wind=0@0 --turbulence=0.0 --control=mouse --disable-sound --disable-random-objects --disable-ai-traffic --shading-flat --fog-disable --disable-specular-highlight --disable-panel --disable-clouds --fdm=jsb --units-meters --enable-terrasync";
Don Gagne's avatar
Don Gagne committed
17 18

QGCHilFlightGearConfiguration::QGCHilFlightGearConfiguration(UAS* mav, QWidget *parent) :
19
    QWidget(parent),
Don Gagne's avatar
Don Gagne committed
20 21 22
    _mav(mav),
    _mavSettingsSubGroup(NULL),
    _resetOptionsAction(tr("Reset to default options"), this)
23

Don Gagne's avatar
Don Gagne committed
24 25 26 27 28 29 30
{
    Q_ASSERT(_mav);
    
    _ui.setupUi(this);
    
    QStringList items;
    if (_mav->getSystemType() == MAV_TYPE_FIXED_WING)
31
    {
32
        items << "EasyStar";
33 34
        items << "Rascal110-JSBSim";
        items << "c172p";
Thomas Gubler's avatar
Thomas Gubler committed
35 36
        items << "YardStik";
        items << "Malolo1";
Don Gagne's avatar
Don Gagne committed
37
        _mavSettingsSubGroup = _mavSettingsSubGroupFixedWing;
38
    }
Don Gagne's avatar
Don Gagne committed
39
    else if (_mav->getSystemType() == MAV_TYPE_QUADROTOR)
40 41
    {
        items << "arducopter";
Don Gagne's avatar
Don Gagne committed
42
        _mavSettingsSubGroup = _mavSettingsSubGroupQuadRotor;
43 44 45
    }
    else
    {
Don Gagne's avatar
Don Gagne committed
46
        // FIXME: Should disable all input, won't work. Show error message in the status label thing.
47 48
        items << "<aircraft>";
    }
Don Gagne's avatar
Don Gagne committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    _ui.aircraftComboBox->addItems(items);
    
    QSettings settings;
    settings.beginGroup(_settingsGroup);
    settings.beginGroup(_mavSettingsSubGroup);
    QString aircraft = settings.value(_aircraftKey).toString();
    QString options = settings.value(_optionsKey).toString();
    QString baroOffset = settings.value(_barometerOffsetKey).toString();
    bool sensorHil = settings.value(_sensorHilKey, QVariant(true)).toBool();
    settings.endGroup();
    settings.endGroup();

    if (!aircraft.isEmpty()) {
        int index = _ui.aircraftComboBox->findText(aircraft);
        if (index != -1) {
            _ui.aircraftComboBox->setCurrentIndex(index);
        }
    }
    if (options.isEmpty()) {
        options = _defaultOptions;
    }
    _ui.optionsPlainTextEdit->setPlainText(options);
    _ui.barometerOffsetLineEdit->setText(baroOffset);
    _ui.sensorHilCheckBox->setChecked(sensorHil);

    // Provide an option on the context menu to reset the option back to default
    _ui.optionsPlainTextEdit->setContextMenuPolicy(Qt::CustomContextMenu);
    bool success = connect(&_resetOptionsAction, SIGNAL(triggered()), this, SLOT(_setDefaultOptions()));
    Q_ASSERT(success);
    success = connect(_ui.optionsPlainTextEdit, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(_showContextMenu(const QPoint &)));
    Q_ASSERT(success);
Don Gagne's avatar
Don Gagne committed
80
    Q_UNUSED(success);  // Silence release build unused variable warning
81 82 83 84
}

QGCHilFlightGearConfiguration::~QGCHilFlightGearConfiguration()
{
Don Gagne's avatar
Don Gagne committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    QString aircraft = _ui.aircraftComboBox->currentText();
    QString options = _ui.optionsPlainTextEdit->toPlainText();
    QString baroOffset = _ui.barometerOffsetLineEdit->text();
    bool sensorHil = _ui.sensorHilCheckBox->isChecked();
    
    QSettings settings;
    settings.beginGroup(_settingsGroup);
    settings.beginGroup(_mavSettingsSubGroup);

    if (aircraft.isEmpty()) {
        settings.remove(_aircraftKey);
    } else {
        settings.setValue(_aircraftKey, aircraft);
    }
    
    if (options.isEmpty() || options == _defaultOptions) {
        settings.remove(_optionsKey);
    } else {
        settings.setValue(_optionsKey, options);
    }
    
    // Double QVariant is to convert from string to float. It will change to 0.0 if invalid.
    settings.setValue(_barometerOffsetKey, QVariant(QVariant(baroOffset).toFloat()));
    
    settings.setValue(_sensorHilKey, QVariant(sensorHil));
    
    settings.endGroup();
    settings.endGroup();
113 114 115 116
}

void QGCHilFlightGearConfiguration::on_startButton_clicked()
{
117
    //XXX check validity of inputs
Don Gagne's avatar
Don Gagne committed
118 119 120
    QString options = _ui.optionsPlainTextEdit->toPlainText();
    options.append(" --aircraft=" + _ui.aircraftComboBox->currentText());
    _mav->enableHilFlightGear(true, options, _ui.sensorHilCheckBox->isChecked(), this);
121
}
122 123 124

void QGCHilFlightGearConfiguration::on_stopButton_clicked()
{
Don Gagne's avatar
Don Gagne committed
125
    _mav->stopHil();
126
}
127 128 129 130 131 132

void QGCHilFlightGearConfiguration::on_barometerOffsetLineEdit_textChanged(const QString& baroOffset)
{
    emit barometerOffsetChanged(baroOffset.toFloat());
}

Don Gagne's avatar
Don Gagne committed
133 134 135 136 137 138 139 140 141 142 143 144 145
void QGCHilFlightGearConfiguration::_showContextMenu(const QPoint &pt)
{
    QMenu* menu = _ui.optionsPlainTextEdit->createStandardContextMenu();
    menu->addAction(&_resetOptionsAction);
    menu->exec(_ui.optionsPlainTextEdit->mapToGlobal(pt));
    delete menu;
}

void QGCHilFlightGearConfiguration::_setDefaultOptions(void)
{
    _ui.optionsPlainTextEdit->setPlainText(_defaultOptions);
}