Getting Started with ROS on Embedded Systems - User Guide - C++ - Parameters

From RidgeRun Developer Wiki





Previous: User Guide/C++/Launch_files Index Next: Examples




Introduction

This wiki is based on: http://wiki.ros.org/Parameter%20Server

ROS parameter server is a shared dictionary between nodes used to retrieve parameters at runtime, normally used for static data on configuration. It is meant to be global so that tools can read and modify it.

Parameters

Parameters follow the same naming convention from ROS, in a hierarchical way so that these are accessed as a tree. As an example from the ROS website, you can have the following parameters:

/camera/left/name: leftcamera
/camera/left/exposure: 1
/camera/right/name: rightcamera
/camera/right/exposure: 1.1

You can also get one dictionary with depth 1 from /camera/left:

name: leftcamera
exposure: 1

Or depth 2 from the whole cameras:

left: { name: leftcamera, exposure: 1 }
right: { name: rightcamera, exposure: 1.1 }

Loading parameters

In the roslaunch example from the launch section here

This will load all the yaml files specified into the parameter server.

Reading from C++

To read the parameters from C++, you can use the following code:

template <typename T>
bool GetParamWithWarning(const ros::NodeHandle& node_handle, const std::string& param, T& dest) {
    bool res = false;
    std::string actual_param;
    if (node_handle.searchParam(param, actual_param)) {
        dest = node_handle.param<T>(actual_param, T());
        res = true;
    } else {
        std::cout << "[Warning] Missing parameter: " << param.c_str() << std::endl;
    }
    return res;
}

This will search for the param using the node handle, and store the result in the destination.

Previous: User Guide/C++/Launch_files Index Next: Examples