一、概述
LCM是进行信息收发的工具。
“LCM is a set of libraries and tools for message passing and data marshalling, targeted at real-time systems where high-bandwidth and low latency are critical. It provides a publish/subscribe message passing model and automatic marshalling/unmarshalling code generation with bindings for applications in a variety of programming languages.”
详见:http://lcm-proj.github.io/
二、安装
1、下载lcm源码(根据自己的需求选择版本,这里以lcm-1.3.0为例)
https://github.com/lcm-proj/lcm/releases
2、安装到指定路径:
sh ./configure --prefix=/home/your_login/Software/lcm-1.3.0/local #若不加--prefix,则安装到系统默认路径;这里不安装到系统默认路径,方便版本管理
make -j12
make install
安装后提示:
Libraries have been installed in:
/home/your_login/Software/lcm-1.3.0/local/lib/python2.7/site-packages/lcm
If you ever happen to want to link against installed libraries in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR' flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable during linking
- use the `-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for more information, such as the ld(1) and ld.so(8) manual pages.
2.1 把C++的库放入环境变量
我选择将LD_LIBRARY_PATH写入bashrc中:
sudo gedit ~/.bashrc
export LD_LIBRARY_PATH=/home/your_login/Software/lcm-1.3.0/local/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
2.2 Python中使用lcm
如果需要使用python,也需要先把模块路径放入sys.path中,具体方法如下:
import sys
sys.path.append('/home/your_login/Software/lcm-1.3.0/local/lib/python2.7/site-packages')
然后再 import lcm ,就可以在python中使用lcm了。
3、安装完成后,可以看到,在local/bin文件夹下生成了三个可执行文件:
lcm-gen -x .msg
三、使用
1、范例可参见lcm/examples:
以上范例都依赖于lcm-gen生成的hpp头文件进行encode或decode。
2、更深入地学习lcm,需要阅读文件lcm/lcm-cpp.hpp
class LCM{
inline int publish(const std::string& channel, const MessageType* msg);
Subscription* subscribe(const std::string& channel, void (MessageHandlerClass::*handlerMethod)(const ReceiveBuffer* rbuf, const std::string& channel, const MessageType* msg), MessageHandlerClass* handler);
}
struct LogEvent {
/*Monotically increasing counter identifying the event number. This field is managed by LCM, and there should be no need to ever set it manually.每一个event都包含记录了eventnum
*如果发出来的是多个channel,同一个channel里的eventnum可能是不连续的,按照lcm收到event的顺序编号;
*/
int64_t eventnum;
/*Timestamp identifying when the event was received. Represented in microseconds since the UNIX epoch.
*注意:lcm自动维护的timestamp是收到event的时间戳;如果要记录发送event的时间戳,需要自己将发送时刻的时间戳作为信息发送出来
*/
int64_t timestamp;
//The LCM channel on which the message was received. 自己定义的channel名
std::string channel;
//The length of the message payload, in bytes
int32_t datalen;
//The message payload.
void* data;
};