def main(argv=sys.argv[1:]): """Main.""" ld = LaunchDescription([ launch_ros.actions.Node(package='demo_nodes_cpp', node_executable='talker', output='screen', remappings=[('chatter', 'my_chatter')]), launch_ros.actions.Node(package='demo_nodes_cpp', node_executable='listener', output='screen', remappings=[('chatter', 'my_chatter')]), ]) print('Starting introspection of launch description...') print('') print(LaunchIntrospector().format_launch_description(ld)) print('') print('Starting launch of launch description...') print('') # ls = LaunchService(debug=True) ls = LaunchService() ls.include_launch_description( get_default_launch_description(prefix_output_with_name=False)) ls.include_launch_description(ld) return ls.run()
def generate_launch_description(): talker = launch_ros.actions.Node( node_name='talker', package='examples_rclcpp_minimal_publisher', node_executable='publisher_lambda', output='screen') listener = launch_ros.actions.LifecycleNode( node_name='listener', package='examples_rclcpp_minimal_subscriber', node_executable='subscriber_lambda', output='screen') configure_listener = launch.actions.EmitEvent( event=launch_ros.events.lifecycle.ChangeState( lifecycle_node_matcher=launch.events.matches_action(listener), transition_id=lifecycle_msgs.msg.Transition.TRANSITION_CONFIGURE)) activate_listener = launch.actions.EmitEvent( event=launch_ros.events.lifecycle.ChangeState( lifecycle_node_matcher=launch.events.matches_action(listener), transition_id=lifecycle_msgs.msg.Transition.TRANSITION_ACTIVATE)) reg_event_handler_listener_configured = launch.actions.RegisterEventHandler( launch_ros.event_handlers.OnStateTransition( target_lifecycle_node=listener, goal_state='inactive', entities=[ launch.actions.LogInfo( msg="Node listener configured, activating"), activate_listener ])) reg_event_handler_listener_active = launch.actions.RegisterEventHandler( launch_ros.event_handlers.OnStateTransition( target_lifecycle_node=listener, goal_state='active', entities=[ launch.actions.LogInfo( msg="Node listener active, starting talker"), talker ])) ld = LaunchDescription() ld.add_action(reg_event_handler_listener_configured) ld.add_action(reg_event_handler_listener_active) ld.add_action(listener) ld.add_action(configure_listener) print(LaunchIntrospector().format_launch_description(ld)) return ld
def main(argv): ld = generate_launch_description() print('Starting introspection of launch description...') print('') print(LaunchIntrospector().format_launch_description(ld)) print('') print('Starting launch of launch description...') print('') ls = LaunchService() ls.include_launch_description(ld) return ls.run()
def main(argv=sys.argv[1:]): """Main.""" ld = generate_launch_description() print('Starting introspection of launch description...') print('') print(LaunchIntrospector().format_launch_description(ld)) print('') print('Starting launch of launch description...') print('') ls = LaunchService() ls.include_launch_description(get_default_launch_description()) ls.include_launch_description(ld) return ls.run()
def generate_launch_description(): """Main.""" ld = LaunchDescription([ launch_ros.actions.Node(package='demo_nodes_cpp', node_executable='talker', output='screen', remappings=[('chatter', 'my_chatter')]), launch_ros.actions.Node(package='demo_nodes_cpp', node_executable='listener', output='screen', remappings=[('chatter', 'my_chatter')]), ]) print('Starting introspection of launch description...') print('') print(LaunchIntrospector().format_launch_description(ld)) print('') print('Starting launch of launch description...') print('') return ld
def main(argv=sys.argv[1:]): """Main.""" # Configure rotating logs. launch.logging.launch_config.log_handler_factory = \ lambda path, encoding=None: launch.logging.handlers.RotatingFileHandler( path, maxBytes=1024, backupCount=3, encoding=encoding) # Any number of actions can optionally be given to the constructor of LaunchDescription. # Or actions/entities can be added after creating the LaunchDescription. user_env_var = 'USERNAME' if platform.system() == 'Windows' else 'USER' ld = LaunchDescription([ launch.actions.LogInfo(msg='Hello World!'), launch.actions.LogInfo(msg=( 'Is that you, ', launch.substitutions.EnvironmentVariable(name=user_env_var), '?' )), ]) # Setup a custom event handler for all stdout/stderr from processes. # Later, this will be a configurable, but always present, extension to the LaunchService. def on_output(event: launch.Event) -> None: for line in event.text.decode().splitlines(): print('[{}] {}'.format( cast(launch.events.process.ProcessIO, event).process_name, line)) ld.add_action(launch.actions.RegisterEventHandler(launch.event_handlers.OnProcessIO( # this is the action ^ and this, the event handler ^ on_stdout=on_output, on_stderr=on_output, ))) # Run whoami, and use its output to log the name of the user. # Prefix just the whoami process with `time`. ld.add_action(launch.actions.SetLaunchConfiguration('launch-prefix', 'time')) # Run whoami, but keep handle to action to make a targeted event handler. if platform.system() == 'Windows': whoami_cmd = ['echo', '%USERNAME%'] else: whoami_cmd = [launch.substitutions.FindExecutable(name='whoami')] whoami_action = launch.actions.ExecuteProcess( cmd=whoami_cmd, shell=True ) ld.add_action(whoami_action) # Make event handler that uses the output. ld.add_action(launch.actions.RegisterEventHandler(launch.event_handlers.OnProcessIO( target_action=whoami_action, # The output of `time` will be skipped since `time`'s output always goes to stderr. on_stdout=lambda event: launch.actions.LogInfo( msg="whoami says you are '{}'.".format(event.text.decode().strip()) ), ))) # Unset launch prefix to prevent other process from getting this setting. ld.add_action(launch.actions.SetLaunchConfiguration('launch-prefix', '')) # Run the counting program, with default options. counter_action = launch.actions.ExecuteProcess(cmd=[sys.executable, '-u', './counter.py']) ld.add_action(counter_action) # Setup an event handler for just this process which will exit when `Counter: 4` is seen. def counter_output_handler(event): target_str = 'Counter: 4' if target_str in event.text.decode(): return launch.actions.EmitEvent(event=launch.events.Shutdown( reason="saw '{}' from '{}'".format(target_str, event.process_name) )) ld.add_action(launch.actions.RegisterEventHandler(launch.event_handlers.OnProcessIO( target_action=counter_action, on_stdout=counter_output_handler, on_stderr=counter_output_handler, ))) # Run the counter a few more times, with various options. ld.add_action(launch.actions.ExecuteProcess( cmd=[sys.executable, '-u', './counter.py', '--ignore-sigint'] )) ld.add_action(launch.actions.ExecuteProcess( cmd=[sys.executable, '-u', './counter.py', '--ignore-sigint', '--ignore-sigterm'] )) # Add our own message for when shutdown is requested. ld.add_action(launch.actions.RegisterEventHandler(launch.event_handlers.OnShutdown( on_shutdown=[launch.actions.LogInfo(msg=[ 'Launch was asked to shutdown: ', launch.substitutions.LocalSubstitution('event.reason'), ])], ))) print('Starting introspection of launch description...') print('') print(LaunchIntrospector().format_launch_description(ld)) print('') print('Starting launch of launch description...') print('') # ls = LaunchService(argv=argv, debug=True) # Use this instead to get more debug messages. ls = LaunchService(argv=argv) ls.include_launch_description(ld) return ls.run()
def generate_launch_description(): joydev = LaunchConfiguration('joydev') declare_joydev = DeclareLaunchArgument( 'joydev', default_value='/dev/input/js0', description='Device file for JoyStick Controller' ) declare_joyconfig = DeclareLaunchArgument( 'joyconfig', default_value='f710', description='Keyconfig of joystick controllers: supported: f710, dualshock3' ) declare_mouse = DeclareLaunchArgument( 'mouse', default_value="true", description='Launch raspimouse node' ) def func_get_joyconfig_file_name(context): param_file = os.path.join( get_package_share_directory('raspimouse_ros2_examples'), 'config', 'joy_' + context.launch_configurations['joyconfig'] + '.yml') if os.path.exists(param_file): return [SetLaunchConfiguration('joyconfig_filename', param_file)] else: return [LogInfo(msg=param_file + " is not exist.")] get_joyconfig_file_name = OpaqueFunction(function=func_get_joyconfig_file_name) joy_node = Node( package='joy_linux', executable='joy_linux_node', parameters=[{'dev': joydev}] ) joystick_control_node = Node( package='raspimouse_ros2_examples', executable='joystick_control.py', parameters=[LaunchConfiguration('joyconfig_filename')] ) def func_launch_mouse_node(context): if context.launch_configurations['mouse'] == "true": return [LifecycleNode( name='raspimouse', package='raspimouse', executable='raspimouse', output='screen', parameters=[os.path.join(get_package_share_directory( 'raspimouse_ros2_examples'), 'config', 'mouse.yml')] )] mouse_node = OpaqueFunction(function=func_launch_mouse_node) ld = LaunchDescription() ld.add_action(declare_joydev) ld.add_action(declare_joyconfig) ld.add_action(declare_mouse) ld.add_action(get_joyconfig_file_name) ld.add_action(joy_node) ld.add_action(joystick_control_node) ld.add_action(mouse_node) print(LaunchIntrospector().format_launch_description(ld)) return ld