Skip to content

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.

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 All releases and download the .whl file. Open a command prompt in your donwload directory and install the python whl with:
      Terminal window
      pip install ecal5-****-win_amd64.whl
  • Ubuntu:

    • Python 3:
      Terminal window
      sudo apt install python3 python3-pip
    • The eCAL Python integration.
      • If you have installed eCAL from a PPA:
        Terminal window
        sudo apt install python3-ecal5
      • If you are not using a PPA, choose the appropiate version from the All releases and download the .whl file. Open a terminal in your donwload directory and install the python whl with:
        Terminal window
        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_snd.py with the following content:

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

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

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