Update warehouse digital twin and robot fleet optimization
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
"""Unit tests for rack-side picking points, approach positions, and grid coordinate conventions."""
|
||||
|
||||
import pytest
|
||||
from warehouse_twin.domain.grid import CellType, GridConfig, WarehouseGrid
|
||||
from warehouse_twin.domain.warehouse import (
|
||||
NodeType,
|
||||
PickingPoint,
|
||||
Position3D,
|
||||
StorageLocation,
|
||||
WarehouseDimensions,
|
||||
WarehouseModel,
|
||||
)
|
||||
from warehouse_twin.routing.graph_builder import WarehouseGraphBuilder
|
||||
from warehouse_twin.routing.path_planner import PathPlanner
|
||||
from warehouse_twin.simulation.dispatcher import FleetDispatcher
|
||||
from warehouse_twin.simulation.sim_engine import SimulationEnvironment
|
||||
from warehouse_twin.domain.robot import Robot, RobotState, RobotTypeSpec
|
||||
from warehouse_twin.domain.task import Task, TaskType
|
||||
|
||||
|
||||
def test_picking_point_calculation_all_orientations():
|
||||
"""Verify front-facing picking point coordinates and yaw across all 4 rack orientations."""
|
||||
center = Position3D(10.0, 20.0, 0.0)
|
||||
dims = (2.8, 1.2, 4.5) # length (X), depth (Y), height (Z)
|
||||
clearance = 0.8
|
||||
|
||||
# 1. Facing South (aisle is at negative Y)
|
||||
loc_south = StorageLocation(
|
||||
location_id="rack_south",
|
||||
zone="zone_b",
|
||||
position=center,
|
||||
rack_dimensions=dims,
|
||||
facing_direction="south",
|
||||
approach_clearance=clearance,
|
||||
)
|
||||
pt_south = loc_south.get_picking_point()
|
||||
expected_y_south = 20.0 - (1.2 / 2.0 + clearance) # 20.0 - 1.4 = 18.6
|
||||
assert pt_south.position.x == pytest.approx(10.0)
|
||||
assert pt_south.position.y == pytest.approx(expected_y_south)
|
||||
assert pt_south.approach_yaw_degrees == 90.0 # Robot at south faces North (+Y)
|
||||
assert pt_south.rack_id == "rack_south"
|
||||
assert pt_south.prim_path == "/World/Warehouse/PickingPoints/rack_south_Front"
|
||||
|
||||
# 2. Facing North (aisle is at positive Y)
|
||||
loc_north = StorageLocation(
|
||||
location_id="rack_north",
|
||||
zone="zone_b",
|
||||
position=center,
|
||||
rack_dimensions=dims,
|
||||
facing_direction="north",
|
||||
approach_clearance=clearance,
|
||||
)
|
||||
pt_north = loc_north.get_picking_point()
|
||||
expected_y_north = 20.0 + (1.2 / 2.0 + clearance) # 20.0 + 1.4 = 21.4
|
||||
assert pt_north.position.x == pytest.approx(10.0)
|
||||
assert pt_north.position.y == pytest.approx(expected_y_north)
|
||||
assert pt_north.approach_yaw_degrees == -90.0 # Robot at north faces South (-Y)
|
||||
|
||||
# 3. Facing West (aisle is at negative X)
|
||||
loc_west = StorageLocation(
|
||||
location_id="rack_west",
|
||||
zone="zone_b",
|
||||
position=center,
|
||||
rack_dimensions=dims,
|
||||
facing_direction="west",
|
||||
approach_clearance=clearance,
|
||||
)
|
||||
pt_west = loc_west.get_picking_point()
|
||||
expected_x_west = 10.0 - (2.8 / 2.0 + clearance) # 10.0 - 2.2 = 7.8
|
||||
assert pt_west.position.x == pytest.approx(expected_x_west)
|
||||
assert pt_west.position.y == pytest.approx(20.0)
|
||||
assert pt_west.approach_yaw_degrees == 0.0 # Robot at west faces East (+X)
|
||||
|
||||
# 4. Facing East (aisle is at positive X)
|
||||
loc_east = StorageLocation(
|
||||
location_id="rack_east",
|
||||
zone="zone_b",
|
||||
position=center,
|
||||
rack_dimensions=dims,
|
||||
facing_direction="east",
|
||||
approach_clearance=clearance,
|
||||
)
|
||||
pt_east = loc_east.get_picking_point()
|
||||
expected_x_east = 10.0 + (2.8 / 2.0 + clearance) # 10.0 + 2.2 = 12.2
|
||||
assert pt_east.position.x == pytest.approx(expected_x_east)
|
||||
assert pt_east.position.y == pytest.approx(20.0)
|
||||
assert pt_east.approach_yaw_degrees == 180.0 # Robot at east faces West (-X)
|
||||
|
||||
|
||||
from warehouse_twin.domain.events import SimulationEventType
|
||||
|
||||
|
||||
def test_picking_point_outside_rack_footprint():
|
||||
"""Ensure picking points have safe clearance distance strictly outside physical rack footprint."""
|
||||
loc = StorageLocation(
|
||||
location_id="rack_test",
|
||||
zone="zone_a",
|
||||
position=Position3D(15.0, 25.0, 0.0),
|
||||
rack_dimensions=(1.9, 0.8, 3.2),
|
||||
facing_direction="south",
|
||||
approach_clearance=0.8,
|
||||
)
|
||||
pt = loc.get_picking_point()
|
||||
half_depth = 0.8 / 2.0
|
||||
dist = loc.position.distance_to(pt.position)
|
||||
assert dist == pytest.approx(half_depth + 0.8)
|
||||
assert pt.position.y < (loc.position.y - half_depth)
|
||||
|
||||
|
||||
def test_warehouse_grid_coordinate_mapping_and_marking():
|
||||
"""Test WarehouseGrid conversion between continuous world coords and discrete cell matrix."""
|
||||
cfg = GridConfig(
|
||||
cell_size=0.5,
|
||||
warehouse_width=30.0,
|
||||
warehouse_depth=20.0,
|
||||
)
|
||||
grid = WarehouseGrid(cfg)
|
||||
assert grid.cols == 60
|
||||
assert grid.rows == 40
|
||||
|
||||
# Test coordinate mapping
|
||||
r, c = grid.world_to_cell(1.2, 2.7)
|
||||
assert c == 2 # 1.2 / 0.5 = 2.4 -> 2
|
||||
assert r == 5 # 2.7 / 0.5 = 5.4 -> 5
|
||||
|
||||
wx, wy = grid.cell_to_world(5, 2)
|
||||
assert wx == pytest.approx(1.25)
|
||||
assert wy == pytest.approx(2.75)
|
||||
|
||||
# Test marking rack footprint as non-traversable
|
||||
grid.mark_rect(8.0, 10.8, 10.0, 11.2, CellType.RACK_FOOTPRINT, occupant_id="rack_1")
|
||||
cell_inside = grid.get_cell_at_world(9.0, 10.5)
|
||||
assert cell_inside is not None
|
||||
assert cell_inside.cell_type == CellType.RACK_FOOTPRINT
|
||||
assert cell_inside.is_traversable is False
|
||||
assert grid.is_traversable(9.0, 10.5) is False
|
||||
|
||||
# Aisle cell is traversable
|
||||
grid.mark_cell(10, 10, CellType.AISLE)
|
||||
cell_aisle = grid.get_cell(10, 10)
|
||||
assert cell_aisle is not None
|
||||
assert cell_aisle.is_traversable is True
|
||||
|
||||
|
||||
def test_graph_builder_instantiates_picking_points(sample_warehouse_config):
|
||||
"""Verify WarehouseGraphBuilder adds PICKING_POINT nodes connected to traversable aisles."""
|
||||
builder = WarehouseGraphBuilder.from_config_dict(sample_warehouse_config)
|
||||
graph = builder.build_graph()
|
||||
|
||||
for loc_id in builder.model.storage_locations:
|
||||
pick_node = f"pick_{loc_id}"
|
||||
assert graph.has_node(pick_node), f"Graph missing picking point {pick_node}"
|
||||
data = graph.nodes[pick_node]
|
||||
assert data["node_type"] == NodeType.PICKING_POINT.value
|
||||
assert "approach_yaw" in data
|
||||
assert "prim_path" in data
|
||||
assert graph.degree(pick_node) >= 2 # Connected to aisle and rack
|
||||
|
||||
|
||||
def test_dispatcher_resolves_rack_to_front_picking_point(sample_warehouse_config):
|
||||
"""Verify FleetDispatcher resolves rack target IDs to the front-facing picking waypoint."""
|
||||
builder = WarehouseGraphBuilder.from_config_dict(sample_warehouse_config)
|
||||
graph = builder.build_graph()
|
||||
dispatcher = FleetDispatcher(graph=graph)
|
||||
|
||||
# When querying rack_a_01, dispatcher must return pick_rack_a_01 in the aisle
|
||||
target_node = dispatcher.get_node_for_entity("rack_a_01")
|
||||
assert target_node == "pick_rack_a_01"
|
||||
|
||||
|
||||
def test_robot_navigates_to_picking_point_not_rack_center(sim_env_builder):
|
||||
"""Simulate a picking mission and assert robot stops at picking point outside rack footprint."""
|
||||
env = sim_env_builder(seed=42)
|
||||
env.run(until=120.0)
|
||||
|
||||
# Inspect ROBOT_ARRIVED events for rack picks
|
||||
arrival_events = [
|
||||
e for e in env.events
|
||||
if e.event_type == SimulationEventType.ROBOT_ARRIVED and e.metadata.get("station", "").startswith("rack_")
|
||||
]
|
||||
assert len(arrival_events) > 0
|
||||
|
||||
for ev in arrival_events:
|
||||
rack_id = ev.metadata["station"]
|
||||
picking_node = ev.metadata.get("picking_point")
|
||||
assert picking_node is not None
|
||||
assert picking_node.startswith("pick_rack_")
|
||||
|
||||
# Position of robot at pick must match picking point, NOT solid rack center
|
||||
loc = env.warehouse.storage_locations[rack_id]
|
||||
pick_pt = loc.get_picking_point()
|
||||
node_pos = env.path_planner.get_node_position(picking_node)
|
||||
|
||||
assert node_pos.x == pytest.approx(pick_pt.position.x, abs=0.1)
|
||||
assert node_pos.y == pytest.approx(pick_pt.position.y, abs=0.1)
|
||||
|
||||
# Distance from solid rack center must be >= 0.8m
|
||||
dist_to_center = loc.position.distance_to(node_pos)
|
||||
assert dist_to_center >= 0.8, f"Robot stopped too close ({dist_to_center}m) to rack center {rack_id}"
|
||||
Reference in New Issue
Block a user