From a4bba0b9594dc02ea842f2c653c530e6913a0907 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Mon, 4 Sep 2023 11:28:47 +0200 Subject: [PATCH] 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 --- examples/python-ros2-dataflow/control_node.py | 26 +++++++++---------- examples/python-ros2-dataflow/dataflow.yml | 2 +- .../python-ros2-dataflow/random_turtle.py | 6 +---- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/examples/python-ros2-dataflow/control_node.py b/examples/python-ros2-dataflow/control_node.py index 6355e065..15ec4bdd 100755 --- a/examples/python-ros2-dataflow/control_node.py +++ b/examples/python-ros2-dataflow/control_node.py @@ -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(), + ), + ) diff --git a/examples/python-ros2-dataflow/dataflow.yml b/examples/python-ros2-dataflow/dataflow.yml index e1f8d936..809c8d95 100644 --- a/examples/python-ros2-dataflow/dataflow.yml +++ b/examples/python-ros2-dataflow/dataflow.yml @@ -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 diff --git a/examples/python-ros2-dataflow/random_turtle.py b/examples/python-ros2-dataflow/random_turtle.py index 4fef3571..caf96ec1 100755 --- a/examples/python-ros2-dataflow/random_turtle.py +++ b/examples/python-ros2-dataflow/random_turtle.py @@ -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(