ros2+Gazebo的一个简单入门例子
1、Gazebo简介
Gazebo是ROS系统中最为常用的三维物理仿真平台,支持动力学引擎,可以实现高质量的图形渲染,不仅可以模拟机器人及周边环境,还可以加入摩擦力、弹性系数等物理属性。所以类似Gazebo这样的仿真平台,可以帮助我们验证机器人算法、优化机器人设计、测试机器人场景应用,为机器人开发提供更多可能。
2、安装运行
# 1通过命令apt进行安装gazebo
sudo apt install gazebo
# 2下载model库 mkdir -p ~/.gazebo cd ~/.gazebo # Clone to a temporary location git clone https://gitee.com/ohhuo/gazebo_models.git /tmp/gazebo_models # Copy contents, overwriting existing files cp -rf /tmp/gazebo_models/* ~/.gazebo/models/
# Remove the temporary clone rm -rf /tmp/gazebo_models rm -rf ~/.gazebo/models/.git
#3 过以下命令启动或直接通过桌面图标启动
gazebo --verbose -s libgazebo_ros_init.so -s libgazebo_ros_factory.so

3、安装gazebo节点与启动服务
#0、安装节点包 sudo apt install ros-${ROS_DISTRO}-gazebo-* #或 sudo apt install ros-$ROS_DISTRO-gazebo-ros-pkgs
#1、查看节点
ros2 node list
#2、查看节点的对外提供的服务
ros2 service list
# 借助AI工具,掌握服务用途。例如:
#/spawn_entity,用于加载模型到gazebo中
#/get_model_list,用于获取模型列表
#/delete_entity,用于删除gazbeo中已经加载的模型
4、创建功能包,建urdf格式仿真模型
| 具体是:
#创建一个myrobot功能包,用来存放我们的URDF模型文件和启动文件
$ mkdir -p ws/src
$ cd ws/src
$ ros2 pkg create myrobot --build-type ament_cmake
#进入到myrobot的目录下,创建launch、urdf文件夹,在urdf文件夹下创建一个demo01_base.urdf文件,这个文件就是一个简单的演示文件,只有一个基础的立方体。
|
<robot name="myrobot"> <link name="base_link"> <visual> <geometry> <box size="0.2 0.2 0.2"/> </geometry> <origin xyz="0.0 0.0 0.0"/> </visual> <collision> <geometry> <box size="0.2 0.2 0.2"/> </geometry> <origin xyz="0.0 0.0 0.0"/> </collision> <inertial> <mass value="0.1"/> <inertia ixx="0.000190416666667" ixy="0" ixz="0" iyy="0.0001904" iyz="0" izz="0.00036"/> </inertial> </link> <gazebo reference="base_link"> <material>Gazebo/Red</material> </gazebo> </robot>
5、编写launch文件,启动仿真,在gazebo显示
launch文件的编写,launch文件主要启动两个部分,启动Gazebo文件,然后将机器人模型加载到Gazebo中。 文件树如下图

在launch目录下新建一个bringup_model.launch.py文件完整的启动文件如下:
import os from launch import LaunchDescription from launch.actions import ExecuteProcess from launch_ros.actions import Node from launch_ros.substitutions import FindPackageShare def generate_launch_description(): robot_name_in_model = 'myrobot' package_name = 'myrobot' urdf_name = "demo01_base.urdf" ld = LaunchDescription() pkg_share = FindPackageShare(package=package_name).find(package_name) urdf_model_path = os.path.join(pkg_share, f'urdf/{urdf_name}') # 检查文件是否存在 if not os.path.exists(urdf_model_path): print(f"Warning: URDF file not found at {urdf_model_path}") # Start Gazebo (使用 gazebo_ros 的 launch 文件更可靠)
# 对应的bash命令行:$ gazebo --verbose -s libgazebo_ros_init.so -s libgazebo_ros_factory.so start_gazebo_cmd = ExecuteProcess( cmd=['gazebo', '--verbose', '-s', 'libgazebo_ros_init.so', '-s', 'libgazebo_ros_factory.so'], output='screen' ) # Launch the robot spawn_entity_cmd = Node( package='gazebo_ros', executable='spawn_entity.py', arguments=['-entity', robot_name_in_model, '-file', urdf_model_path], output='screen' ) ld.add_action(start_gazebo_cmd) ld.add_action(spawn_entity_cmd) return ld
在Cmakelist中填入以下内容,用于将我们的urdf和launch文件夹安装进install目录下
cmake_minimum_required(VERSION 3.8) project(myrobot) if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wall -Wextra -Wpedantic) endif() # find dependencies find_package(ament_cmake REQUIRED) # uncomment the following section in order to fill in # further dependencies manually. # find_package(<dependency> REQUIRED) if(BUILD_TESTING) find_package(ament_lint_auto REQUIRED) # the following line skips the linter which checks for copyrights # comment the line when a copyright and license is added to all source files set(ament_cmake_copyright_FOUND TRUE) # the following line skips cpplint (only works in a git repo) # comment the line when this package is in a git repo and when # a copyright and license is added to all source files set(ament_cmake_cpplint_FOUND TRUE) ament_lint_auto_find_test_dependencies() endif() # 将我们的urdf和launch文件夹安装进install目录下 install( DIRECTORY urdf launch DESTINATION share/${PROJECT_NAME} ) ament_package()
在workspace目录下,编译此包,启动*.launch.py文件:
colcon build --packages-select myrobot
. install/setup.bash
ros2 launch myrobot bringup_model.launch.py

gazebo显示长方体


浙公网安备 33010602011771号