Nav2极简笔记03-启动文件launch

举报
zhangrelay 发表于 2021/08/27 00:15:57 2021/08/27
【摘要】 回顾一下,tb3_simulation_launch.py : """This is all-in-one launch script intended for use by nav2 developers.""" import os from ament_index_python.packages import get_packag...

回顾一下,tb3_simulation_launch.py :


  
  1. """This is all-in-one launch script intended for use by nav2 developers."""
  2. import os
  3. from ament_index_python.packages import get_package_share_directory
  4. from launch import LaunchDescription
  5. from launch.actions import DeclareLaunchArgument, ExecuteProcess, IncludeLaunchDescription
  6. from launch.conditions import IfCondition
  7. from launch.launch_description_sources import PythonLaunchDescriptionSource
  8. from launch.substitutions import LaunchConfiguration, PythonExpression
  9. from launch_ros.actions import Node
  10. def generate_launch_description():
  11. # Get the launch directory
  12. bringup_dir = get_package_share_directory('nav2_bringup')
  13. launch_dir = os.path.join(bringup_dir, 'launch')
  14. # Create the launch configuration variables
  15. slam = LaunchConfiguration('slam')
  16. namespace = LaunchConfiguration('namespace')
  17. use_namespace = LaunchConfiguration('use_namespace')
  18. map_yaml_file = LaunchConfiguration('map')
  19. use_sim_time = LaunchConfiguration('use_sim_time')
  20. params_file = LaunchConfiguration('params_file')
  21. default_bt_xml_filename = LaunchConfiguration('default_bt_xml_filename')
  22. autostart = LaunchConfiguration('autostart')
  23. # Launch configuration variables specific to simulation
  24. rviz_config_file = LaunchConfiguration('rviz_config_file')
  25. use_simulator = LaunchConfiguration('use_simulator')
  26. use_robot_state_pub = LaunchConfiguration('use_robot_state_pub')
  27. use_rviz = LaunchConfiguration('use_rviz')
  28. headless = LaunchConfiguration('headless')
  29. world = LaunchConfiguration('world')
  30. # Map fully qualified names to relative ones so the node's namespace can be prepended.
  31. # In case of the transforms (tf), currently, there doesn't seem to be a better alternative
  32. # https://github.com/ros/geometry2/issues/32
  33. # https://github.com/ros/robot_state_publisher/pull/30
  34. # TODO(orduno) Substitute with `PushNodeRemapping`
  35. # https://github.com/ros2/launch_ros/issues/56
  36. remappings = [('/tf', 'tf'),
  37. ('/tf_static', 'tf_static')]
  38. # Declare the launch arguments
  39. declare_namespace_cmd = DeclareLaunchArgument(
  40. 'namespace',
  41. default_value='',
  42. description='Top-level namespace')
  43. declare_use_namespace_cmd = DeclareLaunchArgument(
  44. 'use_namespace',
  45. default_value='false',
  46. description='Whether to apply a namespace to the navigation stack')
  47. declare_slam_cmd = DeclareLaunchArgument(
  48. 'slam',
  49. default_value='False',
  50. description='Whether run a SLAM')
  51. declare_map_yaml_cmd = DeclareLaunchArgument(
  52. 'map',
  53. default_value=os.path.join(bringup_dir, 'maps', 'turtlebot3_world.yaml'),
  54. description='Full path to map file to load')
  55. declare_use_sim_time_cmd = DeclareLaunchArgument(
  56. 'use_sim_time',
  57. default_value='true',
  58. description='Use simulation (Gazebo) clock if true')
  59. declare_params_file_cmd = DeclareLaunchArgument(
  60. 'params_file',
  61. default_value=os.path.join(bringup_dir, 'params', 'nav2_params.yaml'),
  62. description='Full path to the ROS2 parameters file to use for all launched nodes')
  63. declare_bt_xml_cmd = DeclareLaunchArgument(
  64. 'default_bt_xml_filename',
  65. default_value=os.path.join(
  66. get_package_share_directory('nav2_bt_navigator'),
  67. 'behavior_trees', 'navigate_w_replanning_and_recovery.xml'),
  68. description='Full path to the behavior tree xml file to use')
  69. declare_autostart_cmd = DeclareLaunchArgument(
  70. 'autostart', default_value='true',
  71. description='Automatically startup the nav2 stack')
  72. declare_rviz_config_file_cmd = DeclareLaunchArgument(
  73. 'rviz_config_file',
  74. default_value=os.path.join(bringup_dir, 'rviz', 'nav2_default_view.rviz'),
  75. description='Full path to the RVIZ config file to use')
  76. declare_use_simulator_cmd = DeclareLaunchArgument(
  77. 'use_simulator',
  78. default_value='True',
  79. description='Whether to start the simulator')
  80. declare_use_robot_state_pub_cmd = DeclareLaunchArgument(
  81. 'use_robot_state_pub',
  82. default_value='True',
  83. description='Whether to start the robot state publisher')
  84. declare_use_rviz_cmd = DeclareLaunchArgument(
  85. 'use_rviz',
  86. default_value='True',
  87. description='Whether to start RVIZ')
  88. declare_simulator_cmd = DeclareLaunchArgument(
  89. 'headless',
  90. default_value='False',
  91. description='Whether to execute gzclient)')
  92. declare_world_cmd = DeclareLaunchArgument(
  93. 'world',
  94. # TODO(orduno) Switch back once ROS argument passing has been fixed upstream
  95. # https://github.com/ROBOTIS-GIT/turtlebot3_simulations/issues/91
  96. # default_value=os.path.join(get_package_share_directory('turtlebot3_gazebo'),
  97. # 'worlds/turtlebot3_worlds/waffle.model'),
  98. default_value=os.path.join(bringup_dir, 'worlds', 'waffle.model'),
  99. description='Full path to world model file to load')
  100. # Specify the actions
  101. start_gazebo_server_cmd = ExecuteProcess(
  102. condition=IfCondition(use_simulator),
  103. cmd=['gzserver', '-s', 'libgazebo_ros_init.so', world],
  104. cwd=[launch_dir], output='screen')
  105. start_gazebo_client_cmd = ExecuteProcess(
  106. condition=IfCondition(PythonExpression([use_simulator, ' and not ', headless])),
  107. cmd=['gzclient'],
  108. cwd=[launch_dir], output='screen')
  109. urdf = os.path.join(bringup_dir, 'urdf', 'turtlebot3_waffle.urdf')
  110. start_robot_state_publisher_cmd = Node(
  111. condition=IfCondition(use_robot_state_pub),
  112. package='robot_state_publisher',
  113. executable='robot_state_publisher',
  114. name='robot_state_publisher',
  115. namespace=namespace,
  116. output='screen',
  117. parameters=[{'use_sim_time': use_sim_time}],
  118. remappings=remappings,
  119. arguments=[urdf])
  120. rviz_cmd = IncludeLaunchDescription(
  121. PythonLaunchDescriptionSource(os.path.join(launch_dir, 'rviz_launch.py')),
  122. condition=IfCondition(use_rviz),
  123. launch_arguments={'namespace': '',
  124. 'use_namespace': 'False',
  125. 'rviz_config': rviz_config_file}.items())
  126. bringup_cmd = IncludeLaunchDescription(
  127. PythonLaunchDescriptionSource(os.path.join(launch_dir, 'bringup_launch.py')),
  128. launch_arguments={'namespace': namespace,
  129. 'use_namespace': use_namespace,
  130. 'slam': slam,
  131. 'map': map_yaml_file,
  132. 'use_sim_time': use_sim_time,
  133. 'params_file': params_file,
  134. 'default_bt_xml_filename': default_bt_xml_filename,
  135. 'autostart': autostart}.items())
  136. # Create the launch description and populate
  137. ld = LaunchDescription()
  138. # Declare the launch options
  139. ld.add_action(declare_namespace_cmd)
  140. ld.add_action(declare_use_namespace_cmd)
  141. ld.add_action(declare_slam_cmd)
  142. ld.add_action(declare_map_yaml_cmd)
  143. ld.add_action(declare_use_sim_time_cmd)
  144. ld.add_action(declare_params_file_cmd)
  145. ld.add_action(declare_bt_xml_cmd)
  146. ld.add_action(declare_autostart_cmd)
  147. ld.add_action(declare_rviz_config_file_cmd)
  148. ld.add_action(declare_use_simulator_cmd)
  149. ld.add_action(declare_use_robot_state_pub_cmd)
  150. ld.add_action(declare_use_rviz_cmd)
  151. ld.add_action(declare_simulator_cmd)
  152. ld.add_action(declare_world_cmd)
  153. # Add any conditioned actions
  154. ld.add_action(start_gazebo_server_cmd)
  155. ld.add_action(start_gazebo_client_cmd)
  156. # Add the actions to launch all of the navigation nodes
  157. ld.add_action(start_robot_state_publisher_cmd)
  158. ld.add_action(rviz_cmd)
  159. ld.add_action(bringup_cmd)
  160. return ld

