MAVLinkInspectorController.cc 32.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "MAVLinkInspectorController.h"
#include "QGCApplication.h"
#include "MultiVehicleManager.h"
13
#include <QtCharts/QLineSeries>
14

15 16
QGC_LOGGING_CATEGORY(MAVLinkInspectorLog, "MAVLinkInspectorLog")

17 18 19 20
QT_CHARTS_USE_NAMESPACE

Q_DECLARE_METATYPE(QAbstractSeries*)

Gus Grubba's avatar
Gus Grubba committed
21 22
#define UPDATE_FREQUENCY (1000 / 15)    // 15Hz

23
//-----------------------------------------------------------------------------
24
QGCMAVLinkMessageField::QGCMAVLinkMessageField(QGCMAVLinkMessage *parent, QString name, QString type)
25 26 27
    : QObject(parent)
    , _type(type)
    , _name(name)
28
    , _msg(parent)
29 30
{
    qCDebug(MAVLinkInspectorLog) << "Field:" << name << type;
Gus Grubba's avatar
Gus Grubba committed
31 32
}

33 34 35
//-----------------------------------------------------------------------------
void
QGCMAVLinkMessageField::addSeries(QAbstractSeries* series, bool left)
Gus Grubba's avatar
Gus Grubba committed
36
{
37 38 39 40 41 42 43
    if(!_pSeries) {
        _left = left;
        _pSeries = series;
        emit seriesChanged();
        _dataIndex = 0;
        _msg->msgCtl()->addChartField(this, left);
        _msg->select();
Gus Grubba's avatar
Gus Grubba committed
44 45 46 47 48
    }
}

//-----------------------------------------------------------------------------
void
49
QGCMAVLinkMessageField::delSeries()
Gus Grubba's avatar
Gus Grubba committed
50
{
51 52 53 54 55 56 57 58 59
    if(_pSeries) {
        _values.clear();
        _msg->msgCtl()->delChartField(this, _left);
        QLineSeries* lineSeries = static_cast<QLineSeries*>(_pSeries);
        lineSeries->replace(_values);
        _pSeries = nullptr;
        _left = false;
        emit seriesChanged();
        _msg->select();
Gus Grubba's avatar
Gus Grubba committed
60
    }
61 62
}

63 64 65 66
//-----------------------------------------------------------------------------
QString
QGCMAVLinkMessageField::label()
{
67
    //-- Label is message name + field name
68 69 70
    return QString(_msg->name() + ": " + _name);
}

71
//-----------------------------------------------------------------------------
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
void
QGCMAVLinkMessageField::setSelectable(bool sel)
{
    if(_selectable != sel) {
        _selectable = sel;
        emit selectableChanged();
    }
}

//-----------------------------------------------------------------------------
void
QGCMAVLinkMessageField::updateValue(QString newValue, qreal v)
{
    if(_value != newValue) {
        _value = newValue;
        emit valueChanged();
    }
89
    if(_pSeries) {
90 91 92
        int count = _values.count();
        //-- Arbitrary limit of 1 minute of data at 50Hz for now
        if(count < (50 * 60)) {
93 94
            QPointF p(QGC::groundTimeMilliseconds(), v);
            _values.append(p);
95 96
        } else {
            if(_dataIndex >= count) _dataIndex = 0;
97 98
            _values[_dataIndex].setX(QGC::groundTimeMilliseconds());
            _values[_dataIndex].setY(v);
99 100
            _dataIndex++;
        }
Gus Grubba's avatar
Gus Grubba committed
101
        //-- Auto Range
102
        if((!_left && _msg->msgCtl()->rightRangeIdx() == 0) || (_left && _msg->msgCtl()->leftRangeIdx() == 0)) {
Gus Grubba's avatar
Gus Grubba committed
103 104 105
            qreal vmin  = std::numeric_limits<qreal>::max();
            qreal vmax  = std::numeric_limits<qreal>::min();
            for(int i = 0; i < _values.count(); i++) {
106
                qreal v = _values[i].y();
Gus Grubba's avatar
Gus Grubba committed
107 108 109
                if(vmax < v) vmax = v;
                if(vmin > v) vmin = v;
            }
110
            bool changed = false;
Gus Grubba's avatar
Gus Grubba committed
111 112
            if(std::abs(_rangeMin - vmin) > 0.000001) {
                _rangeMin = vmin;
113
                changed = true;
Gus Grubba's avatar
Gus Grubba committed
114 115 116
            }
            if(std::abs(_rangeMax - vmax) > 0.000001) {
                _rangeMax = vmax;
117 118 119 120
                changed = true;
            }
            if(changed) {
                _msg->msgCtl()->updateYRange(_left);
Gus Grubba's avatar
Gus Grubba committed
121
            }
122 123 124 125 126 127
        }
    }
}

