Skip to content
OpalLink.h 3.81 KiB
Newer Older
pixhawk's avatar
pixhawk committed
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2010 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
    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.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    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
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

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

Bryan Godbolt's avatar
Bryan Godbolt committed
/**
pixhawk's avatar
pixhawk committed
 * @file
 *   @brief Connection to OpalRT
 *   @author Bryan Godbolt <godbolt@ualberta.ca>
 */
Bryan Godbolt's avatar
Bryan Godbolt committed

pixhawk's avatar
pixhawk committed
#ifndef OPALLINK_H
#define OPALLINK_H
Bryan Godbolt's avatar
Bryan Godbolt committed

#include <QMutex>
#include <QDebug>
#include <QTextStreamManipulator>
#include <QTimer>
#include <QQueue>
#include <QByteArray>
#include <QObject>
#include <stdlib.h>

Bryan Godbolt's avatar
Bryan Godbolt committed

#include "LinkInterface.h"
#include "LinkManager.h"
#include "MG.h"
#include "mavlink.h"
#include "mavlink_types.h"
#include "configuration.h"
Bryan Godbolt's avatar
Bryan Godbolt committed
#include "OpalRT.h"
#include "Parameter.h"
Bryan Godbolt's avatar
Bryan Godbolt committed
#include "OpalApi.h"
#include "string.h"
Bryan Godbolt's avatar
Bryan Godbolt committed

pixhawk's avatar
pixhawk committed
/**
Bryan Godbolt's avatar
Bryan Godbolt committed
 * @brief Interface to OpalRT targets
pixhawk's avatar
pixhawk committed
 *
Bryan Godbolt's avatar
Bryan Godbolt committed
 * This is an interface to the OpalRT hardware-in-the-loop (HIL) simulator.
pixhawk's avatar
pixhawk committed
 * This class receives MAVLink packets as if it is a true link, but it
 * interprets the packets internally, and calls the appropriate api functions.
 *
 * @author Bryan Godbolt <godbolt@ualberta.ca>
 * @ref http://www.opal-rt.com/
 */
class OpalLink : public LinkInterface
{
Bryan Godbolt's avatar
Bryan Godbolt committed
    Q_OBJECT

public:
    OpalLink();
Bryan Godbolt's avatar
Bryan Godbolt committed
    /* Connection management */

    int getId();
    QString getName();
    bool isConnected();

    /* Connection characteristics */


    qint64 getNominalDataRate();
    bool isFullDuplex();
    int getLinkQuality();
    qint64 getTotalUpstream();
    qint64 getTotalDownstream();
Bryan Godbolt's avatar
Bryan Godbolt committed
    qint64 getCurrentUpstream();
    qint64 getMaxUpstream();
    qint64 getBitsSent();
    qint64 getBitsReceived();

Bryan Godbolt's avatar
Bryan Godbolt committed
    bool connect();
Bryan Godbolt's avatar
Bryan Godbolt committed

Bryan Godbolt's avatar
Bryan Godbolt committed
    bool disconnect();
Bryan Godbolt's avatar
Bryan Godbolt committed

Bryan Godbolt's avatar
Bryan Godbolt committed
    qint64 bytesAvailable();
Bryan Godbolt's avatar
Bryan Godbolt committed

    int getOpalInstID() {return static_cast<int>(opalInstID);}

Bryan Godbolt's avatar
Bryan Godbolt committed
public slots:

Bryan Godbolt's avatar
Bryan Godbolt committed
    void writeBytes(const char *bytes, qint64 length);
    void heartbeat();

    void setOpalInstID(int instID) {opalInstID = static_cast<unsigned short>(instID);}

protected slots:

    void receiveMessage(mavlink_message_t message);
    void setSignals(double *values);
Bryan Godbolt's avatar
Bryan Godbolt committed
protected:
    QString name;
    int id;
    bool connectState;

    quint64 bitsSentTotal;
    quint64 bitsSentCurrent;
    quint64 bitsSentMax;
    quint64 bitsReceivedTotal;
    quint64 bitsReceivedCurrent;
    quint64 bitsReceivedMax;
    quint64 connectionStartTime;

    QMutex statisticsMutex;
    QMutex receiveDataMutex;
Bryan Godbolt's avatar
Bryan Godbolt committed

    void setName(QString name);

    QTimer* heartbeatTimer;    ///< Timer to emit heartbeats
    int heartbeatRate;         ///< Heartbeat rate, controls the timer interval
    bool m_heartbeatsEnabled;  ///< Enabled/disable heartbeat emission

    QTimer* getSignalsTimer;
    int getSignalsPeriod;

    QQueue<QByteArray>* receiveBuffer;
    QByteArray* sendBuffer;

    const int systemID;
    const int componentID;

Bryan Godbolt's avatar
Bryan Godbolt committed

    uint16_t duty2PulseMicros(double duty);
    uint8_t rescaleNorm(double norm, int ch);

    bool sendRCValues;
};

#endif // OPALLINK_H