自己寫的終究是不如官方發佈的,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,同時打印出每次播放內容