LCM通信库的安装及使用
一. 安装
1. Ubuntu
# 安装依赖
# 必选
sudo apt install build-essential libglib2.0-dev cmake
# 可选,可根据使用的语言选择安装
sudo apt install default-jdk python-all-dev liblua5.1-dev golang doxygen
# 拷贝源码
git clone https://github.com/lcm-proj/lcm.git
cd lcm
# 编译并安装
mkdir build && cd build
cmake ..
make -j4
sudo make install
2. Windows
1)安装 Python
可以使用 Anaconda,也可以直接安装,安装完一定要添加环境变量。添加成功后在 CMD 中输入python
会进入 python 交互命令行。
2)安装 Glib for Windows
从网站上安装 MSYS2 环境,假设我们把 MSYS2 安装到了 D 盘根目录,运行 D:\msys64\msys2_shell.cmd
打开 MSYS2 的终端,在终端中运行pacman -S mingw-w64-x86_64-gtk3
命令安装 Glib。
安装完成后将D:\msys64\mingw64\lib
和D:\msys64\mingw64\bin
添加进环境变量,在正式编译安装 LCM 之前需要重启 Windows 来使环境变量生效。
3)安装 CMake
从网站上安装 CMake,在 Windows 上可以直接下载 Windows x64 Installer 来安装。
4)安装 Visual Studio
从网站上安装 Visual Studio Community 2022(更早的版本也可以),安装 VS 主要是获取其 MSVC 编译环境来进行 LCM 的编译,所以我们只需要安装“使用 C++ 的桌面开发”即可。
5)编译安装 LCM
下载安装包并解压,打开 CMake GUI,选择源码目录和安装目录。
点击 Configure,创建build目录并选择编译器和平台,因为我安装的是 Visual Studio 2022,所以选择 2022 的编译器版本。
等待第一次 Configure 结束后,我们需要把LCM_ENBALE_TESTS
选项去掉,然后重新 Configure。
点击 Generate 生成 Visual Studio 工程,进入/lcm-master/build
文件夹,打开 lcm.sln 并将生成方式从 debug 改成 release。
在 ALL_BUILD 项目上单击右键,选择生成。
编译成功后使用管理员身份重新打开 lcm.sln 解决方案。在 INSTALL 项目上单击右键,选择仅用于项目>>仅生成 INSTALL。
安装成功后将C:\Program Files\lcm\bin
添加进环境变量并重启使之生效。
二. 单机通信
本节以 Windows 环境演示,所有代码在 Ubuntu 下也可以正常运行,Ubuntu 环境下编译运行很简单,只需要安装build-essential
就可以轻松完成下面所有的编译工作。
创建 lcm_test 文件夹,创建lcm_send.cpp
,lcm_receive.cpp
,read_log.cpp
,example_t.lcm
,CMakeLists.txt
五个文件。
lcm_send.cpp:
#include <lcm/lcm-cpp.hpp>#include "exlcm/example_t.hpp"int main(int argc, char ** argv)
{lcm::LCM lcm("udpm://239.255.76.67:7667?ttl=1");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 from computer1";my_data.enabled = true;lcm.publish("EXAMPLE", &my_data);return 0;
}
lcm_receive.cpp:
#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("udpm://239.255.76.67:7667?ttl=1");if(!lcm.good())return 1;Handler handlerObject;lcm.subscribe("EXAMPLE", &Handler::handleMessage, &handlerObject);while(0 == lcm.handle());return 0;
}
read_log.cpp:
#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;
}
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;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.1)project(lcm_test)find_package(lcm REQUIRED)add_executable(lcm_receive lcm_receive.cpp)add_executable(lcm_send lcm_send.cpp)add_executable(read_log read_log.cpp)target_link_libraries(lcm_receive lcm)target_link_libraries(lcm_send lcm)target_link_libraries(read_log lcm)
随后使用命令行(cmd、PowerShell)打开 lcm_test 文件夹,并运行lcm-gen -x example_t.lcm
命令生成消息类型,成功运行后会生成 exlcm 文件夹。若没有识别lcm-gen
命令,则需要检查是否成功安装 lcm,或是否将 lcm 编译结果添加进环境变量。
创建 build 文件夹并进入,随后运行cmake ..
和make
。若没有 mingw 提供的 make 命令,则可以仿照编译 lcm 的流程,使用 CMake GUI 生成 Visual Studio 工程,使用 Visual Studio 编译并运行。下面我都是使用 VSCode+MinGW 进行演示的。
编译完成后先运行 lcm_receive.exe,再运行 lcm_send.exe,便可看到接收到的消息。
三. 多机通信
首先需要保证两台电脑处于一个局域网的子网下,可以使用 ping 命令进行验证。在 WIndows 上运行ipconfig
获取 IP 地址,在 Ubuntu 上运行hostname -I
获取 IP 地址。
在 Windows 上还需要关闭防火墙,打开设置中的“网络和Internet”,依次选择“高级网络设置”,“Windows 防火墙”,“高级设置”。点击“入站规则”,找到“文件和打印机共享(回显请求 - ICMPv4-In)”,将其设置为允许,如下图:
成功 ping 通后,在 Ubuntu 环境中首先需要运行ifconfig | grep -B 1 <ubuntu ip address> | grep "flags"| cut -d ':' -f1
,查看该 IP 对应的网络设备,其中 <ubuntu ip address> 需要用实际获取到的 IP 地址进行替换。
假设我们对应的网络设备是 lo,下面用 <lo> 代替,使用时需要进行替换。运行下面两条命令来显式使能 UDP 多播和添加路由表。
sudo ifconfig <lo> multicast #使能UDP多播
sudo route add -net 224.0.0.0 netmask 240.0.0.0 dev <lo> #添加路由表
Ubuntu 配置结束,可以正常进行消息的收发。
在 Windows 上如果安装了 VMware 等虚拟机,需要先禁用虚拟机的网卡,如下图。
禁用完网卡后就可以进行多机收发的测试了,分别在两台电脑上运行lcm_receive
和lcm_send
即可。如果发现不能正常通信,可以先关闭 Windows 的防火墙再进行尝试,如果问题解决,则可以打开防火墙,在防火墙上开放 UDP 7667 端口即可;如果问题没有解决,说明不是防火墙的问题,可以按照下面这篇文章在 Windows 上配置路由表再进行尝试。
四. 日志保存与读取
在数据收发的过程中,运行lcm-logger
即可进行日志的保存,运行结束后会在当前目录下生成 lcmlog 文件,我们可以使用前面生成的read_log
程序来进行日志的读取。使用 MITlcm-log2smat 还可以将 lcm 日志文件转化为 Matlab(.mat)或 Python Pickle (.pkl) 文件。
五. 参考资料
Lightweight Communications and Marshalling (LCM)
LCM Setup (Windows)
lcm-log2smat
解决Ubuntu无法ping通Windows,但Windows能ping通Ubuntu
网络UDP广播包发不出去或接收不到问题
LCM通信库的安装及使用相关推荐
- windows 10 系统LCM通信库的编译
LCM是Lightweight Communications and Marshalling 的简称,是一个轻量级的通信与编组库.LCM内部封装了UDP,可以满足实时系统通信中高带宽,低延迟的要求.一 ...
- PySerial:Python串口通信库的详细介绍、安装及使用方法攻略
PySerial:Python串口通信库的详细介绍.安装及使用方法攻略 一.PySerial 简介 PySerial 是 Python 的一个串口通信库,支持不同平台下的串口操作.在 Python 应 ...
- 基于Java的RDMA高性能通信库(六):SDP - Java Socket Direct Protocol
目录 1.Java网络编程和套接字API的历史 2.InfiniBand 高速网络通信技术 3. Java 网络协议栈API 4.Java 7 SDP 远程直接内存存取(RDMA) 4.1 Java7 ...
- 基于Java的RDMA高性能通信库(一):IBM jVerbs库
目录 1. verbs API 2. endpoint API 3. jVerbs 应用程序系统和运行时需求(仅限 Linux) 4. Java Socket Over RDMA 与 jVerbs 比 ...
- 消息通信库ZeroMQ 4.0.4安装指南
一.ZeroMQ介绍 ZeroMQ是一个开源的消息队列系统,按照官方的定义,它是一个消息通信库,帮助开发者设计分布式和并行的应用程序. 首先,我们需要明白,ZeroMQ不是传统的消息队列系统(比如Ac ...
- Fast-DDS库的安装教程
Fast-DDS库的安装教程 0 序言 1 安装依赖 2 安装Fast-DDS 2.1 编译foonathan_memory_vendor 2.2 编译Fast-CDR 2.3 编译Fast-DDS ...
- linux——ekho7.7.1(最新版)语音合成库的安装与编译
前言 ~~~~~ TTS技术,TTS是Text To Speech的缩写,即"从文本到语音".它将计算机自己产生的.或外部输入的文字信息转变为可以听得懂的.流利的汉语口语 ...
- IIS+PHP+MySQL+Zend Optimizer+GD库+phpMyAdmin安装配置[完整修正实用版]
IIS+PHP+MySQL+Zend Optimizer+GD库+phpMyAdmin安装配置[完整修正实用版] IIS+PHP+MySQL+Zend Optimizer+GD库+phpMyAdmin ...
- Python+Anaconda中库的安装
查看Anaconda中的Python的开发环境 (1)conda env list 不同环境中库的安装 方式一: Pycharm+Anacond安装完成后的Python文件创建以及No module ...
最新文章
- Linux shell命令总结
- TCP/IP模型各层的作用和设备
- 北斗导航 | 获取观测卫星的位置信息,并绘制卫星的方位角和仰角得到星空图:GSV语句(附Matlab源代码)
- ubuntu之解决安装python3.6.4后出现error while loading shared libraries: libpython3.6m.so.1.0的问题
- Node.js学习(第一章:Node.js安装方法及模块化理解)
- linux脚本中sed -i,Linux Shell 脚本之sed命令详解
- 易语言与python爬虫_022 Python爬虫原理与python爬虫实例大全
- X5之position_estimator_inav_main.c
- 火狐浏览器自动安装xpi扩展
- 词根词缀spers/spher/spir/spond等词根衍生的单词
- android电脑局域网传输方案,通过WiFi文件传输在Android和PC之间传输文件 | MOS86
- 201571030135/201571030137《小学四则运算练习软件》结对项目报告
- python数据可视化之Matplotlib
- UML 工具: JUDE 5.5.2
- C库函数(tolower/toupper)实现字母的大小写转换
- 问题 C: 合唱队形
- vscode调试js
- 他博士毕业论文写了1255页,28岁评教授,38岁当院士!
- github 高级查询
- 文件md5加密基本操作
热门文章
- 11台计算机的英语,计算机常见英语词汇
- Stealth-Persist混合内存系统中持久应用程序的体系结构支持
- java实现的五子棋
- LVS负载均衡集群服务搭建详解
- CentOS Stream 8 安装Oracle 19C (静默模式)
- fabric 部署测试网络
- 全球与中国二手服装市场现状及未来发展趋势
- 【机友会选手机攻略】合约机是什么?和裸机有什么区别?0元购机和购机入网送话费区别?...
- 基于情感词典的情感打分
- matlab 脱离mcr,MATLAB生成exe脱离matlab运行可执行程序