<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="7 8 3 12 7 16"/><polyline points="17 8 21 12 17 16"/><line x1="14" y1="5" x2="10" y2="19"/></svg>

Script

Advanced
Run the live example Free · no sign-up · runs in your browser

The Script block executes user-defined JavaScript or Python (via Pyodide) algorithms synchronously within each simulation step. It enables engineers to implement complex control laws, custom observers, nonlinear switching logic, state machines, and numerical routines that are awkward to express using graphical block diagrams.

Input and output ports are configured dynamically through the block interface, appearing as named variables within the script environment alongside simulation time tt, time step Δt\Delta t, and optional hardware communication interfaces.

Mathematical Model

y(t)=fscript(u(t),t,Δt)\mathbf{y}(t) = \mathbf{f}_{\text{script}}(\mathbf{u}(t), t, \Delta t)

Where:

  • u(t)∈Rm\mathbf{u}(t) \in \mathbb{R}^m represents the set of named input variables passed into the script context.
  • y(t)∈Rp\mathbf{y}(t) \in \mathbb{R}^p represents the calculated values assigned to output variables before the step completes.
  • tt and Δt\Delta t provide instantaneous simulation time and solver integration step size.

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

Example 1: Adaptive Gain Scheduling in JavaScript

A gain scheduler modifies feedback gain based on error magnitude to minimize settling time:

const error = setpoint - measurement;
const gain = Math.abs(error) > 2.0 ? 5.0 : 1.5;
controlSignal = gain * error;

Example 2: Matrix Riccati Equation in Python

A Python script leverages NumPy to compute optimal state feedback gains online:

import numpy as np
error = setpoint - measurement
u_opt = float(-1.25 * error)
controlSignal = u_opt

Remarks & Best Practices

  • Language performance: JavaScript executes natively with negligible per-step overhead; Python runs via Pyodide and requires an initial cold-start load time.
  • Hot-reloading: Modifying and saving code during an active simulation instantly hot-reloads the function inside the worker without restarting.
  • Port naming safety: Port names must be valid identifiers and must not conflict with runtime keywords (t, dt, propar, class, import).
  • Synchronous execution: The script evaluates synchronously inside the solver loop; computationally heavy routines may limit peak simulation rates.

Related Components

  • Plugin: Executes compiled WebAssembly modules for maximum execution speed and IP protection.
  • MathFunction: Performs standard elementary mathematical transformations on scalar inputs.
  • StateSpace: Implements linear multi-variable dynamic systems via state matrices.