WEBVTT

00:00.050 --> 00:05.630
I might have briefly mentioned namespaces a few times during this course, but now is a good time to

00:05.660 --> 00:07.880
explain how to work with them.

00:07.880 --> 00:13.880
This concept doesn't necessarily belong to the launch file section, but as we set them mostly inside

00:13.880 --> 00:18.530
launch files and also with YAML files, I wanted to wait until this point.

00:18.530 --> 00:22.190
So let's start first with the concept of namespaces.

00:22.220 --> 00:24.410
See how to add a namespace to a node.

00:24.410 --> 00:28.070
And finally we will see how to use namespaces in launch files.

00:28.070 --> 00:30.320
So well you can open some terminals.

00:30.320 --> 00:37.490
And when we start a node let's just do a rush to run with my, for example, CBP package.

00:37.490 --> 00:41.000
And let's start the number publisher that we have used quite a lot already.

00:41.030 --> 00:50.090
Well, you know that if I run this and if I do rush to node list, let's also do a rush to topic list.

00:50.930 --> 00:51.320
Okay.

00:51.320 --> 00:54.200
We have this is the node name number publisher.

00:54.200 --> 00:57.770
And we have the number topic published by this one.

00:58.100 --> 01:02.480
Now let's stop And how to add a namespace.

01:02.480 --> 01:04.910
I'm going to add a namespace and show you what it does.

01:05.360 --> 01:11.210
Well, you just do Ross args, just like for everything else, for renaming, for parameters.

01:11.210 --> 01:13.490
And then you add another dash R.

01:13.520 --> 01:13.790
Okay.

01:13.790 --> 01:17.480
So that's going to be considered as a remapping dash R.

01:17.510 --> 01:22.520
And then you have underscore underscore ns for namespace.

01:22.520 --> 01:31.760
So to rename a node you had node like that for namespace you have this and let's say slash test what's

01:31.760 --> 01:32.510
going to happen.

01:32.510 --> 01:34.880
Well first you can see in the logs.

01:35.030 --> 01:40.880
Now we have not just number publisher we have test number publisher okay.

01:40.910 --> 01:44.630
So namespace is going to be added in front of the name.

01:44.630 --> 01:47.630
Well just like you would use a namespace in your code.

01:47.630 --> 01:50.900
If you're already familiar with namespaces you know what it does okay.

01:50.930 --> 01:54.290
It's just adding a name in front of all the other names.

01:54.290 --> 02:00.120
So if I do Ros2 node list now you can see my node is not number publisher.

02:00.150 --> 02:03.240
My node is test number publisher.

02:03.420 --> 02:06.810
And if I do rostopic list you see that.

02:06.810 --> 02:09.000
So we have the result and parameter events.

02:09.000 --> 02:11.250
Those are just kind of on the side.

02:11.250 --> 02:16.830
But you see now my number topic is not number it's test number.

02:16.860 --> 02:17.310
Okay.

02:17.340 --> 02:23.040
So that's one very important thing is that when you add a namespace to a node, when you start, it

02:23.070 --> 02:26.130
is going to change all of the names inside.

02:26.130 --> 02:31.320
So the node name and the topic names, the service name etc..

02:31.350 --> 02:32.700
Let's check the parameters.

02:32.730 --> 02:35.430
Of course two param list.

02:36.540 --> 02:41.700
Well the parameter name of course doesn't change, but the name of the node has changed.

02:41.700 --> 02:48.390
And so let's come back to the code and I'm going to show you the importance of the leading slash.

02:48.390 --> 02:55.200
So well for example in the Python package it's the same code as the C plus plus one.

02:56.460 --> 03:03.450
You can see that when I created here the name of the node, I didn't put any slash.

03:03.480 --> 03:03.660
Okay.

03:03.690 --> 03:07.080
The same for the topic name.

03:07.080 --> 03:08.850
I didn't add any slash.

