Advertisement
Guest User

Gazebo ROS2 launch file

a guest
Apr 8th, 2022
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.76 KB | None | 0 0
  1. import os
  2. from ament_index_python.packages import get_package_share_path
  3. from launch import LaunchDescription
  4. from launch.actions import DeclareLaunchArgument, ExecuteProcess, IncludeLaunchDescription
  5. from launch.conditions import IfCondition, UnlessCondition
  6. from launch.launch_description_sources import PythonLaunchDescriptionSource
  7. from launch.substitutions import Command, LaunchConfiguration, PythonExpression
  8.  
  9. from launch_ros.actions import Node
  10. from launch_ros.parameter_descriptions import ParameterValue
  11.  
  12.  
  13. def generate_launch_description():
  14.     gazebo_ros_pkg = get_package_share_path('gazebo_ros')
  15.     gantry_pkg_path = get_package_share_path('gantry_controller')
  16.     model_path = os.path.join(gantry_pkg_path, 'urdf/gantry.xacro')
  17.     rviz_config_path = os.path.join(gantry_pkg_path, 'rviz/urdf.rviz')
  18.     # TODO: Add world sdf file location and file name.
  19.  
  20.     # Launch configuration variables. Commands to declare these follow
  21.     # below.
  22.     model = LaunchConfiguration('model')
  23.     rviz_config_file = LaunchConfiguration('rviz_config_file')
  24.     gui = LaunchConfiguration('gui')
  25.     headless = LaunchConfiguration('headless')
  26.     use_robot_state_pub = LaunchConfiguration('use_robot_state_pub')
  27.     use_rviz = LaunchConfiguration('use_rviz')
  28.     use_sim_time = LaunchConfiguration('use_sim_time')
  29.     use_simulator = LaunchConfiguration('use_simulator')
  30.  
  31.     # Commands to define the launch arguments.
  32.     declare_model_cmd = DeclareLaunchArgument(
  33.         name='model',
  34.         default_value=str(model_path),
  35.         description='Absolute path to robot URDF file')
  36.     declare_rviz_config_file_cmd = DeclareLaunchArgument(
  37.         name='rviz_config_file',
  38.         default_value=str(rviz_config_path),
  39.         description='Absolute path to rviz config file')
  40.     declare_gui_cmd = DeclareLaunchArgument(
  41.         name='gui',
  42.         default_value='True',
  43.         choices=['True', 'False'],
  44.         description='Flag to enable joint_state_publisher_gui')
  45.     declare_headless_cmd = DeclareLaunchArgument(
  46.         name='headless',
  47.         default_value='False',
  48.         choices=['True', 'False'],
  49.         description='Whether to execute gzclient')
  50.     declare_use_robot_state_pub_cmd = DeclareLaunchArgument(
  51.         name='use_robot_state_pub',
  52.         default_value='True',
  53.         choices=['True', 'False'],
  54.         description='Flag to enable robot_state_publisher')
  55.     declare_user_rviz_cmd = DeclareLaunchArgument(
  56.         name='use_rviz',
  57.         default_value='True',
  58.         choices=['True', 'False'],
  59.         description='Whether to start RVIZ')
  60.     declare_use_sim_time_cmd = DeclareLaunchArgument(
  61.         name='use_sim_time',
  62.         default_value='True',
  63.         choices=['True', 'False'],
  64.         description='Flag to use Gazebo simulation clock')
  65.     declare_use_simulator_cmd = DeclareLaunchArgument(
  66.         name='use_simulator',
  67.         default_value='True',
  68.         choices=['True', 'False'],
  69.         description='Flag to start the simulator')
  70.  
  71.     # Specify the launch actions.
  72.     # Start the gazebo server.
  73.     # TODO: Add world to load: launch_arguments={'world': world}.items())
  74.  
  75.     # TODO: Is there are cleaner way to launch gazebo? (launch files?)
  76.     gazebo_launch_cmd = ExecuteProcess(
  77.         cmd=['gazebo',
  78.              '--verbose',
  79.              '-s', 'libgazebo_ros_init.so',
  80.              '-s', 'libgazebo_ros_factory.so'],
  81.         output='screen')
  82.  
  83.     # Robot state publisher
  84.     robot_description = ParameterValue(Command(['xacro ', model]), value_type=str)
  85.     robot_state_publisher_node = Node(
  86.         condition=IfCondition(use_robot_state_pub),
  87.         package='robot_state_publisher',
  88.         executable='robot_state_publisher',
  89.         parameters=[{'use_sim_time': use_sim_time,
  90.                      'robot_description': robot_description}],
  91.         arguments=[model_path])
  92.  
  93.     # Spawn the robot.
  94.     spawn_entity = Node(
  95.         package='gazebo_ros',
  96.         executable='spawn_entity.py',
  97.         arguments=['-entity', 'gantry', '-topic', 'robot_description'],
  98.         output='screen')
  99.  
  100.     # Launch RViz
  101.     # rviz_node = Node(
  102.     #     condition=IfCondition(use_rviz),
  103.     #     package='rviz2',
  104.     #     executable='rviz2',
  105.     #     name='rviz2',
  106.     #     output='screen',
  107.     #     arguments=['-d', rviz_config_file])
  108.  
  109.     return LaunchDescription([
  110.         # Set up launch arguments.
  111.         declare_model_cmd,
  112.         declare_rviz_config_file_cmd,
  113.         declare_gui_cmd,
  114.         declare_headless_cmd,
  115.         declare_use_robot_state_pub_cmd,
  116.         declare_user_rviz_cmd,
  117.         declare_use_sim_time_cmd,
  118.         declare_use_simulator_cmd,
  119.         # Startup actions.
  120.         gazebo_launch_cmd,
  121.         robot_state_publisher_node,
  122.         spawn_entity,
  123.         # rviz_node,
  124.     ])
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement