0
$\begingroup$

I am trying to spawn a xacro file on gazebo harmonic (ros2 jazzy).

I have found this guide on how to spawn urdf and works fine: https://gazebosim.org/docs/harmonic/spawn_urdf/

It also says to follow a ros2 guide in order to spawn a xacro file but I can not make it work. (If you have a xacro representation of a robot model, you can turn the xacro file into a URDF file using the xacro package: see this tutorial for more information.)

On that tutorial is has instruction how to make robot state publisher publish xacro file and works fine, i tried it out (works with rviz).

Does anybode have any ideas on how to spawn a xacro robot?

Is there a package for gazebo harmonic that can spawn a robot using the robot_state_publiser topic like it has for gazebo classic?

Thank you!

$\endgroup$

2 Answers 2

1
$\begingroup$

I have something like the following working in humble with ignition(gazebo fortress). This was derived from the official documentation found here https://gazebosim.org/docs/fortress/migrating_gazebo_classic_ros2_packages/

import os from ament_index_python.packages import get_package_share_directory from launch import LaunchDescription from launch.substitutions import LaunchConfiguration, Command from launch.actions import DeclareLaunchArgument from launch_ros.actions import Node import launch_ros.descriptions def generate_launch_description(): # Set ignition resource path ign_resource_path = SetEnvironmentVariable( name="IGN_GAZEBO_RESOURCE_PATH", value=[ os.path.join(package_name, "worlds"), ":" + str(Path(desc_package_name).parent.resolve()), ], ) xacro_file = os. path.join("desc_pkg", "urdf", "robot.urdf.xacro") robot_description_config = Command( ["xacro ", xacro_file, " sim_mode:=", use_sim_time] ) # Create a robot_state_publisher node params = { "robot_description": launch_ros.descriptions.ParameterValue( robot_description_config, value_type=str ), "use_sim_time": use_sim_time, } robot_state_publisher_node = Node( package="robot_state_publisher", executable="robot_state_publisher", output="screen", parameters=[params], ) world = os.path.join( get_package_share_directory(package_name), "worlds", "empty_world.sdf" ) gz_sim = IncludeLaunchDescription( PythonLaunchDescriptionSource( [ os.path.join( get_package_share_directory("ros_gz_sim"), "launch", "gz_sim.launch.py", ) ] ), launch_arguments={"gz_args": [" -r -v 4 ", world]}.items(), ) # Spawn the robot in Gazebo spawn_entity = Node( package="ros_gz_sim", executable="create", arguments=[ "-name", "groundhog", "-topic", "/robot_description", "-x", "0", "-y", "0", "-z", "1.4", ], output="screen", ) # Launch! return LaunchDescription( [ DeclareLaunchArgument( "use_sim_time", default_value="true", description="Use sim time if true" ), ign_resource_path robot_state_publisher_node, gz_sim, spawn_entity, ] ) 
$\endgroup$
1
  • $\begingroup$ Looks promising. So this is used to spawn the robot using robot_description like in the gazebo classic right? I hope it will work for ros2 jazzy too. I will try it out in the next day and give everyone an update. Thank you! $\endgroup$ Commented Oct 7, 2024 at 8:47
1
$\begingroup$

quick update: Here is the code that works for me on ROS2 Jazzy and Gazebo Harmonic. It runs robot_state_publisher to publish my XACRO file and then spawns the robot in Gazebo based on the robot_description topic that the robot_state_publisher creates.

import os from ament_index_python.packages import get_package_share_directory from ament_index_python.packages import get_package_share_path from launch import LaunchDescription from launch.substitutions import LaunchConfiguration, Command from launch.actions import DeclareLaunchArgument from launch_ros.actions import Node import launch_ros.descriptions from launch_ros.parameter_descriptions import ParameterValue from launch import LaunchDescription from launch.actions import IncludeLaunchDescription from launch.launch_description_sources import PythonLaunchDescriptionSource def generate_launch_description(): # Check if we're told to use sim time use_sim_time = LaunchConfiguration('use_sim_time') # Get the urdf/xacro file path path_to_urdf = get_package_share_path('simulation_pkg')/'description'/'robot_urdf.xacro' # Create a robot_state_publisher node node_robot_state_publisher = Node( package='robot_state_publisher', executable='robot_state_publisher', name='robot_state_publisher', output='screen', parameters=[{ 'robot_description': ParameterValue(Command(['xacro ', str(path_to_urdf)]), value_type=str) }] ) # Use your custom Gazebo world (optional). Replace 'empty.sdf' with the word "world" (no "" needed) # world = os.path.join( # get_package_share_directory(package_name), "worlds", "empty_world.sdf" # ) gz_sim = IncludeLaunchDescription( PythonLaunchDescriptionSource( [ os.path.join( get_package_share_directory("ros_gz_sim"), "launch", "gz_sim.launch.py", ) ] ), launch_arguments={"gz_args": [" -r -v 4 ", 'empty.sdf']}.items(), ) # Spawn the robot in Gazebo spawn_entity = Node( package="ros_gz_sim", executable="create", arguments=[ "-name", "robot1", "-topic", "/robot_description", "-x", "0", "-y", "0", "-z", "1.4", ], output="screen", ) # Launch! return LaunchDescription([ DeclareLaunchArgument( 'use_sim_time', default_value='false', description='Use sim time if true'), node_robot_state_publisher, gz_sim, spawn_entity ]) 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.