WEBVTT

00:00.080 --> 00:02.900
This is the solution for the activity number two.

00:02.930 --> 00:09.950
In this activity, you have to create some kind of a data pipeline between multiple nodes using topics.

00:09.950 --> 00:15.440
I will only provide a video solution for Python here, and you can also download the complete solution

00:15.440 --> 00:16.400
for Cplusplus.

00:16.400 --> 00:18.350
At the end of this section.

00:18.350 --> 00:25.880
The Cplusplus solution will be exactly the same just using the Cplusplus syntax and Clcp instead of

00:26.060 --> 00:30.440
CLP, and I have divided the solution into two steps.

00:30.440 --> 00:36.050
In this first step in this video, we are going to create the number publisher node, and then in the

00:36.050 --> 00:40.310
next video, in the second step we will do the number counter node.

00:40.310 --> 00:45.440
And the reason why I'm doing this is because we are first going to create the publisher, okay.

00:45.470 --> 00:46.940
We are going to publish a number.

00:46.940 --> 00:52.430
And then we can validate that this publisher is correctly working using the Ros two command line tool.

00:52.430 --> 00:57.470
Then when we know that the publisher is correctly working, we can create the subscriber side.

00:57.470 --> 01:02.330
So let's create a new node which will contain the publisher.

01:02.330 --> 01:05.510
Let's go to our workspace in.

01:05.510 --> 01:12.020
So we're going to continue using the My Python package that we've created before we go to the folder

01:12.020 --> 01:14.900
which has the same name as the package.

01:14.900 --> 01:23.630
And here we already have our nodes, I'm going to add a new one named number publisher Dot Pi.

01:23.660 --> 01:28.130
And let's make it executable okay.

01:28.160 --> 01:31.190
If we want to use the Simulink install.

01:31.190 --> 01:33.740
So chmod plus x.

01:33.740 --> 01:39.110
And you see now it's in green right here I will open VSCode.

01:39.110 --> 01:44.390
So for example here let's go back to the source folder of the workspace.

01:44.390 --> 01:46.580
And let's do code dot.

01:48.710 --> 01:50.900
Okay I'm going to clean that a bit.

01:50.930 --> 01:52.880
We don't need all those files.

01:53.390 --> 02:01.220
And let's go to our number publisher Dot Pi I will start as we did before.

02:01.250 --> 02:07.340
I'm just going to take the template and let's put it there.

02:07.550 --> 02:07.910
Okay.

02:07.940 --> 02:09.800
So we have the template for the node.

02:09.950 --> 02:17.510
We want to create a number publisher node okay which is going to be the class name.

02:17.510 --> 02:20.060
And we need to provide the name here as well.

02:20.060 --> 02:25.700
And the name of the node is going to be number publisher.

02:26.270 --> 02:30.590
And let's remove also those comments here.

02:30.620 --> 02:31.190
Great.

02:31.190 --> 02:35.450
So now we are going to create a publisher inside the constructor.

02:35.450 --> 02:39.320
And this publisher is going to publish on a number topic.

02:39.350 --> 02:40.640
And it's going to publish a number.

02:40.640 --> 02:44.720
And actually I gave you the type that we're going to use for this topic.

02:44.720 --> 02:46.400
And this is the.

02:46.430 --> 02:48.020
So I'm going to type it here.

02:48.020 --> 02:53.180
So Ross to interface you can do Ross to interface show.

02:53.720 --> 02:57.800
And this is example interfaces.

02:57.800 --> 03:04.580
So let's use Autocompletion here and then MSG and then slash int 64.

03:04.610 --> 03:09.740
Okay so we've already seen a string we have here int 64.

03:09.770 --> 03:12.230
Which means that 64 bit integer.

03:12.260 --> 03:15.590
Let's see what we have in this interface because we're going to need to use it.

03:15.950 --> 03:19.460
And well this is very similar to what we had before.

03:19.460 --> 03:23.300
So there is a you see we have a few comments here.

03:23.300 --> 03:24.680
So you can just ignore that.

03:24.680 --> 03:29.840
And we basically have one field which name is data okay.

03:29.870 --> 03:32.690
And this field here is an int 64.

03:32.690 --> 03:34.460
So basically just an integer.

