@ -60,6 +60,8 @@ This document is the consolidated source of truth for AI agents working on the F
## 🖥️ 4. GUI Dashboard Architecture
- **Backend:** Flask (`app.py`) running on port 5000.
- **Simulator Lifecycle:** Dashboard polls `/api/simulator/status` to detect if CARLA is Offline/Ready. Provides one-click initialization via the UI.
- **GPU Resource Control:** Incorporates "Idle Mode" (Synchronous Throttling) to drop CARLA GPU usage to ~0% when inactive or while Shenron processing is active.
- **Streaming:** Uses **Server-Sent Events (SSE)** to stream `stdout` to the browser.
- **Execution:** Spawns `run.bat` as a subprocess.
- **Crucial Rule:** Always set `PYTHONUNBUFFERED=1` to prevent log delays in the GUI console.
@ -75,6 +77,7 @@ This document is the consolidated source of truth for AI agents working on the F
### Common Pitfalls
- **Stale Physics:** Always `world.tick()` once after spawning the Ego but before starting scenario logic.
- **Graceful Shutdown:** Use the "Stop Simulation" button in the dashboard. It writes a `tmp/stop.flag` which the orchestrator checks to ensure all recorders and data converters (Shenron/MCAP) finalize correctly.
- **Progress Bar Clash:** Use `pbar.write()` for logs if a `tqdm` progress bar is active.
- **Asynchronous I/O:** Use `ThreadPoolExecutor` for disk writes (images) to prevent simulation frame drops.
@ -88,4 +91,4 @@ This document is the consolidated source of truth for AI agents working on the F
---
*Generated by Antigravity AI | Last Updated: 2026-04-01 | Refer to .cursorrules for start-of-turn instructions.*
*Generated by Antigravity AI | Last Updated: 2026-04-10 | Refer to .cursorrules for start-of-turn instructions.*
@ -62,9 +62,23 @@ When the `/api/run` endpoint is hit:
---
## 4. Extension Guidelines for AI
When asked to extend the Dashboard, observe the following patterns:
- **Adding new CLI flags:** Update the `POST /api/run` payload parser in `app.py`, extend the `cmd.extend()` builder, and add the corresponding HTML `<input>` in `index.html`.
- **Adding new data streams:** The SSE generator currently pipes raw text. To parse structured data (like active ego speed) for live graphs, consider formatting the intermediate Python prints as JSON and having `app.js` parse the `event.data` payload conditionally.
- **Pathing:** Remember that `app.py` lives in a sub-folder. Operations manipulating the CARLA simulation *must* be rooted up two levels using the `PROJECT_ROOT` pointer established in the file.
## 5. Simulator Lifecycle & GPU Control
The dashboard now incorporates active management of the `CarlaUE4.exe` process and its hardware utilization.
### A. Lifecycle Monitoring
- **Polling:** The backend uses `psutil` to detect if the simulator process is active.
- **Initialization:** If offline, a one-click "Initialize CarlaUE4" button allows the user to launch the simulator directly from the browser.
- **Ping Validation:** The dashboard "Ready" state is only achieved once the backend successfully pings the CARLA server (localhost:2000) and receives a valid version response.
### B. Graceful Stop Protocol (IPC)
Instead of killing the simulation process—which can lead to corrupted radar/lidar data—the "Stop Simulation" button triggers an Inter-Process Communication (IPC) signal:
1. Dashboard writes an empty flag file to `tmp/stop.flag`.
2. The orchestrator (`src/main.py`) checks for this file's existence at the start of every frame tick.
3. If detected, the orchestrator breaks the main loop, allowing all `finally` cleanups, recorder closures, and MCAP conversions to finish elegantly.
### C. GPU Resource Control (Idle Mode)
To prevent CARLA from consuming 100% GPU while waiting for user interaction or during Shenron radar post-processing:
- **Synchronous Throttling:** The dashboard can toggle CARLA into `synchronous_mode`.
- **Idle State:** Without a `world.tick()` call from the client, Unreal Engine pauses its rendering and physics loop, dropping GPU usage to ~0%.
- **Auto-Idle:** The dashboard incorporates a 30-second inactivity timer that automatically pauses the simulator to save power.