WEBVTT

00:00.080 --> 00:05.630
I will separate this solution into three different videos, so it will be easier for you to follow.

00:05.660 --> 00:12.170
First, in this video, the first one, I will create the LED panel node and publish the LED panel state

00:12.170 --> 00:14.570
with a custom message type.

00:14.600 --> 00:21.170
In the second video, I will create the service server inside the LED panel using also a custom service

00:21.170 --> 00:21.860
type.

00:21.860 --> 00:27.860
And in the third video that's going to be the battery node which will call the set LED server.

00:27.860 --> 00:33.200
And once again I do the solution in Python and you can download the complete C plus plus code at the

00:33.200 --> 00:34.370
end of the section.

00:34.370 --> 00:35.660
So let's get started.

00:35.660 --> 00:41.450
We first want to create a custom interface because we want to publish an LED panel state.

00:41.450 --> 00:43.550
And for that we need a custom message.

00:43.550 --> 00:46.010
So that's the first thing we need to create.

00:46.040 --> 00:51.890
Let's go to our workspace and then to our Myrobot interfaces.

00:51.890 --> 00:57.470
And here we are going to create a new message inside the MSG folder.

00:57.470 --> 00:59.090
And I'm going to name it.

00:59.090 --> 01:06.350
So let's do touch LED state Array dot msg.

01:06.440 --> 01:06.890
Okay.

01:06.920 --> 01:10.460
And I just start each word here with an uppercase.

01:10.460 --> 01:13.160
And then I use lowercase for all the other letters.

01:13.400 --> 01:14.180
All right.

01:14.210 --> 01:17.000
Now let's go to VS code.

01:18.470 --> 01:20.810
I'm going to open VS code here.

01:21.770 --> 01:24.680
And let's create that interface.

01:26.060 --> 01:28.430
So I'm going to close all of that.

01:28.430 --> 01:32.900
And in MSG we have led state array.

01:32.930 --> 01:39.230
So what I'm going to do is simply to create a list of integer numbers for the LED state.

01:39.260 --> 01:43.700
To create a list of integer numbers I can put int 64.

01:43.730 --> 01:44.510
That's the type.

01:44.510 --> 01:51.230
And then those brackets to say it's a list and I'm going to name them LED states for example.

01:51.260 --> 01:51.500
Okay.

01:51.530 --> 01:55.880
So we're going to say that zero means off and one means on.

01:55.910 --> 01:57.740
We could have used booleans as well.

01:57.740 --> 02:00.830
But here I'm just using integers with zero and one.

02:00.860 --> 02:02.420
Let's save.

02:02.450 --> 02:02.690
Okay.

02:02.720 --> 02:04.280
So we just have one field.

02:04.340 --> 02:07.640
This is an array of integers.

02:07.640 --> 02:08.690
And that's it.

02:08.720 --> 02:10.790
Now we can build it.

02:10.820 --> 02:16.340
To build it, I'm going to go to Cmakelists.txt and add another line here.

02:16.340 --> 02:20.240
So I'm going to group all the messages and then all the services.

02:20.240 --> 02:23.180
It doesn't matter in the end but it's just a bit cleaner.

02:23.180 --> 02:31.010
So here I add another line MSG slash LED state array dot msg.

02:31.040 --> 02:32.840
Make sure you don't make any typo.

02:32.870 --> 02:37.460
Okay, for this file name let's save the Cmakelists.txt.

02:37.580 --> 02:38.750
And that's it.

02:38.780 --> 02:41.030
Okay we have created the interface.

02:41.030 --> 02:42.320
We add it here.

02:42.350 --> 02:43.910
Now we can build it.

02:44.930 --> 02:48.950
Let's do click on build packages.

02:49.760 --> 02:54.830
Select with my robot interfaces.

02:54.830 --> 03:00.080
You see I still have this warning log but I'm not doing anything wrong.

03:00.080 --> 03:02.510
So I'm just going to ignore that log.

03:03.500 --> 03:03.950
All right.

