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

You got to create a function that reads the parameters.

For instance:

void readParams(const ros::NodeHandle& nh)
{
        std::stringstream ss;
        nh.param<std::string>("ns" , ns , "custom_image");
        nh.param<double>("/"+ns+"/color/a" , color_a , 1.0);
        nh.param<double>("/"+ns+"/color/b" , color_b , 0.0);
        nh.param<double>("/"+ns+"/color/g" , color_g , 0.0);
        nh.param<double>("/"+ns+"/color/r" , color_r , 1.0);
        ss << fixed_frame;
        fixed_frame = ss.str();
}

The example above is reading color parameters, where the following parameters:

ns color_a color_b color_g color_r

are declared in a .yaml file as follows:

# Node namespace
ns: 'custom_image'

# color parameters
color: { 'r': 1.0, 'g': 0.0, 'b': 0.0, 'a': 1.0 }

Thus, nh.param() is a function that receives three arguments:

argument 1 is a string with the name of the parameter as it appears to your ros environment.

argument 2 is a variable that you should have declared prior to call this function. In this example, ns is a string variable, and color_a , color_b , color_g and color_r are double.

argument 3 is a default value that will be parsed to your variable (argument 2) in case the parameter declared (argument 1) could not be found.

In summary, as long as your parameters are visible in your ros environment, this function will read it by looking for argument 1, and store its value to the variable in argument 2.


Now that you have your readParams() function. You only have to call it parsing your node handle.

For instance, in my main() function I did:

    ros::init(argc , argv , "custom_image");
    ROS_INFO("Initializing my node...");

    ros::NodeHandle nh;
    readParams(nh);

You got to create a function that reads the parameters.

For instance:

void readParams(const ros::NodeHandle& nh)
{
        std::stringstream ss;
        nh.param<std::string>("ns" , ns , "custom_image");
        nh.param<double>("/"+ns+"/color/a" , color_a , 1.0);
        nh.param<double>("/"+ns+"/color/b" , color_b , 0.0);
        nh.param<double>("/"+ns+"/color/g" , color_g , 0.0);
        nh.param<double>("/"+ns+"/color/r" , color_r , 1.0);
        ss << fixed_frame;
        fixed_frame = ss.str();
}

The example above is reading color parameters, where the following parameters:

ns color_a color_b color_g color_r

are declared in a .yaml file as follows:

# Node namespace
ns: 'custom_image'

# color parameters
color: { 'r': 1.0, 'g': 0.0, 'b': 0.0, 'a': 1.0 }

Thus, nh.param() is a function that receives three arguments:

argument 1 is a string with the name of the parameter as it appears to your ros environment.

argument 2 is a global variable that you should have declared prior to call this function. In this example, ns is a string variable, and color_a , color_b , color_g and color_r are double.

argument 3 is a default value that will be parsed to your variable (argument 2) in case the parameter declared (argument 1) could not be found.

In summary, as long as your parameters are visible in your ros environment, this function will read it by looking for argument 1, and store its value to the variable in argument 2.


Now that you have your readParams() function. You only have to call it parsing your node handle.

For instance, in my main() function I did:

    ros::init(argc , argv , "custom_image");
    ROS_INFO("Initializing my node...");

    ros::NodeHandle nh;
    readParams(nh);

After that, you can print the variables' values, as they are storing their respective parameters.

You got to create a function that reads the parameters.

For instance:

void readParams(const ros::NodeHandle& nh)
{
        std::stringstream ss;
        nh.param<std::string>("ns" , ns , "custom_image");
        nh.param<double>("/"+ns+"/color/a" , color_a , 1.0);
        nh.param<double>("/"+ns+"/color/b" , color_b , 0.0);
        nh.param<double>("/"+ns+"/color/g" , color_g , 0.0);
        nh.param<double>("/"+ns+"/color/r" , color_r , 1.0);
        ss << fixed_frame;
        fixed_frame = ss.str();
}

The example above is reading color parameters, where the following parameters:

ns color_a color_b color_g color_r

are declared in a .yaml file as follows:

# Node namespace
ns: 'custom_image'

# color parameters
color: { 'r': 1.0, 'g': 0.0, 'b': 0.0, 'a': 1.0 }

Thus, nh.param() is a function that receives three arguments:

argument 1 is a string with the name of the parameter as it appears to your ros environment.

argument 2 is a global variable that you should have declared prior to call this function. In this example, ns is a string variable, and color_a , color_b , color_g and color_r are double.

