Przejdź do treści

Robotic Programming Environments: L4

Communication in distributed systems – ZeroMQ

Task (0-10 points)

Goal

Create a system in which two applications communicate with each other using ZeroMQ library. The first application should send complex data structure to the second application using serialization. The second application should deserialize received data and print the received structure.

Remarks

  • For serialization/deserialization of the data structure use Google Protocol Buffers library.
  • Data structure to send (based on the ROS Pose).

    Point position
    Quaternion orientation

    where Point contains coordinates x, y, z and Quaternion contains elements: x, y, z, w.

  • ZeroMQ in C++: #include <zmq.hpp>, linker option: -lzmq
Example of protobuf usage in Python
  • Install protobuf compiler (if necessary)

    apt install protobuf-compiler
  • Message schema definition (person_msg.proto file)

    syntax = "proto2";
    
    package tutorial;
    
    message Person {
      required string name = 1;
      optional int32 id = 2;
    }
  • Generate code from schema

    protoc -I=. --python_out=. ./person.proto
  • Serialize message and write it to the file

    import person.person_pb2 as person_pb2
    
    person_msg = person_pb2.Person()
    person_msg.name = "John"
    person_msg.id = 5
    
    # Write message to binary file
    with open("person.bin", "wb") as f:
        data = person_msg.SerializeToString()
        f.write(data)
  • Read a message from file and deserialize it

    import person.person_pb2 as person_pb2
    
    # Read message from binary file
    with open("person.bin", "rb") as f:
        msg = person_pb2.Person().FromString(f.read())

Resources: