BlockWerk 文档
<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

在仿真主循环内部直接执行自定义 JavaScript 或 Python 算法逻辑。支持自定义输入与输出端口 — 它们将在您的脚本中作为实时变量直接使用。

🚦 适用场景: 用于非线性控制律、状态观测器(Observer)、模糊控制、倒立摆起摆控制(Swing-up)或复杂自定义数学公式。对于线性动力学及受控对象物理模型,建议使用标准块(Sum、Integrator、Gain 等)— 计算速度更快且更易于在画布上直观调试。

运行时环境变量

每个仿真步长脚本都会接收到以下上下文:

变量名类型说明
tnumber当前仿真时间(秒)。
dtnumber本步长计算的采样时间间隔(秒)。
proparobject硬件通信桥接 — 参见下文的 _硬件控制_。
number您在 Interface(接口)选项卡中声明的每个输入端口对应的变量。

像普通变量一样为您的 输出端口名称 赋值 — 仿真引擎将在每步计算结束后读取它们。每步计算前输出端口重置为 0;任何非有限数值(如 NaN 或 Infinity)均会被强制置零。

示例 — JavaScript

// 输入: setpoint, measurement
// 输出: torque
const error = setpoint - measurement;
torque = 0.5 * error;

示例 — Python (Pyodide)

# 输入: setpoint, measurement
# 输出: torque
error = setpoint - measurement
torque = 0.5 * error
if t % 1 < dt:
    print(f"[ctrl] t={t:.2f}s | err={error:+.3f}")

环境已预置 numpy(别名 np)。若脚本中包含 import scipy 或 from scipy …,系统将自动按需加载 scipy。如需其他 Python 包,可在 async 异步块中使用 import micropip; await micropip.install("…") 安装。

端口命名规则

端口标识符必须是合法的 JavaScript / Python 变量名,且 不得 与以下名称冲突:

  • 注入的系统保留变量 t、dt、propar。
  • 编程语言保留关键字(for、while、class、def、import 等)。
  • 该模块上声明的其他端口 ID。

基于 ProPar 的硬件通信控制

若仿真系统需要与 Bronkhorst 质量流量计(或任意 ProPar 协议设备)通信:

JavaScript:

propar.write(/* node */ 1, /* procNr */ 1, /* paramNr */ 21, 50.0); // 设定值

Python:

propar.instrument(1).fSetpoint = 50.0
# 或
propar.write(1, 206, 50.0)   # 206 = DDE 编号

外部脚本 URL 安全说明

External Script URL 字段 绝不会自动拉取。您必须主动点击 Pull... 并在确认弹窗中确认后才会覆盖本地脚本。拉取的代码拥有仿真 Worker 的全部执行权限。请仅从受信任的源拉取代码。

热重载 (Hot-reload)

在仿真运行中或暂停状态下,保存脚本(Ctrl+S 或点击 Save 按钮)将立即在后台 Worker 中热重载编译后的函数 — 无需重置或重启仿真。

控制台与错误诊断

Console 选项卡实时输出 Worker 日志 — 包括 print()、console.log()、语法编译错误以及运行时异常。发生错误时,模块面板将变为红色并显示最新的异常信息。

在 BlockWerk 中仿真

浏览器即开即用的交互式仿真环境 — 无需安装,由 WebAssembly 强力驱动。

在 BlockWerk 中仿真 →

数学模型

y(t)=fscript(u(t),t)y(t) = f_{\text{script}}(u(t), 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.

使用示例

  • 非线性全状态反馈控制或扩展卡尔曼滤波(EKF)。
  • 请参阅手册中的内置示例以获取该模块的演示。

说明与最佳实践

  • JavaScript 对比 Python:JS 原生执行(计算性能最高);Python 通过 WebAssembly / Pyodide 运行(初次冷启动约需 2–4 秒)。
  • 热重载:支持在仿真运行时无缝更新算法代码。
  • 硬件集成:内置 ProPar API,可无缝融入硬件在环(HIL)控制。

相关模块