ROS2学习记录010-话题之RCLCPP实现
学习鱼香ROS大佬,记录
(一)本节创建一个控制节点和一个被控制节点
控制节点创建一个话题发布者,发布控制命令(command)话题。被控节点创建一个订阅者,订阅控制命令。
(1)依次输入下面的命令,创建chapt3_ws工作空间、example_topic_rclcpp功能包和topic_publisher_01.cpp。
cd d2lros2/ mkdir -p chapt3/chapt3_ws/src cd chapt3/chapt3_ws/src ros2 pkg create example_topic_rclcpp --build-type ament_cmake --dependencies rclcpp touch example_topic_rclcpp/src/topic_publisher_01.cpp创建后的目录结构如下
. └── src └── example_topic_rclcpp ├── CMakeLists.txt ├── include │ └── example_topic_rclcpp ├── package.xml └── src └── topic_publisher_01.cpp(2)创建发布者,topic_publisher_01.cpp代码如下
#include "rclcpp/rclcpp.hpp" #include "std_msgs/msg/string.hpp" class TopicPublisher01 : public rclcpp::Node { public: // 构造函数,有一个参数为节点名称 TopicPublisher01(std::string name) : Node(name) { RCLCPP_INFO(this->get_logger(), "大家好,我是%s.", name.c_str()); // 创建发布者 command_publisher_ = this->create_publisher<std_msgs::msg::String>("command", 10); // 创建定时器,500ms为周期,定时发布 timer_ = this->create_wall_timer(std::chrono::milliseconds(500), std::bind(&TopicPublisher01::timer_callback, this)); } private: void timer_callback() { // 创建消息 std_msgs::msg::String message; message.data = "forward"; // 日志打印 RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str()); // 发布消息 command_publisher_->publish(message); } // 声名定时器指针 rclcpp::TimerBase::SharedPtr timer_; // 声明话题发布者指针 rclcpp::Publisher<std_msgs::msg::String>::SharedPtr command_publisher_; }; int main(int argc, char **argv) { rclcpp::init(argc, argv); /*创建对应节点的共享指针对象*/ auto node = std::make_shared<TopicPublisher01>("topic_publisher_01"); /* 运行节点,并检测退出信号*/ rclcpp::spin(node); rclcpp::shutdown(); return 0; }(3)创建订阅者
第一步:
cd chapt3_ws/src/example_topic_rclcpp touch src/topic_subscribe_01.cpp第二步:topic_subscribe_01.cpp代码内容如下
#include "rclcpp/rclcpp.hpp" #include "std_msgs/msg/string.hpp" class TopicSubscribe01 : public rclcpp::Node { public: TopicSubscribe01(std::string name) : Node(name) { RCLCPP_INFO(this->get_logger(), "大家好,我是%s.", name.c_str()); // 创建一个订阅者订阅话题 command_subscribe_ = this->create_subscription<std_msgs::msg::String>("command", 10, std::bind(&TopicSubscribe01::command_callback, this, std::placeholders::_1)); } private: // 声明一个订阅者 rclcpp::Subscription<std_msgs::msg::String>::SharedPtr command_subscribe_; // 收到话题数据的回调函数 void command_callback(const std_msgs::msg::String::SharedPtr msg) { double speed = 0.0f; if(msg->data == "forward") { speed = 0.2f; } RCLCPP_INFO(this->get_logger(), "收到[%s]指令,发送速度 %f", msg->data.c_str(),speed); } }; int main(int argc, char **argv) { rclcpp::init(argc, argv); /*创建对应节点的共享指针对象*/ auto node = std::make_shared<TopicSubscribe01>("topic_subscribe_01"); /* 运行节点,并检测退出信号*/ rclcpp::spin(node); rclcpp::shutdown(); return 0; }(3)packages.xml内容如下
<buildtool_depend>ament_cmake</buildtool_depend> <depend>rclcpp</depend> <depend>std_msgs</depend> <test_depend>ament_lint_auto</test_depend> <test_depend>ament_lint_common</test_depend>(4)CMakeLists.txt内容如下:
cmake_minimum_required(VERSION 3.8) project(example_topic_rclcpp) if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wall -Wextra -Wpedantic) endif() # find dependencies find_package(ament_cmake REQUIRED) find_package(rclcpp REQUIRED) find_package(std_msgs REQUIRED) if(BUILD_TESTING) find_package(ament_lint_auto REQUIRED) # the following line skips the linter which checks for copyrights # comment the line when a copyright and license is added to all source files set(ament_cmake_copyright_FOUND TRUE) # the following line skips cpplint (only works in a git repo) # comment the line when this package is in a git repo and when # a copyright and license is added to all source files set(ament_cmake_cpplint_FOUND TRUE) ament_lint_auto_find_test_dependencies() endif() ament_package() add_executable(topic_publisher_01 src/topic_publisher_01.cpp) ament_target_dependencies(topic_publisher_01 rclcpp std_msgs) install(TARGETS topic_publisher_01 DESTINATION lib/${PROJECT_NAME} ) add_executable(topic_subscribe_01 src/topic_subscribe_01.cpp) ament_target_dependencies(topic_subscribe_01 rclcpp std_msgs) install(TARGETS topic_subscribe_01 DESTINATION lib/${PROJECT_NAME} )(5)编译
cd chapt3/chapt3_ws/ colcon build --packages-select example_topic_rclcpp(6)测试
运行发布节点
cd chapt3/chapt3_ws/ source install/setup.bash ros2 run example_topic_rclcpp topic_publisher_01运行订阅节点
cd chapt3/chapt3_ws/ source install/setup.bash ros2 run example_topic_rclcpp topic_subscribe_01(7)查看计算图rtq,并刷新
rqt_graph