MAVLinkInspectorController.h 13.5 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

Gus Grubba's avatar
Gus Grubba committed
10 11 12 13
/// @file
/// @brief MAVLink message inspector and charting controller
/// @author Gus Grubba <gus@auterion.com>

14 15 16 17 18 19 20 21
#pragma once

#include "MAVLinkProtocol.h"
#include "Vehicle.h"

#include <QObject>
#include <QString>
#include <QDebug>
22 23
#include <QVariantList>
#include <QtCharts/QAbstractSeries>
24

25 26
Q_DECLARE_LOGGING_CATEGORY(MAVLinkInspectorLog)

27 28 29
QT_CHARTS_USE_NAMESPACE

class QGCMAVLinkMessage;
Gus Grubba's avatar
Gus Grubba committed
30
class QGCMAVLinkVehicle;
31
class MAVLinkChartController;
32 33
class MAVLinkInspectorController;

34
//-----------------------------------------------------------------------------
Gus Grubba's avatar
Gus Grubba committed
35
/// MAVLink message field
36 37
class QGCMAVLinkMessageField : public QObject {
    Q_OBJECT
38
public:
39 40 41 42 43
    Q_PROPERTY(QString          name        READ name       CONSTANT)
    Q_PROPERTY(QString          label       READ label      CONSTANT)
    Q_PROPERTY(QString          type        READ type       CONSTANT)
    Q_PROPERTY(QString          value       READ value      NOTIFY valueChanged)
    Q_PROPERTY(bool             selectable  READ selectable NOTIFY selectableChanged)
44
    Q_PROPERTY(int              chartIndex  READ chartIndex CONSTANT)
45
    Q_PROPERTY(QAbstractSeries* series      READ series     NOTIFY seriesChanged)
46

47
    QGCMAVLinkMessageField(QGCMAVLinkMessage* parent, QString name, QString type);
48

49 50 51 52 53 54 55 56 57 58
    QString         name            () { return _name;  }
    QString         label           ();
    QString         type            () { return _type;  }
    QString         value           () { return _value; }
    bool            selectable      () { return _selectable; }
    bool            selected        () { return _pSeries != nullptr; }
    QAbstractSeries*series          () { return _pSeries; }
    QList<QPointF>* values          () { return &_values;}
    qreal           rangeMin        () { return _rangeMin; }
    qreal           rangeMax        () { return _rangeMax; }
59
    int             chartIndex      ();
60 61 62 63

    void            setSelectable   (bool sel);
    void            updateValue     (QString newValue, qreal v);

64
    void            addSeries       (MAVLinkChartController* chart, QAbstractSeries* series);
65
    void            delSeries       ();
Gus Grubba's avatar
Gus Grubba committed
66
    void            updateSeries    ();
67 68

signals:
69 70 71
    void            seriesChanged       ();
    void            selectableChanged   ();
    void            valueChanged        ();
72 73

private:
74 75 76
    QString     _type;
    QString     _name;
    QString     _value;
77 78 79 80
    bool        _selectable = true;
    int         _dataIndex  = 0;
    qreal       _rangeMin   = 0;
    qreal       _rangeMax   = 0;
81 82 83

    QAbstractSeries*    _pSeries = nullptr;
    QGCMAVLinkMessage*  _msg     = nullptr;
84
    MAVLinkChartController*      _chart   = nullptr;
85
    QList<QPointF>      _values;
86 87 88
};

