7.1.1. String: Hello World#

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 Application, that sends the string “Hello World” to an eCAL topic. The second part will be all about the subscriber, which will receive the message and print it to the console.

You will see how it is done in the major programming languages we support: C++, C, C# and Python.

For CMake usage tips, please refer to the CMake section of this documentation.

7.1.1.1. Hello World Publisher#

Let’s begin with the publisher side of our “Hello World” application.

The base initialization of the eCAL publisher is the same in all languages:

  1. Before you do anything else, you need to initialize eCAL with Initialize(..).

  2. Then you create the publisher and send a message in the frequency you want. In our example we will send the message every 500 ms in an infinite loop. You can add a stop condition to the loop, if you want to send just a limited amount of messages.

  3. After you are done with publishing data and you don’t need eCAL anymore, you can call the Finalize() function to clean up the resources and unregister the process.

For simplicity, we will use the same message type in all languages. As this is simple string message, we will use the eCAL string publisher to send.

 1// Include the basic eCAL header
 2#include <ecal/ecal.h>
 3/* 
 4  We want to publish raw strings, so wie include the string publisher header.
 5  eCAL supports multiple message formats.
 6*/
 7#include <ecal/msg/string/publisher.h>
 8
 9#include <iostream>
10#include <sstream>
11
12int main()
13{
14  std::cout << "--------------------------" << std::endl;
15  std::cout << " C++: HELLO WORLD SENDER"        << std::endl;
16  std::cout << "--------------------------" << std::endl;
17
18  /* 
19    Initialize eCAL. You always have to initialize eCAL before using its API.
20    The name of our eCAL Process will be "hello send". 
21    This name will be visible in the eCAL Monitor, once the process is running.
22  */
23  eCAL::Initialize("hello send");
24
25  /*
26    Print some eCAL version information.
27  */
28  std::cout << "eCAL " << eCAL::GetVersionString() << " (" << eCAL::GetVersionDateString() << ")" << "\n";
29
30  /*
31    Set the state for the program.
32    You can vary between different states like healthy, warning, critical ...
33    This can be used to communicate the application state to applications like eCAL Monitor/Sys.
34  */
35  eCAL::Process::SetState(eCAL::Process::eSeverity::healthy, eCAL::Process::eSeverityLevel::level1, "I feel good!");
36
37  /*
38    Creating the eCAL Publisher. An eCAL Process can create multiple publishers (and subscribers).
39    The topic we are going to publish is called "hello".
40  */
41  eCAL::string::CPublisher publisher("hello");
42
43  /*
44    Creating an inifite publish-loop.
45    eCAL Supports a stop signal; when an eCAL Process is stopped, eCAL::Ok() will return false.
46  */
47  int loop_count = 0;
48  while (eCAL::Ok())
49  {
50    /*
51      Build the string you want to send, using a stringstream in this example.
52    */
53    std::stringstream message;
54    message << "HELLO WORLD FROM C++" << " (" << ++loop_count << ")";
55
56    /*
57      Send the content to other eCAL Processes that have subscribed to the topic "hello".
58    */
59    if(publisher.Send(message.str()))
60      std::cout << "Sent string message in C++ \"" << message.str() << "\"" << "\n";
61    else
62      std::cout << "Sending string message in C++ failed!" << "\n";
63
64    /*
65      Sleep for 500 ms so we send with a frequency of 2 Hz.
66    */
67    eCAL::Process::SleepMS(500);
68  }
69
70  /*
71    Deinitialize eCAL.
72    You should always do that before your application exits.
73  */
74  eCAL::Finalize();
75
76  return(0);
77}

7.1.1.2. Hello World Publisher Files#

 Publisher samples
├─  C++
│  └─  hello_send.cpp
│
├─  C
│  └─  hello_send.c
│
├─  C#
│  └─  hello_send_csharp.cs
│
└─  Python
   └─  hello_send.py

7.1.1.3. Hello World Subscriber#