//-----------------------------------------------------------------------------
void
Gus Grubba's avatar
Gus Grubba committed
128
QGCMAVLinkMessageField::updateSeries()
129 130 131
{
    int count = _values.count();
    if (count > 1) {
Gus Grubba's avatar
Gus Grubba committed
132 133 134 135 136 137 138 139
        QList<QPointF> s;
        s.reserve(count);
        int idx = _dataIndex;
        for(int i = 0; i < count; i++, idx++) {
            if(idx >= count) idx = 0;
            QPointF p(_values[idx]);
            s.append(p);
        }
140
        QLineSeries* lineSeries = static_cast<QLineSeries*>(_pSeries);
Gus Grubba's avatar
Gus Grubba committed
141
        lineSeries->replace(s);
142 143 144 145 146
    }
}

//-----------------------------------------------------------------------------
QGCMAVLinkMessage::QGCMAVLinkMessage(MAVLinkInspectorController *parent, mavlink_message_t* message)
147
    : QObject(parent)
148
    , _msgCtl(parent)
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
{
    _message = *message;
    const mavlink_message_info_t* msgInfo = mavlink_get_message_info(message);
    if (!msgInfo) {
        qWarning() << QStringLiteral("QGCMAVLinkMessage NULL msgInfo msgid(%1)").arg(message->msgid);
        return;
    }
    _name = QString(msgInfo->name);
    qCDebug(MAVLinkInspectorLog) << "New Message:" << _name;
    for (unsigned int i = 0; i < msgInfo->num_fields; ++i) {
        QString type = QString("?");
        switch (msgInfo->fields[i].type) {
            case MAVLINK_TYPE_CHAR:     type = QString("char");     break;
            case MAVLINK_TYPE_UINT8_T:  type = QString("uint8_t");  break;
            case MAVLINK_TYPE_INT8_T:   type = QString("int8_t");   break;
            case MAVLINK_TYPE_UINT16_T: type = QString("uint16_t"); break;
            case MAVLINK_TYPE_INT16_T:  type = QString("int16_t");  break;
            case MAVLINK_TYPE_UINT32_T: type = QString("uint32_t"); break;
            case MAVLINK_TYPE_INT32_T:  type = QString("int32_t");  break;
            case MAVLINK_TYPE_FLOAT:    type = QString("float");    break;
            case MAVLINK_TYPE_DOUBLE:   type = QString("double");   break;
            case MAVLINK_TYPE_UINT64_T: type = QString("uint64_t"); break;
            case MAVLINK_TYPE_INT64_T:  type = QString("int64_t");  break;
        }
        QGCMAVLinkMessageField* f = new QGCMAVLinkMessageField(this, msgInfo->fields[i].name, type);
        _fields.append(f);
    }
}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
//-----------------------------------------------------------------------------
void
QGCMAVLinkMessage::select()
{
    bool sel = false;
    for (int i = 0; i < _fields.count(); ++i) {
        QGCMAVLinkMessageField* f = qobject_cast<QGCMAVLinkMessageField*>(_fields.get(i));
        if(f) {
            if(f->selected()) {
                sel = true;
                break;
            }
        }
    }
    if(sel != _selected) {
        _selected = sel;
        emit selectedChanged();
    }
}

