In this lesson, you will see how to create your first custom Ros2 interface and we will create a message
here.
But first of all, where will you write your interfaces?
Where will you write the message definition?
If we go to our rust workspace in the source folder, we already have two packages.
So in which package?
Well, basically you could technically add message definitions in any package you want.
However, it will be much easier for you to create all the custom interfaces that you have in a single
package dedicated only to custom interfaces that will prevent you from having a dependency mess in the
future, and when you need to import a custom interface in another package, for example, from the
Python package here, you just need to add a dependency to that new interface package.
And that's it.
So let's create a package.
And that's something we will need to do only once okay.
Because once we have this package, any new interface we add is going to be in that same package.
So we go to our source folder okay.
Where we have already our two.
Existing packages.
And we are going to do Ros2 pkg create and I'm going to name it.
My robot interfaces.
So for the name I recommend that you start with the name of your application.
So if your robot here I'm just using my robot.
If your robot is named ABC, you're going to put ABC.
Or if you have an application name, you just put the application name and then underscore interfaces
okay.
We're going to use interfaces as a suffix here to make it clear that this is an interface package.
And then well then that's it.
You could actually put a build type with amount CMake.
But actually if you don't provide it, it's just going to be a CMake by default.
And we don't really need it anyway.
And we're not going to provide any dependencies.
So what I do to create an interface package, I just do Ros to create and the name of the package.
Let's press enter.
And you see we have some logs for the files that were created.
A warning for the license that we can just ignore.
And now we have our new package here.
And if I go inside my robot interfaces, you see this is a C plus plus package.
So we will need the package dot XML file.
As for any package in Ros2, we will also need Cmakelists.txt.
That's where we're going to provide rules to build our interfaces.
But then we don't need src and we don't need include.
So what I'm going to do is just I'm going to clean that.
I'm going to remove the include and I'm going to remove the SRC.
Okay.
So now we just have those two files and we will need to add a few lines inside those files here so we
can build our interfaces.
So let's open.
I'm going to open here this workspace with Visual Studio Code.
and let's go to my robot interfaces.
For example in package dot XML.
So you see here build tool depend its element CMake.
That's what we want.
And we have the build type here as well.
Now what do we add here.
Well there are three lines you need to add in package dot XML.
And in order not to make mistakes, it's actually better to just copy and paste them from an existing
configuration.
So we can go on.
Let's go on the internet and you can just type custom Ros2 interfaces.
We're going to find something and let's go for example to that website.
I might link it to this lesson.
And this website is actually pretty nice for Ros2 tutorials.
Robotics back end.
And I'm absolutely not saying that just because it's my own website.
Okay, so in this tutorial, well, we don't really want to read the tutorial.
We just want to go down here.
You can find the same information under Also ros2 official documentation.
You will find those same lines here.
But you see, when we set up a package for Ros2 custom interfaces, we have three lines we need to add.
Okay.
Build tool depend.
I'm going to zoom in a bit.
Build tool depend with Ros ideal default generators and then execute depend with Ros.
Ideal default runtime and member of group Ros.
Ideal interface packages.
So that's why I'm not just going to write them in order not to make mistakes, I'm just going to copy
and paste them.
Okay.
After the build tool here after build tool depend I just add those three lines.
Okay.
There is nothing really to understand Ros ideal is basically the functionality that's going to create
and manage interfaces.
So you just need to add those lines.
The good thing is after you do this once that's basically it for your whole application.
All right.
So that's it for package dot XML.
We can close it and then we can open Cmakelists.txt and we will first clean a bit.
I don't need this build testing stuff and just remove those comments here.
Okay, so we have the minimum thing for the list.txt and I'm going to go back to that web page.
You need to add this.
Okay Findpackage with Ross ideal default generators.
Then we are going to add Ross ideal generate interfaces.
That's the command to generate interfaces where we're going to put the path to all of the interfaces
we create.
And then you have amend export dependencies with Ross ideal default runtime.
So again it's not really important to understand anything here.
Just have to add this to your configuration.
So let's go here after Findpackage here and before element package.
Let's add a line.
Let's add a space here.
Okay.
And you see your custom interfaces will be here.
One per line and no comma for separating lines, as you can see.
So I'm just going to remove everything now and let's save that.
And now the package is configured.
Okay.
So we have three lines in the package dot XML.
And then we have this fine package this function here and this element export dependencies here for
the Cmakelists.txt.
Now that we have set up the package we can create our first interface.
So how to do that.
Well I'm going to go back here in my robot interfaces package.
And to create a message we are going to create first an MSG folder.
Okay.
And that's where we are going to put all our messages.
I go inside the MSG and let's create a new interface.
Let's say we want to publish the hardware status of our robot.
So that's going to contain a few information about the robot.
For example, I don't know the name, the version number, the temperature.
All kinds of stuff.
So let's create an interface.
Touch hardware status dot msg.
So what are the rules to create names for interfaces?
Well you are going to start with an uppercase and you're going to use upper camel case okay.
So no dashes no underscore is very important.
And you're going to separate each word by an uppercase.
That is going to have an importance for example for the C plus plus header files as we will see later.
So you start with an uppercase.
Each new word has an uppercase.
Also don't write message okay.
We could say hardware status message or something.
Don't write this.
It's going to be redundant.
So just the name of the interface.
And then dot msg for a message definition.
Now I'm going to go back here and let's edit that file so we can use primitive data types.
We can also use existing messages from that package or from other packages.
Let's keep things simple here.
And let's just use primitive types.
Let's say we have a float 64.
That's going to be the temperature okay.
I'm just going to use a few examples with some types.
And then let's say bool are mortals ready.
Okay.
And let's add a string debug message okay.
So you can see I write one field on each line okay.
There is no other separation, no comma, no punctuation.
The first thing I do is I put the type.
So here those are primitive types.
And then the name.
And to separate words I use underscore okay.
It's very important here to use underscores and nothing else.
And that's it.
So it's not really complicated.
Now I'm going to save.
Just make sure that you use exactly the the syntax or the good syntax for primitive types.
I'm going to save this file.
Make sure you save everything.
And then well once I have created a new interface, all I need to do is to go back to my cmakelists.txt.
And inside this rose ideal generate interfaces.
I'm going to add a new line with double quotes, and I'm going to put the path to the interface.
So from here we need to go to the msg folder slash hardware status dot m s g.
And that's it okay.
So you see after you've set up the package, adding a new interface is just about creating a file in
this folder and then adding the path to the file here in that function.
Then you save and then let's go back to the terminal.
Let's stay in that one and let's go back to our Ros2 workspace folder here, and let's do a call on
build.
I'm going to also use packages select to only build this interface.
So my robot interfaces let's build it might take a bit of time the first time that you build some interfaces.
Okay so it took 11 seconds.
It's not that bad.
And now the interface has been built okay.
If you go to the install folder inside the My Robot interfaces, you could find and check the C plus
plus and the Python files.
Okay.
You can try to do that on your own if you want.
But once again it's not really that important because they exist and we don't need to check them.
We can just use them.
And let's see in this terminal the interface we have just created.
So because we have just built here in that terminal, if we're using another one that we haven't sourced
after that we need to source it now.
So let's source the bash RC so we can source our workspace.
And then I can do Ros2 interface show.
And let's start with the package name my robot and let's press tab.
You see I have only one.
So my robot interfaces slash msg slash hardware status I press enter.
And this is the message definition that I have okay.
So now you understand this.
We have done several times Ros2 interface show.
But now you see the whole process to create an interface to build it and then to see it on the terminal.
Great.
And make sure that you can see the interface with this command.
Okay.
If you cannot see the interface, then you will not be able to use the message in your code in the next
lesson.
So make sure that you can do that if needed.
You remove the package, you clean everything up and you start everything again from scratch because
there are quite a few steps here.
So make sure that this is working before you go to the following.