movement_playground.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. )
  20. grbl_handler = GRBLHandler(
  21. grbl_config.port,
  22. grbl_config.baudrate
  23. )
  24. movement = RobotMovement(test_config, grbl_handler)
  25. pump_pin = 17
  26. valve_pin = 27
  27. gpio = PiGPIO([pump_pin, valve_pin], [])
  28. y_offset = 130
  29. # Real positions
  30. # positions = [
  31. # (453, 1028 - y_offset, 0),
  32. # (453, 1028 - y_offset, 107),
  33. # (453, 1028 - y_offset, 0),
  34. # (170, 760 - y_offset, 0),
  35. # (170, 760 - y_offset, 40),
  36. # (170, 760 - y_offset, 0),
  37. # (453, 600 - y_offset, 0), # neutral pos
  38. # ]
  39. # Test positions
  40. positions = [
  41. (100, 100, 0),
  42. (100, 100, 107),
  43. (100, 100, 0),
  44. (150, 150, 0),
  45. (150, 150, 40),
  46. (150, 150, 0),
  47. (100, 150, 0), # neutral pos
  48. ]
  49. async def wait_for_enter():
  50. # Use asyncio.Event to coordinate between input and async code
  51. event = asyncio.Event()
  52. def input_callback():
  53. input() # Wait for Enter
  54. event.set()
  55. # Run input in a separate thread to not block the event loop
  56. loop = asyncio.get_running_loop()
  57. await loop.run_in_executor(None, input_callback)
  58. await event.wait()
  59. async def perform_movement_sequence():
  60. for idx, pos in enumerate(positions):
  61. print(f"Moving to position {pos}...")
  62. if idx == 1:
  63. gpio.set_pin(pump_pin, 1)
  64. await asyncio.sleep(4)
  65. gpio.set_pin(pump_pin, 0)
  66. if idx == 2:
  67. gpio.set_pin(valve_pin, 1)
  68. await asyncio.sleep(0.5)
  69. if idx == 5:
  70. gpio.set_pin(valve_pin, 0)
  71. await asyncio.sleep(0.5)
  72. await movement.move_to_position(*pos)
  73. async def test_movement():
  74. await grbl_handler.connect()
  75. print("Press Enter to start movement sequence (or Ctrl+C to exit)...")
  76. while True:
  77. await wait_for_enter()
  78. print("Starting movement sequence...")
  79. await perform_movement_sequence()
  80. print("\nPress Enter to repeat sequence (or Ctrl+C to exit)...")
  81. if __name__ == "__main__":
  82. try:
  83. asyncio.run(test_movement())
  84. except KeyboardInterrupt:
  85. print("\nExiting...")