如果需要一边导航和建图同步开展,设置参数slam:=True;

ros2 launch nav2_bringup tb3_simulation_launch.py slam:=True

 

这时不用给初始位置,直接导航即可,机器人会同步绘制地图。 


换一个环境(可以任意换,这里以house为例): 

修改(tb3_sim_house.py):


  
  1. declare_world_cmd = DeclareLaunchArgument(
  2. 'world',
  3. # TODO(orduno) Switch back once ROS argument passing has been fixed upstream
  4. # https://github.com/ROBOTIS-GIT/turtlebot3_simulations/issues/91
  5. # default_value=os.path.join(get_package_share_directory('turtlebot3_gazebo'),
  6. # 'worlds/turtlebot3_worlds/waffle.model'),
  7. default_value=os.path.join(bringup_dir, 'worlds', 'waffle_house.model'),
  8. description='Full path to world model file to load')

waffle_house.model


  
  1. <model name="turtlebot3_house">
  2. <static>1</static>
  3. <include>
  4. <uri>model://turtlebot3_house</uri>
  5. </include>
  6. </model>

编译后,使用如下命令:

  • ros2 launch nav2_bringup tb3_sim_house.py slam:=True

注意,house入口处的障碍物-信箱📫,来导航算法不给力,扛起来!!!

 

 

