2Stand-ins for `rospy.client` in ROS2.
4------------------------------------------------------------------------------
5This file is part of rosros - simple unified interface to ROS1 / ROS2.
6Released under the BSD License.
11------------------------------------------------------------------------------
13## @namespace rosros.rospify.client
18import rclpy.exceptions
23from . import exceptions
24from . msproxy import MasterProxy
25from . names import get_name, get_namespace
37_ROSPY_LOG_LEVEL_TO_PY_LEVEL = {
47class _unspecified:
pass
50def delete_param(param_name):
52 Deletes a parameter on the node.
54 @param param_name parameter name
56 @throws KeyError
if parameter
is not set
58 ros2.delete_param(param_name)
61def has_param(param_name):
63 Test if parameter exists on the node.
65 @param param_name parameter name
67 return ros2.has_param(param_name)
70def get_master(env=None):
72 Returns a partial stand-in of `rospy.MasterProxy`.
74 @param env ignored (ROS1 compatbility stand-
in)
79def get_param(param_name, default=_unspecified):
81 Returns a parameter value from the node.
83 @param default default value to
return if key
is not set
84 @return parameter value
86 @throws KeyError
if value
not set
and default
is not given
88 if default
is _unspecified
and not ros2.has_param(param_name):
89 raise KeyError(param_name)
90 return ros2.get_param(param_name, default)
94get_param_cached = get_param
98 """Returns a list of node parameter names, with absolute namespace."""
99 ns = util.namejoin(get_namespace(), get_name())
100 return [util.namejoin(ns, x)
for x
in ros2.get_param_names()]
103def get_published_topics(namespace="/"):
105 Returns a list of published topics.
107 @return list of topic
and type names,
as [[topic, type], ]
109 @throws ROSException
if retrieving topics fails
113 except Exception
as e:
117def init_node(name, argv=None, anonymous=False, log_level=None,
118 disable_rostime=None, disable_rosout=False,
119 disable_signals=None, xmlrpc_port=None, tcpros_port=None):
121 Inits ROS2 and creates ROS2 node.
124 @param name node name,
with no namespace
125 @param argv list of command-line arguments
for the node
126 @param anonymous whether to auto-generate a unique name
for the node,
127 using the given name
as base
128 @param log_level log level to set
for the node logger,
129 one of `rospify.DEBUG .INFO .WARN .ERROR .FATAL`
130 @param disable_rostime ignored (ROS1 compatibility stand-
in)
131 @param disable_rosout whether to suppress auto-publication of rosout
132 @param disable_signals ignored (ROS1 compatibility stand-
in)
133 @param xmlrpc_port ignored (ROS1 compatibility stand-
in)
134 @param tcpros_port ignored (ROS1 compatibility stand-
in)
136 @throws ROSInitException
if initialization/registration fails
137 @throws ValueError
if name
is invalid
138 @return `rclpy.node.Node`
141 raise ValueError(
"name must not be empty")
143 raise ValueError(
"namespaces are not allowed in node names")
145 log_level = _ROSPY_LOG_LEVEL_TO_PY_LEVEL.get(log_level, log_level)
146 try:
return ros2.init_node(name, argv, anonymous=anonymous, log_level=log_level,
147 enable_rosout=
not disable_rosout)
148 except Exception
as e:
152def myargv(argv=None):
154 Returns command-line arguments with remappings removed.
156 @param argv arguments to use
if not `sys.argv`
157 @return list of arguments
with remappings removed
159 REMAP_RGX = re.compile(r"^([\~\/a-z]|_|__)[\w\/]*:=.*", re.I)
160 argv = list(sys.argv
if argv
is None else argv)
161 while "--remap" in argv:
162 idx = argv.index(
"--remap")
164 while len(argv) > idx
and REMAP_RGX.match(argv[idx]):
170 """Registers function to be called on shutdown, after node has been torn down."""
174def search_param(param_name):
176 Search for a parameter on the node.
178 @param param_name parameter name
179 @return key of matching parameter
or None if no matching parameter
181 ns = util.namejoin(get_namespace(), get_name())
182 if param_name.startswith(ns):
183 param_name = param_name[len(ns):]
184 return util.namejoin(ns, param_name)
if param_name
in ros2.get_param_names()
else None
187def set_param(param_name, param_value):
189 Set a parameter on the node.
191 If param_value is a dictionary, it will be treated
as a parameter tree,
192 where param_name
is the namespace. For example:::
193 {
"x": 1,
"y": 2,
"sub": {
"z": 3}}
194 will set param_name.x=1, param_name.y=2,
and param_name.sub.z=3.
195 Furthermore, it will replace all existing parameters
in the
196 param_name namespace
with the parameters
in param_value. You must
197 set parameters individually
if you wish to perform a union update.
199 @param param_name parameter name
200 @param param_value parameter value
202 @throws ROSException
if setting parameter failed
205 if isinstance(param_value, dict):
206 ns =
"%s." % param_name.strip(
".")
207 for n
in [x
for x
in ros2.get_param_names()
if x.startswith(ns)]:
209 for k, v
in util.flatten_dict({param_name: param_value}).items():
211 else: ros2.set_param(param_name, param_value)
212 except Exception
as e:
217 """Spins ROS2 node forever."""
221def wait_for_message(topic, topic_type, timeout=None):
223 Receive one message from topic.
225 This will create a new subscription to the topic, receive one message, then unsubscribe.
227 @param topic name of topic
228 @param topic_type topic type
class
229 @param timeout timeout time
in seconds
or ROS Duration
232 @throws ROSException
if specified timeout
is exceeded
233 @throws ROSInterruptException
if shutdown interrupts wait
235 timeout = ros2.to_sec(timeout) if timeout
is not None else 2**31 - 1
236 sub, timer, rate =
None,
None,
None
241 ros2.destroy_entity(rate)
244 timer = ros2.create_timer(timeout, callback=
None)
245 rate = rclpy.timer.Rate(timer, context=ros2.NODE.context)
246 sub = ros2.create_subscriber(topic, topic_type, on_message)
247 if not msgs: rate.sleep()
249 err =
"timeout exceeded while waiting for message on topic %s" % topic
251 except rclpy.exceptions.ROSInterruptException
as e:
254 ros2.destroy_entity(timer)
255 ros2.destroy_entity(sub)
259 "delete_param",
"get_master",
"get_param",
"get_param_cached",
"get_param_names",
260 "get_published_topics",
"has_param",
"init_node",
"myargv",
"on_shutdown",
261 "search_param",
"set_param",
"spin",
"wait_for_message"
Base exception class for ROS clients.
Exception for operations that interrupted, e.g.
Exception if time moved backwards.
Partial stand-in for `rospy.MasterProxy`.
spin()
Spins ROS2 node forever.
get_param_names()
Returns a list of node parameter names, with absolute namespace.