Now let’s have a look at the subscriber side. Basically, the initialization is the same as for the publisher. The only difference is that you need to assign a callback function to the subscriber:

  1. Call Initialize() to initialize eCAL.

  2. Create the subscriber.

  3. Assign a callback function to the subscriber with SetReceiveCallback.

  4. Do something to keep the process alive. In our example we will use a simple infinite loop. Process the incoming messages as you wish.

  5. After you are done with receiving data and you don’t need eCAL anymore, you can call the Finalize() function to clean up the resources and unregister the process.

 1// Include the basic eCAL header
 2#include <ecal/ecal.h>
 3/* 
 4  We want to receive raw strings, so wie include the string subscriber header.
 5  eCAL supports multiple message formats.
 6*/
 7#include <ecal/msg/string/subscriber.h>
 8
 9#include <iostream>
10#include <sstream>
11#include <chrono>
12#include <thread>
13
14int main()
15{
16  std::cout << "----------------------------" << "\n";
17  std::cout << " C++: HELLO WORLD RECEIVER"   << "\n";
18  std::cout << "----------------------------" << "\n";
19
20  /* 
21    Initialize eCAL. You always have to initialize eCAL before using its API.
22    The name of our eCAL Process will be "hello receive". 
23    This name will be visible in the eCAL Monitor, once the process is running.
24  */
25  eCAL::Initialize("hello receive");
26
27  /*
28    Print some eCAL version information.
29  */
30  std::cout << "eCAL " << eCAL::GetVersionString() << " (" << eCAL::GetVersionDateString() << ")" << "\n";
31
32  /*
33    Set the state for the program.
34    You can vary between different states like healthy, warning, critical ...
35    This can be used to communicate the application state to applications like eCAL Monitor/Sys.
36  */
37  eCAL::Process::SetState(eCAL::Process::eSeverity::healthy, eCAL::Process::eSeverityLevel::level1, "I feel good!");
38
39  /*
40    Creating the eCAL Subscriber. An eCAL Process can create multiple subscribers (and publishers).
41    The topic we are going to receive is called "hello".
42  */
43  eCAL::string::CSubscriber subscriber("hello");
44
45  /*
46    Creating a receive callback. The callback will be called whenever a new message is received.
47  */
48  auto message_callback = [](const eCAL::STopicId& publisher_id_, const std::string& message_, long long time_, long long clock_) { 
49    std::cout << "---------------------------------------------------"                                << "\n";
50    std::cout << " Received string message from topic \"" << publisher_id_.topic_name << "\" in C++ " << "\n";
51    std::cout << "---------------------------------------------------"                                << "\n";
52    std::cout << " Size    : " << message_.size()                                                     << "\n";
53    std::cout << " Time    : " << time_                                                               << "\n";
54    std::cout << " Clock   : " << clock_                                                              << "\n";
55    std::cout << " Message : " << message_                                                            << "\n";
56    std::cout << "\n";
57  };
58  
59  /*
60    Register the callback with the subscriber so it can be called.
61  */
62  subscriber.SetReceiveCallback(message_callback);
63
64  /*
65    Creating an infinite loop.
66    eCAL Supports a stop signal; when an eCAL Process is stopped, eCAL::Ok() will return false.
67  */
68  while (eCAL::Ok())
69  {
70    /*
71      Sleep for 500ms to avoid busy waiting.
72      You can use eCAL::Process::SleepMS() to sleep in milliseconds.
73    */
74    eCAL::Process::SleepMS(500);
75  }
76
77  /*
78    Deinitialize eCAL.
79    You should always do that before your application exits.
80  */
81  eCAL::Finalize();
82
83  return(0);
84}

7.1.1.4. Hello World Subscriber Files#

 Subscriber samples
├─  C++
│  └─  hello_receive.cpp
│
├─  C
│  └─  hello_receive.c
│
├─  C#
│  └─  hello_receive_csharp.cs
│
└─  Python
   └─  hello_receive.py