SerialSimulationLink.h 3.54 KB
Newer Older
pixhawk's avatar
pixhawk committed
1
/*=====================================================================
pixhawk's avatar
pixhawk committed
2 3 4 5 6 7 8 9

QGroundControl Open Source Ground Control Station

(c) 2009 - 2011 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
pixhawk's avatar
pixhawk committed
10 11 12
    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.
pixhawk's avatar
pixhawk committed
13 14

    QGROUNDCONTROL is distributed in the hope that it will be useful,
pixhawk's avatar
pixhawk committed
15 16 17
    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.
pixhawk's avatar
pixhawk committed
18

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

pixhawk's avatar
pixhawk committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
======================================================================*/
 
/**
 * @file
 *   @brief Brief Description
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#ifndef _SERIALSIMULATIONLINK_H_
#define _SERIALSIMULATIONLINK_H_

#include <QString>
#include <QByteArray>
#include <QFile>
#include <QTimer>
#include <QTextStream>
#include <QMutex>
#include <SerialLinkInterface.h>
#include <SerialLink.h>

/**
 * The serial simulation link class simulates a robot connected to the groundstation.
 * It can be either file-based by reading telemetry messages from a file or realtime
 * by communicating with a simulation.
 *
 **/
class SerialSimulationLink : public SerialLinkInterface {
	Q_OBJECT

public:
        SerialSimulationLink(QFile* inputFile=NULL, QFile* outputFile=NULL, int sendrate=20);
	~SerialSimulationLink();

	bool isConnected();
	qint64 bytesAvailable();

	void run();

	/* Extensive statistics for scientific purposes */
	qint64 getNominalDataRate();
	qint64 getTotalUpstream();
	qint64 getShortTermUpstream();
	qint64 getCurrentUpstream();
	qint64 getMaxUpstream();
	qint64 getTotalDownstream();
	qint64 getShortTermDownstream();
	qint64 getCurrentDownstream();
	qint64 getMaxDownstream();
	qint64 getBitsSent();
	qint64 getBitsReceived();

	void enableLoopBackMode(SerialLink* loop);
	QString getPortName();
	int getBaudRate();
	int getBaudRateType();
	int getFlowType();
	int getParityType();
	int getDataBitsType();
	int getStopBitsType();

	int getLinkQuality();
	bool isFullDuplex();

public slots:
	bool setPortName(QString portName);
	bool setBaudRateType(int rateIndex);
	bool setBaudRate(int rate);
	bool setFlowType(int flow);
	bool setParityType(int parity);
	bool setDataBitsType(int dataBits);
	bool setStopBitsType(int stopBits);

	void readLine();
        void writeBytes(char *bytes, qint64 length);
98
        void readBytes();
pixhawk's avatar
pixhawk committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

	bool connect();
	bool disconnect();

	void setMaximumTimeNoise(int milliseconds);

protected:
	QTimer* timer;
	SerialLink* loopBack;
	/** File which contains the input data (simulated robot messages) **/
	QFile* simulationFile;
	/** File where the commands sent by the groundstation are stored **/
	QFile* receiveFile;
	QTextStream stream;
	QTextStream* fileStream;
	QTextStream* outStream;
	/** Buffer for next line. The next line is read ahead */
	QByteArray lineBuffer;
	/** Buffer which can be read from connected protocols through readBytes(). **/
	QByteArray readyBuffer;
	QMutex readyBufferMutex;
	bool _isConnected;
	quint64 rate;
	int maxTimeNoise;
	quint64 lastSent;

	void addTimeNoise();

signals:
	//void connected();
	//void disconnected();
	//void bytesReady(LinkInterface *link);

};

#endif // _SERIALSIMULATIONLINK_H_