news 2026/4/24 18:10:28

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/17 23:12:16

特长生 VS 全科生:AI与AGI的本质区别,一张文说清

近期看到新闻——酷特智能跑通了首个行业级的AGI&#xff08;通用AI&#xff09;&#xff0c;想来聊聊AI与AGI。简单来说&#xff0c;AI&#xff08;人工智能&#xff09;是我们今天正在广泛使用的技术&#xff0c;而AGI&#xff08;通用人工智能&#xff09;是我们努力迈向的未…

作者头像 李华
网站建设 2026/4/24 15:07:39

Dify对接Spring AI总失败?一文看懂版本依赖的4大雷区

第一章&#xff1a;Dify 与 Spring AI 的版本兼容在构建基于 Java 的 AI 应用时&#xff0c;Spring AI 框架为开发者提供了简洁的抽象层&#xff0c;而 Dify 作为低代码 AI 编排平台&#xff0c;支持快速集成外部服务。确保 Dify 与 Spring AI 的版本兼容性是实现稳定通信的关键…

作者头像 李华
网站建设 2026/4/23 20:39:01

对比多线程与batch(在极简单cnn上操作)

batch&#xff0c;从学习dos时&#xff0c;就认识了这个单词&#xff0c;它叫做批处理&#xff01;现在我发现他与并行或多线程是有差别的&#xff01;我们前头所有程序凡是用到batch&#xff0c;均是如下操作&#xff0c;比如batch3&#xff1a;输入一张图片&#xff0c;forwa…

作者头像 李华
网站建设 2026/4/20 10:30:45

乐迪信息:智慧煤矿解决方案:AI摄像机智能预警系统

AI摄像机智能预警系统为煤矿安全生产提供了全新的技术路径。该系统通过在煤矿关键区域部署智能摄像设备&#xff0c;结合AI算法实现对人员行为、设备状态及环境风险的实时识别与预警&#xff0c;有效提升了煤矿安全管理水平。一&#xff1a;系统架构与技术原理AI摄像机智能预警…

作者头像 李华
网站建设 2026/4/18 9:17:47

新教程!AI大模型部署核心:FastAPI从入门到实战!

AI风口正当时&#xff0c; 想从Python基础迈向高薪开发&#xff1f; 想在AI浪潮中手握核心部署能力&#xff1f; 黑马程序员《Python Web开发&#xff1a;FastAPI从入门到实战》全新教程&#xff0c;正式上线&#xff01;一套教程让你轻松掌握AI大模型部署核心技能&#xff…

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

告别“失忆“Agent:构建生产级AI系统的九大黄金法则

文章基于Old Dominion大学研究团队的最新论文&#xff0c;详细介绍了构建生产级Agentic AI工作流的九大黄金法则&#xff0c;包括工具调用优于MCP、直接函数优于Agent工具、单一职责原则等。通过完整的播客生成系统案例&#xff0c;展示了如何将理论转化为可落地的工程实践&…

作者头像 李华