QGCHilFlightGearConfiguration.cc 5.64 KB
Newer Older
1 2
#include "QGCHilFlightGearConfiguration.h"
#include "MainWindow.h"
3
#include "UAS.h"
4

5 6
#include <QMenu>

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

20 21 22 23 24
QGCHilFlightGearConfiguration::QGCHilFlightGearConfiguration(Vehicle* vehicle, QWidget *parent)
    : QWidget(parent)
    , _vehicle(vehicle)
    , _mavSettingsSubGroup(NULL)
    , _resetOptionsAction(tr("Reset to default options"), this)
25

Don Gagne's avatar
Don Gagne committed
26 27 28 29
{
    _ui.setupUi(this);
    
    QStringList items;
30
    if (_vehicle->vehicleType() == MAV_TYPE_FIXED_WING)
31
    {
32
        /*items << "EasyStar";*/
33
        items << "Rascal110-JSBSim";
34
        /*items << "c172p";
Thomas Gubler's avatar
Thomas Gubler committed
35
        items << "YardStik";
36
        items << "Malolo1";*/
Don Gagne's avatar
Don Gagne committed
37
        _mavSettingsSubGroup = _mavSettingsSubGroupFixedWing;
38
    }
39
    /*else if (_vehicle->vehicleType() == 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
    _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);
76
    bool success = connect(&_resetOptionsAction, &QAction::triggered, this, &QGCHilFlightGearConfiguration::_setDefaultOptions);
Don Gagne's avatar
Don Gagne committed
77
    Q_ASSERT(success);
78 79
    success = connect(_ui.optionsPlainTextEdit, &QPlainTextEdit::customContextMenuRequested,
                      this, &QGCHilFlightGearConfiguration::_showContextMenu);
Don Gagne's avatar
Don Gagne committed
80
    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
    QString options = _ui.optionsPlainTextEdit->toPlainText();
    options.append(" --aircraft=" + _ui.aircraftComboBox->currentText());
121
    _vehicle->uas()->enableHilFlightGear(true, options, _ui.sensorHilCheckBox->isChecked(), this);
122
}
123 124 125

void QGCHilFlightGearConfiguration::on_stopButton_clicked()
{
126
    _vehicle->uas()->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);
}