WEBVTT

00:00.050 --> 00:05.510
Let's now see how to use the custom interface you have just created inside your own code.

00:05.660 --> 00:06.800
First with Python.

00:06.800 --> 00:09.530
So we have created a hardware status interface.

00:09.530 --> 00:14.600
I'm just going to create a node that contains a publisher, and we're going to call it hardware status

00:14.600 --> 00:15.230
publisher.

00:15.260 --> 00:16.970
Okay very simple node.

00:16.970 --> 00:22.040
So let's go in our workspace in the Python package.

00:22.040 --> 00:27.140
So I'm going to use the same Python package here and create a new node.

00:27.380 --> 00:34.580
And let's create hardware status publisher dot p y.

00:35.030 --> 00:38.510
And let's make it executable.

00:39.680 --> 00:44.870
Now I'm going to go back to VS code which was already open here.

00:45.170 --> 00:51.290
Let's go to the Python package and let's do what we've done previously.

00:51.290 --> 00:55.430
Let's take the template and let's add it here.

00:55.460 --> 00:58.850
Hardware status publisher dot p y.

00:59.150 --> 01:08.150
And let's name this hardware status Publisher node here and there.

01:08.180 --> 01:19.460
Let's remove those comments and let's also rename the node hardware status publisher.

01:19.490 --> 01:20.330
Okay.

01:20.750 --> 01:21.890
Let's save.

01:21.890 --> 01:23.000
And then.

01:23.120 --> 01:29.240
So the first thing we do when we create a publisher is also to import the interface.

01:29.240 --> 01:38.600
So the interface we've created here you see it's my robot interfaces MSG hardware status.

01:38.600 --> 01:53.840
So in the code we are going to do from my robot interfaces dot msg import hardware status okay.

01:53.870 --> 01:54.710
And I save.

01:54.710 --> 01:57.140
And as you can see well you might have this.

01:57.140 --> 02:03.320
Probably you will have this if you just had VSCode already open like me is that it's not recognized.

02:03.320 --> 02:07.190
So the package is found somehow, but the interface is not recognized.

02:07.220 --> 02:08.300
And why is that?

02:08.330 --> 02:15.980
Well, here you see, I have VS code that was already opened previously in a terminal that was sourced.

02:16.280 --> 02:23.510
But since then we have built the package in that terminal and we have created a new interface.

02:23.810 --> 02:26.780
But vs code in the environment it's working.

02:26.810 --> 02:29.660
Doesn't know about this interface yet.

02:29.690 --> 02:31.430
And so how to solve that issue.

02:31.460 --> 02:32.690
Well it's very simple.

02:32.690 --> 02:40.040
After you create the interface better to close VS code and then to go in a terminal where you've already

02:40.040 --> 02:40.430
sourced.

02:40.430 --> 02:46.130
For example, here you just open a new one or you go to that one in a terminal where you have sourced

02:46.160 --> 02:55.190
your workspace with all the new interfaces, and then you go back here and you open VS code and this

02:55.190 --> 02:55.760
time.

02:55.760 --> 02:57.680
So let's give it a few seconds.

02:58.940 --> 03:01.400
You see we have Ros extension initializing here.

03:01.400 --> 03:03.230
And this time you see now it's green.

03:03.230 --> 03:04.610
It is found.

03:04.640 --> 03:07.370
And so we can use it with the autocompletion and all that.

03:07.400 --> 03:07.670
Okay.

03:07.700 --> 03:13.780
So make sure that when you build an interface and if you want to use it, then you close VSCode and

03:13.780 --> 03:15.790
you just open a new terminal.

03:15.790 --> 03:21.940
You open VSCode again, or you source an existing terminal and you see you open VSCode in a sourced

03:21.940 --> 03:22.690
environment.

03:22.720 --> 03:23.620
Very important.

03:23.650 --> 03:24.160
Great.

03:24.160 --> 03:28.660
So we have imported we have successfully imported the interface that we have created.

03:28.690 --> 03:34.900
Now, as you can see, we are using my robot interfaces inside my Python package.

03:35.140 --> 03:42.460
So we are using another package, which means I'm going to go to my package dot XML for the Python package

03:42.460 --> 03:42.850
here.

03:42.850 --> 03:45.130
And I'm going to add a new depend tag.

03:45.190 --> 03:50.410
Depend my robot interfaces.

03:52.000 --> 03:52.480
All right.

03:52.480 --> 03:56.080
So then now we have three dependencies and that's it.

03:56.110 --> 03:56.710
Okay.

03:56.710 --> 03:57.340
That's it.

03:57.370 --> 04:01.150
You just import it add the dependency in the package dot XML.

04:01.150 --> 04:02.320
And that's basically it.

04:02.350 --> 04:05.110
It's just like any other interface we have used previously.

04:05.140 --> 04:05.320
Okay.

04:05.350 --> 04:08.680
There is nothing more special about the interfaces that you create.

04:08.680 --> 04:10.960
And then well let's see how to use it.

04:10.960 --> 04:16.840
So I'm going to create a very simple Publisher self, let's call it hardware.

