Skip to content

Commit

Permalink
added parameter for timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Rathauscher (LWN) committed Jan 13, 2025
1 parent cd480cb commit d9c1357
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
6 changes: 5 additions & 1 deletion rosapi/scripts/rosapi_node
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Rosapi(Node):
self.declare_parameter("topics_glob", "[*]")
self.declare_parameter("services_glob", "[*]")
self.declare_parameter("params_glob", "[*]")
self.declare_parameter("params_timeout", 5.0)
self.globs = self.get_globs()
self.register_services()

Expand All @@ -87,7 +88,10 @@ class Rosapi(Node):
full_name = self.get_namespace() + self.get_name()
else:
full_name = self.get_namespace() + "/" + self.get_name()
params.init(full_name)

timeout_sec = self.get_parameter("params_timeout").value
params.init(full_name, timeout_sec)

self.create_service(Topics, "/rosapi/topics", self.get_topics)
self.create_service(TopicsForType, "/rosapi/topics_for_type", self.get_topics_for_type)
self.create_service(
Expand Down
18 changes: 13 additions & 5 deletions rosapi/src/rosapi/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@
""" Methods to interact with the param server. Values have to be passed
as JSON in order to facilitate dynamically typed SRV messages """

# Constants
DEFAULT_PARAM_TIMEOUT_SEC = 5.0

# Ensure thread safety for setting / getting parameters.
param_server_lock = threading.RLock()
_node = None
_parent_node_name = ""
_timeout_sec = DEFAULT_PARAM_TIMEOUT_SEC

_parameter_type_mapping = [
"",
Expand All @@ -64,12 +68,12 @@
]


def init(parent_node_name):
def init(parent_node_name, timeout_sec=DEFAULT_PARAM_TIMEOUT_SEC):
"""
Initializes params module with a rclpy.node.Node for further use.
This function has to be called before any other for the module to work.
"""
global _node, _parent_node_name
global _node, _parent_node_name, _timeout_sec
# TODO(@jubeira): remove this node; use rosapi node with MultiThreadedExecutor or
# async / await to prevent the service calls from blocking.
parent_node_basename = parent_node_name.split("/")[-1]
Expand All @@ -81,6 +85,10 @@ def init(parent_node_name):
)
_parent_node_name = get_absolute_node_name(parent_node_name)

if not isinstance(timeout_sec, (int, float)) or timeout_sec <= 0:
raise ValueError("Parameter timeout must be a positive number")
_timeout_sec = timeout_sec


def set_param(node_name, name, value, params_glob):
"""Sets a parameter in a given node"""
Expand Down Expand Up @@ -231,16 +239,16 @@ def _get_param_names(node_name):

client = _node.create_client(ListParameters, f"{node_name}/list_parameters")

ready = client.wait_for_service(timeout_sec=5.0)
ready = client.wait_for_service(timeout_sec=_timeout_sec)
if not ready:
raise RuntimeError("Wait for list_parameters service timed out")

request = ListParameters.Request()
future = client.call_async(request)
if _node.executor:
_node.executor.spin_until_future_complete(future, timeout_sec=2.0)
_node.executor.spin_until_future_complete(future, timeout_sec=_timeout_sec)
else:
rclpy.spin_until_future_complete(_node, future)
rclpy.spin_until_future_complete(_node, future, timeout_sec=_timeout_sec)
response = future.result()

if response is not None:
Expand Down
2 changes: 2 additions & 0 deletions rosbridge_server/launch/rosbridge_websocket_launch.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<arg name="topics_glob" default="" />
<arg name="services_glob" default="" />
<arg name="params_glob" default="" />
<arg name="params_timeout" default="5.0" />
<arg name="bson_only_mode" default="false" />

<arg name="respawn" default="false"/>
Expand Down Expand Up @@ -76,5 +77,6 @@
<param name="topics_glob" value="$(var topics_glob)"/>
<param name="services_glob" value="$(var services_glob)"/>
<param name="params_glob" value="$(var params_glob)"/>
<param name="params_timeout" value="$(var params_timeout)"/>
</node>
</launch>
2 changes: 1 addition & 1 deletion rosbridge_server/scripts/rosbridge_websocket

0 comments on commit d9c1357

Please sign in to comment.