1. 使用 Gazebo 加载 URDF
  2. 安装并启动 Gazebo 插件 这是因为 Gazebo 是独立仿真软件,通信由插件负责
sudo apt install ros-humble-gazebo-ros
gazebo --verbose -s libgazebo_ros_init.so -s libgazebo_ros_factory.so 
  1. 查看插件节点服务(方法)
    • /spawn_entity:加载模型到 Gazebo 中
      • string name:需要加载的实体的名称 (可选的)
      • string xml:实体的 XML 描述字符串, URDF 或者 SDF
      • string robot_namespace:产生的机器人和所有的 ROS 接口的命名空间 多机器人仿真的时候很有用
      • geometry_msgs/Pose initial_pose:机器人的初始化位置
      • string reference_frame:初始姿态是相对于该实体的 frame 定义的;如果保持 emptyworldmap ,则使用 Gazebo 的 world 作为 frame。如果指定了不存在的实体,则会返回错误
    • /get_model_list:获取模型列表
    • /delete_entity:删除 gazbeo 中已经加载的模型
# 查看节点和服务
ros2 node list
ros2 service list
# 服务列表
/delete_entity
/get_model_list
/spawn_entity
/gazebo/describe_parameters
/gazebo/get_parameter_types
/gazebo/get_parameters
/gazebo/list_parameters
/gazebo/set_parameters
/gazebo/set_parameters_atomically
# 示例:请求 /spawn_entity 服务来加载模型
ros2 service type /spawn_entity
# 返回 gazebo_msgs/srv/SpawnEntity,查看接口参数
ros2 interface show gazebo_msgs/srv/SpawnEntity
  1. 加载模型

    1. rqt
    2. Service -> Service Caller
    3. 选择 /spawn_entity 服务
    4. 将 URDF 模型放到 xmlExpression
    5. 点击右上角 Call,模型成功载入 Gazebo
    6. 重复加载:修改 robot_namespaceposition,再 Call,即可拥有两个一模一样的模型
  2. 查询和删除机器人

    1. 选择 /get_model_list 服务
    2. model_namesValue 中可知,当前有“大地”和两个加载进去的模型
    3. 选择 /delete_entity 服务
    4. 修改 nameExpression 为要删除的模型
    5. 点击右上角 Call,模型成功从 Gazebo 删除
  3. 将启动 Gazebo 和载入模型写成 launch 文件

    • 启动 Gazebo 的 cmd 写成节点
    • 使用 gazebo_ros 提供的 spawn_entity.py 节点录入 URDF 文件
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 = 'fishbot'
    package_name = 'fishbot_description'
    urdf_name = "fishbot_gazebo.urdf"
 
    ld = LaunchDescription()
    pkg_share = FindPackageShare(package=package_name).find(package_name) 
    urdf_model_path = os.path.join(pkg_share, f'urdf/{urdf_name}')
 
    # Start Gazebo server
    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
colcon build --packages-select fishbot_description
source install/setup.bash
ros2 launch fishbot_description gazebo.launch.py