ROS2 学习笔记3:了解ROS2 节点 Node

Background 背景

1 ROS 2 图

在接下来的几个教程中,将学习一系列核心ROS 2概念,这些概念构成了所谓的 ROS(2) Graph

ROS图是ROS 2元素同时处理数据的网络。它包含所有可执行文件以及它们之间的连接 (如果要将它们全部映射并可视化)。

思考及疑问: ROS2 Graph, ROS2 Node topology, how to display in graph

2 ROS 2中的节点

ROS中的每个节点应负责单个模块目的 (例如,一个用于控制车轮电机的节点,一个用于控制激光测距仪的节点等)。每个节点可以通过话题、服务、动作或参数向其他节点发送和接收数据。

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

在这里插入图片描述

Prerequisites 前提

安装turtlesim

在新终端中 sourceROS 2

Tasks 任务

1 ROS2 运行

指令ros2 run从包中启动一个可执行文件

ros2 run <package_name> <executable_name> 

运行turtlesim,打开一个新终端,然后输入以下命令

ros2 run turtlesim turtlesim_node 

turtlesim仿真窗口启动,如前面课程所示

这里,包的名字是turtlesim,可执行文件名字是turtlesim_node

我们还不知节点的名字,然而,你可以使用指令ros2 node list来找节点名字

2 ROS2 节点列表

ros2 node list 显示所有正在运行的节点的名称。

当turtlesim仍在另一个终端中运行时,打开一个新终端,然后输入以下命令:

ros2 node list     

终端将返回节点名称:

/turtlesim    

打开另一个新终端,并使用以下命令启动teleop节点:

ros2 run turtlesim turtle_teleop_key   

这里,包仍是 turtlesim包,可执行文件为 turtle_teleop_key

回到 ros2 node list的终端,再次运行。现在,将看到两个活动节点的名称:

/turtlesim   
/teleop_turtle      

2.1 重映射

Remapping https://design.ros2.org/articles/ros_command_line_arguments.html#name-remapping-rules允许您将默认节点属性 (如节点名称、话题名称、服务名称等) 重新分配给自定义值。在上一个教程中,使用 turtle_teleop_key 上的重映射来更改被控制的默认海龟。

现在,让我们重新分配 /turtlesim 节点的名称。在新终端中,运行以下命令:

ros2 run turtlesim turtlesim_node --ros-args --remap __node:=my_turtle   


**思考及疑问: 该命令各参数详解 重映射规则, – remap aaa:= bbb **

由于您再次在turtlesim上调用ros2 run,另一个turtlesim窗口将打开。但是,现在如果您返回到运行 ros2 node list 的终端,并再次运行它,您将看到三个节点名称:

/turtlesim
/teleop_turtle
/my_turtle   

3 ROS2 节点信息

现在你知道节点名字了,也可以通过下面指令来了解节点更多信息:

ros2 node info <node_name> 

要检查您的最新节点 my_turtle ,请运行以下命令:

ros2 node info /my_turtle

ros2 node info 与该节点交互的订阅者、发布者、服务和动作 (ROS图连接) 的列表。输出应如下所示:

**思考及疑问: how to display the topology in Graph **

/my_turtle
  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
    /my_turtle/describe_parameters: rcl_interfaces/srv/DescribeParameters
    /my_turtle/get_parameter_types: rcl_interfaces/srv/GetParameterTypes
    /my_turtle/get_parameters: rcl_interfaces/srv/GetParameters
    /my_turtle/list_parameters: rcl_interfaces/srv/ListParameters
    /my_turtle/set_parameters: rcl_interfaces/srv/SetParameters
    /my_turtle/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically
    /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
  Service Clients:

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

现在尝试在 /teleop_turtle 节点上运行相同的命令,看看它的连接与 my_turtle 有什么不同

在接下来的教程中了解更多关于ROS图连接概念的信息,包括消息类型。

Summary 总结

节点是ROS2 基本元素,在机器人系统里面负责单一模块功能作用

在本课程,通过运行中执行文件turtlesim_nodeturtle_teleop_key,启动turtlesim包中的节点

你学会了使用ros2 node list来获取活动节点的名字,使用ros2 node info指令去深入了解某个节点.对于一个复杂真实的机器人系统,这些工具用在了解数据流显得至关重要

Next steps 下一步

了解ROS2 节点后,可以开始学习话题课程,话题是一种节点交流的方式

Related

Node Concepet

A node is a participant in the ROS 2 graph, which uses a client library to communicate with other nodes. Nodes can communicate with other nodes within the same process, in a different process, or on a different machine. Nodes are typically the unit of computation in a ROS graph; each node should do one logical thing.
Nodes can publish to named topics to deliver data to other nodes, or subscribe to named topics to get data from other nodes. They can also act as a service client to have another node perform a computation on their behalf, or as a service server to provide functionality to other nodes. For long-running computations, a node can act as an action client to have another node perform it on their behalf, or as an action server to provide functionality to other nodes. Nodes can provide configurable parameters to change behavior during run-time.
Nodes are often a complex combination of publishers, subscribers, service servers, service clients, action servers, and action clients, all at the same time.
Connections between nodes are established through a distributed discovery process.

  • 20
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ROS2中,std::make_unique函数用于创建一个唯一指针。它是C++14中引入的一个函数模板,用于在堆上创建一个对象,并返回一个指向该对象的唯一指针。在ROS2中,可以使用std::make_unique函数来创建ROS2节点。例如,可以使用以下代码创建一个继承自rclcpp::Node的类的实例: ```cpp auto node = std::make_unique<MinimalPublisher>(); ``` 这将创建一个MinimalPublisher类的实例,并返回一个指向该实例的唯一指针。然后,可以使用该指针来访问该节点的成员函数和变量。 引用: \[1\] 截至目前,galactic虽然对以上过程进行了一定程度的封装,但封装也极为简化,同时read的流程仍没有封装,使用还是很麻烦。节点的建立ROS2使用了完全的面向对象设计,以C++为例,大量的ROS API都封装在了rclcpp::Node这个类中,也就意味着需要使用这些API,你必须定义一个继承自Node的类,这也就强制用户必须使用类的方式构建整个系统,官方的一个简单的例子如下:class MinimalPublisher : public rclcpp::Node { public: MinimalPublisher() : Node("minimal_publisher"), count_(0) { publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10); timer_ = this->create_wall_timer( 500ms, std::bind(&MinimalPublisher::timer_callback, this)); } private: rclcpp::TimerBase::SharedPtr timer_; rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_; size_t count_; } \[2\] 只要在同一ID的机器就可以接受到彼此的消息。yaml配置文件比较坑的一个细节,ROS2在yaml使用上有两个变化,如下:test_node: ros__parameters: common: topic: "your_topic" \[3\] 主要是下面三个部分:名称重定向日志配置参数使用命令行传参在eloquent之后需要加 --ros-args标志,dashing版本不需要eloquent:ros2 run my_package node_executable --ros-args ... #### 引用[.reference_title] - *1* *2* [ROS2实践总结](https://blog.csdn.net/liu3612162/article/details/121906612)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [[ros2学习]-学习ROS 2工具集](https://blog.csdn.net/weixin_36628778/article/details/106375420)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值