Note: This tutorial assumes that you have completed the previous tutorials: understanding ROS topics.
(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Understanding ROS Services and Parameters

Description: This tutorial introduces ROS services, and parameters as well as using the rosservice and rosparam commandline tools.

Tutorial Level: BEGINNER

Next Tutorial: Using rxconsole and roslaunch

如果上次的`乌龟模拟_节点'还没有被关掉,我们可以利用它来看下它都有哪些services:

ROS Services

Services是另一种与其它节点通信的方法。Services允许发送一个请求,并且 收到一个’’’回馈'’’。

使用 rosservice

rosservice可以被很方便的通过ROS客户端/服务端架构使用services。 rosservice提供很多有用的命令,可以用于topics,如下:

用法:

rosservice list         列出正在运行的services
rosservice call         调用一个service,并允许使用参数
rosservice type         列出service数据类型
rosservice find         搜索某一类型的service
rosservice uri          service ROSRPC uri链接地址

rosservice list

$ rosservice list

list这个命令可以显示出那个乌龟模拟所提供的就个services,分别为:reset,clear, spawn, kill, turtle1/set_pen, /turtle1/teleport_absolute, /turtle1/teleport_relative, turtlesim/get_loggers, and turtlesim/set_logger_level. 额外还可以看到两个与rosout相关的services,/rosout/get_loggers and /rosout/set_logger_level.

  • /clear
    /kill
    /reset
    /rosout/get_loggers
    /rosout/set_logger_level
    /spawn
    /teleop_turtle/get_loggers
    /teleop_turtle/set_logger_level
    /turtle1/set_pen
    /turtle1/teleport_absolute
    /turtle1/teleport_relative
    /turtlesim/get_loggers
    /turtlesim/set_logger_level

现在来仔细看看clearservice的数据类型。使用rosservice type命令:

rosservice type

用法:

rosservice type [service]

现在看看clear是什么类型: Let's find out what type the clear service is:

$ rosservice type clear
  • std_srvs/Empty

这个service是空的,也就是说这个service不需要任何参数(比如:它不会发送任何数据到service提供方,也不会收到任何回应。)。使用这个service,用rosservice call:

rosservice call

用法:

rosservice call [service] [args]

现在我们来调用这个clear service,注意参数为空。

$ rosservice call clear

这正是我们所期望的,它将这个`turtlesim_node'背景清空了

  • turtlesim.png

现在看一下有参数的情况。首先看看spawn的数据类型。

$ rosservice type spawn| rossrv show
  • float32 x
    float32 y
    float32 theta
    string name
    ---
    string name

This service let's us spawn a new turtle at a given location and orientation. The name field is optional, so let's not give our new turtle a name and let turtlesim create one for us.

$ rosservice call spawn 2 2 0.2 ""

The service call returns with the name of the newly created turtle

  • name: turtle2

Now our turtlesim should look like this:

  • turtle(service).png

Using rosparam

rosparam allows you to store and manipulate data on the ROS Parameter Server. The Parameter Server can store integers, floats, boolean, dictionaries, and lists. rosparam uses the YAML markup language for syntax. In simple cases, YAML looks very natural: 1 is an integer, 1.0 is a float, one is a string, true is a boolean, [1, 2, 3] is a list of integers, and {a: b, c: d} is a dictionary. rosparam has many commands that can be used on topics, as shown below:

Usage:

rosparam set            set parameter
rosparam get            get parameter
rosparam load           load parameters from file
rosparam dump           dump parameters to file
rosparam delete         delete parameter
rosparam list           list parameter names

Let's look at what parameters are currently on the param server:

rosparam list

$ rosparam list

Here we can see that the turtlesim node has three parameters on the param server for background color:

  • /background_b
    /background_g
    /background_r
    /roslaunch/uris/aqy:51932
    /run_id

Let's change one of the parameter values using rosparam set:

rosparam set and rosparam set

Usage:

rosparam set [param_name]
rosparam get [param_name]

Here will change the red channel of the background color:

$ rosparam set background_r 150

This changes the parameter value, now we have to call the clear service for the parameter change to take effect:

$ rosservice call clear

Now our turtlesim looks like this:

  • turtle(param).png

Now let's look at the values of other parameters on the param server. Let's get the value of the green background channel:

$ rosparam get background_g 
  • 144

We can also use rosparam get / to show us the contents of the entire Parameter Server.

$ rosparam get /
  • background_b: 255
    background_g: 144
    background_r: 150
    roslaunch:
      uris: {'aqy:51932': 'http://aqy:51932/'}
    run_id: e07ea71e-98df-11de-8875-001b21201aa8

You may wish to store this in a file so that you can reload it at another time. This is easy using rosparam:

rosparam dump and rosparam load

Usage:

rosparam dump [file_name]
rosparam load [file_name] [namespace]

Here we write all the parameters to the file params.yaml

$ rosparam dump params.yaml

You can even load these yaml files into new namespaces, e.g. copy:

$ rosparam load params.yaml copy
$ rosparam get copy/background_b
  • 255

Now that you understand how ROS services and params work, let's try using rxconsole and roslaunch

Wiki: roschina/教程/UnderstandingServicesParams (last edited 2012-03-10 20:30:14 by jizecn)