03:03.950 --> 03:08.150
And we have built the interface now because we have built the interface.

03:08.150 --> 03:12.690
We will need to source all the terminals where we want to be able to use it.

03:12.720 --> 03:18.150
We will also need to close VSCode and open it again from a terminal that is sourced.

03:18.150 --> 03:22.500
So I'm just going to close VSCode and I'm going to close all the terminals.

03:22.530 --> 03:22.890
Okay.

03:22.920 --> 03:25.410
I'm going to open terminals again.

03:27.180 --> 03:27.630
Okay.

03:27.630 --> 03:32.160
And then start VSCode from that folder.

03:33.120 --> 03:33.480
Okay.

03:33.510 --> 03:39.600
So then I can use my interface with the Autocompletion for Python and for C plus.

03:39.600 --> 03:45.930
Plus just a quick note for C plus plus you already have that line here okay in the C and cpp properties

03:45.930 --> 03:46.830
dot JSON.

03:46.830 --> 03:49.440
So for C plus plus it's also going to work.

03:49.560 --> 03:55.260
But first of all let's go in the terminal and let's see if we can find this interface Ros2 interface.

03:55.260 --> 03:59.700
Show my robot interfaces MSG.

03:59.700 --> 04:03.540
And then let's put an L with Autocompletion.

04:03.540 --> 04:05.310
We have led state array.

04:05.340 --> 04:05.820
Great.

04:05.820 --> 04:09.180
And if you can see that, it means that you can use your interface.

04:09.210 --> 04:13.200
If you cannot, you need to go back and make sure that everything is working.

04:13.200 --> 04:15.570
So now let's go here.

04:15.570 --> 04:22.710
And actually I'm going to create a new node because I want to create a node specially for the LED panel.

04:22.710 --> 04:34.260
So let's go to the Python package here and let's create that LED panel dot pi.

04:34.980 --> 04:39.570
Let's make it executable okay.

04:39.570 --> 04:42.300
And let's edit that file.

04:42.630 --> 04:51.450
So I don't need that anymore LED panel dot pi I'm going to take the template I don't need that.

04:52.830 --> 05:01.830
And let's put the template here and it's going to be LED panel node okay.

05:01.830 --> 05:04.080
And let's just call it LED panel.

05:06.120 --> 05:07.050
All right.

05:09.660 --> 05:18.030
And what I want to do here is to publish the states for all the LEDs in this LED panel Every X amount

05:18.030 --> 05:18.810
of time.

05:18.810 --> 05:26.070
So as you can probably guess, we need what we need a publisher and we need a timer to create a publisher

05:26.070 --> 05:27.510
I will need the interface.

05:27.510 --> 05:38.910
So let's import from my robot interfaces dot msg import led state array.

05:39.360 --> 05:49.110
Let's create a publisher here self dot led states publisher or just pub to take less space.

05:50.070 --> 05:58.560
We have self create publisher with the type led state array.

05:58.740 --> 06:00.000
That's the one here.

06:00.930 --> 06:02.610
Then the topic name.

06:02.760 --> 06:08.970
So let's call it LED panel state like the one I gave you in the challenge.

06:08.970 --> 06:11.820
And then we have a queue size.

06:12.060 --> 06:14.580
I'm just going to make it a bit cleaner okay.

06:14.610 --> 06:24.570
After that I'm going to create here in the class a method to publish LED states.

06:26.370 --> 06:31.440
Okay, I'm just going to put pass for now so that I can create a timer.

06:31.470 --> 06:36.360
Self dot LED states.

06:36.390 --> 06:39.390
Timer is equal to self dot.

06:39.390 --> 06:40.560
Create timer.

06:42.960 --> 06:48.090
And well let's say we want to publish every five seconds.

06:49.080 --> 06:49.320
Okay.

06:49.350 --> 06:52.590
We don't necessarily need that very often.

06:52.710 --> 06:56.280
So every five seconds we're going to publish the LED states.

06:56.580 --> 07:01.020
I put five and then self dot publish LED states.

