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

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

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