自动驾驶领域有很多进程间通信的方式,如ROS、Apollo的Cyber RT… 以及一些自动驾驶初创公司对ROS进行改进的通信协议,今天介绍一种适用于高速自动驾驶场景的LCM通信协议,其特点是轻量化、传输速度快,易封装。
LCM(Lightweight Communications and Marshalling)是一组用于消息传递和数据编组的库和工具,其基于UDP传输的属性,传输速度较快,其目标是高带宽和低延迟的实时系统。它提供了一种发布/订阅消息传递模型以及带有各种编程语言C++、Java、python等应用程序绑定的自动编组/解组代码生成,LCM通过将消息封装在不同的Channel中进行通信,这点类似于ROS中的Topic。
参考LCM官方文档:https://github.com/lcm-proj/lcm
安装环境:
Ubuntu16.04;
lcm-1.3.1;
下载地址:https://github.com/lcm-proj/lcm/releases
在Downloads目录下找到刚才下载的lcm-1.3.1.zip压缩包,解压
$ unzip lcm-1.3.1.zip
安装依赖
$ sudo apt-get install build-essential autoconf automake autopoint libglib2.0-dev libtool openjdk-8-jdk python-dev
我习惯在/home下安装,将刚才解压后的文件夹lcm-1.3.1移动到/home下开始安装
$ cd lcm-1.3.1
$ ./configure
$ make
$ sudo make install
为lcm创建一个ld.so.conf文件
$ export LCM_INSTALL_DIR=/usr/local/lib
$ sudo sh -c "echo $LCM_INSTALL_DIR > /etc/ld.so.conf.d/lcm.conf"
$ sudo ldconfig
配置pkgconfig
$ export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$LCM_INSTALL_DIR/pkgconfig
安装python扩展包
$ cd lcm-python
$ sudo python setup.py install
到这里LCM安装完成,介绍几个常用命令:
lcm-logger //记录log
lcm-spy //查看当前数据
lcm-logplayer, lcm-logplayer-gui //log回放
lcm类型定义与编程语言无关,/home下新建文件夹lcm_example用于存放工程,进入lcm_example文件夹,新建example_t.lcm空白文档,写入以下内容
package exlcm;
struct example_t
{
int64_t timestamp;
double position[3];
double orientation[4];
int32_t num_ranges;
int16_t ranges[num_ranges];
string name;
boolean enabled;
}
打开终端,执行命令
$ lcm-gen -x example_t.lcm
如何你使用其他语言,lcm-gen -h 获取帮助,运行之后生成一个文件夹exlcm,并包含一个文件example_t.hpp,到这里lcm结构体定义完成。
在lcm_example目录下,新建send_message.cpp复制以下内容
// file: send_message.cpp
//
// LCM example program.
//
// compile with:
// $ g++ -o send_message send_message.cpp -llcm
//
// On a system with pkg-config, you can also use:
// $ g++ -o send_message send_message.cpp `pkg-config --cflags --libs lcm`
#include
#include "exlcm/example_t.hpp"
int main(int argc, char ** argv)
{
lcm::LCM lcm;
if(!lcm.good())
return 1;
exlcm::example_t my_data;
my_data.timestamp = 0;
my_data.position[0] = 1;
my_data.position[1] = 2;
my_data.position[2] = 3;
my_data.orientation[0] = 1;
my_data.orientation[1] = 0;
my_data.orientation[2] = 0;
my_data.orientation[3] = 0;
my_data.num_ranges = 15;
my_data.ranges.resize(my_data.num_ranges);
for(int i = 0; i < my_data.num_ranges; i++)
my_data.ranges[i] = i;
my_data.name = "example string";
my_data.enabled = true;
lcm.publish("EXAMPLE", &my_data);
return 0;
}
在目录~/lcm_example下打开终端,编译程序send_message,生成可执行文件
$ g++ -o send_message send_message.cpp -llcm
在lcm_example目录下,新建listener.cpp复制以下内容
// file: listener.cpp
//
// LCM example program.
//
// compile with:
// $ gcc -o listener listener.cpp -llcm
//
// On a system with pkg-config, you can also use:
// $ gcc -o listener listener.cpp `pkg-config --cflags --libs lcm`
#include
#include
#include "exlcm/example_t.hpp"
class Handler
{
public:
~Handler() {}
void handleMessage(const lcm::ReceiveBuffer* rbuf,
const std::string& chan,
const exlcm::example_t* msg)
{
int i;
printf("Received message on channel \"%s\":\n", chan.c_str());
printf(" timestamp = %lld\n", (long long)msg->timestamp);
printf(" position = (%f, %f, %f)\n",
msg->position[0], msg->position[1], msg->position[2]);
printf(" orientation = (%f, %f, %f, %f)\n",
msg->orientation[0], msg->orientation[1],
msg->orientation[2], msg->orientation[3]);
printf(" ranges:");
for(i = 0; i < msg->num_ranges; i++)
printf(" %d", msg->ranges[i]);
printf("\n");
printf(" name = '%s'\n", msg->name.c_str());
printf(" enabled = %d\n", msg->enabled);
}
};
int main(int argc, char** argv)
{
lcm::LCM lcm;
if(!lcm.good())
return 1;
Handler handlerObject;
lcm.subscribe("EXAMPLE", &Handler::handleMessage, &handlerObject);
while(0 == lcm.handle());
return 0;
}
在目录~/lcm_example下打开终端,编译程序listener,生成可执行文件
$ gcc -o listener listener.cpp -llcm
在终端运行./listener,再开一个终端运行./send_message,在listener窗口可以看到接收的消息
Received message on channel "EXAMPLE":
timestamp = 0
position = (1.000000, 2.000000, 3.000000)
orientation = (1.000000, 0.000000, 0.000000, 0.000000)
ranges: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
name = 'example string'
enabled = 1
在官网给出的例子中,有一段log数据解析的demo,新建read_log.cpp复制以下内容
// file: read_log.cpp
//
// LCM example program. Demonstrates how to read and decode messages directly
// from a log file in C++. It is also possible to use the log file provider --
// see the documentation on the LCM class for details on that method.
//
// compile with:
// $ g++ -o read_log read_log.cpp -llcm
//
// On a system with pkg-config, you can also use:
// $ g++ -o read_log read_log.cpp `pkg-config --cflags --libs lcm`
#include
#include
#include "exlcm/example_t.hpp"
int main(int argc, char ** argv)
{
if(argc < 2) {
fprintf(stderr, "usage: read_log \n" );
return 1;
}
// Open the log file.
lcm::LogFile log(argv[1], "r");
if(!log.good()) {
perror("LogFile");
fprintf(stderr, "couldn't open log file %s\n", argv[1]);
return 1;
}
while(1) {
// Read a log event.
const lcm::LogEvent *event = log.readNextEvent();
if(!event)
break;
// Only process messages on the EXAMPLE channel.
if(event->channel != "EXAMPLE")
continue;
// Try to decode the message.
exlcm::example_t msg;
if(msg.decode(event->data, 0, event->datalen) != event->datalen)
continue;
// Decode success! Print out the message contents.
printf("Message:\n");
printf(" timestamp = %lld\n", (long long)msg.timestamp);
printf(" position = (%f, %f, %f)\n",
msg.position[0], msg.position[1], msg.position[2]);
printf(" orientation = (%f, %f, %f, %f)\n",
msg.orientation[0], msg.orientation[1], msg.orientation[2],
msg.orientation[3]);
printf(" ranges:");
for(int i = 0; i < msg.num_ranges; i++)
printf(" %d", msg.ranges[i]);
printf("\n");
printf(" name = '%s'\n", msg.name.c_str());
printf(" enabled = %d\n", msg.enabled);
}
// Log file is closed automatically when the log variable goes out of
// scope.
printf("done\n");
return 0;
}
同样在终端编译
$ g++ -o read_log read_log.cpp -llcm
我们先在终端使用以下命令记录log,同时运行几次./send_message发送消息的程序
$ lcm-logger
退出程序,关闭终端后在/home下可以看到以当前时间命名的log数据包,如lcmlog-2020-01-29.00
打开一个终端,只需运行./read_log来读取log数据包。
$ ./read_log '~/lcmlog-2020-01-29.00'
在./read_log窗口可以显示读取的log包数据
Message:
timestamp = 0
position = (1.000000, 2.000000, 3.000000)
orientation = (1.000000, 0.000000, 0.000000, 0.000000)
ranges: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
name = 'example string'
enabled = 1
在lcm-1.3.1/examples/中还有python、java、matlab等其他语言的demo。