test_robot.py 4.8 KB

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