You're reading the documentation for an older, but still supported, version of ROS 2. For information on the latest version, please have a look at Jazzy.
您正在阅读 ROS 2 旧版但仍受支持的文档。有关最新版本的信息,请查看 Jazzy。

Getting Started 开始使用

Installation 安装

Binary packages 二进制软件包

The ros2_control framework is released for ROS 2 humble. To use it, you have to install ros-humble-ros2-control and ros-humble-ros2-controllers packages.
The ros2_control 框架已发布用于 ROS 2 humble。要使用它,您必须安装 ros-humble-ros2-controlros-humble-ros2-controllers 软件包。

Building from Source 从源代码构建

To receive the latest features and bug fixes or if you want to contribute to the framework, you can build the framework from source.
为了接收最新功能和错误修复,或者如果您想为框架做出贡献,您可以从源代码构建框架。

You can choose between the following options:
您可以选择以下选项之一:

  • Stable version: These branches will be released in the future in the respective ROS 2 distribution binaries.
    稳定版本:这些分支将在未来分别以各自 ROS 2 发行版的二进制文件形式发布。

  • Development version: We thrive to make the rolling development version (from the master branches) of the ros2_control stack compatible with earlier releases of ROS2. This is done by building the rolling version of the stack from source with the earlier releases of ROS2.
    开发版本:我们努力使 ros2_control 堆栈的滚动开发版本(从主分支)与 ROS2 的早期版本兼容。这是通过使用 ROS2 的早期版本从源代码构建堆栈的滚动版本来实现的。

  • Download all repositories
    下载所有仓库

    mkdir -p ~/ros2_ws/src
    cd ~/ros2_ws/
    vcs import --input https://raw.githubusercontent.com/ros-controls/ros2_control_ci/master/ros_controls.$ROS_DISTRO.repos src
    
  • Install dependencies: 安装依赖项:

    rosdep update --rosdistro=$ROS_DISTRO
    sudo apt-get update
    rosdep install --from-paths src --ignore-src -r -y
    
  • Build everything, e.g. with:
    构建一切,例如使用:

    . /opt/ros/${ROS_DISTRO}/setup.sh
    colcon build --symlink-install
    
  • Do not forget to source setup.bash from the install folder!
    不要忘记从 install 文件夹中获取 setup.bash

Architecture 架构

The source code for the ros2_control framework can be found in the ros2_control and ros2_controllers GitHub repositories. The following figure shows the architecture of the ros2_control framework.
源代码位于 ros2_control 和 ros2_controllers GitHub 仓库中。以下图显示了 ros2_control 框架的架构。

"ros2_control Architecture"

Controller Manager 控制器管理器

The Controller Manager (CM) connects the controllers and hardware-abstraction sides of the ros2_control framework. It also serves as the entry-point for users via ROS services. The CM implements a node without an executor so that it can be integrated into a custom setup. However, it’s usually recommended to use the default node-setup implemented in ros2_control_node file from the controller_manager package. This manual assumes that you use this default node-setup.
控制器管理器(CM)连接 ros2_control 框架的控制器和硬件抽象部分。它还作为用户通过 ROS 服务的入口点。CM 实现了一个没有执行器的节点,以便它可以集成到自定义设置中。然而,通常建议使用 controller_manager 包中实现的默认节点设置,即 ros2_control_node 文件。本手册假定您使用此默认节点设置。

On the one hand, CM manages (e.g. loads, activates, deactivates, unloads) controllers and the interfaces they require. On the other hand, it has access (via the Resource Manager) to the hardware components, i.e. their interfaces. The Controller Manager matches required and provided interfaces, granting controllers access to hardware when enabled, or reporting an error if there is an access conflict.
一方面,CM 管理(例如加载、激活、停用、卸载)控制器及其所需的接口。另一方面,它通过资源管理器访问硬件组件,即它们的接口。控制器管理器匹配所需和提供的接口,在启用时授予控制器对硬件的访问权限,或者在存在访问冲突时报告错误。

The execution of the control-loop is managed by the CM’s update() method. It reads data from the hardware components, updates outputs of all active controllers, and writes the result to the components.
控制循环的执行由 CM 的 update() 方法管理。它从硬件组件读取数据,更新所有活动控制器的输出,并将结果写入组件。

Resource Manager 资源管理器