//-----------------------------------------------------------------------------
Gus Grubba's avatar
Gus Grubba committed
89
/// MAVLink message
90 91
class QGCMAVLinkMessage : public QObject {
    Q_OBJECT
92
public:
Gus Grubba's avatar
Gus Grubba committed
93 94 95 96 97 98 99
    Q_PROPERTY(quint32              id              READ id             NOTIFY indexChanged)
    Q_PROPERTY(quint32              cid             READ cid            NOTIFY indexChanged)
    Q_PROPERTY(QString              name            READ name           NOTIFY indexChanged)
    Q_PROPERTY(qreal                messageHz       READ messageHz      NOTIFY freqChanged)
    Q_PROPERTY(quint64              count           READ count          NOTIFY messageChanged)
    Q_PROPERTY(QmlObjectListModel*  fields          READ fields         NOTIFY indexChanged)
    Q_PROPERTY(bool                 fieldSelected   READ fieldSelected  NOTIFY fieldSelectedChanged)
100
    Q_PROPERTY(bool                 selected        READ selected       NOTIFY selectedChanged)
101

102 103
    QGCMAVLinkMessage   (QObject* parent, mavlink_message_t* message);
    ~QGCMAVLinkMessage  ();
104

Gus Grubba's avatar
Gus Grubba committed
105 106 107 108 109 110 111 112
    quint32             id              () { return _message.msgid;  }
    quint8              cid             () { return _message.compid; }
    QString             name            () { return _name;  }
    qreal               messageHz       () { return _messageHz; }
    quint64             count           () { return _count; }
    quint64             lastCount       () { return _lastCount; }
    QmlObjectListModel* fields          () { return &_fields; }
    bool                fieldSelected   () { return _fieldSelected; }
113
    bool                selected        () { return _selected; }
114

Gus Grubba's avatar
Gus Grubba committed
115 116 117
    void                updateFieldSelection();
    void                update          (mavlink_message_t* message);
    void                updateFreq      ();
118
    void                setSelected     (bool sel) { _selected = sel; }
119 120

signals:
Gus Grubba's avatar
Gus Grubba committed
121 122 123 124
    void messageChanged                 ();
    void freqChanged                    ();
    void indexChanged                   ();
    void fieldSelectedChanged           ();
125
    void selectedChanged                ();
126 127 128 129

private:
    QmlObjectListModel  _fields;
    QString             _name;
Gus Grubba's avatar
Gus Grubba committed
130
    qreal               _messageHz  = 0.0;
131
    uint64_t            _count      = 0;
Gus Grubba's avatar
Gus Grubba committed
132
    uint64_t            _lastCount  = 0;
133
    mavlink_message_t   _message;   //-- List of QGCMAVLinkMessageField
Gus Grubba's avatar
Gus Grubba committed
134
    bool                _fieldSelected   = false;
135
    bool                _selected   = false;
136 137 138
};

//-----------------------------------------------------------------------------
Gus Grubba's avatar
Gus Grubba committed
139
/// Vehicle MAVLink message belongs to
140 141
class QGCMAVLinkVehicle : public QObject {
    Q_OBJECT
142
public:
Gus Grubba's avatar
Gus Grubba committed
143 144
    Q_PROPERTY(quint8               id              READ id             CONSTANT)
    Q_PROPERTY(QmlObjectListModel*  messages        READ messages       NOTIFY messagesChanged)
145 146
    Q_PROPERTY(QList<int>           compIDs         READ compIDs        NOTIFY compIDsChanged)
    Q_PROPERTY(QStringList          compIDsStr      READ compIDsStr     NOTIFY compIDsChanged)
147

Gus Grubba's avatar
Gus Grubba committed
148 149
    Q_PROPERTY(int                  selected        READ selected       WRITE setSelected   NOTIFY selectedChanged)

150 151
    QGCMAVLinkVehicle   (QObject* parent, quint8 id);
    ~QGCMAVLinkVehicle  ();
152

Gus Grubba's avatar
Gus Grubba committed
153 154
    quint8              id              () { return _id; }
    QmlObjectListModel* messages        () { return &_messages; }
155 156
    QList<int>          compIDs         () { return _compIDs; }
    QStringList         compIDsStr      () { return _compIDsStr; }
Gus Grubba's avatar
Gus Grubba committed
157
    int                 selected        () { return _selected; }
158

Gus Grubba's avatar
Gus Grubba committed
159
    void                setSelected     (int sel);
Gus Grubba's avatar
Gus Grubba committed
160
    QGCMAVLinkMessage*  findMessage     (uint32_t id, uint8_t cid);
Gus Grubba's avatar
Gus Grubba committed
161
    int                 findMessage     (QGCMAVLinkMessage* message);
Gus Grubba's avatar
Gus Grubba committed
162
    void                append          (QGCMAVLinkMessage* message);
163 164

signals:
Gus Grubba's avatar
Gus Grubba committed
165
    void messagesChanged                ();
166
    void compIDsChanged                 ();
Gus Grubba's avatar
Gus Grubba committed
167
    void selectedChanged                ();
168 169 170

private:
    void _checkCompID                   (QGCMAVLinkMessage *message);
171
    void _resetSelection                ();
172 173 174

private:
    quint8              _id;
175 176 177
    QList<int>          _compIDs;
    QStringList         _compIDsStr;
    QmlObjectListModel  _messages;      //-- List of QGCMAVLinkMessage
Gus Grubba's avatar
Gus Grubba committed
178
    int                 _selected = 0;
179 180 181
};

