test_robot.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import pytest
  2. from robot.movement import RobotMovement
  3. from robot.controller import RobotController, Cell, CellStatus
  4. from utils.config import ConfigParser
  5. from api.grbl_handler import GRBLHandler
  6. from 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 is True
  36. assert robot_movement.get_position() == (10.0, 20.0, 30.0)
  37. @pytest.mark.asyncio
  38. async def test_move_relative(self, robot_movement: RobotMovement):
  39. await robot_movement.grbl.connect()
  40. await robot_movement.move_to_position(100.0, 100.0, 100.0)
  41. assert await robot_movement.move_relative(50.0, -50.0, 25.0)
  42. assert robot_movement.get_position() == (150.0, 50.0, 125.0)
  43. class TestRobotController:
  44. def test_init_controller(self, robot_controller: RobotController):
  45. assert len(robot_controller.devices) > 0
  46. assert robot_controller.feeder is not None
  47. assert not robot_controller.gripper_occupied
  48. @pytest.mark.asyncio
  49. async def test_get_next_free_slot(self, robot_controller: RobotController):
  50. slot = robot_controller.get_next_free_slot()
  51. assert slot is not None
  52. assert not slot.occupied
  53. @pytest.mark.asyncio
  54. async def test_insert_and_collect_cell(self, robot_controller: RobotController):
  55. # Test cell insertion
  56. cell = Cell(id="test_cell", status=CellStatus.WAITING)
  57. slot = robot_controller.get_next_free_slot()
  58. robot_controller.gripper_occupied = True
  59. await robot_controller.insert_cell_to_slot(cell, slot)
  60. assert slot.occupied
  61. assert slot.cell_id == cell.id
  62. # Test cell collection
  63. collected_cell = await robot_controller.collect_cell_from_slot(slot)
  64. assert not slot.occupied
  65. assert slot.cell_id is None
  66. assert collected_cell == cell.id
  67. @pytest.mark.asyncio
  68. async def test_sort_empty_gripper(self, robot_controller: RobotController):
  69. cell = Cell(
  70. id="test_cell",
  71. status=CellStatus.COMPLETED,
  72. measurement_slot=1,
  73. capacity=3000.0
  74. )
  75. robot_controller.gripper_occupied = False
  76. assert not await robot_controller.sort_cell(cell)
  77. @pytest.mark.asyncio
  78. async def test_sort_failed_cell(self, robot_controller: RobotController):
  79. failed_cell = Cell(
  80. id=123,
  81. status=CellStatus.FAILED,
  82. measurement_slot=1
  83. )
  84. robot_controller.gripper_occupied = True
  85. await robot_controller.sort_cell(failed_cell)
  86. assert not robot_controller.gripper_occupied
  87. @pytest.mark.asyncio
  88. async def test_sort_cell_by_capacity(self, robot_controller: RobotController):
  89. # Test sorting a cell with capacity that matches a grade
  90. cell = Cell(
  91. id="test_cell",
  92. status=CellStatus.COMPLETED,
  93. measurement_slot=1,
  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. measurement_slot=1,
  104. capacity=0.0
  105. )
  106. robot_controller.gripper_occupied = True
  107. await robot_controller.sort_cell(low_cap_cell)
  108. assert not robot_controller.gripper_occupied