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

Revision history [back]

  • The nav2_map_saver CLI for sure doesn't work, as it doesn't have the capability of setting a custom timeout.

    $ ros2 run nav2_map_server map_saver_cli -h
    
    Usage:
    map_saver_cli [arguments] [--ros-args ROS remapping args]
    
    Arguments:
      -h/--help
      -t <map_topic>
      -f <mapname>
      --occ <threshold_occupied>
      --free <threshold_free>
      --fmt <image_format>
      --mode trinary(default)/scale/raw
    
    NOTE: --ros-args should be passed at the end of command line
    
  • So we create a custom launch file with a parametric YAML file that will help us to setup our timeout argument.

     The Launch File: - let's say map_saver.launch.py

#!/usr/bin/env python3
import os
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from ament_index_python.packages import get_package_share_directory
from launch_ros.actions import Node

def generate_launch_description():

    # .................. Configurable Arguments .....................

    map_saver_params_file = 'map_saver_params.yaml'
    # ...............................................................


    pkg_dir = get_package_share_directory('your_ROS2_pkg')
    map_save_config = os.path.join(pkg_dir, 'path_to_your_config_file', map_saver_params_file)


    return LaunchDescription([

        DeclareLaunchArgument("map_saver_params_file", default_value=map_save_config, description="Map Saver Configuration File"),

        Node(
            package='nav2_map_server',
            executable='map_saver_server',
            output='screen',
            emulate_tty=True,  
            parameters=[LaunchConfiguration('map_saver_params_file')]                  
        ),

        Node(
            package='nav2_lifecycle_manager',
            executable='lifecycle_manager',
            name='lifecycle_manager',
            output='screen',
            emulate_tty=True,  
            parameters=[
                {'use_sim_time': True},
                {'autostart': True},
                {'node_names': ['map_saver']}]
        )

    ])


     The Configuration FIle - map_saver_params.yaml

map_saver:
  ros__parameters:
    use_sim_time: True
    save_map_timeout: 5000 # The glorious timeout parameter
    free_thresh_default: 0.25
    occupied_thresh_default: 0.65


* Execute the launch file once you're done mapping the environment.

* Finally, on a separate terminal, call the service to generate your map:

    $ ros2 service call /map_saver/save_map nav2_msgs/srv/SaveMap "{map_topic: map, map_url: my_map, image_format: pgm, map_mode: trinary, free_thresh: 0.25, occupied_thresh: 0.65}"


References


Note: Please let me know if this solution doesn't work out for you. If in case it does, feel free to upvote my answer.

  • The nav2_map_saver CLI for sure doesn't work, as it doesn't have the capability of setting a custom timeout.

    $ ros2 run nav2_map_server map_saver_cli -h
    
    Usage:
    map_saver_cli [arguments] [--ros-args ROS remapping args]
    
    Arguments:
      -h/--help
      -t <map_topic>
      -f <mapname>
      --occ <threshold_occupied>
      --free <threshold_free>
      --fmt <image_format>
      --mode trinary(default)/scale/raw
    
    NOTE: --ros-args should be passed at the end of command line
    
  • So we create a custom launch file with a parametric YAML file that will help us to setup our timeout argument.

     The Launch File: - let's say map_saver.launch.py

#!/usr/bin/env python3
import os
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from ament_index_python.packages import get_package_share_directory
from launch_ros.actions import Node

def generate_launch_description():

    # .................. Configurable Arguments .....................

    use_sim_time = True
    map_saver_params_file = 'map_saver_params.yaml'
    # ...............................................................


    pkg_dir = get_package_share_directory('your_ROS2_pkg')
    map_save_config = os.path.join(pkg_dir, 'path_to_your_config_file', map_saver_params_file)


    return LaunchDescription([

        DeclareLaunchArgument("use_sim_time", default_value=str(use_sim_time), description="Use simulation/Gazebo clock"),
        DeclareLaunchArgument("map_saver_params_file", default_value=map_save_config, description="Map Saver Configuration File"),

        Node(
            package='nav2_map_server',
            executable='map_saver_server',
            output='screen',
            emulate_tty=True,  
            parameters=[LaunchConfiguration('map_saver_params_file')]                  
        ),

        Node(
            package='nav2_lifecycle_manager',
            executable='lifecycle_manager',
            name='lifecycle_manager',
            output='screen',
            emulate_tty=True,  
            parameters=[
                {'use_sim_time': True},
LaunchConfiguration('use_sim_time')},
                {'autostart': True},
                {'node_names': ['map_saver']}]
        )

    ])


     The Configuration FIle - map_saver_params.yaml

map_saver:
  ros__parameters:
    use_sim_time: True
    save_map_timeout: 5000 # The glorious timeout parameter
    free_thresh_default: 0.25
    occupied_thresh_default: 0.65


* Execute the launch file once you're done mapping the environment.

* Finally, on a separate terminal, call the service to generate your map:

    $ ros2 service call /map_saver/save_map nav2_msgs/srv/SaveMap "{map_topic: map, map_url: my_map, image_format: pgm, map_mode: trinary, free_thresh: 0.25, occupied_thresh: 0.65}"


References


Note: Please let me know if this solution doesn't work out for you. If in case it does, feel free to upvote my answer.