- 操作系统:ubuntu22.04
- IDE:Visual Studio Code
- 编程语言:C++11
- ROS版本:2
ROS 2 Gazebo 三维物理仿真平台简介
Gazebo 是一个强大的三维机器人仿真环境,它能够模拟复杂的机器人系统和环境。结合 ROS 2,你可以使用 Gazebo 进行高级的机器人仿真,包括但不限于:
- 物理仿真:重力、碰撞检测等。
- 传感器仿真:激光雷达(LiDAR)、摄像头、IMU 等。
- 动力学仿真:轮式移动、机械臂运动等。
在 ROS 2 中,通常通过 ros_gz_sim 包来集成 Gazebo 和 ROS 2 的功能。这使得你可以在 ROS 2 节点中发布和订阅话题,同时在 Gazebo 中进行实时的物理仿真。
示例:创建并运行一个简单的机器人模型
目标
我们将创建一个简单的机器人模型,包含一个圆柱体作为基座,并在其顶部添加一个小球。此外,我们还会为该模型添加一些基本的物理属性,如质量、惯性矩阵等,以便于在 Gazebo 中进行物理仿真。
步骤
-
安装必要的软件包
首先确保你的系统已经安装了 Gazebo 和相关的 ROS 2 包。对于 Ubuntu 22.04 和 ROS 2 Humble 版本,可以执行以下命令来安装:
sudo apt update && sudo apt install ros-humble-gazebo-ros-pkgs ros-humble-ros-gz-sim
- 创建工作空间和包
创建一个新的 ROS 2 工作空间,并在一个新的包中存放我们的模型文件和启动脚本。
ros2 pkg create my_robot_description --build-type ament_python --dependencies launch_ros rclpy
mkdir -p ~/ros2_ws/src/my_robot_description/urdf
cd ~/ros2_ws/src/my_robot_description/
- 编写 URDF 文件
在 ~/ros2_ws/src/my_robot_description/urdf/ 目录下创建一个名为 my_robot.urdf.xacro 的文件,并添加如下内容:
<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="simple_robot"><!-- Base Link --><link name="base_link"><visual><geometry><cylinder radius="0.2" length="0.6"/></geometry><material name="blue"><color rgba="0 0 1 1"/></material></visual><collision><geometry><cylinder radius="0.2" length="0.6"/></geometry></collision><inertial><mass value="1"/><inertia ixx="0.01" ixy="0" ixz="0"iyy="0.01" iyz="0"izz="0.01"/></inertial></link><!-- Top Link --><link name="top_link"><visual><geometry><sphere radius="0.1"/></geometry><material name="red"><color rgba="1 0 0 1"/></material></visual><collision><geometry><sphere radius="0.1"/></geometry></collision><inertial><mass value="0.1"/><inertia ixx="0.001" ixy="0" ixz="0"iyy="0.001" iyz="0"izz="0.001"/></inertial></link><!-- Joints --><joint name="fixed_joint" type="fixed"><parent link="base_link"/><child link="top_link"/><origin xyz="0 0 0.3" rpy="0 0 0"/></joint><!-- Gazebo Plugins --><gazebo reference="base_link"><material>Gazebo/Blue</material></gazebo><gazebo reference="top_link"><material>Gazebo/Red</material></gazebo>
</robot>
- 创建 Launch 文件
在 ~/ros2_ws/src/my_robot_description/launch/ 目录下创建一个名为 display_gazebo.launch.py 的文件,并添加如下内容:
import os
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directorydef generate_launch_description():# Path to the world fileworld_file = os.path.join(get_package_share_directory('my_robot_description'), 'worlds', 'empty.world')# Launch Gazebo with the empty worldgazebo = IncludeLaunchDescription(PythonLaunchDescriptionSource([os.path.join(get_package_share_directory('ros_gz_sim'),'launch','gz_sim.launch.py')]),launch_arguments={'gz_args': '-r ' + world_file}.items())# Load the URDF into the /robot_description topicurdf_path = os.path.join(get_package_share_directory('my_robot_description'), 'urdf', 'my_robot.urdf.xacro')robot_state_publisher = Node(package='robot_state_publisher',executable='robot_state_publisher',parameters=[{'robot_description': open(urdf_path).read()}])# Spawn the robot in Gazebospawn_entity = Node(package='ros_gz_sim',executable='create',arguments=['-topic', 'robot_description', '-name', 'my_robot'],output='screen')return LaunchDescription([gazebo,robot_state_publisher,spawn_entity])
- 创建世界文件
在 ~/ros2_ws/src/my_robot_description/worlds/ 目录下创建一个名为 empty.world 的文件,并添加如下内容:
- 构建工作空间
回到工作空间根目录并构建:
cd ~/ros2_ws
colcon build --packages-select my_robot_description
source install/setup.bash
- 运行仿真
使用以下命令启动仿真:
bash
深色版本
ros2 launch my_robot_description display_gazebo.launch.py
你应该可以看到 Gazebo 启动,并且加载了一个空的世界,接着会看到你的简单机器人模型被放置在这个虚拟环境中。