Files

78 lines
2.7 KiB
Matlab
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
function [s, rec] = shell_step(id, s, sc)
%SHELL_STEP 壳的**单拍**:边界感知 → 同一个内核 → 边界执行 →(壳内有对象时)对象推进。
%
% ┌── shell_sense ──▶ pc_autopilot_step(内核,三壳同一份)──▶ shell_act ──┐
% │ │ │
% └──────────── pc_model_step(仅 HIL / SIL:壳内的飞行动力学模型)◀───────┘
%
% ★ 三壳的差别只有两处:① shell_sense/shell_act 按壳选边界(对外接口不同);
% ② 壳内是否有对象(AP 壳没有:真实飞机就是对象)。内核一行都不变。
%
% 输入:id 壳 ID;s 壳状态(见 shell_run);sc 场景
% 输出:s 推进后的壳状态;rec 本拍记录(供 shell_run 记日志/单测断言)
%
% 用法:[s, rec] = shell_step('sil', s, sc);
r = shell_registry(id);
% ---- ① 边界感知(按壳)----
[u, b, sns] = shell_sense(id, s.b, s.x, sc, s.u);
% ---- ①b 上层注入:逐拍指令码(框架/地面站路径;真实系统里来自链路解码)----
if isfield(sc, 'cmds') && ~isempty(sc.cmds)
k = s.k + 1;
if k <= numel(sc.cmds)
u.req_cmd = sc.cmds(k);
else
u.req_cmd = 0;
end
end
% ---- ② 内核单拍(三壳同一份真源)----
y = pc_autopilot_step(u);
% ---- ③ 边界执行(按壳)----
[ac, b] = shell_act(id, b, y, sc);
% ---- ④ 壳内对象(飞行动力学模型):仅 HIL / SIL ----
wind = zeros(3, 1);
if r.has_plant
if r.has_env
% SIL 壳:环境模型(常值风 + Dryden 湍流 + 离散突风;真源 core/model/model_wind.m)
alt_agl = -s.x(13);
if alt_agl < 0
alt_agl = 0;
end
V_air = sqrt(s.x(8)^2 + s.x(9)^2 + s.x(10)^2);
[wind, s.windst] = model_wind(s.windst, u.dt, V_air, alt_agl, sc);
else
% HIL 壳:风由硬件/占位给(真实台架是风洞/大气数据,占位实现 = 场景常值风)
wind = sc.wind_ned;
end
% 对象输入用**归一化 pwm**(pc_act_s 帧就是这个量纲;AP/HIL 的 µs 只出现在对外接口)
s.x = pc_model_step(s.x, ac.pwm, sc.dist_b, wind, s.pb);
end
% ---- ⑤ 状态与记录 ----
s.b = b;
s.u = u;
s.k = s.k + 1;
s.t = s.t + u.dt;
rec = struct();
rec.pwm = ac.pwm;
rec.act_us = ac.act_us;
rec.unit = ac.unit;
rec.sns = sns;
rec.x = s.x;
rec.armed = y.armed;
rec.fs = y.fs;
rec.health = y.health_word;
rec.att_ok = y.att_ok;
rec.nav_stage = y.nav_stage;
rec.nav_wp = y.nav_wp;
rec.tx_int = y.comm_tx_int;
rec.tx_n = [y.comm_tx_n0, y.comm_tx_n1, y.comm_tx_n2];
rec.wind = wind;
end