I set use_sim_time for every node to true; however, AMCL seems to use the system time

This message pops up when I push the button ‘2D Pose Estimate.’ There is my launch file, my config file of AMCL, and the launch file for RVIZ2.

I notice that, in your cloud system, you set ‘use_sim_time’ to false. Why?
We are doing a simulation.

I almost confirm that my AMCL uses its parameters rather that the information in the yam file. I change the set_initial_pose to true, it still wants an initialization. I set the map_topic to some random number, it still can load the map.

localization.launch.py

import os

from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch_ros.actions import Node
from launch.substitutions import LaunchConfiguration
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.actions import DeclareLaunchArgument, ExecuteProcess,IncludeLaunchDescription

from nav2_common.launch import RewrittenYaml

def generate_launch_description():

ld = LaunchDescription()

use_sim_time = LaunchConfiguration('use_sim_time', default='true')

# Launch path
default_launch_file_path = os.path.join(get_package_share_directory('robot_bringup'), 'launch')
print(f"The launch folder is at {default_launch_file_path}." )

# Map path
path_for_robot_slam = get_package_share_directory('robot_slam')
default_map_dir = LaunchConfiguration('default_map_dir', default=os.path.join(
                                              path_for_robot_slam, 'maps','official_map','map.yaml'))
declare_default_map_dir = DeclareLaunchArgument(
        'default_map_dir',
        default_value=default_map_dir,
        description='Full path to map file to load')
ld.add_action(declare_default_map_dir)

# obot_localization package path
path_for_robot_localization = get_package_share_directory('robot_localization')

# lifecycle_manager
run_lifecycle_manager_cmd = Node(
    package='nav2_lifecycle_manager',
    executable='lifecycle_manager',
    name='lifecycle_manager_node',
    output='screen',
    parameters=[{'use_sim_time': use_sim_time},
                {'autostart': True},
                {'node_names': ['map_server','amcl_node']}])
ld.add_action(run_lifecycle_manager_cmd)

run_map_server_cmd = Node(
    package='nav2_map_server',
    executable='map_server',
    name='map_server',
    output='screen',
    parameters=[{'use_sim_time': use_sim_time}, 
                {'yaml_filename': default_map_dir}])
ld.add_action(run_map_server_cmd)

amcl_config_dir = os.path.join(path_for_robot_localization, 'config')
print(f"The current AMCL config dir has been redirected to :{amcl_config_dir}.")
params_file = LaunchConfiguration('params_file',default=os.path.join(amcl_config_dir,'amcl.yaml'))
namespace = LaunchConfiguration('namespace',default='')
param_substitutions = {
    'use_sim_time': use_sim_time
}
configured_params = RewrittenYaml(
    source_file=params_file,
    root_key=namespace,
    param_rewrites=param_substitutions,
    convert_types=True)
run_amcl_cmd = Node(
    package='nav2_amcl',
    executable='amcl',
    name='amcl_node',
    output='screen',
    parameters=[configured_params])
ld.add_action(run_amcl_cmd)

#RVIZ
use_rviz = LaunchConfiguration('use_rviz', default='true')

rviz_config_dir = os.path.join(path_for_robot_localization, 'config')
print(f"The current rviz config dir has been redirected to :{rviz_config_dir}.")
rviz_config_file = LaunchConfiguration('rviz_config_file', default=os.path.join(rviz_config_dir,'robot_localization.rviz'))
start_rviz_cmd = IncludeLaunchDescription(
    PythonLaunchDescriptionSource(
        os.path.join(default_launch_file_path, 'rviz2.launch.py')
    ),
    launch_arguments={
        'rviz_config_file': rviz_config_file,
        'use_rviz': use_rviz
    }.items()
)
ld.add_action(start_rviz_cmd)

return ld

amcl.yaml

