机器人地面站-[QGroundControl源码解析]-[10]-[Comm]

前言

因为项目进度排期较紧,并且觉得之前在代码中添加注释的方法有些生硬用处不大,所以从本片开始,着重介绍类的内容和功能,只对重要代码进行粘贴。

Comm文件夹下有众多的类

一.LinkConfiguration

这个类处理链路的配置,查看属性可以看到有名称,具体的链路接口类,链路类型,是否动态的,是否自动连接的等等。

    Q_PROPERTY(QString          name                READ name           WRITE setName           NOTIFY nameChanged)
    Q_PROPERTY(LinkInterface*   link                READ link                                   NOTIFY linkChanged)
    Q_PROPERTY(LinkType         linkType            READ type                                   CONSTANT)
    Q_PROPERTY(bool             dynamic             READ isDynamic      WRITE setDynamic        NOTIFY dynamicChanged)
    Q_PROPERTY(bool             autoConnect         READ isAutoConnect  WRITE setAutoConnect    NOTIFY autoConnectChanged)
    Q_PROPERTY(QString          settingsURL         READ settingsURL                            CONSTANT)
    Q_PROPERTY(QString          settingsTitle       READ settingsTitle                          CONSTANT)
    Q_PROPERTY(bool             highLatency         READ isHighLatency  WRITE setHighLatency    NOTIFY highLatencyChanged)

 以下是qgc支持的链路类型

    ///  The link types supported by QGC QGC支持的链路类型
    ///  Any changes here MUST be reflected in LinkManager::linkTypeStrings() 这里的任何更改都必须反映在LinkManager::linkTypeStrings()中
    enum LinkType {
#ifndef NO_SERIAL_LINK
        TypeSerial,     ///< Serial Link 串口
#endif
        TypeUdp,        ///< UDP Link
        TypeTcp,        ///< TCP Link
#ifdef QGC_ENABLE_BLUETOOTH
        TypeBluetooth,  ///< Bluetooth Link 蓝牙
#endif
#ifdef QT_DEBUG
        TypeMock,       ///< Mock Link for Unitesting
#endif
        TypeLogReplay,
        TypeLast        // Last type value (type >= TypeLast == invalid)
    };
    Q_ENUM(LinkType)

其中类中有一个创建链路配置的工厂方法

/*!
  Configuration Factory
  @return A new instance of the given type
*/
LinkConfiguration* LinkConfiguration::createSettings(int type, const QString& name)
{
    LinkConfiguration* config = nullptr;
    switch(type) {
#ifndef NO_SERIAL_LINK
        case LinkConfiguration::TypeSerial:
            config = new SerialConfiguration(name);
            break;
#endif
        case LinkConfiguration::TypeUdp:
            config = new UDPConfiguration(name);
            break;
        case LinkConfiguration::TypeTcp:
            config = new TCPConfiguration(name);
            break;
#ifdef QGC_ENABLE_BLUETOOTH
    case LinkConfiguration::TypeBluetooth:
        config = new BluetoothConfiguration(name);
        break;
#endif
        case LinkConfiguration::TypeLogReplay:
            config = new LogReplayLinkConfiguration(name);
            break;
#ifdef QT_DEBUG
        case LinkConfiguration::TypeMock:
            config = new MockConfiguration(name);
            break;
#endif
    }
    return config;
}

根据不同的链路类型创建不同的链路配置类。

二.LinkInterface

该类定义用于与地面站应用程序通信的所有链接的接口。继承自thread,是一个用于通信的类。

三.Link Manager

该类负责组织物理链路。它可以管理任意链接,并负责连接它们,以及分配正确的协议实例将链接数据传输到应用程序。

四.LogReplayLink

该类的文件中还定义了一个类叫做LogReplayLinkConfiguration,继承自上边的LinkConfiguration,是针对本协议的定义的链接配置类。

第二个类就是LogReplayLink,他继承自上边的LinkInterface,用于专门处理logreplay出的信息。

五.MAVLinkProtocol

此类处理所有设备的mavlink协议相关的操作

/****************************************************************************
 *
 * (c) 2009-2020 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.
 *
 ****************************************************************************/

#pragma once

#include <QObject>
#include <QMutex>
#include <QString>
#include <QTimer>
#include <QFile>
#include <QMap>
#include <QByteArray>
#include <QLoggingCategory>

#include "LinkInterface.h"
#include "QGCMAVLink.h"
#include "QGC.h"
#include "QGCTemporaryFile.h"
#include "QGCToolbox.h"

class LinkManager;
class MultiVehicleManager;
class QGCApplication;

Q_DECLARE_LOGGING_CATEGORY(MAVLinkProtocolLog)

/**
 * @brief MAVLink micro air vehicle protocol reference implementation.
 * MAVLink微型飞行器协议参考实现。
 * MAVLink is a generic communication protocol for micro air vehicles.MAVLink是一种用于微型飞行器的通用通信协议。
 * for more information, please see the official website: https://mavlink.io更多信息请访问官方网站:https://mavlink.io
 **/
class MAVLinkProtocol : public QGCTool
{
    Q_OBJECT

public:
    MAVLinkProtocol(QGCApplication* app, QGCToolbox* toolbox);
    ~MAVLinkProtocol();

    /** @brief Get the human-friendly name of this protocol 获取此协议的人类友好的名称 */
    QString getName();
    /** @brief Get the system id of this application 获取此应用程序的系统id*/
    int getSystemId() const;
    /** @brief Get the component id of this application 获取此应用程序的组件id*/
    int getComponentId();

