thesis/control_act.m

70 lines
2.0 KiB
Matlab
Raw Normal View History

2024-07-12 19:14:53 +02:00
function u = control_act(t, x)
2024-07-16 10:58:00 +02:00
global ref dref K b SATURATION PREDICTION_SATURATION_TOLERANCE USE_PREDICTION PREDICTION_STEPS
2024-07-12 19:14:53 +02:00
ref_s = double(subs(ref, t));
dref_s = double(subs(dref, t));
err = ref_s - feedback(x);
2024-07-16 10:58:00 +02:00
u_track = dref_s + K*err;
2024-07-12 19:14:53 +02:00
theta = x(3);
T_inv = [cos(theta), sin(theta); -sin(theta)/b, cos(theta)/b];
2024-07-16 10:58:00 +02:00
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);
2024-07-12 19:14:53 +02:00
2024-07-16 10:58:00 +02:00
global tu uu
tu = [tu, t];
uu = [uu, u_corr];
else
u = T_inv * u_track;
end
2024-07-12 19:14:53 +02:00
% saturation
2024-07-16 10:58:00 +02:00
u = min(SATURATION, max(-SATURATION, u));
2024-07-12 19:14:53 +02:00
end
function x_track = feedback(x)
global b
x_track = [ x(1) + b*cos(x(3)); x(2) + b*sin(x(3)) ];
end