对浮点数进行四舍五入
1 | int getEstimate(float a) |
double 只取小数点后两位
1 | double d = 0.2500000500001; |
回调函数的降频功能
这是从gmapping里学的,cartographer里也有,但是更复杂了1
2
3
4
5
6
7
8
9unsigned int laser_count_ = 0;
int throttle_scans_ = 5; // 自定义的值
SlamGMapping::laserCallback(const sensor_msgs::LaserScan::ConstPtr& scan)
{
laser_count_++;
if ((laser_count_ % throttle_scans_) != 0) // 判断是否降频
return;
// ... 主要代码 ...
}
在终端显示带颜色和风格的文字
1 | // color指定颜色BLACK, RED, GREEN, YELLOW, BLUE, WHITE, option指定文字风格BOLD, REGULAR, UNDERLINE. |
生成极值
C++11中,std::numeric_limits为模板类,在库编译平台提供基础算术类型的极值等属性信息,取代传统C语言,所采用的预处理常数。比较常用的使用是对于给定的基础类型用来判断在当前系统上的最大值、最小值。需包含
std::numeric_limits<double>::max();
的结果是一个很大的数字
数学函数
hypot()
用来求三角形的斜边长,其原型为:double hypot(double x, double y);
,需要#include <stdio.h>
fabs
函数是求绝对值的函数,函数原型是extern float fabs(float x)
,需要#include <math.h>
对double/float数据,一定要使用fabs函数。如果用了abs,就会出现bug,因为返回也是int
反正切函数求角度
atan2
返回给定的 X 及 Y 坐标值的反正切值。反正切的角度值等于 X 轴与通过原点和给定坐标点 (Y坐标, X坐标) 的直线之间的夹角。结果以弧度表示并介于-pi
到pi
之间(不包括-pi
)。
而atan(a/b)
的取值范围介于-pi/2
到pi/2
之间,不包括±pi/2
生成27个三维向量,每个可取-1,0,1,一共27种组合
1 | for (i = 0; i < 3 * 3 * 3; i++) |
1 | int killNode(std::string nodeName) |
获得Linux命令的结果
比如输入ls
,返回当前目录的文件名1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22string getCmdResult(const string &strCmd)
{
char buf[10240] = {0};
FILE *pf = NULL;
if( (pf = popen(strCmd.c_str(), "r")) == NULL )
{
return "";
}
string strResult;
while(fgets(buf, sizeof buf, pf))
{
strResult += buf;
}
pclose(pf);
unsigned int iSize = strResult.size();
if(iSize > 0 && strResult[iSize - 1] == '\n') // linux
{
strResult = strResult.substr(0, iSize - 1);
}
return strResult;
}