What are Ros2?
Interfaces.
In the previous sections on topics and services, you have used some common interfaces to communicate
between nodes with topics.
You have seen that all nodes publishing are subscribing to a topic must use the same data type with
services.
You have seen that the client must send a message respecting a certain data type, and the server must
respond with another message, also respecting another data type.
So a topic is defined by two things a name so you can recognize it, and an interface based on a message
definition which is the data structure of the information that you send.
A service is also defined by two things a name so you can recognize it, and an interface or service
definition.
The service definition contains one message definition for the request and one message definition for
the response.
So it's a pair of messages.
And well you can see topics and services as the communication layer tool, and the interfaces or the
messages are the actual content that you send.
Let's just make another very simple real life analogy.
When you send a letter, the transport company will carry your letter.
The content of this letter is the analogy of a raster interface here using a message definition.
When you send a letter and you wait for a response, then the first letter contains a request message,
and the letter that you receive back contains a response message.
And the combination of the two message definitions is the service definition.
All right?
As you can see, message and service definitions are quite useful.
Without them, you would have nothing to send over a topic or a service.
So now, technically speaking, how is it possible that you can use those interfaces directly in your
code?
Because for now it just looks like a bunch of simple text.
Well, let's say that you create a message definition.
When you use the build command line, the message will be passed by the build system, and then a source
code will be generated for this message in any Ros2 supported language C plus plus Python.
But it could also be another language.
Your interface is now ready and that's why you can, for example, include the message header in your
C plus plus code or import the library in Python because the build system generated that file.
Now how to write an interface.
Well, you will be able to use primitive types that we will see in a minute and also include other message
definitions.
And they are already a lot of existing interfaces in for example, those package that we are going to
browse right now.
So what kind of data types can you use to create a message or a service definition?
If you go on the Ros2 interfaces page on the Ros documentation.
So you can just basically type Ros to interfaces, you will get a page like that, and you can see all
the primitive data types that you can use.
It's not really complicated.
Among them you can see for example we have boolean.
So you will need to write bool.
You have integer numbers.
For example with int 64 float numbers it's the same float 64 for example.
Then you can use string etc. and you can also create arrays of those types.
So can you have an example here.
So that's what you're going to write in the message definition.
We're going to create one in the next lesson.
And then how to interact with this in the code.
Well you see you have the simple plus and the Python equivalent.
So if you're going to use for example a string then in Python it's just going to be a normal string.
And in Cplusplus it's going to be an STD string.
Okay.
For most of it is really, really simple.
So those are all the primitive types that you can use.
Now where to find the existing packages, for example the ones that we have used already.
Well you can find most of them on GitHub.
For example, here I am on GitHub with the example interfaces.
So you have the URL here.
But you could also just type ros2 and then example interfaces GitHub.
You will easily find this page.
Same thing I don't necessarily give you URLs because they might change stuff.
They might move stuff around.
So a very simple Google search will do.
Now I am in this example interfaces package.
So that's the code inside the package.
You see we have a cmakelists.txt.
We have a package dot XML.
And then we have for example in MSG.
So in message we have here if I go down we have int 64 dot msg.
That's the one we have used already.
You see that the definition we have seen which contains an int 64.
So that's an integer number with the field data okay.
So that's something we've already seen.
So you can browse around.
You can find different messages and services here.
And let's go to the Common Interfaces repository on GitHub okay.
This one contains actually it's not just one package, it's a collection of packages.
Okay, so each folder is a different package.
Some of them are already installed when you install Ros2.
For example, the geometry messages were already installed for us.
And if you want to install one that's not installed yet, it's very easy.
For example, let's say you want these shape messages.
I don't know if it's installed, but if it's not you're going to do a sudo apt install and then ros
dash the distribution dash and then shape dash msgs okay.
If you have an underscore for the package name, you're going to replace it with a dash.
So it's very easy to install.
Now let's check some of them.
So let's go to sensor messages.
This one is quite useful okay.
And this one for example we go to message.
And there are a lot of stuff okay.
And let's go for example to joint state.
So you see lots of comments.
And you see also that it's a bit bigger.
So what do we have in this message?
You see we have an STD message header.
So that's a message from another package okay.
You can include a message inside a message that's completely possible.
So this one if you go to check that definition of that message inside this package is going to contain
a timestamp and some other stuff.
And then in this one you can see for example we have an array of strings for name.
So basically for each joint of a robot and then three arrays with float numbers for the position, velocity
and effort.
So as an example, let's say you have a robotic arm with 5 or 6 joints.
And you want to publish the state for the joints.
So you read data from the encoders, and then you want to create a message to publish the state with
the position, maybe velocity, maybe effort.
Well, the stuff you compute for all of the joints of your robot.
Then do you need to create a new message?
No, you can just use that one.
Okay.
You just use that one.
And then you fill the different fields here and you just publish with this message.
Okay.
So you can check this sensor message package.
It's quite useful whenever you have a sensor.
You see here we can use Joy for joystick.
You have an IMU.
You have image okay.
Laser scan point cloud.
So it contains a lot of useful things for common sensors.
And of course, well after you've installed this package, the messages are going to be built.
And you can just use them in Python or C plus plus.
Now let's go back to the root of this repository and let's go to geometry Messages.
And in geometry messages we also have an MSG folder.
And you see so we have a lot of stuff.
And one that we've used here is twist.
And you can see the twist.
So we have a comment.
And then we have vector three linear and vector three angular.
And what is vector three.
So here we don't see a package name in front because we are using a message from that same package.
So if we go back to the message here and we find vector three.
It's here.
You see that vector three contains three primitive data types, which are each a float number with x,
y, and z.
Okay.
So if we come back to the twist.
So you see we have linear x, y and z and angular x, y and z.
And usually this one as you can see it's used for velocity.
And so the twist message is used with the turtle sim simulation that we've used.
But also if you're going to use for example, robots like Turtlebot or basically any mobile robot,
you will use this twist message.
It's very useful.
Okay.
And you can see here combination of several messages inside one message.
And then to finish let's go back here and let's go to standard.
So STD solves standard services.
You see we have service folder and we have used the set bull previously.
You can find set bull is here.
Of course, we have the three dashes to separate the request from the response.
Okay.
And as you can see, this is exactly what we saw in the terminal when we did Ros2 interface show.
And so we have also primitive data types.
If you want you could also include messages.
So you cannot include a service inside the service.
Okay.
It's very important you just include messages.
So any message you create for a topic you can use it for topics, but you can also include it inside
a service definition.
Okay.
You could have a vector three here as a request.
Okay.
So you can include messages inside services.
But you cannot include services inside services.
And well that's about it.
Now we're going to practice of course and see how to create all that by ourselves with our own packages
inside the workspace.
So what you've seen for now is a bit more about what Ros2 interfaces are, how to create them with message
and service definitions.
And you've also discovered some of the most common existing interfaces.