Gus Grubba's avatar
Gus Grubba committed
198 199 200 201 202 203 204 205 206 207
//-----------------------------------------------------------------------------
void
QGCMAVLinkMessage::updateFreq()
{
    quint64 msgCount = _count - _lastCount;
    _messageHz = (0.2 * _messageHz) + (0.8 * msgCount);
    _lastCount = _count;
    emit freqChanged();
}

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
//-----------------------------------------------------------------------------
void
QGCMAVLinkMessage::update(mavlink_message_t* message)
{
    _message = *message;
    _count++;
    const mavlink_message_info_t* msgInfo = mavlink_get_message_info(message);
    if (!msgInfo) {
        qWarning() << QStringLiteral("QGCMAVLinkMessage::update NULL msgInfo msgid(%1)").arg(message->msgid);
        return;
    }
    if(_fields.count() != static_cast<int>(msgInfo->num_fields)) {
        qWarning() << QStringLiteral("QGCMAVLinkMessage::update msgInfo field count mismatch msgid(%1)").arg(message->msgid);
        return;
    }
    uint8_t* m = reinterpret_cast<uint8_t*>(&message->payload64[0]);
    for (unsigned int i = 0; i < msgInfo->num_fields; ++i) {
        QGCMAVLinkMessageField* f = qobject_cast<QGCMAVLinkMessageField*>(_fields.get(static_cast<int>(i)));
        if(f) {
            switch (msgInfo->fields[i].type) {
            case MAVLINK_TYPE_CHAR:
229
                f->setSelectable(false);
230 231 232 233 234
                if (msgInfo->fields[i].array_length > 0) {
                    char* str = reinterpret_cast<char*>(m+ msgInfo->fields[i].wire_offset);
                    // Enforce null termination
                    str[msgInfo->fields[i].array_length - 1] = '\0';
                    QString v(str);
235
                    f->updateValue(v, 0);
236 237 238 239
                } else {
                    // Single char
                    char b = *(reinterpret_cast<char*>(m + msgInfo->fields[i].wire_offset));
                    QString v(b);
240
                    f->updateValue(v, 0);
241 242 243 244 245 246 247 248
                }
                break;
            case MAVLINK_TYPE_UINT8_T:
                if (msgInfo->fields[i].array_length > 0) {
                    uint8_t* nums = m+msgInfo->fields[i].wire_offset;
                    // Enforce null termination
                    QString tmp("%1, ");
                    QString string;
249
                    for (unsigned int j = 0; j < msgInfo->fields[i].array_length - 1; ++j) {
250 251
                        string += tmp.arg(nums[j]);
                    }
252 253
                    string += QString::number(nums[msgInfo->fields[i].array_length - 1]);
                    f->updateValue(string, static_cast<qreal>(nums[0]));
254 255 256
                } else {
                    // Single value
                    uint8_t u = *(m+msgInfo->fields[i].wire_offset);
257
                    f->updateValue(QString::number(u), static_cast<qreal>(u));
258 259 260 261 262 263 264 265
                }
                break;
            case MAVLINK_TYPE_INT8_T:
                if (msgInfo->fields[i].array_length > 0) {
                    int8_t* nums = reinterpret_cast<int8_t*>(m + msgInfo->fields[i].wire_offset);
                    // Enforce null termination
                    QString tmp("%1, ");
                    QString string;
266
                    for (unsigned int j = 0; j < msgInfo->fields[i].array_length - 1; ++j) {
267 268
                        string += tmp.arg(nums[j]);
                    }
269 270
                    string += QString::number(nums[msgInfo->fields[i].array_length - 1]);
                    f->updateValue(string, static_cast<qreal>(nums[0]));
271 272 273
                } else {
                    // Single value
                    int8_t n = *(reinterpret_cast<int8_t*>(m+msgInfo->fields[i].wire_offset));
274
                    f->updateValue(QString::number(n), static_cast<qreal>(n));
275 276 277 278 279 280 281 282
                }
                break;
            case MAVLINK_TYPE_UINT16_T:
                if (msgInfo->fields[i].array_length > 0) {
                    uint16_t* nums = reinterpret_cast<uint16_t*>(m + msgInfo->fields[i].wire_offset);
                    // Enforce null termination
                    QString tmp("%1, ");
                    QString string;
283
                    for (unsigned int j = 0; j < msgInfo->fields[i].array_length - 1; ++j) {
284 285
                        string += tmp.arg(nums[j]);
                    }
286 287
                    string += QString::number(nums[msgInfo->fields[i].array_length - 1]);
                    f->updateValue(string, static_cast<qreal>(nums[0]));
288 289 290
                } else {
                    // Single value
                    uint16_t n = *(reinterpret_cast<uint16_t*>(m + msgInfo->fields[i].wire_offset));
291
                    f->updateValue(QString::number(n), static_cast<qreal>(n));
292 293 294 295 296 297 298 299
                }
                break;
            case MAVLINK_TYPE_INT16_T:
                if (msgInfo->fields[i].array_length > 0) {
                    int16_t* nums = reinterpret_cast<int16_t*>(m + msgInfo->fields[i].wire_offset);
                    // Enforce null termination
                    QString tmp("%1, ");
                    QString string;
300
                    for (unsigned int j = 0; j < msgInfo->fields[i].array_length - 1; ++j) {
301 302
                        string += tmp.arg(nums[j]);
                    }
303 304
                    string += QString::number(nums[msgInfo->fields[i].array_length - 1]);
                    f->updateValue(string, static_cast<qreal>(nums[0]));
305 306 307
                } else {
                    // Single value
                    int16_t n = *(reinterpret_cast<int16_t*>(m + msgInfo->fields[i].wire_offset));
308
                    f->updateValue(QString::number(n), static_cast<qreal>(n));
309 310 311 312 313 314 315 316
                }
                break;
            case MAVLINK_TYPE_UINT32_T:
                if (msgInfo->fields[i].array_length > 0) {
                    uint32_t* nums = reinterpret_cast<uint32_t*>(m + msgInfo->fields[i].wire_offset);
                    // Enforce null termination
                    QString tmp("%1, ");
                    QString string;
317
                    for (unsigned int j = 0; j < msgInfo->fields[i].array_length - 1; ++j) {
318 319
                        string += tmp.arg(nums[j]);
                    }
320 321
                    string += QString::number(nums[msgInfo->fields[i].array_length - 1]);
                    f->updateValue(string, static_cast<qreal>(nums[0]));
322 323 324
                } else {
                    // Single value
                    uint32_t n = *(reinterpret_cast<uint32_t*>(m + msgInfo->fields[i].wire_offset));
Gus Grubba's avatar
Gus Grubba committed
325 326 327
                    //-- Special case
                    if(_message.msgid == MAVLINK_MSG_ID_SYSTEM_TIME) {
                        QDateTime d = QDateTime::fromMSecsSinceEpoch(static_cast<qint64>(n),Qt::UTC,0);
328
                        f->updateValue(d.toString("HH:mm:ss"), static_cast<qreal>(n));
Gus Grubba's avatar
Gus Grubba committed
329
                    } else {
330
                        f->updateValue(QString::number(n), static_cast<qreal>(n));
Gus Grubba's avatar
Gus Grubba committed
331
                    }
332 333 334 335 336 337 338 339
                }
                break;
            case MAVLINK_TYPE_INT32_T:
                if (msgInfo->fields[i].array_length > 0) {
                    int32_t* nums = reinterpret_cast<int32_t*>(m + msgInfo->fields[i].wire_offset);
                    // Enforce null termination
                    QString tmp("%1, ");
                    QString string;
340
                    for (unsigned int j = 0; j < msgInfo->fields[i].array_length - 1; ++j) {
341 342
                        string += tmp.arg(nums[j]);
                    }
343 344
                    string += QString::number(nums[msgInfo->fields[i].array_length - 1]);
                    f->updateValue(string, static_cast<qreal>(nums[0]));
345 346 347
                } else {
                    // Single value
                    int32_t n = *(reinterpret_cast<int32_t*>(m + msgInfo->fields[i].wire_offset));
348
                    f->updateValue(QString::number(n), static_cast<qreal>(n));
349 350 351 352 353 354 355 356
                }
                break;
            case MAVLINK_TYPE_FLOAT:
                if (msgInfo->fields[i].array_length > 0) {
                    float* nums = reinterpret_cast<float*>(m + msgInfo->fields[i].wire_offset);
                    // Enforce null termination
                    QString tmp("%1, ");
                    QString string;
357
                    for (unsigned int j = 0; j < msgInfo->fields[i].array_length - 1; ++j) {
358 359
                       string += tmp.arg(static_cast<double>(nums[j]));
                    }
360 361
                    string += QString::number(static_cast<double>(nums[msgInfo->fields[i].array_length - 1]));
                    f->updateValue(string, static_cast<qreal>(nums[0]));
362 363 364
                } else {
                    // Single value
                    float fv = *(reinterpret_cast<float*>(m + msgInfo->fields[i].wire_offset));
365
                    f->updateValue(QString::number(static_cast<double>(fv)), static_cast<qreal>(fv));
366 367 368 369 370 371 372 373
                }
                break;
            case MAVLINK_TYPE_DOUBLE:
                if (msgInfo->fields[i].array_length > 0) {
                    double* nums = reinterpret_cast<double*>(m + msgInfo->fields[i].wire_offset);
                    // Enforce null termination
                    QString tmp("%1, ");
                    QString string;
374
                    for (unsigned int j = 0; j < msgInfo->fields[i].array_length - 1; ++j) {
375 376
                        string += tmp.arg(nums[j]);
                    }
377 378
                    string += QString::number(static_cast<double>(nums[msgInfo->fields[i].array_length - 1]));
                    f->updateValue(string, static_cast<qreal>(nums[0]));
379 380 381
                } else {
                    // Single value
                    double d = *(reinterpret_cast<double*>(m + msgInfo->fields[i].wire_offset));
382
                    f->updateValue(QString::number(d), static_cast<qreal>(d));
383 384 385 386 387 388 389 390
                }
                break;
            case MAVLINK_TYPE_UINT64_T:
                if (msgInfo->fields[i].array_length > 0) {
                    uint64_t* nums = reinterpret_cast<uint64_t*>(m + msgInfo->fields[i].wire_offset);
                    // Enforce null termination
                    QString tmp("%1, ");
                    QString string;
391
                    for (unsigned int j = 0; j < msgInfo->fields[i].array_length - 1; ++j) {
392 393
                        string += tmp.arg(nums[j]);
                    }
394 395
                    string += QString::number(nums[msgInfo->fields[i].array_length - 1]);
                    f->updateValue(string, static_cast<qreal>(nums[0]));
396 397 398
                } else {
                    // Single value
                    uint64_t n = *(reinterpret_cast<uint64_t*>(m + msgInfo->fields[i].wire_offset));
Gus Grubba's avatar
Gus Grubba committed
399 400 401
                    //-- Special case
                    if(_message.msgid == MAVLINK_MSG_ID_SYSTEM_TIME) {
                        QDateTime d = QDateTime::fromMSecsSinceEpoch(n/1000,Qt::UTC,0);
402
                        f->updateValue(d.toString("yyyy MM dd HH:mm:ss"), static_cast<qreal>(n));
Gus Grubba's avatar
Gus Grubba committed
403
                    } else {
404
                        f->updateValue(QString::number(n), static_cast<qreal>(n));
Gus Grubba's avatar
Gus Grubba committed
405
                    }
406 407 408 409 410 411 412 413
                }
                break;
            case MAVLINK_TYPE_INT64_T:
                if (msgInfo->fields[i].array_length > 0) {
                    int64_t* nums = reinterpret_cast<int64_t*>(m + msgInfo->fields[i].wire_offset);
                    // Enforce null termination
                    QString tmp("%1, ");
                    QString string;
414
                    for (unsigned int j = 0; j < msgInfo->fields[i].array_length - 1; ++j) {
415 416
                        string += tmp.arg(nums[j]);
                    }
417 418
                    string += QString::number(nums[msgInfo->fields[i].array_length - 1]);
                    f->updateValue(string, static_cast<qreal>(nums[0]));
419 420 421
                } else {
                    // Single value
                    int64_t n = *(reinterpret_cast<int64_t*>(m + msgInfo->fields[i].wire_offset));
422
                    f->updateValue(QString::number(n), static_cast<qreal>(n));
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
                }
                break;
            }
        }
    }
    emit messageChanged();
}

