ROS學(xué)習(xí)(四)ROS系統(tǒng)下LCM通信

如果工程只用ROS搭建,那么是不需要用到LCM的。但如果ROS只是整體系統(tǒng)的一部分,則需要利用內(nèi)存共享或者網(wǎng)絡(luò)傳輸?shù)确绞脚c其他程序進(jìn)行數(shù)據(jù)交互,這時(shí)候LCM就是一種比較簡單的選擇,例如自動(dòng)駕駛中車內(nèi)利用ROS進(jìn)行數(shù)據(jù)流處理控制,車間利用LCM通信。

1. LCM的介紹與安裝

(1)LCM 簡介

自動(dòng)駕駛領(lǐng)域有很多進(jìn)程間通信的方式,如ROS、Apollo的Cyber RT以及一些自動(dòng)駕駛初創(chuàng)公司對ROS進(jìn)行改進(jìn)的通信協(xié)議,今天介紹一種適用于高速自動(dòng)駕駛場景的LCM通信協(xié)議,其特點(diǎn)是輕量化、傳輸速度快,易封裝。

LCM(Lightweight Communications and Marshalling)是一組用于消息傳遞和數(shù)據(jù)編組的庫和工具,其基于UDP傳輸?shù)膶傩?,傳輸速度較快,其目標(biāo)是高帶寬和低延遲的實(shí)時(shí)系統(tǒng)。它提供了一種發(fā)布/訂閱消息傳遞模型以及帶有各種編程語言C++、Java、python等應(yīng)用程序綁定的自動(dòng)編組/解組代碼生成,LCM通過將消息封裝在不同的Channel中進(jìn)行通信,這點(diǎn)類似于ROS中的Topic。

(2)LCM安裝

Releases · lcm-proj/lcm · GitHub下載源碼包,本文下載的是lcm-1.4.0.zip。打開terminal并cd到解壓后的文件夾,依次執(zhí)行:

mkdir build

cd build

cmake ..

make

編譯完成后執(zhí)行:

sudo make install

完成LCM的安裝。然后告訴系統(tǒng)lib的庫所在位置:

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

至此LCM安裝配置完成。

2. LCM 通信示例

LCM可以將數(shù)據(jù)進(jìn)行封裝和發(fā)送,使用UDP組播的方式發(fā)送出去。首先我們需要有我們自己的數(shù)據(jù)。LCM給我們提供了一個(gè)程序,可以很簡單的將我們的結(jié)構(gòu)體轉(zhuǎn)變?yōu)長CM需要的格式(這里必須轉(zhuǎn)變,否則LCM將無法發(fā)送或封裝),這個(gè)程序就是lcm-gen。

新建文件夾lcm_example用于存放LCM工程,進(jìn)入lcm_example文件夾,新建example_t.lcm空白文檔來定義一個(gè)我們自己想要的結(jié)構(gòu)體:

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;

}

執(zhí)行:?lcm-gen -x example_t.lcm

生成一個(gè)文件夾exlcm,并包含一個(gè)文件example_t.hpp,到這里lcm結(jié)構(gòu)體定義完成。

接下來我們建立收發(fā)此結(jié)構(gòu)體類型message的LCM收發(fā)機(jī)。

(1)建立publisher

在lcm_example目錄下,新建send_message.cpp復(fù)制以下內(nèi)容:

#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;

}

(2)建立subscriber

在lcm_example目錄下,新建listener.cpp復(fù)制以下內(nèi)容:

#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;

}

(3)編輯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文件成功后,生成可執(zhí)行文件send_message和listener。

分別在兩個(gè)terminal中cd到可執(zhí)行文件所在位置,分別運(yùn)行:

./listener

./send_message

便可在listerner窗口中看見由send_message發(fā)送的信息。

3. ROS系統(tǒng)下LCM通信示例

接下來把LCM連接到ROS系統(tǒng)中使用。

(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);//第一個(gè)參數(shù)是通道名,第二個(gè)參數(shù)是數(shù)據(jù)指針,第三個(gè)參數(shù)是長度

? std::cout << "發(fā)送成功!";

? ros::spinOnce();

}

(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();

}

(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}

編譯運(yùn)行即可。簡單來說,在ROS中使用LCM,需要將lcm相應(yīng)頭文件include進(jìn)來,并且生成lcm變量,使用lcm.publish和lcm.subscribe即可。

如有問題歡迎聯(lián)系討論。

參考文檔:自動(dòng)駕駛消息傳輸機(jī)制LCM的安裝與使用_python_snail_zcx的博客-CSDN博客ROS系統(tǒng)學(xué)習(xí)7---LCM數(shù)據(jù)的發(fā)送和接收_matlab_weixinhum-CSDN博客

下一篇:ROS學(xué)習(xí)(五)問題小結(jié) - 簡書

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容