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

Extra parameter to start a container (backport #616) #618

Merged
merged 1 commit into from
Oct 15, 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
4 changes: 3 additions & 1 deletion ros_gz_bridge/launch/ros_gz_bridge.launch
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
<arg name="bridge_name" />
<arg name="config_file" />
<arg name="container_name" default="ros_gz_container" />
<arg name="create_own_container" default="False" />
<arg name="namespace" default="" />
<arg name="use_composition" default="True" />
<arg name="use_composition" default="False" />
<arg name="use_respawn" default="False" />
<arg name="log_level" default="info" />
<ros_gz_bridge
bridge_name="$(var bridge_name)"
config_file="$(var config_file)"
container_name="$(var container_name)"
create_own_container="$(var create_own_container)"
namespace="$(var namespace)"
use_composition="$(var use_composition)"
use_respawn="$(var use_respawn)"
Expand Down
43 changes: 38 additions & 5 deletions ros_gz_bridge/launch/ros_gz_bridge.launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from launch.actions import DeclareLaunchArgument, GroupAction
from launch.conditions import IfCondition
from launch.substitutions import LaunchConfiguration, PythonExpression
from launch_ros.actions import LoadComposableNodes, Node
from launch_ros.actions import ComposableNodeContainer, LoadComposableNodes, Node
from launch_ros.descriptions import ComposableNode


Expand All @@ -27,6 +27,7 @@ def generate_launch_description():
bridge_name = LaunchConfiguration('bridge_name')
config_file = LaunchConfiguration('config_file')
container_name = LaunchConfiguration('container_name')
create_own_container = LaunchConfiguration('create_own_container')
namespace = LaunchConfiguration('namespace')
use_composition = LaunchConfiguration('use_composition')
use_respawn = LaunchConfiguration('use_respawn')
Expand All @@ -46,12 +47,21 @@ def generate_launch_description():
description='Name of container that nodes will load in if use composition',
)

declare_create_own_container_cmd = DeclareLaunchArgument(
'create_own_container',
default_value='False',
description='Whether the bridge should start its own ROS container when using composition \
(not recommended). This option should only be set to true if you plan to put your ROS \
node in the container created by the bridge. This is not needed if you want Gazebo and \
the bridge to be in the same ROS container.',
)

declare_namespace_cmd = DeclareLaunchArgument(
'namespace', default_value='', description='Top-level namespace'
)

declare_use_composition_cmd = DeclareLaunchArgument(
'use_composition', default_value='True', description='Use composed bringup if True'
'use_composition', default_value='False', description='Use composed bringup if True'
)

declare_use_respawn_cmd = DeclareLaunchArgument(
Expand Down Expand Up @@ -81,8 +91,29 @@ def generate_launch_description():
],
)

