2024-07-12 19:13:29 +02:00
|
|
|
clc
|
|
|
|
clear all
|
|
|
|
close all
|
|
|
|
|
2024-07-27 14:50:46 +02:00
|
|
|
%TESTS = ["sin_faster", "sin", "circle", "straightline", "reverse_straightline"]
|
|
|
|
TESTS = ["straightline"]
|
2024-07-12 19:13:29 +02:00
|
|
|
|
2024-07-27 14:50:46 +02:00
|
|
|
s_ = size(TESTS);
|
2024-07-14 15:16:05 +02:00
|
|
|
|
2024-07-27 14:50:46 +02:00
|
|
|
for i = 1:s_(2)
|
|
|
|
clear data sim_data
|
|
|
|
close all
|
|
|
|
|
|
|
|
%for i = 1:1
|
|
|
|
TEST = convertStringsToChars(TESTS(i))
|
|
|
|
|
|
|
|
sim_data = load(['tests/' TEST '/common.mat']);
|
|
|
|
sim_data.q0 = set_initial_conditions(sim_data.INITIAL_CONDITIONS);
|
|
|
|
[ref dref] = set_trajectory(sim_data.TRAJECTORY);
|
|
|
|
sim_data.ref = ref;
|
|
|
|
sim_data.dref = dref;
|
|
|
|
|
|
|
|
spmd (3)
|
|
|
|
worker_index = spmdIndex;
|
|
|
|
data = load(['tests/' TEST '/' num2str(spmdIndex) '.mat']);
|
|
|
|
|
|
|
|
sim_data.PREDICTION_HORIZON = data.PREDICTION_HORIZON;
|
|
|
|
sim_data.U_corr_history = zeros(2,1,sim_data.PREDICTION_HORIZON);
|
|
|
|
sim_data
|
|
|
|
|
|
|
|
[t, q, ref_t, U, U_track, U_corr] = simulate_discr(sim_data);
|
|
|
|
|
|
|
|
disp('Done')
|
|
|
|
end
|
|
|
|
|
|
|
|
h = []
|
|
|
|
s1_ = size(worker_index);
|
|
|
|
for n = 1:s1_(2)
|
|
|
|
h_ = figure('Name', [TEST ' ' num2str(n)] );
|
|
|
|
h = [h, h_];
|
|
|
|
plot_results(t{n}, q{n}, ref_t{n}, U{n}, U_track{n}, U_corr{n});
|
|
|
|
end
|
2024-07-26 20:13:43 +02:00
|
|
|
|
2024-07-27 14:50:46 +02:00
|
|
|
f1 = [ TEST '-' datestr(datetime)];
|
|
|
|
f = ['results/' f1];
|
|
|
|
mkdir(f)
|
|
|
|
savefig(h, [f '/' f1 '.fig']);
|
|
|
|
save([f '/workspace.mat']);
|
2024-07-26 20:13:43 +02:00
|
|
|
|
2024-07-27 14:50:46 +02:00
|
|
|
copyfile(['tests/' TEST], f);
|
|
|
|
end
|
2024-07-26 20:13:43 +02:00
|
|
|
|
|
|
|
%%
|
2024-07-27 14:50:46 +02:00
|
|
|
|
|
|
|
% data = load(['tests/' TEST '/' num2str(2) '.mat']);
|
|
|
|
% sim_data.PREDICTION_HORIZON = data.PREDICTION_HORIZON;
|
|
|
|
% sim_data.U_corr_history = zeros(2,1,sim_data.PREDICTION_HORIZON);
|
|
|
|
% sim_data
|
|
|
|
% [t, q, ref_t, U, U_track, U_corr] = simulate_discr(sim_data);
|
|
|
|
%
|
|
|
|
% %%
|
|
|
|
% plot_results(t,q,ref_t,U, U_track, U_corr);
|
2024-07-16 10:58:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
%% FUNCTION DECLARATIONS
|
|
|
|
|
|
|
|
% Discrete-time simulation
|
2024-07-26 20:13:43 +02:00
|
|
|
function [t, q, ref_t, U, U_track, U_corr] = simulate_discr(sim_data)
|
2024-07-24 14:57:19 +02:00
|
|
|
tc = sim_data.tc;
|
|
|
|
steps = sim_data.tfin/tc
|
2024-07-14 15:16:05 +02:00
|
|
|
|
2024-07-24 14:57:19 +02:00
|
|
|
q = sim_data.q0';
|
2024-07-14 15:16:05 +02:00
|
|
|
t = 0;
|
2024-07-26 20:13:43 +02:00
|
|
|
[u_discr, u_track, u_corr, U_corr_history] = control_act(t, q, sim_data);
|
|
|
|
sim_data.U_corr_history = U_corr_history;
|
2024-07-14 15:16:05 +02:00
|
|
|
U = u_discr';
|
2024-07-26 20:13:43 +02:00
|
|
|
U_corr = u_corr';
|
|
|
|
U_track = u_track';
|
2024-07-14 15:16:05 +02:00
|
|
|
|
|
|
|
for n = 1:steps
|
|
|
|
tspan = [(n-1)*tc n*tc];
|
2024-07-23 18:07:50 +02:00
|
|
|
z0 = q(end, :);
|
2024-07-14 15:16:05 +02:00
|
|
|
|
2024-07-24 14:57:19 +02:00
|
|
|
%[v, z] = ode45(@sistema_discr, tspan, z0, u_discr);
|
|
|
|
[v, z] = ode45(@(v, z) sistema_discr(v, z, u_discr), tspan, z0);
|
2024-07-12 19:13:29 +02:00
|
|
|
|
2024-07-23 18:07:50 +02:00
|
|
|
q = [q; z];
|
2024-07-14 15:16:05 +02:00
|
|
|
t = [t; v];
|
|
|
|
|
2024-07-26 20:13:43 +02:00
|
|
|
[u_discr, u_track, u_corr, U_corr_history] = control_act(t(end), q(end, :), sim_data);
|
|
|
|
sim_data.U_corr_history = U_corr_history;
|
2024-07-14 15:16:05 +02:00
|
|
|
U = [U; ones(length(v), 1)*u_discr'];
|
2024-07-26 20:13:43 +02:00
|
|
|
U_corr = [U_corr; ones(length(v), 1)*u_corr'];
|
|
|
|
U_track = [U_track; ones(length(v), 1)*u_track'];
|
2024-07-14 15:16:05 +02:00
|
|
|
end
|
2024-07-12 19:13:29 +02:00
|
|
|
|
2024-07-24 14:57:19 +02:00
|
|
|
ref_t = double(subs(sim_data.ref, t'))';
|
2024-07-13 11:33:13 +02:00
|
|
|
end
|
2024-07-12 19:13:29 +02:00
|
|
|
|
2024-07-14 15:16:05 +02:00
|
|
|
|
2024-07-16 10:58:00 +02:00
|
|
|
% Continuos-time simulation
|
2024-07-23 18:07:50 +02:00
|
|
|
function [t, q, ref, U] = simulate_cont(tfin)
|
|
|
|
global ref q0
|
2024-07-14 15:16:05 +02:00
|
|
|
|
|
|
|
% simulation time
|
|
|
|
tspan = linspace(0, tfin);
|
|
|
|
% execute simulation
|
2024-07-23 18:07:50 +02:00
|
|
|
[t, q] = ode45(@sistema, tspan, q0);
|
2024-07-14 15:16:05 +02:00
|
|
|
|
|
|
|
% recalc and save input at each timestep
|
|
|
|
ts = size(t);
|
|
|
|
rows = ts(1);
|
|
|
|
U = zeros(rows, 2);
|
|
|
|
for row = 1:rows
|
2024-07-23 18:07:50 +02:00
|
|
|
U(row, :) = control_act(t(row), q(row, :));
|
2024-07-14 15:16:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
% plot results
|
|
|
|
ref = double(subs(ref, t'))';
|
|
|
|
end
|
2024-07-26 20:13:43 +02:00
|
|
|
%%
|
2024-07-14 15:16:05 +02:00
|
|
|
|
2024-07-16 10:58:00 +02:00
|
|
|
% Plots
|
2024-07-27 14:50:46 +02:00
|
|
|
function plot_results(t, x, ref, U, U_track, U_corr)
|
2024-07-26 20:13:43 +02:00
|
|
|
subplot(4,2,1)
|
2024-07-14 15:16:05 +02:00
|
|
|
hold on
|
2024-07-26 20:13:43 +02:00
|
|
|
title("trajectory / state")
|
2024-07-14 15:16:05 +02:00
|
|
|
plot(ref(:, 1), ref(:, 2), "DisplayName", "Ref")
|
|
|
|
plot(x(:, 1), x(:, 2), "DisplayName", "state")
|
|
|
|
xlabel('x')
|
|
|
|
ylabel('y')
|
|
|
|
legend()
|
2024-07-26 20:13:43 +02:00
|
|
|
subplot(4,2,3)
|
2024-07-14 15:16:05 +02:00
|
|
|
plot(t, U(:, 1))
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('input v')
|
2024-07-26 20:13:43 +02:00
|
|
|
subplot(4,2,4)
|
2024-07-14 15:16:05 +02:00
|
|
|
plot(t, U(:, 2))
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('input w')
|
2024-07-26 20:13:43 +02:00
|
|
|
|
|
|
|
subplot(4,2,5)
|
|
|
|
plot(t, U_corr(:, 1))
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('input correction v')
|
|
|
|
subplot(4,2,6)
|
|
|
|
plot(t, U_corr(:, 2))
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('input correction w')
|
|
|
|
|
|
|
|
|
|
|
|
subplot(4,2,7)
|
|
|
|
plot(t, U_track(:, 1))
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('tracking input v')
|
|
|
|
subplot(4,2,8)
|
|
|
|
plot(t, U_track(:, 2))
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('tracking input w')
|
2024-07-14 15:16:05 +02:00
|
|
|
|
|
|
|
|
2024-07-26 20:13:43 +02:00
|
|
|
subplot(8,4,3)
|
2024-07-14 15:16:05 +02:00
|
|
|
hold on
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('x')
|
|
|
|
plot(t, ref(:, 1), "DisplayName", "X_{ref}");
|
|
|
|
plot(t, x(:, 1), "DisplayName", "X");
|
|
|
|
legend()
|
|
|
|
hold off
|
|
|
|
|
2024-07-26 20:13:43 +02:00
|
|
|
subplot(8,4,4)
|
2024-07-14 15:16:05 +02:00
|
|
|
plot(t, ref(:, 1) - x(:, 1));
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('x error')
|
|
|
|
|
2024-07-26 20:13:43 +02:00
|
|
|
subplot(8,4,7)
|
2024-07-14 15:16:05 +02:00
|
|
|
hold on
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('y')
|
|
|
|
plot(t, ref(:, 2), "DisplayName", "Y_{ref}");
|
|
|
|
plot(t, x(:, 2), "DisplayName", "Y");
|
|
|
|
legend()
|
|
|
|
hold off
|
|
|
|
|
2024-07-26 20:13:43 +02:00
|
|
|
subplot(8,4,8)
|
2024-07-14 15:16:05 +02:00
|
|
|
plot(t, ref(:, 2) - x(:, 2));
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('y error')
|
2024-07-26 20:13:43 +02:00
|
|
|
|
2024-07-14 15:16:05 +02:00
|
|
|
end
|