//-----------------------------------------------------------------------------
Gus Grubba's avatar
Gus Grubba committed
182
/// MAVLink message charting controller
183
class MAVLinkChartController : public QObject {
184 185
    Q_OBJECT
public:
186
    MAVLinkChartController(MAVLinkInspectorController* parent, int index);
187

188 189 190 191 192
    Q_PROPERTY(QVariantList chartFields         READ chartFields            NOTIFY chartFieldsChanged)
    Q_PROPERTY(QDateTime    rangeXMin           READ rangeXMin              NOTIFY rangeXMinChanged)
    Q_PROPERTY(QDateTime    rangeXMax           READ rangeXMax              NOTIFY rangeXMaxChanged)
    Q_PROPERTY(qreal        rangeYMin           READ rangeYMin              NOTIFY rangeYMinChanged)
    Q_PROPERTY(qreal        rangeYMax           READ rangeYMax              NOTIFY rangeYMaxChanged)
Gus Grubba's avatar
Gus Grubba committed
193
    Q_PROPERTY(int          chartIndex          READ chartIndex             CONSTANT)
194 195 196 197 198

    Q_PROPERTY(quint32      rangeYIndex         READ rangeYIndex            WRITE setRangeYIndex    NOTIFY rangeYIndexChanged)
    Q_PROPERTY(quint32      rangeXIndex         READ rangeXIndex            WRITE setRangeXIndex    NOTIFY rangeXIndexChanged)

    Q_INVOKABLE void        addSeries           (QGCMAVLinkMessageField* field, QAbstractSeries* series);
199 200
    Q_INVOKABLE void        delSeries           (QGCMAVLinkMessageField* field);

201 202 203 204 205 206 207 208 209
    Q_INVOKABLE MAVLinkInspectorController* controller() { return _controller; }

    QVariantList            chartFields         () { return _chartFields; }
    QDateTime               rangeXMin           () { return _rangeXMin;   }
    QDateTime               rangeXMax           () { return _rangeXMax;   }
    qreal                   rangeYMin           () { return _rangeYMin;   }
    qreal                   rangeYMax           () { return _rangeYMax;   }
    quint32                 rangeXIndex         () { return _rangeXIndex; }
    quint32                 rangeYIndex         () { return _rangeYIndex; }
210
    int                     chartIndex          () { return _index; }
211 212 213

    void                    setRangeXIndex      (quint32 t);
    void                    setRangeYIndex      (quint32 r);
214
    void                    updateXRange        ();
215
    void                    updateYRange        ();
216 217

signals:
218 219 220 221 222 223 224
    void chartFieldsChanged ();
    void rangeXMinChanged   ();
    void rangeXMaxChanged   ();
    void rangeYMinChanged   ();
    void rangeYMaxChanged   ();
    void rangeYIndexChanged ();
    void rangeXIndexChanged ();
225

226
private slots:
227
    void _refreshSeries     ();
228 229

private:
230 231 232
    QTimer              _updateSeriesTimer;
    QDateTime           _rangeXMin;
    QDateTime           _rangeXMax;
233
    int                 _index               = 0;
234 235 236 237 238 239 240
    qreal               _rangeYMin           = 0;
    qreal               _rangeYMax           = 1;
    quint32             _rangeXIndex         = 0;                    ///< 5 Seconds
    quint32             _rangeYIndex         = 0;                    ///< Auto Range
    QVariantList        _chartFields;
    MAVLinkInspectorController* _controller  = nullptr;
};
241

