Let's now see how to use parameters in your Cplusplus nodes.
So I encourage you to first watch the previous lesson on Python where I detailed everything.
Here.
I'm going to focus more on the syntax because in the end the parameters will are going to work exactly
the same between Python and Cplusplus.
So I'm going to go back to the code.
Well here we have the Python code and the cplusplus code.
I'm going to add it into the number publishers cpp.
So once again this is the code we have written for an activity with the topics section.
And here what do we have.
Well very simply we have a publisher a timer and we publish a number here.
We publish the number two in this method that we call every one second with this timer.
So now let's use parameters for those values so that we can provide a different value at runtime if
we want.
And there are two things you need to do for each parameter.
You need to first declare it just to say that it exists within the node, and also set a default value.
And then you need to get its value so that you can use it.
And we're going to do that.
What I'm going to do that directly inside the constructor.
Usually I declare the parameters and I get them before I do everything else.
How to declare a parameter.
Well I'm going to use this.
So this is not mandatory.
But again it's to make it explicit that it's from the node class.
And you have a declare parameter method.
Here from the Clcp node class you need to give first a name.
So that's going to be number.
And then you can give a default value let's say two.
All right.
And you have declared one parameter in C plus plus.
So after this line the number parameter will exist.
The default value is two.
And because the default value is two like this that's also going to set the data type for the parameter.
And the parameter is going to be an integer.
Let's declare a second one.
Declare parameter.
Let's call it timer period.
And this one is going to be 1.0.
All right.
And after this those two parameters are declared.
But if we just keep the code like this, well nothing is going to happen because the parameters will
exist kind of within the node, but we don't use them within the code.
So for example, instead of initializing the number here, I'm not going to do that.
I'm going to say number.
So that's the private attribute we have here that we're going to use to publish is equal to this get
parameter.
I need to provide the name.
That's number.
And this will give us a parameter object.
You see an Clcp parameter.
From this we can do dot.
And then you can see in C plus plus it's a bit different than in Python.
In C plus plus we have to do as and then the data type that we want.
So this is an integer we do as int and that's a function.
So we use the parentheses.
All right.
So that's going to get the parameter value as an integer.
And then we set that inside the number which is an integer here.
Then I'm going to do the same for the time period I could create a class attribute.
But I'm just going to create a simple variable here double time period.
And that's going to be this get parameter.
We provide the name.
So that's going to be the same as what we've declared here.
And then we do as double because it's a double number.
All right.
So make sure that you match the type here.
And then well once we have declared the parameters and once we have the values in the code we can use
them.
So number is already used here.
And in the timer here.
Instead of doing that.
I'm gonna want a different ways to write that, but I'm going to do STD Chrono duration and use this
syntax with double type.
Okay.
And I'm going to provide the timer period like that.
Right.
So I create a duration with the timer period that I got as a double from the parameters.
And that's it for the code.
So you see you're going to have a bunch of lines to declare and get the parameters at the beginning
of your node constructor.
Then you just use those values just like normal variables and attributes.
Let's save.
And we're going to go back to the terminal.
Let's build in the raster workspace packages.
Select my CP package.
All right.
And let's source here Source bash RC and let's do a Ros to run my Python package and number publisher.
Actually, if I do the C plus plus code, better to launch the C plus plus code.
So I have just run the number publisher from the C plus plus package, and we can see if I do Ros two
topic equal with the number topic.
We have two published every one second, simply because I haven't provided any value for the parameters
so that the default value is going to be used.
Okay.
So once again when you declare a parameter the parameter just exists.
So it means that when you do Ros two param list you will find the parameter here.
Now you see we have more stuff here.
But you can see we have the number and the timer period.
So they exist and they have a default value.
And then you can get them.
And if you don't provide them at runtime the default value is going to be used.
So now I can do that.
And I can do.
So actually let's clear and then Ross args dash p number let's say 20.
And then another dash p time period which is going to be let's say 3.4 because that's the float number.
And you can see for example here I have an error because I have forgotten a colon here.
Okay.
And now it's working.
If I do Rostopic echo it's going to publish the number 20 every 3.4 seconds.
And you can see we have the number here.
So it's working.
And one more thing we can test is what if you provide a parameter that doesn't exist.
So let's say you have a typo or maybe you provide a parameter that.
So this param doesn't exist with a value?
I don't know.
Three.
Let's say you want to provide a string.
So that's also not a type.
What's going to happen.
You see that it's still working.
And if I do Rosetta param list, the parameter that we have provided here doesn't appear.
Okay.
So basically you can provide any parameter that you want even if it's not declared in this case it's
just going to be ignored.
Okay.
If the parameter is not declared in the code and if you don't provide the exact same value, then you
see it's not taken into account anyway.
It doesn't appear in the parameter list.
All right.
So make sure also that when you provide the parameter name and value that this is exactly the same name
that you have set here when you declare the parameter.
All right.
And that's it for parameters for C plus plus.
So now you know how to use them in your nodes.