主要步骤:
1. 安装LCM
2. 使用CMake组织工程并编译
1. 安装依赖,使用的是Ubuntu16.04的环境,后续验证ubuntu 20.04,该教程同样可以跑通。
# 必选
sudo apt install build-essential libglib2.0-dev cmake
# 可选,可根据使用的语言选择安装
sudo apt install default-jdk python-all-dev liblua5.1-dev golang doxygen
2. 下载、编译并安装
# 拷贝源码
git clone https://github.com/lcm-proj/lcm.git
cd lcm
# 编译并安装
mkdir build && cd build
cmake ..
make -j4
sudo make install
2. 使用CMake组织工程并跑通通信示例
生成头文件:
创建 lcm_test 文件夹,新建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_test 文件夹,运行
lcm-gen -x example_t.lcm
命令生成消息类型,成功运行后会生成 exlcm 文件夹,文件夹中有example_t.hpp的头文件。若没有识别lcm-gen
命令,则需要检查是否成功安装 lcm,或是否将 lcm 编译结果添加进环境变量。
创建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;
}
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)
编译并运行,在lcm_test 文件夹下新建build文件夹,在build文件夹中执行
cmake ..
make
生成三个可执行文件,lcm_receive,lcm_send, read_log.
先执行./lcm_receive;再./lcm_send,即可以收到发的内容。