Skip to content

Commit

Permalink
Merge branch 'humble' into backport/rosapi-private-names
Browse files Browse the repository at this point in the history
  • Loading branch information
bjsowa authored Jan 26, 2025
2 parents c6eedec + ac367b8 commit ad346bc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
5 changes: 4 additions & 1 deletion rosapi/scripts/rosapi_node
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,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.declare_parameter("use_private_service_names", False)
self.globs = self.get_globs()
self.register_services()
Expand All @@ -89,7 +90,9 @@ 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)

use_private_service_names = (
self.get_parameter("use_private_service_names").get_parameter_value().bool_value
Expand Down
23 changes: 15 additions & 8 deletions rosapi/src/rosapi/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,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 @@ -63,12 +67,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 @@ -80,6 +84,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 @@ -225,21 +233,20 @@ def _get_param_names(node_name):
# This method is called in a service callback; calling a service of the same node
# will cause a deadlock.
global _parent_node_name
if node_name == _parent_node_name:
if node_name == _parent_node_name or node_name == _node.get_fully_qualified_name():
return []

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

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

request = ListParameters.Request()
future = client.call_async(request)
if _node.executor:
_node.executor.spin_until_future_complete(future)
_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 @@ -20,6 +20,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 unless="$(var bson_only_mode)" name="binary_encoder" default="default"/>
Expand Down Expand Up @@ -70,5 +71,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>

0 comments on commit ad346bc

Please sign in to comment.