【ROS/DDS】FastDDS:C++编写一个发布者和订阅者应用程序(三)
详细介绍如何创建具有发布者和订阅者的简单 Fast DDS 应用程序 逐步使用 C++ API
相关资源可下载:
一、条件
1.1先决条件
- 首先,您需要按照安装手册中概述的步骤安装 eProsima Fast DDS 及其所有依赖项。 您还需要完成安装手册中概述的安装 eProsima Fast DDS-Gen 工具的步骤。此外,本教程中提供的所有命令都是针对 Linux 的 环境。
#1.首先创建目录
mkdir workspace_DDSHelloWorld && cd workspace_DDSHelloWorld
mkdir src build
1.2 导入链接库及其相关依赖项
- DDS 应用程序需要 Fast DDS 和 Fast CDR 库。 根据安装过程,遵循使这些库可用于我们的 DDS 应用程序的过程 会略有不同。
- 如果我们从二进制文件或手动安装进行安装,则这些库已经可以访问 从工作区。 在 Linux 上,头文件可以分别位于快速 DDS 和快速 CDR 的目录 /usr/local/include/fastdds/ 和 /usr/local/include/fastcdr/ 中。两者的编译库可以在 目录 /usr/local/lib/。
二、编写 FastDDS 发布者和订阅者应用程序
- 我们将使用 CMake 工具来管理项目的构建。 使用您首选的文本编辑器,创建一个名为 CMakeLists.txt 的新文件,然后复制并粘贴以下代码片段。 将此文件保存在工作区的根目录中。如果您已执行这些步骤,则应该 workspace_DDSHelloWorld。
cmake_minimum_required(VERSION 3.20)project(DDSHelloWorld)# Find requirements
if(NOT fastcdr_FOUND)find_package(fastcdr 2 REQUIRED)
endif()if(NOT fastdds_FOUND)find_package(fastdds 3 REQUIRED)
endif()# Set C++11
include(CheckCXXCompilerFlag)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG ORCMAKE_CXX_COMPILER_ID MATCHES "Clang")check_cxx_compiler_flag(-std=c++11 SUPPORTS_CXX11)if(SUPPORTS_CXX11)add_compile_options(-std=c++11)else()message(FATAL_ERROR "Compiler doesn't support C++11")endif()
endif()message(STATUS "Configuring HelloWorld publisher/subscriber example...")
file(GLOB DDS_HELLOWORLD_SOURCES_CXX "src/*.cxx")
2.1 构建主题数据类型(创建 IDL 文件)
- eProsima Fast DDS-Gen 是一个 Java 应用程序,它使用 接口描述语言 (IDL) 文件。此应用程序可以执行两个不同的操作:
- 为您的自定义主题生成 C++ 定义。
- 生成使用您的主题数据的函数示例。
在 workspace 目录中,执行以下命令:
cd src && touch HelloWorld.idl
这将在 src 目录中创建 HelloWorld.idl 文件。 在文本编辑器中打开文件,然后复制并粘贴以下代码片段。
struct HelloWorld
{unsigned long index;string message;
};
生成在 C++11 中实现此数据类型的源代码
#<path/to/Fast DDS-Gen>/scripts/fastddsgen HelloWorld.idl
#/home/glr/Fast-DDS/src/fastddsgen/scripts/fastddsgen -example CMake HelloWorld.idl
/home/glr/Fast-DDS/src/fastddsgen/scripts/fastddsgen HelloWorld.idl
这必须生成以下文件:
- HelloWorld.hpp:HelloWorld 类型定义。
- HelloWorldPubSubTypes.cxx:Fast DDS 用于支持 HelloWorld 类型的接口。
- HelloWorldPubSubTypes.h:HelloWorldPubSubTypes.cxx 的头文件。
- HelloWorldCdrAux.ipp:HelloWorld 类型的序列化和反序列化代码。
- HelloWorldCdrAux.hpp:HelloWorldCdrAux.ipp 的头文件。
- HelloWorldTypeObjectSupport.cxx:TypeObject 表示注册代码。
- HelloWorldTypeObjectSupport.hpp:HelloWorldTypeObjectSupport.cxx 的头文件。
2.2 编写Fast-DDS Publisher
从工作区的 src 目录中,运行以下命令以下载 HelloWorldPublisher.cpp 文件。
wget -O HelloWorldPublisher.cpp https://raw.githubusercontent.com/eProsima/Fast-RTPS-docs/master/code/Examples/C++/DDSHelloWorld/src/HelloWorldPublisher.cpp
- 这是发布者应用程序的 C++ 源代码:
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License./*** @file HelloWorldPublisher.cpp**/#include "HelloWorldPubSubTypes.hpp"#include <chrono>
#include <thread>#include <fastdds/dds/domain/DomainParticipant.hpp>
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/publisher/DataWriter.hpp>
#include <fastdds/dds/publisher/DataWriterListener.hpp>
#include <fastdds/dds/publisher/Publisher.hpp>
#include <fastdds/dds/topic/TypeSupport.hpp>using namespace eprosima::fastdds::dds;class HelloWorldPublisher
{
private:HelloWorld hello_;DomainParticipant* participant_;Publisher* publisher_;Topic* topic_;DataWriter* writer_;TypeSupport type_;class PubListener : public DataWriterListener{public:PubListener(): matched_(0){}~PubListener() override{}void on_publication_matched(DataWriter*,const PublicationMatchedStatus& info) override{if (info.current_count_change == 1){matched_ = info.total_count;std::cout << "Publisher matched." << std::endl;}else if (info.current_count_change == -1){matched_ = info.total_count;std::cout << "Publisher unmatched." << std::endl;}else{std::cout << info.current_count_change<< " is not a valid value for PublicationMatchedStatus current count change." << std::endl;}}std::atomic_int matched_;} listener_;public:HelloWorldPublisher(): participant_(nullptr), publisher_(nullptr), topic_(nullptr), writer_(nullptr), type_(new HelloWorldPubSubType()){}virtual ~HelloWorldPublisher(){if (writer_ != nullptr){publisher_->delete_datawriter(writer_);}if (publisher_ != nullptr){participant_->delete_publisher(publisher_);}if (topic_ != nullptr){participant_->delete_topic(topic_);}DomainParticipantFactory::get_instance()->delete_participant(participant_);}//!Initialize the publisherbool init(){hello_.index(0);hello_.message("HelloWorld");DomainParticipantQos participantQos;participantQos.name("Participant_publisher");participant_ = DomainParticipantFactory::get_instance()->create_participant(0, participantQos);if (participant_ == nullptr){return false;}// Register the Typetype_.register_type(participant_);// Create the publications Topictopic_ = participant_->create_topic("HelloWorldTopic", "HelloWorld", TOPIC_QOS_DEFAULT);if (topic_ == nullptr){return false;}// Create the Publisherpublisher_ = participant_->create_publisher(PUBLISHER_QOS_DEFAULT, nullptr);if (publisher_ == nullptr){return false;}// Create the DataWriterwriter_ = publisher_->create_datawriter(topic_, DATAWRITER_QOS_DEFAULT, &listener_);if (writer_ == nullptr){return false;}return true;}//!Send a publicationbool publish(){if (listener_.matched_ > 0){hello_.index(hello_.index() + 1);writer_->write(&hello_);return true;}return false;}//!Run the Publishervoid run(uint32_t samples){uint32_t samples_sent = 0;while (samples_sent < samples){if (publish()){samples_sent++;std::cout << "Message: " << hello_.message() << " with index: " << hello_.index()<< " SENT" << std::endl;}std::this_thread::sleep_for(std::chrono::milliseconds(1000));}}
};int main(int argc,char** argv)
{std::cout << "Starting publisher." << std::endl;uint32_t samples = 10;HelloWorldPublisher* mypub = new HelloWorldPublisher();if(mypub->init()){mypub->run(samples);}delete mypub;return 0;
}
下一个块包括允许使用 Fast DDS API 的 C++ 头文件:
- DomainParticipantFactory.允许创建和销毁 DomainParticipant 对象。
- DomainParticipant.充当所有其他 Entity 对象的容器,以及 Publisher、Subscriber、 和Topic 对象。
- TypeSupport.为参与者提供序列化、反序列化和获取 特定数据类型。
- Publisher. 它是负责创建
- DataWriter 的对象。
- DataWriter.允许应用程序设置要在给定 Topic 下发布的数据的值。
- DataWriterListener.允许重新定义 DataWriterListener 的函数。
2.3 编写快速 DDS 订阅者
- 从工作区的 src 目录中,执行以下命令以下载 HelloWorldSubscriber.cpp 文件。
wget -O HelloWorldSubscriber.cpp https://raw.githubusercontent.com/eProsima/Fast-RTPS-docs/master/code/Examples/C++/DDSHelloWorld/src/HelloWorldSubscriber.cpp
这是订阅者应用程序的 C++ 源代码。 应用程序将运行订阅服务器,直到收到主题 HelloWorldTopic 下的 10 个样本。
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License./*** @file HelloWorldSubscriber.cpp**/#include "HelloWorldPubSubTypes.hpp"#include <chrono>
#include <thread>#include <fastdds/dds/domain/DomainParticipant.hpp>
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/subscriber/DataReader.hpp>
#include <fastdds/dds/subscriber/DataReaderListener.hpp>
#include <fastdds/dds/subscriber/qos/DataReaderQos.hpp>
#include <fastdds/dds/subscriber/SampleInfo.hpp>
#include <fastdds/dds/subscriber/Subscriber.hpp>
#include <fastdds/dds/topic/TypeSupport.hpp>using namespace eprosima::fastdds::dds;class HelloWorldSubscriber
{
private:DomainParticipant* participant_;Subscriber* subscriber_;DataReader* reader_;Topic* topic_;TypeSupport type_;class SubListener : public DataReaderListener{public:SubListener(): samples_(0){}~SubListener() override{}void on_subscription_matched(DataReader*,const SubscriptionMatchedStatus& info) override{if (info.current_count_change == 1){std::cout << "Subscriber matched." << std::endl;}else if (info.current_count_change == -1){std::cout << "Subscriber unmatched." << std::endl;}else{std::cout << info.current_count_change<< " is not a valid value for SubscriptionMatchedStatus current count change" << std::endl;}}void on_data_available(DataReader* reader) override{SampleInfo info;if (reader->take_next_sample(&hello_, &info) == eprosima::fastdds::dds::RETCODE_OK){if (info.valid_data){samples_++;std::cout << "Message: " << hello_.message() << " with index: " << hello_.index()<< " RECEIVED." << std::endl;}}}HelloWorld hello_;std::atomic_int samples_;}listener_;public:HelloWorldSubscriber(): participant_(nullptr), subscriber_(nullptr), topic_(nullptr), reader_(nullptr), type_(new HelloWorldPubSubType()){}virtual ~HelloWorldSubscriber(){if (reader_ != nullptr){subscriber_->delete_datareader(reader_);}if (topic_ != nullptr){participant_->delete_topic(topic_);}if (subscriber_ != nullptr){participant_->delete_subscriber(subscriber_);}DomainParticipantFactory::get_instance()->delete_participant(participant_);}//!Initialize the subscriberbool init(){DomainParticipantQos participantQos;participantQos.name("Participant_subscriber");participant_ = DomainParticipantFactory::get_instance()->create_participant(0, participantQos);if (participant_ == nullptr){return false;}// Register the Typetype_.register_type(participant_);// Create the subscriptions Topictopic_ = participant_->create_topic("HelloWorldTopic", "HelloWorld", TOPIC_QOS_DEFAULT);if (topic_ == nullptr){return false;}// Create the Subscribersubscriber_ = participant_->create_subscriber(SUBSCRIBER_QOS_DEFAULT, nullptr);if (subscriber_ == nullptr){return false;}// Create the DataReaderreader_ = subscriber_->create_datareader(topic_, DATAREADER_QOS_DEFAULT, &listener_);if (reader_ == nullptr){return false;}return true;}//!Run the Subscribervoid run(uint32_t samples){while (listener_.samples_ < samples){std::this_thread::sleep_for(std::chrono::milliseconds(100));}}};int main(int argc,char** argv)
{std::cout << "Starting subscriber." << std::endl;uint32_t samples = 10;HelloWorldSubscriber* mysub = new HelloWorldSubscriber();if (mysub->init()){mysub->run(samples);}delete mysub;return 0;
}
2.4 CMakeLists.txt修改运行
- CMakeLists.txt修改
add_executable(DDSHelloWorldPublisher src/HelloWorldPublisher.cpp ${DDS_HELLOWORLD_SOURCES_CXX})
target_link_libraries(DDSHelloWorldPublisher fastdds fastcdr)add_executable(DDSHelloWorldSubscriber src/HelloWorldSubscriber.cpp ${DDS_HELLOWORLD_SOURCES_CXX})
target_link_libraries(DDSHelloWorldSubscriber fastdds fastcdr)
- cd到build目录,直接编译
cmake ..
cmake --build .
./DDSHelloWorldSubscriber
三、运行结果
Publisher
Subscriber
遇到的问题记录
可以查看当前ros2 是否使用fastdds作为中间件,可以使用
ros2 doctor --report