主要内容

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:开始使用ROS,Connect to a ROS Network

创建参数树

启动MATLAB中的ROS主站和参数服务器。

罗斯尼特
Launching ROS Core... Done in 0.93826 seconds. Initializing ROS master on http://172.29.207.162:59979. Initializing global node /matlab_global_node_66770 with NodeURI http://dcc434238glnxa64:34371/ and MasterURI http://localhost:59979.

创建一个参数树对象以与参数服务器进行交互。使用参数树与参数服务器交互并呼叫函数,如set,get,del,hassearch. Create a new parameter server usingrosparam.

ptree = rosparam
ptree = ParameterTree with properties: AvailableParameters: {0x1 cell}

Add New Parameters

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

有(Ptree,'ROBOT_IP')
ans =logical0

Ifhasreturns0(false)作为输出,那么robot_ip.在参数服务器上找不到名称。

添加一些参数,指示参数服务器的机器人的IP地址。使用set为此目的的功能。

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

Therobot_ip.现在可以使用连接到此ROS主机的所有节点使用参数。您可以在命名空间中指定参数。例如/ myrobot / robot_ipparameter is within the/myrobot命名空间在此示例中。

Set more parameters with different data types.

set(ptree,'MAX_SPEED',1.5);

使用单元格数组作为输入setfunction. Set a parameter that has the goal coordinates {x, y, z} for the robot.

set(ptree,'目标',{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');

获取参数值

Retrieve the robot's IP address from the ROBOT_IP parameter in the/myrobotnamespace using thegetfunction:

robotip = get(ptree,'/myrobot/ROBOT_IP')
Robotip ='192.168.1.100'

Get List of All Parameters

要获取存储在参数服务器上的整个参数列表,请使用点表示法访问AvailableParameters财产。该列表包含在上一节中添加的所有参数。

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 thesetfunction 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 thehasfunction.

Modify the最大速度parameter:

set(ptree,'MAX_SPEED',1.0);

修改后的值可以有不同的数据类型from a previously assigned value. For example, the value of the最大速度parameter is currently of type double. Set a string value for the最大速度parameter:

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

Delete Parameters

使用delfunction to delete a parameter from the parameter server.

删除goalparameter.

德尔(Ptree,'目标');

检查是否goalparameter has been deleted. Use thehasfunction.

有(Ptree,'目标')
ans =logical0

输出是0(false), which means the parameter was deleted from the parameter server.

搜索参数

Search for all the parameters that contain'myrobot'使用searchcommand:

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

关闭ROS网络

Shut down the ROS master and delete the global node.

rosshutdown
Shutting down global node /matlab_global_node_66770 with NodeURI http://dcc434238glnxa64:34371/ and MasterURI http://localhost:59979. Shutting down ROS master on http://172.29.207.162:59979.

Next Steps