//-----------------------------------------------------------------------------
QGCMAVLinkVehicle::QGCMAVLinkVehicle(QObject* parent, quint8 id)
    : QObject(parent)
    , _id(id)
{
    qCDebug(MAVLinkInspectorLog) << "New Vehicle:" << id;
}

//-----------------------------------------------------------------------------
QGCMAVLinkMessage*
Gus Grubba's avatar
Gus Grubba committed
441
QGCMAVLinkVehicle::findMessage(uint32_t id, uint8_t cid)
442 443 444 445
{
    for(int i = 0; i < _messages.count(); i++) {
        QGCMAVLinkMessage* m = qobject_cast<QGCMAVLinkMessage*>(_messages.get(i));
        if(m) {
Gus Grubba's avatar
Gus Grubba committed
446
            if(m->id() == id && m->cid() == cid) {
447 448 449 450 451 452 453
                return m;
            }
        }
    }
    return nullptr;
}

Gus Grubba's avatar
Gus Grubba committed
454 455 456 457 458 459 460 461 462 463 464
//-----------------------------------------------------------------------------
static bool
messages_sort(QObject* a, QObject* b)
{
    QGCMAVLinkMessage* aa = qobject_cast<QGCMAVLinkMessage*>(a);
    QGCMAVLinkMessage* bb = qobject_cast<QGCMAVLinkMessage*>(b);
    if(!aa || !bb) return false;
    if(aa->id() == bb->id()) return aa->cid() < bb->cid();
    return aa->id() < bb->id();
}

