[ROS2] Launch gazebo.launch.py from my own launch file
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
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.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 providerobot_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, )
Thanks so much shonigmann for that elaboration + example!
happy to help - I had the same questions a few weeks ago!