242
//-----------------------------------------------------------------------------
Gus Grubba's avatar
Gus Grubba committed
243
/// MAVLink message inspector controller (provides the logic for UI display)
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
class MAVLinkInspectorController : public QObject
{
    Q_OBJECT
public:
    MAVLinkInspectorController();
    ~MAVLinkInspectorController();

    Q_PROPERTY(QStringList          vehicleNames        READ vehicleNames           NOTIFY vehiclesChanged)
    Q_PROPERTY(QmlObjectListModel*  vehicles            READ vehicles               NOTIFY vehiclesChanged)
    Q_PROPERTY(QmlObjectListModel*  charts              READ charts                 NOTIFY chartsChanged)
    Q_PROPERTY(QGCMAVLinkVehicle*   activeVehicle       READ activeVehicle          NOTIFY activeVehiclesChanged)
    Q_PROPERTY(QStringList          timeScales          READ timeScales             NOTIFY timeScalesChanged)
    Q_PROPERTY(QStringList          rangeList           READ rangeList              NOTIFY rangeListChanged)

    Q_INVOKABLE MAVLinkChartController* createChart     ();
    Q_INVOKABLE void                    deleteChart     (MAVLinkChartController* chart);

    QmlObjectListModel*             vehicles            () { return &_vehicles;     }
    QmlObjectListModel*             charts              () { return &_charts;       }
    QGCMAVLinkVehicle*              activeVehicle       () { return _activeVehicle; }
    QStringList                     vehicleNames        () { return _vehicleNames;  }
    QStringList                     timeScales          ();
    QStringList                     rangeList           ();
Gus Grubba's avatar
Gus Grubba committed
267 268 269 270 271 272 273 274

    class TimeScale_st : public QObject {
    public:
        TimeScale_st(QObject* parent, const QString& l, uint32_t t);
        QString     label;
        uint32_t    timeScale;
    };

275 276 277 278 279 280 281
    class Range_st : public QObject {
    public:
        Range_st(QObject* parent, const QString& l, qreal r);
        QString     label;
        qreal       range;
    };

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    const QList<TimeScale_st*>&     timeScaleSt         () { return _timeScaleSt; }
    const QList<Range_st*>&         rangeSt             () { return _rangeSt; }

signals:
    void vehiclesChanged            ();
    void chartsChanged              ();
    void activeVehiclesChanged      ();
    void timeScalesChanged          ();
    void rangeListChanged           ();

private slots:
    void _receiveMessage            (LinkInterface* link, mavlink_message_t message);
    void _vehicleAdded              (Vehicle* vehicle);
    void _vehicleRemoved            (Vehicle* vehicle);
    void _setActiveVehicle          (Vehicle* vehicle);
    void _refreshFrequency          ();

private:
    QGCMAVLinkVehicle* _findVehicle (uint8_t id);

private:

304 305 306
    int                 _selectedSystemID       = 0;                    ///< Currently selected system
    int                 _selectedComponentID    = 0;                    ///< Currently selected component
    QStringList         _timeScales;
307
    QStringList         _rangeList;
308
    QGCMAVLinkVehicle*  _activeVehicle          = nullptr;
Gus Grubba's avatar
Gus Grubba committed
309
    QTimer              _updateFrequencyTimer;
310
    QStringList         _vehicleNames;
311
    QmlObjectListModel  _vehicles;                                      ///< List of QGCMAVLinkVehicle
312
    QmlObjectListModel  _charts;                                        ///< List of MAVLinkCharts
Gus Grubba's avatar
Gus Grubba committed
313
    QList<TimeScale_st*>_timeScaleSt;
314
    QList<Range_st*>    _rangeSt;
315

316
};