组装与配置
fr_quad 不维护自己的积分器,也不复制六自由度方程:它用 core 的声明 API 把 std 的标准块、自己的推进与气动块、以及一份实测标定表组装成一个可编译系统。这一章讲清八个块各自是谁、参数从哪儿来、编译期要定下哪些选择。
对应示例:models/fr_quad/examples/simulate.py、models/fr_quad/config.toml。
最小可运行
python
from fly_ruler_core import runtime
from pathlib import Path
from fly_ruler_core import ModelSettings, artifacts, compiler
from fr_quad import QuadSystem, load_config
root = Path(__file__).resolve().parent.parent
cfg = load_config(root / "config.toml")
program = compiler.prepare(
QuadSystem.build("Root", cfg), settings=ModelSettings(integrator="rk4", dt=0.01)
)
target_program = compiler.compile(program)
model = runtime.bind(target_program)QuadSystem.build("Root", cfg) 返回一个声明好的构建器,根系统名 Root 决定所有完整路径的前缀;模型准备、目标生成与运行绑定依次把它变成可执行函数。要跑通这段代码,环境里安装 fly-ruler 即可,CasADi 随基础依赖安装,标定表由 config.toml 提供。
八个块怎么接
| 块 | 来自 | 负责 |
|---|---|---|
MassProperties | 模型包(blocks/mass.py) | 按运行时不确定度缩放质量与惯量,输出 mass、inertia、inertia_inv |
RigidBody6DOFQuaternion | std | 六自由度刚体方程,参数打开 enable_external_mass=True、关闭大地坐标输出 |
QuadRotorMotor | 模型包(blocks/rotor.py) | 归一化指令 → PWM → 目标转速 → 一阶转速动态 |
QuadRotorAerodynamics | 模型包(blocks/rotor.py) | 推力表、X 型力臂力矩、二次阻力与偏航反扭矩 |
ConstantGravity | std | 常量重力,质量由 mass.mass 喂入 |
ExternalWind | 模型包(blocks/wind.py) | NED 风按姿态旋转到机体系 |
GroundContact | std | 四点多点地面接触,模型包给滑橇的默认配置 |
QuadSensor | 模型包(blocks/sensor.py) | 位置、速度、欧拉角、角速度、比力与转速拼成 19 通道向量 |
合力与力矩在系统层拼装:气动力与地面力相加,再加上姿态旋转后的重力,写回刚体的 force_body;力矩只含气动与地面两项。块名会进入完整路径,例如转子状态是 Root.rotor::motor_rpms,刚体位置是 Root.rb::pos_ned。
配置从哪来
QuadRotorConfig() 的字段默认值就是实测标定表,适合先跑通再用 TOML 覆盖;load_config(path) 读 models/fr_quad/config.toml 的 uav_model 与 fm_model 两节:
toml
[uav_model]
mass = 1.612868879
inertia = [0.00937366, -0.00011772, 0.00007083, -0.00011772, 0.01175506, -0.00011032, 0.00007083, -0.00011032, 0.01753355]
[fm_model]
pwm_to_rpm_breakpoints = [1301.0, 1347.0, 1402.0, 1459.0, 1522.0, 1591.0, 1661.0, 1702.0, 1778.0, 1932.0]
pwm_to_rpm_table = [2847.0, 3689.0, 4585.0, 5400.0, 6400.0, 7000.0, 7800.0, 8222.0, 9300.0, 10700.0]
rpm_to_thrust_breakpoints = [2847.0, 3689.0, 4585.0, 5400.0, 6400.0, 7000.0, 7800.0, 8222.0, 9300.0, 10700.0]
rpm_to_thrust_table = [0.6664, 1.4308, 2.3422, 3.3418, 4.9098, 5.929, 7.399, 7.6244, 10.976, 13.72]
motor_time_constant = 0.02
initial_rpm = 5271.2117152035735
arm_length = 0.205
motor_directions = [1.0, 1.0, -1.0, -1.0]惯量既可以写成扁平的 9 个数,也可以写成嵌套的 3×3;QuadRotorConfig 在构造时校验形状、对称性与正定性,并求逆得到 inertia_inv,非法输入直接抛 ValueError。地面、传感器噪声与不确定度是可选节,缺省时用源码默认值。
编译期定什么
ModelSettings 决定 integrator(示例用 "rk4",也可选 "euler")和 dt(示例用 0.01);OperationContext.store 与 cache 决定缓存位置和复用方式。后端在 compile(..., target=...) 上选:
python
from fly_ruler_core import runtime
from fly_ruler_core import ModelSettings, artifacts, compiler, targets
program = compiler.prepare(
QuadSystem.build("Root", cfg), settings=ModelSettings(integrator="rk4", dt=0.01)
)
target_program = compiler.compile(program, target=targets.casadi)
model = runtime.bind(target_program)默认目标是 CasADi;也可显式选择 targets.numpy、targets.casadi、targets.jax。JAX 默认使用 float32,FlyRuler 不会自动开启 x64;下面的 CPU+x64 配置用于对照高精度参考,需要 float64 时才显式选择:
python
from fly_ruler_core.runtime.jax_config import JaxRuntimePolicy, configure_jax_runtime
configure_jax_runtime(
JaxRuntimePolicy(platforms=("cpu",), preallocate=False, enable_x64=True)
)编译期配置(尺度、布局、标定表)改动后要重新编译;model.p 里的不确定度是运行时参数,改它们不用重编译。
边界与坑
| 现象 | 原因 |
|---|---|
构造配置时抛 ValueError | 惯量形状不是 9 个数或 3×3、不对称或不正定 |
| JAX 与其他后端的结果差异超出预期 | JAX 默认使用 float32;初值、输入、步长和求解配置也会影响比较。若参考值要求 float64,在首次导入 JAX 前显式启用 x64 |
| 改了 TOML 但结果没变 | 确认新配置已传入 System.build;声明内容变化应定位到新产物,必要时用 cache="refresh" 排查 |
| 指令给 1 却没到满推力 | 指令先仿射到 PWM 1000–2000、再裁剪到 [0, 2400],标定表两端之外的转速落在饱和区 |
| 想改旋翼数量或布局 | 旋翼顺序、arm_length 与 motor_directions 一起决定力矩与反扭矩符号,改动要带数值回归 |
这一章用到的 API
- quad 系统:
QuadSystem、build_initial_state - quad 配置:
QuadRotorConfig、load_config、UncertaintyConfig、SensorNoiseConfig - core 编译器:
compiler.prepare、compiler.compile、ModelSettings与配置字段 - core 后端与目标:
targets.numpy、targets.casadi、targets.jax - JAX 运行策略:
JaxRuntimePolicy、configure_jax_runtime