平台:Ubuntu22.04,ros2:humble
问题:在学习完小鱼的“动手学ros2”建模仿真篇后,我学会了如何运用launch将单个机器人添加Gazebo中和利用rqt添加多个机器人到gazebo。
现在我想利用一个launch将多个机器人模型展示在gazebo中,并且在rviz2中显示多个机器人。目前,在gazebo中可以显示多个机器人,但是rviz2中,只能显示一个机器人模型,且模型来回跳动,如图:
launch文件如下:
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():
name1 = 'fishbot1'
name2 = 'fishbot2'
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}')
gazebo_world_path = os.path.join(pkg_share, 'world/fishbot.world')
# Start Gazebo server
start_gazebo_cmd = ExecuteProcess(
cmd=['gazebo', '--verbose','-s', 'libgazebo_ros_init.so', '-s', 'libgazebo_ros_factory.so',gazebo_world_path],
output='screen')
# Launch the robot1
spawn_entity_cmd1 = Node(
package='gazebo_ros',
executable='spawn_entity.py',
# namespace='robot1',
arguments=[
'-entity', name1,
'-file', urdf_model_path,
'-robot_namespace', name1,
],
output='screen'
)
# Start Robot1 State publisher
start_robot_state_publisher_cmd1 = Node(
package='robot_state_publisher',
executable='robot_state_publisher',
namespace=name1,
arguments=[urdf_model_path]
)
# Launch the robot2
spawn_entity_cmd2 = Node(
package='gazebo_ros',
executable='spawn_entity.py',
# namespace='robot2',
arguments=[
'-entity', name2,
'-file', urdf_model_path,
'-robot_namespace', name2,
'-x', '0.5'
],
output='screen'
)
# Start Robot2 State publisher
start_robot_state_publisher_cmd2 = Node(
package='robot_state_publisher',
executable='robot_state_publisher',
namespace=name2,
arguments=[urdf_model_path]
)
# Launch RViz
start_rviz_cmd = Node(
package='rviz2',
executable='rviz2',
name='rviz2',
output='screen',
# arguments=['-d', default_rviz_config_path]
)
ld.add_action(start_gazebo_cmd)
ld.add_action(spawn_entity_cmd1)
ld.add_action(spawn_entity_cmd2)
ld.add_action(start_robot_state_publisher_cmd1)
ld.add_action(start_robot_state_publisher_cmd2)
ld.add_action(start_rviz_cmd)
return ld