ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

[ROS2] Launch gazebo.launch.py from my own launch file

asked 2021-03-29 21:33:07 -0600

808brick gravatar image

In ROS1 it was possible with one launch file to start up gazebo, start the robot state publisher, and then spawn the URDF into gazebo. I can't seem to recreate that functionality in ROS2.

My launch file is as follows:

import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, ExecuteProcess
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node
from launch.launch_description_sources import PythonLaunchDescriptionSource

def generate_launch_description():

  use_sim_time = LaunchConfiguration('use_sim_time', default='false')
  urdf_file_name = 'urdf/camera_bot.xacro'

  print("urdf_file_name : {}".format(urdf_file_name))

  urdf = os.path.join(
      get_package_share_directory('ros2_sim_pkg'),
      urdf_file_name)

  return LaunchDescription([

        DeclareLaunchArgument(
            'use_sim_time',
            default_value='false',
            description='Use simulation (Gazebo) clock if true'),

        # ExecuteProcess(
        #     cmd=["ros2", "launch", "gazebo_ros", "gazebo.launch.py"]
        # ),

        Node(
            package='robot_state_publisher',
            executable='robot_state_publisher',
            name='robot_state_publisher',
            output='screen',
            parameters=[{'use_sim_time': use_sim_time}],
            arguments=[urdf]),

        Node(
            package='gazebo_ros',
            executable='spawn_entity.py',
            name='urdf_spawner',
            output='screen',
            arguments=["-topic", "/robot_description", "-entity", "cam_bot"])
  ])

You can see my attempt in the commented our ExecuteProcess tag, and I also tried including it through IncludeLaunchDescription and got the same error of: ImportError: cannot import name 'GazeboRosPaths' from 'scripts'

I posted a reply to this thread as well https://github.com/ros-simulation/gazebo_ros_pkgs/issues/1050#issuecomment-809852881

For now I have to get around it by simply launching gazebo manually and then running my launch file in a separate terminal. The exact commands I have in different terminals are as follows: ros2 launch gazebo_ros gazebo.launch.py and ros2 launch ros2_sim_pkg cam_bot_world.launch.py

Surely there is a more streamlined method that I am just unaware of?

If anyone has any insight, it would be greatly appreciated.

Edit: It is worth noting that I am on Ubuntu 20, using ROS2 Foxy, installed from pre-built binaries

edit retag flag offensive close merge delete

Comments

On a side note, I have come to find that it seems XACRO is not supported for ros2 launching. My second file was never getting included into the URDF like it does with ROS1, so in this case the camera_bot.xacro file is just a plain old URDF format.

808brick gravatar image 808brick  ( 2021-03-30 03:38:12 -0600 )edit
1

Xacro is indeed supported by ROS2. You can either import xacro in your launch file to parse the URDF, or you can run an inline command to parse the xacro file. In either case, I believe it is now preferred to provide robot_state_publisher with the URDF file contents as a parameter, and not the file path as an argument (the latter results in deprecation warnings if memory serves).

For reference, I use xacro as follows:

start_robot_state_publisher_cmd = Node( package='robot_state_publisher', executable='robot_state_publisher', name='robot_state_publisher', output='screen', parameters=[ {'use_sim_time': use_sim_time}, {'robot_description': Command(['xacro', ' ', urdf, ' gazebo:=False'])} ], remappings=remappings, )

shonigmann gravatar image shonigmann  ( 2021-03-30 12:02:01 -0600 )edit

Thanks so much shonigmann for that elaboration + example!

808brick gravatar image 808brick  ( 2021-03-30 17:24:27 -0600 )edit

happy to help - I had the same questions a few weeks ago!

shonigmann gravatar image shonigmann  ( 2021-03-30 18:16:17 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
4

answered 2021-03-30 03:35:52 -0600

808brick gravatar image

After hours of looking around, I finally found a method that worked. Use this tag within the launch file:

ExecuteProcess( cmd=['gazebo', '--verbose', '-s', 'libgazebo_ros_factory.so'], output='screen'),

For those interested, my full launch file is a follows:

import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, ExecuteProcess
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node
from launch.launch_description_sources import PythonLaunchDescriptionSource

def generate_launch_description():

  use_sim_time = LaunchConfiguration('use_sim_time', default='false')
  urdf_file_name = 'urdf/camera_bot.xacro'

  print("urdf_file_name : {}".format(urdf_file_name))

  urdf = os.path.join(
      get_package_share_directory('ros2_sim_pkg'),
      urdf_file_name)

  return LaunchDescription([

        DeclareLaunchArgument(
            'use_sim_time',
            default_value='false',
            description='Use simulation (Gazebo) clock if true'),

        ExecuteProcess(
            cmd=['gazebo', '--verbose', '-s', 'libgazebo_ros_factory.so'],
            output='screen'),

        Node(
            package='robot_state_publisher',
            executable='robot_state_publisher',
            name='robot_state_publisher',
            output='screen',
            parameters=[{'use_sim_time': use_sim_time}],
            arguments=[urdf]),

        Node(
            package='joint_state_publisher',
            executable='joint_state_publisher',
            name='joint_state_publisher',
            output='screen',
            parameters=[{'use_sim_time': use_sim_time}]
            ),

        Node(
            package='gazebo_ros',
            executable='spawn_entity.py',
            name='urdf_spawner',
            output='screen',
            arguments=["-topic", "/robot_description", "-entity", "cam_bot"])
  ])
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2021-03-29 21:31:28 -0600

Seen: 10,119 times

Last updated: Mar 29 '21