I see that you have implemented a simple pendulum control system using Model Predictive Control (MPC) in MATLAB. To introduce feedback delay into your control system, you can use a state-space representation that accounts for feedback delay. Here's how you can modify your code to include feedback delay:
% previous code
% Feedback delay buffer
u_buffer = zeros(1, tu);
for i = 1:length(tt)
g = 2 * (Phi' * Q' * F * x0 - Phi' * Q' * Rp(i:Np + i - 1));
% Apply feedback delay
if (i - tu) <= 0
u = 0;
else
u = u_buffer(mod(i - tu - 1, tu) + 1); % Corrected line
end
% Store the current control input in the buffer
u_buffer = [u_buffer(2:end), u];
u1 = quadprog(H, g);
ustr(:, i) = u1;
y(i, 1) = Y;
x = Ad * x0 + Bd * u;
Y = Cd * x;
x0 = x;
end
% code afterwards
The line that needed modification is the one where we retrieve the delayed control input from the "u_buffer". I've added the "mod" function to handle cases where the delay index becomes negative. This ensures that the code correctly accesses the delayed control input from the buffer.
I hope this helps.