Browse Source

Add dataflow example

tags/v0.2.5-alpha.2
haixuanTao 2 years ago
parent
commit
f8ccd3b813
4 changed files with 95 additions and 1 deletions
  1. +32
    -0
      examples/python-dataflow/control_node.py
  2. +19
    -0
      examples/python-dataflow/dataflow.yaml
  3. +14
    -1
      examples/python-dataflow/random_turtle.py
  4. +30
    -0
      examples/python-standalone-bridge/random_turtle.py

+ 32
- 0
examples/python-dataflow/control_node.py View File

@@ -0,0 +1,32 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import random
from dora import Node
import pyarrow as pa

node = Node()

event = node.next()
direction = {
"linear": {
"x": random.random() + 1,
},
"angular": {
"z": (random.random() - 0.5) * 5,
},
}
if event["type"] == "INPUT":
print(
f"""Node received:
id: {event["id"]},
value: {event["data"]},
metadata: {event["metadata"]}"""
)
node.send_output(
"direction",
pa.array(
[random.random() + 1, 0, 0, 0, 0, random.random() - 0.5],
type=pa.uint8(),
),
)

+ 19
- 0
examples/python-dataflow/dataflow.yaml View File

@@ -0,0 +1,19 @@
nodes:
- id: dora_ros2_bridge
custom:
source: python
python: random_turtle.py
inputs:
direction: control/direction
timer: dora/timer/millis/50
outputs:
- turtle_pose

- id: control
custom:
source: python
args: ./control_node.py
inputs:
turtle_pose: dora_ros2_bridge/turtle_pose
outputs:
- direction

+ 14
- 1
examples/python-dataflow/random_turtle.py View File

@@ -1,6 +1,7 @@
import time, random
from dora_ros2_bridge import *
from dora import Node
import pyarrow as pa

node = Node()
context = Ros2Context()
@@ -34,4 +35,16 @@ for event in node:
pose = pose_reader.next()
if pose == None:
break
node.send_output(pose)
node.send_output(
pa.array(
[
pose["x"],
pose["y"],
pose["z"],
pose["theta"],
pose["linar_velocity"],
pose["angular_velocity"],
],
type=pa.uint8(),
)
)

+ 30
- 0
examples/python-standalone-bridge/random_turtle.py View File

@@ -0,0 +1,30 @@
from dora_ros2_bridge import *
import time, random

context = Ros2Context()
node = context.new_node("turtle_teleop", "/ros2_demo", Ros2NodeOptions(rosout = True))

topic_qos = Ros2QosPolicies(reliable = True, max_blocking_time = 0.1)

turtle_twist_topic = node.create_topic("/turtle1/cmd_vel", "geometry_msgs::Twist", topic_qos)
twist_writer = node.create_publisher(turtle_twist_topic)

turtle_pose_topic = node.create_topic("/turtle1/pose", "turtlesim::Pose", topic_qos)
pose_reader = node.create_subscription(turtle_pose_topic)

for i in range(500):
direction = {
'linear': {
'x': random.random() + 1,
},
'angular': {
'z': (random.random() - 0.5) * 5,
}
}
twist_writer.publish(direction);
while True:
pose = pose_reader.next()
if pose == None:
break
print(pose)
time.sleep(0.5)

Loading…
Cancel
Save