QGCFlightGearLink.h 4.29 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * 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 18 19 20 21 22 23 24 25


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

#ifndef QGCFLIGHTGEARLINK_H
#define QGCFLIGHTGEARLINK_H

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

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

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

public:
41
    QGCFlightGearLink(Vehicle* vehicle, QString startupArguments, QString remoteHost=QString("127.0.0.1:49000"), QHostAddress host = QHostAddress::Any, quint16 port = 49005);
42 43 44 45 46 47 48 49 50 51 52 53 54
    ~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
55 56 57 58 59 60
    /**
     * @brief Get remote host and port
     * @return string in format <host>:<port>
     */
    QString getRemoteHost();

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

    int getAirFrameIndex()
    {
        return -1;
    }

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

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

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

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

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

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

110
    void readBytes();
111 112

private slots:
113 114 115 116 117 118
    /**
     * @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
     **/
119
    void _writeBytes(const QByteArray data);
120 121

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

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

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

139
    Vehicle*    _vehicle;
Don Gagne's avatar
Don Gagne committed
140 141 142
    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
143 144 145

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

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

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

#endif // QGCFLIGHTGEARLINK_H