https://blog.youkuaiyun.com/wxflamy/article/details/79155060
LCM(Lightweight Communications and Marshalling)是一套用于消息传递和数据编组的库和工具,针对高带宽和低延迟至关重要的实时系统。 它提供了一个发布/订阅消息传递模型和自动编组/解组代码生成,并为各种编程语言的应用程序提供绑定。
目前版本是1.3.1,下载地址为:https://github.com/lcm-proj/lcm/releases
在Ubuntu下,所依赖的包有:
build-essential
libglib2.0-dev
建议安装的包有:
openjdk-6-jdk
python-dev
安装
开启终端依次运行下列代码
$ unzip lcm-1.3.1.zip
$ cd lcm-1.3.1
$ ./configure
$ make
$ sudo make install
$ sudo ldconfig
安装之后
为lcm创建一个ld.so.conf文件:
$ export LCM_INSTALL_DIR=/usr/local/lib
$ echo $LCM_INSTALL_DIR > /etc/ld.so.conf.d/lcm.conf
配置pkgconfig来查找lcm.pc
$ export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$LCM_INSTALL_DIR/pkgconfig
Python用户需要使用.pth文件将lcm安装位置添加到Python的站点包搜索路径。
$ export PYTHON_VERSION=$(python -c "import sys; print(\"%s.%s\" % sys.version_info[:2])")
$ export PYTHON_USER_SITE=$(python -m site --user-site)
$ echo "$LCM_INSTALL_DIR/python$PYTHON_VERSION/site-packages" > $PYTHON_USER_SITE/lcm.pth
应用教程(C++)
http://lcm-proj.github.io/tut_cpp.html
多播配置1
export LCM_DEFAULT_URL=udpm://239.255.76.67:7667?ttl=1
一个ROS使用LCM例子
#include "ros/ros.h"
#include <cstdlib>
#include <stdio.h>
#include <lcm/lcm-cpp.hpp>
#include "exlcm/robot_control_t.hpp"
class Handler
{
public:
~Handler() {}
objrecog_msgs::object_id lcmsrv;
void handleMessage(const lcm::ReceiveBuffer* rbuf,
const std::string& chan,
const robot_control_t* msg)
{
printf("Received message on channel \"%s\":\n",chan.c_str());
printf("Received message 2: \"%d\":\n",msg->iparams[1]);
printf("Received message 1: \"%d\":\n",msg->iparams[0]);
if(msg->iparams[0]==1)
lcmsrv.request.object_id = msg->iparams[1];
else
lcmsrv.request.object_id = 0;
}
};
int main(int argc, char** argv)
{
ros::init(argc, argv, "this_node_name");
ros::NodeHandle n;
ros::ServiceClient client = n.serviceClient<objrecog_msgs::object_id>("/service_name");
ros::Rate loop_rate(50);
lcm::LCM lcm;
Handler handlerObject;
lcm.subscribe("SERVICE_COMMAND", &Handler::handleMessage, &handlerObject);
while (n.ok())
{
ros::spinOnce();
if (!lcm.good()) {
printf("stop\n");
break;
}
if (lcm.handle() != 0)
break;
ROS_INFO("Success,%d",handlerObject.lcmsrv.request.object_id);
if(handlerObject.lcmsrv.request.object_id !=0)
{
if (client.call(handlerObject.lcmsrv))
{
ROS_INFO("Success");
}
else
{
ROS_ERROR("Failed to call service pose");
return 1;
}
}
loop_rate.sleep();
}
return 0;
}