Error when launch a normal file

Hi,

I created an ament-cmake package to do exercise 2.1

But when I launch a launch file, the error happens.

This is my launch file:

import os

from launch import LaunchDescription
from launch_ros.actions import Node

from ament_index_python.packages import get_package_share_directory

def generate_launch_description():

# region cartographer_ros
mapping_cartographer_package = "mapping_cartographer"
mapping_cartographer_sharePath = get_package_share_directory(mapping_cartographer_package)
mapping_cartographer_paramDirectory = os.path.join(mapping_cartographer_sharePath, "param")
mapping_cartographer_paramFileName = "mapping_cartographer.lua"

cartographer_node = Node(
    name        = 'cartographer_node',
    package     = 'cartographer_ros',
    executable  = 'cartographer_node',
    output      = 'screen',
    arguments   = [
        '-configuration_directory', mapping_cartographer_paramDirectory,
        '-configuration_basename', mapping_cartographer_paramFileName
    ],
    parameters=[
        {
            'use_sim_time': True
        }
    ],
    # remappings=[
    #     ('/old_topic_name', '/new_topic_name'),
    # ]
),
# endregion

# region occupancy_grid_node
occupancy_grid_node = Node(
    name        = 'occupancy_grid_node',
    package     = 'cartographer_ros',
    executable  = 'cartographer_occupancy_grid_node',
    output      ='screen',
    arguments   = [
        '-resolution', '0.05',
        '-publish_period_sec', '1.0'
    ],
    parameters=[
        {
            'use_sim_time': True
        },
    ],
    # remappings=[
    #     ('/old_topic_name', '/new_topic_name'),
    # ]
)
# endregion



return LaunchDescription(
    [
        cartographer_node,
        occupancy_grid_node
    ]
)

Hi, make sure you wrap your pasted code under three ` symbols to improve readability.

This is most likely an issue on the structure of your .launch.py. Compare with this correct structure:

import os
from launch import LaunchDescription
from ament_index_python.packages import get_package_share_directory
from launch_ros.actions import Node


def generate_launch_description():

    cartographer_config_dir = os.path.join(
        get_package_share_directory("cartographer_ros"), "config"
    )
    configuration_basename = "/path/to/cartographer.lua"

    return LaunchDescription(
        [
            Node(
                package="cartographer_ros",
                executable="cartographer_node",
                name="cartographer_node",
                output="screen",
                parameters=[{"use_sim_time": False}],
                arguments=[
                    "-configuration_directory",
                    cartographer_config_dir,
                    "-configuration_basename",
                    configuration_basename,
                ],
            ),
            Node(
                package="cartographer_ros",
                executable="cartographer_occupancy_grid_node",
                output="screen",
                name="occupancy_grid_node",
                parameters=[{"use_sim_time": False}],
                remappings=[],
            ),
        ]
    )

Hi

I found out the problem.

I create node then return the node at the end. So I must not use , after the command. It is the python error.

But If I create node in return command, I use , to seperate nodes.

Thanks for helping

1 Like