This commit is contained in:
Dane Sabo 2025-04-28 15:06:57 -04:00
parent 042d46b474
commit 59e659ba42
2 changed files with 55 additions and 15 deletions

View File

@ -33,7 +33,6 @@ sys_third_register = c2d(sys_cont, Ts_third_register, 'zoh');
max_time = 1e-2; max_time = 1e-2;
[ref, t, N] = make_hdd_reference(max_time, Ts_whole_register, 1e-2, 42); [ref, t, N] = make_hdd_reference(max_time, Ts_whole_register, 1e-2, 42);
[ref_third, t_third, N_third] = make_hdd_reference(9*Ts_third_register, Ts_third_register, 1e-2, 42);
%% Running a Simulation %% Running a Simulation
% ADC Delay, no sub-steps - Setting Up Simulation % ADC Delay, no sub-steps - Setting Up Simulation
@ -44,18 +43,20 @@ opts.N = N;
opts.K = [0.7+1e-4i 0.7-1e-4i]; opts.K = [0.7+1e-4i 0.7-1e-4i];
opts.L = [0.1+0.01i 0.1-0.01i]; opts.L = [0.1+0.01i 0.1-0.01i];
opts.r = ref; opts.r = ref;
opts.plotting = false; opts.plotting = true;
[x_hist, y_hist, u_hist, x_hat_hist] = solve_full_step(opts); [x_hist, y_hist, u_hist, x_hat_hist] = solve_full_step(opts);
% ADC delay, with sub-steps % ADC delay, with sub-steps
opts.K = [0.7+1e-4i 0.7-1e-4i]; [ref_third, t_third, N_third] = make_hdd_reference(12*Ts_third_register, Ts_third_register, 1e-2, 42);
opts.L = [0.4+0.01i 0.4-0.01i]; opts.K = [0.99+1e-4i 0.99-1e-4i];
opts.L = [0.2+0.01i 0.2-0.01i];
opts.debug= true;
opts.sys = sys_third_register; opts.sys = sys_third_register;
opts.sub_steps = 3; opts.sub_steps = 3;
opts.r = ref_third; opts.r = ref_third;
opts.N = N_third/3; opts.N = N_third/3;
opts.res = 12; opts.res = 48;
opts.plotting = true; opts.plotting = true;
[x_hist, y_hist, u_hist, x_hat_hist] = solve_sub_step(opts); [x_hist, y_hist, u_hist, x_hat_hist] = solve_sub_step(opts);

View File

