QGCFlightGearLink.h 4.21 KB
Newer Older
1 2
/****************************************************************************
 *
3
 *   (c) 2009-2018 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9 10 11 12 13 14 15 16 17


/**
 * @file
 *   @brief UDP connection (server) for unmanned vehicles
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

18
#pragma once
19 20 21 22 23 24

#include <QString>
#include <QList>
#include <QMap>
#include <QMutex>
#include <QUdpSocket>
lm's avatar
lm committed
25
#include <QTimer>
lm's avatar
lm committed
26
#include <QProcess>
27 28

#include "LinkInterface.h"
29
#include "QGCConfig.h"
lm's avatar
lm committed
30
#include "UASInterface.h"
31
#include "QGCHilLink.h"
32 33
#include "QGCHilFlightGearConfiguration.h"
#include "Vehicle.h"
34

35
class QGCFlightGearLink : public QGCHilLink
36 37 38 39
{
    Q_OBJECT

public:
40
    QGCFlightGearLink(Vehicle* vehicle, QString startupArguments, QString remoteHost=QString("127.0.0.1:49000"), QHostAddress host = QHostAddress::Any, quint16 port = 49005);
41 42 43 44 45 46 47 48 49 50 51 52 53
    ~QGCFlightGearLink();

    bool isConnected();
    qint64 bytesAvailable();
    int getPort() const {
        return port;
    }

    /**
     * @brief The human readable port name
     */
    QString getName();

Lorenz Meier's avatar
Lorenz Meier committed
54 55 56 57 58 59
    /**
     * @brief Get remote host and port
     * @return string in format <host>:<port>
     */
    QString getRemoteHost();

Lorenz Meier's avatar
Lorenz Meier committed
60 61 62 63 64 65 66 67 68 69
    QString getVersion()
    {
        return QString("FlightGear %1").arg(flightGearVersion);
    }

    int getAirFrameIndex()
    {
        return -1;
    }

70 71 72 73
    bool sensorHilEnabled() {
        return _sensorHilEnabled;
    }

Thomas Gubler's avatar
Thomas Gubler committed
74 75 76
    void sensorHilEnabled(bool sensorHilEnabled) {
        _sensorHilEnabled = sensorHilEnabled;
    }
Don Gagne's avatar
Don Gagne committed
77 78
    
    static bool parseUIArguments(QString uiArgs, QStringList& argList);
Thomas Gubler's avatar
Thomas Gubler committed
79

80
    void run();
Don Gagne's avatar
Don Gagne committed
81 82 83
    
signals:
    void showCriticalMessageFromThread(const QString& title, const QString& message);
84 85

public slots:
lm's avatar
lm committed
86
//    void setAddress(QString address);
87 88
    void setPort(int port);
    /** @brief Add a new host to broadcast messages to */
lm's avatar
lm committed
89
    void setRemoteHost(const QString& host);
lm's avatar
lm committed
90
    /** @brief Send new control states to the simulation */
91
    void updateControls(quint64 time, float rollAilerons, float pitchElevator, float yawRudder, float throttle, quint8 systemMode, quint8 navMode);
Lorenz Meier's avatar
Lorenz Meier committed
92 93 94 95 96
    /** @brief Set the simulator version as text string */
    void setVersion(const QString& version)
    {
        Q_UNUSED(version);
    }
97

Lorenz Meier's avatar
Lorenz Meier committed
98 99 100 101 102
    void selectAirframe(const QString& airframe)
    {
        Q_UNUSED(airframe);
    }

103 104 105 106 107 108
    void enableSensorHIL(bool enable) {
        if (enable != _sensorHilEnabled)
            _sensorHilEnabled = enable;
            emit sensorHilChanged(enable);
    }

109
    void readBytes();
110 111

private slots:
112 113 114 115 116 117
    /**
     * @brief Write a number of bytes to the interface.
     *
     * @param data Pointer to the data byte array
     * @param size The size of the bytes array
     **/
118
    void _writeBytes(const QByteArray data);
119 120

public slots:
lm's avatar
lm committed
121 122
    bool connectSimulation();
    bool disconnectSimulation();
123

124
    void setStartupArguments(QString startupArguments);
125
    void setBarometerOffset(float barometerOffsetkPa);
Don Gagne's avatar
Don Gagne committed
126
    void processError(QProcess::ProcessError err);
127

128 129
protected:
    void setName(QString name);
Don Gagne's avatar
Don Gagne committed
130
    
Don Gagne's avatar
Don Gagne committed
131 132 133 134
private slots:
    void _printFgfsOutput(void);
    void _printFgfsError(void);
    
Don Gagne's avatar
Don Gagne committed
135
private:
Don Gagne's avatar
Don Gagne committed
136 137
    static bool _findUIArgument(const QStringList& uiArgList, const QString& argLabel, QString& argValue);

138
    Vehicle*    _vehicle;
Don Gagne's avatar
Don Gagne committed
139 140 141
    QString     _fgProcessName;             ///< FlightGear process to start
    QString     _fgProcessWorkingDirPath;   ///< Working directory to start FG process in, empty for none
    QStringList _fgArgList;                 ///< Arguments passed to FlightGear process
Don Gagne's avatar
Don Gagne committed
142 143 144

    QUdpSocket* _udpCommSocket;             ///< UDP communication sockect between FG and QGC
    QProcess*   _fgProcess;                 ///< FlightGear process
145 146
    
    QString     _fgProtocolFileFullyQualified;  ///< Fully qualified file name for protocol file
147 148 149 150 151 152 153 154 155 156 157 158 159

    QString name;
    QHostAddress host;
    QHostAddress currentHost;
    quint16 currentPort;
    quint16 port;
    int id;
    bool connectState;

    unsigned int flightGearVersion;
    QString startupArguments;
    bool _sensorHilEnabled;
    float barometerOffsetkPa;
160 161
};