03:08.850 --> 03:14.790
And what you can see is if I don't add any slash one leading slash is going to be added.

03:14.820 --> 03:15.150
Okay.

03:15.180 --> 03:16.800
I didn't provide that in the code.

03:16.800 --> 03:23.130
It's been added automatically because you're always going to start from let's call it the root namespace,

03:23.130 --> 03:26.880
which is no namespace, but it's going to add a slash anyway.

03:26.910 --> 03:31.050
So we have slash node name slash topic name.

03:31.050 --> 03:34.830
And here I have added that another test namespace.

03:34.830 --> 03:39.210
So now it's slash test slash number publisher okay.

03:39.240 --> 03:42.300
And by providing the name like this.

03:42.300 --> 03:48.030
So you can see if we don't provide any namespace it's going to add a slash automatically.

03:48.030 --> 03:51.900
But if we provide a namespace it's going to add the namespace in front of the name.

03:51.900 --> 03:57.690
Now let's say for example in this publisher let's say that I add a slash okay.

03:57.720 --> 04:02.320
What's going to happen if I add a slash to this publisher?

04:02.320 --> 04:04.060
So now it's slash number.

04:04.090 --> 04:07.150
Let's save and let's build again.

04:07.510 --> 04:11.680
So I'm going to go to Roster Workspace.

04:11.680 --> 04:14.830
Click on Build packages.

04:14.950 --> 04:19.030
Select with my Pi PG.

04:19.960 --> 04:21.910
Let's just build like this.

04:23.320 --> 04:23.740
What.

04:23.740 --> 04:26.860
We have a warning here but it's not a problem.

04:26.890 --> 04:27.370
Okay.

04:27.370 --> 04:31.150
We haven't done anything bad so let's source.

04:32.620 --> 04:32.950
Okay.

04:32.980 --> 04:33.700
And let's run.

04:33.700 --> 04:34.990
Let's do rush to run.

04:34.990 --> 04:36.520
So let's start.

04:36.550 --> 04:38.470
So I have changed the Python one.

04:38.470 --> 04:45.910
I'm going to of course launch with the Python package with rush to run my Pi PG number publisher.

04:46.480 --> 04:48.520
And then I add a namespace.

04:50.710 --> 04:52.900
So of course it's still working.

04:53.590 --> 05:00.100
And let's do Ros2 node list we have slash test slash number publisher.

05:00.400 --> 05:02.080
If I do roster topic list.

05:03.730 --> 05:07.000
You see we don't have slash test slash number.

05:07.000 --> 05:08.440
We just have slash number.

05:08.440 --> 05:09.460
Why is that?

05:09.460 --> 05:13.960
Because you see in the code I have provided a leading slash.

05:14.080 --> 05:14.440
Okay.

05:14.470 --> 05:20.110
So if you provide a leading slash you're going to say that that's not going to change even if you add

05:20.110 --> 05:21.700
a namespace.

05:21.730 --> 05:22.210
Okay.

05:22.240 --> 05:26.230
You've already said that this is going to be leading slash number.

05:26.230 --> 05:29.590
If you don't provide a slash then a slash is going to be added.

05:29.590 --> 05:33.820
And if you provide a namespace the namespace can also be added.

05:33.820 --> 05:40.960
So if you want to make sure that no namespaces can change topic name a node name or whatever, you add

05:40.990 --> 05:46.450
a slash here at the beginning in your code, but if not, you just don't add any slash.

05:46.450 --> 05:48.400
So that's an important difference here.

05:48.400 --> 05:54.820
And by default here in the code I have not added any slash, which means that I can add as many namespaces

05:54.820 --> 05:58.070
as I want, and it's quite modular this way.

05:58.100 --> 06:04.220
Okay, so let's save again and just again let's build.

06:05.030 --> 06:10.700
I didn't use Simulink install but okay let's source and let's run again.

