1-step mpc

master
EmaMaker 2024-07-16 10:58:00 +02:00
parent c79a8744b2
commit 44f65aed77
3 changed files with 87 additions and 17 deletions

View File

@ -1,22 +1,64 @@
function u = control_act(t, x) function u = control_act(t, x)
global ref dref K b SATURATION PREDICTION_SATURATION_TOLERANCE USE_PREDICTION PREDICTION_STEPS
global ref dref K b saturation
ref_s = double(subs(ref, t)); ref_s = double(subs(ref, t));
dref_s = double(subs(dref, t)); dref_s = double(subs(dref, t));
err = ref_s - feedback(x); err = ref_s - feedback(x);
u_nom = dref_s + K*err; u_track = dref_s + K*err;
theta = x(3); theta = x(3);
T_inv = [cos(theta), sin(theta); -sin(theta)/b, cos(theta)/b]; T_inv = [cos(theta), sin(theta); -sin(theta)/b, cos(theta)/b];
u = T_inv * ( u_nom ); u = zeros(2,1);
if USE_PREDICTION==true
% 1-step prediction
% quadprog solves the problem in the form
% min 1/2 x'Hx +f'x
% where x is u_corr. Since u_corr is (v_corr; w_corr), and I want
% to minimize u'u (norm squared of the function itself) H must be
% the identity matrix of size 2
H = eye(2)*2;
% no linear of constant terms, so
f = [];
% and there are box constraints on the saturation, as upper/lower
% bounds
%T = inv(T_inv);
%lb = -T*saturation - u_track;
%ub = T*saturation - u_track;
% matlab says this is a more efficient way of doing
% inv(T_inv)*saturation
%lb = -T_inv\saturation - u_track;
%ub = T_inv\saturation - u_track;
% Resolve box constraints as two inequalities
A_deq = [T_inv; -T_inv];
d = T_inv*u_track;
b_deq = [SATURATION - ones(2,1)*PREDICTION_SATURATION_TOLERANCE - d;
SATURATION - ones(2,1)*PREDICTION_SATURATION_TOLERANCE + d];
% solve the problem
% no <= constraints
% no equality constraints
% only upper/lower bound constraints
options = optimoptions('quadprog', 'Display', 'off');
u_corr = quadprog(H, f, A_deq, b_deq, [],[],[],[],[],options);
u = T_inv * (u_track + u_corr);
global tu uu
tu = [tu, t];
uu = [uu, u_corr];
else
u = T_inv * u_track;
end
% saturation % saturation
u = min(saturation, max(-saturation, u)); u = min(SATURATION, max(-SATURATION, u));
end end
function x_track = feedback(x) function x_track = feedback(x)

View File

@ -19,14 +19,14 @@ switch i
xref = 5 + 10*s; xref = 5 + 10*s;
yref = 0; yref = 0;
case 4 case 4
xref = 5*cos(s); xref = 2.5*cos(s);
yref = 5*sin(s); yref = 2.5*sin(s);
case 5 case 5
xref = 15*cos(s); xref = 15*cos(s);
yref = 15*sin(s); yref = 15*sin(s);
case 6 case 6
xref = 5*cos(0.05*s); xref = 5*cos(0.15*s);
yref = 5*sin(0.05*s); yref = 5*sin(0.15*s);
end end
ref = [xref; yref]; ref = [xref; yref];

View File

