当前位置:   article > 正文

通过slam同步建图程序看ros2节点参数传递生效顺序_ros2 launch 节点执行顺序

ros2 launch 节点执行顺序

缘起:

ros2 节点参数可以通过启动节点添加,也可以通过launch文件添加,还可以通过配置文件.yaml文件添加,或者是几种方式套用添加,给初学者带来很大的困惑具体是哪个生效了呢,我们拿slam_toolbox  包的online_sync_launch.py来剖析。

程序安装参考这里

(SLAM) 建图时导航 — Navigation 2 1.0.0 文档

我们这里采用源码编译安装,方便随时修改代码编译运行看效果

源码下载地址:https://github.com/SteveMacenski/slam_toolbox/tree/foxy-develicon-default.png?t=N7T8https://github.com/SteveMacenski/slam_toolbox/tree/foxy-devel

 文件目录结构

 online_sync_launch.py  # 同步建图launch文件源码

  1. import os
  2. from launch import LaunchDescription
  3. from launch.actions import DeclareLaunchArgument, LogInfo
  4. from launch.conditions import UnlessCondition
  5. from launch.substitutions import LaunchConfiguration, PythonExpression
  6. from launch_ros.actions import Node
  7. from ament_index_python.packages import get_package_share_directory
  8. from nav2_common.launch import HasNodeParams
  9. def generate_launch_description():
  10. use_sim_time = LaunchConfiguration('use_sim_time')
  11. params_file = LaunchConfiguration('params_file')
  12. default_params_file = os.path.join(get_package_share_directory("slam_toolbox"),
  13. 'config', 'mapper_params_online_sync.yaml')
  14. declare_use_sim_time_argument = DeclareLaunchArgument(
  15. 'use_sim_time',
  16. default_value='true',
  17. description='Use simulation/Gazebo clock')
  18. declare_params_file_cmd = DeclareLaunchArgument(
  19. 'params_file',
  20. default_value=default_params_file,
  21. description='Full path to the ROS2 parameters file to use for the slam_toolbox node')
  22. # If the provided param file doesn't have slam_toolbox params, we must pass the
  23. # default_params_file instead. This could happen due to automatic propagation of
  24. # LaunchArguments. See:
  25. # https://github.com/ros-planning/navigation2/pull/2243#issuecomment-800479866
  26. has_node_params = HasNodeParams(source_file=params_file,
  27. node_name='slam_toolbox')
  28. actual_params_file = PythonExpression(['"', params_file, '" if ', has_node_params,
  29. ' else "', default_params_file, '"'])
  30. log_param_change = LogInfo(msg=['provided params_file ', params_file,
  31. ' does not contain slam_toolbox parameters. Using default: ',
  32. default_params_file],
  33. condition=UnlessCondition(has_node_params))
  34. start_sync_slam_toolbox_node = Node(
  35. parameters=[
  36. actual_params_file,
  37. {'use_sim_time': use_sim_time}
  38. ],
  39. package='slam_toolbox',
  40. executable='sync_slam_toolbox_node',
  41. name='slam_toolbox',
  42. output='screen')
  43. ld = LaunchDescription()
  44. ld.add_action(declare_use_sim_time_argument)
  45. ld.add_action(declare_params_file_cmd)
  46. ld.add_action(log_param_change)
  47. ld.add_action(start_sync_slam_toolbox_node)
  48. return ld

这里我们只看节点配置部分

  1. start_sync_slam_toolbox_node = Node(
  2. parameters=[
  3. actual_params_file,
  4. {'use_sim_time': use_sim_time}
  5. ],
  6. package='slam_toolbox',
  7. executable='sync_slam_toolbox_node',
  8. name='slam_toolbox',
  9. output='screen')

lanuch文件节点参数传递语句:

parameters=[

actual_params_file,

{'use_sim_time': use_sim_time}

],

这里我们只拿最常用的一个参数use_sim_time举例,看看是哪一步的设置生效了,其他参数逻辑是一样的。

启动这个节点

