2024-07-12 19:13:29 +02:00
|
|
|
clc
|
|
|
|
clear all
|
|
|
|
close all
|
|
|
|
|
|
|
|
global x0 ref dref b K saturation
|
|
|
|
|
2024-07-13 11:33:13 +02:00
|
|
|
TRAJECTORY = 4
|
2024-07-12 19:13:29 +02:00
|
|
|
INITIAL_CONDITIONS = 0
|
|
|
|
% 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-13 11:33:13 +02:00
|
|
|
K = eye(2)*2.5
|
2024-07-12 19:13:29 +02:00
|
|
|
% saturation
|
2024-07-13 11:33:13 +02:00
|
|
|
saturation = [5; 0.5];
|
2024-07-12 19:13:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
% initial state
|
|
|
|
% In order, [x, y, theta]
|
|
|
|
x0 = set_initial_conditions(INITIAL_CONDITIONS)
|
|
|
|
% trajectory to track
|
|
|
|
[ref, dref] = set_trajectory(TRAJECTORY)
|
|
|
|
|
|
|
|
|
|
|
|
% simulation time
|
2024-07-13 11:33:13 +02:00
|
|
|
tspan = 0:0.1:60;
|
2024-07-12 19:13:29 +02:00
|
|
|
% execute simulation
|
2024-07-13 11:33:13 +02:00
|
|
|
[t, x] = ode45(@sistema, tspan, x0);
|
2024-07-12 19:13:29 +02:00
|
|
|
|
2024-07-13 11:33:13 +02:00
|
|
|
% recalc and save input at each timestep
|
|
|
|
ts = size(t);
|
|
|
|
rows = ts(1);
|
|
|
|
U = zeros(rows, 2);
|
|
|
|
for row = 1:rows
|
|
|
|
U(row, :) = control_act(t(row), x(row, :));
|
|
|
|
end
|
2024-07-12 19:13:29 +02:00
|
|
|
|
|
|
|
% plot results
|
2024-07-13 11:33:13 +02:00
|
|
|
ref_t = double(subs(ref, t'))';
|
2024-07-12 19:13:29 +02:00
|
|
|
|
|
|
|
subplot(3,2,1)
|
|
|
|
hold on
|
2024-07-13 11:33:13 +02:00
|
|
|
xlabel('t')
|
|
|
|
ylabel('x')
|
2024-07-12 19:13:29 +02:00
|
|
|
plot(t, ref_t(:, 1));
|
|
|
|
plot(t, x(:, 1));
|
|
|
|
legend()
|
2024-07-13 11:33:13 +02:00
|
|
|
hold off
|
|
|
|
|
2024-07-12 19:13:29 +02:00
|
|
|
subplot(3,2,2)
|
|
|
|
plot(t, ref_t(:, 1) - x(:, 1));
|
2024-07-13 11:33:13 +02:00
|
|
|
xlabel('t')
|
|
|
|
ylabel('x error')
|
|
|
|
|
2024-07-12 19:13:29 +02:00
|
|
|
subplot(3,2,3)
|
|
|
|
hold on
|
2024-07-13 11:33:13 +02:00
|
|
|
xlabel('t')
|
|
|
|
ylabel('y')
|
2024-07-12 19:13:29 +02:00
|
|
|
plot(t, ref_t(:, 2));
|
|
|
|
plot(t, x(:, 2));
|
|
|
|
legend()
|
2024-07-13 11:33:13 +02:00
|
|
|
hold off
|
|
|
|
|
2024-07-12 19:13:29 +02:00
|
|
|
subplot(3,2,4)
|
|
|
|
plot(t, ref_t(:, 2) - x(:, 2));
|
2024-07-13 11:33:13 +02:00
|
|
|
xlabel('t')
|
|
|
|
ylabel('y error')
|
|
|
|
|
|
|
|
|
|
|
|
subplot(3,2,5)
|
|
|
|
plot(t, U(:, 1))
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('input v')
|
2024-07-12 19:13:29 +02:00
|
|
|
|
2024-07-13 11:33:13 +02:00
|
|
|
subplot(3,2,6)
|
|
|
|
plot(t, U(:, 2))
|
|
|
|
xlabel('t')
|
|
|
|
ylabel('input w')
|