465 466 467 468 469
//-----------------------------------------------------------------------------
void
QGCMAVLinkVehicle::append(QGCMAVLinkMessage* message)
{
    _messages.append(message);
Gus Grubba's avatar
Gus Grubba committed
470 471 472 473 474 475 476 477 478
    //-- Sort messages by id and then cid
    if(_messages.count() > 0) {
        std::sort(_messages.objectList()->begin(), _messages.objectList()->end(), messages_sort);
        for(int i = 0; i < _messages.count(); i++) {
            QGCMAVLinkMessage* m = qobject_cast<QGCMAVLinkMessage*>(_messages.get(i));
            if(m) {
                emit m->indexChanged();
            }
        }
479
        _checkCompID(message);
Gus Grubba's avatar
Gus Grubba committed
480
    }
481 482 483
    emit messagesChanged();
}

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
//-----------------------------------------------------------------------------
void
QGCMAVLinkVehicle::_checkCompID(QGCMAVLinkMessage* message)
{
    if(_compIDsStr.isEmpty()) {
        _compIDsStr << tr("All");
    }
    if(!_compIDs.contains(static_cast<int>(message->cid()))) {
        int cid = static_cast<int>(message->cid());
        _compIDs.append(cid);
        _compIDsStr << QString::number(cid);
        emit compIDsChanged();
    }
}

Gus Grubba's avatar
Gus Grubba committed
499 500 501 502 503 504 505 506
//-----------------------------------------------------------------------------
MAVLinkInspectorController::TimeScale_st::TimeScale_st(QObject* parent, const QString& l, uint32_t t)
    : QObject(parent)
    , label(l)
    , timeScale(t)
{
}

507 508 509 510 511 512 513 514
//-----------------------------------------------------------------------------
MAVLinkInspectorController::Range_st::Range_st(QObject* parent, const QString& l, qreal r)
    : QObject(parent)
    , label(l)
    , range(r)
{
}

