Browse Source

Improve Python ROS2 example

- reduce rate of Twist messages (every 500ms instead of triggered by received Pose)
- print received Pose messages on single line to prevent splitting in logs
tags/v0.2.5-alpha.3
Philipp Oppermann 2 years ago
parent
commit
a4bba0b959
Failed to extract signature
3 changed files with 15 additions and 19 deletions
  1. +13
    -13
      examples/python-ros2-dataflow/control_node.py
  2. +1
    -1
      examples/python-ros2-dataflow/dataflow.yml
  3. +1
    -5
      examples/python-ros2-dataflow/random_turtle.py

+ 13
- 13
examples/python-ros2-dataflow/control_node.py View File

@@ -12,16 +12,16 @@ for i in range(500):
if event is None:
break
if event["type"] == "INPUT":
print(
f"""Node received:
id: {event["id"]},
value: {event["value"]},
metadata: {event["metadata"]}"""
)
node.send_output(
"direction",
pa.array(
[random.random() + 1, 0, 0, 0, 0, (random.random() - 0.5) * 5],
type=pa.float64(),
),
)
match event["id"]:
case "turtle_pose":
print(
f"""Pose: {event["value"]}""".replace("\r", "").replace("\n", " ")
)
case "tick":
node.send_output(
"direction",
pa.array(
[random.random() + 1, 0, 0, 0, 0, (random.random() - 0.5) * 5],
type=pa.float64(),
),
)

+ 1
- 1
examples/python-ros2-dataflow/dataflow.yml View File

@@ -4,7 +4,6 @@ nodes:
source: ./random_turtle.py
inputs:
direction: control/direction
tick: dora/timer/millis/50
outputs:
- turtle_pose

@@ -13,5 +12,6 @@ nodes:
source: ./control_node.py
inputs:
turtle_pose: turtle/turtle_pose
tick: dora/timer/millis/500
outputs:
- direction

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

@@ -53,15 +53,11 @@ for i in range(500):

direction_arrow = pa.array([pa.scalar(direction)])
twist_writer.publish(direction_arrow)
case "tick":
pass

case "external":
pose = event.inner()[0].as_py()

assert (
pose["x"] != 5.544445
), "turtle should not be at initial x axis"
assert pose["x"] != 5.544445, "turtle should not be at initial x axis"
dora_node.send_output(
"turtle_pose",
pa.array(


Loading…
Cancel
Save