Main Content

Access the ROS Parameter Server

This example explores how to add and retrieve parameters on the ROS parameter server. The parameter server usually runs on the same device that launches the ROS master. The parameters are accessible globally over the ROS network and can be used to store static data such as configuration parameters. Supported data types include strings, integers, doubles, logicals, and cell arrays.

Prerequisites: Get Started with ROS, Connect to a ROS Network

Create Parameter Tree

Start the ROS master and parameter server in MATLAB.

rosinit
Launching ROS Core...
Done in 0.88355 seconds.
Initializing ROS master on http://172.20.220.163:54734.
Initializing global node /matlab_global_node_34615 with NodeURI http://dcc821001glnxa64:44727/ and MasterURI http://localhost:54734.

Create a parameter tree object to interact with the parameter server. Use the parameter tree to interact with the parameter server and call functions such as set, get, del, has and search. Create a new parameter server using rosparam.

ptree = rosparam
ptree = 
  ParameterTree with properties:

    AvailableParameters: {0x1 cell}

Add New Parameters

To set a parameter for the robot IP address, use the parameter name ROBOT_IP. Check if a parameter with the same name already exists. Use the has function.

has(ptree,'ROBOT_IP')
ans = logical
   0

If has returns 0 (false) as the output, then the ROBOT_IP name could not be found on the parameter server.

Add some parameters indicating a robot's IP address to the parameter server. Use the set function for this purpose.

set(ptree,'ROBOT_IP','192.168.1.1');
set(ptree,'/myrobot/ROBOT_IP','192.168.1.100');

The ROBOT_IP parameters are now available to all nodes connected to this ROS master. You can specify parameters within a namespace. For example, the /myrobot/ROBOT_IP parameter is within the /myrobot namespace in this example.

Set more parameters with different data types.

set(ptree,'MAX_SPEED',1.5);

Use a cell array as an input to the set function. Set a parameter that has the goal coordinates {x, y, z} for the robot.

set(ptree,'goal',{5.0,2.0,0.0});

Set additional parameters to populate the parameter server.

set(ptree,'/myrobot/ROBOT_NAME','TURTLE');
set(ptree,'/myrobot/MAX_SPEED',1.5);
set(ptree,'/newrobot/ROBOT_NAME','NEW_TURTLE');

Get Parameter Values

Retrieve the robot's IP address from the ROBOT_IP parameter in the /myrobot namespace using the get function:

robotIP = get(ptree,'/myrobot/ROBOT_IP')
robotIP = 
'192.168.1.100'

Get List of All Parameters

To get the entire list of parameters stored on the parameter server, use dot notation to access the AvailableParameters property. The list contains all the parameters that you added in previous sections.

plist = ptree.AvailableParameters
plist = 7x1 cell
    {'/MAX_SPEED'          }
    {'/ROBOT_IP'           }
    {'/goal'               }
    {'/myrobot/MAX_SPEED'  }
    {'/myrobot/ROBOT_IP'   }
    {'/myrobot/ROBOT_NAME' }
    {'/newrobot/ROBOT_NAME'}

Modify Existing Parameters

You can also use the set function to change parameter values. Note that the modification of a parameter is irreversible, since the parameter server will simply overwrite the parameter with the new value. You can verify if a parameter already exists by using the has function.

Modify the MAX_SPEED parameter:

set(ptree,'MAX_SPEED',1.0);

The modified value can have a different data type from a previously assigned value. For example, the value of the MAX_SPEED parameter is currently of type double. Set a string value for the MAX_SPEED parameter:

set(ptree,'MAX_SPEED','none');

Delete Parameters

Use the del function to delete a parameter from the parameter server.

Delete the goal parameter.

del(ptree,'goal');

Check if the goal parameter has been deleted. Use the has function.

has(ptree,'goal')
ans = logical
   0

The output is 0 (false), which means the parameter was deleted from the parameter server.

Search Parameters

Search for all the parameters that contain 'myrobot' using the search command:

results = search(ptree,'myrobot')
results = 1x3 cell
    {'/myrobot/MAX_SPEED'}    {'/myrobot/ROBOT_IP'}    {'/myrobot/ROBOT_NAME'}

Shut Down the ROS Network

Shut down the ROS master and delete the global node.

rosshutdown
Shutting down global node /matlab_global_node_34615 with NodeURI http://dcc821001glnxa64:44727/ and MasterURI http://localhost:54734.
Shutting down ROS master on http://172.20.220.163:54734.

Next Steps

Related Topics