当前位置: 首页 > news >正文

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
http://www.cnnetsun.cn/news/1471998.html

相关文章:

  • 如何高效提取Instagram公开数据?Toutatis工具全方位使用指南
  • LFM2.5-1.2B-Thinking-GGUF入门指南:GGUF格式+llama.cpp运行时核心概念
  • glibc内存管理:malloc与free原理详解
  • 如何通过内网穿透技术,实现跨地域USB设备的安全共享与远程管理
  • TurboDiffusion保姆级教程:零基础玩转Wan2.1/Wan2.2文生视频
  • Python代码秒变WebAssembly?3大主流编译工具实测对比(启动耗时↓87%、内存占用↓62%)
  • 从CAN到车载以太网:AUTOSAR网络管理的“跨界”挑战与配置实战
  • 抖音内容智能下载工具:轻松保存无水印高清视频的终极解决方案
  • 短视频信息流广告ROI提升难?IP精准定位帮你锁定高价值受众
  • PaddlePaddle-v3.3实战指南:Jupyter启动故障排除手册
  • 蓝桥杯基础--时间复杂度
  • 如何用yfinance构建金融数据管道:从数据获取到策略实现的全流程指南
  • UE5性能调优实战:手把手教你用Unreal Insights揪出卡顿元凶(附完整配置流程)
  • 拆解汉朔电子价签:如何用2.13寸墨水屏DIY智能时钟(STM32开发指南)
  • 西门子S7-1200博途V16自动洗车控制系统:程序组态仿真与功能详解
  • InternLM2-Chat-1.8B与Dify平台集成:快速构建AI智能体应用
  • 别再直接拔电源了!聊聊Ubuntu里shutdown、halt、reboot这几个命令到底有啥区别
  • # 发散创新:基于RxJS的事件流驱动型前端架构设计与实践在现代前端开发中,**事件流(
  • 5步突破金融数据壁垒:面向量化分析师的yfinance实战指南
  • 高通平台Camera CTS测试避坑指南:从enableMCTFwithReferenceFrame配置错误看metadata设置陷阱
  • 软工毕业设计最新方向怎么做
  • Wan2.2-I2V-A14B镜像深度解析:PyTorch2.4+CUDA12.4编译适配细节
  • RDMA操作全解析:为什么send/recv适合控制消息而read/write适合大数据传输?
  • 通用GUI编程技术——Win32 原生编程实战(番外)——TabControl 的深色背景与 EnableThemeDialogTexture
  • Z-Image-GGUF模型微调入门:使用自定义数据集提升特定风格生成能力
  • 3步释放C盘空间:给Windows用户的智能清理工具
  • PyTorch 2.8镜像惊艳效果展示:RTX 4090D上运行Sora类模型的高清视频生成作品集
  • PCL平面分割实战:从算法原理(RANSAC)到在机器人SLAM与三维重建中的应用
  • Python+OpenCV实战:5种图像处理矩阵运算让你的照片秒变大片
  • LTX-Video全场景部署指南:从本地开发到企业级应用落地