跳转至

Mock Components(模拟组件)

Mock Components 是硬件组件(System、Sensor、Actuator)的简易“仿真”实现。它们通过将命令直接镜像到状态(mirror commands to states),提供理想化的行为。

其主要用途是在无真实硬件的情况下,测试 ros2_control 框架的完整数据流(“piping”),包括: - 控制器(Controllers) - 状态广播器(Broadcasters) - 启动文件(Launch files) - 与 MoveIt 等系统的集成

核心优势:大幅减少在物理硬件上的调试时间,显著提升开发效率。


通用系统(Generic System)

GenericSystem 实现了 hardware_interface::SystemInterface,支持命令接口(command interfaces)和状态接口(state interfaces)。

主要特性

  • ✅ 支持 Mimic 关节(从 URDF 自动解析)
  • ✅ 支持带偏移或不带偏移地将命令镜像到状态
  • ✅ 提供虚拟命令接口,用于从外部节点设置传感器数据(需配合前馈控制器)
  • ✅ 提供虚拟 GPIO 接口,用于从外部节点设置数字/模拟信号(需配合前馈控制器)

参数配置示例

以下是一个包含所有可选参数(含默认值)的完整 <ros2_control> 配置示例:

<ros2_control name="MockHardwareSystem" type="system">
  <hardware>
    <plugin>mock_components/GenericSystem</plugin>
    <param name="calculate_dynamics">false</param>
    <param name="custom_interface_with_following_offset"></param>
    <param name="disable_commands">false</param>
    <param name="mock_gpio_commands">false</param>
    <param name="mock_sensor_commands">false</param>
    <param name="position_state_following_offset">0.0</param>
  </hardware>

  <joint name="joint1">
    <command_interface name="position"/>
    <command_interface name="velocity"/>
    <state_interface name="position">
      <param name="initial_value">3.45</param>
    </state_interface>
    <state_interface name="velocity"/>
    <state_interface name="acceleration"/>
  </joint>

  <joint name="joint2">
    <command_interface name="velocity"/>
    <command_interface name="acceleration"/>
    <state_interface name="position">
      <param name="initial_value">2.78</param>
    </state_interface>
    <state_interface name="velocity"/>
    <state_interface name="acceleration"/>
  </joint>

  <gpio name="flange_vacuum">
    <command_interface name="vacuum"/>
    <state_interface name="vacuum" data_type="double"/>
  </gpio>
</ros2_control>

📌 参考示例: - example_2:演示 calculate_dynamics 的使用 - example_10:演示与 GPIO 接口的结合使用


组件级参数(Component Parameters)

参数 类型 默认值 说明
calculate_dynamics boolean false 若为 true,使用前向欧拉积分有限差分法从命令计算状态(如从速度积分得位置)
custom_interface_with_following_offset string "" 指定一个自定义接口名称,将带偏移的命令映射到该接口(见下文 position_state_following_offset
disable_commands boolean false 禁用命令到状态的镜像。可用于模拟硬件连接异常(无反馈)或测试开环控制场景
mock_gpio_commands boolean false 为 GPIO 创建虚拟命令接口,允许外部节点(如前馈控制器)设置其值
mock_sensor_commands boolean false 为传感器创建虚拟命令接口,允许外部节点注入仿真数据
position_state_following_offset double 0.0 在镜像命令到状态时,添加的偏移量
• 若 custom_interface_with_following_offset 为空 → 偏移应用于 position 状态接口
• 若指定了自定义接口 → position + offset 的值写入该自定义接口

接口级参数(Per-Interface Parameters)

initial_value(可选;double)

设置状态接口在启动后的初始值

<state_interface name="position">
  <param name="initial_value">3.45</param>
</state_interface>
  • 默认值0.0
  • 适用范围
  • 关节的状态接口(在 configure 生命周期阶段初始化)
  • Mock Components 还支持GPIOSensor 的状态接口设置初始值
  • 兼容性:此参数与 gz_ros2_control 插件共享,确保跨仿真器配置一致性

典型使用场景

  1. 控制器开发与测试
    在无硬件情况下验证 JointTrajectoryControllerForwardCommandController 等行为。

  2. 故障模拟
    通过 disable_commands=true 模拟传感器失效或通信中断。

  3. 外部信号注入
    结合 ForwardCommandController,通过话题向 mock_gpio_commandsmock_sensor_commands 注入仿真数据(如真空吸盘状态、力传感器读数)。

  4. 带偏移仿真
    使用 position_state_following_offset=0.1 模拟编码器零点偏移或机械间隙。


💡 提示:Mock Components 是 ros2_control 开发工作流的核心工具,推荐在 CI/CD 流程中广泛使用,以确保软件栈在部署前已充分验证。