@ -89,7 +89,6 @@ end
% ---------------- Simulation loop ------------------------------------ % ---------------- Simulation loop ------------------------------------
for k = (1:J:(N-1)*J)%N-1 for k = (1:J:(N-1)*J)%N-1
k
x_hist(:,k+1) = A*x_hist(:,k+0) + B*u_hist(k+0); x_hist(:,k+1) = A*x_hist(:,k+0) + B*u_hist(k+0);
y_hist(:,k+0) = C*x_hist(:,k+0); y_hist(:,k+0) = C*x_hist(:,k+0);
@ -103,11 +102,11 @@ for k = (1:J:(N-1)*J)%N-1
B*u_hist(k+0) + ... B*u_hist(k+0) + ...
L*( ADC_output(1) - C*x_hat_hist(:,k+0)); L*( ADC_output(1) - C*x_hat_hist(:,k+0));
u_hist(k+1) = K*(r(:,k+1)-x_hat_hist(:,k+1)); u_hist(k+2) = K*(r(:,k+1)-x_hat_hist(:,k+1));
%SECOND SUBSTEP %SECOND SUBSTEP
x_hist(:,k+2) = A*x_hist(:,k+1) + B*u_hist(k+1); x_hist(:,k+2) = A*x_hist(:,k+1) + B*u_hist(k+1);
y_hist(:,k+1) = C*x_hist(:,k+1); y_hist(:,k+2) = C*x_hist(:,k+1);
x_hat_hist(:,k+2) = A*(A*x_hat_hist(:,k+0) + ... x_hat_hist(:,k+2) = A*(A*x_hat_hist(:,k+0) + ...
B*u_hist(k+0) + ... B*u_hist(k+0) + ...
@ -119,7 +118,7 @@ for k = (1:J:(N-1)*J)%N-1
%THIRD SUBSTEP %THIRD SUBSTEP
x_hist(:,k+3) = A*x_hist(:,k+2) + B*u_hist(k+2); x_hist(:,k+3) = A*x_hist(:,k+2) + B*u_hist(k+2);
y_hist(:,k+2) = C*x_hist(:,k+2); y_hist(:,k+3) = C*x_hist(:,k+2);
x_hat_hist(:,k+3) = A*(A*(A*x_hat_hist(:,k+0) + ... x_hat_hist(:,k+3) = A*(A*(A*x_hat_hist(:,k+0) + ...
B*u_hist(k+0) + ... B*u_hist(k+0) + ...
@ -130,13 +129,47 @@ for k = (1:J:(N-1)*J)%N-1
B*u_hist(k+2); B*u_hist(k+2);
u_hist(k+3) = K*(r(:,k+3)-x_hat_hist(:,k+3)); u_hist(k+3) = K*(r(:,k+3)-x_hat_hist(:,k+3));
% DEBUG PRINTS (only if opts.debug)
if isfield(opts,'debug') && opts.debug
fprintf("\n=== Macrostep k = %d ===\n", k);
% Sensor / ADC
fprintf("-- Sensor + ADC --\n");
fprintf(" sensor_value = [%s] (y_hist(:,%d)*2^%d)\n", ...
num2str(sensor_value.', ' %.4f'), k, res);
[mADC,nADC] = size(ADC_output);
fprintf(" ADC_output = %d×%d matrix:\n", mADC, nADC);
for row = 1:mADC
fprintf(" row %d: [%s]\n", row, num2str(ADC_output(row,:), ' %.4f'));
end end
x_hist % Microstep 1 (@ k+1)
x_hat_hist fprintf("\n-- Microstep 1 (t = k+1) --\n");
y_hist fprintf(" r(:,%d) = [%s]\n", k+1, num2str(r(:,k+1).', ' %.4f'));
u_hist fprintf(" x_hist(:,%d) = [%s]\n", k+1, num2str(x_hist(:,k+1).', ' %.4f'));
fprintf(" x_hat_hist(:,%d) = [%s]\n", k+1, num2str(x_hat_hist(:,k+1).', ' %.4f'));
fprintf(" y_hist(:,%d) = [%s]\n", k+1, num2str(y_hist(:,k+1).', ' %.4f'));
fprintf(" u_hist(%d) = %.4f\n", k+1, u_hist(k+1));
% Microstep 2 (@ k+2)
fprintf("\n-- Microstep 2 (t = k+2) --\n");
fprintf(" r(:,%d) = [%s]\n", k+1, num2str(r(:,k+2).', ' %.4f'));
fprintf(" x_hist(:,%d) = [%s]\n", k+2, num2str(x_hist(:,k+2).', ' %.4f'));
fprintf(" x_hat_hist(:,%d) = [%s]\n", k+2, num2str(x_hat_hist(:,k+2).', ' %.4f'));
fprintf(" y_hist(:,%d) = [%s]\n", k+2, num2str(y_hist(:,k+2).', ' %.4f'));
fprintf(" u_hist(%d) = %.4f\n", k+2, u_hist(k+2));
% Microstep 3 (@ k+3)
fprintf("\n-- Microstep 3 (t = k+3) --\n");
fprintf(" r(:,%d) = [%s]\n", k+1, num2str(r(:,k+3).', ' %.4f'));
fprintf(" x_hist(:,%d) = [%s]\n", k+3, num2str(x_hist(:,k+3).', ' %.4f'));
fprintf(" x_hat_hist(:,%d) = [%s]\n", k+3, num2str(x_hat_hist(:,k+3).', ' %.4f'));
fprintf(" y_hist(:,%d) = [%s]\n", k+3, num2str(y_hist(:,k+3).', ' %.4f'));
fprintf(" u_hist(%d) = %.4f\n", k+3, u_hist(k+3));
end % if debugend
end
% ---------------- Plot results --------------------------------------- % ---------------- Plot results ---------------------------------------
@ -144,6 +177,7 @@ u_hist
if plotting if plotting
figure; figure;
time = (0:N*J)*Ts; time = (0:N*J)*Ts;
for i = 1:nx for i = 1:nx
subplot(nx+1,2,i); subplot(nx+1,2,i);
plot(time,x_hat_hist(i,:),'-xr', time,x_hist(i,:),'-ob'); plot(time,x_hat_hist(i,:),'-xr', time,x_hist(i,:),'-ob');
@ -152,8 +186,13 @@ if plotting
if i==nx, xlabel('Time (s)'); end if i==nx, xlabel('Time (s)'); end
legend('x_{hat}', 'x') legend('x_{hat}', 'x')
end end
subplot(nx+1,2,3); subplot(nx+1,2,3);
stairs(time(1:N), r(1,1:N), "Color", "#22FF22") hold on;
stairs(time(1:N), r(1,1:N), "Color", "#00FF80")
stairs(time(1:N), r(2,1:N), "Color", "#88aa00")
hold off;
ylabel('Position Demanded'); ylabel('Position Demanded');
xlabel('Time (s)'); xlabel('Time (s)');