515 516 517 518 519 520 521 522
//-----------------------------------------------------------------------------
MAVLinkInspectorController::MAVLinkInspectorController()
{
    MultiVehicleManager* multiVehicleManager = qgcApp()->toolbox()->multiVehicleManager();
    connect(multiVehicleManager, &MultiVehicleManager::vehicleAdded,   this, &MAVLinkInspectorController::_vehicleAdded);
    connect(multiVehicleManager, &MultiVehicleManager::vehicleRemoved, this, &MAVLinkInspectorController::_vehicleRemoved);
    MAVLinkProtocol* mavlinkProtocol = qgcApp()->toolbox()->mavlinkProtocol();
    connect(mavlinkProtocol, &MAVLinkProtocol::messageReceived, this, &MAVLinkInspectorController::_receiveMessage);
Gus Grubba's avatar
Gus Grubba committed
523 524 525 526
    connect(&_updateFrequencyTimer, &QTimer::timeout, this, &MAVLinkInspectorController::_refreshFrequency);
    connect(&_updateSeriesTimer, &QTimer::timeout, this, &MAVLinkInspectorController::_refreshSeries);
    _updateFrequencyTimer.start(1000);
    _updateSeriesTimer.start(UPDATE_FREQUENCY);
Gus Grubba's avatar
Gus Grubba committed
527 528
    MultiVehicleManager *manager = qgcApp()->toolbox()->multiVehicleManager();
    connect(manager, &MultiVehicleManager::activeVehicleChanged, this, &MAVLinkInspectorController::_setActiveVehicle);
Gus Grubba's avatar
Gus Grubba committed
529 530 531 532 533
    _timeScaleSt.append(new TimeScale_st(this, tr("5 Sec"),   5 * 1000));
    _timeScaleSt.append(new TimeScale_st(this, tr("10 Sec"), 10 * 1000));
    _timeScaleSt.append(new TimeScale_st(this, tr("30 Sec"), 30 * 1000));
    _timeScaleSt.append(new TimeScale_st(this, tr("60 Sec"), 60 * 1000));
    emit timeScalesChanged();
534 535 536 537 538 539 540 541 542 543 544 545
    _rangeSt.append(new Range_st(this, tr("Auto"),    0));
    _rangeSt.append(new Range_st(this, tr("10,000"),  10000));
    _rangeSt.append(new Range_st(this, tr("1,000"),   1000));
    _rangeSt.append(new Range_st(this, tr("100"),     100));
    _rangeSt.append(new Range_st(this, tr("10"),      10));
    _rangeSt.append(new Range_st(this, tr("1"),       1));
    _rangeSt.append(new Range_st(this, tr("0.1"),     0.1));
    _rangeSt.append(new Range_st(this, tr("0.01"),    0.01));
    _rangeSt.append(new Range_st(this, tr("0.001"),   0.001));
    _rangeSt.append(new Range_st(this, tr("0.0001"),  0.0001));
    emit rangeListChanged();
    updateXRange();
546 547 548 549 550 551 552
}

//-----------------------------------------------------------------------------
MAVLinkInspectorController::~MAVLinkInspectorController()
{
}

Gus Grubba's avatar
Gus Grubba committed
553 554 555 556 557 558 559 560 561 562 563 564
//----------------------------------------------------------------------------------------
QStringList
MAVLinkInspectorController::timeScales()
{
    if(!_timeScales.count()) {
        for(int i = 0; i < _timeScaleSt.count(); i++) {
            _timeScales << _timeScaleSt[i]->label;
        }
    }
    return _timeScales;
}

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
//----------------------------------------------------------------------------------------
QStringList
MAVLinkInspectorController::rangeList()
{
    if(!_rangeList.count()) {
        for(int i = 0; i < _rangeSt.count(); i++) {
            _rangeList << _rangeSt[i]->label;
        }
    }
    return _rangeList;
}

//-----------------------------------------------------------------------------
void
MAVLinkInspectorController::setLeftRangeIdx(quint32 r)
{
    if(r < static_cast<quint32>(_rangeSt.count())) {
        _leftRangeIndex = r;
Gus Grubba's avatar
Gus Grubba committed
583
        qreal range = _rangeSt[static_cast<int>(r)]->range;
584 585 586
        emit leftRangeChanged();
        //-- If not Auto, use defined range
        if(_leftRangeIndex > 0) {
Gus Grubba's avatar
Gus Grubba committed
587
            _leftRangeMin = -range;
588
            emit leftRangeMinChanged();
Gus Grubba's avatar
Gus Grubba committed
589
            _leftRangeMax = range;
590 591 592 593 594 595 596 597 598 599 600
            emit leftRangeMaxChanged();
        }
    }
}

//-----------------------------------------------------------------------------
void
MAVLinkInspectorController::setRightRangeIdx(quint32 r)
{
    if(r < static_cast<quint32>(_rangeSt.count())) {
        _rightRangeIndex = r;
Gus Grubba's avatar
Gus Grubba committed
601
        qreal range = _rangeSt[static_cast<int>(r)]->range;
602 603 604
        emit rightRangeChanged();
        //-- If not Auto, use defined range
        if(_rightRangeIndex > 0) {
Gus Grubba's avatar
Gus Grubba committed
605
            _rightRangeMin = -range;
606
            emit rightRangeMinChanged();
Gus Grubba's avatar
Gus Grubba committed
607
            _rightRangeMax = range;
608 609 610 611 612
            emit rightRangeMaxChanged();
        }
    }
}

