当前位置:网站首页>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
边栏推荐
- Stm32g0 USB DFU upgrade verification error -2
- #gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
- 数据降维——主成分分析
- R语言dplyr包rowwise函数、mutate函数计算dataframe数据中多个数据列在每行的最大值、并生成行最大值对应的数据列(row maximum)
- QT中的QPropertyAnimation使用和toast案列
- 预处理和预处理宏
- 【JVM调优实战100例】03——JVM堆调优四例
- Excel如何进行隔行复制粘贴
- Exness in-depth good article: dynamic series - Case Analysis of gold liquidity (V)
- 页面标题组件
猜你喜欢

Excel查找一列中的相同值,删除该行或替换为空值

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

yolov3 训练自己的数据集之生成train.txt

使用CLion编译OGLPG-9th-Edition源码
![[0701] [paper reading] allowing data imbalance issue with perforated input during influence](/img/c7/9b7dc4b4bda4ecfe07aec1367fe059.png)
[0701] [paper reading] allowing data imbalance issue with perforated input during influence

Talk about the design of red envelope activities in e-commerce system
![[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning](/img/59/6c776e0607a52962b72fbea2e64c8e.png)
[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning

Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future

Stm32g0 USB DFU upgrade verification error -2

Mysql高级篇学习总结8:InnoDB数据存储结构页的概述、页的内部结构、行格式
随机推荐
R语言dplyr包rowwise函数、mutate函数计算dataframe数据中多个数据列在每行的最大值、并生成行最大值对应的数据列(row maximum)
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
2022软件工程期末考试 回忆版
MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format
Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比
Markdown基础语法
[test development] software testing - concept
论文导读 | 关于将预训练语言模型作为知识库的分析与批评
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
数字滚动带动画
How to enable the run dashboard function of idea
ORA-01455: converting column overflows integer datatype
高频面试题
R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
Emmet basic syntax
C的内存管理
STM32G0 USB DFU 升级校验出错-2
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
Excel如何进行隔行复制粘贴