mag_dist_playground.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import sys
  2. import time
  3. from robot_control.src.robot.mag_distributor import MagDistributor
  4. from robot_control.src.utils.config import ConfigParser
  5. from robot_control.src.api.gpio import GPIOInterface, MockGPIO, PiGPIO
  6. def main():
  7. # You may need to adjust these to your environment
  8. config = ConfigParser().config
  9. gpio_config = config.gpio
  10. gpio_config.mag_dist_rot_servo_pin = 6
  11. if gpio_config.debug:
  12. gpio = MockGPIO()
  13. else:
  14. gpio = PiGPIO()
  15. mag_dist = MagDistributor(config, gpio)
  16. print("MagDistributor Playground")
  17. print("Commands:")
  18. print(" h - home")
  19. print(" m <pos_mm> <rot_deg> [speed_mmmin] [servo_speed_ms] - move to position")
  20. print(" q - quit")
  21. print("Examples:")
  22. print(" m -50 60 - move to -50mm, 60° with default speeds")
  23. print(" m -50 60 1000 - move to -50mm, 60° at 1000 mm/min")
  24. print(" m -50 60 1000 500 - move to -50mm, 60° at 1000 mm/min, servo 500ms")
  25. while True:
  26. cmd = input("Enter command: ").strip()
  27. if cmd == "q":
  28. print("Exiting.")
  29. break
  30. elif cmd == "h":
  31. print("Homing...")
  32. import asyncio
  33. asyncio.run(mag_dist.home())
  34. print("Homed.")
  35. elif cmd.startswith("m "):
  36. try:
  37. parts = cmd.split()
  38. if len(parts) < 3:
  39. print("Usage: m <pos_mm> <rot_deg> [speed_mmmin] [servo_speed_ms]")
  40. continue
  41. pos_mm = float(parts[1])
  42. rot_deg = float(parts[2])
  43. speed_mmmin = float(parts[3]) if len(parts) > 3 else None
  44. servo_speed_ms = float(parts[4]) if len(parts) > 4 else 1000
  45. print(f"Moving to {pos_mm} mm, {rot_deg}°" +
  46. (f" at {speed_mmmin} mm/min" if speed_mmmin else " (default speed)") +
  47. f" (servo: {servo_speed_ms}ms)")
  48. if speed_mmmin:
  49. # Convert speed to step delay
  50. step_delay = mag_dist._speed_to_step_delay(speed_mmmin)
  51. mag_dist.move_mag_distributor_at_pos(pos_mm, rot_deg, servo_speed_ms, step_delay)
  52. else:
  53. mag_dist.move_mag_distributor_at_pos(pos_mm, rot_deg, servo_speed_ms)
  54. print(f"✓ Moved to {pos_mm} mm, {rot_deg}°")
  55. except Exception as e:
  56. print(f"Invalid move command: {e}")
  57. print("Usage: m <pos_mm> <rot_deg> [speed_mmmin] [servo_speed_ms]")
  58. else:
  59. print("Unknown command.")
  60. if __name__ == "__main__":
  61. main()