Gus Grubba's avatar
Gus Grubba committed
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
//----------------------------------------------------------------------------------------
void
MAVLinkInspectorController::_setActiveVehicle(Vehicle* vehicle)
{
    if(vehicle) {
        QGCMAVLinkVehicle* v = _findVehicle(static_cast<uint8_t>(vehicle->id()));
        if(v) {
            _activeVehicle = v;
        } else {
            _activeVehicle = nullptr;
        }
    } else {
        _activeVehicle = nullptr;
    }
    emit activeVehiclesChanged();
}

630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
//-----------------------------------------------------------------------------
QGCMAVLinkVehicle*
MAVLinkInspectorController::_findVehicle(uint8_t id)
{
    for(int i = 0; i < _vehicles.count(); i++) {
        QGCMAVLinkVehicle* v = qobject_cast<QGCMAVLinkVehicle*>(_vehicles.get(i));
        if(v) {
            if(v->id() == id) {
                return v;
            }
        }
    }
    return nullptr;
}

Gus Grubba's avatar
Gus Grubba committed
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
//-----------------------------------------------------------------------------
void
MAVLinkInspectorController::_refreshFrequency()
{
    for(int i = 0; i < _vehicles.count(); i++) {
        QGCMAVLinkVehicle* v = qobject_cast<QGCMAVLinkVehicle*>(_vehicles.get(i));
        if(v) {
            for(int i = 0; i < v->messages()->count(); i++) {
                QGCMAVLinkMessage* m = qobject_cast<QGCMAVLinkMessage*>(v->messages()->get(i));
                if(m) {
                    m->updateFreq();
                }
            }
        }
    }
}

662 663 664 665
//-----------------------------------------------------------------------------
void
MAVLinkInspectorController::_vehicleAdded(Vehicle* vehicle)
{
666 667
    QGCMAVLinkVehicle* v = _findVehicle(static_cast<uint8_t>(vehicle->id()));
    if(v) {
668
        v->messages()->clearAndDeleteContents();
669 670 671 672
        emit v->messagesChanged();
    } else {
        v = new QGCMAVLinkVehicle(this, static_cast<uint8_t>(vehicle->id()));
        _vehicles.append(v);
673
        _vehicleNames.append(tr("Vehicle %1").arg(vehicle->id()));
674 675
    }
    emit vehiclesChanged();
676 677 678 679 680 681
}

//-----------------------------------------------------------------------------
void
MAVLinkInspectorController::_vehicleRemoved(Vehicle* vehicle)
{
682 683 684 685
    QGCMAVLinkVehicle* v = _findVehicle(static_cast<uint8_t>(vehicle->id()));
    if(v) {
        v->deleteLater();
        _vehicles.removeOne(v);
686
        QString vs = tr("Vehicle %1").arg(vehicle->id());
687 688
        _vehicleNames.removeOne(vs);
        emit vehiclesChanged();
689 690 691 692 693
    }
}

//-----------------------------------------------------------------------------
void
694
MAVLinkInspectorController::_receiveMessage(LinkInterface*, mavlink_message_t message)
695
{
696 697 698 699 700
    QGCMAVLinkMessage* m = nullptr;
    QGCMAVLinkVehicle* v = _findVehicle(message.sysid);
    if(!v) {
        v = new QGCMAVLinkVehicle(this, message.sysid);
        _vehicles.append(v);
701
        _vehicleNames.append(tr("Vehicle %1").arg(message.sysid));
702
        emit vehiclesChanged();
Gus Grubba's avatar
Gus Grubba committed
703 704 705 706
        if(!_activeVehicle) {
            _activeVehicle = v;
            emit activeVehiclesChanged();
        }
707
    } else {
Gus Grubba's avatar
Gus Grubba committed
708
        m = v->findMessage(message.msgid, message.compid);
709
    }
710 711 712
    if(!m) {
        m = new QGCMAVLinkMessage(this, &message);
        v->append(m);
713
    } else {
714
        m->update(&message);
715 716 717 718 719
    }
}

//-----------------------------------------------------------------------------
void
720
MAVLinkInspectorController::addChartField(QGCMAVLinkMessageField* field, bool left)
721 722
{
    QVariant f = QVariant::fromValue(field);
723 724 725 726 727 728 729 730
    QVariantList* pList;
    if(left) {
        pList = &_leftChartFields;
    } else {
        pList = &_rightChartFields;
    }
    for(int i = 0; i < pList->count(); i++) {
        if(pList->at(i) == f) {
731 732 733
            return;
        }
    }
734 735 736 737 738 739 740
    pList->append(f);
    if(left) {
        emit leftChartFieldsChanged();
    } else {
        emit rightChartFieldsChanged();
    }
    emit seriesCountChanged();
741 742 743 744
}

