Skip to content

Commit

Permalink
Add create_own_container parameter to ros_gz_sim launch files.
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Agüero <caguero@openrobotics.org>
  • Loading branch information
caguero committed Oct 3, 2024
1 parent 42bdda3 commit 306b3bd
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
2 changes: 2 additions & 0 deletions 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="create_own_container" default="True" />
<arg name="use_composition" default="True" />
<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>
32 changes: 28 additions & 4 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,6 +33,9 @@ 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='True',
description='Whether we should start our own ROS container when using composition.',)
declare_use_composition_cmd = DeclareLaunchArgument(
'use_composition', default_value='True',
description='Use composed bringup if True')
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
3 changes: 3 additions & 0 deletions ros_gz_sim/launch/ros_gz_sim.launch
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<arg name="bridge_name" />
<arg name="config_file" />
<arg name="container_name" default="ros_gz_container" />
<arg name="create_own_container" default="True" />
<arg name="namespace" default="" />
<arg name="use_composition" default="True" />
<arg name="use_respawn" default="False" />
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
10 changes: 10 additions & 0 deletions 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,6 +49,12 @@ 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='True',
description='Whether we should start a ROS container when using composition.',
)

declare_namespace_cmd = DeclareLaunchArgument(
'namespace', default_value='', description='Top-level namespace'
)
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
13 changes: 13 additions & 0 deletions ros_gz_sim/ros_gz_sim/actions/gzserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(
world_sdf_file: Optional[SomeSubstitutionsType] = '',
world_sdf_string: Optional[SomeSubstitutionsType] = '',
container_name: Optional[SomeSubstitutionsType] = 'ros_gz_container',
create_own_container: Optional[SomeSubstitutionsType] = 'True',
use_composition: Optional[SomeSubstitutionsType] = 'True',
**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]

0 comments on commit 306b3bd

Please sign in to comment.