Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse spawner service clients #1947

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions controller_manager/controller_manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from .controller_manager_services import (
ServiceCallerNode,
configure_controller,
list_controller_types,
list_controllers,
Expand All @@ -30,6 +31,7 @@
)

__all__ = [
"ServiceCallerNode",
"configure_controller",
"list_controller_types",
"list_controllers",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
)

import rclpy
from rclpy.node import Node
import yaml
from rcl_interfaces.msg import Parameter

Expand Down Expand Up @@ -55,6 +56,18 @@ class ServiceNotFoundError(Exception):
pass


class ServiceCallerNode(Node):
def __init__(self, node_name: str):
super().__init__(node_name)
# initialize dictionary to store service clients
self.service_clients = {}

def __del__(self):
# destroy all service clients
for key in self.service_clients:
self.destroy_client(self.service_clients[key])


def service_caller(
node,
service_name,
Expand Down Expand Up @@ -92,7 +105,13 @@ def service_caller(
fully_qualified_service_name = (
f"{namespace}/{service_name}" if not service_name.startswith("/") else service_name
)
cli = node.create_client(service_type, fully_qualified_service_name)

if (service_type, fully_qualified_service_name) not in node.service_clients:
cli = node.create_client(service_type, fully_qualified_service_name)
# save client for next time so we're not creating a new one every time
node.service_clients[(service_type, fully_qualified_service_name)] = cli
else:
cli = node.service_clients[(service_type, fully_qualified_service_name)]

while not cli.service_is_ready():
node.get_logger().info(
Expand Down
4 changes: 2 additions & 2 deletions controller_manager/controller_manager/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import warnings

from controller_manager import (
ServiceCallerNode,
configure_controller,
list_controllers,
load_controller,
Expand All @@ -32,7 +33,6 @@
from controller_manager.controller_manager_services import ServiceNotFoundError

import rclpy
from rclpy.node import Node
from rclpy.signals import SignalHandlerOptions


Expand Down Expand Up @@ -160,7 +160,7 @@ def main(args=None):
if not os.path.isfile(param_file):
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), param_file)

node = Node("spawner_" + controller_names[0])
node = ServiceCallerNode("spawner_" + controller_names[0])

if node.get_namespace() != "/" and args.namespace:
raise RuntimeError(
Expand Down
Loading