ros2 launch slam_toolbox online_sync_launch.py

  1. [INFO] [launch]: All log files can be found below /home/m/.ros/log/2023-09-06-01-39-24-649989-ubun-44795
  2. [INFO] [launch]: Default logging verbosity is set to INFO
  3. [INFO] [sync_slam_toolbox_node-1]: process started with pid [44797]
  4. [sync_slam_toolbox_node-1] [INFO] [1693935564.841328831] [slam_toolbox]: Node using stack size 40000000
  5. [sync_slam_toolbox_node-1] [INFO] [1693935564.869237603] [slam_toolbox]: Using solver plugin solver_plugins::CeresSolver
  6. [sync_slam_toolbox_node-1] [INFO] [1693935564.869558263] [slam_toolbox]: CeresSolver: Using SCHUR_JACOBI preconditioner.


查看节点下载参数

m@ubun:~/ros2_ws$ ros2 node list
/slam_toolbox
/transform_listener_impl_563889db4b50
m@ubun:~/ros2_ws$ ros2 param dump slam_toolbox
Saving to:  ./slam_toolbox.yaml

下载的slam_toolbox节点参数详情

  1. /slam_toolbox:
  2. ros__parameters:
  3. angle_variance_penalty: 1.0
  4. base_frame: base_footprint
  5. ceres_dogleg_type: TRADITIONAL_DOGLEG
  6. ceres_linear_solver: SPARSE_NORMAL_CHOLESKY
  7. ceres_loss_function: None
  8. ceres_preconditioner: SCHUR_JACOBI
  9. ceres_trust_strategy: LEVENBERG_MARQUARDT
  10. coarse_angle_resolution: 0.0349
  11. coarse_search_angle_offset: 0.349
  12. correlation_search_space_dimension: 0.5
  13. correlation_search_space_resolution: 0.01
  14. correlation_search_space_smear_deviation: 0.1
  15. debug_logging: false
  16. distance_variance_penalty: 0.5
  17. do_loop_closing: true
  18. enable_interactive_mode: true
  19. fine_search_angle_offset: 0.00349
  20. interactive_mode: false
  21. link_match_minimum_response_fine: 0.1
  22. link_scan_maximum_distance: 1.5
  23. loop_match_maximum_variance_coarse: 3.0
  24. loop_match_minimum_chain_size: 10
  25. loop_match_minimum_response_coarse: 0.35
  26. loop_match_minimum_response_fine: 0.45
  27. loop_search_maximum_distance: 3.0
  28. loop_search_space_dimension: 8.0
  29. loop_search_space_resolution: 0.05
  30. loop_search_space_smear_deviation: 0.03
  31. map_file_name: ''
  32. map_frame: map
  33. map_name: /map
  34. map_start_at_dock: null
  35. map_start_pose: null
  36. map_update_interval: 5.0
  37. minimum_angle_penalty: 0.9
  38. minimum_distance_penalty: 0.5
  39. minimum_time_interval: 0.5
  40. minimum_travel_distance: 0.5
  41. minimum_travel_heading: 0.5
  42. mode: mapping
  43. odom_frame: odom
  44. paused_new_measurements: false
  45. paused_processing: false
  46. resolution: 0.05
  47. scan_buffer_maximum_scan_distance: 10.0
  48. scan_buffer_size: 10
  49. scan_topic: /scan
  50. solver_plugin: solver_plugins::CeresSolver
  51. tf_buffer_duration: 30.0
  52. throttle_scans: 1
  53. transform_publish_period: 0.02
  54. transform_timeout: 0.2
  55. use_response_expansion: true
  56. use_scan_barycenter: true
  57. use_scan_matching: true
  58. use_sim_time: true

看最后一项:

use_sim_time: true

launch文件默认设置:

  1. declare_use_sim_time_argument = DeclareLaunchArgument(
  2. 'use_sim_time',
  3. default_value='true',
  4. description='Use simulation/Gazebo clock')

