movement_playground.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import asyncio
  2. import time
  3. from dataclasses import dataclass
  4. from robot_control.src.robot.movement import RobotMovement
  5. from robot_control.src.api.grbl_handler import GRBLHandler
  6. from robot_control.src.utils.config import RobotConfig, GRBLConfig, MovementConfig, GPIOConfig, MQTTConfig
  7. from robot_control.src.api.gpio import PiGPIO
  8. """
  9. This is a test script for the robot movement system.
  10. It initializes the GRBL handler and the robot movement system,
  11. and performs a series of simple A->B movements to test the system.
  12. Can also use GPIO pins to control pump and valve.
  13. """
  14. # grbl_config = GRBLConfig(port="debug", baudrate=115200)
  15. grbl_config = GRBLConfig(port="/dev/ttyACM0", baudrate=115200)
  16. test_config = MovementConfig(
  17. speed=1000,
  18. safe_height=10,
  19. work_height=0
  20. )
  21. grbl_handler = GRBLHandler(
  22. grbl_config.port,
  23. grbl_config.baudrate
  24. )
  25. movement = RobotMovement(test_config, grbl_handler)
  26. pump_pin = 17
  27. valve_pin = 27
  28. gpio = PiGPIO([pump_pin, valve_pin], [])
  29. y_offset = 130
  30. # Real positions
  31. # positions = [
  32. # (453, 1028 - y_offset, 0),
  33. # (453, 1028 - y_offset, 107),
  34. # (453, 1028 - y_offset, 0),
  35. # (170, 760 - y_offset, 0),
  36. # (170, 760 - y_offset, 40),
  37. # (170, 760 - y_offset, 0),
  38. # (453, 600 - y_offset, 0), # neutral pos
  39. # ]
  40. # Test positions
  41. positions = [
  42. (100, 100, 0),
  43. (100, 100, 107),
  44. (100, 100, 0),
  45. (150, 150, 0),
  46. (150, 150, 40),
  47. (150, 150, 0),
  48. (100, 150, 0), # neutral pos
  49. ]
  50. async def wait_for_enter():
  51. # Use asyncio.Event to coordinate between input and async code
  52. event = asyncio.Event()
  53. def input_callback():
  54. input() # Wait for Enter
  55. event.set()
  56. # Run input in a separate thread to not block the event loop
  57. loop = asyncio.get_running_loop()
  58. await loop.run_in_executor(None, input_callback)
  59. await event.wait()
  60. async def perform_movement_sequence():
  61. for idx, pos in enumerate(positions):
  62. print(f"Moving to position {pos}...")
  63. if idx == 1:
  64. gpio.set_pin(pump_pin, 1)
  65. await asyncio.sleep(4)
  66. gpio.set_pin(pump_pin, 0)
  67. if idx == 2:
  68. gpio.set_pin(valve_pin, 1)
  69. await asyncio.sleep(0.5)
  70. if idx == 5:
  71. gpio.set_pin(valve_pin, 0)
  72. await asyncio.sleep(0.5)
  73. await movement.move_to_position(*pos)
  74. async def test_movement():
  75. await grbl_handler.connect()
  76. print("Press Enter to start movement sequence (or Ctrl+C to exit)...")
  77. while True:
  78. await wait_for_enter()
  79. print("Starting movement sequence...")
  80. await perform_movement_sequence()
  81. print("\nPress Enter to repeat sequence (or Ctrl+C to exit)...")
  82. if __name__ == "__main__":
  83. try:
  84. asyncio.run(test_movement())
  85. except KeyboardInterrupt:
  86. print("\nExiting...")