diff --git a/samples/python/capnp/README.md b/samples/python/capnp/README.md new file mode 100644 index 0000000000..a5cece1f7d --- /dev/null +++ b/samples/python/capnp/README.md @@ -0,0 +1,19 @@ +## Capnp Publisher and Subscriber Example + +Minimal Publisher - publishes an example Imu message +`python3 pub.py` + +Minimal Subscriber - subscribes to the Imu message +`python3 sub.py` + +Capnp Message Struct + +``` +struct Imu { + timestamp @0: Int64; + accel @1: List(Float32); + gyro @2: List(Float32); +} +``` + + diff --git a/samples/python/capnp/messages.capnp b/samples/python/capnp/messages.capnp new file mode 100644 index 0000000000..35f711cf10 --- /dev/null +++ b/samples/python/capnp/messages.capnp @@ -0,0 +1,8 @@ +@0xb6249c5df1f7e512; + +struct Imu { + timestamp @0: Int64; + accel @1: List(Float32); + gyro @2: List(Float32); +} + diff --git a/samples/python/capnp/pub.py b/samples/python/capnp/pub.py new file mode 100644 index 0000000000..84068a97f2 --- /dev/null +++ b/samples/python/capnp/pub.py @@ -0,0 +1,32 @@ +import sys +import time +import capnp +import random +import time +import messages_capnp as msgs +import ecal.core.core as ecal_core +from ecal.core.publisher import StringPublisher, MessagePublisher, CapnPublisher + +if __name__ == "__main__": + ecal_core.initialize(sys.argv, "CapnProto Message Publisher") + + # Create a CapnProto Publisher that publishes on the topic "sensor_imu" + pub = CapnPublisher("sensor_imu", msgs.Imu) + + # Infinite loop (using ecal_core.ok() will enable us to gracefully shutdown + # the process from another application) + while ecal_core.ok(): + # Create a capnproto Imu message, populate it with random values and publish it to the topic + imu_msg = msgs.Imu.new_message() + imu_msg.timestamp = time.monotonic_ns() + imu_msg.accel = [round(random.uniform(2.0, 20.0), 2), round(random.uniform(2.0, 20.0), 2), round(random.uniform(2.0, 20.0), 2)] + imu_msg.gyro = [round(random.uniform(0.0, 4.3), 2), round(random.uniform(0.0, 4.3), 2), round(random.uniform(0.0, 4.3), 2)] + + print("Sending: {}".format(imu_msg)) + pub.send(imu_msg) + + # Sleep 500 ms + time.sleep(0.5) + + # finalize eCAL API + ecal_core.finalize() diff --git a/samples/python/capnp/sub.py b/samples/python/capnp/sub.py new file mode 100644 index 0000000000..355ac71250 --- /dev/null +++ b/samples/python/capnp/sub.py @@ -0,0 +1,28 @@ +import sys +import time +import capnp +import messages_capnp as msgs +import ecal.core.core as ecal_core +from ecal.core.subscriber import MessageSubscriber, CapnSubscriber + +# callback function for the subscriber +def callback(topic_name, msg, time): + # print the received message + print(msg) + +if __name__ == "__main__": + # Initialize eCAL + ecal_core.initialize(sys.argv, "CapnProto Message Subscriber") + + # Create a subscriber that listenes on the "sensor_imu" topic + sub = CapnSubscriber("sensor_imu", msgs.Imu) + + # Set the callback + sub.set_callback(callback) + + # infinite loop that keeps the script alive + while ecal_core.ok(): + time.sleep(0.5) + + # finalize eCAL API + ecal_core.finalize()