WHCSRL 技术网

ROS2机器人实验报告提示05➡寻路⬅Nav2

上一节,构建了有机器人和一些障碍物仿真元宇宙。

如何让机器人具备自主导航的能力呢???


先介绍偷懒的方式:

安装turtlesim3包,然后使用官方教程即可。

  • sudo apt update 
  • sudo apt install ros-galactic-turtlebot3-gazebo ros-galactic-turtlebot3-msgs ros-galactic-turtlebot3-simulations 

直接运行:

  •   ros2 launch nav2_bringup tb3_simulation_launch.py

查看是否报错!

如果打不开,多尝试几次!

export TURTLEBOT3_MODEL=waffle
export GAZEBO_MODEL_PATH=$GAZEBO_MODEL_PATH:/opt/ros/galactic/share/turtlebot3_gazebo/models

如上环境一定要争取配置,推荐添加到bash文件中。

 

 

 

 

 

更多内容参考博客相关章节或观看钉钉直播课回放。


思考题:

对比ros2 launch nav2_bringup tb3_simulation_launch.py和ros2 launch sam_bot_description display.launch.py。

实现机器人自动导航,需要哪些功能包,如何实现。

tb3_simulation_launch.py

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

sam_bot_description display.launch.py

  1. import launch
  2. from launch.substitutions import Command, LaunchConfiguration
  3. import launch_ros
  4. import os
  5. def generate_launch_description():
  6. pkg_share = launch_ros.substitutions.FindPackageShare(package='sam_bot_description').find('sam_bot_description')
  7. default_model_path = os.path.join(pkg_share, 'src/description/sam_bot_description.urdf')
  8. default_rviz_config_path = os.path.join(pkg_share, 'rviz/urdf_config.rviz')
  9. world_path=os.path.join(pkg_share, 'world/my_world.sdf')
  10. robot_state_publisher_node = launch_ros.actions.Node(
  11. package='robot_state_publisher',
  12. executable='robot_state_publisher',
  13. parameters=[{'robot_description': Command(['xacro ', LaunchConfiguration('model')])}]
  14. )
  15. joint_state_publisher_node = launch_ros.actions.Node(
  16. package='joint_state_publisher',
  17. executable='joint_state_publisher',
  18. name='joint_state_publisher',
  19. condition=launch.conditions.UnlessCondition(LaunchConfiguration('gui'))
  20. )
  21. rviz_node = launch_ros.actions.Node(
  22. package='rviz2',
  23. executable='rviz2',
  24. name='rviz2',
  25. output='screen',
  26. arguments=['-d', LaunchConfiguration('rvizconfig')],
  27. )
  28. spawn_entity = launch_ros.actions.Node(
  29. package='gazebo_ros',
  30. executable='spawn_entity.py',
  31. arguments=['-entity', 'sam_bot', '-topic', 'robot_description'],
  32. output='screen'
  33. )
  34. robot_localization_node = launch_ros.actions.Node(
  35. package='robot_localization',
  36. executable='ekf_node',
  37. name='ekf_filter_node',
  38. output='screen',
  39. parameters=[os.path.join(pkg_share, 'config/ekf.yaml'), {'use_sim_time': LaunchConfiguration('use_sim_time')}]
  40. )
  41. return launch.LaunchDescription([
  42. launch.actions.DeclareLaunchArgument(name='gui', default_value='True',
  43. description='Flag to enable joint_state_publisher_gui'),
  44. launch.actions.DeclareLaunchArgument(name='model', default_value=default_model_path,
  45. description='Absolute path to robot urdf file'),
  46. launch.actions.DeclareLaunchArgument(name='rvizconfig', default_value=default_rviz_config_path,
  47. description='Absolute path to rviz config file'),
  48. launch.actions.DeclareLaunchArgument(name='use_sim_time', default_value='True',
  49. description='Flag to enable use_sim_time'),
  50. launch.actions.ExecuteProcess(cmd=['gazebo', '--verbose', '-s', 'libgazebo_ros_factory.so', world_path], output='screen'),
  51. joint_state_publisher_node,
  52. robot_state_publisher_node,
  53. spawn_entity,
  54. robot_localization_node,
  55. rviz_node
  56. ])

能否修改sam_bot_description display.launch.py,是其不仅具备SLAM还具备导航功能呢?

?_?

^_^


 

推荐阅读