07:01.020 --> 07:02.850
That's the callback.

07:02.880 --> 07:16.140
And finally let's add a log get logger info LED panel node has been started.

07:17.220 --> 07:17.490
Okay.

07:17.520 --> 07:21.630
And what do we do in this publish led state method.

07:21.630 --> 07:28.350
We're going to create a message of type LED state array.

07:28.680 --> 07:35.790
And this message here has a LED state field.

07:35.970 --> 07:36.180
Okay.

07:36.210 --> 07:39.750
If you are not sure you see we have it here from the terminal.

07:39.780 --> 07:41.730
You can also well you have written the code.

07:41.730 --> 07:44.280
So you can just go back to your interface and check that.

07:44.280 --> 07:48.720
And we also have the Autocompletion led state of type.

07:48.900 --> 07:51.990
Well that's the list of integers.

07:51.990 --> 07:58.830
So I'm going to initialize in the constructor I'm going to initialize self led state.

07:59.250 --> 08:03.420
And I'm going to initialize that to an array with three elements.

08:03.450 --> 08:06.300
Let's say we have three LEDs just like in the example I gave you.

08:06.300 --> 08:08.850
And those three LEDs are turned off.

08:08.880 --> 08:11.580
So once again zero means turned off.

08:11.580 --> 08:13.890
One would be turned on.

08:14.310 --> 08:14.610
All right.

08:14.610 --> 08:17.010
So now they are all turned off.

08:17.040 --> 08:20.160
We're going to see how to turn them on with a service.

08:20.160 --> 08:22.920
But for now we just publish the state as it is.

08:22.920 --> 08:29.050
So edit state is equal to self led states.

08:29.350 --> 08:32.590
And then we can do self dot led states.

08:32.590 --> 08:38.260
Publisher dot publish the message.

08:38.950 --> 08:39.220
Okay.

08:39.220 --> 08:46.330
So once again we have a very classic publisher and timer combo where we publish every X amount of time

08:46.330 --> 08:48.490
here every five seconds.

08:48.580 --> 08:50.170
So I'm going to save that.

08:50.170 --> 08:54.970
And let's create an executable here setup.py.

08:55.090 --> 09:08.110
I add a comma and then LED panel is equal to my pi pkg dot led panel with the main function.

09:08.290 --> 09:10.240
Okay let's go back here.

09:10.330 --> 09:18.700
Let's build the package called con build packages.

09:18.700 --> 09:22.180
Select with the Python one.

09:23.770 --> 09:27.550
And I'm also going to add the symlink install option here.

09:27.640 --> 09:31.390
So we can modify without having to build again.

09:31.990 --> 09:41.710
So now from this one, for example, let's use the terminal and let's do Ros to run my Pi PG with LED

09:42.010 --> 09:42.880
panel.

09:43.810 --> 09:44.260
Okay.

09:44.290 --> 09:46.000
The node has been started.

09:46.330 --> 09:50.140
Let's just source all the terminals here.

09:51.880 --> 09:52.360
Okay.

09:52.360 --> 09:55.780
And let's do a Ros two topic list.

09:56.350 --> 09:58.300
We should see our LED panel.

09:58.300 --> 10:01.060
State Ros two topic.

10:01.480 --> 10:04.930
Equal LED panel state.

10:06.460 --> 10:08.830
So it's publishing every five seconds.

10:08.830 --> 10:10.360
So you might need to wait a bit.

10:10.390 --> 10:11.500
You see we have one.

10:11.500 --> 10:16.060
And then we should have another one five seconds later etc..

10:16.300 --> 10:18.400
And that's how you see an array.

10:18.430 --> 10:18.670
Okay.

10:18.700 --> 10:21.280
You see we have the name of the field.

10:21.280 --> 10:24.850
And then one element per line here with a dash.

10:24.850 --> 10:29.440
So when you do a ros2 topic that's how you're going to see lists of elements.

10:29.470 --> 10:30.010
Great.

10:30.010 --> 10:32.980
And the first step for this activity is finished.
