|
| 1 | +# Copyright 2025 AIT Austrian Institute of Technology GmbH |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from launch import LaunchDescription |
| 16 | +from launch.actions import DeclareLaunchArgument, IncludeLaunchDescription |
| 17 | +from launch.actions import RegisterEventHandler |
| 18 | +from launch.event_handlers import OnProcessExit |
| 19 | +from launch.launch_description_sources import PythonLaunchDescriptionSource |
| 20 | +from launch.substitutions import Command, FindExecutable, LaunchConfiguration, PathJoinSubstitution |
| 21 | + |
| 22 | +from launch_ros.actions import Node |
| 23 | +from launch_ros.substitutions import FindPackageShare |
| 24 | + |
| 25 | + |
| 26 | +def generate_launch_description(): |
| 27 | + # Launch Arguments |
| 28 | + use_sim_time = LaunchConfiguration('use_sim_time', default=True) |
| 29 | + gz_args = LaunchConfiguration('gz_args', default='') |
| 30 | + |
| 31 | + # Get URDF via xacro |
| 32 | + robot_description_content = Command( |
| 33 | + [ |
| 34 | + PathJoinSubstitution([FindExecutable(name='xacro')]), |
| 35 | + ' ', |
| 36 | + PathJoinSubstitution( |
| 37 | + [FindPackageShare('gz_ros2_control_demos'), |
| 38 | + 'urdf', 'test_cart_velocity_custom_plugin.xacro.urdf'] |
| 39 | + ), |
| 40 | + ] |
| 41 | + ) |
| 42 | + robot_description = {'robot_description': robot_description_content} |
| 43 | + robot_controllers = PathJoinSubstitution( |
| 44 | + [ |
| 45 | + FindPackageShare('gz_ros2_control_demos'), |
| 46 | + 'config', |
| 47 | + 'cart_controller_velocity.yaml', |
| 48 | + ] |
| 49 | + ) |
| 50 | + |
| 51 | + node_robot_state_publisher = Node( |
| 52 | + package='robot_state_publisher', |
| 53 | + executable='robot_state_publisher', |
| 54 | + output='screen', |
| 55 | + parameters=[robot_description] |
| 56 | + ) |
| 57 | + |
| 58 | + gz_spawn_entity = Node( |
| 59 | + package='ros_gz_sim', |
| 60 | + executable='create', |
| 61 | + output='screen', |
| 62 | + arguments=['-topic', 'robot_description', |
| 63 | + '-name', 'cart', '-allow_renaming', 'true'], |
| 64 | + ) |
| 65 | + |
| 66 | + joint_state_broadcaster_spawner = Node( |
| 67 | + package='controller_manager', |
| 68 | + executable='spawner', |
| 69 | + arguments=['joint_state_broadcaster'], |
| 70 | + ) |
| 71 | + joint_trajectory_controller_spawner = Node( |
| 72 | + package='controller_manager', |
| 73 | + executable='spawner', |
| 74 | + arguments=[ |
| 75 | + 'joint_trajectory_controller', |
| 76 | + '--param-file', |
| 77 | + robot_controllers, |
| 78 | + ], |
| 79 | + ) |
| 80 | + |
| 81 | + # Bridge |
| 82 | + bridge = Node( |
| 83 | + package='ros_gz_bridge', |
| 84 | + executable='parameter_bridge', |
| 85 | + arguments=['/clock@rosgraph_msgs/msg/Clock[gz.msgs.Clock'], |
| 86 | + output='screen' |
| 87 | + ) |
| 88 | + |
| 89 | + return LaunchDescription([ |
| 90 | + # Launch gazebo environment |
| 91 | + IncludeLaunchDescription( |
| 92 | + PythonLaunchDescriptionSource( |
| 93 | + [PathJoinSubstitution([FindPackageShare('ros_gz_sim'), |
| 94 | + 'launch', |
| 95 | + 'gz_sim.launch.py'])]), |
| 96 | + launch_arguments=[('gz_args', [gz_args, ' -r -v 1 empty.sdf'])]), |
| 97 | + RegisterEventHandler( |
| 98 | + event_handler=OnProcessExit( |
| 99 | + target_action=gz_spawn_entity, |
| 100 | + on_exit=[joint_state_broadcaster_spawner], |
| 101 | + ) |
| 102 | + ), |
| 103 | + RegisterEventHandler( |
| 104 | + event_handler=OnProcessExit( |
| 105 | + target_action=joint_state_broadcaster_spawner, |
| 106 | + on_exit=[joint_trajectory_controller_spawner], |
| 107 | + ) |
| 108 | + ), |
| 109 | + bridge, |
| 110 | + node_robot_state_publisher, |
| 111 | + gz_spawn_entity, |
| 112 | + # Launch Arguments |
| 113 | + DeclareLaunchArgument( |
| 114 | + 'use_sim_time', |
| 115 | + default_value=use_sim_time, |
| 116 | + description='If true, use simulated clock'), |
| 117 | + ]) |
0 commit comments