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.9 kB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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["value"][0],
  41. },
  42. "angular": {
  43. "z": event["value"][5],
  44. },
  45. }
  46. print(direction, flush=True)
  47. # TODO FIXME: The below line seems to result in an
  48. # `["linear", "angular"]` array, completely ignoring the values.
  49. direction_arrow = pa.array(
  50. direction,
  51. # TODO: maybe type annotations help somehow?
  52. # type=pa.map_(
  53. # pa.utf8(),
  54. # pa.map_(pa.utf8(), pa.float32()),
  55. # ),
  56. )
  57. print(direction_arrow, flush=True)
  58. # twist_writer.publish(direction_arrow)
  59. case "tick":
  60. pass
  61. case "external":
  62. pose = event.inner()[0].as_py()
  63. assert (
  64. pose["x"] != 5.544445
  65. ), "turtle should not be at initial x axis"
  66. dora_node.send_output(
  67. "turtle_pose",
  68. pa.array(
  69. [
  70. pose["x"],
  71. pose["y"],
  72. pose["theta"],
  73. pose["linear_velocity"],
  74. pose["angular_velocity"],
  75. ],
  76. type=pa.float64(),
  77. ),
  78. )

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