MAVLinkSimulationLink.h 4.06 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2
/*=====================================================================

3
QGroundControl Open Source Ground Control Station
pixhawk's avatar
pixhawk committed
4

5
(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
pixhawk's avatar
pixhawk committed
6

7
This file is part of the QGROUNDCONTROL project
pixhawk's avatar
pixhawk committed
8

9
    QGROUNDCONTROL is free software: you can redistribute it and/or modify
pixhawk's avatar
pixhawk committed
10 11 12 13
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

14
    QGROUNDCONTROL is distributed in the hope that it will be useful,
pixhawk's avatar
pixhawk committed
15 16 17 18 19
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
20
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
pixhawk's avatar
pixhawk committed
21 22 23 24 25

======================================================================*/

/**
 * @file
26
 *   @brief Definition of MAVLinkSimulationLink
pixhawk's avatar
pixhawk committed
27 28 29 30 31 32 33 34 35 36 37 38 39
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#ifndef MAVLINKSIMULATIONLINK_H
#define MAVLINKSIMULATIONLINK_H

#include <QFile>
#include <QTimer>
#include <QTextStream>
#include <QQueue>
#include <QMutex>
lm's avatar
lm committed
40
#include <QMap>
41
#include <qmath.h>
pixhawk's avatar
pixhawk committed
42
#include <inttypes.h>
pixhawk's avatar
pixhawk committed
43
#include "QGCMAVLink.h"
lm's avatar
lm committed
44

pixhawk's avatar
pixhawk committed
45
#include "LinkInterface.h"
pixhawk's avatar
pixhawk committed
46 47 48

class MAVLinkSimulationLink : public LinkInterface
{
pixhawk's avatar
pixhawk committed
49
    Q_OBJECT
pixhawk's avatar
pixhawk committed
50
public:
pixhawk's avatar
pixhawk committed
51
    MAVLinkSimulationLink(QString readFile="", QString writeFile="", int rate=5, QObject* parent = 0);
pixhawk's avatar
pixhawk committed
52
    ~MAVLinkSimulationLink();
53
    bool isConnected() const;
pixhawk's avatar
pixhawk committed
54 55 56 57 58 59 60 61
    qint64 bytesAvailable();

    void run();

    bool connect();
    bool disconnect();

    /* Extensive statistics for scientific purposes */
62
    qint64 getNominalDataRate() const;
pixhawk's avatar
pixhawk committed
63 64 65 66 67 68 69 70
    qint64 getTotalUpstream();
    qint64 getShortTermUpstream();
    qint64 getCurrentUpstream();
    qint64 getMaxUpstream();
    qint64 getTotalDownstream();
    qint64 getShortTermDownstream();
    qint64 getCurrentDownstream();
    qint64 getMaxDownstream();
71 72 73 74 75 76 77 78 79 80 81 82 83 84
    qint64 getBitsSent() const;
    qint64 getBitsReceived() const;

    QString getName() const;
    int getId() const;
    int getBaudRate() const;
    int getBaudRateType() const;
    int getFlowType() const;
    int getParityType() const;
    int getDataBitsType() const;
    int getStopBitsType() const;

    int getLinkQuality() const;
    bool isFullDuplex() const;
pixhawk's avatar
pixhawk committed
85 86 87

public slots:
    void writeBytes(const char* data, qint64 size);
88
    void readBytes();
89
    /** @brief Mainloop simulating the mainloop of the MAV */
pixhawk's avatar
pixhawk committed
90
    virtual void mainloop();
91
    bool connectLink(bool connect);
92
    void connectLink();
93
    void sendMAVLinkMessage(const mavlink_message_t* msg);
pixhawk's avatar
pixhawk committed
94 95 96 97 98 99


protected:

    // UAS properties
    float roll, pitch, yaw;
100 101
    double x, y, z;
    double spX, spY, spZ, spYaw;
pixhawk's avatar
pixhawk committed
102 103 104 105 106
    int battery;

    QTimer* timer;
    /** File which contains the input data (simulated robot messages) **/
    QFile* simulationFile;
107
    QFile* mavlinkLogFile;
pixhawk's avatar
pixhawk committed
108 109 110
    QString simulationHeader;
    /** File where the commands sent by the groundstation are stored **/
    QFile* receiveFile;
111
    QTextStream textStream;
pixhawk's avatar
pixhawk committed
112 113 114 115 116 117 118 119
    QTextStream* fileStream;
    QTextStream* outStream;
    /** Buffer which can be read from connected protocols through readBytes(). **/
    QMutex readyBufferMutex;
    bool _isConnected;
    quint64 rate;
    int maxTimeNoise;
    quint64 lastSent;
120 121 122 123
    static const int streamlength = 4096;
    unsigned int streampointer;
    //const int testoffset = 0;
    uint8_t stream[streamlength];
pixhawk's avatar
pixhawk committed
124 125 126 127 128 129

    int readyBytes;
    QQueue<uint8_t> readyBuffer;

    int id;
    QString name;
pixhawk's avatar
pixhawk committed
130
    qint64 timeOffset;
pixhawk's avatar
pixhawk committed
131
    mavlink_sys_status_t status;
132
    mavlink_heartbeat_t system;
lm's avatar
lm committed
133
    QMap<QString, float> onboardParams;
pixhawk's avatar
pixhawk committed
134

pixhawk's avatar
pixhawk committed
135 136
    void enqueue(uint8_t* stream, uint8_t* index, mavlink_message_t* msg);

137
    static const uint8_t systemId = 220;
138
    static const uint8_t componentId = 200;
139 140
    static const uint16_t version = 1000;

141
signals:
pixhawk's avatar
pixhawk committed
142
    void valueChanged(int uasId, QString curve, double value, quint64 usec);
143
    void messageReceived(const mavlink_message_t& message);
pixhawk's avatar
pixhawk committed
144 145 146 147

};

#endif // MAVLINKSIMULATIONLINK_H