Skip to content

Commit 5be3f26

Browse files
committed
FIx: State Management Issues
Make Visuals Smoother
1 parent 517088f commit 5be3f26

3 files changed

Lines changed: 677 additions & 72 deletions

File tree

main_complete_integration.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def broadcast_state(scenario_status):
279279
)
280280

281281
# Start automatic monitoring
282-
scenario_controller.start_auto_monitoring()
282+
# scenario_controller.start_auto_monitoring() # DISABLED: Controlled by simulation_loop now
283283

284284
# Add API endpoints
285285
integrate_scenario_controller(app, scenario_controller, load_model)
@@ -385,6 +385,10 @@ def simulation_loop():
385385
print(f"V2G State Update: {V2G_UPDATE}s (V2G session rate)")
386386
print("="*70 + "\n")
387387

388+
# BROADCAST OPTIMIZATION
389+
BROADCAST_INTERVAL = 5 # Send 1 update per 5 physics steps
390+
step_counter = 0
391+
388392
while system_state['running']:
389393
try:
390394
step_start = time_module.perf_counter()
@@ -411,6 +415,15 @@ def simulation_loop():
411415
sumo_time = (time_module.perf_counter() - sumo_start) * 1000
412416
perf_stats['sumo_step'].append(sumo_time)
413417

418+
# SOCKET BROADCAST: Frame Skipping Logic
419+
step_counter += 1
420+
if step_counter % BROADCAST_INTERVAL == 0:
421+
try:
422+
status = scenario_controller.get_system_status() if scenario_controller else {"status": "Running"}
423+
broadcast_state(status)
424+
except Exception as e:
425+
print(f"Broadcast loop error: {e}")
426+
414427
# ASYNC VEHICLE SPAWNING: Process spawn queue in batches (max 5 per tick)
415428
# This prevents UI freezing when bulk spawning vehicles
416429
global vehicle_spawn_queue

manhattan_sumo_manager.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,9 @@ def spawn_vehicles(self, count: int = 10, ev_percentage: float = 0.3, battery_mi
570570
else:
571571
continue
572572

573-
# Try to find route
574-
route_result = traci.simulation.findRoute(origin, destination)
573+
# CRITICAL FIX: Use findRoute with vType to validate departure edge permissions
574+
# This checks if the vehicle TYPE can actually depart from the origin edge
575+
route_result = traci.simulation.findRoute(origin, destination, vType=vtype)
575576

576577
if route_result and route_result.edges and len(route_result.edges) > 0:
577578
# Valid route found!
@@ -580,13 +581,20 @@ def spawn_vehicles(self, count: int = 10, ev_percentage: float = 0.3, battery_mi
580581
# Add route
581582
traci.route.add(route_id, route_result.edges)
582583

583-
# Add vehicle
584-
traci.vehicle.add(
585-
vehicle_id,
586-
route_id,
587-
typeID=vtype,
588-
depart="now"
589-
)
584+
# Add vehicle with explicit departure checking
585+
try:
586+
traci.vehicle.add(
587+
vehicle_id,
588+
route_id,
589+
typeID=vtype,
590+
depart="now"
591+
)
592+
except Exception as e:
593+
# If departure fails, remove the edge from valid_edges
594+
if origin in valid_edges:
595+
valid_edges.remove(origin)
596+
print(f" Departure failed on edge {origin}: {str(e)}, removed from valid edges")
597+
continue
590598

591599
# Set REALISTIC Manhattan speeds and COLLISION PREVENTION
592600
traci.vehicle.setMaxSpeed(vehicle_id, 13.9) # 50 km/h (31 mph) - realistic city speed

0 commit comments

Comments
 (0)