In this post we are going to show how to launch robots in a Gazebo simulation in ROS2 by using XACRO files.
This is very important, due to the fact that XACRO files are generally used to generate a URDF file, and finally spawned. But sometimes importing the XACRO files directly save us some steps every time we want to re-spawn the robot in a given world. Let’s start!
1. Setup the environment
In this post, we are using TheConstruct app and you can start using it for free and without installing anything 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 need to
- install XACRO library in order to import it inside the python launch file. You don’t need to do this if you are using The Construct app.
sudo apt install ros-foxy-xacro
- download and compile the source code. Get the code here. If you are using The Construct app, this has already been done for you when you copied the rosject.
Basically, we are going to spawn the robot defined in this file ros2_ws/src/box_car/box_car_description/robot/box_bot.xacro
2. Understanding the use of the XACRO file in the Simulation launch file
Now we go for the launch file. In our example package, it is located at ros2_ws/src/box_car/box_car_description/launch/spawn_robot_launch_v3.launch.py
There we have two ways of launching. The first one, which is commented, relies 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 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)
3. Understanding the spawn script that uses the converted XACRO file
The executable python file called by the launch one is located right hereros2_ws/src/box_car/box_car_description/launch/spawn_box_bot.py
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:
4. Watch the full Video that explains How to use XACRO files with Gazebo in ROS2
Please watch the video of this post here, to better understand the launch file and the spawn script.
5. Conclusion
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!
0 Comments