|
|
@@ -4,6 +4,7 @@ from typing import List, Tuple
|
|
|
from .config import ConfigParser, SlotConfig
|
|
|
from .movement import RobotMovement
|
|
|
import logging
|
|
|
+from api.routes import MQTTHandler, Device
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@@ -38,12 +39,30 @@ class RobotController:
|
|
|
self.movement = RobotMovement()
|
|
|
self.movement.set_speed(self.system_settings.speed)
|
|
|
|
|
|
+ # Initialize MQTT handler
|
|
|
+ mqtt_config = self.config.get_mqtt_config() # Add this to config parser
|
|
|
+ self.mqtt_handler = MQTTHandler(
|
|
|
+ broker=mqtt_config.get('broker'),
|
|
|
+ port=mqtt_config.get('port')
|
|
|
+ )
|
|
|
+
|
|
|
+ # Register all devices with MQTT handler
|
|
|
+ for device in self.devices:
|
|
|
+ self.mqtt_handler.register_device(
|
|
|
+ Device(device_id=device.id, num_slots=len(device.slots))
|
|
|
+ )
|
|
|
+
|
|
|
# Initialize with configured values
|
|
|
self.total_slots = sum(len(device.slots) for device in self.devices)
|
|
|
self.work_queue: List[SlotConfig] = []
|
|
|
|
|
|
self.gripper_occupied = False
|
|
|
|
|
|
+ async def cleanup(self):
|
|
|
+ """Cleanup resources on shutdown"""
|
|
|
+ # await self.movement.cleanup() TODO[SG]: Implement cleanup method in movement.py
|
|
|
+ self.mqtt_handler.cleanup()
|
|
|
+
|
|
|
async def pick_cell_from_feeder(self):
|
|
|
if self.gripper_occupied:
|
|
|
logger.error("Gripper already occupied")
|
|
|
@@ -58,9 +77,9 @@ class RobotController:
|
|
|
await self.movement.move_to_position(x, y, z)
|
|
|
# Grip cell
|
|
|
if await self.movement.activate_gripper():
|
|
|
- self.gripper_occupied = True
|
|
|
- else:
|
|
|
raise RuntimeError("Failed to grip cell")
|
|
|
+ self.gripper_occupied = True
|
|
|
+
|
|
|
# Move back to safe height
|
|
|
await self.movement.move_to_position(x, y, self.system_settings.safe_height)
|
|
|
logger.info("Cell picked from feeder")
|
|
|
@@ -90,10 +109,10 @@ class RobotController:
|
|
|
logger.error("Gripper not occupied")
|
|
|
return
|
|
|
slot = self.get_next_free_slot()
|
|
|
- if slot:
|
|
|
- await self.insert_cell_to_slot(cell, slot)
|
|
|
- else:
|
|
|
+ if not slot:
|
|
|
logger.error("No free slots available")
|
|
|
+ return
|
|
|
+ await self.insert_cell_to_slot(cell, slot)
|
|
|
|
|
|
async def insert_cell_to_slot(self, cell: Cell, slot: SlotConfig):
|
|
|
if slot.occupied:
|
|
|
@@ -111,12 +130,12 @@ class RobotController:
|
|
|
# Move down to insertion position
|
|
|
await self.movement.move_to_position(x, y, z)
|
|
|
# Release cell
|
|
|
- if await self.movement.deactivate_gripper():
|
|
|
- slot.occupied = True
|
|
|
- slot.cell_id = cell.id
|
|
|
- self.gripper_occupied = False
|
|
|
- else:
|
|
|
- raise RuntimeError("Failed to release cell")
|
|
|
+ if not await self.movement.deactivate_gripper():
|
|
|
+ raise RuntimeError("Failed to release cell")
|
|
|
+ slot.occupied = True
|
|
|
+ slot.cell_id = cell.id
|
|
|
+ self.gripper_occupied = False
|
|
|
+
|
|
|
# Move back to safe height
|
|
|
await self.movement.move_to_position(x, y, self.system_settings.safe_height)
|
|
|
logger.info(f"Cell {cell.id} inserted to slot at position {slot.position}")
|
|
|
@@ -137,13 +156,13 @@ class RobotController:
|
|
|
# Move down to collection position
|
|
|
await self.movement.move_to_position(x, y, z)
|
|
|
# Grip cell
|
|
|
- if await self.movement.activate_gripper():
|
|
|
- self.gripper_occupied = True
|
|
|
- cell_id = slot.cell_id
|
|
|
- slot.occupied = False
|
|
|
- slot.cell_id = None
|
|
|
- else:
|
|
|
+ if not await self.movement.activate_gripper():
|
|
|
raise RuntimeError("Failed to grip cell")
|
|
|
+ self.gripper_occupied = True
|
|
|
+ cell_id = slot.cell_id
|
|
|
+ slot.occupied = False
|
|
|
+ slot.cell_id = None
|
|
|
+
|
|
|
# Move back to safe height
|
|
|
await self.movement.move_to_position(x, y, self.system_settings.safe_height)
|
|
|
logger.info(f"Cell {cell_id} collected from slot at position {slot.position}")
|
|
|
@@ -183,10 +202,10 @@ class RobotController:
|
|
|
# Move to dropoff position
|
|
|
await self.movement.move_to_position(x, y, z)
|
|
|
# Release cell
|
|
|
- if await self.movement.deactivate_gripper():
|
|
|
- self.gripper_occupied = False
|
|
|
- else:
|
|
|
+ if not await self.movement.deactivate_gripper():
|
|
|
raise RuntimeError("Failed to release cell")
|
|
|
+ self.gripper_occupied = False
|
|
|
+
|
|
|
# Move back to safe height
|
|
|
await self.movement.move_to_position(x, y, self.system_settings.safe_height)
|
|
|
logger.info(f"Cell dropped off at grade {dropoff_grade.name}")
|