踩坑目录
1.ros里面引用自定义头文件
2.ros::Time和ros::Walltime
3.时间戳转化为北京时间
4.可变参数,重构print
5.引用头文件编译不过 https://blog.csdn.net/qq_16775293/article/details/80597763
找了一年的错发现是函数名多打了一个P
6.同一个功能包,头文件和cpp分开写,要做成库然后链接
https://blog.csdn.net/qq_32761549/article/details/104578900
7.编译 ,跨功能包引用头文件,会在当前目录下寻找cpp一同编译(CMakeLists好像不适用)
8.ROS下跨包调用库
1.ros里面引用自定义头文件
把头文件放到功能包下的include/{功能包} 目录下面,然后在功能包CMakelists里面INCLUDE_DIRS include
这句话前面的注释去掉。
2…ros::Time和ros::Walltime
这两个都是时间戳,Time会随着机器暂停而暂停,Walltime不会随着机器暂停而暂停跟随世界时间走。接口都一样,有两个成员sec秒和nsec毫秒
3.时间戳转化为北京时间
#include <cstring>
#include <time.h>
#include <ros/ros.h>
typedef struct times
{
int Year;
int Mon;
int Day;
int Hour;
int Min;
int Second;
}Times;
Times stamp_to_standard(int stampTime, string &output_time)
{
time_t tick = (time_t)stampTime;
struct tm tm;
char s[100];
Times standard;
//tick = time(NULL);
tm = *localtime(&tick);
strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S ", &tm);
// printf("%d: %s\n", (int)tick, s);
for (int i = 0; i < 100; i++) {
if (s[i] == NULL) {
break;
}
output_time += s[i];
}
standard.Year = atoi(s);
standard.Mon = atoi(s+5);
standard.Day = atoi(s+8);
standard.Hour = atoi(s+11);
standard.Min = atoi(s+14);
standard.Second = atoi(s+17);
return standard;
}
具体不想研究了…大概就是转化为time的格式然后输出。
4.可变参数,重构print
#include <stdarg.h> // 可变参数的头文件
void PERCEPTION(string file_name, int line, string fmt, ...) {
va_list args;
va_start(args, fmt);
char arry[256];
strcpy(arry, fmt.c_str());
char buff[256];
vsnprintf(buff, sizeof(buff), arry, args);
// cout << buff <<endl;
va_end(args);
// perror(buff);
ros::Time::init();
// WallTime:挂钟时间,取现实世界的时间戳不会因为机器暂停而暂停
ros::WallTime now_wall_time = ros::WallTime::now();
int stmap = now_wall_time.sec;
string log_time;
Times BeiJing_time = stamp_to_standard(stmap, log_time);
cout << log_time << "[Perception] " << buff << std::endl;
std::ofstream file;
file.open("src/log_info/log.csv", std::ios_base::app);
file << log_time << ',' <<"[Perception]" << ',' << file_name.c_str() << ',' << line << ',' << buff << std::endl;
file.close();
}
参数列表中的…就是可变参数
5.引用头文件编译不过
A功能包引用B功能包头文件,需要在A功能包CMakelists和package中加入对B功能包的依赖。
6.同一个功能包,头文件和cpp分开写,要做成库然后链接
https://blog.csdn.net/qq_32761549/article/details/104578900
add_library(log_handle include/log/log.h src/log.cpp) // log_handle做成一个库
target_link_libraries(log_handle ${catkin_LIBRARIES}) // 链接库
add_executable (test_log src/test.cpp)
target_link_libraries(test_log log_handle ${catkin_LIBRARIES}) // 这一步最重要,把库和可执行文件链接起来
7.编译原理,跨功能包引用头文件,会在当前目录下寻找cpp一同编译
我在B包中引用了A包的头文件,但是一直报函数未定义;因为我.h和.cpp没有放到同一个目录下,编译B包的时候找不到cpp就没有编译;放到同一个目录下即可。
8.ROS下跨包调用库
没找到解决办法,.h文件能看到,.cpp就是看不到,一直报函数未定义的错误。应该和CMakeLists有关,关键就是生成了一个静态库,但是怎么调用它。