1. 准备模型
mkdir -p ~/.gazebo/models/table
echo 'export GAZEBO_MODEL_PATH=$HOME/.gazebo/models:$GAZEBO_MODEL_PATH' >> ~/.bashrc
source ~/.bashrc
# 从https://github.com/osrf/gazebo_models下载模型
# 桌子
cd ~/.gazebo/models/table
wget https://raw.githubusercontent.com/osrf/gazebo_models/master/table/model.sdf
wget https://raw.githubusercontent.com/osrf/gazebo_models/master/table/model.config
2. 通过world文件(方案一)
2.1 创建新world文件
cd ~/suo/ur_gazebo/install/ur_simulation_gazebo/share/ur_simulation_gazebo
mkdir worlds
cd worlds
touch compo.world
# 写入下面内容
<?xml version="1.0"?>
<sdf version="1.6">
<world name="default">
<!-- 原始UR5模型(由ur_sim_control.launch.py自动加载) -->
<!-- 新增桌子模型 -->
<include>
<uri>model://table</uri>
<pose>1.5 0 0 0 0 0</pose>
</include>
</world>
</sdf>
2.2. 修改启动文件
# 启动文件
/home/suoxd/suo/ur_gazebo/src/Universal_Robots_ROS2_Gazebo_Simulation/ur_simulation_gazebo/launch/ur_sim_control.launch.py
gazebo = IncludeLaunchDescription(
PythonLaunchDescriptionSource(
[FindPackageShare("gazebo_ros"), "/launch", "/gazebo.launch.py"]
),
launch_arguments={
"gui": gazebo_gui,
"world": PathJoinSubstitution([
FindPackageShare('ur_simulation_gazebo'),
'worlds',
'compo.world' # 新增的世界文件
])
}.items(),
)
2.3 手动拷贝world文件
在ROS 2中,安装非代码文件(如worlds、models、launch文件等)需要使用install(DIRECTORY …)命令。例如,安装launch文件时,可能会有类似install(DIRECTORY launch DESTINATION share/${PROJECT_NAME})的指令。用户需要在CMakeLists.txt中添加类似的命令来安装worlds目录。或者,可以手动将worlds文件从src目录拷贝到install目录。
2.4 编译拷贝
修改ur_simulation_gazebo/CMakeLists.txt,增加对worlds目录的编译
install(
DIRECTORY worlds/
DESTINATION share/${PROJECT_NAME}/worlds
FILES_MATCHING
PATTERN "*.world"
)
重新编译项目
colcon build --packages-select ur_simulation_gazebo --cmake-force-configure
3. 通过节点(方案二)
# 启动文件
/home/suoxd/suo/ur_gazebo/src/Universal_Robots_ROS2_Gazebo_Simulation/ur_simulation_gazebo/launch/ur_sim_control.launch.py
# 新建桌子节点
spawn_table = Node(
package='gazebo_ros',
executable='spawn_entity.py',
arguments=[
'-entity', 'table',
'-x', '1.5', '-y', '0', '-z', '0',
'-database', 'table'
],
output='screen'
)
nodes_to_start = [
robot_state_publisher_node,
joint_state_broadcaster_spawner,
delay_rviz_after_joint_state_broadcaster_spawner,
initial_joint_controller_spawner_stopped,
initial_joint_controller_spawner_started,
gazebo,
gazebo_spawn_robot,
spawn_table, #新增节点
]
4. 仿真启动
ros2 launch ur_simulation_gazebo ur_sim_control.launch.py ur_type:=ur5 launch_rviz:=false