LP-106 Setup Wizard refresh : Add dual servo setup (dual aileron or
[librepilot.git] / ground / gcs / src / plugins / uavtalk / telemetry.h
blob43d67b10023b634c3d61c2e224f986171bbcd4b3
1 /**
2 ******************************************************************************
4 * @file telemetry.h
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
7 * @{
8 * @addtogroup UAVTalkPlugin UAVTalk Plugin
9 * @{
10 * @brief The UAVTalk protocol plugin
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #ifndef TELEMETRY_H
29 #define TELEMETRY_H
31 #include "uavtalk.h"
32 #include "uavobjectmanager.h"
33 #include "gcstelemetrystats.h"
34 #include <QMutex>
35 #include <QMutexLocker>
36 #include <QTimer>
37 #include <QQueue>
38 #include <QMap>
40 class ObjectTransactionInfo : public QObject {
41 Q_OBJECT
43 public:
44 ObjectTransactionInfo(QObject *parent);
45 ~ObjectTransactionInfo();
46 UAVObject *obj;
47 bool allInstances;
48 bool objRequest;
49 qint32 retriesRemaining;
50 bool acked;
51 QPointer<class Telemetry>telem;
52 QTimer *timer;
53 private slots:
54 void timeout();
57 class Telemetry : public QObject {
58 Q_OBJECT
60 public:
61 typedef struct {
62 quint32 txBytes;
63 quint32 txObjectBytes;
64 quint32 txObjects;
65 quint32 txErrors;
66 quint32 txRetries;
68 quint32 rxBytes;
69 quint32 rxObjectBytes;
70 quint32 rxObjects;
71 quint32 rxErrors;
72 quint32 rxSyncErrors;
73 quint32 rxCrcErrors;
74 } TelemetryStats;
76 Telemetry(UAVTalk *utalk, UAVObjectManager *objMngr);
77 ~Telemetry();
78 TelemetryStats getStats();
79 void resetStats();
80 void transactionTimeout(ObjectTransactionInfo *info);
82 private:
83 // Constants
84 static const int REQ_TIMEOUT_MS = 250;
85 static const int MAX_RETRIES = 2;
86 static const int MAX_UPDATE_PERIOD_MS = 1000;
87 static const int MIN_UPDATE_PERIOD_MS = 1;
88 static const int MAX_QUEUE_SIZE = 20;
90 // Types
91 /**
92 * Events generated by objects
94 typedef enum {
95 EV_NONE = 0x00, /** No event */
96 EV_UNPACKED = 0x01, /** Object data updated by unpacking */
97 EV_UPDATED = 0x02, /** Object data updated by changing the data structure */
98 EV_UPDATED_MANUAL = 0x04, /** Object update event manually generated */
99 EV_UPDATED_PERIODIC = 0x08, /** Object update event generated by timer */
100 EV_UPDATE_REQ = 0x10 /** Request to update object data */
101 } EventMask;
103 typedef struct {
104 UAVObject *obj;
105 qint32 updatePeriodMs; /** Update period in ms or 0 if no periodic updates are needed */
106 qint32 timeToNextUpdateMs; /** Time delay to the next update */
107 } ObjectTimeInfo;
109 typedef struct {
110 UAVObject *obj;
111 EventMask event;
112 bool allInstances;
113 } ObjectQueueInfo;
115 // Variables
116 UAVObjectManager *objMngr;
117 UAVTalk *utalk;
118 GCSTelemetryStats *gcsStatsObj;
119 QList<ObjectTimeInfo> objList;
120 QQueue<ObjectQueueInfo> objQueue;
121 QQueue<ObjectQueueInfo> objPriorityQueue;
122 QMap<quint32, QMap<quint32, ObjectTransactionInfo *> *> transMap;
123 QMutex *mutex;
124 QTimer *updateTimer;
125 QTimer *statsTimer;
126 qint32 timeToNextUpdateMs;
127 quint32 txErrors;
128 quint32 txRetries;
130 // Methods
131 void registerObject(UAVObject *obj);
132 void addObject(UAVObject *obj);
133 void setUpdatePeriod(UAVObject *obj, qint32 periodMs);
134 void connectToObjectInstances(UAVObject *obj, quint32 eventMask);
135 void connectToObject(UAVObject *obj, quint32 eventMask);
136 void updateObject(UAVObject *obj, quint32 eventMask);
137 void processObjectUpdates(UAVObject *obj, EventMask event, bool allInstances, bool priority);
138 void processObjectTransaction(ObjectTransactionInfo *transInfo);
139 void processObjectQueue();
141 ObjectTransactionInfo *findTransaction(UAVObject *obj);
142 void openTransaction(ObjectTransactionInfo *trans);
143 void closeTransaction(ObjectTransactionInfo *trans);
144 void closeAllTransactions();
146 private slots:
147 void objectUpdatedAuto(UAVObject *obj);
148 void objectUpdatedManual(UAVObject *obj, bool all = false);
149 void objectUpdatedPeriodic(UAVObject *obj);
150 void objectUnpacked(UAVObject *obj);
151 void updateRequested(UAVObject *obj, bool all = false);
152 void newObject(UAVObject *obj);
153 void newInstance(UAVObject *obj);
154 void processPeriodicUpdates();
155 void transactionCompleted(UAVObject *obj, bool success);
158 #endif // TELEMETRY_H