//-----------------------------------------------------------------------------
void
745
MAVLinkInspectorController::delChartField(QGCMAVLinkMessageField* field, bool left)
746 747
{
    QVariant f = QVariant::fromValue(field);
748 749 750 751 752 753 754 755 756 757 758 759 760
    QVariantList* pList;
    if(left) {
        pList = &_leftChartFields;
    } else {
        pList = &_rightChartFields;
    }
    for(int i = 0; i < pList->count(); i++) {
        if(pList->at(i) == f) {
            pList->removeAt(i);
            if(left) {
                emit leftChartFieldsChanged();
            } else {
                emit rightChartFieldsChanged();
761
            }
762
            emit seriesCountChanged();
763 764 765 766 767
            return;
        }
    }
}

768 769 770 771 772 773 774 775 776
//-----------------------------------------------------------------------------
void
MAVLinkInspectorController::setTimeScale(quint32 t)
{
    _timeScale = t;
    emit timeScaleChanged();
    updateXRange();
}

777 778 779 780
//-----------------------------------------------------------------------------
void
MAVLinkInspectorController::updateXRange()
{
Gus Grubba's avatar
Gus Grubba committed
781 782 783 784 785 786
    if(_timeScale < static_cast<quint32>(_timeScaleSt.count())) {
        qint64 t = static_cast<qint64>(QGC::groundTimeMilliseconds());
        _rangeXMax = QDateTime::fromMSecsSinceEpoch(t);
        _rangeXMin = QDateTime::fromMSecsSinceEpoch(t - _timeScaleSt[static_cast<int>(_timeScale)]->timeScale);
        emit rangeMinXChanged();
        emit rangeMaxXChanged();
787
    }
788
}
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838

//-----------------------------------------------------------------------------
void
MAVLinkInspectorController::updateYRange(bool left)
{
    QVariantList* pList;
    if(left) {
        pList = &_leftChartFields;
    } else {
        pList = &_rightChartFields;
    }
    if(pList->count()) {
        qreal vmin  = std::numeric_limits<qreal>::max();
        qreal vmax  = std::numeric_limits<qreal>::min();
        for(int i = 0; i < pList->count(); i++) {
            QObject* object = qvariant_cast<QObject*>(pList->at(i));
            QGCMAVLinkMessageField* pField = qobject_cast<QGCMAVLinkMessageField*>(object);
            if(pField) {
                if(vmax < pField->rangeMax()) vmax = pField->rangeMax();
                if(vmin > pField->rangeMin()) vmin = pField->rangeMin();
            }
        }
        if(left) {
            if(std::abs(_leftRangeMin - vmin) > 0.000001) {
                _leftRangeMin = vmin;
                emit leftRangeMinChanged();
            }
            if(std::abs(_leftRangeMax - vmax) > 0.000001) {
                _leftRangeMax = vmax;
                emit leftRangeMaxChanged();
            }
        } else {
            if(std::abs(_rightRangeMin - vmin) > 0.000001) {
                _rightRangeMin = vmin;
                emit rightRangeMinChanged();
            }
            if(std::abs(_rightRangeMax - vmax) > 0.000001) {
                _rightRangeMax = vmax;
                emit rightRangeMaxChanged();
            }
        }
    }
}

//-----------------------------------------------------------------------------
void
MAVLinkInspectorController::addSeries(QGCMAVLinkMessageField* field, QAbstractSeries* series, bool left)
{
    if(field) {
        field->addSeries(series, left);
Gus Grubba's avatar
Gus Grubba committed
839
        _updateSeriesTimer.start(UPDATE_FREQUENCY);
840 841 842 843 844 845 846 847 848 849 850 851
    }
}

//-----------------------------------------------------------------------------
void
MAVLinkInspectorController::delSeries(QGCMAVLinkMessageField* field)
{
    if(field) {
        field->delSeries();
    }
    if(_leftChartFields.count() == 0 && _rightChartFields.count() == 0) {
        updateXRange();
Gus Grubba's avatar
Gus Grubba committed
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
        _updateSeriesTimer.stop();
    }
}

//-----------------------------------------------------------------------------
void
MAVLinkInspectorController::_refreshSeries()
{
    updateXRange();
    for(int i = 0; i < _leftChartFields.count(); i++) {
        QObject* object = qvariant_cast<QObject*>(_leftChartFields.at(i));
        QGCMAVLinkMessageField* pField = qobject_cast<QGCMAVLinkMessageField*>(object);
        if(pField) {
            pField->updateSeries();
        }
    }
    for(int i = 0; i < _rightChartFields.count(); i++) {
        QObject* object = qvariant_cast<QObject*>(_rightChartFields.at(i));
        QGCMAVLinkMessageField* pField = qobject_cast<QGCMAVLinkMessageField*>(object);
        if(pField) {
            pField->updateSeries();
        }
874 875
    }
}