test_robot.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import pytest
  2. from robot_control.src.robot.movement import RobotMovement
  3. from robot_control.src.robot.controller import RobotController, Cell, CellStatus
  4. from robot_control.src.utils.config import ConfigParser
  5. from robot_control.src.api.grbl_handler import GRBLHandler
  6. from robot_control.src.api.gpio import MockGPIO
  7. import asyncio
  8. @pytest.fixture
  9. def robot_movement():
  10. config = ConfigParser().config
  11. grbl = config.grbl
  12. grbl.port = "debug"
  13. grbl_handler = GRBLHandler(grbl.port, grbl.baudrate)
  14. return RobotMovement(config.movement, grbl_handler)
  15. @pytest.fixture
  16. def robot_controller():
  17. config = ConfigParser().config
  18. config.grbl.port = "debug"
  19. config.mqtt.broker = "debug" # connects to test mqtt broker
  20. mock_gpio = MockGPIO()
  21. feeder_queue = asyncio.Queue(config.feeder.max_num_cells)
  22. defeeder_queue = asyncio.Queue(config.defeeder.max_num_cells)
  23. controller = RobotController(
  24. config,
  25. gpio_handler=mock_gpio,
  26. i2c=None,
  27. vision=None,
  28. pump_controller=None,
  29. feeder_queue=feeder_queue,
  30. defeeder_queue=defeeder_queue
  31. )
  32. return controller
  33. class TestRobotMovement:
  34. def test_init_position(self, robot_movement: RobotMovement):
  35. assert robot_movement.get_position() == (0.0, 0.0, 0.0)
  36. assert not robot_movement.is_moving
  37. @pytest.mark.asyncio
  38. async def test_set_speed(self, robot_movement: RobotMovement):
  39. await robot_movement.grbl.connect()
  40. speed_mmin = 400
  41. await robot_movement.set_speed(speed_mmin)
  42. assert robot_movement.default_speed_mmmin == speed_mmin
  43. with pytest.raises(ValueError):
  44. await robot_movement.set_speed(-400)
  45. # TODO [SG]: Getting current position currently does not work in debug mode
  46. @pytest.mark.asyncio
  47. async def test_move_to_position(self, robot_movement: RobotMovement):
  48. await robot_movement.grbl.connect()
  49. result = await robot_movement.move_to_position(10.0, 20.0, 30.0)
  50. assert result == (10.0, 20.0, 30.0)
  51. @pytest.mark.asyncio
  52. async def test_move_relative(self, robot_movement: RobotMovement):
  53. pos = (100.0, 100.0, 100.0)
  54. rel_pos = (50.0, -50.0, 25.0)
  55. await robot_movement.grbl.connect()
  56. await robot_movement.move_to_position(*pos)
  57. result = await robot_movement.move_relative(*rel_pos)
  58. # assert result == pos + rel_pos
  59. # TODO [SG]: Check doesnt work for now in the debug mode
  60. class TestRobotController:
  61. def test_init_controller(self, robot_controller: RobotController):
  62. assert len(robot_controller.devices) > 0
  63. assert robot_controller.feeder is not None
  64. assert not robot_controller.gripper_occupied
  65. @pytest.mark.asyncio
  66. async def test_get_next_free_slot(self, robot_controller: RobotController):
  67. slot = robot_controller.get_next_free_slot()
  68. assert slot is not None
  69. assert not slot.occupied
  70. @pytest.mark.asyncio
  71. async def test_insert_and_pick_cell(self, robot_controller: RobotController):
  72. # Test cell insertion
  73. cell = Cell(id=1234, status=CellStatus.WAITING)
  74. slot = robot_controller.get_next_free_slot()
  75. assert slot is not None
  76. robot_controller.gripper_occupied = True
  77. await robot_controller.insert_cell_to_slot(cell, slot)
  78. assert slot.occupied
  79. assert slot.cell_id == cell.id
  80. # Test cell collection
  81. collected_cell = await robot_controller.pick_cell_from_slot(slot)
  82. assert not slot.occupied
  83. assert slot.cell_id is None
  84. assert collected_cell == cell.id
  85. @pytest.mark.asyncio
  86. async def test_sort_empty_gripper(self, robot_controller: RobotController):
  87. cell = Cell(
  88. id=1234,
  89. status=CellStatus.COMPLETED,
  90. health=100
  91. )
  92. robot_controller.gripper_occupied = False
  93. assert not await robot_controller.sort_cell(cell)
  94. @pytest.mark.asyncio
  95. async def test_sort_failed_cell(self, robot_controller: RobotController):
  96. failed_cell = Cell(
  97. id=123,
  98. status=CellStatus.ERROR
  99. )
  100. robot_controller.gripper_occupied = True
  101. await robot_controller.sort_cell(failed_cell)
  102. assert not robot_controller.gripper_occupied
  103. @pytest.mark.asyncio
  104. async def test_sort_cell_by_capacity(self, robot_controller: RobotController):
  105. # Test sorting a cell with capacity that matches a grade
  106. cell = Cell(
  107. id=0,
  108. status=CellStatus.COMPLETED,
  109. health=100
  110. )
  111. robot_controller.gripper_occupied = True
  112. await robot_controller.sort_cell(cell)
  113. assert not robot_controller.gripper_occupied
  114. # Test sorting a cell with capacity that doesn't match any grade
  115. low_cap_cell = Cell(
  116. id=1234,
  117. status=CellStatus.COMPLETED,
  118. health=0.0
  119. )
  120. robot_controller.gripper_occupied = True
  121. await robot_controller.sort_cell(low_cap_cell)
  122. assert not robot_controller.gripper_occupied