STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_robust_ddp.cpp
Go to the documentation of this file.
1#include <chrono>
2#include <iomanip>
3#include <iostream>
4
6
7using namespace fsw::control;
8
9int main() {
10 std::cout << "=== Robust DDP Controller Test ===" << std::endl;
11
12 // ── Configure ──────────────────────────────────────────────────────
14 config.N = 20;
15 config.dt = 0.01;
16 config.qF = 1.0;
17 config.qMR = 10.0;
18 config.qGas = 0.1;
19 config.qSwitch = 0.01;
20 config.MR_min = 1.5;
21 config.MR_max = 3.0;
22 config.P_u_max = 10e6;
23 config.P_copv_min = 1e6;
24 config.injector_dp_frac = 0.1;
25 config.V_copv = 0.006; // 6 L COPV
26 config.reg_ratio = 0.8;
27 config.rho_F = 800.0;
28 config.rho_O = 1140.0;
29 config.Cd_fuel = 0.7;
30 config.Cd_ox = 0.7;
31 config.A_inj_fuel = 1e-4;
32 config.A_inj_ox = 1e-4;
33 config.c_star = 1600.0;
34 config.Cf = 1.5;
35 config.A_throat = 1e-3;
36 config.V_u_F_init = 0.01;
37 config.V_u_O_init = 0.01;
38 config.T_gas_init = 293.0;
39 config.n_polytropic = 1.2;
40 config.max_iterations = 5;
41 config.altitude_target = -1.0; // cutoff disabled
42
43 RobustDDPController controller;
44 if (!controller.initialize(config)) {
45 std::cerr << "Failed to initialize controller" << std::endl;
46 return 1;
47 }
48
49 // ── Measurement ────────────────────────────────────────────────────
51 meas.P_copv = 20e6; // 20 MPa
52 meas.P_reg = 16e6; // 16 MPa
53 meas.P_u_fuel = 974.0 * 6894.76; // ~6.71 MPa
54 meas.P_u_ox = 1305.0 * 6894.76; // ~9.00 MPa
55 meas.P_d_fuel = 6.5e6;
56 meas.P_d_ox = 8.5e6;
57 meas.timestamp = std::chrono::steady_clock::now();
58
59 // ── Nav ────────────────────────────────────────────────────────────
61 nav.h = 0.0;
62 nav.vz = 0.0;
63 nav.theta = 0.0;
64 nav.mass = 25.0;
65
66 // ── Command ────────────────────────────────────────────────────────
69 cmd.thrust_desired = 5000.0; // 5 kN
70
71 // ── Run 10 steps ───────────────────────────────────────────────────
72 std::cout << std::fixed << std::setprecision(2);
73 std::cout
74 << "\nTick | duty_F | duty_O | F_est [N] | MR_est | P_ch [MPa] | safe_filt | cutoff\n";
75 std::cout << std::string(90, '-') << "\n";
76
77 for (int i = 0; i < 10; ++i) {
78 auto [act, diag] = controller.step(meas, nav, cmd);
79
80 std::cout << std::setw(4) << i << " | " << std::setw(6) << act.duty_F << " | "
81 << std::setw(6) << act.duty_O << " | " << std::setw(10) << diag.F_estimated
82 << " | " << std::setw(6) << diag.MR_estimated << " | " << std::setw(10)
83 << diag.P_ch / 1e6 << " | " << (diag.safety_filtered ? " YES " : " no ")
84 << " | " << (diag.cutoff_active ? "YES" : " no") << "\n";
85
86 // Simulate slight blowdown: COPV drops, ullage grows
87 meas.P_copv *= 0.995;
88 meas.P_u_fuel *= 0.998;
89 meas.P_u_ox *= 0.998;
90 meas.P_d_fuel = 0.97 * meas.P_u_fuel;
91 meas.P_d_ox = 0.97 * meas.P_u_ox;
92 nav.h += nav.vz * config.dt;
93 nav.vz += (diag.F_estimated / nav.mass - G0) * config.dt;
94 }
95
96 // ── Cutoff test ────────────────────────────────────────────────────
97 std::cout << "\n--- Cutoff test (altitude_target = 100 m) ---\n";
98 controller.reset();
99 config.altitude_target = 100.0;
100 config.cutoff_delta = 20.0;
101 controller.initialize(config);
102
103 nav.h = 80.0;
104 nav.vz = 25.0; // apogee ≈ 80 + 25²/19.6 ≈ 112 m → cutoff
105 auto [act_cut, diag_cut] = controller.step(meas, nav, cmd);
106 std::cout << " h=80, vz=25 → cutoff=" << (diag_cut.cutoff_active ? "YES" : "NO") << "\n";
107
108 nav.h = 10.0;
109 nav.vz = 5.0; // apogee ≈ 10 + 25/19.6 ≈ 11.3 m → NO cutoff
110 auto [act_nocut, diag_nocut] = controller.step(meas, nav, cmd);
111 std::cout << " h=10, vz=5 → cutoff=" << (diag_nocut.cutoff_active ? "YES" : "NO") << "\n";
112
113 std::cout << "\n✅ Robust DDP Controller test completed successfully\n";
114 return 0;
115}
Robust Hybrid Optimal Controller.
bool initialize(const Config &config)
std::pair< ActuationCommand, Diagnostics > step(const Measurement &meas, const NavState &nav, const Command &cmd)
Execute one control step.
static const idle_config_t config
Definition idle_task.c:17
constexpr double G0
std::chrono::steady_clock::time_point timestamp
int main()