$npx -y skills add matlab/matlab-agentic-toolkit --skill matlab-cosimulate-sumo-simulinkBuild Simulink models that co-simulate with Eclipse SUMO traffic simulator. Use when creating SUMO-Simulink co-simulation, traffic simulation, TraCI connection, vehicle-in-the-loop testing, or ADAS scenario validation with SUMO. Covers Server/Client setup, Reader/Writer/Actor blo
| 1 | # Build SUMO-Simulink Co-simulation |
| 2 | |
| 3 | Create Simulink models that run synchronized co-simulation with Eclipse SUMO using the `SumoInterfaceLibrary` blocks from the Automated Driving Toolbox Interface for Eclipse SUMO Traffic Simulator. |
| 4 | |
| 5 | ## When to Use |
| 6 | |
| 7 | - Building a Simulink model that connects to Eclipse SUMO |
| 8 | - Setting up traffic co-simulation for ADAS/AD controller testing |
| 9 | - Reading vehicle states (position, speed) from SUMO into Simulink |
| 10 | - Controlling an ego vehicle in SUMO from a Simulink controller |
| 11 | - Spawning or removing vehicles dynamically during simulation |
| 12 | - Generating random background traffic without writing route files |
| 13 | |
| 14 | ## When NOT to Use |
| 15 | |
| 16 | - Pure SUMO simulation without Simulink (use SUMO's CLI or Python TraCI directly) |
| 17 | - Vehicle dynamics modeling (use Vehicle Dynamics Blockset) |
| 18 | - Scenario design with Driving Scenario Designer (different workflow) |
| 19 | |
| 20 | ## Prerequisites |
| 21 | |
| 22 | 1. **Eclipse SUMO installed** with `SUMO_HOME` environment variable set |
| 23 | 2. **Support package installed**: "Automated Driving Toolbox Interface for Eclipse SUMO Traffic Simulator" |
| 24 | 3. **Windows: append trailing separator to `SUMO_HOME`** — The Server block's launcher concatenates `SUMO_HOME + "bin\..."` without a separator. If `SUMO_HOME` lacks a trailing `\` or `/`, every `sim()` fails with "Failed to launch Eclipse SUMO simulator" even though SUMO is installed correctly. Always run before building/simulating: |
| 25 | |
| 26 | ```matlab |
| 27 | if ~endsWith(getenv('SUMO_HOME'), filesep) |
| 28 | setenv('SUMO_HOME', [getenv('SUMO_HOME') filesep]); |
| 29 | end |
| 30 | ``` |
| 31 | |
| 32 | 4. **Verify**: `getenv('SUMO_HOME')` must return a valid path ending with a separator. |
| 33 | |
| 34 | ## Preferred Construction Path |
| 35 | |
| 36 | When `mcp__matlab__model_edit` (SATK) is available, prefer it over raw `add_block`/`add_line`. It applies auto-layout after every edit, produces clean wiring with no overlap, and lets you reference newly-added blocks by `ref` within the same call. The patterns below still apply — express them as `add_block`/`connect`/`configure` operations in a single JSON payload. Fall back to `add_block`/`add_line` only when SATK is unavailable or when the operation is not expressible in the schema (e.g., setting MATLAB Function `Script` via `sfroot`). |
| 37 | |
| 38 | **Note:** When wiring `EnablePort` on a freshly-created Enabled Subsystem whose default `In1`/`Out1` have been deleted, `model_edit` cannot resolve the Enable port. Workaround: `add_line(model, 'Source/1', 'SubsystemName/Enable', 'autorouting','smart')`. |
| 39 | |
| 40 | ## Workflow |
| 41 | |
| 42 | ### Idempotent Build Template |
| 43 | |
| 44 | ```matlab |
| 45 | modelName = 'sumo_cosim'; |
| 46 | workDir = fileparts(mfilename('fullpath')); |
| 47 | % SUMO_HOME workaround (Windows) |
| 48 | sh = getenv('SUMO_HOME'); |
| 49 | if ~endsWith(sh, filesep), setenv('SUMO_HOME', [sh filesep]); end |
| 50 | % Idempotent rebuild |
| 51 | if bdIsLoaded(modelName), close_system(modelName, 0); end |
| 52 | slxPath = fullfile(workDir, [modelName '.slx']); |
| 53 | if isfile(slxPath), delete(slxPath); end |
| 54 | new_system(modelName); |
| 55 | open_system(modelName); |
| 56 | set_param(modelName, 'StopTime','100', 'SolverType','Fixed-step', 'FixedStep','0.1'); |
| 57 | % ... build model ... |
| 58 | save_system(modelName, slxPath); |
| 59 | ``` |
| 60 | |
| 61 | ### 1. Prepare SUMO Scenario Files |
| 62 | |
| 63 | Ask the user for their existing `.sumocfg`. Only generate new files if explicitly requested. For quick networks use `netgenerate`: |
| 64 | |
| 65 | ```matlab |
| 66 | sumoHome = getenv('SUMO_HOME'); |
| 67 | cmd = sprintf('"%s" --grid --grid.number 2 --grid.length 200 --output-file "%s"', ... |
| 68 | fullfile(sumoHome,'bin','netgenerate'), fullfile(pwd,'network.net.xml')); |
| 69 | system(cmd); |
| 70 | ``` |
| 71 | |
| 72 | Minimal `.sumocfg` (route-files optional when using `EnableRandomTraffic`): |
| 73 | |
| 74 | ```xml |
| 75 | <configuration> |
| 76 | <input><net-file value="network.net.xml"/></input> |
| 77 | <time><begin value="0"/><end value="100"/><step-length value="0.1"/></time> |
| 78 | </configuration> |
| 79 | ``` |
| 80 | |
| 81 | **Route file rules:** All `<vehicle>`, `<person>`, and `<personFlow>` entries MUST be sorted by `depart` time (`begin` for flows). Out-of-order entries are silently ignored. Routes used by Actor blocks must be standalone `<route>` elements — not embedded in `<flow>`. |
| 82 | |
| 83 | **OpenDRIVE import:** Use `netconvert --opendrive-files file.xodr -o network.net.xml`. Note: SUMO applies a `netOffset` (visible in `.net.xml` `<location>` element) — all SUMO coordinates = OpenDRIVE coordinates + netOffset. |
| 84 | |
| 85 | ### 2. Create Server and Client |
| 86 | |
| 87 | ```matlab |
| 88 | add_block('SumoInterfaceLibrary/Server', [modelName '/SUMO Server']); |
| 89 | set_param([modelName '/SUMO Ser |