LCM(Lightweight Communications and Marshalling)

1、LCM(Lightweight Communications and Marshalling)

  1. 是一组用于消息传递和数据编组的库和工具,

  2. 目标是高带宽和低延迟的实时系统。

  3. 提供了一种发布/订阅消息传递模型以及带有各种编程语言C++、Java、python等应用程序绑定的自动编组/解组代码生成,通过将消息封装在不同的Channel中进行通信,这点类似于ROS中的Topic。

  4. 基于UDP传输的属性,传输速度较快,

  • 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.

2、搭建LCM

  • 安装依赖

sudo apt-get install build-essential autoconf automake autopoint libglib2.0-dev libtool openjdk-8-jdk python-dev
unzip lcm-1.4.0.zip
cd lcm-1.4.0/
mkdir build
cd build
cmake ..
make
编译完成后执行
sudo make install
完成LCM的安装
  • 然后告诉系统lib的库所在位置:,为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来查找lcm.pc:

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$LCM_INSTALL_DIR/pkgconfig
  • 至此LCM安装配置完成。

3、LCM 通信示例

3.1、结构体定义(C++)(example_t.lcm-->/exlcm/example_t.hpp)

数据类型: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
运行之后生成一个文件夹exlcm,并包含一个文件example_t.hpp,到这里lcm结构体定义完成。

如果你使用其他语言,lcm-gen -h 获取帮助,

生成的c++类如下:

​3.2、发送消息(send_message.cpp-->)

在lcm_example目录下,新建send_message.cpp复制以下内容(The full example is available in runnable form as examples/cpp/send_message.cpp in the LCM source distribution.)

// 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 <lcm/lcm-cpp.hpp>
#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,生成可执行文件,或者执行3.4建立CMakeLists.txt文件
g++ -o send_message send_message.cpp -llcm

3.3、接收消息(listener.cpp-->)

在lcm_example目录下,新建listener.cpp复制以下内容(The full example is available in runnable form as examples/cpp/listener.cpp in the LCM source distribution.)

// 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 <stdio.h>
#include <lcm/lcm-cpp.hpp>
#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,生成可执行文件,或者执行3.4建立CMakeLists.txt文件
gcc -o listener listener.cpp -llcm

3.4、编辑CMakeLists.txt文件

在lcm_example目录下,新建CMakeLists.txt文件

project(lcm_test)
set(CMAKE_CXX_STANDARD 11)

add_executable(send_message send_message.cpp)
target_link_libraries(send_message lcm)

add_executable(listener listener.cpp)
target_link_libraries(listener lcm)
编译CMakeLists.txt文件成功后,生成可执行文件send_message和listener。
cmake

make

3.5、发送-接收消息

分别在两个terminal中cd到可执行文件所在位置,分别运行:

./listener
./send_message

在listener窗口可以看到接收的消息

3.6、lcm-log数据解析

在官网给出的例子中,有一段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 <stdio.h>

#include <lcm/lcm-cpp.hpp>

#include "exlcm/example_t.hpp"