06:11.900 --> 06:18.650
And now you see it's going to be slash test slash number because I didn't provide a slash.

06:18.650 --> 06:23.210
So that's quite important difference here and something to understand.

06:23.240 --> 06:23.690
All right.

06:23.690 --> 06:29.270
And namespaces are quite useful if you want to split your application for example for different robots

06:29.300 --> 06:31.130
let's say you have three different robots.

06:31.130 --> 06:34.100
And each robot has a node to control the wheel.

06:34.100 --> 06:39.320
And that's basically from the same node using the same topics and services, etc..

06:39.350 --> 06:44.900
How can you make sure that each robot is not going to get the comments for another robot, and that

06:44.900 --> 06:50.030
you're not going to just mess up communication between your wheel control nodes?

06:50.030 --> 06:56.240
Well, you can just use a namespace like slash robot one for the first, one, slash robot two, slash

06:56.240 --> 06:56.930
robot three.

06:56.930 --> 07:01.430
And then you make sure that the topics for robot one are just for robot one.

07:01.460 --> 07:01.670
Okay.

07:01.700 --> 07:05.630
So all the nodes for the robot one will have a namespace.

07:05.660 --> 07:09.710
All the nodes for the robot two will have another namespace, etc..

07:09.740 --> 07:10.010
Okay.

07:10.040 --> 07:13.640
So this way you can make your application super dynamic.

07:13.670 --> 07:16.250
You don't even need to change the code at all.

07:16.250 --> 07:20.780
You just add a namespace when you start the node and how to add a namespace.

07:20.780 --> 07:23.870
You do dash r with ns.

07:23.900 --> 07:28.280
So underscore underscore ns and you provide a namespace here.

07:28.280 --> 07:31.340
And now is where I come back to the launch files.

07:31.340 --> 07:35.750
Because usually we're going to start our applications from launch files.

07:35.750 --> 07:39.770
So that's where you are going to add namespaces.

07:39.770 --> 07:42.950
And to add a namespace inside a launch file.

07:42.950 --> 07:43.940
It's very easy.

07:43.970 --> 07:53.600
You see we have our node tag with pkg exec name and namespace namespace.

07:54.830 --> 08:01.680
And we're going to start with a slash and let's say well let's also use test namespace or let's say

08:01.710 --> 08:02.520
ABC.

08:02.730 --> 08:10.980
So let's see what happens if I just add a namespace ABC here for just that node for the number publisher

08:11.010 --> 08:11.970
node only.

08:13.230 --> 08:28.260
So this time I'm going to build the robot bring up and then source and then Rose to launch my robot

08:28.290 --> 08:34.890
bring up with number app dot launch dot XML.

08:36.510 --> 08:41.940
Okay, you can see already the name of the node has changed rose to node list.

08:42.810 --> 08:45.000
So the same thing as we saw previously.

08:45.000 --> 08:48.870
Now it's slash ABC slash my number publisher.

08:48.870 --> 08:57.570
And now if I do rose to topic list you see that the namespace has been applied to the topic.

08:57.570 --> 08:59.310
And here it's kind of weird.

08:59.310 --> 09:01.560
Let's let's see with our cut graph.

09:03.570 --> 09:05.160
And let's understand what's happening.

09:05.160 --> 09:09.240
So if I refresh uh well you can group namespaces here.

09:09.240 --> 09:10.020
You see.

09:10.020 --> 09:15.870
But if I go to zero, maybe it's a bit cleaner here for what we want.

09:15.900 --> 09:22.560
So you see we have our ABC my number publisher node which is publishing to ABC number.

09:22.590 --> 09:25.740
Then we have my number counter which is subscribing to my number.

09:25.740 --> 09:30.210
So of course we have a topic mismatch and those cannot communicate.

09:30.270 --> 09:32.640
But there is one thing that you see.

09:32.670 --> 09:38.340
The topic name here is ABC number in the launch file.

