feat(phase-5): implement battery model, charging queues, production deadlines, and safe reassignment
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
"""Unit tests for BatteryModel: power consumption, load scaling, and mission energy feasibility."""
|
||||
|
||||
import pytest
|
||||
|
||||
from warehouse_twin.domain.robot import Robot, RobotTypeSpec
|
||||
from warehouse_twin.domain.warehouse import Position3D
|
||||
from warehouse_twin.fleet.battery_model import (
|
||||
BatteryModel,
|
||||
InfeasibleEnergyMissionError,
|
||||
MissionEnergyEstimate,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def robot_specs():
|
||||
return {
|
||||
"light_amr": RobotTypeSpec(
|
||||
type_id="light_amr",
|
||||
name="Light AMR",
|
||||
description="Small parts AMR",
|
||||
payload_capacity_kg=150.0,
|
||||
max_speed_mps=1.8,
|
||||
acceleration_mps2=0.8,
|
||||
battery_capacity_kwh=3.0,
|
||||
power_consumption_kw={
|
||||
"idle": 0.05,
|
||||
"moving_empty": 0.35,
|
||||
"moving_loaded": 0.65,
|
||||
"handling": 0.15,
|
||||
},
|
||||
allowed_zones={"zone_a", "zone_b", "zone_c"},
|
||||
dimensions=(0.8, 0.6, 0.35),
|
||||
),
|
||||
"heavy_amr": RobotTypeSpec(
|
||||
type_id="heavy_amr",
|
||||
name="Heavy AMR",
|
||||
description="Heavy pallet AMR",
|
||||
payload_capacity_kg=1500.0,
|
||||
max_speed_mps=1.1,
|
||||
acceleration_mps2=0.4,
|
||||
battery_capacity_kwh=15.0,
|
||||
power_consumption_kw={
|
||||
"idle": 0.12,
|
||||
"moving_empty": 0.90,
|
||||
"moving_loaded": 2.20,
|
||||
"handling": 0.50,
|
||||
},
|
||||
allowed_zones={"zone_a", "zone_b", "zone_c"},
|
||||
dimensions=(1.4, 1.0, 0.45),
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def light_robot(robot_specs):
|
||||
spec = robot_specs["light_amr"]
|
||||
return Robot(
|
||||
robot_id="amr_light_01",
|
||||
robot_type="light_amr",
|
||||
position=Position3D(2.0, 36.0, 0.0),
|
||||
maximum_speed=spec.max_speed_mps,
|
||||
payload_capacity=spec.payload_capacity_kg,
|
||||
battery_level=3.0, # 100%
|
||||
battery_capacity=spec.battery_capacity_kwh,
|
||||
energy_consumption=dict(spec.power_consumption_kw),
|
||||
allowed_zones=set(spec.allowed_zones),
|
||||
)
|
||||
|
||||
|
||||
def test_power_consumption_by_mode(light_robot):
|
||||
model = BatteryModel()
|
||||
assert model.calculate_power_kw(light_robot, mode="idle") == 0.05
|
||||
assert model.calculate_power_kw(light_robot, mode="handling") == 0.15
|
||||
assert model.calculate_power_kw(light_robot, mode="moving_empty") == 0.35
|
||||
|
||||
|
||||
def test_load_dependent_power_scaling(light_robot):
|
||||
model = BatteryModel()
|
||||
# At 0 kg payload, moving_loaded should equal moving_empty (0.35 kW)
|
||||
p_zero = model.calculate_power_kw(light_robot, mode="moving_loaded", payload_kg=0.0)
|
||||
assert p_zero == 0.65 # Default if 0 mass specified in loaded mode
|
||||
|
||||
# At half payload (75 kg of 150 kg capacity): 0.35 + (0.65 - 0.35) * 0.5 = 0.50 kW
|
||||
p_half = model.calculate_power_kw(light_robot, mode="moving_loaded", payload_kg=75.0)
|
||||
assert pytest.approx(p_half, rel=1e-3) == 0.50
|
||||
|
||||
# At full payload (150 kg of 150 kg capacity): 0.65 kW
|
||||
p_full = model.calculate_power_kw(light_robot, mode="moving_loaded", payload_kg=150.0)
|
||||
assert pytest.approx(p_full, rel=1e-3) == 0.65
|
||||
|
||||
|
||||
def test_transit_and_handling_energy_estimation(light_robot):
|
||||
model = BatteryModel()
|
||||
# 180 meters at 1.8 m/s = 100 seconds. Empty power = 0.35 kW -> 0.35 * 100 / 3600 = 0.009722 kWh
|
||||
e_transit = model.estimate_transit_energy_kwh(light_robot, distance_meters=180.0, is_loaded=False)
|
||||
assert pytest.approx(e_transit, rel=1e-3) == (0.35 * 100.0) / 3600.0
|
||||
|
||||
# 60 seconds handling at 0.15 kW -> 0.15 * 60 / 3600 = 0.0025 kWh
|
||||
e_handling = model.estimate_handling_energy_kwh(light_robot, duration_seconds=60.0)
|
||||
assert pytest.approx(e_handling, rel=1e-3) == (0.15 * 60.0) / 3600.0
|
||||
|
||||
|
||||
def test_mission_feasibility_high_battery(light_robot):
|
||||
model = BatteryModel(reserve_threshold_percent=20.0)
|
||||
estimate = model.estimate_mission_energy(
|
||||
robot=light_robot,
|
||||
dist_to_pickup=20.0,
|
||||
dist_to_dropoff=40.0,
|
||||
dist_drop_to_charger=15.0,
|
||||
payload_kg=50.0,
|
||||
)
|
||||
assert estimate.is_feasible is True
|
||||
assert estimate.projected_post_mission_soc_pct > 95.0
|
||||
assert estimate.reserve_headroom_pct > 75.0
|
||||
|
||||
|
||||
def test_mission_feasibility_rejected_on_low_battery(light_robot):
|
||||
# Set battery down to 21% (0.63 kWh out of 3.0 kWh)
|
||||
light_robot.battery_level = 0.63
|
||||
model = BatteryModel(reserve_threshold_percent=20.0)
|
||||
|
||||
# Long mission requiring 0.15 kWh -> remaining 0.48 kWh (16% SoC < 20% reserve)
|
||||
estimate = model.estimate_mission_energy(
|
||||
robot=light_robot,
|
||||
dist_to_pickup=500.0,
|
||||
dist_to_dropoff=500.0,
|
||||
dist_drop_to_charger=100.0,
|
||||
payload_kg=150.0,
|
||||
)
|
||||
assert estimate.is_feasible is False
|
||||
assert estimate.projected_post_mission_soc_pct < 20.0
|
||||
|
||||
with pytest.raises(InfeasibleEnergyMissionError):
|
||||
model.validate_mission_feasibility(
|
||||
robot=light_robot,
|
||||
dist_to_pickup=500.0,
|
||||
dist_to_dropoff=500.0,
|
||||
dist_drop_to_charger=100.0,
|
||||
payload_kg=150.0,
|
||||
)
|
||||
|
||||
|
||||
def test_charge_time_calculation(light_robot):
|
||||
model = BatteryModel(charging_efficiency=0.95)
|
||||
# Recharging from 1.0 kWh to 2.5 kWh (delta 1.5 kWh) on a 25.0 kW charger
|
||||
# effective power = 25.0 * 0.95 = 23.75 kW
|
||||
# time = (1.5 / 23.75) * 3600 = 227.37 s
|
||||
t_seconds = model.compute_charging_duration_seconds(
|
||||
current_kwh=1.0,
|
||||
target_kwh=2.5,
|
||||
charger_power_kw=25.0,
|
||||
)
|
||||
assert pytest.approx(t_seconds, rel=1e-2) == (1.5 / (25.0 * 0.95)) * 3600.0
|
||||
Reference in New Issue
Block a user