Skip to content

Commit

Permalink
Add capnp python examples
Browse files Browse the repository at this point in the history
  • Loading branch information
CruxDevStuff committed May 7, 2023
1 parent 0849ff7 commit 68744bd
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
19 changes: 19 additions & 0 deletions samples/python/capnp/README.md
Original file line number Diff line number Diff line change
@@ -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);
}
```


8 changes: 8 additions & 0 deletions samples/python/capnp/messages.capnp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@0xb6249c5df1f7e512;

struct Imu {
timestamp @0: Int64;
accel @1: List(Float32);
gyro @2: List(Float32);
}

32 changes: 32 additions & 0 deletions samples/python/capnp/pub.py
Original file line number Diff line number Diff line change
@@ -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()
28 changes: 28 additions & 0 deletions samples/python/capnp/sub.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 68744bd

Please sign in to comment.