    /** @brief Get protocol version check state 获取协议版本检查状态*/
    bool versionCheckEnabled() const {
        return m_enable_version_check;
    }
    /** @brief Get the protocol version 获取协议版本*/
    int getVersion() {
        return MAVLINK_VERSION;
    }
    /** @brief Get the currently configured protocol version 获取当前配置的协议版本*/
    unsigned getCurrentVersion() const{
        return _current_version;
    }
    /**
     * Reset the counters for all metadata for this link.
     * 重置此链接的所有元数据的计数器
     */
    virtual void resetMetadataForLink(LinkInterface *link);

    /// Suspend/Restart logging during replay.在重播期间暂停/重新启动日志记录
    void suspendLogForReplay(bool suspend);

    /// Set protocol version 设置协议版本
    void setVersion(unsigned version);

    // Override from QGCTool
    virtual void setToolbox(QGCToolbox *toolbox);

public slots:
    /** @brief Receive bytes from a communication interface 从通信接口接收字节*/
    void receiveBytes(LinkInterface* link, QByteArray b);

    /** @brief Log bytes sent from a communication interface 从通信接口发送的日志字节*/
    void logSentBytes(LinkInterface* link, QByteArray b);

    /** @brief Set the system id of this application 设置此应用程序的系统id */
    void setSystemId(int id);

    /** @brief Enable / disable version check 启用/禁用版本检查*/
    void enableVersionCheck(bool enabled);

    /** @brief Load protocol settings 加载协议设置*/
    void loadSettings();
    /** @brief Store protocol settings 存储协议设置*/
    void storeSettings();

    /// @brief Deletes any log files which are in the temp directory 删除临时目录中的所有日志文件
    static void deleteTempLogFiles(void);

    /// Checks for lost log files 检查丢失的日志文件
    void checkForLostLogFiles(void);

protected:
    bool        m_enable_version_check;                         ///< Enable checking of version match of MAV and QGC 开启MAV和QGC版本匹配检查
    uint8_t     lastIndex[256][256];                            ///< Store the last received sequence ID for each system/componenet pair 为每个系统/组件对存储最后接收到的序列ID
    uint8_t     firstMessage[256][256];                         ///< First message flag 第一个消息标志
    uint64_t    totalReceiveCounter[MAVLINK_COMM_NUM_BUFFERS];  ///< The total number of successfully received messages 成功接收的消息总数
    uint64_t    totalLossCounter[MAVLINK_COMM_NUM_BUFFERS];     ///< Total messages lost during transmission. 传输过程中丢失的消息总数。
    float       runningLossPercent[MAVLINK_COMM_NUM_BUFFERS];   ///< Loss rate 丢失率

    mavlink_message_t _message; //消息
    mavlink_status_t _status; //消息状态

    bool        versionMismatchIgnore;
    int         systemId;
    unsigned    _current_version;
    int         _radio_version_mismatch_count;

signals:
    /// Heartbeat received on link 链路上收到心跳
    void vehicleHeartbeatInfo(LinkInterface* link, int vehicleId, int componentId, int vehicleFirmwareType, int vehicleType);

    /** @brief Message received and directly copied via signal 接收到的信息通过信号直接复制*/
    void messageReceived(LinkInterface* link, mavlink_message_t message);
    /** @brief Emitted if version check is enabled / disabled 如果启用/禁用版本检查,则触发*/
    void versionCheckChanged(bool enabled);
    /** @brief Emitted if a message from the protocol should reach the user 当来自协议的消息到达用户时发出*/
    void protocolStatusMessage(const QString& title, const QString& message);
    /** @brief Emitted if a new system ID was set 如果设置了新的系统ID,则触发*/
    void systemIdChanged(int systemId);

    void mavlinkMessageStatus(int uasId, uint64_t totalSent, uint64_t totalReceived, uint64_t totalLoss, float lossPercent);

    /**
     * @brief Emitted if a new radio status packet received 当接收到一个新的无线电状态包时发出
     *
     * @param rxerrors receive errors
     * @param fixed count of error corrected packets
     * @param rssi local signal strength in dBm
     * @param remrssi remote signal strength in dBm
     * @param txbuf how full the tx buffer is as a percentage
     * @param noise background noise level
     * @param remnoise remote background noise level
     */
    void radioStatusChanged(LinkInterface* link, unsigned rxerrors, unsigned fixed, int rssi, int remrssi,
    unsigned txbuf, unsigned noise, unsigned remnoise);

    /// Emitted when a temporary telemetry log file is ready for saving 当临时遥测日志文件准备好保存时发出
    void saveTelemetryLog(QString tempLogfile);

    /// Emitted when a telemetry log is started to save. 当遥测日志开始保存时发出
    void checkTelemetrySavePath(void);

private slots:
    void _vehicleCountChanged(void);

private:
    bool _closeLogFile(void);
    void _startLogging(void);
    void _stopLogging(void);

    bool _logSuspendError;      ///< true: Logging suspended due to error 日志记录由于错误而挂起
    bool _logSuspendReplay;     ///< true: Logging suspended due to replay 由于重放,日志暂停
    bool _vehicleWasArmed;      ///< true: Vehicle was armed during log sequence 在日志序列中,车辆被武装起来

    QGCTemporaryFile    _tempLogFile;            ///< File to log to
    static const char*  _tempLogFileTemplate;    ///< Template for temporary log file
    static const char*  _logFileExtension;       ///< Extension for log files

    LinkManager*            _linkMgr;
    MultiVehicleManager*    _multiVehicleManager;
};

总结:

后面的文件类似都是协议管理消息的收发,因此不再赘述。

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值