2024-07-12 19:13:29 +02:00
|
|
|
clc
|
|
|
|
clear all
|
|
|
|
close all
|
|
|
|
|
2024-07-16 10:58:00 +02:00
|
|
|
%% global variables
|
2024-07-24 11:49:34 +02:00
|
|
|
global q0 ref dref b tc K SATURATION tc tfin USE_PREDICTION PREDICTION_HORIZON PREDICTION_SATURATION_TOLERANCE;
|
2024-07-12 19:13:29 +02:00
|
|
|
|
2024-07-16 10:58:00 +02:00
|
|
|
%% variables
|
|
|
|
TRAJECTORY = 6
|
2024-07-14 15:16:05 +02:00
|
|
|
INITIAL_CONDITIONS = 1
|
2024-07-16 10:58:00 +02:00
|
|
|
USE_PREDICTION = false
|
2024-07-24 11:49:34 +02:00
|
|
|
PREDICTION_HORIZON = 5
|
2024-07-12 19:13:29 +02:00
|
|
|
% distance from the center of the unicycle to the point being tracked
|
|
|
|
% ATTENZIONE! CI SARA' SEMPRE UN ERRORE COSTANTE DOVUTO A b. Minore b,
|
|
|
|
% minore l'errore
|
|
|
|
b = 0.2
|
|
|
|
% proportional gain
|
2024-07-14 15:16:05 +02:00
|
|
|
K = eye(2)*2
|
|
|
|
|
2024-07-24 11:49:34 +02:00
|
|
|
tc = 0.1
|
2024-07-23 18:07:50 +02:00
|
|
|
tfin=30
|
2024-07-14 15:16:05 +02:00
|
|
|
|
2024-07-12 19:13:29 +02:00
|
|
|
% saturation
|
2024-07-14 15:16:05 +02:00
|
|
|
% HYP: a diff. drive robot with motors spinning at 100rpm -> 15.7 rad/s.
|
|
|
|
% Radius of wheels 10cm. Wheels distanced 15cm from each other
|
|
|
|
% applying transformation, v
|
|
|
|
% saturation = [1.57, 20];
|
2024-07-23 18:07:50 +02:00
|
|
|
SATURATION = [1; 1];
|
|
|
|
PREDICTION_SATURATION_TOLERANCE = 0.0;
|
2024-07-12 19:13:29 +02:00
|
|
|
|
2024-07-16 10:58:00 +02:00
|
|
|
%% launch simulation
|
2024-07-12 19:13:29 +02:00
|
|
|
% initial state
|
|
|
|
% In order, [x, y, theta]
|
2024-07-23 18:07:50 +02:00
|
|
|
q0 = set_initial_conditions(INITIAL_CONDITIONS)
|
2024-07-12 19:13:29 +02:00
|
|
|
% trajectory to track
|
|
|
|
[ref, dref] = set_trajectory(TRAJECTORY)
|
|
|
|
|
2024-07-16 10:58:00 +02:00
|
|
|
global tu uu
|
|
|
|
|
2024-07-23 18:07:50 +02:00
|
|
|
figure(1)
|
2024-07-24 11:49:34 +02:00
|
|
|
PREDICTION_HORIZON = 0;
|
|
|
|
[t, q, ref_t, U] = simulate_discr(tfin);
|
2024-07-23 18:07:50 +02:00
|
|
|
plot_results(t, q, ref_t, U);
|
2024-07-16 10:58:00 +02:00
|
|
|
|
|
|
|
figure(2)
|
2024-07-24 11:49:34 +02:00
|
|
|
PREDICTION_HORIZON = 1;
|
|
|
|
[t1, q1, ref_t1, U1] = simulate_discr(tfin);
|
2024-07-23 18:07:50 +02:00
|
|
|
plot_results(t1, q1, ref_t1, U1);
|
2024-07-16 10:58:00 +02:00
|
|
|
|
|
|
|
figure(3)
|
2024-07-24 11:49:34 +02:00
|
|
|
PREDICTION_HORIZON = 2;
|
|
|
|
[t2, q2, ref_t2, U2] = simulate_discr(tfin);
|
|
|
|
plot_results(t2, q2, ref_t2, U2);
|
|
|
|
|
|
|
|
%figure(3)
|
|
|
|
%subplot(1, 2, 1)
|
|
|
|
%plot(tu, uu(1, :))
|
|
|
|
%subplot(1, 2, 2)
|
|
|
|
%plot(tu, uu(2, :))
|
2024-07-16 10:58:00 +02:00
|
|
|
%plot_results(t, x-x1, ref_t-ref_t1, U-U1);
|
2024-07-14 15:16:05 +02:00
|
|
|
|
2024-07-16 10:58:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
%% FUNCTION DECLARATIONS
|
|
|
|
|
|
|
|
% Discrete-time simulation
|
2024-07-24 11:49:34 +02:00
|
|
|
function [t, q, ref_t, U] = simulate_discr(tfin)
|
|
|
|
global ref q0 u_discr tc
|
2024-07-14 15:16:05 +02:00
|
|
|
|
|
|
|
steps = tfin/tc
|
|
|
|
|
2024-07-23 18:07:50 +02:00
|
|
|
q = q0';
|
2024-07-14 15:16:05 +02:00
|
|
|
t = 0;
|
2024-07-23 18:07:50 +02:00
|
|
|
u_discr = control_act(t, q0);
|
2024-07-14 15:16:05 +02:00
|
|
|
U = u_discr';
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
[v, z] = ode45(@sistema, 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-23 18:07:50 +02:00
|
|
|
u_discr = control_act(t(end), q(end, :));
|
2024-07-14 15:16:05 +02:00
|
|
|
U = [U; ones(length(v), 1)*u_discr'];
|
|
|
|
end
|
2024-07-12 19:13:29 +02:00
|
|
|
|
2024-07-14 15:16:05 +02:00
|
|
|
ref_t = double(subs(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-16 10:58:00 +02:00
|
|
|
% Plots
|
2024-07-14 15:16:05 +02:00
|
|
|
function plot_results(t, x, ref, U)
|
|
|
|
subplot(2,2,1)
|
|
|
|
hold on
|
|
|
|
plot(ref(:, 1), ref(:, 2), "DisplayName", "Ref")
|
|
|
|
plot(x(:, 1), x(:, 2), "DisplayName", "state")
|
|
|
|
xlabel('x')
|
|
|
|
ylabel('y')
|
|
|
|
legend()
|
|
|
|
subplot(2,2,3)
|
|
|
|
plot(t, U(:, 1))
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('input v')
|
|
|
|
subplot(2,2,4)
|
|
|
|
plot(t, U(:, 2))
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('input w')
|
|
|
|
|
|
|
|
|
|
|
|
subplot(4,4,3)
|
|
|
|
hold on
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('x')
|
|
|
|
plot(t, ref(:, 1), "DisplayName", "X_{ref}");
|
|
|
|
plot(t, x(:, 1), "DisplayName", "X");
|
|
|
|
legend()
|
|
|
|
hold off
|
|
|
|
|
|
|
|
subplot(4,4,4)
|
|
|
|
plot(t, ref(:, 1) - x(:, 1));
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('x error')
|
|
|
|
|
|
|
|
subplot(4,4,7)
|
|
|
|
hold on
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('y')
|
|
|
|
plot(t, ref(:, 2), "DisplayName", "Y_{ref}");
|
|
|
|
plot(t, x(:, 2), "DisplayName", "Y");
|
|
|
|
legend()
|
|
|
|
hold off
|
|
|
|
|
|
|
|
subplot(4,4,8)
|
|
|
|
plot(t, ref(:, 2) - x(:, 2));
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('y error')
|
|
|
|
end
|