param_talker.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2008, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 #
34 # Revision $Id: listener.py 5263 2009-07-17 23:30:38Z sfkwc $
35 
36 ## Simple talker demo that listens to std_msgs/Strings published
37 ## to the 'chatter' topic
38 
39 import rospy
40 from std_msgs.msg import String
41 
43  rospy.init_node('param_talker')
44 
45  # Fetch values from the Parameter Server. In this example, we fetch
46  # parameters from three different namespaces:
47  #
48  # 1) global (/global_example)
49  # 2) parent (/foo/utterance)
50  # 3) private (/foo/param_talker/topic_name)
51 
52  # fetch a /global parameter
53  global_example = rospy.get_param("/global_example")
54  rospy.loginfo("%s is %s", rospy.resolve_name('/global_example'), global_example)
55 
56  # fetch the utterance parameter from our parent namespace
57  utterance = rospy.get_param('utterance')
58  rospy.loginfo("%s is %s", rospy.resolve_name('utterance'), utterance)
59 
60  # fetch topic_name from the ~private namespace
61  topic_name = rospy.get_param('~topic_name')
62  rospy.loginfo("%s is %s", rospy.resolve_name('~topic_name'), topic_name)
63 
64  # fetch a parameter, using 'default_value' if it doesn't exist
65  default_param = rospy.get_param('default_param', 'default_value')
66  rospy.loginfo('%s is %s', rospy.resolve_name('default_param'), default_param)
67 
68  # fetch a group (dictionary) of parameters
69  gains = rospy.get_param('gains')
70  p, i, d = gains['P'], gains['I'], gains['D']
71  rospy.loginfo("gains are %s, %s, %s", p, i, d)
72 
73  # set some parameters
74  rospy.loginfo('setting parameters...')
75  rospy.set_param('list_of_floats', [1., 2., 3., 4.])
76  rospy.set_param('bool_True', True)
77  rospy.set_param('~private_bar', 1+2)
78  rospy.set_param('to_delete', 'baz')
79  rospy.loginfo('...parameters have been set')
80 
81  # delete a parameter
82  if rospy.has_param('to_delete'):
83  rospy.delete_param('to_delete')
84  rospy.loginfo("deleted %s parameter"%rospy.resolve_name('to_delete'))
85  else:
86  rospy.loginfo('parameter %s was already deleted'%rospy.resolve_name('to_delete'))
87 
88  # search for a parameter
89  param_name = rospy.search_param('global_example')
90  rospy.loginfo('found global_example parameter under key: %s'%param_name)
91 
92  # publish the value of utterance repeatedly
93  pub = rospy.Publisher(topic_name, String, queue_size=10)
94  while not rospy.is_shutdown():
95  pub.publish(utterance)
96  rospy.loginfo(utterance)
97  rospy.sleep(1)
98 
99 if __name__ == '__main__':
100  try:
101  param_talker()
102  except rospy.ROSInterruptException: pass
103 
def param_talker()
Definition: param_talker.py:42


rospy_tutorials
Author(s): Ken Conley
autogenerated on Fri Jun 7 2019 22:01:45