WEBVTT

00:00.080 --> 00:04.940
This is the solution for the first challenge of the activity five on parameters.

00:04.940 --> 00:12.290
And what we want to do here is to come back to the robot news station node that we have created at the

00:12.290 --> 00:18.350
beginning of the course, and we want to be able to provide a different robot name at runtime.

00:18.350 --> 00:21.380
So you see this is hard coded in the code.

00:21.380 --> 00:21.980
For now.

00:21.980 --> 00:26.960
We want to be able to choose a different robot name when we start the node.

00:26.960 --> 00:33.170
And so for example it's going to say hi this is and then a different robot name plus from the robot

00:33.170 --> 00:34.070
news station.

00:34.070 --> 00:35.090
And how to do that.

00:35.120 --> 00:37.220
Well we're going to use a parameters.

00:37.730 --> 00:44.030
So I'm going to the beginning of the constructor here I have my super that I live.

00:44.030 --> 00:48.800
And then I'm going to do self declare parameter.

00:48.950 --> 00:53.630
And the name of the parameter I gave you is robot name.

00:53.960 --> 00:56.420
And I'm going to give it a default value.

00:56.420 --> 00:58.280
I'm going to use that one okay.

00:58.310 --> 01:00.590
That was the value I wanted to use in the code.

01:00.590 --> 01:03.440
So let's keep it as a default value.

01:03.830 --> 01:05.450
And now the robot name.

01:05.450 --> 01:10.370
Instead of just hard coding it, I will simply do self.

01:11.120 --> 01:22.700
So self dot get parameter with the value robot name it must be the same and dot value.

01:22.790 --> 01:24.530
Okay, once again very important.

01:24.530 --> 01:25.820
Don't add any parentheses.

01:25.820 --> 01:29.420
And you see that's basically all we need to change in the code.

01:29.420 --> 01:32.330
Because then we use the robot name variable here.

01:32.330 --> 01:36.140
So I've kind of already prepared the code in advance for that.

01:36.140 --> 01:40.940
If you had hard coded the name directly in the string, you should have.

01:40.940 --> 01:42.140
For example, like this.

01:42.140 --> 01:47.990
You create a attribute a class attribute, and you set the class attribute here with the value of the

01:47.990 --> 01:48.800
parameter.

01:49.100 --> 01:49.520
Okay.

01:49.520 --> 01:51.410
So with those two lines that's it.

01:51.920 --> 01:53.600
Now I'm going to save.

01:53.600 --> 01:57.170
And I think I used Simulink install before.

01:57.170 --> 02:00.200
So we don't need to compile this again.

02:00.230 --> 02:08.870
Let's do Ros2 run my pip pkg with I think it was robot new station to execute table.

02:09.940 --> 02:11.710
Okay, so it's starting.

02:11.710 --> 02:14.560
We haven't provided any value for the parameters.

02:14.560 --> 02:18.370
Let's see with Ros2 param list.

02:19.930 --> 02:20.320
Okay.

02:20.350 --> 02:23.140
And you can see we have a robot name.

02:23.170 --> 02:31.240
If I do Ros2 param get robot new station and robot name.

02:33.250 --> 02:41.890
This is a string and the value is C-3po, just like we have here with the default value.

02:42.550 --> 02:49.840
Then if I do ros two topics, let's do Ros two topic list and the name was Robot News.

02:49.990 --> 02:54.220
Ros two topic equal robot news.

02:56.050 --> 02:56.620
We see.

02:56.650 --> 02:59.590
Hi, this is C-3po from the robot news station.

02:59.620 --> 02:59.980
All right.

03:00.010 --> 03:03.460
So now let's stop this and let's run it again.

03:03.460 --> 03:09.370
But this time I'm going to provide a parameter ros args dash p.

03:09.370 --> 03:14.380
And then the name of the parameter is robot name exactly?

03:14.530 --> 03:17.860
And let's say a b, c.

03:19.960 --> 03:20.290
Okay.

03:20.320 --> 03:21.460
It's starting.

03:21.460 --> 03:23.680
And we can see directly with the topic.

03:24.430 --> 03:27.940
We have hi, this is ABC from the robot News station.

03:27.940 --> 03:32.080
And we can get the parameter ABC.

03:32.290 --> 03:32.710
All right.

03:32.740 --> 03:35.710
So it's working now if you want to add.

03:35.710 --> 03:39.160
So if you want to add a space in a string.

03:39.160 --> 03:43.120
So let's say the name of the robot is the robot.

03:43.150 --> 03:49.750
If you want to add a space where that's not going to work you need to add double quotes okay.

03:51.640 --> 04:00.220
And here you see we have the robot and the parameter value is that one you see.

04:00.220 --> 04:03.610
So you just add double quotes if you want to add a space.

04:03.610 --> 04:03.970
Great.

04:03.970 --> 04:08.590
And now well I'm gonna start this node.

04:08.620 --> 04:17.830
Let's for example rename it dash R and then node and let's name it two.

04:17.860 --> 04:21.400
So that was robot new station, let's say station one.

04:23.230 --> 04:23.530
Okay.

04:23.530 --> 04:25.450
So it's station one and it's publishing.

04:25.450 --> 04:30.430
Note that I start kind of always with renaming Remappings and then parameters.

04:30.430 --> 04:34.630
You don't have to follow that order, but I think it makes things a bit cleaner.

04:34.630 --> 04:35.860
So I have this note here.

04:35.860 --> 04:45.760
Let's start another node here Ros two run my pi pkg robot new station.

04:46.390 --> 04:57.760
Let's rename let's say station two and then dash p robot name.

04:58.870 --> 05:03.790
And that's going to be discard for example.

05:04.120 --> 05:07.720
And okay we have station two.

05:07.750 --> 05:15.970
If I look at what we get on the robot news topic you see then we get both okay.

05:16.000 --> 05:18.580
Because both are publishers to that node.

05:18.580 --> 05:20.710
And so everything is correctly working.

05:20.710 --> 05:22.590
Just one last thing I want to show you.

05:22.590 --> 05:32.040
If I do Ros2 param list, then you see we have those two nodes and each node has a different set of

05:32.040 --> 05:32.940
parameters.

05:32.970 --> 05:33.330
Okay.

05:33.360 --> 05:40.560
If I do ros2 param get station one with robot name.

05:40.740 --> 05:45.120
So somehow well you see that it's a bit stuck.

05:45.120 --> 05:47.340
So autocompletion with parameters.

05:47.340 --> 05:55.020
Sometimes it's stuck so well okay, it's still a bit buggy, so I recommend just you copy paste the

05:55.020 --> 05:56.370
parameters like that.

05:56.940 --> 06:04.290
And what I wanted to show you is that for those two nodes you see those two parameters have different

06:04.290 --> 06:04.860
value.

06:04.860 --> 06:12.000
So the robot name parameter here only exists within that node and has a different value than that one

06:12.030 --> 06:12.390
okay.

06:12.420 --> 06:16.530
So if you kill that node it's going to kill that parameter inside this node okay.

06:16.560 --> 06:17.640
They have the same name.

06:17.640 --> 06:25.710
But you can see they each belong to a different node and they only exist as long as the node exists.