load_composable_nodes = LoadComposableNodes(
condition=IfCondition(use_composition),
load_composable_nodes_with_container = ComposableNodeContainer(
condition=IfCondition(
PythonExpression([use_composition, ' and ', create_own_container])),
name=LaunchConfiguration('container_name'),
namespace='',
package='rclcpp_components',
executable='component_container',
composable_node_descriptions=[
ComposableNode(
package='ros_gz_bridge',
plugin='ros_gz_bridge::RosGzBridge',
name=bridge_name,
namespace=namespace,
parameters=[{'config_file': config_file}],
extra_arguments=[{'use_intra_process_comms': True}],
),
],
output='screen',
)

load_composable_nodes_without_container = LoadComposableNodes(
condition=IfCondition(
PythonExpression([use_composition, ' and not ', create_own_container])),
target_container=container_name,
composable_node_descriptions=[
ComposableNode(
Expand All @@ -103,12 +134,14 @@ def generate_launch_description():
ld.add_action(declare_bridge_name_cmd)
ld.add_action(declare_config_file_cmd)
ld.add_action(declare_container_name_cmd)
ld.add_action(declare_create_own_container_cmd)
ld.add_action(declare_namespace_cmd)
ld.add_action(declare_use_composition_cmd)
ld.add_action(declare_use_respawn_cmd)
ld.add_action(declare_log_level_cmd)
# Add the actions to launch all of the bridge nodes
ld.add_action(load_nodes)
ld.add_action(load_composable_nodes)
ld.add_action(load_composable_nodes_with_container)
ld.add_action(load_composable_nodes_without_container)

return ld
15 changes: 14 additions & 1 deletion ros_gz_bridge/ros_gz_bridge/actions/ros_gz_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ 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] = 'True',
use_composition: Optional[SomeSubstitutionsType] = 'False',
use_respawn: Optional[SomeSubstitutionsType] = 'False',
log_level: Optional[SomeSubstitutionsType] = 'info',
**kwargs
Expand All @@ -52,6 +53,7 @@ def __init__(
:param: bridge_name Name of ros_gz_bridge node
:param: config_file YAML config file.
:param: container_name Name of container that nodes will load in if use composition.
:param: create_own_container Whether to start a ROS container when using composition.
:param: namespace Top-level namespace.
:param: use_composition Use composed bringup if True.
:param: use_respawn Whether to respawn if a node crashes (when composition is disabled).
Expand All @@ -61,6 +63,7 @@ def __init__(
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
Expand All @@ -83,6 +86,10 @@ def parse(cls, entity: Entity, parser: Parser):
'container_name', data_type=str,
optional=True)

create_own_container = entity.get_attr(
'create_own_container', data_type=str,
optional=True)

namespace = entity.get_attr(
'namespace', data_type=str,
optional=True)
Expand Down Expand Up @@ -111,6 +118,11 @@ def parse(cls, entity: Entity, parser: Parser):
container_name = parser.parse_substitution(container_name)
kwargs['container_name'] = container_name

if isinstance(create_own_container, str):
create_own_container = \
parser.parse_substitution(create_own_container)
kwargs['create_own_container'] = create_own_container

if isinstance(namespace, str):
namespace = parser.parse_substitution(namespace)
kwargs['namespace'] = namespace
Expand Down Expand Up @@ -139,6 +151,7 @@ def execute(self, context: LaunchContext) -> Optional[List[Action]]:
launch_arguments=[('bridge_name', self.__bridge_name),
('config_file', self.__config_file),
('container_name', self.__container_name),
('create_own_container', self.__create_own_container),
('namespace', self.__namespace),
('use_composition', self.__use_composition),
('use_respawn', self.__use_respawn),
Expand Down
4 changes: 3 additions & 1 deletion ros_gz_sim/launch/gz_server.launch
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
<arg name="world_sdf_file" default="empty.sdf" />
<arg name="world_sdf_string" default="" />
<arg name="container_name" default="ros_gz_container" />
<arg name="use_composition" default="True" />
<arg name="create_own_container" default="False" />
<arg name="use_composition" default="False" />
<gz_server
world_sdf_file="$(var world_sdf_file)"
world_sdf_string="$(var world_sdf_string)"
container_name="$(var container_name)"
create_own_container="$(var create_own_container)"
use_composition="$(var use_composition)">
</gz_server>
</launch>
34 changes: 29 additions & 5 deletions ros_gz_sim/launch/gz_server.launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from launch.actions import DeclareLaunchArgument
from launch.conditions import IfCondition
from launch.substitutions import LaunchConfiguration, PythonExpression, TextSubstitution
from launch_ros.actions import ComposableNodeContainer, Node
from launch_ros.actions import ComposableNodeContainer, LoadComposableNodes, Node
from launch_ros.descriptions import ComposableNode


Expand All @@ -33,8 +33,11 @@ def generate_launch_description():
declare_container_name_cmd = DeclareLaunchArgument(
'container_name', default_value='ros_gz_container',
description='Name of container that nodes will load in if use composition',)
declare_create_own_container_cmd = DeclareLaunchArgument(
'create_own_container', default_value='False',
description='Whether we should start our own ROS container when using composition.',)
declare_use_composition_cmd = DeclareLaunchArgument(
'use_composition', default_value='True',
'use_composition', default_value='False',
description='Use composed bringup if True')

load_nodes = Node(
Expand All @@ -46,8 +49,10 @@ def generate_launch_description():
'world_sdf_string': LaunchConfiguration('world_sdf_string')}],
)

load_composable_nodes = ComposableNodeContainer(
condition=IfCondition(LaunchConfiguration('use_composition')),
load_composable_nodes_with_container = ComposableNodeContainer(
condition=IfCondition(
PythonExpression([LaunchConfiguration('use_composition'), ' and ',
LaunchConfiguration('create_own_container')])),
name=LaunchConfiguration('container_name'),
namespace='',
package='rclcpp_components',
Expand All @@ -65,16 +70,35 @@ def generate_launch_description():
output='screen',
)

load_composable_nodes_without_container = LoadComposableNodes(
condition=IfCondition(
PythonExpression([LaunchConfiguration('use_composition'), ' and not ',
LaunchConfiguration('create_own_container')])),
target_container=LaunchConfiguration('container_name'),
composable_node_descriptions=[
ComposableNode(
package='ros_gz_sim',
plugin='ros_gz_sim::GzServer',
name='gz_server',
parameters=[{'world_sdf_file': LaunchConfiguration('world_sdf_file'),
'world_sdf_string': LaunchConfiguration('world_sdf_string')}],
extra_arguments=[{'use_intra_process_comms': True}],
),
],
)

# Create the launch description and populate
ld = LaunchDescription()

# Declare the launch options
ld.add_action(declare_world_sdf_file_cmd)
ld.add_action(declare_world_sdf_string_cmd)
ld.add_action(declare_container_name_cmd)
ld.add_action(declare_create_own_container_cmd)
ld.add_action(declare_use_composition_cmd)
# Add the actions to launch all of the gz_server nodes
ld.add_action(load_nodes)
ld.add_action(load_composable_nodes)
ld.add_action(load_composable_nodes_with_container)
ld.add_action(load_composable_nodes_without_container)

return ld
5 changes: 4 additions & 1 deletion ros_gz_sim/launch/ros_gz_sim.launch
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
<arg name="bridge_name" />
<arg name="config_file" />
<arg name="container_name" default="ros_gz_container" />
<arg name="create_own_container" default="False" />
<arg name="namespace" default="" />
<arg name="use_composition" default="True" />
<arg name="use_composition" default="False" />
<arg name="use_respawn" default="False" />
<arg name="log_level" default="info" />
<arg name="world_sdf_file" default="empty.sdf" />
Expand All @@ -12,12 +13,14 @@
world_sdf_file="$(var world_sdf_file)"
world_sdf_string="$(var world_sdf_string)"
container_name="$(var container_name)"
create_own_container="$(var create_own_container)"
use_composition="$(var use_composition)">
</gz_server>
<ros_gz_bridge
bridge_name="$(var bridge_name)"
config_file="$(var config_file)"
container_name="$(var container_name)"
create_own_container="False"
namespace="$(var namespace)"
use_composition="$(var use_composition)"
use_respawn="$(var use_respawn)"
Expand Down
12 changes: 11 additions & 1 deletion ros_gz_sim/launch/ros_gz_sim.launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def generate_launch_description():
bridge_name = LaunchConfiguration('bridge_name')
config_file = LaunchConfiguration('config_file')
container_name = LaunchConfiguration('container_name')
create_own_container = LaunchConfiguration('create_own_container')
namespace = LaunchConfiguration('namespace')
use_composition = LaunchConfiguration('use_composition')
use_respawn = LaunchConfiguration('use_respawn')
Expand All @@ -48,12 +49,18 @@ def generate_launch_description():
description='Name of container that nodes will load in if use composition',
)

declare_create_own_container_cmd = DeclareLaunchArgument(
'create_own_container',
default_value='False',
description='Whether we should start a ROS container when using composition.',
)

declare_namespace_cmd = DeclareLaunchArgument(
'namespace', default_value='', description='Top-level namespace'
)

declare_use_composition_cmd = DeclareLaunchArgument(
'use_composition', default_value='True', description='Use composed bringup if True'
'use_composition', default_value='False', description='Use composed bringup if True'
)

declare_use_respawn_cmd = DeclareLaunchArgument(
Expand Down Expand Up @@ -96,6 +103,8 @@ def generate_launch_description():
'gz_server.launch.py'])]),
launch_arguments=[('world_sdf_file', world_sdf_file),
('world_sdf_string', world_sdf_string),
('container_name', container_name),
('create_own_container', create_own_container),
('use_composition', use_composition), ])

