test_robot.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. @pytest.fixture
  8. def robot_movement():
  9. config = ConfigParser()
  10. grbl_config = config.get_grbl_config()
  11. grbl_handler = GRBLHandler(grbl_config.get('port'), grbl_config.get('baudrate'))
  12. return RobotMovement(config, grbl_handler)
  13. @pytest.fixture
  14. def robot_controller():
  15. config = ConfigParser()
  16. config.config["mqtt"]["broker"] = "debug" # connects to test mqtt broker
  17. mock_gpio = MockGPIO()
  18. controller = RobotController(config, gpio_handler=mock_gpio)
  19. return controller
  20. class TestRobotMovement:
  21. def test_init_position(self, robot_movement: RobotMovement):
  22. assert robot_movement.get_position() == (0.0, 0.0, 0.0)
  23. assert not robot_movement.is_moving
  24. @pytest.mark.asyncio
  25. async def test_set_speed(self, robot_movement: RobotMovement):
  26. await robot_movement.grbl.connect()
  27. await robot_movement.set_speed(200.0)
  28. assert robot_movement.speed == 200.0
  29. with pytest.raises(ValueError):
  30. await robot_movement.set_speed(-100.0)
  31. # TODO [SG]: Getting current position currently does not work in debug mode
  32. @pytest.mark.asyncio
  33. async def test_move_to_position(self, robot_movement: RobotMovement):
  34. await robot_movement.grbl.connect()
  35. result = await robot_movement.move_to_position(10.0, 20.0, 30.0)
  36. assert result == (10.0, 20.0, 30.0)
  37. @pytest.mark.asyncio
  38. async def test_move_relative(self, robot_movement: RobotMovement):
  39. pos = (100.0, 100.0, 100.0)
  40. rel_pos = (50.0, -50.0, 25.0)
  41. await robot_movement.grbl.connect()
  42. await robot_movement.move_to_position(*pos)
  43. result = await robot_movement.move_relative(*rel_pos)
  44. # assert result == pos + rel_pos
  45. # TODO [SG]: Check doesnt work for now in the debug mode
  46. class TestRobotController:
  47. def test_init_controller(self, robot_controller: RobotController):
  48. assert len(robot_controller.devices) > 0
  49. assert robot_controller.feeder is not None
  50. assert not robot_controller.gripper_occupied
  51. @pytest.mark.asyncio
  52. async def test_get_next_free_slot(self, robot_controller: RobotController):
  53. slot = robot_controller.get_next_free_slot()
  54. assert slot is not None
  55. assert not slot.occupied
  56. @pytest.mark.asyncio
  57. async def test_insert_and_collect_cell(self, robot_controller: RobotController):
  58. # Test cell insertion
  59. cell = Cell(id="test_cell", status=CellStatus.WAITING)
  60. slot = robot_controller.get_next_free_slot()
  61. robot_controller.gripper_occupied = True
  62. await robot_controller.insert_cell_to_slot(cell, slot)
  63. assert slot.occupied
  64. assert slot.cell_id == cell.id
  65. # Test cell collection
  66. collected_cell = await robot_controller.collect_cell_from_slot(slot)
  67. assert not slot.occupied
  68. assert slot.cell_id is None
  69. assert collected_cell == cell.id
  70. @pytest.mark.asyncio
  71. async def test_sort_empty_gripper(self, robot_controller: RobotController):
  72. cell = Cell(
  73. id="test_cell",
  74. status=CellStatus.COMPLETED,
  75. capacity=3000.0
  76. )
  77. robot_controller.gripper_occupied = False
  78. assert not await robot_controller.sort_cell(cell)
  79. @pytest.mark.asyncio
  80. async def test_sort_failed_cell(self, robot_controller: RobotController):
  81. failed_cell = Cell(
  82. id=123,
  83. status=CellStatus.ERROR
  84. )
  85. robot_controller.gripper_occupied = True
  86. await robot_controller.sort_cell(failed_cell)
  87. assert not robot_controller.gripper_occupied
  88. @pytest.mark.asyncio
  89. async def test_sort_cell_by_capacity(self, robot_controller: RobotController):
  90. # Test sorting a cell with capacity that matches a grade
  91. cell = Cell(
  92. id="test_cell",
  93. status=CellStatus.COMPLETED,
  94. capacity=3000.0
  95. )
  96. robot_controller.gripper_occupied = True
  97. await robot_controller.sort_cell(cell)
  98. assert not robot_controller.gripper_occupied
  99. # Test sorting a cell with capacity that doesn't match any grade
  100. low_cap_cell = Cell(
  101. id="low_cap_cell",
  102. status=CellStatus.COMPLETED,
  103. capacity=0.0
  104. )
  105. robot_controller.gripper_occupied = True
  106. await robot_controller.sort_cell(low_cap_cell)
  107. assert not robot_controller.gripper_occupied