03:34.490 --> 03:35.090
All right.

03:35.120 --> 03:41.090
So now that we know this let's include that interface inside our node.

03:41.090 --> 03:50.210
So right here from example interfaces dot msg import.

03:50.240 --> 03:52.490
And you can see we find the int.

03:52.490 --> 03:58.670
So we have int 1632 64 eight okay we have different ones.

03:58.670 --> 04:00.650
We're just going to use in 64 here.

04:00.650 --> 04:07.580
And because we use example interfaces we just make sure that in our package dot XML we have the dependency.

04:07.580 --> 04:13.190
But that's already the case because we've used this package already before for the other nodes in this

04:13.190 --> 04:13.850
package.

04:14.300 --> 04:18.230
So let's go back and let's create a publisher.

04:18.230 --> 04:20.750
I'm going to create the publisher in the constructor.

04:20.750 --> 04:22.100
So self dot.

04:22.100 --> 04:26.150
So let's name it number publisher okay.

04:26.720 --> 04:29.360
Let's try to give explicit names.

04:29.360 --> 04:35.270
And we're going to use self dot create publisher.

04:35.660 --> 04:37.280
What's the first thing we need to give.

04:37.310 --> 04:38.600
You see the message type.

04:38.600 --> 04:41.210
So that's going to be int 64.

04:41.240 --> 04:43.220
That's what we've just imported here.

04:43.520 --> 04:45.200
Then we need to give a name.

04:45.200 --> 04:47.570
So what name here I gave you.

04:47.570 --> 04:50.840
The name before is going to be number topic okay.

04:50.870 --> 04:52.790
The topic doesn't exist yet.

04:52.790 --> 04:55.280
There is no subscriber or publisher to this topic.

04:55.280 --> 04:57.410
So here we create the first publisher.

04:57.410 --> 05:01.220
We decide of the name and then we also have a queue size.

05:01.220 --> 05:02.690
I'm just going to put ten.

05:02.720 --> 05:08.090
Then after I create a publisher I'm also going to create a function to publish.

05:08.090 --> 05:09.200
So that's best practice.

05:09.230 --> 05:14.570
To do that we create a function that's just going to publish a message on that topic and let's name

05:14.570 --> 05:19.340
it Publish number with self.

05:19.340 --> 05:21.680
Because we are in a class and what do we do?

05:21.710 --> 05:23.150
We create a message.

05:23.150 --> 05:28.100
So msg is equal to int 64.

05:28.100 --> 05:31.460
And in this message if you remember we had.

05:31.460 --> 05:36.500
So in the terminal we saw that we have a data field of type int 64.

05:36.500 --> 05:40.250
So let's go back and we can do message dot data.

05:40.280 --> 05:46.580
Usually we find the data here is equal to and well let's give a number.

05:46.610 --> 05:55.160
Actually I'm going to create a self number here is equal to two for example.

05:55.160 --> 06:00.470
And let's do message data is equal to self number.

06:00.530 --> 06:03.440
So we're going to publish the same number every time okay.

06:03.470 --> 06:05.240
Let's just for this example.

06:05.240 --> 06:07.790
And then well we have the message.

06:07.790 --> 06:09.260
What do we do with this message.

06:09.260 --> 06:12.290
We are going to publish it with the number publisher.

06:12.290 --> 06:18.570
So self number publisher dot Publish.

06:19.230 --> 06:19.890
Publish what?

06:19.920 --> 06:21.180
The message.

06:21.180 --> 06:22.560
As simple as that.

06:22.560 --> 06:25.500
So you see, the structure is always the same.

06:25.500 --> 06:26.580
And then.

06:26.580 --> 06:28.710
Well, how do we call that?

06:28.740 --> 06:32.610
So we want to call it, let's say every one second.

06:32.610 --> 06:39.180
So I don't know if I gave you a frequency to call this function to publish a message, but let's say

06:39.180 --> 06:40.860
every one second.

06:40.890 --> 06:42.090
How do we do this.

06:42.120 --> 06:44.190
We are going to create a timer.

06:44.190 --> 06:55.440
So self dot let's call it number timer is equal to self create timer.

06:55.920 --> 06:59.700
We provide a timer period in Saigon.

06:59.700 --> 07:04.290
So let's say 1.0 and then the reference to the function.