09:38.340 --> 09:40.950
So we have added a namespace ABC.

09:41.130 --> 09:45.330
And then we have say that we want to remap number to my number.

09:45.330 --> 09:49.290
And as you can see that clearly this remapping didn't work.

09:49.290 --> 09:55.090
And that's normal because at this point the topic name is not slash number.

09:55.120 --> 09:59.260
The topic name is slash ABC slash number.

09:59.260 --> 10:03.280
So this remapping is not going to have any effect.

10:03.280 --> 10:04.450
And how to solve this.

10:04.450 --> 10:07.090
Well actually you see I have used the slash here.

10:07.090 --> 10:09.820
So we are just going to remove the slash.

10:09.970 --> 10:12.760
Same thing here in the remap okay.

10:12.790 --> 10:15.190
Because it's going to look for slash number.

10:15.220 --> 10:15.970
Exactly.

10:15.970 --> 10:19.540
If I just put number then it's going to rename number.

10:19.540 --> 10:25.570
Even if it's in slash abt slash number it's going to rename number to my number.

10:25.600 --> 10:34.660
Otherwise I would have needed to do slash ABC slash number to slash ABC slash my number.

10:34.660 --> 10:37.630
But that doesn't make it very scalable okay.

10:37.630 --> 10:40.840
If you add a namespace then you need to change it everywhere.

10:41.410 --> 10:47.770
So in this case I just provide the topic name without the slash.

10:47.770 --> 10:52.690
And well now we're going to have the slash ABC slash my number topic.

10:52.690 --> 10:55.870
We also want to have the same topic name on that side.

10:55.870 --> 10:58.720
So we're also going to add the namespace here.

10:58.720 --> 11:08.260
It's going to make more sense namespace ABC and remove this slash that we've added previously okay.

11:08.290 --> 11:11.200
Now let's run again.

11:11.200 --> 11:18.790
So build and then source and then launch.

11:20.680 --> 11:28.150
And let's uh well let's see with Ross two topic list what's going to happen.

11:28.150 --> 11:30.910
You see we have everything is good.

11:31.150 --> 11:35.890
And we can verify that we've actually graph okay perfect.

11:35.920 --> 11:41.410
Now it's slash ABC my number publisher publishing to slash ABC my number.

11:41.410 --> 11:44.560
And this one is subscribing to the correct topic.

11:44.560 --> 11:52.240
And you see also that topic name here has changed because we've applied the ABC namespace to everything

11:52.240 --> 11:53.630
inside this node.

11:53.630 --> 11:54.740
And for the view here.

11:54.740 --> 11:57.500
Well you see I have put zero for namespaces.

11:57.500 --> 11:59.180
But then you can put one for example.

11:59.180 --> 12:01.760
And it's going to group the nodes inside.

12:01.760 --> 12:05.420
You see boxes that correspond to the namespace.

12:05.420 --> 12:11.690
So depending on some cases sometimes it's going to be more readable like this or more readable.

12:11.690 --> 12:15.170
As you increase this number you can just check on your own.

12:15.200 --> 12:15.770
All right.

12:15.770 --> 12:20.780
And that's going to be it for namespaces and how to apply them in launch files.

12:20.780 --> 12:25.730
So you can see in the end this concept and how to use it is not that complicated.

12:26.000 --> 12:31.790
And just in case you need to add a namespace inside of Python launch file, well, this one is quite

12:31.790 --> 12:32.240
easy.

12:32.270 --> 12:37.460
You see, once you have the structure, you just add a namespace argument here.

12:37.490 --> 12:38.810
Okay, with the namespace.

12:38.810 --> 12:41.840
And what I've done is also remove this slash here.

12:41.870 --> 12:45.590
Same reason as I explained to you in the XML launch file.

12:45.860 --> 12:46.280
Okay.

12:46.280 --> 12:52.640
So just namespace like that is going to do the same thing as this in XML.
