瀏覽代碼

[feat]:完成launch文件编写

鱼香ROS 3 年之前
父節點
當前提交
21ddd806db
共有 1 個文件被更改,包括 83 次插入54 次删除
  1. 83 54
      docs/chapt10/10.9使用Fishbot进行自主导航.md

+ 83 - 54
docs/chapt10/10.9使用Fishbot进行自主导航.md

@@ -1,108 +1,137 @@
 # 10.9使用FishBot进行自主导航
 
-1.编写launch文件
+经过前面三节的铺垫,我们只需要再编写一个launch文件启动nav2就可以让fishbot自己动起来了。
 
-```
+## 1.编写launch文件
+
+我们将地图、配置文件传递给nav2为我们提供好的launch文件中即可。
+
+> 再一个launch文件中包裹另一个功能包中的luanch文件采用的是`IncludeLaunchDescription`和`PythonLaunchDescriptionSource`
+
+```python
 '''
 作者: 小鱼
 公众号: 鱼香ROS
 QQ交流群: 2642868461
-描述: file content
+描述: Nav2 launch启动文件
 '''
 import os
-
 from ament_index_python.packages import get_package_share_directory
 from launch import LaunchDescription
-from launch.actions import DeclareLaunchArgument
 from launch.actions import IncludeLaunchDescription
 from launch.launch_description_sources import PythonLaunchDescriptionSource
 from launch.substitutions import LaunchConfiguration
 from launch_ros.actions import Node
 
 
-
 def generate_launch_description():
+    #=============================1.定位到包的地址=============================================================
     fishbot_navigation2_dir = get_package_share_directory('fishbot_navigation2')
     nav2_bringup_dir = get_package_share_directory('nav2_bringup')
-
-    use_sim_time = LaunchConfiguration('use_sim_time', default='true')
+    
+    
+    #=============================2.声明参数,获取配置文件路径===================================================
+    # use_sim_time 这里要设置成true,因为gazebo是仿真环境,其时间是通过/clock话题获取,而不是系统时间
+    use_sim_time = LaunchConfiguration('use_sim_time', default='true') 
     map_yaml_path = LaunchConfiguration('map',default=os.path.join(fishbot_navigation2_dir,'maps','fishbot_map.yaml'))
     nav2_param_path = LaunchConfiguration('params_file',default=os.path.join(fishbot_navigation2_dir,'param','fishbot.yaml'))
-
     rviz_config_dir = os.path.join(nav2_bringup_dir,'rviz','nav2_default_view.rviz')
 
-    return LaunchDescription([
-        DeclareLaunchArgument('use_sim_time',default_value=use_sim_time,description='Use simulation (Gazebo) clock if true'),
-        DeclareLaunchArgument('map',default_value=map_yaml_path,description='Full path to map file to load'),
-        DeclareLaunchArgument('params_file',default_value=nav2_param_path,description='Full path to param file to load'),
-
-        IncludeLaunchDescription(
+    #=============================3.声明启动launch文件,传入:地图路径、是否使用仿真时间以及nav2参数文件==============
+    nav2_bringup_launch = IncludeLaunchDescription(
             PythonLaunchDescriptionSource([nav2_bringup_dir,'/launch','/bringup_launch.py']),
             launch_arguments={
                 'map': map_yaml_path,
                 'use_sim_time': use_sim_time,
                 'params_file': nav2_param_path}.items(),
-        ),
-        Node(
+        )
+    rviz_node =  Node(
             package='rviz2',
             executable='rviz2',
             name='rviz2',
             arguments=['-d', rviz_config_dir],
             parameters=[{'use_sim_time': use_sim_time}],
-            output='screen'),
-    ])
-
+            output='screen')
+    
+    return LaunchDescription([nav2_bringup_launch,rviz_node])
 ```
 
-2.CMakeLists.txt
+## 2.安装并添加依赖
 
-```
+### 2.1 修改CMakeLists.txt
+
+添加install指令,将文件拷贝到install目录
+
+```cmake
 cmake_minimum_required(VERSION 3.5)
 project(fishbot_navigation2)
 
-# Default to C99
-if(NOT CMAKE_C_STANDARD)
-  set(CMAKE_C_STANDARD 99)
-endif()
+# find dependencies
+find_package(ament_cmake REQUIRED)
+install(
+  DIRECTORY launch param maps
+  DESTINATION share/${PROJECT_NAME}
+)
+
+ament_package()
+```
 
-# Default to C++14
-if(NOT CMAKE_CXX_STANDARD)
-  set(CMAKE_CXX_STANDARD 14)
-endif()
+### 2.2 添加依赖
+
+```xml
+<?xml version="1.0"?>
+<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
+<package format="3">
+  <name>fishbot_navigation2</name>
+  <version>0.0.0</version>
+  <description>TODO: Package description</description>
+  <maintainer email="sangxin2014@sina.com">root</maintainer>
+  <license>TODO: License declaration</license>
+
+  <buildtool_depend>ament_cmake</buildtool_depend>
+
+  <test_depend>ament_lint_auto</test_depend>
+  <test_depend>ament_lint_common</test_depend>
+  <exec_depend>nav2_bringup</exec_depend>
+  <export>
+    <build_type>ament_cmake</build_type>
+  </export>
+</package>
+```
 
-if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
-  add_compile_options(-Wall -Wextra -Wpedantic)
-endif()
+## 3.构建运行
 
-# find dependencies
-find_package(ament_cmake REQUIRED)
-# uncomment the following section in order to fill in
-# further dependencies manually.
-# find_package(<dependency> REQUIRED)
+### 3.1 构建
 
+```shell
+colcon build --packages-up-to  fishbot_navigation2
+```
 
+### 3.2 运行
 
-if(BUILD_TESTING)
-  find_package(ament_lint_auto REQUIRED)
-  # the following line skips the linter which checks for copyrights
-  # uncomment the line when a copyright and license is not present in all source files
-  #set(ament_cmake_copyright_FOUND TRUE)
-  # the following line skips cpplint (only works in a git repo)
-  # uncomment the line when this package is not in a git repo
-  #set(ament_cmake_cpplint_FOUND TRUE)
-  ament_lint_auto_find_test_dependencies()
-endif()
+#### 3.2.1 运行仿真
 
-install(
-  DIRECTORY launch  config param maps
-  DESTINATION share/${PROJECT_NAME}
-)
+```
+source install/setup.bash
+ros2 launch fishbot_description gazebo.launch.py
+```
 
-ament_package()
+#### 3.2.2 运行Nav2
 
 ```
+source install/setup.bash
+ros2 launch fishbot_navigation2 navigation2.launch.py
+```
+
+## 4.初始化位置
+
+启动后正常你应该在RVIZ和终端看到一个错误
+
+## 5.单点导航
+
+## 6.多点(路点)导航
 
-3.
+## 7.总结
 
 --------------