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

Improve argument parsing in Actions #663

Merged
merged 2 commits into from
Dec 11, 2024
Merged
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
177 changes: 92 additions & 85 deletions ros_gz_bridge/ros_gz_bridge/actions/ros_gz_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@

"""Module for the ros_gz bridge action."""

from typing import List
from typing import Optional
from typing import Dict, List, Optional, Union

from launch.action import Action
from launch.actions import GroupAction
from launch.conditions import IfCondition
from launch.frontend import Entity, expose_action, Parser
from launch.launch_context import LaunchContext
from launch.some_substitutions_type import SomeSubstitutionsType
from launch.substitutions import PythonExpression
from launch.substitutions import TextSubstitution
from launch.utilities.type_utils import normalize_typed_substitution, perform_typed_substitution
from launch_ros.actions import ComposableNodeContainer, LoadComposableNodes, Node
from launch_ros.descriptions import ComposableNode

Expand All @@ -37,13 +35,13 @@ def __init__(
*,
bridge_name: SomeSubstitutionsType,
config_file: SomeSubstitutionsType,
container_name: Optional[SomeSubstitutionsType] = 'ros_gz_container',
create_own_container: Optional[SomeSubstitutionsType] = 'False',
namespace: Optional[SomeSubstitutionsType] = '',
use_composition: Optional[SomeSubstitutionsType] = 'False',
use_respawn: Optional[SomeSubstitutionsType] = 'False',
log_level: Optional[SomeSubstitutionsType] = 'info',
bridge_params: Optional[SomeSubstitutionsType] = '',
container_name: SomeSubstitutionsType = 'ros_gz_container',
create_own_container: Union[bool, SomeSubstitutionsType] = False,
namespace: SomeSubstitutionsType = '',
use_composition: Union[bool, SomeSubstitutionsType] = False,
use_respawn: Union[bool, SomeSubstitutionsType] = False,
log_level: SomeSubstitutionsType = 'info',
bridge_params: SomeSubstitutionsType = '',
**kwargs
) -> None:
"""
Expand All @@ -60,20 +58,43 @@ def __init__(
:param: bridge_params Extra parameters to pass to the bridge.
"""
super().__init__(**kwargs)

self.__bridge_name = bridge_name
self.__config_file = config_file
self.__container_name = container_name
self.__create_own_container = create_own_container
self.__namespace = namespace
self.__use_composition = use_composition
self.__use_respawn = use_respawn

# This is here to allow using strings or booleans as values for boolean variables when
# the Action is used from Python i.e., this allows users to do:
# RosGzBridge(bridge_name='bridge1', use_composition='true', create_own_container=True)
# Note that use_composition is set to a string while create_own_container is set to a
# boolean. The reverse would also work.
# At some point, we might want to deprecate this and only allow setting booleans since
# that's what users would expect when calling this from Python
if isinstance(create_own_container, str):
self.__create_own_container = normalize_typed_substitution(
TextSubstitution(text=create_own_container), bool
)
else:
self.__create_own_container = normalize_typed_substitution(
create_own_container, bool
)

if isinstance(use_composition, str):
self.__use_composition = normalize_typed_substitution(
TextSubstitution(text=use_composition), bool
)
else:
self.__use_composition = normalize_typed_substitution(use_composition, bool)

self.__use_respawn = normalize_typed_substitution(use_respawn, bool)
self.__log_level = log_level
self.__bridge_params = bridge_params

@classmethod
def parse(cls, entity: Entity, parser: Parser):
"""Parse ros_gz_bridge."""
_, kwargs = super().parse(entity, parser)
kwargs: Dict = super().parse(entity, parser)[1]