The Resource Manager (RM) abstracts physical hardware and its drivers (called hardware components) for the ros2_control framework. The RM loads the components using the pluginlib-library, manages their lifecycle and components’ state and command interfaces. The abstraction provided by RM allows reuse of implemented hardware components, e.g., robot and gripper, without any implementation, and flexible hardware application for state and command interfaces, e.g., separate hardware/communication libraries for motor control and encoder reading.
资源管理器(RM)为 ros2_control 框架抽象物理硬件及其驱动程序(称为硬件组件)。RM 使用 pluginlib -库加载组件,管理它们的生命周期和组件状态及命令接口。RM 提供的抽象允许重用已实现的硬件组件,例如机器人抓取器,无需任何实现,并且为状态和命令接口提供灵活的硬件应用,例如为电机控制和编码器读取分别使用独立的硬件/通信库。

In the control loop execution, the RM’s read() and write() methods handle the communication with the hardware components.
在控制循环执行中,RM 的 read()write() 方法处理与硬件组件的通信。

Controllers 控制器

The controllers in the ros2_control framework are based on control theory. They compare the reference value with the measured output and, based on this error, calculate a system’s input. The controllers are objects derived from ControllerInterface (controller_interface package in ros2_control) and exported as plugins using pluginlib-library. For an example of a controller check the ForwardCommandController implementation in the ros2_controllers repository. The controller lifecycle is based on the LifecycleNode class, which implements the state machine described in the Node Lifecycle Design document.
控制器在 ros2_control 框架中基于控制理论。它们将参考值与测量输出进行比较,并根据此误差计算系统的输入。控制器是从 ControllerInterface(ros2_control 中的 controller_interface 包)派生的对象,并使用 pluginlib -库作为插件导出。有关控制器的示例,请查看 ros2_controllers 存储库中的 ForwardCommandController 实现。控制器生命周期基于 LifecycleNode 类,该类实现了节点生命周期设计文档中描述的状态机。

When the control-loop is executed, the update() method is called. This method can access the latest hardware state and enable the controller to write to the hardware command interfaces.
当控制回路执行时,调用 update() 方法。此方法可以访问最新的硬件状态,并使控制器能够写入硬件命令接口。

User Interfaces 用户界面

Users interact with the ros2_control framework using Controller Manager’s services. For a list of services and their definitions, check the srv folder in the controller_manager_msgs package.
用户通过控制器管理器的服务与 ros2_control 框架交互。有关服务和其定义的列表,请检查 controller_manager_msgs 包中的 srv 文件夹。

While service calls can be used directly from the command line or via nodes, there exists a user-friendly Command Line Interface (CLI) which integrates with the ros2 cli. This supports auto-complete and has a range of common commands available. The base command is ros2 control. For the description of our CLI capabilities, see the Command Line Interface (CLI) documentation.
虽然服务调用可以直接从命令行或通过节点使用,但存在一个用户友好的 Command Line Interface (CLI),它与 ros2 cli 集成。这支持自动完成,并提供了一系列常用命令。基本命令是 ros2 control 。有关我们 CLI 功能的描述,请参阅命令行界面(CLI)文档。

Hardware Components 硬件组件

The hardware components realize communication to physical hardware and represent its abstraction in the ros2_control framework. The components have to be exported as plugins using pluginlib-library. The Resource Manager dynamically loads those plugins and manages their lifecycle.
硬件组件实现与物理硬件的通信,并在 ros2_control 框架中表示其抽象。组件必须使用 pluginlib -library 导出为插件。资源管理器动态加载这些插件并管理其生命周期。

There are three basic types of components:
有三个基本类型的组件:

System 系统

Complex (multi-DOF) robotic hardware like industrial robots. The main difference between the Actuator component is the possibility to use complex transmissions like needed for humanoid robot’s hands. This component has reading and writing capabilities. It is used when there is only one logical communication channel to the hardware (e.g., KUKA-RSI).
复杂(多自由度)的机器人硬件,如工业机器人。执行器组件的主要区别在于可以使用复杂传动,如类人机器人手部所需。该组件具有读写能力。当硬件只有一个逻辑通信通道时使用(例如,KUKA-RSI)。

Sensor 传感器

Robotic hardware is used for sensing its environment. A sensor component is related to a joint (e.g., encoder) or a link (e.g., force-torque sensor). This component type has only reading capabilities.
机器人硬件用于感知其环境。传感器组件与关节(例如,编码器)或连杆(例如,力矩传感器)相关。此类组件类型只有读取能力。

Actuator 执行器

Simple (1 DOF) robotic hardware like motors, valves, and similar. An actuator implementation is related to only one joint. This component type has reading and writing capabilities. Reading is not mandatory if not possible (e.g., DC motor control with Arduino board). The actuator type can also be used with a multi-DOF robot if its hardware enables modular design, e.g., CAN-communication with each motor independently.
简单(1 自由度)的机器人硬件,如电机、阀门等。执行器实现仅与一个关节相关。此类组件具有读写能力。如果不可行(例如,使用 Arduino 板控制直流电机),则读取不是必需的。执行器类型也可以与多自由度机器人一起使用,如果其硬件支持模块化设计,例如,每个电机独立进行 CAN 通信。

