当前位置:网站首页>GMapping代码解析[通俗易懂]
GMapping代码解析[通俗易懂]
2022-07-02 18:08:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
前言:
最近正好 用到GMapping,需要改代码, 但看过也总是在忘,那干脆写篇博客记录 下来同时也可以帮助想要了解GMapping代码的同学。
代码的入口依然是main函数,但GMapping代码中由很多是没有用的,所以并 不需要挨个看,可以说代码的作者代码能力挺强但代码风格却是不敢恭维。这里就 不带大家挨个文件度代码,只是对几个主要的 函数进行介绍。
在看代码前,读者最好选择一个IDE来看代码,因为会涉及大量的跳转,如果只是手动去找的话就太累了。yi
main.cpp
int
main(int argc, char** argv)
{
ros::init(argc, argv, "slam_gmapping");
SlamGMapping gn;
gn.startLiveSlam();
ros::spin();
return(0);
}代码的入口 ,主要作用就是生成了一个类对象,类对象调用成员函数startLiveSlam()。说明一下ros里面基础知识这里就不过多介绍,读者自学一下ros就行。
startLiveSlam()
/*订阅一些主题 发布一些主题*/
void SlamGMapping::startLiveSlam()
{
entropy_publisher_ = private_nh_.advertise<std_msgs::Float64>("entropy", 1, true);
sst_ = node_.advertise<nav_msgs::OccupancyGrid>("map", 1, true);
sstm_ = node_.advertise<nav_msgs::MapMetaData>("map_metadata", 1, true);
ss_ = node_.advertiseService("dynamic_map", &SlamGMapping::mapCallback, this);
//订阅激光数据 同时和odom_frame之间的转换同步
scan_filter_sub_ = new message_filters::Subscriber<sensor_msgs::LaserScan>(node_, "scan", 5);
scan_filter_ = new tf::MessageFilter<sensor_msgs::LaserScan>(*scan_filter_sub_, tf_, odom_frame_, 5);
scan_filter_->registerCallback(boost::bind(&SlamGMapping::laserCallback, this, _1));
std::cout <<"Subscribe LaserScan & odom!!!"<<std::endl;
/*发布转换关系的线程*/
transform_thread_ = new boost::thread(boost::bind(&SlamGMapping::publishLoop, this, transform_publish_period_));
}这个函数的功能就是发布发布四个消息,订阅一个话题,会涉及boost库的一些知识,网上都有搜一下就行。下一步进行回调函数laserCallback中。
laserCallback()
void SlamGMapping::laserCallback(const sensor_msgs::LaserScan::ConstPtr& scan)
{
laser_count_++;
if ((laser_count_ % throttle_scans_) != 0)//throttle_scans_实现降频,如果激光频率较高而
return; //处理器计算能力有限,可以降低处理激光数据的频率
static ros::Time last_map_update(0,0);
// We can't initialize the mapper until we've got the first scan
if(!got_first_scan_)
{
if(!initMapper(*scan))//第一帧激光数据时执行初始化
return;
got_first_scan_ = true;
}
GMapping::OrientedPoint odom_pose;
if(addScan(*scan, odom_pose))//初始化完后,再有激光数据时执行addScan函数
{
ROS_DEBUG("scan processed");
GMapping::OrientedPoint mpose = gsp_->getParticles()[gsp_->getBestParticleIndex()].pose;
ROS_DEBUG("new best pose: %.3f %.3f %.3f", mpose.x, mpose.y, mpose.theta);
ROS_DEBUG("odom pose: %.3f %.3f %.3f", odom_pose.x, odom_pose.y, odom_pose.theta);
ROS_DEBUG("correction: %.3f %.3f %.3f", mpose.x - odom_pose.x, mpose.y - odom_pose.y, mpose.theta - odom_pose.theta);
tf::Transform laser_to_map = tf::Transform(tf::createQuaternionFromRPY(0, 0, mpose.theta), tf::Vector3(mpose.x, mpose.y, 0.0)).inverse();
tf::Transform odom_to_laser = tf::Transform(tf::createQuaternionFromRPY(0, 0, odom_pose.theta), tf::Vector3(odom_pose.x, odom_pose.y, 0.0));
map_to_odom_mutex_.lock();
map_to_odom_ = (odom_to_laser * laser_to_map).inverse();
map_to_odom_mutex_.unlock();
/*如果没有地图那肯定需要直接更新,如果有地图了则需要到时间了,才更新地图了*/
if(!got_map_ || (scan->header.stamp - last_map_update) > map_update_interval_)
{
/*多久更新一次地图*/
updateMap(*scan);
last_map_update = scan->header.stamp;
ROS_DEBUG("Updated the map");
}
}
else
ROS_DEBUG("cannot process scan");
}未完待续
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/148589.html原文链接:https://javaforall.cn
边栏推荐
- High frequency interview questions
- MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
- Masa framework - DDD design (1)
- 【测试开发】软件测试—概念篇
- Kubernetes three open interfaces first sight
- 27: Chapter 3: develop Passport Service: 10: [registration / login] interface: after the registration / login is OK, save the user session information (uid, utoken) to redis and cookies; (one main poi
- When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
- PHP-Parser羽毛球预约小程序开发require线上系统
- R语言ggplot2可视化:可视化折线图、使用labs函数为折线图添加自定义的X轴标签信息
- juypter notebook 修改默认打开文件夹以及默认浏览器
猜你喜欢

UML 类图

SLC、MLC、TLC 和 QLC NAND SSD 之间的区别:哪个更好?
![[test development] software testing - concept](/img/24/9ee885d46f7200ae7449957ca96b9d.png)
[test development] software testing - concept

数据降维——主成分分析

How to clean up discarded PVs and their corresponding folders

Thoroughly understand the point cloud processing tutorial based on open3d!

消息队列消息丢失和消息重复发送的处理策略

Excel finds the same value in a column, deletes the row or replaces it with a blank value

ICDE 2023|TKDE Poster Session(CFP)

中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
随机推荐
Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
Have you stepped on the nine common pits in the e-commerce system?
[test development] takes you to know what software testing is
How to delete the border of links in IE? [repeat] - how to remove borders around links in IE? [duplicate]
C文件输入操作
How performance testing creates business value
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
数据降维——主成分分析
Hongmeng's fourth learning
[paper reading] Ca net: leveraging contextual features for lung cancer prediction
2022 compilation principle final examination recall Edition
How to enable the run dashboard function of idea
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
Singapore summer tourism strategy: play Singapore Sentosa Island in one day
The difference between interceptor and filter
Compile oglpg-9th-edition source code with clion
metric_logger小解
ORA-01455: converting column overflows integer datatype
论文导读 | 关于将预训练语言模型作为知识库的分析与批评
[0701] [paper reading] allowing data imbalance issue with perforated input during influence