MAVLinkInspectorController.cc 6.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
/****************************************************************************
 *
 *   (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"

//-----------------------------------------------------------------------------
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);
}

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

//-----------------------------------------------------------------------------
void
MAVLinkInspectorController::_vehicleAdded(Vehicle* vehicle)
{
    _vehicleIDs.append(vehicle->id());
}

//-----------------------------------------------------------------------------
void
MAVLinkInspectorController::_vehicleRemoved(Vehicle* vehicle)
{
    int idx = _vehicleIDs.indexOf(vehicle->id());
    if(idx >= 0) {
        _vehicleIDs.removeAt(idx);
    }
}

//-----------------------------------------------------------------------------
void
MAVLinkInspectorController::_receiveMessage(LinkInterface* link,mavlink_message_t message)
{
    Q_UNUSED(link);
    quint64 receiveTime;
    if (_selectedSystemID    != 0 && _selectedSystemID    != message.sysid)  return;
    if (_selectedComponentID != 0 && _selectedComponentID != message.compid) return;
    // Create dynamically an array to store the messages for each UAS
    if (!_uasMessageStorage.contains(message.sysid)) {
        mavlink_message_t* msg = new mavlink_message_t;
        *msg = message;
        _uasMessageStorage.insertMulti(message.sysid,msg);
    }
    bool msgFound = false;
    QMap<int, mavlink_message_t*>::const_iterator iteMsg = _uasMessageStorage.find(message.sysid);
    mavlink_message_t* uasMessage = iteMsg.value();
    while((iteMsg != _uasMessageStorage.end()) && (iteMsg.key() == message.sysid)) {
        if (iteMsg.value()->msgid == message.msgid) {
            msgFound   = true;
            uasMessage = iteMsg.value();
            break;
        }
        ++iteMsg;
    }
    if (!msgFound) {
        mavlink_message_t* msgIdMessage = new mavlink_message_t;
        *msgIdMessage = message;
        _uasMessageStorage.insertMulti(message.sysid,msgIdMessage);
    } else {
        *uasMessage = message;
    }
    // Looking if this message has already been received once
    msgFound = false;
    QMap<int, QMap<int, quint64>* >::const_iterator ite = _uasLastMessageUpdate.find(message.sysid);
    QMap<int, quint64>* lastMsgUpdate = ite.value();
    while((ite != _uasLastMessageUpdate.end()) && (ite.key() == message.sysid)) {
        if(ite.value()->contains(message.msgid)) {
            msgFound = true;
            //-- Point to the found message
            lastMsgUpdate = ite.value();
            break;
        }
        ++ite;
    }
    receiveTime = QGC::groundTimeMilliseconds();
    //-- If the message doesn't exist, create a map for the frequency, message count and time of reception
    if(!msgFound) {
        //-- Create a map for the message frequency
        QMap<int, float>* messageHz = new QMap<int,float>;
        messageHz->insert(message.msgid,0.0f);
        _uasMessageHz.insertMulti(message.sysid,messageHz);
        //-- Create a map for the message count
        QMap<int, unsigned int>* messagesCount = new QMap<int, unsigned int>;
        messagesCount->insert(message.msgid,0);
        _uasMessageCount.insertMulti(message.sysid,messagesCount);
        //-- Create a map for the time of reception of the message
        QMap<int, quint64>* lastMessage = new QMap<int, quint64>;
        lastMessage->insert(message.msgid,receiveTime);
        _uasLastMessageUpdate.insertMulti(message.sysid,lastMessage);
        //-- Point to the created message
        lastMsgUpdate = lastMessage;
    } else {
        //-- The message has been found/created
        if((lastMsgUpdate->contains(message.msgid)) && (_uasMessageCount.contains(message.sysid))) {
            //-- Looking for and updating the message count
            unsigned int count = 0;
            QMap<int, QMap<int, unsigned int>* >::const_iterator iter = _uasMessageCount.find(message.sysid);
            QMap<int, unsigned int> * uasMsgCount = iter.value();
            while((iter != _uasMessageCount.end()) && (iter.key() == message.sysid)) {
                if(iter.value()->contains(message.msgid)) {
                    uasMsgCount = iter.value();
                    count = uasMsgCount->value(message.msgid,0);
                    uasMsgCount->insert(message.msgid,count+1);
                    break;
                }
                ++iter;
            }
        }
        lastMsgUpdate->insert(message.msgid,receiveTime);
    }
}

//-----------------------------------------------------------------------------
void
MAVLinkInspectorController::_reset()
{
    QMap<int, mavlink_message_t* >::iterator ite;
    for(ite = _uasMessageStorage.begin(); ite != _uasMessageStorage.end(); ++ite) {
        delete ite.value();
        ite.value() = nullptr;
    }
    _uasMessageStorage.clear();
    QMap<int, QMap<int, float>* >::iterator iteHz;
    for(iteHz = _uasMessageHz.begin(); iteHz != _uasMessageHz.end(); ++iteHz) {
        iteHz.value()->clear();
        delete iteHz.value();
        iteHz.value() = nullptr;
    }
    _uasMessageHz.clear();
    QMap<int, QMap<int, unsigned int>*>::iterator iteCount;
    for(iteCount = _uasMessageCount.begin(); iteCount != _uasMessageCount.end(); ++iteCount) {
        iteCount.value()->clear();
        delete iteCount.value();
        iteCount.value() = nullptr;
    }
    _uasMessageCount.clear();
    QMap<int, QMap<int, quint64>* >::iterator iteLast;
    for(iteLast = _uasLastMessageUpdate.begin(); iteLast != _uasLastMessageUpdate.end(); ++iteLast) {
        iteLast.value()->clear();
        delete iteLast.value();
        iteLast.value() = nullptr;
    }
    _uasLastMessageUpdate.clear();
}