int main(int argc, char ** argv)
{
    if(argc < 2) {
        fprintf(stderr, "usage: read_log <logfile>\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.4.0/examples/中还有python、java、matlab等其他语言的demo。

3b、LCM通信示例2 ROS系统学习7---LCM数据的发送和接收 - 古月居 (guyuehome.com)

3b.1、LCM_Client.cpp:

#include <iostream>
#include "lcm/lcm-cpp.hpp"
 
int main()
{
	lcm::LCM lcm;
	if (!lcm.good())
	{
		return 1;
	}
	char data[5];
	data[0] = 1;
	data[1] = 5;
	data[2] = 1;
	data[3] = 2;
	data[4] = 1;
	lcm.publish("EXAMPLE", data,5);//第一个参数是通道名,第二个参数是数据指针,第三个参数是长度
	std::cout << "发送成功!";
	return 0;
}

然后编译+链接:

g++ `pkg-config --cflags lcm` -I. -o Client.o -c LCM_Client.cpp 
g++ -o Client Client.o `pkg-config --libs lcm` 

3b.2、LCM_Server.cpp:

#include <iostream>
#include "lcm/lcm-cpp.hpp"
 
class MyMessageHandler 
{
public:
	void onMessage(const lcm::ReceiveBuffer* rbuf, const std::string& channel) 
	{
		std::cout << (int)((unsigned char*)rbuf->data)[0] << std::endl;
		std::cout << (int)((unsigned char*)rbuf->data)[1] << std::endl;
		std::cout << (int)((unsigned char*)rbuf->data)[2] << std::endl;
		std::cout << (int)((unsigned char*)rbuf->data)[3] << std::endl;
		std::cout << (int)((unsigned char*)rbuf->data)[4] << std::endl;
		std::cout << "接收成功!";
	}
};
 
int main() {
	lcm::LCM lcm;
	MyMessageHandler handler;
	lcm.subscribe("EXAMPLE", &MyMessageHandler::onMessage, &handler);
	while (true)
		lcm.handle();
	return 0;
}

同样编译+链接:

g++ `pkg-config --cflags lcm` -I. -o Server.o -c LCM_Server.cpp 
g++ -o Server Server.o `pkg-config --libs lcm`

4. ROS系统下LCM通信示例

4.1、建立LCM_Client.cpp文件

#include <ros/ros.h>
#include <iostream>
#include "lcm/lcm-cpp.hpp"

int main(int argc, char** argv)
{

  ros::init(argc, argv, "image_publisher");
  ros::NodeHandle nh;

  lcm::LCM lcm;
  if (!lcm.good())
  {
    return 1;
  }

  char data[5];
  data[0] = 1;
  data[1] = 5;
  data[2] = 1;
  data[3] = 2;
  data[4] = 1;
  lcm.publish("EXAMPLE", data,5);//第一个参数是通道名,第二个参数是数据指针,第三个参数是长度
  std::cout << "发送成功!";

  ros::spinOnce();
}

4.2、建立LCM_Server.cpp文件

#include <ros/ros.h>
#include <iostream>
#include "lcm/lcm-cpp.hpp"

class MyMessageHandler
{
public:
  void onMessage(const lcm::ReceiveBuffer* rbuf, const std::string& channel)
  {
    std::cout << (int)((unsigned char*)rbuf->data)[0] << std::endl;
    std::cout << (int)((unsigned char*)rbuf->data)[1] << std::endl;
    std::cout << (int)((unsigned char*)rbuf->data)[2] << std::endl;
    std::cout << (int)((unsigned char*)rbuf->data)[3] << std::endl;
    std::cout << (int)((unsigned char*)rbuf->data)[4] << std::endl;
    std::cout << "接收成功!";
  }
};

int main(int argc, char** argv)
{
  ros::init(argc, argv, "image_publisher");
  ros::NodeHandle nh;

  lcm::LCM lcm;
  MyMessageHandler handler;
  lcm.subscribe("EXAMPLE", &MyMessageHandler::onMessage, &handler);
  while (true)
    lcm.handle();

  ros::spin();
}

4.3、修改CmakeLists.txt文件

include_directories(${catkin_INCLUDE_DIRS})

add_executable(LCM_Client src/LCM_Client.cpp)
target_link_libraries(LCM_Client lcm)
target_link_libraries(LCM_Client ${catkin_LIBRARIES})

add_executable(LCM_Server src/LCM_Server.cpp)
target_link_libraries(LCM_Server lcm) //necessary for lcm message transmission
target_link_libraries(LCM_Server ${catkin_LIBRARIES}
编译运行即可。
cmake_minimum_required(VERSION 2.8.3)
project(lcm_test2)
set(CMAKE_CXX_STANDARD 11)

find_package(catkin REQUIRED COMPONENTS
  roscpp
)

include_directories(
    lcm
    src
    ${catkin_INCLUDE_DIRS}
)

add_executable(LCM_Client src/LCM_Client.cpp)
target_link_libraries(LCM_Client lcm ${catkin_LIBRARIES})

add_executable(LCM_Server src/LCM_Server.cpp)
target_link_libraries(LCM_Server lcm ${catkin_LIBRARIES})
简单来说,在ROS中使用LCM,需要将lcm相应头文件include进来,并且生成lcm变量,使用lcm.publish和lcm.subscribe即可。

参考资料

* [LCM downloads](Releases · lcm-proj/lcm · GitHub) * [Website and documentation](LCM Documentation — LCM documentation)

[LCM通信示例参考]自动驾驶消息传输机制LCM的安装与使用_lcm 自动驾驶_一大群蜗牛的博客-CSDN博客

[LCM通信示例参考,ROS下使用参考]ROS学习(四)ROS系统下LCM通信 - 简书 (jianshu.com) ROS系统学习7---LCM数据的发送和接收 - 古月居 (guyuehome.com) 机器人控制系统——LCM - 古月居 (guyuehome.com) LCM源代码之通信(三) | 豌豆哥的小站 (gitee.io)

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

October407

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值