Ask Your Question

Parameter server gives odd values if read from my C++ code

asked 2019-01-28 07:51:14 -0500

oiseau gravatar image

Hello everyone,

I have an issue with my parameters, when I launched my launch file where I load the yaml file, I can see that parameters from the yaml file are being loaded to the parameter server. When I get this values with rosparam get structure, everything is fine.

In the terminal, I got the first time the good values :

 * /rosdistro: kinetic
 * /rosversion: 1.12.14
 * /structure/angle: 120
 * /structure/bras: 3
 * /structure/longueur: 550

But, when loading inside my code, I get this odd values :

angle : 32767, bras : -429406320, longueur : 0

Part of my code where I get these values from the parameter server.

int bras;
int angle;
int longueur;
if (nh.param("/structure/angle", angle) && nh.param("/structure/bras", bras) && nh.param("/structure/longueur", longueur))
    cout << "angle : " << angle << ", bras : " << bras << ", longueur : " << longueur << endl;
    if (bras*angle > 360)   {
        cout << bras*angle << endl;
        ROS_ERROR("Erreur sur le nombre de bras ou l'angle entre les bras de la structure.");
}else   {
    ROS_ERROR("Erreur chargement paramètres structure.");

I am loading inside this code, some other parameters, but there are fine :

int wantedId;
std::string topicFiducial;
nh.param<std::string>("topicFiducial", topicFiducial, "aruco_detect/fiducial_transforms");
nh.param<int>("wantedId", wantedId, 42);

I am using Kinetic with Ubuntu 16.

edit retag flag offensive close merge delete


It might be because you haven't initialised the variables bras, angle, longeuer

curi_ROS gravatar image curi_ROS  ( 2019-01-28 07:57:36 -0500 )edit

When initialized to zero, I don't get odd values, but I don't get the good values too, only zero. The parameter exits, because I am not getting any error.

oiseau gravatar image oiseau  ( 2019-01-28 08:05:35 -0500 )edit

1 Answer

Sort by » oldest newest most voted

answered 2019-01-28 08:18:46 -0500

Delb gravatar image

updated 2019-01-28 08:25:48 -0500

As @curi_ROS said you haven't initialized the variables. But even if you had you would always get the same values (the ones from the initialization) because you misuse the method param. There are two declaration of this method, from the docs :

template<typename T >
void    param (const std::string &param_name, T &param_val, const T &default_val) const
    Assign value from parameter server, with default.

template<typename T >
T   param (const std::string &param_name, const T &default_val)
    Return value from parameter server, or default if unavailable.

You actually use the second one which will return the value of the parameter or the default value if the parameter isn't find on the parameter server. In your case you have nh.param("/structure/angle", angle) which will return the value of the parameter /structure/angle but you won't store this value. So when you print them you print uninitialized variables.

You wil have to define 3 more variables for the default values and store the output of the function in your previous variables. Moreover, you can't use a condition if to check if the parameter exist or not and you don't need to because this verification is done within the function param (see the source code here). So your code should look like :

int bras_default = 0 //Put a real default value
int angle_default = 0; //same
int longueur_default = 0; //same

int bras = nh.param("/structure/bras", bras_default) ;
int angle = nh.param("/structure/angle", angle_default);
int longueur = nh.param("/structure/longueur", longueur_default);

//This will print the actual values of your parameters
cout << "angle : " << angle << ", bras : " << bras << ", longueur : " << longueur << endl;

The other parameters are successfully loaded (wantedId and topicFiducial) because you use the first declaration of the function param which assign to the second argument the value of the parameter (or its default value if not found).

edit flag offensive delete link more


Thank you, I misunderstood this documentation....

oiseau gravatar image oiseau  ( 2019-01-28 10:22:23 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-01-28 07:51:14 -0500

Seen: 127 times

Last updated: Jan 28 '19