如果我们启动这个launch文件加上参数

 m@ubun:~/ros2_ws$ ros2 launch slam_toolbox online_sync_launch.py use_sim_time:=false

  1. [INFO] [launch]: All log files can be found below /home/m/.ros/log/2023-09-06-01-48-36-038660-ubun-45408
  2. [INFO] [launch]: Default logging verbosity is set to INFO
  3. [INFO] [sync_slam_toolbox_node-1]: process started with pid [45410]
  4. [sync_slam_toolbox_node-1] [INFO] [1693936116.286793795] [slam_toolbox]: Node using stack size 40000000
  5. [sync_slam_toolbox_node-1] [INFO] [1693936116.307085638] [slam_toolbox]: Using solver plugin solver_plugins::CeresSolver
  6. [sync_slam_toolbox_node-1] [INFO] [1693936116.307325365] [slam_toolbox]: CeresSolver: Using SCHUR_JACOBI preconditioner.

再次下载参数配置文件

m@ubun:~/ros2_ws$ ros2 param dump slam_toolbox
Saving to:  ./slam_toolbox.yaml

  1. /slam_toolbox:
  2. ros__parameters:
  3. angle_variance_penalty: 1.0
  4. base_frame: base_footprint
  5. ceres_dogleg_type: TRADITIONAL_DOGLEG
  6. ceres_linear_solver: SPARSE_NORMAL_CHOLESKY
  7. ceres_loss_function: None
  8. ceres_preconditioner: SCHUR_JACOBI
  9. ceres_trust_strategy: LEVENBERG_MARQUARDT
  10. coarse_angle_resolution: 0.0349
  11. coarse_search_angle_offset: 0.349
  12. correlation_search_space_dimension: 0.5
  13. correlation_search_space_resolution: 0.01
  14. correlation_search_space_smear_deviation: 0.1
  15. debug_logging: false
  16. distance_variance_penalty: 0.5
  17. do_loop_closing: true
  18. enable_interactive_mode: true
  19. fine_search_angle_offset: 0.00349
  20. interactive_mode: false
  21. link_match_minimum_response_fine: 0.1
  22. link_scan_maximum_distance: 1.5
  23. loop_match_maximum_variance_coarse: 3.0
  24. loop_match_minimum_chain_size: 10
  25. loop_match_minimum_response_coarse: 0.35
  26. loop_match_minimum_response_fine: 0.45
  27. loop_search_maximum_distance: 3.0
  28. loop_search_space_dimension: 8.0
  29. loop_search_space_resolution: 0.05
  30. loop_search_space_smear_deviation: 0.03
  31. map_file_name: ''
  32. map_frame: map
  33. map_name: /map
  34. map_start_at_dock: null
  35. map_start_pose: null
  36. map_update_interval: 5.0
  37. minimum_angle_penalty: 0.9
  38. minimum_distance_penalty: 0.5
  39. minimum_time_interval: 0.5
  40. minimum_travel_distance: 0.5
  41. minimum_travel_heading: 0.5
  42. mode: mapping
  43. odom_frame: odom
  44. paused_new_measurements: false
  45. paused_processing: false
  46. resolution: 0.05
  47. scan_buffer_maximum_scan_distance: 10.0
  48. scan_buffer_size: 10
  49. scan_topic: /scan
  50. solver_plugin: solver_plugins::CeresSolver
  51. tf_buffer_duration: 30.0
  52. throttle_scans: 1
  53. transform_publish_period: 0.02
  54. transform_timeout: 0.2
  55. use_response_expansion: true
  56. use_scan_barycenter: true
  57. use_scan_matching: true
  58. use_sim_time: false

use_sim_time: false   

重新修改参数启动节点

m@ubun:~/ros2_ws$ ros2 launch slam_toolbox online_sync_launch.py use_sim_time:=true

再次下载配置文件

use_sim_time: true 

这里可以看出use_sim_time 的值生效顺序ros2 launch slam_toolbox online_sync_launch.py use_sim_time:=true 或者use_sim_time:=false 是大于launch内的默认配置的。所以我们可以不管launch内的默认配置,直接在启动launch文件时在后面加上参数配置。

