lcm使用

一、概述

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: 利用msg消息生成hpp头文件,生成的头文件中包含了encode和decode的函数,以及hash校验函数(见:decode(const void *buf, int offset, int maxlen)函数中的if (msg_hash != getHash()) return -1。以保证encode时记录log的头文件与decode时的头文件信息一致,否则会解析错误!!注意:务必保证存和读时的头文件内容是完全一致的)。其中,msg文件中定义的是数据类型,如果msg的多个结构体中,涉及到一个结构体嵌套了另一个结构体,则生成的头文件中_computeHash函数内的hash值也会在头文件中耦合在一起,从而保证所有的头文件在存、读的过程中都保持版本一致。使用方法:
 lcm-gen -x  .msg
  • lcm-logger:接收发送出来的lcm信息,存储成log文件;
  • lcm-logplayer: 播放记录的log,再次把log中的信息以lcm形式广播出去 (可配合listener使用,把播放的lcm信息打印在屏幕上;listener文件见下文)。

三、使用

1、范例可参见lcm/examples:

  • send-message : lcm发送数据;
  • listener :接收send-message发送的数据,显示在屏幕上;
  • read-log: 读入已经存储好的log,以便使用数据;

以上范例都依赖于lcm-gen生成的hpp头文件进行encode或decode。

2、更深入地学习lcm,需要阅读文件lcm/lcm-cpp.hpp

  • LCM类:主要实现lcm信息的收、发,即publish和subscribe功能
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);
}
  • LogEvent类:每一个LogEvent包括以下信息:
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;
};
  • LogFile类:负责打开log文件,从log文件中读取Event等。


 

 

你可能感兴趣的:(Ubuntu基础)