@ -2,10 +2,14 @@ clc
clear all clear all
close all close all
global x0 ref dref b K saturation tc tfin %% global variables
global x0 ref dref b K SATURATION tc tfin USE_PREDICTION PREDICTION_STEP PREDICTION_SATURATION_TOLERANCE;
TRAJECTORY = 0 %% variables
TRAJECTORY = 6
INITIAL_CONDITIONS = 1 INITIAL_CONDITIONS = 1
USE_PREDICTION = false
PREDICTION_STEPS = 1
% distance from the center of the unicycle to the point being tracked % distance from the center of the unicycle to the point being tracked
% ATTENZIONE! CI SARA' SEMPRE UN ERRORE COSTANTE DOVUTO A b. Minore b, % ATTENZIONE! CI SARA' SEMPRE UN ERRORE COSTANTE DOVUTO A b. Minore b,
% minore l'errore % minore l'errore
@ -13,24 +17,47 @@ b = 0.2
% proportional gain % proportional gain
K = eye(2)*2 K = eye(2)*2
tfin=10
% saturation % saturation
% HYP: a diff. drive robot with motors spinning at 100rpm -> 15.7 rad/s. % HYP: a diff. drive robot with motors spinning at 100rpm -> 15.7 rad/s.
% Radius of wheels 10cm. Wheels distanced 15cm from each other % Radius of wheels 10cm. Wheels distanced 15cm from each other
% applying transformation, v % applying transformation, v
% saturation = [1.57, 20]; % saturation = [1.57, 20];
saturation = [1.57; 20]; SATURATION = [1.57; 20];
PREDICTION_SATURATION_TOLERANCE = 0.1;
%% launch simulation
% initial state % initial state
% In order, [x, y, theta] % In order, [x, y, theta]
x0 = set_initial_conditions(INITIAL_CONDITIONS) x0 = set_initial_conditions(INITIAL_CONDITIONS)
% trajectory to track % trajectory to track
[ref, dref] = set_trajectory(TRAJECTORY) [ref, dref] = set_trajectory(TRAJECTORY)
[t, x, ref_t, U] = simulate_discr(60, 0.1); global tu uu
plot_results(t, x, ref_t, U);
%figure(1)
%USE_PREDICTION = false;
%[t, x, ref_t, U] = simulate_discr(tfin, 0.05);
%plot_results(t, x, ref_t, U);
figure(2)
USE_PREDICTION = true;
[t1, x1, ref_t1, U1] = simulate_discr(tfin, 0.05);
plot_results(t1, x1, ref_t1, U1);
figure(3)
subplot(1, 2, 1)
plot(tu, uu(1, :))
subplot(1, 2, 2)
plot(tu, uu(2, :))
%plot_results(t, x-x1, ref_t-ref_t1, U-U1);
%% FUNCTION DECLARATIONS
% Discrete-time simulation
function [t, x, ref_t, U] = simulate_discr(tfin, tc) function [t, x, ref_t, U] = simulate_discr(tfin, tc)
global ref x0 u_discr global ref x0 u_discr
@ -41,7 +68,6 @@ function [t, x, ref_t, U] = simulate_discr(tfin, tc)
u_discr = control_act(t, x0); u_discr = control_act(t, x0);
U = u_discr'; U = u_discr';
for n = 1:steps for n = 1:steps
tspan = [(n-1)*tc n*tc]; tspan = [(n-1)*tc n*tc];
z0 = x(end, :); z0 = x(end, :);
@ -51,7 +77,7 @@ function [t, x, ref_t, U] = simulate_discr(tfin, tc)
x = [x; z]; x = [x; z];
t = [t; v]; t = [t; v];
u_discr = control_act(t(end, :), x(end, :)); u_discr = control_act(t(end), x(end, :));
U = [U; ones(length(v), 1)*u_discr']; U = [U; ones(length(v), 1)*u_discr'];
end end
@ -59,6 +85,7 @@ function [t, x, ref_t, U] = simulate_discr(tfin, tc)
end end
% Continuos-time simulation
function [t, x, ref, U] = simulate_cont(tfin) function [t, x, ref, U] = simulate_cont(tfin)
global ref x0 global ref x0
@ -79,6 +106,7 @@ function [t, x, ref, U] = simulate_cont(tfin)
ref = double(subs(ref, t'))'; ref = double(subs(ref, t'))';
end end
% Plots
function plot_results(t, x, ref, U) function plot_results(t, x, ref, U)
subplot(2,2,1) subplot(2,2,1)
hold on hold on