amcl:
  ros__parameters:
    use_sim_time: true
    alpha1: 0.2
    alpha2: 0.2
    alpha3: 0.2
    alpha4: 0.2
    alpha5: 0.2
    base_frame_id: "base_footprint"
    beam_skip_distance: 0.5
    beam_skip_error_threshold: 0.9
    beam_skip_threshold: 0.3
    do_beamskip: false
    global_frame_id: "map"
    lambda_short: 0.1
    laser_likelihood_max_dist: 2.0
    laser_max_range: 100.0
    laser_min_range: -1.0
    laser_model_type: "likelihood_field"
    max_beams: 60
    max_particles: 8000
    min_particles: 200
    odom_frame_id: "odom"
    pf_err: 0.05
    pf_z: 0.99
    recovery_alpha_fast: 0.0
    recovery_alpha_slow: 0.0
    resample_interval: 1
    robot_model_type: "nav2_amcl::DifferentialMotionModel"
    save_pose_rate: 0.5
    sigma_hit: 0.2
    tf_broadcast: true
    transform_tolerance: 1.0
    update_min_a: 0.2
    update_min_d: 0.25
    z_hit: 0.5
    z_max: 0.05
    z_rand: 0.5
    z_short: 0.05
    scan_topic: scan
    map_topic: map
    set_initial_pose: false
    always_reset_initial_pose: false
    first_map_only: true
    initial_pose:
      x: 0.0
      y: 0.0
      z: 0.0
      yaw: 0.0

rviz2.launch.py

#!/usr/bin/env python3

import os
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, ExecuteProcess,IncludeLaunchDescription,RegisterEventHandler
from ament_index_python.packages import get_package_share_directory
from launch.substitutions import Command, LaunchConfiguration
from launch.conditions import IfCondition, UnlessCondition
from launch_ros.actions import Node
from launch.event_handlers import OnProcessExit

def generate_launch_description():
# Create the launch description and populate
ld = LaunchDescription()

use_sim_time = LaunchConfiguration('use_sim_time', default='true')
declare_use_sim_time = DeclareLaunchArgument(
        'use_sim_time',
        default_value='true',
        description='Use simulation (Gazebo) clock if true')
ld.add_action(declare_use_sim_time)

wait_time = '5s'
print(f"Rviz2 will start before {wait_time}. Rviz runs before other program will course errors.")

# Robot path
TURTLEBOT3_MODEL = os.environ['TURTLEBOT3_MODEL']
path_for_turtlebot_description = get_package_share_directory("turtlebot_description")
default_rviz_config_path = os.path.join(
    path_for_turtlebot_description,
    'rviz',
    'model.rviz')
print(f'Rviz default config file is at : {default_rviz_config_path}.')


# RVIZ.
declare_rviz_config_file_cmd = DeclareLaunchArgument(
    name='rviz_config_file',
    default_value=default_rviz_config_path,
    description='Relative path to the RVIZ config file to use')
ld.add_action(declare_rviz_config_file_cmd)

use_rviz = LaunchConfiguration('use_rviz')
declare_use_rviz_cmd = DeclareLaunchArgument(
    name='use_rviz',
    default_value='True',
    #default_value='False',
    description='Whether to start RVIZ')
ld.add_action(declare_use_rviz_cmd) 
rviz_config_file = LaunchConfiguration('rviz_config_file')
start_rviz_cmd = Node(
    condition=IfCondition(use_rviz),
    package='rviz2',
    executable='rviz2',
    name='rviz2',
    output='screen',
    arguments=['-d', rviz_config_file],
    parameters=[{'use_sim_time': use_sim_time}])
#ld.add_action(start_rviz_cmd)
sleep_cmd = ExecuteProcess(
    cmd=['sleep', wait_time],
    output='screen')
ld.add_action(sleep_cmd)
event_sleep = RegisterEventHandler(
    event_handler=\
        OnProcessExit(target_action=sleep_cmd,on_exit=[start_rviz_cmd])
    )
ld.add_action(event_sleep)

return ld

Hello @bluebird,

you are right, thank you for pointing that out. The repository of the course has been updated.

You can update your course files by running git pull:

Using a WebShell please cd into

~/ros2_ws/src/ros2_nav_files

and run

git pull

This will download the latest version from the remote repository.

I apologize for the inconvenience. Please let me know if there are any other issues,

Roberto

The problem is that I have encountered the same problem at my local Virtual Machine. Can you find some errors in the files that I have provided?

Hello @bluebird,

after having launched all your nodes, what is the output of this command?

ros2 param get /amcl use_sim_time

The problem occurs because the AMCL insists to use its built-in config file.

After multi failures, I have to copy some codes from the nav2_bringup. I make two launch files. One is to directly use the official launch file ‘localization_launch.py.’ Another is to copy some codes from the file ‘localization_launch.py.’ Both files can use the local config file rather than the built-in file.

