In this post we are going to show how to launch robots in a ROS2 simulation launch file by using their XACRO file
This is very important, due to the fact that they are generally used to generate a URDF file, and finally spawned. But sometimes we are working on the XACRO files and importing this file directly save us some steps every time we want to re-spawn the robot in a given world. Let’s start!
1. Get the environment
We need to install XACRO library in order to import it inside the python launch file
In this post, we are using TheConstruct app and you can start using for free just by creating your account.
Copy the rosject we are working with by clicking here and you are ready to go!
If you prefer to work on your own ROS environment, you can get the code here
Basically we are going to spawn the robot define in this file
Whose content is not important for the sake of this demo, see it as a regular robot description.
2. Installing the necessary packages
In order to work with xacro commands, you will need the library installed. You achieve that by running the command below:
sudo apt install ros-foxy-xacro
And we are good to go
3. Updating the launch file
Now we go for the launch file. In our example package, it is located at
There you can two ways of launching. The first one, which is commented, relay on a previously generated URDF file
urdf = os.path.join(get_package_share_directory('box_car_description'), 'robot/', 'box_bot.urdf')
assert os.path.exists(urdf), "Thebox_bot.urdf doesnt exist in "+str(urdf)
But in this case, we want to use the original xacro file, so we can keep the launch file using the development version of the robot description:
xacro_file = os.path.join(get_package_share_directory('box_car_description'), 'robot/', 'box_bot.xacro')
assert os.path.exists(xacro_file), "The box_bot.xacro doesnt exist in "+str(xacro_file)
4. Understanding the spawn script
The executable python file called by the launch one is located right here
And it’s important to understand that we need this way to handle the arguments passed.
In the case of the XACRO file, it should be taken by using the index 1 of sys.argv, in order to get the content properly.
At any moment, you must be able to launch the given simulation and have a similar result as below:
Great, that’s the result you should have. You can keep developing your robot and re-spawning it without having to re-generate the URDF file again and again.
If this content helped you, don’t forget to share and leave a comment to let us know you like this kind of post!