Script
AdvancedExecute custom JavaScript or Python logic inside the simulation loop. Define your own input and output ports — they become live variables in your script.
🚦 When to use: for nonlinear control laws, observers, fuzzy logic, swing-up controllers, custom math. Stick to standard blocks (Sum, Integrator, Gain, etc.) for plant models and linear dynamics — they are faster and easier to debug visually.
Mathematical Model
Parameters
| Parameters | Label | Type | Default | Description |
|---|---|---|---|---|
language |
Language | select | javascript |
Scripting language. Switching language replaces the default starter code if the editor is untouched. |
script |
Script | code | // Available variables: t, dt, propar, and your custom input names
// Assign values to your custom output names
out = input * 2; |
Script executed every simulation step. Use the dedicated Script editor (double-click the block) for syntax highlighting and live console output. |
customInputs |
Input Ports | text | [{"id":"input","label":"Input","dataType":"number"}] |
JSON array of input ports. Each entry: { id, label, dataType }. Edit via the Interface tab in the script editor. |
customOutputs |
Output Ports | text | [{"id":"out","label":"Output","dataType":"number"}] |
JSON array of output ports. Each entry: { id, label, dataType }. Edit via the Interface tab in the script editor. |
scriptUrl |
External Script URL | text | |
Optional URL. Click 'Pull' in the editor to fetch and replace the local script — never auto-fetched. |
Usage Examples
- Zie de ingebouwde voorbeelden in de handleiding voor een demonstratie van dit blok.
Remarks & Best Practices
- JavaScript vs Python: JS runs natively (fastest); Python runs via Pyodide (slower, ~2–4 s cold start) — use JS for high-frequency loops
- Hot-reload: While the simulation is running, saving the script hot-reloads it in the worker — no restart needed
- Port collisions: Port names must not clash with runtime variables (t, dt, propar) or language keywords
- Hardware access: Use
propar.write()for real-time Bronkhorst MFC control; writes are queued to the main thread - Error handling: Runtime errors are rate-limited in the console; the block face turns red and displays the latest error message
Related Components
- Constant: Eenvoudige signaalbron