7.1. Hello World

Important

This tutorial will only work with eCAL 5.7.3 and upwards. It will not work with older versions that were published as .msi installer (it missed some libraries).

Please switch to Ubuntu, if you are using an old eCAL Version.

After you have learned a lot about the pre-compiled applications that come with eCAL, let’s create our own! In the good habit of every tutorial, we will write a Hello World Applications, that sends the string “Hello World” to an eCAL topic.

eCAL uses CMake as a cross-platform build toolchain. We will explain the CMake commands needed for the tutorial, but not extensively dive into CMake.

7.1.1. Dependencies

First, you have to install some more development dependencies:

7.1.2. Hello World Publisher

Somewhere on your hard drive create an empty directory and create a file CMakeLists.txt and main.cpp with the following content:

  • CMakeLists.txt:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    cmake_minimum_required(VERSION 3.0)
    
    project(hello_world_snd)
    
    find_package(eCAL REQUIRED)
    
    set(source_files
      main.cpp
    )
    
    add_executable(${PROJECT_NAME} ${source_files})
    
    target_link_libraries(${PROJECT_NAME}
      eCAL::core
    )
    

    Note

    What is happening here?

    Line 3 creates a project “hello_world_snd”. This will also be the name of the executable (line 11).

    Line 5 tells CMake to find the eCAL installation. Line 13-15 will link the executable against it.

    Line 7-9 create a list of all our source files, which currently only contains main.cpp. We add that source file for compiling our executable in line 11.

  • main.cpp:

     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
    32
    33
    #include <ecal/ecal.h>
    #include <ecal/msg/string/publisher.h>
    
    #include <iostream>
    #include <thread>
    
    int main(int argc, char** argv)
    {
      // Initialize eCAL. The name of our Process will be "Hello World Publisher"
      eCAL::Initialize(argc, argv, "Hello World Publisher");
    
      // Create a String Publisher that publishes on the topic "hello_world_topic"
      eCAL::string::CPublisher<std::string> publisher("hello_world_topic");
    
      // Create a counter, so something changes in our message
      int counter = 0;
    
      // Infinite loop (using eCAL::Ok() will enable us to gracefully shutdown the
      // Process from another application)
      while (eCAL::Ok())
      {
        // Create a message with a counter an publish it to the topic
        std::string message = "Hello World " + std::to_string(++counter);
        std::cout << "Sending message: " << message << std::endl;
        publisher.Send(message);
    
        // Sleep 500 ms
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
      }
    
      // finalize eCAL API
      eCAL::Finalize();
    }
    

    Note

    What is happening here?

    Line 1 includes the basic eCAL header. As we want to publish raw strings, line 2 includes the eCAL String-Publisher. eCAL Supports multiple message formats.

    Line 10 initialized eCAL. You always have to initialize eCAL before using its API. The name of our eCAL Process will be “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_topic”.

    The while loop from line 20 will cause an infinite publish-loop. eCAL supports a stop-signal; when an eCAL Process is stopped, eCAL::Ok() will return false.

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

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

Now that you have the source code ready, create a _build directory and build the code!

  • Windows:

    mkdir _build
    cd _build
    cmake .. -A x64
    cmake --build . --parallel
    
  • Ubuntu:

    mkdir _build
    cd _build
    cmake ..
    make
    

Now execute the hello_world_snd (.exe) and take a look at the eCAL Monitor! You will see the “Hello World Publisher” process and the “hello_world_topic”.

eCAL Monitor Hello World

7.1.3. Hello World Subscriber

Again, create a new directory somewhere and add create the CMakeLists.txt and main.cpp with the following content:

  • CMakeLists.txt:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    cmake_minimum_required(VERSION 3.0)
    
    project(hello_world_rec)
    
    find_package(eCAL REQUIRED)
    
    set(source_files
      main.cpp
    )
    
    add_executable(${PROJECT_NAME} ${source_files})
    
    target_link_libraries(${PROJECT_NAME}
      eCAL::core
    )
    

    Note

    What is happening here?

    Line 3 creates a project “hello_world_snd”. This will also be the name of the executable (line 11).

    Line 5 tells CMake to find the eCAL installation. Line 13-15 will link the executable against it.

    Line 7-9 create a list of all our source files, which currently only contains main.cpp. We add that source file for compiling our executable in line 11.

  • main.cpp:

     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
    #include <ecal/ecal.h>
    #include <ecal/msg/string/subscriber.h>
    
    #include <iostream>
    #include <thread>
    
    void HelloWorldCallback(const std::string& message)
    {
      std::cout << "Received Message: " << message << std::endl;
    }
    
    
    int main(int argc, char** argv)
    {
      eCAL::Initialize(argc, argv, "Hello World Subscriber");
      eCAL::string::CSubscriber<std::string> subscriber("hello_world_topic");
    
      // Set the Callback
      subscriber.AddReceiveCallback(std::bind(&HelloWorldCallback, std::placeholders::_2));
    
      // Just don't exit
      while (eCAL::Ok())
      {
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
      }
    
      // finalize eCAL API
      eCAL::Finalize();
    }
    

    Note

    What is happening here?

    Line 7-10 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_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.

Now that you have the source code ready, create a _build directory and build the code!

  • Windows:

    mkdir _build
    cd _build
    cmake .. -A x64
    cmake --build . --parallel
    
  • Ubuntu:

    mkdir _build
    cd _build
    cmake ..
    make
    

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

eCAL Hello World sender and receiver

In the next chapter you will learn how to properly structure your messages with protobuf!

7.1.4. Files


├─  hello_world_snd
│  ├─  CMakeLists.txt
│  └─  main.cpp
│
└─  hello_world_rec
   ├─  CMakeLists.txt
   └─  main.cpp