07:04.290 --> 07:10.740
So that's going to be the callback self dot publish number without any parentheses.

07:10.740 --> 07:11.640
Once again.

07:11.640 --> 07:22.050
And that's basically it I'm going to add another log self getlogger just to say so info just to say

07:22.050 --> 07:28.170
that number publisher has been studied.

07:28.380 --> 07:29.730
And that's the end of the code.

07:29.730 --> 07:34.650
So when we create the node here we're going to go to the constructor.

07:34.680 --> 07:35.940
We're going to create a publisher.

07:35.940 --> 07:37.770
We're going to create a timer.

07:37.770 --> 07:43.980
And then when we make the node spin well with the timer we will have one callback every one second.

07:43.980 --> 07:47.580
So this function is going to be called every one second.

07:47.580 --> 07:50.970
And we're just going to publish every one second.

07:50.970 --> 07:53.460
So let's make sure we save the file.

07:53.460 --> 07:55.830
And then we are going to create an executable.

07:55.830 --> 07:57.960
So let's go to setup.py.

07:57.990 --> 08:01.710
Let's add a comma here new line.

08:01.710 --> 08:04.380
And then well let's name it number.

08:04.890 --> 08:07.530
Publisher is equal to.

08:07.560 --> 08:09.600
We have the mypy pkg.

08:11.790 --> 08:14.340
And then dot what's the file name.

08:14.340 --> 08:19.680
The file name is number publisher number publisher okay.

08:19.710 --> 08:21.090
We don't put the extension.

08:21.090 --> 08:23.280
And then what function we want to call.

08:23.280 --> 08:25.410
Recall what is always the same main.

08:25.410 --> 08:27.810
So I put main here.

08:27.840 --> 08:29.430
I'm going to save this.

08:29.460 --> 08:29.760
Okay.

08:29.790 --> 08:30.510
Very important.

08:30.510 --> 08:32.160
You save all the files.

08:32.190 --> 08:42.240
Let's go back to the terminal and let's go back here to this folder Ros2 workspace I'm going to do call

08:42.240 --> 08:45.450
code build packages.

08:47.160 --> 08:54.780
Select with the Python package and let's use also symlink install.

08:56.130 --> 08:56.880
Let's press enter.

08:56.910 --> 08:58.260
Let's build this package.

08:58.260 --> 09:01.650
So it's going to install the Python executables.

09:01.650 --> 09:06.750
And actually with symlink installed we can directly run the code that we have written so that if we

09:06.750 --> 09:09.870
modify it we can just run it again.

09:09.870 --> 09:12.300
And we don't need to do a build again.

09:12.300 --> 09:13.710
So let's try that.

09:13.710 --> 09:15.480
I'm going to try that in this terminal.

09:15.480 --> 09:19.170
And first because we have actually built something.

09:19.170 --> 09:22.440
So even if we use all of those options we have built.

09:22.620 --> 09:31.740
So I need to do a source bash I see, and I'm just going to source all the terminal so you can open

09:31.740 --> 09:34.530
new ones or just source the existing one.

09:35.940 --> 09:36.600
Okay.

09:36.660 --> 09:39.090
And here let's do so.

09:39.120 --> 09:44.250
Let's clear and let's do Ros to run my Pi PG.

09:44.280 --> 09:47.580
And that's going to be number publisher.

09:47.580 --> 09:50.100
You see you should find it with the Autocompletion.

09:50.130 --> 09:51.300
Let's press enter.

09:51.300 --> 09:55.980
And we see number publisher has been started here.

09:55.980 --> 09:58.170
Let's do a Ros to node list.

09:58.200 --> 10:00.870
We can see the number publisher.

10:00.900 --> 10:03.930
Let's do a Ros two topic list.

10:04.050 --> 10:06.600
We can see the number topic okay.

10:06.600 --> 10:09.210
Plus the two other topics we always have.

10:09.210 --> 10:14.340
And we can do Ros two topic equal with number.

10:14.790 --> 10:16.200
And what do we have.

10:16.230 --> 10:21.000
You see we have data two every one second.

10:21.000 --> 10:23.610
So it is correctly working.

10:23.610 --> 10:30.180
And we have finished the first step of this activity with this number publisher.
