|
| 1 | +from launch import LaunchDescription |
| 2 | +from launch.actions import ( |
| 3 | + OpaqueFunction, |
| 4 | + EmitEvent, |
| 5 | + RegisterEventHandler, |
| 6 | +) |
| 7 | +from launch.substitutions import TextSubstitution |
| 8 | +from launch_ros.actions import LifecycleNode |
| 9 | +from launch.event_handlers import OnProcessStart |
| 10 | +from launch.event_handlers.on_shutdown import OnShutdown |
| 11 | +from launch.events import matches_action |
| 12 | +from launch_ros.event_handlers import OnStateTransition |
| 13 | +from launch_ros.events.lifecycle import ChangeState |
| 14 | +from lifecycle_msgs.msg import Transition |
| 15 | + |
| 16 | + |
| 17 | +def launch_setup(context): |
| 18 | + description = [] |
| 19 | + serial_driver_onde = LifecycleNode( |
| 20 | + package='serial_driver', |
| 21 | + executable='serial_bridge', |
| 22 | + name='serial_bridge_node', |
| 23 | + namespace=TextSubstitution(text=''), |
| 24 | + parameters=[{'device_name': '/dev/ttyUSB0', |
| 25 | + 'baud_rate': 115200, |
| 26 | + 'flow_control': 'none', |
| 27 | + 'parity': 'none', |
| 28 | + 'stop_bits': "1"}], |
| 29 | + remappings=[("serial_read", "rscp/serial/rx"), |
| 30 | + ("serial_write", "rscp/serial/tx")], |
| 31 | + output='screen', |
| 32 | + ) |
| 33 | + configure_event_handler = RegisterEventHandler( |
| 34 | + event_handler=OnProcessStart( |
| 35 | + target_action=serial_driver_onde, |
| 36 | + on_start=[ |
| 37 | + EmitEvent( |
| 38 | + event=ChangeState( |
| 39 | + lifecycle_node_matcher=matches_action(serial_driver_onde), |
| 40 | + transition_id=Transition.TRANSITION_CONFIGURE, |
| 41 | + ), |
| 42 | + ), |
| 43 | + ], |
| 44 | + ) |
| 45 | + ) |
| 46 | + |
| 47 | + activate_event_handler = RegisterEventHandler( |
| 48 | + event_handler=OnStateTransition( |
| 49 | + target_lifecycle_node=serial_driver_onde, |
| 50 | + start_state='configuring', |
| 51 | + goal_state='inactive', |
| 52 | + entities=[ |
| 53 | + EmitEvent( |
| 54 | + event=ChangeState( |
| 55 | + lifecycle_node_matcher=matches_action(serial_driver_onde), |
| 56 | + transition_id=Transition.TRANSITION_ACTIVATE, |
| 57 | + ), |
| 58 | + ), |
| 59 | + ], |
| 60 | + ) |
| 61 | + ) |
| 62 | + |
| 63 | + shutdown_event_handler = RegisterEventHandler( |
| 64 | + event_handler=OnShutdown( |
| 65 | + on_shutdown=[ |
| 66 | + EmitEvent( |
| 67 | + event=ChangeState( |
| 68 | + lifecycle_node_matcher=matches_action(serial_driver_onde), |
| 69 | + transition_id=Transition.TRANSITION_ACTIVE_SHUTDOWN, |
| 70 | + ) |
| 71 | + ) |
| 72 | + ] |
| 73 | + ) |
| 74 | + ) |
| 75 | + description += [ |
| 76 | + serial_driver_onde, |
| 77 | + configure_event_handler, |
| 78 | + activate_event_handler, |
| 79 | + shutdown_event_handler, |
| 80 | + ] |
| 81 | + |
| 82 | + |
| 83 | + return description |
| 84 | + |
| 85 | + |
| 86 | +def generate_launch_description(): |
| 87 | + return LaunchDescription( |
| 88 | + [ |
| 89 | + OpaqueFunction(function=launch_setup), |
| 90 | + ] |
| 91 | + ) |
0 commit comments