ROS大型工程学习(二) 怎么阅读大型工程

基本思路是由点到面,由浅到深。

1、首先从launch文件入手。

文件中会看到比如:

 <node ns="room_exploration" pkg="ipa_room_exploration" type="room_exploration_server" name="room_exploration_server" output="screen" respawn="true" respawn_delay="">
<rosparam command="load" file="$(find ipa_room_exploration)/ros/launch/room_exploration_action_server_params.yaml"/>
</node>

node里可以看出来节点写在哪个文件中。

rosparam中写里参数规定在哪个文件里。

我们先看参数yaml文件。

2、yaml文件。

随便粘贴一段,从命名可以大致猜出是干什么的。然后转到cpp文件中搜索这些参数,看看到底写了什么。

 room_exploration_algorithm:   #

 # display final trajectory plan step by step
# bool
display_trajectory: true # delate the points with the same pose in the trajectory,to optimze the trajectory
# bool
delate_points: true # map correction
# ==============
# Applies a closing operation to neglect inaccessible areas and map errors/artifacts if the map_correction_closing_neighborhood_size parameter is larger than .
# The parameter then specifies the iterations (or neighborhood size) of that closing operation.
map_correction_closing_neighborhood_size:

3、cpp文件

先找main函数。比如:

 int main(int argc, char** argv)
{
ros::init(argc, argv, "room_exploration_server");
ros::Time::init(); ros::NodeHandle nh("~"); RoomExplorationServer explorationObj(nh, ros::this_node::getName()); ros::spin(); return ;
}

解读:

第3、4行:初始化,声明一个节点叫room_exploration_server。

第6行: ros::NodeHandle nh("~");把节点参数加载进来,动态加载参数。

第8行:开始启用节点。看类RoomExplorationServer定义在哪里,找到相应位置,进入下一步。

4、类的定义

接上一步,看类的定义。

比如:

 RoomExplorationServer::RoomExplorationServer(ros::NodeHandle nh, std::string name_of_the_action) :
