教程
如何在launch中启动gazebo,配置gazebo的参数?
ros_gz_sim
这个功能包包含两个launch启动文件:
gz_server.launch.py:
用于启动gazebo的server。
ros2 launch ros_gz_sim gz_server.launch.py world_sdf_file:=empty.sdf
gz_sim.launch.py:
用于启动gazebo的GUI和server.
ros2 launch ros_gz_sim gz_sim.launch.py gz_args:=empty.sdf
配置过程
1,加载gz_sim的launch文件
pkg_ros_gz_sim = get_package_share_directory('ros_gz_sim')
gazebo = IncludeLaunchDescription(
PythonLaunchDescriptionSource(
os.path.join(pkg_ros_gz_sim, 'launch', 'gz_sim.launch.py')),
launch_arguments={'gz_args': '-r ' + world_file}.items(),
)
IncludeLaunchDescription和PythonLaunchDescriptionSource;
gz sim启动参数配置
gz sim的参数可以通过以下指令查看:
gz sim --help
-r
-r Run simulation on start.
启动后立即运行仿真(不暂停)。
世界文件
2,启动ros_gz_sim的create
#2,执行create启动ros_gz_sim节点
#entiy的初始位置和姿态
entity_x = '0.0'
entity_y = '0.0'
entity_z = '0.3'
#entity_roll='0.0'
#entity_pitch='0.0'
entity_yaw = '0.0'
spawn_mbot_entity=Node(
package="ros_gz_sim",
executable="create",
arguments=["-topic","/robot_description",
"-name","mbot",
"-x",entity_x,
"-y",entity_y,
"-z",entity_z,
"-Y",entity_yaw],
output="screen"
)
为了在Gazebo Sim中为了使用urdf文件生成模型,我们将启动一个世界,并使用world(gazebo)的“
/world/empty/create”,该服务使用EntityFactory消息类型。
3,启动ros_gz_bridge
4,启动robot_state_publisher
#4,启动robot_state_publish
robot_state_publisher_node=Node(
package="robot_state_publisher",
executable="robot_state_publisher",
parameters=[{"robot_description":robot_description_config.toxml(),
"use_sim_time": True}],
output="screen"
)
'use_sim_time': True的作用:
核心功能
-
启用仿真时间:
当设置为True
时,节点会从 ROS 2 的/clock
Topic 获取时间(通常由 Gazebo 等仿真器发布),而非使用系统物理时间。 -
同步仿真环境:
确保所有节点在仿真环境中使用统一的时间基准,避免因系统时钟导致的时序问题。