A detailed explanation of hardware components is given in the Hardware Access through Controllers design document.
硬件组件的详细说明在通过控制器访问硬件的设计文档中给出。

Hardware Description in URDF
硬件描述在 URDF 中

The ros2_control framework uses the <ros2_control>-tag in the robot’s URDF file to describe its components, i.e., the hardware setup. The chosen structure enables tracking together multiple xacro-macros into one without any changes. The example hereunder shows a position-controlled robot with 2-DOF (RRBot), an external 1-DOF force-torque sensor, and an externally controlled 1-DOF parallel gripper as its end-effector. For more examples and detailed explanations, check the ros2_control_demos site and ROS 2 Control Components URDF Examples design document.
The ros2_control 框架使用机器人 URDF 文件中的 <ros2_control> -tag 来描述其组件,即硬件设置。所选结构允许将多个 xacro -macros 跟踪到一起而不做任何更改。下面的示例展示了一个位置控制的 2-自由度(RRBot)机器人、一个外部 1-自由度力矩传感器以及一个外部控制的 1-自由度平行夹爪作为其末端执行器。更多示例和详细说明,请查看 ros2_control_demos 网站 和 ROS 2 Control Components URDF Examples 设计文档。

<ros2_control name="RRBotSystemPositionOnly" type="system">
 <hardware>
   <plugin>ros2_control_demo_hardware/RRBotSystemPositionOnlyHardware</plugin>
   <param name="example_param_write_for_sec">2</param>
   <param name="example_param_read_for_sec">2</param>
 </hardware>
 <joint name="joint1">
   <command_interface name="position">
     <param name="min">-1</param>
     <param name="max">1</param>
   </command_interface>
   <state_interface name="position"/>
 </joint>
 <joint name="joint2">
   <command_interface name="position">
     <param name="min">-1</param>
     <param name="max">1</param>
   </command_interface>
   <state_interface name="position"/>
 </joint>
</ros2_control>
<ros2_control name="RRBotForceTorqueSensor1D" type="sensor">
 <hardware>
   <plugin>ros2_control_demo_hardware/ForceTorqueSensor1DHardware</plugin>
   <param name="example_param_read_for_sec">0.43</param>
 </hardware>
 <sensor name="tcp_fts_sensor">
   <state_interface name="force"/>
   <param name="frame_id">rrbot_tcp</param>
   <param name="min_force">-100</param>
   <param name="max_force">100</param>
 </sensor>
</ros2_control>
<ros2_control name="RRBotGripper" type="actuator">
 <hardware>
   <plugin>ros2_control_demo_hardware/PositionActuatorHardware</plugin>
   <param name="example_param_write_for_sec">1.23</param>
   <param name="example_param_read_for_sec">3</param>
 </hardware>
 <joint name="gripper_joint ">
   <command_interface name="position">
     <param name="min">0</param>
     <param name="max">50</param>
   </command_interface>
   <state_interface name="position"/>
   <state_interface name="velocity"/>
 </joint>
</ros2_control>

Running the Framework for Your Robot
运行您的机器人框架

To run the ros2_control framework, do the following. The example files can be found in the ros2_control_demos repository.
要运行 ros2_control 框架,请执行以下操作。示例文件可以在 ros2_control_demos 仓库中找到。

  1. Create a YAML file with the configuration of the controller manager and two controllers. (Example configuration for RRBot)
    创建一个控制器管理器和两个控制器的 YAML 配置文件。(RRBot 的示例配置)

  2. Extend the robot’s URDF description with needed <ros2_control> tags. It is recommended to use macro files (xacro) instead of pure URDF. (Example URDF for RRBot)
    扩展机器人的 URDF 描述,添加所需的 <ros2_control> 标签。建议使用宏文件(xacro)而不是纯 URDF。(RRBot 的示例 URDF)

  3. Create a launch file to start the node with Controller Manager. You can use a default ros2_control node (recommended) or integrate the controller manager in your software stack. (Example launch file for RRBot)
    创建启动文件以启动带有控制器管理器的节点。您可以使用默认的 ros2_control 节点(推荐)或在您的软件堆栈中集成控制器管理器。(RRBot 的示例启动文件)

NOTE: You could alternatively use a script to create setup a skeleton of the “hardware_interface” package by using the scripts provided by one of our maintainers.
注意:您也可以使用脚本,通过使用我们维护者提供的脚本创建“hardware_interface”包的骨架。