argument 3 is a default value that will be parsed to your variable (argument 2) in case the parameter declared (argument 1) could not be found.

In summary, as long as your parameters are visible in your ros environment, this function will read it by looking for argument 1, and store its value to the variable in argument 2.


Now that you have your readParams() function. You only have to call it parsing your node handle.

For instance, in my main() function I did:

    ros::init(argc , argv , "custom_image");
    ROS_INFO("Initializing my node...");

    ros::NodeHandle nh;
    readParams(nh);

After that, you can print the variables' values, as they are storing their respective parameters.

(If you need more help, take a look at: http://wiki.ros.org/roscpp/Overview/Parameter%20Server)

You got to create a function that reads the parameters.

For instance:

void readParams(const ros::NodeHandle& nh)
{
        std::stringstream ss;
        nh.param<std::string>("ns" , ns , "custom_image");
        nh.param<double>("/"+ns+"/color/a" , color_a , 1.0);
        nh.param<double>("/"+ns+"/color/b" , color_b , 0.0);
        nh.param<double>("/"+ns+"/color/g" , color_g , 0.0);
        nh.param<double>("/"+ns+"/color/r" , color_r , 1.0);
        ss << fixed_frame;
        fixed_frame = ss.str();
}

The example above is reading color parameters, where the following parameters:

ns color_a color_b color_g ns , color_a ,color_b , color_g , color_r

are declared in a .yaml .yaml file as follows:

# Node namespace
ns: 'custom_image'

# color parameters
color: { 'r': 1.0, 'g': 0.0, 'b': 0.0, 'a': 1.0 }

Thus, nh.param() is a function that receives three arguments:

argument 1 is a string with the name of the parameter as it appears to your ros environment.

argument 2 is a global variable that you should have declared prior to call this function. In this example, ns is a string variable, and color_a , color_b , color_g and color_r are double.

argument 3 is a default value that will be parsed to your variable (argument 2) just in case the parameter declared (argument 1) could not cannot be found.

In summary, as long as your parameters are visible in your ros environment, this function will read it by looking for argument 1, and store its value to the variable in argument 2.


Now that you have your readParams() function. You only have to call it parsing your node handle.

For instance, in my main() function I did:

    ros::init(argc , argv , "custom_image");
    ROS_INFO("Initializing my node...");

    ros::NodeHandle nh;
    readParams(nh);

After that, you can print the variables' values, as they are storing their respective parameters.

(If you need more help, take a look at: http://wiki.ros.org/roscpp/Overview/Parameter%20Server)

You got to create a function that reads the parameters.

For instance:

void readParams(const ros::NodeHandle& nh)
{
        std::stringstream ss;
        nh.param<std::string>("ns" , ns , "custom_image");
        nh.param<double>("/"+ns+"/color/a" , color_a , 1.0);
        nh.param<double>("/"+ns+"/color/b" , color_b , 0.0);
        nh.param<double>("/"+ns+"/color/g" , color_g , 0.0);
        nh.param<double>("/"+ns+"/color/r" , color_r , 1.0);
        ss << fixed_frame;
        fixed_frame = ss.str();
}

The example above is reading color parameters, where the following parameters:

ns , color_a ,color_b , color_g , color_r

are declared in a .yaml file as follows:

# Node namespace
ns: 'custom_image'

# color parameters
color: { 'r': 1.0, 'g': 0.0, 'b': 0.0, 'a': 1.0 }

Thus, nh.param() is a function that receives three arguments:

argument 1 is a string with the name of the parameter as it appears to your ros environment.

argument 2 is a global variable that you should have declared prior to call this function. In this example, ns is a string variable, and color_a , color_b , color_g and color_r are double.

argument 3 is a default value that will be parsed to your variable (argument 2) just in case the parameter declared (argument 1) cannot be found.

In summary, as long as your parameters are visible in your ros environment, this function will read it by looking for argument 1, and store its value to the variable in argument 2.


Now that you have your readParams() function. You only have to call it parsing your node handle.

For instance, in my main() function I did:

    ros::init(argc , argv , "custom_image");
    ROS_INFO("Initializing my node...");

    ros::NodeHandle nh;
    readParams(nh);

After that, you can print the variables' values, as they are storing their respective parameters.

(If you need more help, take a look at: http://wiki.ros.org/roscpp/Overview/Parameter%20Server)