自己写的终究是不如官方发布的,https://github.com/lcm-proj/lcm/tree/master/lcm-logger
再官方github上找到了用lcm录数据并播放的源码,提供的功能也比较丰富,重要是可以按照信号发送的频率录下来,同时按频率播放,复现当时的场景。简单介绍一下。
源码编译生产2个可执行文件,lcm-logger和lcm-logplayer。
lcm-logger程序提供了命令行参数选择,生成的log是以.00 , .01为后缀的log文件,把后缀替换为.log也是可以使用的。
"Options:\n"
"\n"
" -c, --channel=CHAN Channel string to pass to lcm_subscribe.\n"
" (default: \".*\")\n"
" --flush-interval=MS Flush the log file to disk every MS milliseconds.\n"
" (default: 100)\n"
" -f, --force Overwrite existing files\n"
" -h, --help Shows this help text and exits\n"
" -i, --increment Automatically append a suffix to FILE\n"
" such that the resulting filename does not\n"
" already exist. This option precludes -f and\n"
" --rotate\n"
" -l, --lcm-url=URL Log messages on the specified LCM URL\n"
" -m, --max-unwritten-mb=SZ Maximum size of received but unwritten\n"
" messages to store in memory before dropping\n"
" messages. (default: 100 MB)\n"
" --rotate=NUM When creating a new log file, rename existing files\n"
" out of the way and always write to FILE.0. If\n"
" FILE.0 already exists, it is renamed to FILE.1. If\n"
" FILE.1 exists, it is renamed to FILE.2, etc. If\n"
" FILE.NUM exists, then it is deleted. This option\n"
" precludes -i.\n"
" --split-mb=N Automatically start writing to a new log\n"
" file once the log file exceeds N MB in size\n"
" (can be fractional). This option requires -i\n"
" or --rotate.\n"
" -q, --quiet Suppress normal output and only report errors.\n"
" -a, --append Append events to the given log file.\n"
" -s, --strftime Format FILE with strftime.\n"
" -v, --invert-channels Invert channels. Log everything that CHAN\n"
" does not match.\n"
一般用到的参数有:
--channel:选择录制的lcm信息通道,看源码只能跟一个通道,不能同时选择录2个及以上的通道。如果不选择,会录制所有收到的通道信息。
--split-mb:设置一个大小,达到后自动新建一个新的log,可以避免log太大带来的不方便。
--invert-channels:选择不录某个通道。
如果只需要录制特定的某几个通道,可以修改源码这部分:
if(logger.invert_channels) {
// if inverting the channels, subscribe to everything and invert on the
// callback
lcm_subscribe(logger.lcm, ".*", message_handler, &logger);
char *regexbuf = g_strdup_printf("^%s$", chan_regex);
GError *rerr = NULL;
logger.regex = g_regex_new(regexbuf, (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, &rerr);
if(rerr) {
fprintf(stderr, "%s\n", rerr->message);
g_free(regexbuf);
return 1;
}
g_free(regexbuf);
} else {
// otherwise, let LCM handle the regex
//lcm_subscribe(logger.lcm, chan_regex, message_handler, &logger);
修改这部分
lcm_subscribe(logger.lcm, "LOGITECH_IMAGE", message_handler, &logger);
lcm_subscribe(logger.lcm, "RMVCU", message_handler, &logger);
}
原来是如果不设置--invert-channels,将会录下选择的某个通道或者所有通道。修改后,如果不设置--invert-channels,录制特定的两个通道。
使用时可以:
./lcm-logger 录制能收到的所有lcm通道,以默认方式命名log文件
./lcm-logger xxx.log 录制能收到的所有lcm通道,以设置的名称命名log文件
./lcm-logger --channel=xxx 录制通道名为xxx的信息,以默认方式命名log文件
lcm-logplayer提供了对录制log的播放,通过lcm把log中的信息再发送出来,同样提供了命令行参数:
Options:\n\
-v, --verbose Print information about each packet.\n\
-s, --speed=NUM Playback speed multiplier. Default is 1.0.\n\
-e, --regexp=EXPR GLib regular expression of channels to play.\n\
-l, --lcm-url=URL Play logged messages on the specified LCM URL.\n\
-h, --help Shows some help text and exits.\n\
\n", cmd);
-v是选择是否把通道信息打印出来
-s是选择发送速度,一般是使用默认速度,还原录制时的场景。
使用时:
./lcm-logplayer -v xxx.log 以默认速度播放xxx.log,同时打印出每次播放内容