bridge_name = entity.get_attr(
'bridge_name', data_type=str,
Expand Down Expand Up @@ -169,77 +190,63 @@ def execute(self, context: LaunchContext) -> Optional[List[Action]]:
bridge_params_pairs = simplified_bridge_params.split(',')
parsed_bridge_params = dict(pair.split(':') for pair in bridge_params_pairs)

if isinstance(self.__use_composition, list):
self.__use_composition = self.__use_composition[0]

if isinstance(self.__create_own_container, list):
self.__create_own_container = self.__create_own_container[0]

if isinstance(self.__use_respawn, list):
self.__use_respawn = self.__use_respawn[0]

# Standard node configuration
load_nodes = GroupAction(
condition=IfCondition(PythonExpression(['not ', self.__use_composition])),
actions=[
Node(
package='ros_gz_bridge',
executable='bridge_node',
name=self.__bridge_name,
namespace=self.__namespace,
output='screen',
respawn=bool(self.__use_respawn),
respawn_delay=2.0,
parameters=[{'config_file': self.__config_file, **parsed_bridge_params}],
arguments=['--ros-args', '--log-level', self.__log_level],
),
],
use_composition_eval = perform_typed_substitution(
context, self.__use_composition, bool
)
create_own_container_eval = perform_typed_substitution(
context, self.__create_own_container, bool
)

launch_descriptions: List[Action] = []

if not use_composition_eval:
# Standard node configuration
launch_descriptions.append(Node(
package='ros_gz_bridge',
executable='bridge_node',
name=self.__bridge_name,
namespace=self.__namespace,
output='screen',
respawn=perform_typed_substitution(context, self.__use_respawn, bool),
respawn_delay=2.0,
parameters=[{'config_file': self.__config_file, **parsed_bridge_params}],
arguments=['--ros-args', '--log-level', self.__log_level],
))

# Composable node with container configuration
load_composable_nodes_with_container = ComposableNodeContainer(
condition=IfCondition(
PythonExpression([self.__use_composition, ' and ', self.__create_own_container])
),
name=self.__container_name,
namespace='',
package='rclcpp_components',
executable='component_container',
composable_node_descriptions=[
ComposableNode(
package='ros_gz_bridge',
plugin='ros_gz_bridge::RosGzBridge',
name=self.__bridge_name,
namespace=self.__namespace,
parameters=[{'config_file': self.__config_file, **parsed_bridge_params}],
extra_arguments=[{'use_intra_process_comms': True}],
),
],
output='screen',
)
if use_composition_eval and create_own_container_eval:
launch_descriptions.append(ComposableNodeContainer(
name=self.__container_name,
namespace='',
package='rclcpp_components',
executable='component_container',
composable_node_descriptions=[
ComposableNode(
package='ros_gz_bridge',
plugin='ros_gz_bridge::RosGzBridge',
name=self.__bridge_name,
namespace=self.__namespace,
parameters=[{'config_file': self.__config_file, **parsed_bridge_params}],
extra_arguments=[{'use_intra_process_comms': True}],
),
],
output='screen',
))

# Composable node without container configuration
load_composable_nodes_without_container = LoadComposableNodes(
condition=IfCondition(
PythonExpression(
[self.__use_composition, ' and not ', self.__create_own_container]
)
),
target_container=self.__container_name,
composable_node_descriptions=[
ComposableNode(
package='ros_gz_bridge',
plugin='ros_gz_bridge::RosGzBridge',
name=self.__bridge_name,
namespace=self.__namespace,
parameters=[{'config_file': self.__config_file, **parsed_bridge_params}],
extra_arguments=[{'use_intra_process_comms': True}],
),
],
)

return [
load_nodes,
load_composable_nodes_with_container,
load_composable_nodes_without_container
]
if use_composition_eval and not create_own_container_eval:
launch_descriptions.append(LoadComposableNodes(
target_container=self.__container_name,
composable_node_descriptions=[
ComposableNode(
package='ros_gz_bridge',
plugin='ros_gz_bridge::RosGzBridge',
name=self.__bridge_name,
namespace=self.__namespace,
parameters=[{'config_file': self.__config_file, **parsed_bridge_params}],
extra_arguments=[{'use_intra_process_comms': True}],
),
],
))

return launch_descriptions
Loading
Loading