This is the solution for the first challenge of the activity five on parameters.
And what we want to do here is to come back to the robot news station node that we have created at the
beginning of the course, and we want to be able to provide a different robot name at runtime.
So you see this is hard coded in the code.
For now.
We want to be able to choose a different robot name when we start the node.
And so for example it's going to say hi this is and then a different robot name plus from the robot
news station.
And how to do that.
Well we're going to use a parameters.
So I'm going to the beginning of the constructor here I have my super that I live.
And then I'm going to do self declare parameter.
And the name of the parameter I gave you is robot name.
And I'm going to give it a default value.
I'm going to use that one okay.
That was the value I wanted to use in the code.
So let's keep it as a default value.
And now the robot name.
Instead of just hard coding it, I will simply do self.
So self dot get parameter with the value robot name it must be the same and dot value.
Okay, once again very important.
Don't add any parentheses.
And you see that's basically all we need to change in the code.
Because then we use the robot name variable here.
So I've kind of already prepared the code in advance for that.
If you had hard coded the name directly in the string, you should have.
For example, like this.
You create a attribute a class attribute, and you set the class attribute here with the value of the
parameter.
Okay.
So with those two lines that's it.
Now I'm going to save.
And I think I used Simulink install before.
So we don't need to compile this again.
Let's do Ros2 run my pip pkg with I think it was robot new station to execute table.
Okay, so it's starting.
We haven't provided any value for the parameters.
Let's see with Ros2 param list.
Okay.
And you can see we have a robot name.
If I do Ros2 param get robot new station and robot name.
This is a string and the value is C-3po, just like we have here with the default value.
Then if I do ros two topics, let's do Ros two topic list and the name was Robot News.
Ros two topic equal robot news.
We see.
Hi, this is C-3po from the robot news station.
All right.
So now let's stop this and let's run it again.
But this time I'm going to provide a parameter ros args dash p.
And then the name of the parameter is robot name exactly?
And let's say a b, c.
Okay.
It's starting.
And we can see directly with the topic.
We have hi, this is ABC from the robot News station.
And we can get the parameter ABC.
All right.
So it's working now if you want to add.
So if you want to add a space in a string.
So let's say the name of the robot is the robot.
If you want to add a space where that's not going to work you need to add double quotes okay.
And here you see we have the robot and the parameter value is that one you see.
So you just add double quotes if you want to add a space.
Great.
And now well I'm gonna start this node.
Let's for example rename it dash R and then node and let's name it two.
So that was robot new station, let's say station one.
Okay.
So it's station one and it's publishing.
Note that I start kind of always with renaming Remappings and then parameters.
You don't have to follow that order, but I think it makes things a bit cleaner.
So I have this note here.
Let's start another node here Ros two run my pi pkg robot new station.
Let's rename let's say station two and then dash p robot name.
And that's going to be discard for example.
And okay we have station two.
If I look at what we get on the robot news topic you see then we get both okay.
Because both are publishers to that node.
And so everything is correctly working.
Just one last thing I want to show you.
If I do Ros2 param list, then you see we have those two nodes and each node has a different set of
parameters.
Okay.
If I do ros2 param get station one with robot name.
So somehow well you see that it's a bit stuck.
So autocompletion with parameters.
Sometimes it's stuck so well okay, it's still a bit buggy, so I recommend just you copy paste the
parameters like that.
And what I wanted to show you is that for those two nodes you see those two parameters have different
value.
So the robot name parameter here only exists within that node and has a different value than that one
okay.
So if you kill that node it's going to kill that parameter inside this node okay.
They have the same name.
But you can see they each belong to a different node and they only exist as long as the node exists.