OpalLink.h 3.84 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*=====================================================================

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
24
/**
pixhawk's avatar
pixhawk committed
25 26 27 28
 * @file
 *   @brief Connection to OpalRT
 *   @author Bryan Godbolt <godbolt@ualberta.ca>
 */
Bryan Godbolt's avatar
Bryan Godbolt committed
29

pixhawk's avatar
pixhawk committed
30 31
#ifndef OPALLINK_H
#define OPALLINK_H
Bryan Godbolt's avatar
Bryan Godbolt committed
32 33

#include <QMutex>
34
#include <QDebug>
35
#include <QTextStreamManipulator>
36 37 38 39
#include <QTimer>
#include <QQueue>
#include <QByteArray>
#include <QObject>
40 41
#include <stdlib.h>

Bryan Godbolt's avatar
Bryan Godbolt committed
42 43 44 45

#include "LinkInterface.h"
#include "LinkManager.h"
#include "MG.h"
pixhawk's avatar
pixhawk committed
46
#include "QGCMAVLink.h"
47
#include "QGCConfig.h"
Bryan Godbolt's avatar
Bryan Godbolt committed
48
#include "OpalRT.h"
49
#include "ParameterList.h"
50
#include "Parameter.h"
51
#include "QGCParamID.h"
Bryan Godbolt's avatar
Bryan Godbolt committed
52
#include "OpalApi.h"
53
#include "errno.h"
54
#include "string.h"
Bryan Godbolt's avatar
Bryan Godbolt committed
55

pixhawk's avatar
pixhawk committed
56
/**
Bryan Godbolt's avatar
Bryan Godbolt committed
57
 * @brief Interface to OpalRT targets
pixhawk's avatar
pixhawk committed
58
 *
Bryan Godbolt's avatar
Bryan Godbolt committed
59
 * This is an interface to the OpalRT hardware-in-the-loop (HIL) simulator.
pixhawk's avatar
pixhawk committed
60 61 62 63 64 65
 * 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/
 */
66 67
class OpalLink : public LinkInterface
{
Bryan Godbolt's avatar
Bryan Godbolt committed
68
    Q_OBJECT
69 70 71

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

74 75 76
    int getId() const;
    QString getName() const;
    bool isConnected() const;
Bryan Godbolt's avatar
Bryan Godbolt committed
77

78 79 80
    qint64 getConnectionSpeed() const;
    qint64 getCurrentInDataRate() const;
    qint64 getCurrentOutDataRate() const;
Bryan Godbolt's avatar
Bryan Godbolt committed
81

82
    void run();
83

84 85 86
    int getOpalInstID() {
        return static_cast<int>(opalInstID);
    }
87 88 89 90 91
    
    // These are left unimplemented in order to cause linker errors which indicate incorrect usage of
    // connect/disconnect on link directly. All connect/disconnect calls should be made through LinkManager.
    bool connect(void);
    bool disconnect(void);
92

Bryan Godbolt's avatar
Bryan Godbolt committed
93 94
public slots:

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

97
    void readBytes();
Bryan Godbolt's avatar
Bryan Godbolt committed
98

99 100
    void heartbeat();

101 102
    void getSignals();

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

107 108 109
protected slots:

    void receiveMessage(mavlink_message_t message);
110
    void setSignals(double *values);
111

Bryan Godbolt's avatar
Bryan Godbolt committed
112 113 114 115 116 117 118
protected:
    QString name;
    int id;
    bool connectState;

    quint64 connectionStartTime;

119
    QMutex receiveDataMutex;
Bryan Godbolt's avatar
Bryan Godbolt committed
120 121

    void setName(QString name);
122 123 124 125

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

    QTimer* getSignalsTimer;
    int getSignalsPeriod;

130 131 132 133 134 135
    QQueue<QByteArray>* receiveBuffer;
    QByteArray* sendBuffer;

    const int systemID;
    const int componentID;

136
    void getParameterList();
137
    OpalRT::ParameterList *params;
138 139

    unsigned short opalInstID;
Bryan Godbolt's avatar
Bryan Godbolt committed
140 141

    uint16_t duty2PulseMicros(double duty);
142
    uint8_t rescaleNorm(double norm, int ch);
143
    int8_t rescaleControllerOutput(double raw);
144 145

    bool sendRCValues;
146
    bool sendRawController;
147
    bool sendPosition;
148 149 150 151 152
    
private:
    // From LinkInterface
    virtual bool _connect(void);
    virtual bool _disconnect(void);
153 154 155
};

#endif // OPALLINK_H