# Create the launch description and populate
Expand All @@ -105,6 +114,7 @@ def generate_launch_description():
ld.add_action(declare_bridge_name_cmd)
ld.add_action(declare_config_file_cmd)
ld.add_action(declare_container_name_cmd)
ld.add_action(declare_create_own_container_cmd)
ld.add_action(declare_namespace_cmd)
ld.add_action(declare_use_composition_cmd)
ld.add_action(declare_use_respawn_cmd)
Expand Down
15 changes: 14 additions & 1 deletion ros_gz_sim/ros_gz_sim/actions/gzserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def __init__(
world_sdf_file: Optional[SomeSubstitutionsType] = '',
world_sdf_string: Optional[SomeSubstitutionsType] = '',
container_name: Optional[SomeSubstitutionsType] = 'ros_gz_container',
use_composition: Optional[SomeSubstitutionsType] = 'True',
create_own_container: Optional[SomeSubstitutionsType] = 'False',
use_composition: Optional[SomeSubstitutionsType] = 'False',
**kwargs
) -> None:
"""
Expand All @@ -49,12 +50,14 @@ def __init__(
:param: world_sdf_file Path to the SDF world file.
:param: world_sdf_string SDF world string.
:param: container_name Name of container that nodes will load in if use composition.
:param: create_own_container Whether to start a ROS container when using composition.
:param: use_composition Use composed bringup if True.
"""
super().__init__(**kwargs)
self.__world_sdf_file = world_sdf_file
self.__world_sdf_string = world_sdf_string
self.__container_name = container_name
self.__create_own_container = create_own_container
self.__use_composition = use_composition

@classmethod
Expand All @@ -74,6 +77,10 @@ def parse(cls, entity: Entity, parser: Parser):
'container_name', data_type=str,
optional=True)

create_own_container = entity.get_attr(
'create_own_container', data_type=str,
optional=True)

use_composition = entity.get_attr(
'use_composition', data_type=str,
optional=True)
Expand All @@ -90,6 +97,11 @@ def parse(cls, entity: Entity, parser: Parser):
container_name = parser.parse_substitution(container_name)
kwargs['container_name'] = container_name

if isinstance(create_own_container, str):
create_own_container = \
parser.parse_substitution(create_own_container)
kwargs['create_own_container'] = create_own_container

if isinstance(use_composition, str):
use_composition = parser.parse_substitution(use_composition)
kwargs['use_composition'] = use_composition
Expand All @@ -106,6 +118,7 @@ def execute(self, context: LaunchContext) -> Optional[List[Action]]:
launch_arguments=[('world_sdf_file', self.__world_sdf_file),
('world_sdf_string', self.__world_sdf_string),
('container_name', self.__container_name),
('create_own_container', self.__create_own_container),
('use_composition', self.__use_composition), ])

return [gz_server_description]
Loading