|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
# :version: 0.1.0 |
|
5
|
|
|
# :copyright: Copyright (C) 2014 Universidad Carlos III de Madrid. |
|
6
|
|
|
# Todos los derechos reservados. |
|
7
|
|
|
# :license LASR_UC3M v1.0, ver LICENCIA.txt |
|
8
|
|
|
|
|
9
|
|
|
# Este programa es software libre: puede redistribuirlo y/o modificarlo |
|
10
|
|
|
# bajo los terminos de la Licencia Academica Social Robotics Lab - UC3M |
|
11
|
|
|
# publicada por la Universidad Carlos III de Madrid, tanto en su version 1.0 |
|
12
|
|
|
# como en una version posterior. |
|
13
|
|
|
|
|
14
|
|
|
# Este programa se distribuye con la intencion de que sea util, |
|
15
|
|
|
# pero SIN NINGUNA GARANTIA. Para mas detalles, consulte la |
|
16
|
|
|
# Licencia Academica Social Robotics Lab - UC3M version 1.0 o posterior. |
|
17
|
|
|
|
|
18
|
|
|
# Usted ha recibido una copia de la Licencia Academica Social |
|
19
|
|
|
# Robotics Lab - UC3M en el fichero LICENCIA.txt, que tambien se encuentra |
|
20
|
|
|
# disponible en <URL a la LASR_UC3Mv1.0>. |
|
21
|
|
|
|
|
22
|
|
|
""" |
|
23
|
|
|
Some utils to ease the use of loading parameters from ROS. |
|
24
|
|
|
|
|
25
|
|
|
:author: Victor Gonzalez Pacheco (victor.gonzalez.pacheco at gmail.com) |
|
26
|
|
|
:date: 2014-04 |
|
27
|
|
|
""" |
|
28
|
|
|
# import roslib |
|
29
|
|
|
# roslib.load_manifest('rospy_utils') |
|
30
|
|
|
import rospy |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
from collections import namedtuple |
|
33
|
|
|
# from itertools import imap |
|
34
|
|
|
|
|
35
|
|
|
from .iter_utils import as_iter |
|
36
|
|
|
|
|
37
|
|
|
Param = namedtuple('Param', 'name value') |
|
38
|
|
|
# Param.__doc__ = """A struct defining a pair param_name, param_value.""" |
|
39
|
|
|
MASTER_PARAMS = ('/roslaunch', '/rosdistro', '/rosversion', '/run_id') |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
class ParamNotFoundError(Exception): |
|
43
|
|
|
|
|
44
|
|
|
"""Error that occurs when is not possible to laod a param.""" |
|
45
|
|
|
pass |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
def __get_parameter(pname): |
|
49
|
|
|
param_full_name = rospy.search_param(pname) |
|
50
|
|
|
if not param_full_name: |
|
51
|
|
|
raise ParamNotFoundError("'{}'".format(pname)) |
|
52
|
|
|
p = Param(param_full_name, rospy.get_param(param_full_name)) |
|
53
|
|
|
rospy.logdebug("Param '{}'. Value: '{}'".format(p.name, p.value)) |
|
54
|
|
|
return p |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
def get_parameters(parameters): |
|
58
|
|
|
""" |
|
59
|
|
|
Yield the params from Parameter Server that are in attribute 'parameters' |
|
60
|
|
|
|
|
61
|
|
|
:input: A list parameters to retrieve from the ParamServer |
|
62
|
|
|
:yields: A Param("name value) namedtuple |
|
63
|
|
|
:raises: ParamNotFoundError in case a parameter Error |
|
64
|
|
|
:raises: ValueError in case parameters is empty |
|
65
|
|
|
""" |
|
66
|
|
|
params = as_iter(parameters) |
|
67
|
|
|
for pname in params: |
|
68
|
|
|
try: |
|
69
|
|
|
yield __get_parameter(pname) |
|
70
|
|
|
except ParamNotFoundError as pnf: |
|
71
|
|
|
pnf.message = pnf.message \ |
|
72
|
|
|
+ "Error when loading parameter '{}'!".format(pname) |
|
73
|
|
|
raise |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
def get_all_user_params(): |
|
77
|
|
|
""" |
|
78
|
|
|
Yield all user parameters loaded in the parameter server. |
|
79
|
|
|
|
|
80
|
|
|
It yields all parameters except the ones that are |
|
81
|
|
|
already loaded by the ROSMaster. |
|
82
|
|
|
|
|
83
|
|
|
Attributes: |
|
84
|
|
|
:param ns: The namespace where to look for. |
|
85
|
|
|
Yields: |
|
86
|
|
|
:return param(param_full_name, param_value): yields A param |
|
87
|
|
|
""" |
|
88
|
|
|
|
|
89
|
|
|
all_param_names = rospy.get_param_names() |
|
90
|
|
|
|
|
91
|
|
|
for pn in all_param_names: |
|
92
|
|
|
if any(map(pn.__contains__, MASTER_PARAMS)): |
|
93
|
|
|
continue # skip parameters that are loaded by rosmaster |
|
94
|
|
|
pvalue = rospy.get_param(pn) |
|
95
|
|
|
yield Param(pn, pvalue) |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
def load_params(params): |
|
99
|
|
|
""" |
|
100
|
|
|
Generator that yields the values of the entered params. |
|
101
|
|
|
|
|
102
|
|
|
:raises: ParamNotFoundError in case a parameter Error |
|
103
|
|
|
:raises: ValueError in case parameters is empty |
|
104
|
|
|
""" |
|
105
|
|
|
for _, pvalue in get_parameters(params): |
|
106
|
|
|
yield pvalue |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
def __attach_parameter(obj, param, create_new=False): |
|
110
|
|
|
""" |
|
111
|
|
|
Modify obj by attaching to it a parameter. |
|
112
|
|
|
|
|
113
|
|
|
:param obj: Object to add the parameters |
|
114
|
|
|
:param param: parameter to add from the parameter server |
|
115
|
|
|
:type param: Param |
|
116
|
|
|
:param create_attribs: (Defautl: False) |
|
117
|
|
|
Wether to add new attributes to the object |
|
118
|
|
|
in case they do not have (see example) |
|
119
|
|
|
""" |
|
120
|
|
|
pname = param.name.rsplit('/', 1)[-1] # Get rid of param namespace |
|
121
|
|
|
if not hasattr(obj, pname) and not create_new: |
|
122
|
|
|
raise AttributeError("'{}' does not have attribute '{}'" |
|
123
|
|
|
.format(obj.__class__, pname)) |
|
124
|
|
|
setattr(obj, pname, param.value) |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
def attach_parameters(obj, params, create_new=False): |
|
128
|
|
|
""" |
|
129
|
|
|
Attach a list of parameters to a node. |
|
130
|
|
|
|
|
131
|
|
|
:param obj: Object to add the parameters |
|
132
|
|
|
:param params: name of the params to add from the parameter server |
|
133
|
|
|
:type params: a string or a list of strings |
|
134
|
|
|
:param create_attribs: (Defautl: False) |
|
135
|
|
|
Wether to add new attributes to the object |
|
136
|
|
|
in case they do not have (see example) |
|
137
|
|
|
:return: obj with new attributes attached |
|
138
|
|
|
|
|
139
|
|
|
Example: |
|
140
|
|
|
|
|
141
|
|
|
>>> rospy.set_param('p', 'hi!') |
|
142
|
|
|
>>> rospy.set_param('p2', 'bye!') |
|
143
|
|
|
>>> my_obj = SomeClass(p='zzz') |
|
144
|
|
|
>>> print my_obj.p, my_obj.p2 |
|
145
|
|
|
'zzz' |
|
146
|
|
|
AttributeError: 'SomeClass' object has no attribute 'p2' |
|
147
|
|
|
>>> my_obj = attach_parameters(my_obj, ['p', p2', create_new=True) |
|
148
|
|
|
>>> print my_obj.p, my_obj.p2 |
|
149
|
|
|
'hi!', 'bye! |
|
150
|
|
|
""" |
|
151
|
|
|
try: |
|
152
|
|
|
params = get_parameters(params) |
|
153
|
|
|
for p in params: |
|
154
|
|
|
__attach_parameter(obj, p, create_new) |
|
155
|
|
|
return obj |
|
156
|
|
|
except ValueError: |
|
157
|
|
|
return obj |
|
158
|
|
|
except Exception: |
|
159
|
|
|
raise |
|
160
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.