<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
运行在线示例 免费 · 无需注册 · 直接在浏览器中运行

Script 模块在每个仿真步长内同步执行用户自定义的 JavaScript 或 Python(基于 Pyodide)算法代码。工程师可以借此实现复杂的现代控制律、状态观测器、非线性切换逻辑、有限状态机以及难以用传统图形连线表达的高阶数值计算例程。

输入和输出端口可通过模块属性动态添加与配置,并在脚本环境中作为具名变量直接访问,同时环境还提供了瞬时仿真时间 tt、求解器积分步长 Δt\Delta t 以及可选的硬件通信接口。

数学模型

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

其中:

  • u(t)∈Rm\mathbf{u}(t) \in \mathbb{R}^m 表示传入脚本执行环境的一组具名输入变量。
  • y(t)∈Rp\mathbf{y}(t) \in \mathbb{R}^p 表示在当前步长结束前脚本赋值输出的目标计算值。
  • tt 与 Δt\Delta t 分别提供瞬时仿真时间与求解器数值积分步长。

参数

参数 Label 类型 默认值 描述
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.

使用示例

示例 1: JavaScript 中的自适应增益调度

自适应增益调度程序根据控制误差大小实时动态调节反馈增益,以加速系统响应并消除超调:

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

示例 2: Python 中的状态反馈矩阵求解

Python 脚本借助 NumPy 库实时计算最优状态反馈控制量:

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

相关模块