-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0849ff7
commit 68744bd
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |