With this lesson, we conclude the creation of the robots digital twin, which will be a fundamental
component throughout the course to continue implementing and testing the remaining functionalities.
In fact, you can also continue using it to test your own ideas and receive a fast feedback that will
help you to accelerate the development.
So back in Visual Studio Code, let's start by creating a new launch file.
So still here within the Arduino board description package.
Within the launch folder, let's create a new launch file called gazebo dot launch dot Pi.
Perfect.
And now as usual.
So as we did also for the display.launch.py file, let's import from the launch package the launch description
class.
And actually the launch file will just contain one function that is the generate It launch description
function, and this function will actually just return an object of type launch description and the
constructor of this class.
So the constructor of the launch description class will just take a list as input.
And now we are going to fill this list here with all the actions.
So with all the nodes that we want to execute with this launch file for the simulation of our robot
in gazebo, we need to start the robot Estate publisher node, which, as we saw in one of the previous
lessons, reads the Urdf model of the robot and publish the Urdf model into a topic.
So into a Ros two topic that then we can use to visualize the robot interface, or also to simulate
it in gazebo.
So instead of rewriting all the instructions for the robot estate publisher node, we can just copy
all of them from the display dot pi.
So here we can copy the model argument.
We can also copy the robot description.
And also here the robot state publisher node.
So let's copy the three of them and let's paste it here.
So in the generate launch description perfect.
We need to import all the missing classes so we can copy also the include statements.
Perfect.
And also as you can see here we are missing to declare this variable.
So the Arduino bot description directory variable.
And let's copy also this variable here.
And let's paste it in our gazebo simulation.
So perfect in our gazebo launch file in this case.
So here when we launch the robot state publisher node, apart from setting the parameter robot description
we can also set an additional parameter.
So here after the robot description let's set the parameter called Use same time.
And now let's set this parameter to true.
And this is due to the fact that now we are starting the robot estate publisher node for the gazebo
simulation.
And so we want to set the user sim time argument to true.
And this is a general parameter.
So the user sim time that is available in any ros2 node.
So basically when you create any ros2 node it will have automatically this parameter here.
So the user sim time that will inform the node whether it is running on a simulation of the robot or
whether the node is running on the real robot.
Perfect.
So after starting the Robot Estate Publisher node, we can also create and set a new environmental variable
in Linux.
So in ubuntu where we are running that will be used by gazebo in order to load and to simulate the Urdf
model of our robot properly.
So to set an environmental variable from a launch file, we can import from the launch actions package.
So from this package here we can import the set environmental variable class.
And now we can create a new instance of this class.
For example let's do it here.
And this is called gazebo.
So let's call this variable gazebo resource path.
And so this is an instance of the set environmental variable.
And now here within the constructor of this class we need to indicate the name of the environmental
variable that we want to set.
And this is the gazebo same resource path.
And basically with this environmental variable we want to communicate to the gazebo simulation where
in the file system it should look for the Urdf model of our robot, and also for all the meshes that
will be used for the simulation.
So actually simply we can say that the value of this variable.
So this is a list.
And for the moment we just want to insert one single value that is the Arduino board description directory.
And so actually we want to tell to gazebo that it has to look within this package here.
So within the Arduino board description package in order to find the meshes of our robot and also the
Urdf model of our robot.
But instead of passing it like this, so instead of passing it as a plain directory, we need to convert
it to a Python path.
So let's import from the path library the path class.
And so we can convert this one here into a Python path.
Perfect.
And to this Python path we can access to its parent.
Perfect.
And let's use the function resolve in order to access to the path of the parent of this directory here.
And finally, let's simply convert all of these into the format that is just a string.
So we are converting the parent of this directory here into a string.
And we are setting this one as an environmental variable in our ubuntu system that then gazebo will
read.
So this one here will be read by gazebo.
And it will be used in order to know the directory of the meshes and the urdf of our robot.
So now we can finally also start gazebo.
And to do so we are going to start.
So we're going to execute another launch file from this launch file.
So basically we're going to nest a launch file in order to do so.
Still from the launch actions package.
So from this one here we need to import another class that is the include launch description.
And so we can use this class here to include a launch file within another launch file.
So let's create here a new variable of this class that is called gazebo.
So with this variable here we're going to start the gazebo simulation.
And this is an instance of the include launch description.
Now here we need to specify the type of the file that we want to launch.
And also we have to specify the directory of the launch file that we want to start.
The other launch file that we want to start is also a Python launch file.
So let's import it.
So from the launch and the launch description sources.
So this one with the final s let's import the Python launch description source.
And now we can use this class here within the include launch description.
In order to declare that the other launch file that we want to execute is also a Python launch file.
Perfect.
And now we can finally insert the directory of these other Python launch file that we want to start.
So again we can use the OS path join.
And the launch file that we want to start is in the Ros gazebo simulation package.
So one of the packages that we have installed at the beginning of the course.
So let's access it with the Get package Share directory function.
And we want to access to the directory of the Ros gazebo simulation package.
And from this package from the launch folder we want to start the launch file that is called.
So let's go here.
So after the Os.path.join we want to start the launch file gazebo sim dot launch dot pi.
And actually here we are missing to insert this one into a vector.
perfect.
So the input of the Python launch description sources class.
So this one here has to be a vector.
And so this one indicates the directory of the launch folder that is within this package here.
And so from the launch folder of this package of the gazebo we want to execute the gazebo SIM.
Don't launch don't pi.
Now we can also configure this node.
So we can configure this launch file with some launch arguments.
And also in this case we need to set a list of launch arguments starting with the argument that is called
gazebo args.
So now we can set all the arguments.
So all the gazebo arguments within a new vector.
So within a new array.
And now here we can insert another string with all the arguments that we want to set to get.
And so we can use a space.
And then we can add the flag minus v four.
And this one indicates the verbosity level.
And so by setting this 1 to 4, we are setting that we want to print the output of this node.
So the output of gazebo in the terminal in order to see its output and eventually inspect any error.
Then let's also use the flag minus r in order to inform that we want to start the simulation immediately.
So as soon as we start the gazebo launch file, we want to start the simulation.
And also we need to specify the name of the word.
So the name of the simulated word in which we want to spawn.
So in which we want to simulate our robot.
And for the moment we are just fine by simulating it within an empty word.
So within a word that has no elements, it has just a ground.
So it has just the ground floor.
And also it has a light source that we can use in order to visualize our robot with this.
So with this instruction here we will start an empty gazebo simulation.
So we will start an empty gazebo environment without any simulated object inside this world.
So in fact, here, as you can see, we are just starting the empty gazebo world.
So a world which has no element in it.
And now, within this empty gazebo world, we want to start.
So we want to simulate our robot using another node that is still available in this package here.
So in the Ros gazebo SIM package.
So we can create a new instance of the node class and let's call it gazebo spawn entity.
And so this is an instance of the node class.
And we want to use this instance in order to start from the package.
That is again the Ros gazebo SIM package.
So from this package here we want to start the executable that is called the create executable.
And also we want to configure this executable.
And so this one.
So this executable here is the one that will spawn our robot model into the gazebo simulation.
So let's configure it.
And let's first set its output to be screen.
So like this in order to visualize.
So here there is a typo it's screen.
In order to visualize the log messages for this node into the terminal.
And then we need to configure it with some arguments.
So this take a list of arguments.
And here so we can start by configuring the flag topic which we can use in order to indicate the name
of the topic where the Urdf model of our robot is being published.
And actually this topic here is the robot description topic.
And in fact so this note here the robot state publisher node which opens so which reads the Urdf model
of our robot will publish it.
So in the topic that is called the robot description topic.
And so now this node here.
So the create node can read the Urdf model of the robot directly from this topic here.
So directly from the robot description topic.
Also let's configure another argument.
So let's use a comma and let's configure the argument that is called name.
And here let's simply set the name of our robot which we have called just Arduino bot.
Perfect.
And so this node here then we will spawn this robot here.
So this description.
So this urdf within this gazebo and the environment.
Perfect.
So now we need to start just one last node that is the gazebo Ros2 bridge.
So this is another instance of the node class and now this node.
So we want to start from the package that is called Ros gazebo bridge.
So from this package we want to start a node that is called.
So the executable is called parameter bridge.
And this node here.
So we can use the parameter bridge node in order to remap.
Namely in order to connect the gazebo messages that are transmitted in the gazebo simulation using the
gazebo transport protocol into Ros two messages.
So we want to create this remap.
We want to create this connection so that other Ros two nodes and other Ros two applications can use.
So can access all the messages that are generated in gazebo.
So in order to do so, in order to define how to do this remap we need to set some arguments.
So again this is a list.
And in this list of arguments we need to specify.
So here let's start by specifying the name of the topic.
So the name of the Ross two topic in which we want to publish some messages.
So let's say that we want to publish a new message in the topic called clock.
And the type of the message that we want to publish is a Ross graph messages.
And from the messages we want to publish a message of type clock.
So here simply we are saying that in this topic here.
So in this Ross two topic called clock, we are going to publish a message of this type.
And actually now we want to specify.
So which is the gazebo information.
So which is the gazebo message that we want to remap into this Ross two message.
So we can use a single parentheses.
So a single squared parentheses.
And here we can specify that the gazebo message that we want to remap is the gazebo Go messages and
the message of type clock.
Perfect.
So with this note here we are converting a gazebo message of this type here that is created and generated
in gazebo simulation.
And we are converting this one into a message of this type here.
So Ros graph messages clock that will be published in this Ros two topic here.
Now with this we have declared all the instructions that we need in order to simulate our modeling gazebo.
So all of these are all the declaration of the instructions that we need now in order to actually start
using them.
We need to declare them here.
So we need to list them in the launch description class constructor.
And so here let's start with the declaration of the model argument.
Then we also have here this one here this environmental variable that is the gazebo resource path.
Then after that we have also the robot estate publisher node.
So the one that reads the Urdf model of our robot and publishes it in the robot description topic.
Then also let's start the gazebo simulation.
So the empty gazebo simulation.
And then within this empty gazebo simulation, let's also spawn our robot using the gazebo spawn empty
node.
And finally, let's also start this node here.
So the gazebo Ros two bridge, which is that will remap this topic here.
So this message from the gazebo simulation to Ros2 a perfect.
So with this one we can also save this launch file here.
And we have completed the creation of this launch file that starts in the order the robot state publisher,
then the empty gazebo simulation.
Then it spawns.
So this one here will spawn our robot in the empty gazebo simulation.
And then we will expose some gazebo messages into Ros two topics.
Now, as we have declared so as we have used some nodes.
So here we can see the create node and also the parameter bridge node from these two packages.
So from the Ros gazebo SIM package and from the Ros gazebo bridge package, we also need to declare
them here in the package dot XML.
So here we need to declare a new execution dependency from the Ros gazebo SIM package.
And then also another execution dependency from the Ros gazebo bridge package.
Perfect.
And so also with this we have completed the package dot XML.
So we have just introduced these two new dependencies.
So we can save also this file.
And finally we can see our launch file works.
And now it starts the gazebo simulation and spawns our robot in it.
So to do so, let's open a new terminal.
Here, let's go to the workspace and let's build it in order to apply the changes.
Here we go.
And then in a new window of the terminal let's source the workspace and then let's launch.
So with Ros2 launch we can start from the Arduino board description package.
So we can start the node that is called gazebo dot launch dot pi.
So we can start this launch file here that we have just created.
So let's press enter.
And as soon as we do so we can see that the simulation of our robot starts in gazebo.
So we can see that automatically the gazebo window is started.
We can also see that our robot is automatically spawned in the gazebo simulation.
And also we can see that it is bending on itself because the control system is not yet started.
And so here we can see that now our robot is perfectly visible in the gazebo simulation.
So we can start using this tool.
So this gazebo simulation in order to develop new applications.
And so also in order to explore new projects and new ideas, and also to get an immediate feedback on
the robot's behavior.
Now, if in your case, as you can see here, you are having a quite small real time factor as I am,
you can just change the physics of the gazebo world so you can click anywhere in the gazebo world,
and then you can go to physics.
So here and then you can increase the step size.
For example from instead of using 0.001 we can just use 0.01.
So let's press enter.
And so this will increase the step size.
And we can see that as we do so we are getting a real time factor that is much better.
And so this will create a simulation that in general will be lighter and also faster to execute.