ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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) ;
int angle = nh.param("/structure/angle", angle);
int longueur = nh.param("/structure/longueur", longueur);

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

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) bras_default) ;
int angle = nh.param("/structure/angle", angle);
angle_default);
int longueur = nh.param("/structure/longueur", longueur);
longueur_default);

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

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