|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Pick Place Harmonic - Gazebo World Launcher |
| 4 | +Launches ONLY: Gazebo Harmonic + UR5 Robot + Controllers |
| 5 | +MoveIt and RViz are launched separately by the Academy |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +from launch import LaunchDescription |
| 10 | +from launch.actions import ExecuteProcess, TimerAction |
| 11 | +from launch_ros.actions import Node |
| 12 | +from ament_index_python.packages import get_package_share_directory |
| 13 | +import xacro |
| 14 | + |
| 15 | + |
| 16 | +def generate_launch_description(): |
| 17 | + # Get package directories |
| 18 | + pkg_share_dir = get_package_share_directory("ur5_gripper_description") |
| 19 | + robotiq_pkg_share_dir = get_package_share_directory("robotiq_description") |
| 20 | + |
| 21 | + # Workspace for gz_ros2_control |
| 22 | + workspace_dir = "/home/dev_ws" |
| 23 | + gz_lib_path = os.path.join(workspace_dir, 'install', 'gz_ros2_control', 'lib') |
| 24 | + |
| 25 | + # Set environment variables for Gazebo Harmonic |
| 26 | + gz_env = { |
| 27 | + 'GZ_SIM_SYSTEM_PLUGIN_PATH': f"{gz_lib_path}:{os.environ.get('GZ_SIM_SYSTEM_PLUGIN_PATH', '')}", |
| 28 | + 'GZ_SIM_RESOURCE_PATH': f"{pkg_share_dir}/share:{robotiq_pkg_share_dir}/share:{os.environ.get('GZ_SIM_RESOURCE_PATH', '')}", |
| 29 | + 'LD_LIBRARY_PATH': f"{gz_lib_path}:{os.environ.get('LD_LIBRARY_PATH', '')}" |
| 30 | + } |
| 31 | + |
| 32 | + # Process URDF with xacro |
| 33 | + xacro_file = os.path.join(pkg_share_dir, "urdf", "ur5_robotiq85_gripper.urdf.xacro") |
| 34 | + controllers_file = os.path.join(pkg_share_dir, "config", "ur5_controllers.yaml") |
| 35 | + |
| 36 | + robot_description_content = xacro.process_file( |
| 37 | + xacro_file, |
| 38 | + mappings={ |
| 39 | + "ur_type": "ur5", |
| 40 | + "name": "ur", |
| 41 | + "prefix": "", |
| 42 | + "use_fake_hardware": "false", |
| 43 | + "sim_gazebo": "false", |
| 44 | + "sim_gz": "true", |
| 45 | + "simulation_controllers": controllers_file, |
| 46 | + } |
| 47 | + ).toxml() |
| 48 | + |
| 49 | + robot_description = {"robot_description": robot_description_content} |
| 50 | + |
| 51 | + # Robot state publisher |
| 52 | + robot_state_publisher = Node( |
| 53 | + package="robot_state_publisher", |
| 54 | + executable="robot_state_publisher", |
| 55 | + output="screen", |
| 56 | + parameters=[robot_description, {"use_sim_time": True}], |
| 57 | + ) |
| 58 | + |
| 59 | + # Static transform (world → base_link) |
| 60 | + static_tf = Node( |
| 61 | + package="tf2_ros", |
| 62 | + executable="static_transform_publisher", |
| 63 | + name="static_transform_publisher", |
| 64 | + arguments=["0", "0", "0.9", "0", "0", "0", "world", "base_link"], |
| 65 | + output="screen", |
| 66 | + parameters=[{"use_sim_time": True}], |
| 67 | + ) |
| 68 | + |
| 69 | + # Gazebo Sim |
| 70 | + world_file = os.path.join(robotiq_pkg_share_dir, 'world', 'warehouse_arm_harmonic.world') |
| 71 | + gz_cmd = ( |
| 72 | + f'export GZ_SIM_SYSTEM_PLUGIN_PATH="{gz_env["GZ_SIM_SYSTEM_PLUGIN_PATH"]}" && ' |
| 73 | + f'export GZ_SIM_RESOURCE_PATH="{gz_env["GZ_SIM_RESOURCE_PATH"]}" && ' |
| 74 | + f'export LD_LIBRARY_PATH="{gz_env["LD_LIBRARY_PATH"]}" && ' |
| 75 | + f'gz sim -r -v 4 "{world_file}"' |
| 76 | + ) |
| 77 | + |
| 78 | + gazebo = ExecuteProcess( |
| 79 | + cmd=['bash', '-c', gz_cmd], |
| 80 | + output='screen', |
| 81 | + shell=False |
| 82 | + ) |
| 83 | + |
| 84 | + # Spawn robot |
| 85 | + spawn_entity = Node( |
| 86 | + package="ros_gz_sim", |
| 87 | + executable="create", |
| 88 | + arguments=[ |
| 89 | + "-topic", "robot_description", |
| 90 | + "-name", "ur5_robotiq", |
| 91 | + "-allow_renaming", "true", |
| 92 | + "-x", "0.0", "-y", "0.0", "-z", "0.9", |
| 93 | + "-R", "0.0", "-P", "0.0", "-Y", "0.0" |
| 94 | + ], |
| 95 | + output="screen", |
| 96 | + ) |
| 97 | + |
| 98 | + # Clock bridge |
| 99 | + gz_ros2_bridge_clock = Node( |
| 100 | + package="ros_gz_bridge", |
| 101 | + executable="parameter_bridge", |
| 102 | + arguments=["/clock@rosgraph_msgs/msg/Clock[gz.msgs.Clock"], |
| 103 | + output="screen", |
| 104 | + parameters=[{"use_sim_time": True}], |
| 105 | + ) |
| 106 | + |
| 107 | + # Spawn controllers via background script (more reliable than launch system) |
| 108 | + controller_script = os.path.join(os.path.dirname(__file__), 'spawn_controllers.sh') |
| 109 | + spawn_controllers = ExecuteProcess( |
| 110 | + cmd=['bash', controller_script], |
| 111 | + output='screen', |
| 112 | + shell=False |
| 113 | + ) |
| 114 | + |
| 115 | + return LaunchDescription([ |
| 116 | + gazebo, |
| 117 | + robot_state_publisher, |
| 118 | + spawn_entity, |
| 119 | + static_tf, |
| 120 | + gz_ros2_bridge_clock, |
| 121 | + spawn_controllers, # This will run in background and spawn controllers with delays |
| 122 | + ]) |
0 commit comments