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
Start the ROS master and parameter server in MATLAB®.
rosinit
Initializing ROS master on http://bat5216glnxa64:44281/. Initializing global node /matlab_global_node_22332 with NodeURI http://bat5216glnxa64:36871/
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: {}
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 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,'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');
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'
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'}
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');
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 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 the ROS master and delete the global node.
rosshutdown
Shutting down global node /matlab_global_node_22332 with NodeURI http://bat5216glnxa64:36871/ Shutting down ROS master on http://bat5216glnxa64:44281/.
For application examples, see the Get Started with Gazebo and a Simulated TurtleBot or Get Started with a Real TurtleBot examples.