04:16.840 --> 04:28.690
So h w status pub is equal to self create publisher with hardware status.

04:29.050 --> 04:30.430
That's the interface here.

04:31.330 --> 04:35.410
And then let's call it hardware status.

04:35.530 --> 04:39.040
And let's give it a queue size okay.

04:39.040 --> 04:49.120
And let's create a timer self timer self create timer with let's say one second.

04:49.120 --> 04:53.650
And then let's create a callback actually not a call.

04:53.680 --> 04:58.810
Let's call it publish hardware status.

05:01.900 --> 05:02.200
Okay.

05:02.200 --> 05:06.520
That's going to be the function we use to create a message and publish on the topic.

05:06.670 --> 05:10.870
And so to create a message we are simply going to use the hardware status.

05:10.870 --> 05:15.400
So hardware status we are going to use the class and create an object.

05:15.400 --> 05:16.120
That's it.

05:16.320 --> 05:23.850
And then we can do MSG, dot and you see we have all of the fields, for example MSG dot temperature

05:24.810 --> 05:34.530
and let's say 43.7 and then MSG dot amateurs ready and let's say true.

05:35.010 --> 05:37.470
So you see we use the data type for each field.

05:37.470 --> 05:47.220
And then MSG dot debug message is equal to let's say nothing special.

05:47.250 --> 05:47.610
All right.

05:47.610 --> 05:53.340
So you see we just fill the different fields of the message that we have created.

05:53.340 --> 06:02.760
And then after we create the message we publish it with the hardware status publisher Dot publish the

06:02.760 --> 06:03.420
message.

06:03.420 --> 06:08.970
And then I can put the callback here self dot publish hardware status.

06:09.420 --> 06:11.310
And let's finish with a log.

06:11.790 --> 06:12.960
Get logger.

06:15.360 --> 06:25.740
Info with How do it status publisher has been studied.

06:26.100 --> 06:28.020
Okay, so I went quite fast here.

06:28.020 --> 06:32.670
But this is a structure that we have done already several times in this course.

06:32.700 --> 06:33.900
It's always the same.

06:33.900 --> 06:38.040
We create a publisher, a timer, we create a message, we publish the message.

06:38.070 --> 06:42.450
The only difference here is that we use a custom interface, but the code is basically the same.

06:42.450 --> 06:45.240
So let's save and let's test it.

06:45.240 --> 06:49.620
So I'm going to create a new executable here in setup.py.

06:50.010 --> 06:56.490
Let's name it hardware status publisher is equal to.

06:56.520 --> 06:58.530
So the file name is hardware.

06:59.100 --> 07:10.230
Actually we need to put the package name Mypy pkg dot hardware status publisher.

07:10.230 --> 07:12.240
And still the main function.

07:12.270 --> 07:12.450
Okay.

07:12.450 --> 07:17.790
You see I use a slightly different name for the executable and the file name this time, which is completely

07:17.790 --> 07:18.420
fine.

07:18.810 --> 07:26.490
And let's go there again And let's go back here and let's build.

07:26.610 --> 07:35.100
Click on build with packages select and the Python package.

07:36.720 --> 07:38.640
And then let's choose the workspace.

07:38.640 --> 07:40.050
Let's just do it here.

07:40.080 --> 07:45.720
We can just do the source install setup bash from the workspace directly.

07:46.080 --> 07:56.400
And then let's do Ros to run my Pi PG with hardware status publisher okay it is publishing.

07:56.400 --> 08:01.980
And here for example, let's do a Ros two topic list.

08:03.060 --> 08:11.880
We see hardware status and then Ros two topic equal hardware status.

08:12.120 --> 08:14.730
And as you can see we have an error message.

08:14.730 --> 08:16.950
So that's why I kept those terminals like this.

08:16.950 --> 08:19.020
Why do we have an error message.

08:19.020 --> 08:26.660
It says the message type is invalid simply because well if you remember we have built the message here,

08:26.660 --> 08:29.960
and since then we haven't sourced that environment.

08:29.990 --> 08:31.340
Okay, so this one is sourced.

08:31.370 --> 08:32.420
This one is sourced.

08:32.420 --> 08:36.740
So the same command here should run in this terminal.

08:37.970 --> 08:38.330
Okay.

08:38.360 --> 08:45.830
You see we receive the messages but not in that one because we haven't sourced the environment.

08:45.860 --> 08:46.100
Okay.

08:46.130 --> 08:47.720
So this is very important.

08:47.750 --> 08:53.210
And in this case, as you can see when we build a new interface and a new node, what I recommend really

08:53.210 --> 08:56.120
is just let's close everything.

08:57.110 --> 08:59.150
Let's reopen everything.

08:59.150 --> 09:08.180
So now everything is correctly sourced to run my Python pkg with hardware status publisher.

09:08.270 --> 09:18.020
And here for example, rose to topic equal with a hardware status.

09:19.490 --> 09:20.990
And this time is working.

09:20.990 --> 09:24.410
And we're just publishing the same thing every second.

09:24.440 --> 09:24.890
All right.

09:24.890 --> 09:29.660
So now you know how to use a custom message inside your own Python nodes.
