news 2026/6/13 19:49:47

ros2 订阅与发布-cpp

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
ros2 订阅与发布-cpp

基础

ros2 run turtlesim turtlesim_node //运行乌龟节点 ros2 node list //查询所有运行的节点 ros2 node info /turtlesim //查询乌龟节点的信息 //可发现 乌龟节点订阅了 /turtle1/cmd_vel //话题 消息接口是 geometry_msgs/msg/Twist //同时 乌龟节点 发布了一个话题 来输出自己的位置 //话题 /turtle1/pose 消息接口 turtlesim/msg/pose

流程

创建包 并添加 geometry_msg turtlesim 依赖

ros2 pkg create demo_cpp_topic --build-type ament_cmake --dependencies rclcpp geometry_msgs turtlesim --license Apache-2.0

在包下的src下编写turtle_circle.cpp

#include "rclcpp/rclcpp.hpp" #include "geometry_msgs/msg/twist.hpp" #include <chrono> using namespace std::chrono_literals; class TurtleCircle : public rclcpp::Node{ private: rclcpp::TimerBase::SharedPtr timer_; rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr publisher_; public: explicit TurtleCircle(const std::string& node_name):Node(node_name){ publisher_=this->create_publisher<geometry_msgs::msg::Twist>( "/turtle1/cmd_vel",10); //相比py的简单粗暴 cpp需要bind将函数变成可直接调用的回调函数 timer_=this->create_wall_timer( 1000ms,std::bind(&TurtleCircle::timer_callback,this)); } private: void timer_callback(){ auto msg = geometry_msgs::msg::Twist(); msg.linear.x=1.0; msg.angular.z=0.5; publisher_->publish(msg); } }; int main(int argc ,char**argv){ rclcpp::init(argc,argv); auto node=std::make_shared<TurtleCircle>("thrtle_circle"); rclcpp::spin(node); rclcpp::shutdown(); return 0; }

编写完成之后在CMakeLists.txt中添加节点 ,依赖之后构建项目就能运行了(当然要先source)

再开启乌龟节点 就可以看到乌龟转圈了

同理 编写 turtle_control.cpp 可以让乌龟向目标位置前进

#include "geometry_msgs/msg/twist.hpp" #include "rclcpp/rclcpp.hpp" #include "turtlesim/msg/pose.hpp" class TurtleController:public rclcpp::Node{ private: rclcpp::Subscription<turtlesim::msg::Pose>::SharedPtr pose_subscription_; rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr velocity_publisher_; double target_x_{1.0}; double target_y_{1.0}; double k_{1.0}; double max_speed_{3.0}; private: void on_pose_received_(const turtlesim::msg::Pose::SharedPtr pose){ auto message=geometry_msgs::msg::Twist(); double current_x =pose->x; double current_y=pose->y; RCLCPP_INFO(this->get_logger(),"now location:(x=%f,y=%f)", current_x,current_y); double distance = std:: sqrt((target_x_-current_x)*(target_x_-current_x)+ (target_y_-current_y)*(target_y_-current_y)); double angle = std::atan2(target_y_-current_y,target_x_-current_x)-pose->theta; if(distance>0.1){ if(fabs(angle)>0.2){ message.angular.z=fabs(angle); }else{ message.linear.x=k_ * distance; } } if(message.linear.x>max_speed_){ message.linear.x=max_speed_; } velocity_publisher_->publish(message); } public: TurtleController():Node("turtle_controller"){ velocity_publisher_ = this->create_publisher<geometry_msgs::msg::Twist>( "/turtle1/cmd_vel",10); pose_subscription_=this->create_subscription<turtlesim::msg::Pose>( "turtle1/pose",10,std::bind(&TurtleController::on_pose_received_,this, std::placeholders::_1)); } }; int main(int argc ,char** argv){ rclcpp::init(argc,argv); auto node=std::make_shared<TurtleController>(); rclcpp::spin(node); rclcpp::shutdown(); return 0; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 13:34:13

GitHub Actions持续集成TensorFlow项目时使用清华源提速

GitHub Actions持续集成TensorFlow项目时使用清华源提速 在构建一个基于 TensorFlow 的开源项目 CI 流水线时&#xff0c;你是否曾经历过这样的场景&#xff1a;每次 pip install tensorflow 都像在“看运气”&#xff1f;国际网络波动、下载中断、超时失败……尤其在中国境内…

作者头像 李华
网站建设 2026/6/10 15:23:44

Dify结果过滤难?掌握这3种重排序策略,精准锁定关键信息

第一章&#xff1a;检索重排序的 Dify 结果过滤在构建基于大语言模型的应用时&#xff0c;检索增强生成&#xff08;RAG&#xff09;系统常面临检索结果相关性不足的问题。Dify 作为低代码 AI 应用开发平台&#xff0c;提供了灵活的结果过滤与重排序机制&#xff0c;可有效提升…

作者头像 李华
网站建设 2026/6/10 15:24:43

春节前科技盛宴!小米全家桶扎堆来袭,17 Ultra + 双 Turbo 机皇齐亮相

对数码爱好者来说&#xff0c;年底最期待的莫过于厂商的 “压轴新品秀”。小米这次直接放大招&#xff0c;12 月 14 日曝光的春节前新品清单堪称 “全家桶豪华套餐”—— 从第五代骁龙 8 至尊版加持的小米 17 Ultra&#xff0c;到全球首发天玑 8500 的 REDMI Turbo 5 系列&…

作者头像 李华
网站建设 2026/6/12 13:14:13

构建可持续的自动化测试维护体系

随着敏捷开发与持续集成的普及&#xff0c;自动化测试已成为现代软件工程中不可或缺的一环。然而&#xff0c;许多团队在初期投入自动化后&#xff0c;逐渐面临脚本失效、环境依赖复杂、维护成本高昂等挑战。究其根源&#xff0c;往往是由于缺乏前瞻性的维护策略所致。一、脚本…

作者头像 李华
网站建设 2026/6/13 15:55:04

孩子学编程到底有没有用?这篇文章告诉你!

最近好多家长都在问&#xff1a;现在满大街都在说少儿编程&#xff0c;是不是真的值得学习&#xff1f;我家孩子天天这就知道玩游戏&#xff0c;学这个真的有用吗&#xff1f;说实话&#xff0c;我们能理解大家的焦虑。咱们小时候学的是奥数、英语&#xff0c;现在的孩子起跑线…

作者头像 李华