0
$\begingroup$

Rosanswers logo

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 :

PARAMETERS
 * /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.


Originally posted by oiseau on ROS Answers with karma: 32 on 2019-01-28

Post score: 0


Original comments

Comment by curi_ROS on 2019-01-28:
It might be because you haven't initialised the variables bras, angle, longeuer

Comment by oiseau on 2019-01-28:
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.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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).


Originally posted by Delb with karma: 3907 on 2019-01-28

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by oiseau on 2019-01-28:
Thank you, I misunderstood this documentation....

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.