8.1. Hello World (Python)

While eCAL is written in C++, it also offers a great python integration. Again, we are going to explore that integration by implementing a Hello World Application, that sends the string “Hello World” to an eCAL topic.

Important

The eCAL Python integration is called python3-ecal5. With the 5 at the end. The other name has already been taken by another package.

8.1.1. Dependencies

First, you have to install some more development dependencies:

  • Windows:

    • Python 3 64-bit (https://www.python.org/downloads/)

    • The eCAL python integration. Choose the appropiate version from the eCAL Download Archive and download the .whl file. Open a command prompt in your donwload directory and install the python whl with:

      pip install ecal5-****-win_amd64.whl
      
  • Ubuntu:

    • Python 3:

      sudo apt install python3 python3-pip
      
    • The eCAL Python integration.

      • If you have installed eCAL from a PPA:

        sudo apt install python3-ecal5
        
      • If you are not using a PPA, choose the appropiate version from the eCAL Download Archive and download the .whl file. Open a terminal in your donwload directory and install the python whl with:

        sudo pip3 install ecal5-*-linux_x86_64.whl
        

8.1.2. Hello World Publisher

Somewhere on your hard drive create an empty directory and create a file hello_world.py with the following content:

  • hello_world.py:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    import sys
    import time
    
    import ecal.core.core as ecal_core
    from ecal.core.publisher import StringPublisher
    
    if __name__ == "__main__":
      # initialize eCAL API. The name of our Process will be "Python Hello World Publisher"
      ecal_core.initialize(sys.argv, "Python Hello World Publisher")
    
      # Create a String Publisher that publishes on the topic "hello_world_python_topic"
      pub = StringPublisher("hello_world_python_topic")
      
      # Create a counter, so something changes in our message
      counter = 0
      
      # Infinite loop (using ecal_core.ok() will enable us to gracefully shutdown
      # the process from another application)
      while ecal_core.ok():
        # Create a message with a counter an publish it to the topic
        current_message = "Hello World {:6d}".format(counter)
        print("Sending: {}".format(current_message))
        pub.send(current_message)
        
        # Sleep 500 ms
        time.sleep(0.5)
        
        counter = counter + 1
      
      # finalize eCAL API
      ecal_core.finalize()
    

    Note

    What is happening here?

    Line 4-5 imports the eCAL Core and the eCAL String publisher (In this tutorial we only want to send raw strings). eCAL supports multiple message formats.

    Line 9 Initialized the eCAL API. The name of our eCAL Process will be “Python Hello World Publisher”. This name will be visible in the eCAL Monitor, once the Process is running.

    Line 13 creates an eCAL Publisher. An eCAL Process can create multiple publishers (and multiple subscribers). The topic we are publishing on will be “hello_world_python_topic”.

    line 19 Creates an infinite publish-loop. eCAL supports a stop-signal; when an eCAL Process is stopped, ecal_core.ok() will return false.

    Line 23 will publish our message and send it to other eCAL Processes that have subscribed on the topic.

    Line 31 de-initializes eCAL. You should always do that before your application exits.

Now execute your python hello world script and take a look at the eCAL Monitor! You will see the “Python Hello World Publisher” process and the “hello_world_python_topic”.

  • Windows: python hello_world_snd.py

  • Ubuntu: python3 hello_world_snd.py

eCAL Monitor Hello World

8.1.3. Hello World Subscriber

For the subscriber, create another file hello_world_rec.py with the following content:

  • hello_world_rec.py:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    import sys
    import time
    
    import ecal.core.core as ecal_core
    from ecal.core.subscriber import StringSubscriber
    
    # Callback for receiving messages
    def callback(topic_name, msg, time):
      print("Received: {}".format(msg))
    
    if __name__ == "__main__":
      # Initialize eCAL
      ecal_core.initialize(sys.argv, "Python Hello World Publisher")
    
      # Create a subscriber that listenes on the "hello_world_python_topic"
      sub = StringSubscriber("hello_world_python_topic")
    
      # Set the Callback
      sub.set_callback(callback)
      
      # Just don't exit
      while ecal_core.ok():
        time.sleep(0.5)
      
      # finalize eCAL API
      ecal_core.finalize()
    

    Note

    What is happening here?

    Line 5 Imports the eCAL StringSubscriber class

    Line 8-9 Is the receive callback. This method will be called whenever a new message arrives.

    Line 16 creates an eCAL subscriber that listens to the “hello_world_python_topic”.

    Line 19 adds the receive callback from above to the subscriber, so it can be called.

    Important

    eCAL Receive callbacks run in the subscriber’s receive thread. While the callback is running, the subscriber cannot receive new data. So, if your callback needs really long to compute, you may have to decouple your computations to not loose messages.

When you now execute hello_world_snd.py and hello_world_rec.py, the receiver application will receive the messages sent by the sender.

Python Hello World Sender and Receiver

Awesome, you have written your first eCAL publisher and subscriber with python! In general however you will want to have a better way to structure your data than a raw string. So let’s head on to the next topic where we will check out protobuf!

8.1.4. Files


├─  hello_world_snd.py
└─  hello_world_rec.py