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.

head.py 2.1 kB

10 months ago
10 months ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """TODO: Add docstring."""
  2. import os
  3. import numpy as np
  4. from dora import Node
  5. from reachy2_sdk import ReachySDK
  6. def main():
  7. """TODO: Add docstring."""
  8. robot_ip = os.getenv("ROBOT_IP", "10.42.0.80")
  9. for _i in range(5):
  10. reachy = ReachySDK(robot_ip)
  11. if reachy.head is not None:
  12. reachy.head.turn_on()
  13. reachy.head.goto([0, 0, 0])
  14. break
  15. fov_h = 107
  16. fov_v = 91
  17. resolution = [720, 960]
  18. roll, _pitch, yaw = reachy.head.get_current_positions()
  19. node = Node()
  20. for event in node:
  21. if event["type"] == "INPUT":
  22. if event["id"] == "boxes2d":
  23. boxes = event["value"].to_numpy()
  24. # [x, y, z, _rx, ry, rz] = event["value"].to_numpy()
  25. x_min = boxes[0]
  26. y_min = boxes[1]
  27. x_max = boxes[2]
  28. y_max = boxes[3]
  29. x = (
  30. x_min + x_max
  31. ) / 2 - 10 # Deviate a bit to take into account the off centered camera
  32. y = (3 * y_min + y_max) / 4
  33. ry = (x - resolution[1] / 2) * fov_h / 2 / resolution[1]
  34. rz = (y - resolution[0] / 2) * fov_v / 2 / resolution[0]
  35. if np.abs(yaw) > 45 and yaw * -ry > 0:
  36. reachy.head.cancel_all_goto()
  37. roll, _pitch, yaw = reachy.head.get_current_positions()
  38. reachy.head.rotate_by(pitch=0, yaw=0, roll=-roll, duration=1.5)
  39. else:
  40. reachy.head.cancel_all_goto()
  41. roll, _pitch, yaw = reachy.head.get_current_positions()
  42. reachy.head.rotate_by(pitch=rz, yaw=-ry, roll=-roll, duration=1.5)
  43. [roll, _pitch, yaw] = reachy.head.get_current_positions()
  44. if "look" in event["id"]:
  45. [x, y, z] = event["value"].to_numpy()
  46. reachy.head.cancel_all_goto()
  47. reachy.head.look_at(x, y, z, duration=0.5)
  48. if reachy.head is not None:
  49. reachy.head.turn_off()
  50. if __name__ == "__main__":
  51. main()