ROS2学习笔记(2)什么是ROS2 nodes

本文详细介绍了ROS2中的节点概念,通过实例探讨了/turtlesim和/teleop_turtle节点的功能,展示了它们如何通过topic和服务进行数据交互。学习了节点的结构和信息查看方法,以及节点间通信的重要性。

什么是ROS2 nodes


先了解一下ROS2 graph。ROS2 graph是一个ROS2元素同时处理数据的网络。它包含了所有可执行程序和它们之间的连接。

ROS中每个负责单独功能的模块叫做节点(node)(例如,一个节点用于控制轮子电机,一个节点用于控制激光测距等)。每个节点可以通过topics(话题)、services(服务)、actions(动作)或parameters(参数)向其它节点发送和接收数据。

如以下动图所示,包含3个节点,通过话题和服务进行数据传输。

在这里插入图片描述

一个完整的机器人系统由许多协同工作的节点组成。在 ROS 2 中,单个可执行程序(C++ 程序、Python 程序等)可以包含一个或多个节点。

节点里面有啥?

在上一篇学习笔记里我已经安装了ROS2,并且可以正常运行海龟模拟器(turtlesim)。那就通过turtlesim来学一下节点。

运行turtlesim

打开一个终端,先加载ROS2环境source /opt/ros/galactic/setup.bash(每次打开新的terminal必 先source一下 )。然后运行命令:

ros2 run turtlesim turtlesim_node

看到海龟模拟器窗口

在这里插入图片描述


查看节点列表

可以通过以下命令才查看当前系统中正在运行的节点。新开一个终端输入:

ros2 node list

终端返回:

/turtlesim

说明当前节点列表中只有一个叫/turtlesim的节点。

再开一个新的终端,运行海龟控制键盘:

ros2 run turtlesim turtle_teleop_key

然后在前一个终端重新查看以下节点列表。

终端返回:

/turtlesim

/teleop_turtle

可以看到现在节点列表中有两个节点。


查看节点信息

通过ros2 node list命令可以知道当前运行中的节点个数和名称,现在可以用以下命令来看关于节点的更多信息

ros2 node info <node_name>

比如我要查看/turtlesim这个节点,在终端输入

ros2 node info /turtlesim

终端返回下面这一大串的信息,有订阅者列表,发布者列表,服务列表和动作列表。

/turtlesim
  Subscribers:
    /parameter_events: rcl_interfaces/msg/ParameterEvent
    /turtle1/cmd_vel: geometry_msgs/msg/Twist
  Publishers:
    /parameter_events: rcl_interfaces/msg/ParameterEvent
    /rosout: rcl_interfaces/msg/Log
    /turtle1/color_sensor: turtlesim/msg/Color
    /turtle1/pose: turtlesim/msg/Pose
  Service Servers:
    /clear: std_srvs/srv/Empty
    /kill: turtlesim/srv/Kill
    /reset: std_srvs/srv/Empty
    /spawn: turtlesim/srv/Spawn
    /turtle1/set_pen: turtlesim/srv/SetPen
    /turtle1/teleport_absolute: turtlesim/srv/TeleportAbsolute
    /turtle1/teleport_relative: turtlesim/srv/TeleportRelative
    /turtlesim/describe_parameters: rcl_interfaces/srv/DescribeParameters
    /turtlesim/get_parameter_types: rcl_interfaces/srv/GetParameterTypes
    /turtlesim/get_parameters: rcl_interfaces/srv/GetParameters
    /turtlesim/list_parameters: rcl_interfaces/srv/ListParameters
    /turtlesim/set_parameters: rcl_interfaces/srv/SetParameters
    /turtlesim/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically
  Service Clients:

  Action Servers:
    /turtle1/rotate_absolute: turtlesim/action/RotateAbsolute
  Action Clients:

再来看一下海龟操控键盘节点/teleop_turtle的信息:

/teleop_turtle
  Subscribers:
    /parameter_events: rcl_interfaces/msg/ParameterEvent
  Publishers:
    /parameter_events: rcl_interfaces/msg/ParameterEvent
    /rosout: rcl_interfaces/msg/Log
    /turtle1/cmd_vel: geometry_msgs/msg/Twist
  Service Servers:
    /teleop_turtle/describe_parameters: rcl_interfaces/srv/DescribeParameters
    /teleop_turtle/get_parameter_types: rcl_interfaces/srv/GetParameterTypes
    /teleop_turtle/get_parameters: rcl_interfaces/srv/GetParameters
    /teleop_turtle/list_parameters: rcl_interfaces/srv/ListParameters
    /teleop_turtle/set_parameters: rcl_interfaces/srv/SetParameters
    /teleop_turtle/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically
  Service Clients:

  Action Servers:

  Action Clients:
    /turtle1/rotate_absolute: turtlesim/action/RotateAbsolute

可以看到/turtlesim/teleop_turtle里有几个相同的信息。比如/turtlesim节点的订阅者列表里有/turtle1/cmd_vel,而/teleop_turtle节点的发布者列表也有/turtle1/cmd_vel。比如/turtlesim节点的动作服务端里/turtle1/rotate_absolute,而/teleop_turtle节点的动作客户端里也有/turtle1/rotate_absolute

再看一下,我们启动海龟键盘turtle_teleop_key程序的时候,会返回这一段操作帮助信息:

Reading from keyboard
---------------------------
Use arrow keys to move the turtle.
Use G|B|V|C|D|E|R|T keys to rotate to absolute orientations. 'F' to cancel a rotation.
'Q' to quit.

是不是可以想到键盘控制海龟行走的控制数据就是通过两个节点中相同的信息建立数据连接通信的呢?


关于ROS2 节点的学习就先到这吧。


参考:
  • ROS 2 Documentation:https://docs.ros.org/en/galactic/index.html
### ROS2 C++ Topic Communication Learning Materials and Tutorials For those interested in exploring how to use topics for communication within the ROS2 framework using C++, several resources provide comprehensive guidance on this subject. The official documentation of ROS 2 offers an extensive tutorial that covers setting up publishers and subscribers with detailed explanations and code examples written in C++. This resource is invaluable as it not only explains the theory but also provides practical steps necessary to establish topic-based communications between nodes[^1]. Additionally, a popular choice among developers includes tutorials available from Robot Ignite Academy which presents step-by-step instructions alongside video demonstrations specifically tailored towards understanding message passing through topics via C++ interfaces in ROS2 environments. These sessions are designed to cater both beginners who need foundational knowledge about ROS concepts along with more advanced users looking into optimizing their applications' performance when dealing with real-time data exchange over networks. Another noteworthy mention goes out to Open Robotics’ own set of educational materials where one can find well-documented samples illustrating various aspects related to working with messages across different programming languages including C++; these documents often include best practices recommendations ensuring efficient utilization while adhering closely to community standards established around ROS ecosystem development processes. ```cpp // Example Code Snippet Demonstrating Basic Publisher Node Implementation Using rclcpp Library In C++ #include "rclcpp/rclcpp.hpp" using namespace std::chrono_literals; int main(int argc, char * argv[]) { rclcpp::init(argc, argv); auto node = rclcpp::Node::make_shared("example_publisher"); auto publisher = node->create_publisher<std_msgs::msg::String>("topic", 10); auto msg = std_msgs::msg::String(); msg.data = "Hello, world!"; rclcpp::WallRate loop_rate(1s); // Publish every second while (rclcpp::ok()) { RCLCPP_INFO(node->get_logger(), "Publishing: '%s'", msg.data.c_str()); publisher->publish(msg); rclcpp::spin_some(node); loop_rate.sleep(); } rclcpp::shutdown(); } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kal-Lai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值