You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

random_turtle.py 2.3 kB

2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import time
  4. import random
  5. import dora
  6. from dora import Node
  7. import pyarrow as pa
  8. ros2_context = dora.experimental.ros2_bridge.Ros2Context()
  9. ros2_node = ros2_context.new_node(
  10. "turtle_teleop",
  11. "/ros2_demo",
  12. dora.experimental.ros2_bridge.Ros2NodeOptions(rosout=True),
  13. )
  14. topic_qos = dora.experimental.ros2_bridge.Ros2QosPolicies(
  15. reliable=True, max_blocking_time=0.1
  16. )
  17. turtle_twist_topic = ros2_node.create_topic(
  18. "/turtle1/cmd_vel", "geometry_msgs::Twist", topic_qos
  19. )
  20. twist_writer = ros2_node.create_publisher(turtle_twist_topic)
  21. turtle_pose_topic = ros2_node.create_topic(
  22. "/turtle1/pose", "turtlesim::Pose", topic_qos
  23. )
  24. pose_reader = ros2_node.create_subscription(turtle_pose_topic)
  25. dora_node = Node()
  26. dora_node.merge_external_events(pose_reader)
  27. print("looping", flush=True)
  28. for i in range(500):
  29. event = dora_node.next()
  30. if event is None:
  31. break
  32. match event["kind"]:
  33. case "dora":
  34. match event["type"]:
  35. case "INPUT":
  36. match event["id"]:
  37. case "direction":
  38. direction = {
  39. "linear": {
  40. "x": event["data"][0],
  41. },
  42. "angular": {
  43. "z": event["data"][5],
  44. },
  45. }
  46. print(direction, flush=True)
  47. twist_writer.publish(pa.array(direction))
  48. case "tick":
  49. pass
  50. case "external":
  51. pose = event.inner()[0].as_py()
  52. assert (
  53. pose["x"] != 5.544445
  54. ), "turtle should not be at initial x axis"
  55. dora_node.send_output(
  56. "turtle_pose",
  57. pa.array(
  58. [
  59. pose["x"],
  60. pose["y"],
  61. pose["theta"],
  62. pose["linear_velocity"],
  63. pose["angular_velocity"],
  64. ],
  65. type=pa.float64(),
  66. ),
  67. )

DORA (Dataflow-Oriented Robotic Architecture) is middleware designed to streamline and simplify the creation of AI-based robotic applications. It offers low latency, composable, and distributed datafl