Browse Source

fixing couple of issue within the dataflow example

tags/v0.2.5-alpha.2
haixuanTao 2 years ago
parent
commit
49a1695156
4 changed files with 43 additions and 42 deletions
  1. +12
    -20
      examples/python-dataflow/control_node.py
  2. +5
    -5
      examples/python-dataflow/dataflow.yaml
  3. +15
    -8
      examples/python-dataflow/random_turtle.py
  4. +11
    -9
      examples/python-standalone-bridge/random_turtle.py

+ 12
- 20
examples/python-dataflow/control_node.py View File

@@ -7,26 +7,18 @@ 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:
for event in node:
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(),
),
)
)
node.send_output(
"direction",
pa.array(
[random.random() + 1, 0, 0, 0, 0, random.random() - 0.5],
type=pa.uint8(),
),
)

+ 5
- 5
examples/python-dataflow/dataflow.yaml View File

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

@@ -14,6 +14,6 @@ nodes:
source: python
args: ./control_node.py
inputs:
turtle_pose: dora_ros2_bridge/turtle_pose
turtle_pose: turtle/turtle_pose
outputs:
- direction
- direction

+ 15
- 8
examples/python-dataflow/random_turtle.py View File

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

node = Node()

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

@@ -17,7 +18,8 @@ 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 event in node:
print("looping", flush=True)
for event in dora_node:
match event["type"]:
case "INPUT":
match event["id"]:
@@ -30,21 +32,26 @@ for event in node:
"z": event["data"][5],
},
}

print(direction, flush=True)
twist_writer.publish(direction)
case "tick":
pose = pose_reader.next()

if pose == None:
break
node.send_output(
print("stop", flush=True)
continue

dora_node.send_output(
"turtle_pose",
pa.array(
[
pose["x"],
pose["y"],
pose["z"],
pose["theta"],
pose["linar_velocity"],
pose["linear_velocity"],
pose["angular_velocity"],
],
type=pa.uint8(),
)
),
)

+ 11
- 9
examples/python-standalone-bridge/random_turtle.py View File

@@ -2,11 +2,13 @@ from dora_ros2_bridge import *
import time, random

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

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

turtle_twist_topic = node.create_topic("/turtle1/cmd_vel", "geometry_msgs::Twist", topic_qos)
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)
@@ -14,14 +16,14 @@ pose_reader = node.create_subscription(turtle_pose_topic)

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


Loading…
Cancel
Save