LCM (Lightweight Communications and Marshalling)

1. LCM (Lightweight Communications and Marshalling)

  1. is a set of libraries and tools for message passing and data marshalling,

  2. The goal is real-time systems with high bandwidth and low latency.

  3. Provides a publish/subscribe messaging model and automatic marshalling/unmarshalling code generation with application bindings in various programming languages such as C++, Java, python, etc., and communicates by encapsulating messages in different Channels, This is similar to Topic in ROS.

  4. Based on the properties of UDP transmission, the transmission speed is faster,

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

  • install dependencies

sudo apt-get install build-essential autoconf automake autopoint libglib2.0-dev libtool openjdk-8-jdk python-dev
  • Download the source package from Releases · lcm-proj/lcm (github.com). This article downloads lcm-1.4.0.zip. Open the terminal and cd to the decompressed folder, and execute in sequence:

unzip lcm-1.4.0.zip
cd lcm-1.4.0/
mkdir build
cd build
cmake..
make
 Execute after compilation is complete
sudo make install
Complete the installation of LCM
  • Then tell the system where the lib library is located: Create a ld.so.conf file for lcm:

export LCM_INSTALL_DIR=/usr/local/lib
sudo sh -c "echo $LCM_INSTALL_DIR> /etc/ld.so.conf.d/lcm.conf"
Update
sudo ldconfig
  • Configure pkgconfig to find lcm.pc:

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$LCM_INSTALL_DIR/pkgconfig
  • At this point, the LCM installation and configuration is complete.

3. Example of LCM communication

3.1. Structure definition (C++) (example_t.lcm–>/exlcm/example_t.hpp)

Data type: The lcm type definition has nothing to do with the programming language. Create a new folder lcm_example under /home to store projects, enter the lcm_example folder, create a new example_t.lcm blank file, and write the following content

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;
}
Open the terminal and execute the command
lcm-gen -x example_t.lcm
After running, a folder exlcm is generated, and a file example_t.hpp is included, and the definition of the lcm structure is completed here. 

If you use other languages, lcm-gen -h for help,

The generated c++ classes are as follows:

?3.2, send message (send_message.cpp–>)

In the lcm_example directory, create a new send_message.cpp and copy the following content (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", & amp;my_data);

    return 0;
}
Open the terminal under the directory ~/lcm_example, compile the program send_message, generate an executable file, or execute 3.4 to create a CMakeLists.txt file
g + + -o send_message send_message.cpp -llcm

3.3, receive message (listener.cpp–>)

In the lcm_example directory, create a new listener.cpp and copy the following content (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":\
", chan.c_str());
            printf("timestamp = %lld\
", (long long)msg->timestamp);
            printf("position = (%f, %f, %f)\
",
                    msg->position[0], msg->position[1], msg->position[2]);
            printf("orientation = (%f, %f, %f, %f)\
",
                    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("\
");
            printf("name = '%s'\
", msg->name.c_str());
            printf("enabled = %d\
", msg->enabled);
        }
};

int main(int argc, char** argv)
{
    lcm::LCM lcm;

    if(!lcm.good())
        return 1;

    Handler handlerObject;
    lcm.subscribe("EXAMPLE", & amp;Handler::handleMessage, & amp;handlerObject);

    while(0 == lcm. handle());

    return 0;
}
Open the terminal under the directory ~/lcm_example, compile the program listener, generate an executable file, or execute 3.4 to create a CMakeLists.txt file
gcc -o listener listener.cpp -llcm

3.4, edit the CMakeLists.txt file

In the lcm_example directory, create a new CMakeLists.txt file

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)
After compiling the CMakeLists.txt file successfully, generate executable files send_message and listener. 
cmake

make

3.5, send-receive message

In the two terminals, cd to the location of the executable file and run respectively:

./listener
./send_message

You can see the received message in the listener window

3.6, lcm-log data analysis

In the example given on the official website, there is a demo of log data analysis, create a new read_log.cpp to copy the following content

// 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>\
");
        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\
", 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:\
");
        printf("timestamp = %lld\
", (long long)msg.timestamp);
        printf("position = (%f, %f, %f)\
",
                msg.position[0], msg.position[1], msg.position[2]);
        printf("orientation = (%f, %f, %f, %f)\
",
                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("\
");
        printf("name = '%s'\
", msg.name.c_str());
        printf("enabled = %d\
", msg.enabled);
    }

    // Log file is closed automatically when the log variable goes out of
    // scope.

    printf("done\
");
    return 0;
}
Also compile in the terminal
$ g + + -o read_log read_log.cpp -llcm

We first use the following command to record the log in the terminal, and run the ./send_message program to send messages several times at the same time

$ lcm-logger

Exit the program, and after closing the terminal, you can see the log data packets named after the current time under /home, such as lcmlog-2020-01-29.00

Open a terminal and just run ./read_log to read log packets.

$ ./read_log ‘~/lcmlog-2020-01-29.00’

The read log packet data can be displayed in the ./read_log window

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
In lcm-1.4.0/examples/, there are demos of python, java, matlab and other languages. 

3b, LCM communication example 2 ROS system learning 7 — LCM data sending and receiving – Gu Yueju (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);//The first parameter is the channel name, the second parameter is the data pointer, and the third parameter is the length
std::cout << "Send successfully!";
return 0;
}

Then compile + link:

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 & amp; 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 << "Received successfully!";
}
};
 
int main() {
lcm::LCM lcm;
MyMessageHandler handler;
lcm.subscribe("EXAMPLE", &MyMessageHandler::onMessage, &handler);
while (true)
lcm. handle();
return 0;
}

Also compile + link:

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

4. Example of LCM communication under ROS system

4.1. Create LCM_Client.cpp file

#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);//The first parameter is the channel name, the second parameter is the data pointer, and the third parameter is the length
  std::cout << "Send successfully!";

  ros::spinOnce();
}

4.2. Create LCM_Server.cpp file

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

class MyMessageHandler
{
public:
  void onMessage(const lcm::ReceiveBuffer* rbuf, const std::string & amp; 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 << "Received successfully!";
  }
};

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, Modify the CmakeLists.txt file

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}
Compile and run. 
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})
Simply speaking, to use LCM in ROS, you need to include the corresponding header file of lcm, and generate lcm variables, just use lcm.publish and lcm.subscribe.

References

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

[LCM communication example reference] The installation and use of the automatic driving message transmission mechanism LCM_lcm automatic driving_A large group of snails’ blog-CSDN blog

[LCM communication example reference, ROS use reference] ROS learning (4) LCM communication under ROS system – short book (jianshu.com) ROS system learning 7 — LCM data sending and receiving – Gu Yueju (guyuehome.com ) Robot control system – LCM – Gu Yueju (guyuehome.com) LCM source code communication (3) | Pea brother’s small station (gitee.io)