基本思路是由点到面,由浅到深。
1、首先从launch文件入手。
文件中会看到比如:
1 <node ns="room_exploration" pkg="ipa_room_exploration" type="room_exploration_server" name="room_exploration_server" output="screen" respawn="true" respawn_delay="2"> 2 <rosparam command="load" file="$(find ipa_room_exploration)/ros/launch/room_exploration_action_server_params.yaml"/> 3 </node>
node里可以看出来节点写在哪个文件中。
rosparam中写里参数规定在哪个文件里。
我们先看参数yaml文件。
2、yaml文件。
随便粘贴一段,从命名可以大致猜出是干什么的。然后转到cpp文件中搜索这些参数,看看到底写了什么。
1 room_exploration_algorithm: 2 #6 2 3 # display final trajectory plan step by step 4 # bool 5 display_trajectory: true 6 7 # delate the points with the same pose in the trajectory,to optimze the trajectory 8 # bool 9 delate_points: true 10 11 # map correction 12 # ============== 13 # Applies a closing operation to neglect inaccessible areas and map errors/artifacts if the map_correction_closing_neighborhood_size parameter is larger than 0. 14 # The parameter then specifies the iterations (or neighborhood size) of that closing operation. 15 map_correction_closing_neighborhood_size: 1
3、cpp文件
先找main函数。比如:
1 int main(int argc, char** argv) 2 { 3 ros::init(argc, argv, "room_exploration_server"); 4 ros::Time::init(); 5 6 ros::NodeHandle nh("~"); 7 8 RoomExplorationServer explorationObj(nh, ros::this_node::getName()); 9 10 ros::spin(); 11 12 return 0; 13 }
解读:
第3、4行:初始化,声明一个节点叫room_exploration_server。
第6行: ros::NodeHandle nh("~");把节点参数加载进来,动态加载参数。
第8行:开始启用节点。看类RoomExplorationServer定义在哪里,找到相应位置,进入下一步。
4、类的定义
接上一步,看类的定义。
比如:
1 RoomExplorationServer::RoomExplorationServer(ros::NodeHandle nh, std::string name_of_the_action) : 2 node_handle_(nh), 3 room_exploration_server_(node_handle_, name_of_the_action, boost::bind(&RoomExplorationServer::exploreRoom, this, _1), false) 4 { 5 // Parameters 6 std::cout << " -------------------------- Room Exploration Parameters: -------------------------- "; 7 node_handle_.param("room_exploration_algorithm", room_exploration_algorithm_, 6); 8 std::cout << "room_exploration/room_exploration_algorithm = " << room_exploration_algorithm_ << std::endl; 9 node_handle_.param("display_trajectory", display_trajectory_, false); 10 std::cout << "room_exploration/display_trajectory = " << display_trajectory_ << std::endl; 11 node_handle_.param("delate_points", delate_points_, false); 12 std::cout << "room_exploration/delate_points = " << delate_points_ << std::endl; 13 node_handle_.param("map_correction_closing_neighborhood_size", map_correction_closing_neighborhood_size_, 2); 14 std::cout << "room_exploration/map_correction_closing_neighborhood_size = " << map_correction_closing_neighborhood_size_ << std::endl; 15 node_handle_.param("return_path", return_path_, true); 16 std::cout << "room_exploration/return_path = " << return_path_ << std::endl; 17 node_handle_.param("execute_path", execute_path_, false); 18 std::cout << "room_exploration/execute_path = " << execute_path_ << std::endl; 19 node_handle_.param("goals_eps", goal_eps_, 0.17); 20 std::cout << "room_exploration/goals_eps = " << goal_eps_ << std::endl; 21 node_handle_.param("use_dyn_goal_eps", use_dyn_goal_eps_, true); 22 std::cout << "room_exploration/use_dyn_goal_eps = " << use_dyn_goal_eps_ << std::endl; 23 node_handle_.param("interrupt_navigation_publishing", interrupt_navigation_publishing_, false); 24 std::cout << "room_exploration/interrupt_navigation_publishing = " << interrupt_navigation_publishing_ << std::endl; 25 node_handle_.param("revisit_areas", revisit_areas_, false); 26 std::cout << "room_exploration/revisit_areas = " << revisit_areas_ << std::endl; 27 node_handle_.param("left_sections_min_area", left_sections_min_area_, 0.01); 28 std::cout << "room_exploration/left_sections_min_area_ = " << left_sections_min_area_ << std::endl; 29 global_costmap_topic_ = "/move_base_linear/global_costmap/costmap"; // 给move_base_linear发送costmap 30 node_handle_.param<std::string>("global_costmap_topic", global_costmap_topic_); 31 std::cout << "room_exploration/global_costmap_topic = " << global_costmap_topic_ << std::endl; 32 node_handle_.param<std::string>("coverage_check_service_name", coverage_check_service_name_, "/room_exploration/coverage_check_server/coverage_check"); 33 std::cout << "room_exploration/coverage_check_service_name = " << coverage_check_service_name_ << std::endl; 34 map_frame_ = "map"; 35 node_handle_.param<std::string>("map_frame", map_frame_); 36 std::cout << "room_exploration/map_frame = " << map_frame_ << std::endl; 37 camera_frame_ = "base_link"; 38 node_handle_.param<std::string>("camera_frame", camera_frame_); 39 std::cout << "room_exploration/camera_frame = " << camera_frame_ << std::endl; 40 41 if (room_exploration_algorithm_ == 2) // set boustrophedon exploration parameters 42 { 43 node_handle_.param("min_cell_area", min_cell_area_, 10.0); 44 std::cout << "room_exploration/min_cell_area_ = " << min_cell_area_ << std::endl; 45 node_handle_.param("path_eps", path_eps_, 2.0); 46 std::cout << "room_exploration/path_eps_ = " << path_eps_ << std::endl; 47 node_handle_.param("grid_obstacle_offset", grid_obstacle_offset_, 0.0); 48 std::cout << "room_exploration/grid_obstacle_offset_ = " << grid_obstacle_offset_ << std::endl; 49 node_handle_.param("max_deviation_from_track", max_deviation_from_track_, -1); 50 std::cout << "room_exploration/max_deviation_from_track_ = " << max_deviation_from_track_ << std::endl; 51 node_handle_.param("cell_visiting_order", cell_visiting_order_, 1); 52 std::cout << "room_exploration/cell_visiting_order = " << cell_visiting_order_ << std::endl; 53 ROS_INFO("You have chosen the boustrophedon exploration method."); 54 } 55 else if (room_exploration_algorithm_ == 6) // set energyfunctional explorator parameters 56 { 57 node_handle_.param("grid_obstacle_offset", grid_obstacle_offset_, 0.0); 58 std::cout << "room_exploration/grid_obstacle_offset_ = " << grid_obstacle_offset_ << std::endl; 59 ROS_INFO("You have chosen the energy functional exploration method."); 60 } 61 if (revisit_areas_ == true) 62 ROS_INFO("Areas not seen after the initial execution of the path will be revisited."); 63 else 64 ROS_INFO("Areas not seen after the initial execution of the path will NOT be revisited."); 65 66 // min area for revisiting left sections 67 path_pub_ = node_handle_.advertise<nav_msgs::Path>("coverage_path", 2); 68 69 //Start action server 70 room_exploration_server_.start(); 71 ROS_INFO("Action server for room exploration has been initialized......"); 72 }
不难看出,这一步骤和前面的很多步骤联系到了一起。
类中先是加载里句柄, ros::NodeHandle nh("~")在第3步中提到过。
然后,声明了各项参数,在第2步的yaml文件中写过(最后的参数取值当然是以yaml文件中为准)
然后是一些不同选择的参数有不同的功能。
类定义完成后,就开始看各个类函数究竟干了些什么。
5、函数
这个就看自己需要哪些功能了,一般来说,函数是按照功能模块划分的。
如果你是看导航的工程文件,分析思路是:
先按照常理分析:导航时第一步是不是应该有个目标点(有了目标点才能开始规划路径,很好理解)?那么目标点就作为函数的切入点,找到goal传到了哪里,从传入的函数开始阅读,按照功能一步一步往下推(比如,找到目标点后,是不是需要知道机器人本体、地图信息和障碍物信息?然后生成代价地图?接着进行全局规划,知道机器人运动的大方向?再进行局部规划,实时更新障碍物信息?等等。。。。),常听到的A×、Dji算法实际上就写在规划里了。
以上所讲的是一种思维,大型工程如何下手的思维。只要思维培养好,剩下的就是编程和算法本身的学习问题了。而在工程中应用的算法,也基本都是开源算法改的。
作为学生或研究人员,还是希望大家深究算法内在逻辑,踏踏实实学习数学和编程知识,把基础打牢,科研这事急不得。
有什么问题欢迎留言探讨。