The problem remains that I don’t know where is wrong in my original launch file.
Professors, it will be very appreciated if you can check the original launch file for me.

localization2.launch.py

#Directly copy

import os

from ament_index_python.packages import get_package_share_directory

from launch import LaunchDescription
from launch.conditions import IfCondition
from launch.substitutions import LaunchConfiguration, PythonExpression
from launch_ros.actions import Node, LoadComposableNodes
from launch_ros.descriptions import ComposableNode
from nav2_common.launch import RewrittenYaml
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.actions import DeclareLaunchArgument, ExecuteProcess,IncludeLaunchDescription,DeclareLaunchArgument, GroupAction, SetEnvironmentVariable

def generate_launch_description():
# Launch path
default_launch_file_path = os.path.join(get_package_share_directory(‘robot_bringup’), ‘launch’)
print(f"The launch folder is at {default_launch_file_path}." )

# Create the launch description and populate
ld = LaunchDescription()
    
use_sim_time = LaunchConfiguration('use_sim_time', default='true')

# Map path
path_for_robot_slam = get_package_share_directory('robot_slam')
default_map_dir = LaunchConfiguration('default_map_dir', default=os.path.join(
                                              path_for_robot_slam, 'maps','official_map','map.yaml'))

path_for_robot_localization = get_package_share_directory('robot_localization')
params_file = LaunchConfiguration('params_file',default=os.path.join(
                                              path_for_robot_localization, 'config','amcl_params.yaml'))    
namespace = LaunchConfiguration('namespace',default='')
param_substitutions = {
    'use_sim_time': use_sim_time,
    'yaml_filename': default_map_dir}

remappings = [('/tf', 'tf'),
              ('/tf_static', 'tf_static')]

configured_params = RewrittenYaml(
    source_file=params_file,
    root_key=namespace,
    param_rewrites=param_substitutions,
    convert_types=True)

run_map_server_cmd = Node(
            package='nav2_map_server',
            executable='map_server',
            name='map_server',
            output='screen',
            parameters=[configured_params],
            remappings=remappings)
ld.add_action(run_map_server_cmd)


run_amcl_cmd = Node(
            package='nav2_amcl',
            executable='amcl',
            name='amcl',
            output='screen',
            parameters=[configured_params],
            remappings=remappings)
ld.add_action(run_amcl_cmd)

lifecycle_nodes = ['map_server', 'amcl']
run_lifecycle_manager_cmd = Node(
            package='nav2_lifecycle_manager',
            executable='lifecycle_manager',
            name='lifecycle_manager_localization',
            output='screen',
            parameters=[{'use_sim_time': use_sim_time},
                        {'autostart': True},
                        {'node_names': lifecycle_nodes}])
ld.add_action(run_lifecycle_manager_cmd)

#RVIZ
use_rviz = LaunchConfiguration('use_rviz', default='true')

rviz_config_dir = os.path.join(path_for_robot_localization, 'config')
print(f"The current rviz config dir has been redirected to :{rviz_config_dir}.")
rviz_config_file = LaunchConfiguration('rviz_config_file', default=os.path.join(rviz_config_dir,'robot_localization.rviz'))
start_rviz_cmd = IncludeLaunchDescription(
    PythonLaunchDescriptionSource(
        os.path.join(default_launch_file_path, 'rviz2.launch.py')
    ),
    launch_arguments={
        'rviz_config_file': rviz_config_file,
        'use_rviz': use_rviz
    }.items()
)
ld.add_action(start_rviz_cmd)


return ld

Hi @bluebird,

I hope you can see that it is really difficult to offer you any pointers to where the problem might be via this forum. We don’t know your exact System/Environment Setup. We don’t know the exact sequence of commands that you are running, what you closed or killed before or why might be still running on the background… We don’t have the content of all files being executed. We don’t see the warnings or error messages that apear on your screen. We cannot run a command like the one I send you:
ros2 param get /amcl use_sim_time
to do the debugging. I hope you understand that the one that has the best chances to debug the program is your self.

After this rather long intro, to your question about your launch file:
I might have overlooked something, and I can’t execute it to see the log messages are, but can say that I quickly skimmed through your launch file and it looks good to me.

Cheers,

Roberto