How to convert launch file ros1 to ros2

666 views Asked by At

I have the following launch file written in XML format working fine in ros1 noetic I am not able to launch it in ros2.

<launch>
  <arg
    name="model" />
  <param
    name="robot_description"
    textfile="$(find mobile_robot)/urdf/mobile_robot.urdf" />
  <node
    name="joint_state_publisher_gui"
    pkg="joint_state_publisher_gui"
    type="joint_state_publisher_gui" />
  <node
    name="robot_state_publisher"
    pkg="robot_state_publisher"
    type="robot_state_publisher" />
  <node
    name="rviz"
    pkg="rviz"
    type="rviz"
    args="-d $(find mobile_robot)/urdf.rviz" />
</launch>
2

There are 2 answers

0
BTables On

You should check out the official documentation for converting launch files. The tl;dr version is that the type tag has been changed to exec, ns to namespace. There is also no global params, and the textfile param type has been removed(both are relevant to your code). And finally, rviz is not a ros2 package; it's now rviz2.

I'd also suggest working on the formatting of your launch files for future posts, this doesn't follow the ros formatting standards and is fairly hard to read.

0
nullbyte91 On

You can write something like this,

import os

from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node
from launch_ros.actions import Node

def generate_launch_description():
    use_sim_time = LaunchConfiguration('use_sim_time', default='true')
    urdf = os.path.join(
    get_package_share_directory('mobile_robot'),
    'urdf',
    'mobile_robot.urdf')
    
    rviz = os.path.join(
    get_package_share_directory('mobile_robot'), 'urdf.rviz')          
    return LaunchDescription([
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource(
                os.path.join(pkg_gazebo_ros, 'launch', 'gzserver.launch.py')
            ),
            launch_arguments={'world': world}.items(),
        ),

        IncludeLaunchDescription(
            PythonLaunchDescriptionSource(
                os.path.join(pkg_gazebo_ros, 'launch', 'gzclient.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='rviz2',
            executable='rviz2',
            name='rviz2',
            arguments=['-d', rviz],
            output='screen'),
    ])