rqt_roscomm_util.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Willow Garage, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 #
33 # Author: Isaac Saito
34 
35 import os
36 
37 import genmsg
38 import roslaunch
39 from roslaunch import RLException
40 import rospkg
41 import rospy
42 import rostopic
43 
44 
45 class RqtRoscommUtil(object):
46 
47  @staticmethod
48  def load_parameters(config, caller_id):
49  """
50  Load parameters onto the parameter server.
51 
52  Copied from ROSLaunchRunner.
53 
54  @type config: roslaunch.config.ROSLaunchConfig
55  @raise RLException:
56  """
57 
58  # XMLRPC proxy for communicating with master, 'xmlrpclib.ServerProxy'
59  param_server = config.master.get()
60 
61  param = None
62  try:
63  # multi-call style xmlrpc
64  # According to API doc, get_multi() returns
65  # multicall XMLRPC proxy for communicating with master,
66  # "xmlrpclib.MultiCall"
67  param_server_multi = config.master.get_multi()
68 
69  # clear specified parameter namespaces
70  # 2468 unify clear params to prevent error
71  for param in roslaunch.launch._unify_clear_params(config.clear_params):
72  if param_server.hasParam(caller_id, param)[2]:
73  param_server_multi.deleteParam(caller_id, param)
74  r = param_server_multi()
75  for code, msg, _ in r:
76  if code != 1:
77  raise RLException("Failed to clear parameter {}: ".format(msg))
78  except RLException:
79  raise
80  except Exception as e:
81  rospy.logerr("load_parameters: unable to set params " +
82  "(last param was [{}]): {}".format(param, e))
83  raise # re-raise as this is fatal
84 
85  try:
86  # multi-call objects are not reusable
87  param_server_multi = config.master.get_multi()
88  for param in config.params.values():
89  # suppressing this as it causes too much spam
90  # printlog("setting parameter [%s]"%param.key)
91  param_server_multi.setParam(caller_id, param.key, param.value)
92  r = param_server_multi()
93  for code, msg, _ in r:
94  if code != 1:
95  raise RLException("Failed to set parameter: %s" % (msg))
96  except RLException:
97  raise
98  except Exception as e:
99  print("load_parameters: unable to set params (last param was " +
100  "[%s]): %s" % (param, e))
101  raise # re-raise as this is fatal
102  rospy.logdebug("... load_parameters complete")
103 
104  @staticmethod
105  def iterate_packages(subdir):
106  """
107  Iterator for packages that contain the given subdir.
108 
109  This method is generalizing rosmsg.iterate_packages.
110 
111  @param subdir: eg. 'launch', 'msg', 'srv', 'action'
112  @type subdir: str
113  @raise ValueError:
114  """
115  if subdir == None or subdir == '':
116  raise ValueError('Invalid package subdir = {}'.format(subdir))
117 
118  rospack = rospkg.RosPack()
119 
120  pkgs = rospack.list()
121  rospy.logdebug('pkgs={}'.format(pkgs))
122  for p in pkgs:
123  d = os.path.join(rospack.get_path(p), subdir)
124  rospy.logdebug('rospack dir={}'.format(d))
125  if os.path.isdir(d):
126  yield p, d
127 
128  @staticmethod
129  def list_files(package, subdir, file_extension='.launch'):
130  """
131  #TODO: Come up with better name of the method.
132 
133  Taken from rosmsg.
134  Lists files contained in the specified package
135 
136  @param package: package name, ``str``
137  @param file_extension: Defaults to '.launch', ``str``
138  :returns: list of msgs/srv in package, ``[str]``
139  """
140  if subdir == None or subdir == '':
141  raise ValueError('Invalid package subdir = {}'.format(subdir))
142 
143  rospack = rospkg.RosPack()
144 
145  path = os.path.join(rospack.get_path(package), subdir)
146 
147  return [genmsg.resource_name(package, t) for t in RqtRoscommUtil._list_types(path, file_extension)]
148 
149  @staticmethod
150  def _list_types(path, ext):
151  """
152  Taken from rosmsg
153 
154  List all messages in the specified package
155  :param package str: name of package to search
156  :param include_depends bool: if True, will also list messages in
157  package dependencies.
158  :returns [str]: message type names
159  """
160  types = RqtRoscommUtil._list_resources(path,
161  RqtRoscommUtil._msg_filter(ext))
162 
163  result = [x for x in types]
164  # result = [x[:-len(ext)] for x in types] # Remove extension
165 
166  result.sort()
167  return result
168 
169  @staticmethod
170  def _list_resources(path, rfilter=os.path.isfile):
171  """
172  Taken from rosmsg._list_resources
173 
174  List resources in a package directory within a particular
175  subdirectory. This is useful for listing messages, services, etc...
176  :param rfilter: resource filter function that returns true if filename
177  is the desired resource type, ``fn(filename)->bool``
178  """
179  resources = []
180  if os.path.isdir(path):
181  resources = [f for f
182  in os.listdir(path) if rfilter(os.path.join(path, f))]
183  else:
184  resources = []
185  return resources
186 
187  @staticmethod
188  def _msg_filter(ext):
189  """
190  Taken from rosmsg._msg_filter
191  """
192  def mfilter(f):
193  """
194  Predicate for filtering directory list. matches message files
195  :param f: filename, ``str``
196  """
197  return os.path.isfile(f) and f.endswith(ext)
198  return mfilter
199 
200  @staticmethod
202  """
203  @rtype: bool
204  """
205  try:
206  # Checkif rosmaster is running or not.
207  rostopic.get_topic_class('/rosout')
208  return True
209  except rostopic.ROSTopicIOException as e:
210  return False
rqt_py_common.rqt_roscomm_util.RqtRoscommUtil.iterate_packages
def iterate_packages(subdir)
Definition: rqt_roscomm_util.py:105
rqt_py_common.rqt_roscomm_util.RqtRoscommUtil
Definition: rqt_roscomm_util.py:45
rqt_py_common.rqt_roscomm_util.RqtRoscommUtil._msg_filter
def _msg_filter(ext)
Definition: rqt_roscomm_util.py:188
rqt_py_common.rqt_roscomm_util.RqtRoscommUtil._list_resources
def _list_resources(path, rfilter=os.path.isfile)
Definition: rqt_roscomm_util.py:170
rqt_py_common.rqt_roscomm_util.RqtRoscommUtil._list_types
def _list_types(path, ext)
Definition: rqt_roscomm_util.py:150
rqt_py_common.rqt_roscomm_util.RqtRoscommUtil.list_files
def list_files(package, subdir, file_extension='.launch')
Definition: rqt_roscomm_util.py:129
rqt_py_common.rqt_roscomm_util.RqtRoscommUtil.load_parameters
def load_parameters(config, caller_id)
Definition: rqt_roscomm_util.py:48
rqt_py_common.rqt_roscomm_util.RqtRoscommUtil.is_roscore_running
def is_roscore_running()
Definition: rqt_roscomm_util.py:201


rqt_py_common
Author(s): Dorian Scholz , Isaac Saito, Dirk Thomas
autogenerated on Fri Jul 12 2024 02:31:13