京東網(wǎng)站建設(shè)的意義seo信息網(wǎng)
在智能駕駛中,DDS有可能被廣泛使用,因此推出這篇說明教程。
1、基于【QT開發(fā)(5)】教程的項目文檔進行開發(fā)
2、安裝DDS
查看《【eProsima Fast DDS(1)】安裝eProsima Fast DDS》
至少安裝:
foonathan_memory_vendor,一個 STL 兼容的 C++ 內(nèi)存分配器 庫。
fastcdr,一個根據(jù) CDR 標準進行數(shù)據(jù)序列化的 C++ 庫。
fastrtps,eProsima Fast DDS庫的核心庫。
測試安裝是否正確的方法:在cmakelists 中加入
find_package(fastcdr REQUIRED)
find_package(foonathan_memory REQUIRED)
find_package(fastrtps REQUIRED)
如果cmake 提示找到了該庫,則表示庫ok。
3、在項目中添加通信message文件
我們一般是先寫 DDS 的idl 文件(本質(zhì)是定義定義數(shù)據(jù)結(jié)構(gòu)體類型),然后通過DDS的代碼生成工具生成 cpp 和hpp文件。如下圖:
因為我們使用的【QT開發(fā)(5)】教程的項目文檔進行開發(fā),哪個項目直接遍歷src 目錄里面全部的cpp和hpp文件。因此我們把message 復(fù)制進入src目錄即可,不用變更cmakelists
例如我們定義兩個數(shù)據(jù)結(jié)構(gòu)體PerceptionCommand 和WorkingStatus
module auto_msg {module msg {struct PerceptionCommand {uint64 time_stamp;uint8 system_command;uint8 system_reset;};};
};
module auto_msg {module msg {struct WorkingStatus {uint64 time_stamp;uint32 counter;};};
};
4、為了保持ROS2 代碼的風(fēng)格兼容,我們導(dǎo)入了rclcpp
目的:移植rclcpp 的publisher、subscription、和timer
因為我們使用的【QT開發(fā)(5)】教程的項目文檔進行開發(fā),哪個項目直接遍歷src 目錄里面全部的cpp和hpp文件。因此我們把rclcpp復(fù)制進入src目錄即可,不用變更cmakelists
5、修改cmakelists 增加fastrtps
增加
find_package(fastcdr REQUIRED)
find_package(foonathan_memory REQUIRED)
find_package(fastrtps REQUIRED)
修改target_link_libraries ,增加fastrtps
target_link_libraries( emptyApp fmtQt5::Widgets${OpenCV_LIBS}fastrtpspthread)
因為我們使用的【QT開發(fā)(5)】教程的項目文檔進行開發(fā),哪個項目直接遍歷src 目錄里面全部的cpp和hpp文件。因此我們把message 、rclcpp復(fù)制進入src目錄即可,不用變更cmakelists
6、增加一個基于DDS 通信的核心功能
我們建立一個example文件夾,建立example.cpp 和 example.hpp
先寫 example.hpp,首先引入頭文件
#include <rclcpp/rclcpp.hpp>
#include <PerceptionCommand.hpp> // 這個是message 里面定義的 dds 通信數(shù)據(jù)結(jié)構(gòu)體
#include <WorkingStatus.hpp> // 這個是message 里面定義的 dds 通信數(shù)據(jù)結(jié)構(gòu)體
然后建立一個對象Example class
class Example : public rclcpp::Node
{public:Example();~Example();int Init();private:// 1# 訂閱者rclcpp::Subscription<auto_msg::msg::PerceptionCommand>::SharedPtr perceptionCommand_sub_;// 2# 信息存儲的成員rclcpp::AtomicSet<auto_msg::msg::PerceptionCommand> perceptionCommand_;// 3#建立 訂閱者的回調(diào)函數(shù) msg_PerceptionCommand_callback,實現(xiàn)的數(shù)據(jù)存放在perceptionCommand_成員void msg_PerceptionCommand_callback(const auto_msg::msg::PerceptionCommand::SharedPtr msg);// 4# 建立一個50ms 的定時器rclcpp::TimerBase::SharedPtr workingStatus_timer_;// 5# 建立發(fā)布者 Publisherrclcpp::Publisher<auto_msg::msg::WorkingStatus>::SharedPtr workingStatus_pub_;rclcpp::AtomicSet<auto_msg::msg::WorkingStatus> workingStatus_;// 6# 定時器的回調(diào)函數(shù)void timer_WorkingStatus_callback();};
我們建立了6個函數(shù),分別的作用是
1、建立一個 perceptionCommand_sub_ 訂閱者
2、建立一個 perceptionCommand_ 的信息存儲的成員
3、建立 訂閱者的回調(diào)函數(shù) msg_PerceptionCommand_callback,實現(xiàn)把收到的數(shù)據(jù)存放在 perceptionCommand_ 的信息存儲的成員
里面
4、建立一個50ms 的定時器
5、建立一個 workingStatus_pub_ 的發(fā)布者;4、建立一個 workingStatus_ 成員
6、建立一個 定時器的回調(diào)函數(shù),處理perceptionCommand_ 成員
的數(shù)據(jù),并發(fā)送workingStatus_pub_ 數(shù)據(jù)。
example.cpp 里面這么寫
int Example::Init()
{counter_ = 0;workingStatus_pub_ = this->create_publisher<auto_msg::msg::WorkingStatus>("example_status_pub",2);perceptionCommand_sub_ = this->create_subscription<auto_msg::msg::PerceptionCommand>("perception_command_pub",2,std::bind(&Example::msg_PerceptionCommand_callback, this, _1));workingStatus_timer_ = this->create_wall_timer(50, std::bind(&Example::timer_WorkingStatus_callback, this)); // 50 millisecondreturn 0;
}void Example::timer_WorkingStatus_callback()
{// here is how use the msg which had receivedauto_msg::msg::PerceptionCommand perceptionCommand = perceptionCommand_.Get();if (perceptionCommand.system_command() == 0x06) {// for example, do sth what you want when command equal some valueint a = 1;}auto_msg::msg::WorkingStatus workingStatus;workingStatus.time_stamp() = rclcpp::PlatformGetMs();workingStatus_pub_->publish(workingStatus);
}void Example::msg_PerceptionCommand_callback(const auto_msg::msg::PerceptionCommand::SharedPtr msg)
{perceptionCommand_.Set(*msg);
}
7、修改main 文件
增加頭文件引用
#include <rclcpp/rclcpp.hpp>
#include "example.h"
main 函數(shù)里面加入
rclcpp::init(argc, argv);std::shared_ptr<yanyx::auto::Example> example = std::make_shared<yanyx::auto::Example>();example->Init();rclcpp::spin(example);
關(guān)于 rclcpp::spin()
的說明可以看我的另外一篇文章《【eProsima Fast DDS(2)】ROS2:spin() spin_some()函數(shù)》,這個是堵塞了main 函數(shù)。
整體上處理的思路是:
1、建立一個 perceptionCommand_sub_ 訂閱者
2、建立一個 perceptionCommand_ 的信息存儲的成員
3、建立 訂閱者的回調(diào)函數(shù) msg_PerceptionCommand_callback,實現(xiàn)把收到的數(shù)據(jù)存放在 perceptionCommand_ 的信息存儲的成員
里面
4、建立一個50ms 的定時器
5、建立一個 workingStatus_pub_ 的發(fā)布者;4、建立一個 workingStatus_ 成員
6、建立一個 定時器的回調(diào)函數(shù),處理perceptionCommand_ 成員
的數(shù)據(jù),并發(fā)送workingStatus_pub_ 數(shù)據(jù)。