$npx -y skills add matlab/matlab-agentic-toolkit --skill roadrunner-scenario-simulatingExpert guidance for simulating RoadRunner scenarios via the MATLAB programmatic API and Simulink co-simulation. Use when the user wants to run a simulation, step through a simulation, control actors during co-simulation, add observers, attach sensors, retrieve simulation logs, or
| 1 | # RoadRunner Scenario Simulation |
| 2 | |
| 3 | Simulate RoadRunner scenarios, step through simulations, control actors in co-simulation, add observers, attach sensors, and retrieve results — from MATLAB and Simulink. |
| 4 | |
| 5 | ## When to Use |
| 6 | |
| 7 | - User wants to run a RoadRunner scenario simulation |
| 8 | - User wants to step through a simulation frame-by-frame |
| 9 | - User wants to control an actor externally (co-simulation) |
| 10 | - User wants to observe simulation state (read-only monitoring) |
| 11 | - User wants to add sensors and read target poses or lane boundaries |
| 12 | - User wants to retrieve simulation logs programmatically |
| 13 | - User wants to read or write scenario variables |
| 14 | - User asks about co-simulation with Simulink |
| 15 | - User asks about publishing actor behaviors |
| 16 | |
| 17 | ## When NOT to Use |
| 18 | |
| 19 | - Launching or connecting to RoadRunner |
| 20 | - Authoring scenarios (adding actors, paths, behaviors in the editor) |
| 21 | - Building or editing scenes (roads, terrain, assets) |
| 22 | - Exporting actor trajectories to CSV — use `exportActorTrajectoryToCSV` directly |
| 23 | - Working with `drivingScenario` (that is a DIFFERENT toolbox — Automated Driving Toolbox) |
| 24 | |
| 25 | ## Critical: Do NOT Confuse With drivingScenario |
| 26 | |
| 27 | `drivingScenario` (from Automated Driving Toolbox) is a MATLAB-native scenario tool. |
| 28 | RoadRunner Scenario simulation uses completely different APIs on the `roadrunner` object. |
| 29 | **Never mix these two — they are unrelated.** |
| 30 | |
| 31 | --- |
| 32 | |
| 33 | ## Decision Tree |
| 34 | |
| 35 | ``` |
| 36 | User wants to simulate → |
| 37 | ├── Just run to completion? → Workflow A (simulateScenario) |
| 38 | ├── Need step control OR programmatic log? → Workflow B (createSimulation) |
| 39 | ├── Need to read actor state during sim? → Workflow C (getAttribute) |
| 40 | ├── Need external actor control? → |
| 41 | │ ├── From MATLAB? → Workflow D (System object co-sim) |
| 42 | │ └── From Simulink? → Workflow E (Simulink blocks) |
| 43 | ├── Need read-only monitoring? → Workflow F (Observers) |
| 44 | └── Need sensor data during sim? → Workflow G (SensorSimulation) |
| 45 | ``` |
| 46 | |
| 47 | --- |
| 48 | |
| 49 | ## Workflow A: Simple Simulation (run to completion) |
| 50 | |
| 51 | Use when the user just wants to simulate an already-open scenario: |
| 52 | |
| 53 | ```matlab |
| 54 | openScenario(rrApp, "MyScenario"); |
| 55 | simulateScenario(rrApp, EnableLogging=true); |
| 56 | ``` |
| 57 | |
| 58 | Options: `Pacing`, `IsBlocking`, `IsSteppingStart`, `EnableLogging`. |
| 59 | |
| 60 | **Important:** `simulateScenario` does NOT return a log object. Use it when you only need to run to completion. If you need **programmatic access** to the simulation log in MATLAB, use `createSimulation` (Workflow B) instead. |
| 61 | |
| 62 | --- |
| 63 | |
| 64 | ## Workflow B: Step-by-Step Simulation |
| 65 | |
| 66 | Use when the user needs frame-by-frame control or programmatic log access. |
| 67 | |
| 68 | **CRITICAL call order:** `createSimulation` must be called BEFORE `simulateScenario`. The Scenario Server rejects new connections while a simulation is running or paused. Also, do NOT use `set(rrSim, SimulationCommand="Start")` then `"Step"` — `"Start"` runs the sim freely to completion. |
| 69 | |
| 70 | ```matlab |
| 71 | % 1. Get the simulation handle FIRST (before anything is running) |
| 72 | rrSim = createSimulation(rrApp); |
| 73 | stepSize = 0.01; |
| 74 | set(rrSim, StepSize=stepSize); |
| 75 | set(rrSim, MaxSimulationTime=30); |
| 76 | |
| 77 | % 2. THEN start simulation in stepping mode |
| 78 | simulateScenario(rrApp, IsSteppingStart=true, IsBlocking=false, EnableLogging=true); |
| 79 | pause(0.5); % Allow sim to initialize before stepping |
| 80 | |
| 81 | % 3. Step through the simulation |
| 82 | for i = 1:numSteps |
| 83 | set(rrSim, SimulationCommand="Step"); |
| 84 | pause(stepSize); % REQUIRED — Step is async, must wait for frame to complete |
| 85 | end |
| 86 | set(rrSim, SimulationCommand="Stop"); |
| 87 | |
| 88 | simLog = get(rrSim, "SimulationLog"); |
| 89 | ``` |
| 90 | |
| 91 | **CRITICAL:** `"Step"` is **asynchronous** — you MUST add `pause(stepSize)` after each Step. Without it, commands pile up and are silently dropped. Do NOT use dot-method syntax (`rrSim.step()`) — always use `set(rrSim, SimulationCommand=...)`. Do NOT set `Logging="On"` during stepping — use `EnableLogging=true` in the `simulateScenario` call. |
| 92 | |
| 93 | ### SimulationCommand values |
| 94 | |
| 95 | `"Start"`, `"Step"`, `"Pause"`, `"Continue"`, `"Stop"`, `"Replay"` |
| 96 | |
| 97 | Replay uses positional syntax: `set(rrSim, "SimulationCommand", "Replay", fileName)` |
| 98 | |
| 99 | ### Polling SimulationStatus |
| 100 | |
| 101 | `get(rrSim, "SimulationStatus")` returns: `"Inactive"`, `"Running"`, `"Paused"`, `"Done"` |
| 102 | |
| 103 | Use in wait loops when running non-blocking simulations. Check BOTH `"Done"` and `"Inactive"` — short scenarios may transition past `"D |