Note: This tutorial assumes that you have completed the previous tutorials: Writing a service and client.
(!) 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.

rospyでパラメータを使う

Description: このチュートリアルはrospyの中でParametersをどのようにして取得し設定するかの様々な方法について扱います。rospyNodeからのパラメータの取得、設定、削除は、とてもシンプルです。

Tutorial Level: BEGINNER

Next Tutorial: Logging with rospy

You may wish to view the "param_talker" tutorial in the rospy_tutorials Package. It uses a roslaunch file to initialize some Parameters and then runs a rospy Node that reads from those Parameters.

$ roscd rospy_tutorials/006_parameters
$ roslaunch param_talker.launch

Following explains what you see when you run the command above.

Parameter Types

You can use integers, floats, strings and booleans as Parameter values. You can also use lists and dictionaries of these types, though dictionaries have additional meaning that can be very useful.

Dictionaries are equivalent to ROS Namespaces. They are an effective way of grouping similar Parameters together so that you can get and set them atomically. For example, if you had a set of gains:

/gains/P = 1.0
/gains/I = 2.0
/gains/D = 3.0

In rospy, you could set and access those values individually or together as a dictionary. The Parameter /gains has the Python dictionary value

{'P': 1.0, 'I': 2.0, 'D': 3.0}

Getting, Setting, and Deleting Parameters

Getting a parameter is as simple as calling rospy.get_param(param_name):

# get a global parameter
rospy.get_param('/global_param_name')

# get a parameter from our parent namespace
rospy.get_param('param_name')

# get a parameter from our private namespace
rospy.get_param('~private_param_name')

You can also specify a default value if the parameter doesn't exist:

rospy.get_param('foo', 'default_value')

Similarly, you set a parameter by calling rospy.set_param(param_name, param_value):

rospy.set_param('some_numbers', [1., 2., 3., 4.])
rospy.set_param('truth', True)
rospy.set_param('~private_bar', 1+2)

You can delete parameter by calling rospy.delete_param(param_name):

rospy.delete_param('param_name')

If you don't know whether or not a parameter exists, you can call rospy.has_param(param_name):

if rospy.has_param('to_delete'):
    rospy.delete_param('to_delete')

Resolving Parameter Names

Names in ROS can be remapped and your Node may get pushed into a namespace. rospy does most of the work for you by automatically resolving any names you pass into get_param, set_param, etc... However, for debugging purposes, you may wish to print out the names of the Parameters that you are accessing.

In order to determine what the actual name of a Parameter is, you should call rospy.resolve_name(name), e.g.:

value = rospy.get_param('~foo')
rospy.loginfo('Parameter %s has value %s', rospy.resolve_name('~foo'), value)

rospy.resolve_name() will apply any remapping rules and also figure out your Node's namespace.

Searching for a Parameter

In ROS, you can search for a Parameter if you don't know what namespace it is set in. This search starts in the Node's private namespace and proceeds upwards to the global namespace. In order to use this search API in rospy, you can rospy.search_param(param_name) to find the resolved Parameter name. You can then get and set the Parameter normally, e.g.

full_param_name = rospy.search_param('foo')
param_value = rospy.get_param(full_param_name)

Next tutorial: ja/rospy_tutorials/Tutorials/numpy.

Wiki: ja/rospy_tutorials/Tutorials/Parameters (last edited 2014-09-22 16:17:22 by Moirai)