MATLAB Examples

Access the ROS Parameter Server

Contents

Introduction

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: docid:robotics_examples.example-ROSGettingStartedExample, docid:robotics_examples.example-ROSNetworkingExample

Create Parameter Tree

  • Start the ROS master and parameter server in MATLAB®.
rosinit
Initializing ROS master on http://bat7842maci:64351/.
Initializing global node /matlab_global_node_22860 with NodeURI http://bat7842maci:64355/
  • 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.
has(ptree,'ROBOT_IP')
ans =

  logical

   0

has returns 0 (false) as the output. This means that 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,'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

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

    '192.168.1.100'

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' }
    {'/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

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

   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 array

    {'/myrobot/MAX_SPEED'}    {'/myrobot/ROBOT_IP'}    {'/myrobot/ROBOT_...'}

Shut Down ROS Network

  • Shut down the ROS master and delete the global node.
rosshutdown
Shutting down global node /matlab_global_node_22860 with NodeURI http://bat7842maci:64355/
Shutting down ROS master on http://bat7842maci:64351/.

Next Steps