2024-07-12 19:13:29 +02:00
|
|
|
clc
|
|
|
|
clear all
|
|
|
|
close all
|
|
|
|
|
2024-09-11 19:34:41 +02:00
|
|
|
% options
|
2024-09-29 10:47:37 +02:00
|
|
|
ROBOT = 'diffdrive'
|
|
|
|
%TESTS = ["straightline/chill", "straightline/chill_errortheta_pisixths", "straightline/toofast", "straightline/chill_errory", "circle/start_center", "figure8/chill", "figure8/toofast", "square"]
|
|
|
|
TESTS = ["circle/start_center"]
|
2024-07-12 19:13:29 +02:00
|
|
|
|
2024-09-11 19:34:41 +02:00
|
|
|
% main
|
2024-07-27 14:50:46 +02:00
|
|
|
s_ = size(TESTS);
|
2024-09-11 15:05:20 +02:00
|
|
|
for i = 1:length(TESTS)
|
2024-09-11 19:34:41 +02:00
|
|
|
clearvars -except i s_ TESTS ROBOT
|
2024-08-05 18:18:22 +02:00
|
|
|
close all
|
2024-07-27 14:50:46 +02:00
|
|
|
|
2024-09-11 19:34:41 +02:00
|
|
|
% load simulation parameters common to all robots and all tests
|
2024-08-05 18:18:22 +02:00
|
|
|
sim_data = load(["tests/robot_common.mat"]);
|
|
|
|
|
2024-07-27 14:50:46 +02:00
|
|
|
TEST = convertStringsToChars(TESTS(i))
|
|
|
|
|
2024-09-11 19:34:41 +02:00
|
|
|
% load test data (trajectory, etc)
|
2024-08-05 18:18:22 +02:00
|
|
|
test_data = load(['tests/' TEST '/common.mat']);
|
|
|
|
for fn = fieldnames(test_data)'
|
|
|
|
sim_data.(fn{1}) = test_data.(fn{1});
|
|
|
|
end
|
2024-09-10 13:04:57 +02:00
|
|
|
|
2024-09-11 19:34:41 +02:00
|
|
|
% set trajectory and starting conditions
|
2024-07-27 14:50:46 +02:00
|
|
|
sim_data.q0 = set_initial_conditions(sim_data.INITIAL_CONDITIONS);
|
2024-08-05 18:18:22 +02:00
|
|
|
[ref dref] = set_trajectory(sim_data.TRAJECTORY, sim_data);
|
2024-07-27 14:50:46 +02:00
|
|
|
sim_data.ref = ref;
|
|
|
|
sim_data.dref = dref;
|
2024-09-11 19:34:41 +02:00
|
|
|
|
|
|
|
% spawn a new worker for each controller
|
|
|
|
% 1: track only
|
|
|
|
% 2: track + 1step
|
|
|
|
% 3: track + multistep
|
2024-09-29 10:47:37 +02:00
|
|
|
spmd (2)
|
2024-07-27 14:50:46 +02:00
|
|
|
worker_index = spmdIndex;
|
2024-09-11 19:34:41 +02:00
|
|
|
% load controller-specific options
|
2024-08-05 18:18:22 +02:00
|
|
|
data = load(['tests/' num2str(worker_index) '.mat']);
|
2024-09-11 19:34:41 +02:00
|
|
|
for fn = fieldnames(data)'
|
|
|
|
sim_data.(fn{1}) = data.(fn{1});
|
|
|
|
end
|
|
|
|
|
|
|
|
% load robot-specific options
|
|
|
|
% put here to overwrite any parameter value left over in the tests
|
|
|
|
% .mat files, just in case
|
|
|
|
data = load(['tests/' ROBOT '.mat']);
|
2024-07-30 11:29:29 +02:00
|
|
|
for fn = fieldnames(data)'
|
|
|
|
sim_data.(fn{1}) = data.(fn{1});
|
|
|
|
end
|
|
|
|
|
2024-09-11 19:34:41 +02:00
|
|
|
% initialize prediction horizon
|
2024-07-27 14:50:46 +02:00
|
|
|
sim_data.U_corr_history = zeros(2,1,sim_data.PREDICTION_HORIZON);
|
|
|
|
sim_data
|
2024-07-30 11:29:29 +02:00
|
|
|
|
2024-09-11 19:34:41 +02:00
|
|
|
% simulate robot
|
2024-09-10 21:29:15 +02:00
|
|
|
tic;
|
2024-09-10 21:17:56 +02:00
|
|
|
[t, q, y, ref_t, U, U_track, U_corr, U_corr_pred_history, Q_pred] = simulate_discr(sim_data);
|
2024-09-10 21:29:15 +02:00
|
|
|
toc;
|
2024-07-27 14:50:46 +02:00
|
|
|
|
|
|
|
disp('Done')
|
|
|
|
end
|
|
|
|
|
2024-09-10 21:17:22 +02:00
|
|
|
% save simulation data
|
2024-09-11 19:34:41 +02:00
|
|
|
f1 = [ TEST '/' char(datetime, 'dd-MM-yyyy-HH-mm-ss')]; % windows compatible name
|
2024-09-29 10:47:37 +02:00
|
|
|
f = ['results-' ROBOT '-costfun-ddronly/' f1];
|
2024-09-10 21:17:22 +02:00
|
|
|
mkdir(f)
|
|
|
|
% save workspace
|
|
|
|
dsave([f '/workspace_composite.mat']);
|
|
|
|
% save test file
|
|
|
|
copyfile(['tests/' TEST], f);
|
|
|
|
|
|
|
|
% save figures + plot results
|
2024-07-28 12:58:36 +02:00
|
|
|
h = [];
|
2024-09-10 21:17:22 +02:00
|
|
|
% plot results
|
2024-07-27 14:50:46 +02:00
|
|
|
s1_ = size(worker_index);
|
|
|
|
for n = 1:s1_(2)
|
2024-07-31 21:21:29 +02:00
|
|
|
h = [h, figure('Name', [TEST ' ' num2str(n)] )];
|
2024-07-27 14:50:46 +02:00
|
|
|
plot_results(t{n}, q{n}, ref_t{n}, U{n}, U_track{n}, U_corr{n});
|
|
|
|
end
|
2024-09-10 21:17:22 +02:00
|
|
|
% plot correction different between 1-step and multistep
|
|
|
|
h = [h, figure('Name', 'difference between 1step and multistep')];
|
2024-08-13 11:45:36 +02:00
|
|
|
subplot(2,1,1)
|
|
|
|
plot(t{2}, U_corr{2}(:, 1) - U_corr{3}(:, 1))
|
|
|
|
xlabel('t')
|
2024-09-11 19:34:41 +02:00
|
|
|
ylabel(['difference on ' sim_data{1}.input1_name ' between 1-step and multistep'])
|
2024-08-13 11:45:36 +02:00
|
|
|
subplot(2,1,2)
|
|
|
|
plot(t{2}, U_corr{2}(:, 2) - U_corr{3}(:, 2))
|
|
|
|
xlabel('t')
|
2024-09-11 19:34:41 +02:00
|
|
|
ylabel(['difference on ' sim_data{1}.input2_name ' between 1-step and multistep'])
|
2024-09-10 21:17:22 +02:00
|
|
|
% save figures
|
|
|
|
savefig(h, [f '/figure.fig']);
|
2024-07-31 21:21:29 +02:00
|
|
|
|
|
|
|
%video(q{1}', ref_t{1}', 0.1, t{1}, 2, sim_data{1}.tc*0.05, "aa");
|
|
|
|
%video(q{2}', ref_t{2}', 0.1, t{2}, 2, sim_data{1}.tc*0.05, "aa");
|
|
|
|
%video(q{3}', ref_t{3}', 0.1, t{3}, 2, sim_data{1}.tc*0.05, "aa");
|
2024-07-27 14:50:46 +02:00
|
|
|
end
|
2024-07-26 20:13:43 +02:00
|
|
|
|
2024-07-16 10:58:00 +02:00
|
|
|
|
|
|
|
%% FUNCTION DECLARATIONS
|
|
|
|
|
|
|
|
% Discrete-time simulation
|
2024-09-10 21:17:56 +02:00
|
|
|
function [t, q, y, ref_t, U, U_track, U_corr, U_corr_pred_history, Q_pred] = 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-31 21:21:29 +02:00
|
|
|
Q_pred = zeros(sim_data.PREDICTION_HORIZON,3,sim_data.tfin/sim_data.tc + 1);
|
2024-08-05 18:18:22 +02:00
|
|
|
U_corr_pred_history=zeros(sim_data.PREDICTION_HORIZON,2,steps);
|
2024-07-31 21:21:29 +02:00
|
|
|
|
|
|
|
[u_discr, u_track, u_corr, U_corr_history, q_pred] = control_act(t, q, sim_data);
|
2024-07-26 20:13:43 +02:00
|
|
|
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-31 21:21:29 +02:00
|
|
|
Q_pred(:, :, 1) = q_pred;
|
2024-09-10 16:01:39 +02:00
|
|
|
y = [];
|
2024-09-11 19:34:41 +02:00
|
|
|
|
|
|
|
if eq(sim_data.robot, 0)
|
|
|
|
fun = @(t, q, u_discr, sim_data) unicycle(t, q, u_discr, sim_data);
|
|
|
|
elseif eq(sim_data.robot, 1)
|
|
|
|
fun = @(t, q, u_discr, sim_data) diffdrive(t, q, u_discr, sim_data);
|
|
|
|
end
|
2024-07-14 15:16:05 +02:00
|
|
|
|
|
|
|
for n = 1:steps
|
2024-07-28 12:58:36 +02:00
|
|
|
sim_data.old_u_corr = u_corr;
|
|
|
|
sim_data.old_u_track = u_track;
|
|
|
|
sim_data.old_u = u_discr;
|
|
|
|
|
2024-07-14 15:16:05 +02:00
|
|
|
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-09-10 13:04:57 +02:00
|
|
|
opt = odeset('MaxStep', 0.005);
|
2024-09-11 19:34:41 +02:00
|
|
|
[v, z] = ode45(@(v, z) fun(v, z, u_discr, sim_data), tspan, z0, opt);
|
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-09-10 21:17:22 +02:00
|
|
|
|
2024-07-31 21:21:29 +02:00
|
|
|
[u_discr, u_track, u_corr, U_corr_history, q_pred] = control_act(t(end), q(end, :), sim_data);
|
2024-07-26 20:13:43 +02:00
|
|
|
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-31 21:21:29 +02:00
|
|
|
Q_pred(:, :, 1+n) = q_pred;
|
2024-08-05 18:18:22 +02:00
|
|
|
|
2024-09-10 16:01:39 +02:00
|
|
|
U_corr_pred_history(:,:,n) = permute(U_corr_history, [3, 1, 2]);
|
|
|
|
|
|
|
|
y1 = q(:, 1) + sim_data.b * cos(q(:,3));
|
|
|
|
y2 = q(:, 2) + sim_data.b * sin(q(:,3));
|
2024-09-13 20:29:18 +02:00
|
|
|
y = [y; [y1, y2]];
|
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-26 20:13:43 +02:00
|
|
|
%%
|