首先打开一个新终端,并且设置环境变量,以便ros2命令能够正常工作。然后进入到dev_ws/src
文件夹,运行创建功能包的指令:
ros2 pkg create --build-type ament_cmake cpp_pubsub
您的终端将返回一条消息,验证功能包cpp_pubsub
及其所有必要文件和文件夹的创建。
going to create a new package
package name: cpp_pubsub
destination directory: /home/libo/dev_ws/src
package format: 3
version: 0.0.0
description: TODO: Package description
maintainer: ['libo ']
licenses: ['TODO: License declaration']
build type: ament_cmake
dependencies: []
creating folder ./cpp_pubsub
creating ./cpp_pubsub/package.xml
creating source and include folder
creating folder ./cpp_pubsub/src
creating folder ./cpp_pubsub/include/cpp_pubsub
creating ./cpp_pubsub/CMakeLists.txt
在cpp_pubsub
功能包的src文件夹下,创建一个发布者节点的代码文件publisher_member_function.cpp
,然后拷贝以下代码放进去:
#include
#include
#include
#include
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using namespace std::chrono_literals;
/* This example creates a subclass of Node and uses std::bind() to register a
* member function as a callback from the timer. */
class MinimalPublisher : public rclcpp::Node
{
public:
MinimalPublisher()
: Node("minimal_publisher"), count_(0)
{
publisher_ = this->create_publisher("topic", 10);
timer_ = this->create_wall_timer(500ms, std::bind(&MinimalPublisher::timer_callback, this));
}
private:
void timer_callback()
{
auto message = std_msgs::msg::String();
message.data = "Hello, world! " + std::to_string(count_++);
RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
publisher_->publish(message);
}
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher::SharedPtr publisher_;
size_t count_;
};
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared());
rclcpp::shutdown();
return 0;
}
接下来对上述代码进行简单的注释。代码的顶部包括您将要使用的标准C++头文件。rclcpp/rclcpp.hpp
是ROS2中常用C++接口的头文件,使用C++编写的ROS2节点程序一定需要包含该头文件。std_msgs/msg/string.hpp
是ROS2中字符串消息的头文件,后边我们会周期发布一个HelloWorld的字符串消息,所以需要包含该头文件。
#include
#include
#include
#include
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using namespace std::chrono_literals;
下一行通过继承rclcpp::Node
节点基类创建节点类MinimalPublisher
。代码中的每个this都引用节点。
class MinimalPublisher : public rclcpp::Node
接下来是节点类MinimalPublisherd
的构造函数,将count_
变量初始化为0,节点名初始化为“minimal_publisher”
。构造函数内先是创建了一个发布者,发布的话题名是topic
,话题消息是String
,保存消息的队列长度是10
,然后创建了一个定时器timer_
,做了一个500ms
的定时,每次触发定时器后,都会运行回调函数timer_callback
。
public:
MinimalPublisher()
: Node("minimal_publisher"), count_(0)
{
publisher_ = this->create_publisher("topic", 10);
timer_ = this->create_wall_timer(500ms, std::bind(&MinimalPublisher::timer_callback, this));
}
timer_callback
是这里的关键,每次触发都会发布一次话题消息。message
中保存的字符串是Hello world加一个计数值,然后通过RCLCPP_INFO
宏函数打印一次日志信息,再通过发布者的publish
方法将消息发布出去。
Private:
void timer_callback()
{
auto message = std_msgs::msg::String();
message.data = "Hello, world! " + std::to_string(count_++);
RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
publisher_->publish(message);
}
最后是计时器、发布者和计数器字段的声明。
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher::SharedPtr publisher_;
size_t count_;
定义完节点类后还需要编写main函数,先初始化ROS2节点,然后使用rclcpp::spin
创建MinimalPublisher
,并且进入自旋锁,当退出锁时,就会关闭节点结束了。完成以上发布者的代码后,功能包里还有一些内容需要设置。
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared());
rclcpp::shutdown();
return 0;
}
打开功能包的package.xml文件,根据前面的教程,先把
,
和
这些基础信息填写好:
Examples of minimal publisher/subscriber using rclcpp
Your Name
Apache License 2.0
然后还需要添加依赖项,放到ament_cmake
下边:
rclcpp
std_msgs
接下来打开CMakeLists.txt
文件,在find_package
语句下,新加入两行:
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
然后再设置具体的编译规则,:
add_executable(talker src/publisher_member_function.cpp)
ament_target_dependencies(talker rclcpp std_msgs)
最后还要设置安装的规则,这样ros2 run命令才找得到可执行文件:
install(TARGETS
talker
DESTINATION lib/${PROJECT_NAME})
完整的CMakeLists.txt
文件应该就是这样的:
cmake_minimum_required(VERSION 3.5)
project(cpp_pubsub)
Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
add_executable(talker src/publisher_member_function.cpp)
ament_target_dependencies(talker rclcpp std_msgs)
install(TARGETS
talker
DESTINATION lib/${PROJECT_NAME})
ament_package()
回到dev_ws/src/cpp_pubsub/src
文件夹下,创建订阅者节点的代码subscriber_member_function.cpp
:
#include
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using std::placeholders::_1;
class MinimalSubscriber : public rclcpp::Node
{
public:
MinimalSubscriber()
: Node("minimal_subscriber")
{
subscription_ = this->create_subscription(
"topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
}
private:
void topic_callback(const std_msgs::msg::String::SharedPtr msg) const
{
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
}
rclcpp::Subscription::SharedPtr subscription_;
};
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared());
rclcpp::shutdown();
return 0;
}
订阅者的代码整体流程和发布者类似,现在的节点名叫minimal_subscriber
,构造函数中创建了订阅者,订阅String
消息,订阅的话题名叫做“topic”
,保存消息的队列长度是10
,当订阅到数据时,会进入回调函数topic_callback
。根据前面的教程,发布者和订阅者使用的话题名称和消息类型必须匹配才能进行通信。
public:
MinimalSubscriber()
: Node("minimal_subscriber")
{
subscription_ = this->create_subscription(
"topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
}
回调函数中会收到String消息,然后并没有做太多处理,只是通过RCLCPP_INFO
打印出来。
private:
void topic_callback(const std_msgs::msg::String::SharedPtr msg) const
{
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
}
rclcpp::Subscription::SharedPtr subscription_;
main
函数中的内容和发布者几乎是一致的,就不再赘述。它们的唯一区别就在于:对于发布者节点而言,rclcpp::spin
意味着启动计时器,但对于订阅者而言,rclcpp::spin
仅仅意味着随时准备接收消息。
由于该节点的依赖项和发布者一样,我们就不需要修改package.xml
文件了,不过编译规则CMakeList.txt
还是得加一些内容:
add_executable(listener src/subscriber_member_function.cpp)
ament_target_dependencies(listener rclcpp std_msgs)
install(TARGETS
talker
listener
DESTINATION lib/${PROJECT_NAME})
编译前先确认下功能包的依赖项有没有都安装好,在dev_ws
路径下运行如下命令:
rosdep install -i --from-path src --rosdistro eloquent -y
安装完毕后还是在该路径下编译cpp_pubsub功能包:
colcon build --packages-select cpp_pubsub
编译完成后,打开一个新的终端,设置工作空间的环境变量后,运行发布者:
. install/setup.bash
ros2 run cpp_pubsub talker
运行成功后可以看到终端每隔0.5s打印一次日志信息:
[INFO] [minimal_publisher]: Publishing: "Hello World: 0"
[INFO] [minimal_publisher]: Publishing: "Hello World: 1"
[INFO] [minimal_publisher]: Publishing: "Hello World: 2"
[INFO] [minimal_publisher]: Publishing: "Hello World: 3"
[INFO] [minimal_publisher]: Publishing: "Hello World: 4"
再打开一个新的终端,设置工作空间的环境变量后,运行订阅者:
. install/setup.bash
ros2 run cpp_pubsub listener
订阅者启动后,终端中会显示当前订阅者收到的消息内容:
[INFO] [minimal_subscriber]: I heard: "Hello World: 10"
[INFO] [minimal_subscriber]: I heard: "Hello World: 11"
[INFO] [minimal_subscriber]: I heard: "Hello World: 12"
[INFO] [minimal_subscriber]: I heard: "Hello World: 13"
[INFO] [minimal_subscriber]: I heard: "Hello World: 14"
在终端中按Ctrl+C
即可退出。到此为止,本篇我们一起创建了两个节点,并且通过话题实现了两个节点之间的通信。