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

4 5
#include <QMenu>

Don Gagne's avatar
Don Gagne committed
6 7 8 9 10 11 12 13 14 15 16
// 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.
17
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 --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
18 19

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

Don Gagne's avatar
Don Gagne committed
25 26 27 28 29 30 31
{
    Q_ASSERT(_mav);
    
    _ui.setupUi(this);
    
    QStringList items;
    if (_mav->getSystemType() == MAV_TYPE_FIXED_WING)
32
    {
33
        items << "EasyStar";
34 35
        items << "Rascal110-JSBSim";
        items << "c172p";
Thomas Gubler's avatar
Thomas Gubler committed
36 37
        items << "YardStik";
        items << "Malolo1";
Don Gagne's avatar
Don Gagne committed
38
        _mavSettingsSubGroup = _mavSettingsSubGroupFixedWing;
39
    }
Don Gagne's avatar
Don Gagne committed
40
    else if (_mav->getSystemType() == MAV_TYPE_QUADROTOR)
41 42
    {
        items << "arducopter";
Don Gagne's avatar
Don Gagne committed
43
        _mavSettingsSubGroup = _mavSettingsSubGroupQuadRotor;
44 45 46
    }
    else
    {
Don Gagne's avatar
Don Gagne committed
47
        // FIXME: Should disable all input, won't work. Show error message in the status label thing.
48 49
        items << "<aircraft>";
    }
Don Gagne's avatar
Don Gagne committed
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 80
    _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
81
    Q_UNUSED(success);  // Silence release build unused variable warning
82 83 84 85
}

QGCHilFlightGearConfiguration::~QGCHilFlightGearConfiguration()
{
Don Gagne's avatar
Don Gagne committed
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 113
    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();
114 115 116 117
}

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

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

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

Don Gagne's avatar
Don Gagne committed
134 135 136 137 138 139 140 141 142 143 144 145 146
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);
}