news 2026/4/15 12:20:42

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/4/15 3:32:02

Web前端教程 4

CSS盒子模型弹性盒子模型浮动清除浮动定位CSS新属性媒体查询雪碧图字体图标

作者头像 李华
网站建设 2026/4/15 3:33:47

灵活用工费咋算?亲测案例复盘分享

灵活用工费计算逻辑与技术革新&#xff1a;基于天语灵活用工平台的深度解析行业痛点分析当前灵活用工平台领域面临两大核心挑战&#xff1a;算薪效率与合规风险。传统系统在处理复杂用工场景时&#xff0c;常因多维度变量&#xff08;如工时波动、岗位差异、政策变动&#xff0…

作者头像 李华
网站建设 2026/4/15 3:31:41

2025年12月成都四川工作服厂家推荐:专业品牌排行榜单深度对比分析

一、引言 工作服作为企业形象塑造与员工劳动防护的重要载体&#xff0c;其采购决策直接关系到企业运营成本控制、品牌视觉统一性及员工安全保障。对于成都及周边地区的企事业单位采购负责人、行政管理者以及创业者而言&#xff0c;如何在众多供应商中筛选出具备稳定生产能力、…

作者头像 李华
网站建设 2026/4/6 0:37:07

7个常见错误避免,确保YashanDB实施的成功率

在当前数据驱动的业务环境中&#xff0c;数据库系统作为核心信息基础设施&#xff0c;其性能瓶颈、数据一致性保障及高可用性实现等挑战日益突显。YashanDB作为一款支持多种部署形态的先进数据库系统&#xff0c;集成了行列混合存储、分布式和共享集群架构等多项技术优势&#…

作者头像 李华
网站建设 2026/4/11 7:45:05

从Git仓库获取TensorRT示例代码并运行BERT推理

从Git仓库获取TensorRT示例代码并运行BERT推理 在当前AI应用快速落地的背景下&#xff0c;将大型语言模型高效部署到生产环境已成为工程团队的核心挑战之一。以BERT为代表的Transformer架构虽然在自然语言理解任务中表现出色&#xff0c;但其庞大的参数量和密集的矩阵运算使得…

作者头像 李华
网站建设 2026/4/15 12:01:30

从 paperzz 到 8 款工具:降重 / 降 AIGC 的 “差异化战场”—— 谁能守住学术表达的 “精准与合规”?

学术写作的 “终局考验”&#xff0c;往往藏在初稿完成后的 “细节优化” 里&#xff1a;重复率超标、AIGC 痕迹被检测、表述既冗余又生硬…… 而一批定位 “学术表达优化” 的工具正在涌现 —— 从 paperzz 的 “轻量型精准优化” 到其他工具的 “文献联动降重”“学科化 AIGC…

作者头像 李华