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

Revision history [back]

click to hide/show revision 1
initial version

Thanks for this post! I struggled a lot to get the map server running in ros2 with a given map file until i found this..

I just wanna add my launch config here, because it does not require the extra param yaml file you used.


My map_server.launch.py file:

import os
import yaml
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import ExecuteProcess
from launch.substitutions import LaunchConfiguration
import launch_ros.actions


def generate_launch_description():

ld = LaunchDescription()

# map file
map_file_path = os.path.join(
    get_package_share_directory('nav2_benchmark'),
    'maps',
    'map.yaml'
)

map_server_cmd = Node(
    package='nav2_map_server',
    executable='map_server',
    output='screen',
    parameters=[{'yaml_filename': map_file_path}])


lifecycle_nodes = ['map_server']
use_sim_time = True
autostart = True

start_lifecycle_manager_cmd = launch_ros.actions.Node(
        package='nav2_lifecycle_manager',
        executable='lifecycle_manager',
        name='lifecycle_manager',
        output='screen',
        emulate_tty=True,  # https://github.com/ros2/launch/issues/188
        parameters=[{'use_sim_time': use_sim_time},
                    {'autostart': autostart},
                    {'node_names': lifecycle_nodes}])


ld.add_action(map_server_cmd)
ld.add_action(start_lifecycle_manager_cmd)

return ld

Note: To make this work using more or less "classic" ROS1 Workflow, you will need to add these lines to your CMakeLists.txt:

# Install the launch file
   install(DIRECTORY launch/
   DESTINATION share/${PROJECT_NAME}/launch
)

# Install the map file
   install(DIRECTORY maps/
   DESTINATION share/${PROJECT_NAME}/maps
)

And this to your package.xml:

<exec_depend>ros2launch</exec_depend>

Now my output looks like this and i can see the map in RVIZ:

marco@md3v360c:~/ros2_ws$ ros2 launch nav2_benchmark map_server.launch.py 
[INFO] [launch]: All log files can be found below /home/marco/.ros/log/2023-07-20-11-47-15-309631-md3v360c-26348
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [lifecycle_manager-2]: process started with pid [26351]
[INFO] [map_server-1]: process started with pid [26349]
[lifecycle_manager-2] [INFO] [1689846435.421007174] [lifecycle_manager]: Creating
[lifecycle_manager-2] [INFO] [1689846435.422937482] [lifecycle_manager]: Creating and initializing lifecycle service clients
[lifecycle_manager-2] [INFO] [1689846435.429748909] [lifecycle_manager]: Starting managed nodes bringup...
[lifecycle_manager-2] [INFO] [1689846435.429841009] [lifecycle_manager]: Configuring map_server
[map_server-1] [INFO] [1689846435.431032914] [map_server]: 
[map_server-1]  map_server lifecycle node launched. 
[map_server-1]  Waiting on external lifecycle transitions to activate
[map_server-1]  See https://design.ros2.org/articles/node_lifecycle.html for more information.
[map_server-1] [INFO] [1689846435.431162214] [map_server]: Creating
[map_server-1] [INFO] [1689846435.431647816] [map_server]: Configuring
[map_server-1] [INFO] [map_io]: Loading yaml file: /home/marco/ros2_ws/install/nav2_benchmark/share/nav2_benchmark/maps/map.yaml
[map_server-1] [DEBUG] [map_io]: resolution: 0.025
[map_server-1] [DEBUG] [map_io]: origin[0]: -8
[map_server-1] [DEBUG] [map_io]: origin[1]: -4
[map_server-1] [DEBUG] [map_io]: origin[2]: 0
[map_server-1] [DEBUG] [map_io]: free_thresh: 0.25
[map_server-1] [DEBUG] [map_io]: occupied_thresh: 0.65
[map_server-1] [DEBUG] [map_io]: mode: trinary
[map_server-1] [DEBUG] [map_io]: negate: 0
[map_server-1] [INFO] [map_io]: Loading image_file: /home/marco/ros2_ws/install/nav2_benchmark/share/nav2_benchmark/maps/modified_map_2.pgm
[map_server-1] [DEBUG] [map_io]: Read map /home/marco/ros2_ws/install/nav2_benchmark/share/nav2_benchmark/maps/modified_map_2.pgm: 549 X 412 map @ 0.025 m/cell
[lifecycle_manager-2] [INFO] [1689846435.453434802] [lifecycle_manager]: Activating map_server
[map_server-1] [INFO] [1689846435.453657303] [map_server]: Activating
[map_server-1] [INFO] [1689846435.454019105] [map_server]: Creating bond (map_server) to lifecycle manager.
[lifecycle_manager-2] [INFO] [1689846435.557139113] [lifecycle_manager]: Server map_server connected with bond.
[lifecycle_manager-2] [INFO] [1689846435.557207313] [lifecycle_manager]: Managed nodes are active
[lifecycle_manager-2] [INFO] [1689846435.557242914] [lifecycle_manager]: Creating bond timer...