不玩了,正常建图和导航效果如下:

 

比自己遥控省时省力。

重要的功能包:

  • turtlebot3
  • navigation2
  • slam_toolbox 

更多功能自己摸索一下。


roslaunch2示例

节点加载


  
  1. from launch import LaunchDescription
  2. from launch_ros.actions import Node
  3. def generate_launch_description():
  4. return LaunchDescription([
  5. Node(
  6. name='node_runtime_name',
  7. package='ros2_package_name',
  8. executable='name_of_executable',
  9. parameters=[{'name_of_int_param': 1,
  10. 'name_of_str_param': 'value'}],
  11. remappings=[('from', 'to')],
  12. output='screen',
  13. ),
  14. # More Nodes!
  15. ])

加载参数yaml


  
  1. node_name:
  2. ros__parameters:
  3. some_int_param: 1
  4. some_str_param: "the_value"

更具体一点:


  
  1. from ament_index_python.packages import get_package_share_directory
  2. # Assuming you have a file called package_name/config/params.yaml
  3. node_params = os.path.join(
  4. get_package_share_directory('package_name'),
  5. 'config',
  6. 'params.yaml'
  7. )
  8. # Add this to your LaunchDescription
  9. Node(
  10. name='node_runtime_name',
  11. package='ros2_package_name',
  12. executable='name_of_executable',
  13. parameters=[{'another_param': 42.0},
  14. node_params]
  15. )

包含其他launch文件


  
  1. import os
  2. from ament_index_python.packages import get_package_share_directory
  3. from launch.actions import IncludeLaunchDescription
  4. from launch.launch_description_sources import PythonLaunchDescriptionSource
  5. # Assuming you have a file called package_name/launch/my_launch.launch.py
  6. my_launch_file = os.path.join(
  7. get_package_share_directory('package_name'),
  8. 'launch',
  9. 'my_launch.launch.py'
  10. )
  11. # Add this to your LaunchDescription
  12. IncludeLaunchDescription(
  13. PythonLaunchDescriptionSource([my_launch_file])
  14. ),

加载URDF文件:


  
  1. import os
  2. from ament_index_python.packages import get_package_share_directory
  3. from launch_ros.actions import Node
  4. # Load the URDF into a parameter
  5. desc_dir = get_package_share_directory('robot_description_pkg')
  6. urdf_path = os.path.join(desc_dir, 'robots', 'my_urdf.urdf')
  7. urdf = open(urdf_path).read()
  8. # Add this to your LaunchDescription
  9. Node(
  10. name='robot_state_publisher',
  11. package='robot_state_publisher',
  12. executable='robot_state_publisher',
  13. parameters=[{'robot_description': urdf}],
  14. )

文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。

原文链接:zhangrelay.blog.csdn.net/article/details/119931907

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。