news 2026/4/3 4:28:51

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/2 3:15:06

iPhone 20要变“鹅卵石”?四曲面无边框传闻来袭,LG砸钱改造生产线

对苹果数码爱好者来说&#xff0c;每一代iPhone的设计革新都是最值得期待的科技盛宴。近日&#xff0c;Wccftech的一则报道让数码圈炸开了锅&#xff1a;苹果未来的iPhone 20或将采用“四曲面”全面屏设计&#xff0c;追求近乎无边框的视觉效果&#xff0c;而为了配合这一激进设…

作者头像 李华
网站建设 2026/4/1 17:31:24

LobeChat能否制作问卷调查?社研工作者福音

LobeChat 能否制作问卷调查&#xff1f;社研工作者的新选择 在社会研究领域&#xff0c;设计一份有效的问卷从来都不是简单的事。传统的电子表单工具虽然普及&#xff0c;但面对复杂的研究逻辑、动态的提问路径和多样化的受访者表达时&#xff0c;往往显得僵硬而低效。更不用说…

作者头像 李华
网站建设 2026/4/1 19:39:12

Resilience重试机制

&#x1f3af; 从零了解 Resilience 重试机制&#xff1a;用 Go 构建健壮的容错系统 在构建稳定可靠的系统时&#xff0c;我们经常会遇到各种临时失败&#xff0c;比如&#xff1a; 网络短暂不可达第三方 API 超时数据库瞬时错误 这些失败不一定是致命的&#xff0c;合理的重…

作者头像 李华
网站建设 2026/3/26 20:14:25

HyperbolicRAG:双曲空间如何解决RAG多跳检索难题?大模型开发者必学技术

HyperbolicRAG通过双曲空间表示解决传统RAG在多跳问答中的局限性。针对语义枢纽和层次缺失问题&#xff0c;该方案提出层次感知表示学习、无监督双向对齐和双路检索互排融合方法。实验表明&#xff0c;该方法在6个数据集上检索性能全部Top-1&#xff0c;端到端QA结果比基线高0.…

作者头像 李华
网站建设 2026/4/1 16:12:33

30亿参数小模型如何媲美千亿级大模型?Nanbeige4-3B的技术突破与实践指南

本文介绍了Boss直聘南北阁大模型实验室发布的Nanbeige4-3B小语言模型&#xff0c;仅30亿参数却通过创新的数据筛选体系和训练方法&#xff0c;在数学推理、科学推理、工具调用等多项评测中超越同体量甚至更大规模的模型&#xff0c;展现了小模型通过算法优化实现"以小搏大…

作者头像 李华