import os from ament_index_python.packages import get_package_share_directory from launch import LaunchDescription from launch.actions import DeclareLaunchArgument, ExecuteProcess, IncludeLaunchDescription from launch.actions import RegisterEventHandler from launch.event_handlers import OnProcessExit from launch.launch_description_sources import PythonLaunchDescriptionSource from launch.substitutions import LaunchConfiguration from launch_ros.actions import Node import xacro def generate_launch_description(): # Launch Arguments use_sim_time = LaunchConfiguration('use_sim_time', default=True) ignition_ros2_control_demos_path = os.path.join( get_package_share_directory('kuka_gazebo')) xacro_file = os.path.join(ignition_ros2_control_demos_path, 'urdf', 'kr210.xacro.urdf') doc = xacro.parse(open(xacro_file)) xacro.process_doc(doc) params = {'robot_description': doc.toxml()} node_robot_state_publisher = Node( package='robot_state_publisher', executable='robot_state_publisher', output='screen', parameters=[params] ) spawn_entity = Node( package='ros_gz_sim', executable='create', output='screen', arguments=['-string', doc.toxml(), '-name', 'kuka', '-allow_renaming', 'true'], ) load_joint_state_broadcaster = Node( package="controller_manager", executable="spawner", arguments=["joint_state_broadcaster", "--controller-manager", "/controller_manager"], ) load_joint_trajectory_controller = Node( package="controller_manager", executable="spawner", arguments=["joint_trajectory_controller", "-c", "/controller_manager"], ) return LaunchDescription([ # Launch gazebo environment IncludeLaunchDescription( PythonLaunchDescriptionSource( [os.path.join(get_package_share_directory('ros_ign_gazebo'), 'launch', 'ign_gazebo.launch.py')]), launch_arguments=[('gz_args', [' -r -v 4 empty.sdf'])]), RegisterEventHandler( event_handler=OnProcessExit( target_action=spawn_entity, on_exit=[load_joint_state_broadcaster], ) ), RegisterEventHandler( event_handler=OnProcessExit( target_action=load_joint_state_broadcaster, on_exit=[load_joint_trajectory_controller], ) ), spawn_entity, node_robot_state_publisher, ])