node_handle_(nh),
room_exploration_server_(node_handle_, name_of_the_action, boost::bind(&RoomExplorationServer::exploreRoom, this, _1), false)
{
// Parameters
std::cout << "\n--------------------------\nRoom Exploration Parameters:\n--------------------------\n";
node_handle_.param("room_exploration_algorithm", room_exploration_algorithm_, );
std::cout << "room_exploration/room_exploration_algorithm = " << room_exploration_algorithm_ << std::endl;
node_handle_.param("display_trajectory", display_trajectory_, false);
std::cout << "room_exploration/display_trajectory = " << display_trajectory_ << std::endl;
node_handle_.param("delate_points", delate_points_, false);
std::cout << "room_exploration/delate_points = " << delate_points_ << std::endl;
node_handle_.param("map_correction_closing_neighborhood_size", map_correction_closing_neighborhood_size_, );
std::cout << "room_exploration/map_correction_closing_neighborhood_size = " << map_correction_closing_neighborhood_size_ << std::endl;
node_handle_.param("return_path", return_path_, true);
std::cout << "room_exploration/return_path = " << return_path_ << std::endl;
node_handle_.param("execute_path", execute_path_, false);
std::cout << "room_exploration/execute_path = " << execute_path_ << std::endl;
node_handle_.param("goals_eps", goal_eps_, 0.17);
std::cout << "room_exploration/goals_eps = " << goal_eps_ << std::endl;
node_handle_.param("use_dyn_goal_eps", use_dyn_goal_eps_, true);
std::cout << "room_exploration/use_dyn_goal_eps = " << use_dyn_goal_eps_ << std::endl;
node_handle_.param("interrupt_navigation_publishing", interrupt_navigation_publishing_, false);
std::cout << "room_exploration/interrupt_navigation_publishing = " << interrupt_navigation_publishing_ << std::endl;
node_handle_.param("revisit_areas", revisit_areas_, false);
std::cout << "room_exploration/revisit_areas = " << revisit_areas_ << std::endl;
node_handle_.param("left_sections_min_area", left_sections_min_area_, 0.01);
std::cout << "room_exploration/left_sections_min_area_ = " << left_sections_min_area_ << std::endl;
global_costmap_topic_ = "/move_base_linear/global_costmap/costmap"; // 给move_base_linear发送costmap
node_handle_.param<std::string>("global_costmap_topic", global_costmap_topic_);
std::cout << "room_exploration/global_costmap_topic = " << global_costmap_topic_ << std::endl;
node_handle_.param<std::string>("coverage_check_service_name", coverage_check_service_name_, "/room_exploration/coverage_check_server/coverage_check");
std::cout << "room_exploration/coverage_check_service_name = " << coverage_check_service_name_ << std::endl;
map_frame_ = "map";
node_handle_.param<std::string>("map_frame", map_frame_);
std::cout << "room_exploration/map_frame = " << map_frame_ << std::endl;
camera_frame_ = "base_link";
node_handle_.param<std::string>("camera_frame", camera_frame_);
std::cout << "room_exploration/camera_frame = " << camera_frame_ << std::endl; if (room_exploration_algorithm_ == ) // set boustrophedon exploration parameters
{
node_handle_.param("min_cell_area", min_cell_area_, 10.0);
std::cout << "room_exploration/min_cell_area_ = " << min_cell_area_ << std::endl;
node_handle_.param("path_eps", path_eps_, 2.0);
std::cout << "room_exploration/path_eps_ = " << path_eps_ << std::endl;
node_handle_.param("grid_obstacle_offset", grid_obstacle_offset_, 0.0);
std::cout << "room_exploration/grid_obstacle_offset_ = " << grid_obstacle_offset_ << std::endl;
node_handle_.param("max_deviation_from_track", max_deviation_from_track_, -);
std::cout << "room_exploration/max_deviation_from_track_ = " << max_deviation_from_track_ << std::endl;
node_handle_.param("cell_visiting_order", cell_visiting_order_, );
std::cout << "room_exploration/cell_visiting_order = " << cell_visiting_order_ << std::endl;
ROS_INFO("You have chosen the boustrophedon exploration method.");
}
else if (room_exploration_algorithm_ == ) // set energyfunctional explorator parameters
{
node_handle_.param("grid_obstacle_offset", grid_obstacle_offset_, 0.0);
std::cout << "room_exploration/grid_obstacle_offset_ = " << grid_obstacle_offset_ << std::endl;
ROS_INFO("You have chosen the energy functional exploration method.");
}
if (revisit_areas_ == true)
ROS_INFO("Areas not seen after the initial execution of the path will be revisited.");
else
ROS_INFO("Areas not seen after the initial execution of the path will NOT be revisited."); // min area for revisiting left sections
path_pub_ = node_handle_.advertise<nav_msgs::Path>("coverage_path", ); //Start action server
room_exploration_server_.start();
ROS_INFO("Action server for room exploration has been initialized......");
}

不难看出,这一步骤和前面的很多步骤联系到了一起。

类中先是加载里句柄, ros::NodeHandle nh("~")在第3步中提到过。

然后,声明了各项参数,在第2步的yaml文件中写过(最后的参数取值当然是以yaml文件中为准)

然后是一些不同选择的参数有不同的功能。

类定义完成后,就开始看各个类函数究竟干了些什么。

5、函数

这个就看自己需要哪些功能了,一般来说,函数是按照功能模块划分的。

如果你是看导航的工程文件,分析思路是:

先按照常理分析:导航时第一步是不是应该有个目标点(有了目标点才能开始规划路径,很好理解)?那么目标点就作为函数的切入点,找到goal传到了哪里,从传入的函数开始阅读,按照功能一步一步往下推(比如,找到目标点后,是不是需要知道机器人本体、地图信息和障碍物信息?然后生成代价地图?接着进行全局规划,知道机器人运动的大方向?再进行局部规划,实时更新障碍物信息?等等。。。。),常听到的A×、Dji算法实际上就写在规划里了。

以上所讲的是一种思维,大型工程如何下手的思维。只要思维培养好,剩下的就是编程和算法本身的学习问题了。而在工程中应用的算法,也基本都是开源算法改的。

作为学生或研究人员,还是希望大家深究算法内在逻辑,踏踏实实学习数学和编程知识,把基础打牢,科研这事急不得。

有什么问题欢迎留言探讨。

上一篇:linux bash shell run ros launch file and multi_node


下一篇:ROS launch 文件的编写