Let's now see how to use the custom interface you have just created inside your own code.
First with Python.
So we have created a hardware status interface.
I'm just going to create a node that contains a publisher, and we're going to call it hardware status
publisher.
Okay very simple node.
So let's go in our workspace in the Python package.
So I'm going to use the same Python package here and create a new node.
And let's create hardware status publisher dot p y.
And let's make it executable.
Now I'm going to go back to VS code which was already open here.
Let's go to the Python package and let's do what we've done previously.
Let's take the template and let's add it here.
Hardware status publisher dot p y.
And let's name this hardware status Publisher node here and there.
Let's remove those comments and let's also rename the node hardware status publisher.
Okay.
Let's save.
And then.
So the first thing we do when we create a publisher is also to import the interface.
So the interface we've created here you see it's my robot interfaces MSG hardware status.
So in the code we are going to do from my robot interfaces dot msg import hardware status okay.
And I save.
And as you can see well you might have this.
Probably you will have this if you just had VSCode already open like me is that it's not recognized.
So the package is found somehow, but the interface is not recognized.
And why is that?
Well, here you see, I have VS code that was already opened previously in a terminal that was sourced.
But since then we have built the package in that terminal and we have created a new interface.
But vs code in the environment it's working.
Doesn't know about this interface yet.
And so how to solve that issue.
Well it's very simple.
After you create the interface better to close VS code and then to go in a terminal where you've already
sourced.
For example, here you just open a new one or you go to that one in a terminal where you have sourced
your workspace with all the new interfaces, and then you go back here and you open VS code and this
time.
So let's give it a few seconds.
You see we have Ros extension initializing here.
And this time you see now it's green.
It is found.
And so we can use it with the autocompletion and all that.
Okay.
So make sure that when you build an interface and if you want to use it, then you close VSCode and
you just open a new terminal.
You open VSCode again, or you source an existing terminal and you see you open VSCode in a sourced
environment.
Very important.
Great.
So we have imported we have successfully imported the interface that we have created.
Now, as you can see, we are using my robot interfaces inside my Python package.
So we are using another package, which means I'm going to go to my package dot XML for the Python package
here.
And I'm going to add a new depend tag.
Depend my robot interfaces.
All right.
So then now we have three dependencies and that's it.
Okay.
That's it.
You just import it add the dependency in the package dot XML.
And that's basically it.
It's just like any other interface we have used previously.
Okay.
There is nothing more special about the interfaces that you create.
And then well let's see how to use it.
So I'm going to create a very simple Publisher self, let's call it hardware.
So h w status pub is equal to self create publisher with hardware status.
That's the interface here.
And then let's call it hardware status.
And let's give it a queue size okay.
And let's create a timer self timer self create timer with let's say one second.
And then let's create a callback actually not a call.
Let's call it publish hardware status.
Okay.
That's going to be the function we use to create a message and publish on the topic.
And so to create a message we are simply going to use the hardware status.
So hardware status we are going to use the class and create an object.
That's it.
And then we can do MSG, dot and you see we have all of the fields, for example MSG dot temperature
and let's say 43.7 and then MSG dot amateurs ready and let's say true.
So you see we use the data type for each field.
And then MSG dot debug message is equal to let's say nothing special.
All right.
So you see we just fill the different fields of the message that we have created.
And then after we create the message we publish it with the hardware status publisher Dot publish the
message.
And then I can put the callback here self dot publish hardware status.
And let's finish with a log.
Get logger.
Info with How do it status publisher has been studied.
Okay, so I went quite fast here.
But this is a structure that we have done already several times in this course.
It's always the same.
We create a publisher, a timer, we create a message, we publish the message.
The only difference here is that we use a custom interface, but the code is basically the same.
So let's save and let's test it.
So I'm going to create a new executable here in setup.py.
Let's name it hardware status publisher is equal to.
So the file name is hardware.
Actually we need to put the package name Mypy pkg dot hardware status publisher.
And still the main function.
Okay.
You see I use a slightly different name for the executable and the file name this time, which is completely
fine.
And let's go there again And let's go back here and let's build.
Click on build with packages select and the Python package.
And then let's choose the workspace.
Let's just do it here.
We can just do the source install setup bash from the workspace directly.
And then let's do Ros to run my Pi PG with hardware status publisher okay it is publishing.
And here for example, let's do a Ros two topic list.
We see hardware status and then Ros two topic equal hardware status.
And as you can see we have an error message.
So that's why I kept those terminals like this.
Why do we have an error message.
It says the message type is invalid simply because well if you remember we have built the message here,
and since then we haven't sourced that environment.
Okay, so this one is sourced.
This one is sourced.
So the same command here should run in this terminal.
Okay.
You see we receive the messages but not in that one because we haven't sourced the environment.
Okay.
So this is very important.
And in this case, as you can see when we build a new interface and a new node, what I recommend really
is just let's close everything.
Let's reopen everything.
So now everything is correctly sourced to run my Python pkg with hardware status publisher.
And here for example, rose to topic equal with a hardware status.
And this time is working.
And we're just publishing the same thing every second.
All right.
So now you know how to use a custom message inside your own Python nodes.