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

Initializing ROS master on http://bat583203glnxa64:39767/.
Initializing global node /matlab_global_node_81432 with NodeURI http://bat583203glnxa64:37143/

Create a parameter tree object to interact with the parameter server. The parameter tree allows you to interact with the parameter server and provides functions such as set, get, del, has and search. Since we started a new parameter server in this example, no parameters are currently stored there.

ptree = rosparam
ptree = 
  ParameterTree with properties:

    AvailableParameters: {}

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.

ans = logical

If has returns 0 (false) as the output, then that means currently 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, '/myrobot/ROBOT_IP','');

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.


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

Suppose you want to get the robot's IP address from the ROBOT_IP parameter because you want to use this IP address to connect to this robot. There are two parameters with ROBOT_IP name in it, but you only want the IP address of myrobot. You can retrieve this parameter value using the get function:

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

Get List of All Parameters

To get the entire list of parameters stored on the parameter server, inspect the AvailableParameters property. The list will contain all the parameters that you added in previous sections.

plist = ptree.AvailableParameters
plist = 7x1 cell array
    {'/MAX_SPEED'          }
    {'/ROBOT_IP'           }
    {'/goal'               }
    {'/myrobot/MAX_SPEED'  }
    {'/myrobot/ROBOT_IP'   }
    {'/myrobot/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

You can delete the parameter from the parameter server using the del function.

Delete the goal parameter.

del(ptree, 'goal');

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

has(ptree, 'goal')
ans = logical

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 array
    {'/myrobot/MAX_SPEED'}    {'/myrobot/ROBOT_IP'}    {'/myrobot/ROBOT_...'}

Shut Down ROS Network

Shut down the ROS master and delete the global node.

Shutting down global node /matlab_global_node_81432 with NodeURI http://bat583203glnxa64:37143/
Shutting down ROS master on http://bat583203glnxa64:39767/.

Next Steps