这里还有另外一种情况,launch内导入了配置文件的.yaml文件,我们还拿use_sim_time举例。

  1. start_sync_slam_toolbox_node = Node(
  2. parameters=[
  3. actual_params_file,
  4. {'use_sim_time': use_sim_time}
  5. ],

actual_params_file其实就是导入的下面的文件

default_params_file = os.path.join(get_package_share_directory("slam_toolbox"),

'config', 'mapper_params_online_sync.yaml')

mapper_params_online_sync.yaml

  1. slam_toolbox:
  2. ros__parameters:
  3. # Plugin params
  4. solver_plugin: solver_plugins::CeresSolver
  5. ceres_linear_solver: SPARSE_NORMAL_CHOLESKY
  6. ceres_preconditioner: SCHUR_JACOBI
  7. ceres_trust_strategy: LEVENBERG_MARQUARDT
  8. ceres_dogleg_type: TRADITIONAL_DOGLEG
  9. ceres_loss_function: None
  10. # ROS Parameters
  11. odom_frame: odom
  12. map_frame: map
  13. base_frame: base_footprint
  14. scan_topic: /scan
  15. mode: mapping #localization
  16. # if you'd like to immediately start continuing a map at a given pose
  17. # or at the dock, but they are mutually exclusive, if pose is given
  18. # will use pose
  19. #map_file_name: test_steve
  20. #map_start_pose: [0.0, 0.0, 0.0]
  21. #map_start_at_dock: true
  22. debug_logging: false
  23. throttle_scans: 1
  24. transform_publish_period: 0.02 #if 0 never publishes odometry
  25. map_update_interval: 5.0
  26. resolution: 0.05
  27. max_laser_range: 20.0 #for rastering images
  28. minimum_time_interval: 0.5
  29. transform_timeout: 0.2
  30. tf_buffer_duration: 30.
  31. stack_size_to_use: 40000000 #// program needs a larger stack size to serialize large maps
  32. enable_interactive_mode: true
  33. # General Parameters
  34. use_scan_matching: true
  35. use_scan_barycenter: true
  36. minimum_travel_distance: 0.5
  37. minimum_travel_heading: 0.5
  38. scan_buffer_size: 10
  39. scan_buffer_maximum_scan_distance: 10.0
  40. link_match_minimum_response_fine: 0.1
  41. link_scan_maximum_distance: 1.5
  42. loop_search_maximum_distance: 3.0
  43. do_loop_closing: true
  44. loop_match_minimum_chain_size: 10
  45. loop_match_maximum_variance_coarse: 3.0
  46. loop_match_minimum_response_coarse: 0.35
  47. loop_match_minimum_response_fine: 0.45
  48. # Correlation Parameters - Correlation Parameters
  49. correlation_search_space_dimension: 0.5
  50. correlation_search_space_resolution: 0.01
  51. correlation_search_space_smear_deviation: 0.1
  52. # Correlation Parameters - Loop Closure Parameters
  53. loop_search_space_dimension: 8.0
  54. loop_search_space_resolution: 0.05
  55. loop_search_space_smear_deviation: 0.03
  56. # Scan Matcher Parameters
  57. distance_variance_penalty: 0.5
  58. angle_variance_penalty: 1.0
  59. fine_search_angle_offset: 0.00349
  60. coarse_search_angle_offset: 0.349
  61. coarse_angle_resolution: 0.0349
  62. minimum_angle_penalty: 0.9
  63. minimum_distance_penalty: 0.5
  64. use_response_expansion: true
  65. #临时在下面添加的参数
  66. use_sim_time: true

我们在.yaml后面添加了use_sim_time: true

再次启动节点

ros2 launch slam_toolbox online_sync_launch.py use_sim_time:=true 或者use_sim_time:=false

下载参数

ros2 param dump slam_toolbox
Saving to:  ./slam_toolbox.yaml 

  1. /slam_toolbox:
  2. ros__parameters:
  3. angle_variance_penalty: 1.0
  4. base_frame: base_footprint
  5. ceres_dogleg_type: TRADITIONAL_DOGLEG
  6. ceres_linear_solver: SPARSE_NORMAL_CHOLESKY
  7. ceres_loss_function: None
  8. ceres_preconditioner: SCHUR_JACOBI
  9. ceres_trust_strategy: LEVENBERG_MARQUARDT
  10. coarse_angle_resolution: 0.0349
  11. coarse_search_angle_offset: 0.349
  12. correlation_search_space_dimension: 0.5
  13. correlation_search_space_resolution: 0.01
  14. correlation_search_space_smear_deviation: 0.1
  15. debug_logging: false
  16. distance_variance_penalty: 0.5
  17. do_loop_closing: true
  18. enable_interactive_mode: true
  19. fine_search_angle_offset: 0.00349
  20. interactive_mode: false
  21. link_match_minimum_response_fine: 0.1
  22. link_scan_maximum_distance: 1.5
  23. loop_match_maximum_variance_coarse: 3.0
  24. loop_match_minimum_chain_size: 10
  25. loop_match_minimum_response_coarse: 0.35
  26. loop_match_minimum_response_fine: 0.45
  27. loop_search_maximum_distance: 3.0
  28. loop_search_space_dimension: 8.0
  29. loop_search_space_resolution: 0.05
  30. loop_search_space_smear_deviation: 0.03
  31. map_file_name: ''
  32. map_frame: map
  33. map_name: /map
  34. map_start_at_dock: null
  35. map_start_pose: null
  36. map_update_interval: 5.0
  37. minimum_angle_penalty: 0.9
  38. minimum_distance_penalty: 0.5
  39. minimum_time_interval: 0.5
  40. minimum_travel_distance: 0.5
  41. minimum_travel_heading: 0.5
  42. mode: mapping
  43. odom_frame: odom
  44. paused_new_measurements: false
  45. paused_processing: false
  46. resolution: 0.05
  47. scan_buffer_maximum_scan_distance: 10.0
  48. scan_buffer_size: 10
  49. scan_topic: /scan
  50. solver_plugin: solver_plugins::CeresSolver
  51. tf_buffer_duration: 30.0
  52. throttle_scans: 1
  53. transform_publish_period: 0.02
  54. transform_timeout: 0.2
  55. use_response_expansion: true
  56. use_scan_barycenter: true
  57. use_scan_matching: true
  58. use_sim_time: true

不管怎么修改launch的启动参数,use_sim_time: true不变,说明launch文件内加载的.yaml 的权重是大于launch文件后面的启动参数的。

注意我们每次修改完程序都需要重新编译,要不然修改是不生效的。

我们这么反复测试只是为了验证参数在传递过程中哪里生效了,因为launch文件经过层层套娃,一个参数传递了好多次。

参数生效顺序 launch内的.yaml文件大于 ros2 launch slam_toolbox online_sync_launch.py use_sim_time:=true  大于launch文件内的默认参数配置

declare_use_sim_time_argument = DeclareLaunchArgument(

'use_sim_time',

default_value='true',

description='Use simulation/Gazebo clock')

虽然yaml文件的权重最大,但是yaml文件可以在launch文件内进行替换,

  1. # Create our own temporary YAML files that include substitutions
  2. # 创建我们自己的临时YAML文件,其中包括替换
  3. param_substitutions = {
  4. 'use_sim_time': use_sim_time,
  5. 'yaml_filename': map_yaml_file}
  6. configured_params = RewrittenYaml(
  7. source_file=params_file,
  8. root_key=namespace,
  9. param_rewrites=param_substitutions,
  10. convert_types=True)

经过这样替换yaml文件的某些参数,使yaml文件的某些参数权重不再是最大。而变成

ros2 launch slam_toolbox online_sync_launch.py use_sim_time:=true  这样的后缀参数权重最大。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/555360
推荐阅读
相关标签
  

闽ICP备14008679号