- 使用 Gazebo 加载 URDF
- 安装并启动 Gazebo 插件 这是因为 Gazebo 是独立仿真软件,通信由插件负责
sudo apt install ros-humble-gazebo-ros
gazebo --verbose -s libgazebo_ros_init.so -s libgazebo_ros_factory.so - 查看插件节点服务(方法)
/spawn_entity:加载模型到 Gazebo 中string name:需要加载的实体的名称 (可选的)string xml:实体的 XML 描述字符串, URDF 或者 SDFstring robot_namespace:产生的机器人和所有的 ROS 接口的命名空间 多机器人仿真的时候很有用geometry_msgs/Pose initial_pose:机器人的初始化位置string reference_frame:初始姿态是相对于该实体的 frame 定义的;如果保持empty或world或map,则使用 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-
加载模型
rqtService -> Service Caller- 选择
/spawn_entity服务
- 将 URDF 模型放到
xml的Expression处 - 点击右上角 Call,模型成功载入 Gazebo
- 重复加载:修改
robot_namespace和position,再 Call,即可拥有两个一模一样的模型
-
查询和删除机器人
- 选择
/get_model_list服务
- 在
model_names的Value中可知,当前有“大地”和两个加载进去的模型 - 选择
/delete_entity服务
- 修改
name的Expression为要删除的模型 - 点击右上角 Call,模型成功从 Gazebo 删除
- 选择
-
将启动 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 ldcolcon build --packages-select fishbot_description
source install/setup.bash
ros2 launch fishbot_description gazebo.launch.py