Skip to content

推进与记录 ​

单速率离线仿真直接调用 discrete_fn,不需要调度器。把返回的状态、反馈和诊断一起交给 Recorder,时间写积分端点。

最小可运行 ​

python
from fr_quad import read_observation

for step in range(300):
    x, xd, y, effects = model.discrete_fn(step * 0.01, x, xd, u, p)
observation = read_observation(model, y, clean=True)
print(observation.position_ned, observation.velocity_ned)

discrete_fn 返回 DiscreteResult(x_next, xd_next, y, effects),不修改入参。只求当前导数时使用 continuous_fn,返回 ContinuousResult(xdot, xd_next, y, effects)。四元数存储四维、导数切空间三维,因此本模型 n_x=17、n_xdot=16。

按路径读输出 ​

根只有 Root::clean_y 与 Root::y 两个 19 维物理向量,打包输出长度为 38。read_observation 默认读取加噪测量,clean=True 读取无噪声物理观测,并在主机边界转换成不可变 Observation。

通道含义单位
0:3NED 位置m
3:6NED 速度m/s
6:9roll/pitch/yawrad
9:12FRD 角速度rad/s
12:15FRD 比力,排除重力、包含接触力m/s²
15:19FR/RL/FL/RR 转速rpm

原始四元数、机体速度等从状态读取,诊断从 effects 读取:

python
from fr_quad import QuadPaths

paths = QuadPaths()
state = model.x.read(x, by="path")
diagnostics = model.effects.read(effects, by="path")
print(state[paths.x.quat])
print(diagnostics[paths.effects.ground_normal_forces])

气动载荷由 Root.aero::force_body、Root.aero::moment_body 拥有;根不重复声明。质量、惯量、风和接触诊断通过 paths.effects 定位。模型使用工具不缓存向量下标。

记录与回读 ​

py
recorder = Recorder(model, root / "results/quad_nominal")
for step in range(300):
    x, xd, y, effects = model.discrete_fn(step * 0.01, x, xd, u, p)
    recorder.record(
        step, (step + 1) * 0.01, x=x, xd=xd, y=y, u=u, p=p, effect=effects
    )
recorder.save("simulation")

位置的原始状态列从 x 读取,观测从 y 读取:

python
from fly_ruler_std.runtime.record import Loader

loader = Loader("results/quad_nominal", filename="simulation", model=model)
pos_ned = loader.get_x("Root.rb::pos_ned")
measurement = loader.get_y("Root::y")

本模型没有离散状态;带离散状态的父系统应遵守状态与采样时序,不要用新提交的历史状态重新解释端点输出。

六个示例脚本 ​

在 models/fr_quad 执行:

recipe演示
just example-simulate3 s 标称仿真与记录
just example-free-fall从 10 m 无动力下落 5 s
just example-disturbances指令与风场景各推进 0.1 s
just example-trim电机部分配平与物理反馈线性化
just example-backendsNumPy、CasADi、可选 JAX 对照
just example-inspect模型布局与 Graph

just test-examples 运行全部离线脚本,并对 inspect 使用 --no-graph。

边界与坑 ​

现象处理
在 y 中找不到四元数或接触力四元数从 x、接触力从 effects 读取
记录列无法读取使用与记录相同布局的模型,并选择正确变量空间
希望保持 JAX 原生数组使用变量视图;read_observation 是显式主机转换

这一章用到的 API ​