2024-07-31 21:23:17 +02:00
|
|
|
function [u, ut, uc, U_corr_history, q_pred] = control_act(t, q, sim_data)
|
2024-07-31 09:47:09 +02:00
|
|
|
dc = decouple_matrix(q, sim_data);
|
2024-07-24 14:57:19 +02:00
|
|
|
ut = utrack(t, q, sim_data);
|
2024-07-28 11:25:35 +02:00
|
|
|
ut = dc*ut;
|
2024-09-16 18:04:18 +02:00
|
|
|
|
2024-09-13 20:26:01 +02:00
|
|
|
[uc, U_corr_history, q_pred] = ucorr(t, q, sim_data);
|
2024-09-16 18:04:18 +02:00
|
|
|
|
|
|
|
uc = dc*uc;
|
2024-07-28 11:25:35 +02:00
|
|
|
u = ut+uc;
|
2024-07-24 11:49:34 +02:00
|
|
|
% saturation
|
2024-07-24 14:57:19 +02:00
|
|
|
u = min(sim_data.SATURATION, max(-sim_data.SATURATION, u));
|
2024-07-24 11:49:34 +02:00
|
|
|
end
|
2024-07-12 19:14:53 +02:00
|
|
|
|
2024-07-31 21:23:17 +02:00
|
|
|
function [u_corr, U_corr_history, q_pred] = ucorr(t, q, sim_data)
|
2024-07-24 14:57:19 +02:00
|
|
|
pred_hor = sim_data.PREDICTION_HORIZON;
|
|
|
|
SATURATION = sim_data.SATURATION;
|
|
|
|
PREDICTION_SATURATION_TOLERANCE = sim_data.PREDICTION_SATURATION_TOLERANCE;
|
|
|
|
tc = sim_data.tc;
|
|
|
|
|
2024-07-28 11:25:35 +02:00
|
|
|
u_corr = zeros(2,1);
|
|
|
|
U_corr_history = zeros(2,1,sim_data.PREDICTION_HORIZON);
|
2024-07-31 21:23:17 +02:00
|
|
|
q_act = q;
|
|
|
|
u_track_pred=zeros(2,1, pred_hor);
|
|
|
|
T_inv_pred=zeros(2,2, pred_hor);
|
|
|
|
|
|
|
|
q_pred = [];
|
2024-07-28 11:25:35 +02:00
|
|
|
|
2024-09-10 13:04:57 +02:00
|
|
|
s_ = SATURATION - ones(2,1)*PREDICTION_SATURATION_TOLERANCE;
|
2024-07-24 14:57:19 +02:00
|
|
|
if eq(pred_hor, 0)
|
2024-07-24 11:49:34 +02:00
|
|
|
return
|
2024-09-10 13:04:57 +02:00
|
|
|
elseif eq(pred_hor, 1)
|
2024-07-31 09:47:09 +02:00
|
|
|
T_inv = decouple_matrix(q_act, sim_data);
|
2024-09-10 13:04:57 +02:00
|
|
|
ut = utrack(t, q_act, sim_data);
|
2024-09-16 18:27:41 +02:00
|
|
|
|
|
|
|
% minimize v
|
|
|
|
H = 2*eye(2);
|
2024-09-16 18:04:18 +02:00
|
|
|
|
|
|
|
f = zeros(2,1);
|
|
|
|
A = [T_inv; -T_inv];
|
|
|
|
%A = [eye(2); -eye(2)];
|
2024-07-24 11:49:34 +02:00
|
|
|
|
2024-09-10 13:04:57 +02:00
|
|
|
d = T_inv*ut;
|
|
|
|
b = [s_-d;s_+d];
|
|
|
|
|
|
|
|
% solve qp problem
|
2024-09-16 18:04:18 +02:00
|
|
|
options = optimoptions('quadprog', 'Display', 'off');
|
|
|
|
u_corr = quadprog(H, f, A, b, [],[],[],[],[],options);
|
2024-09-10 13:04:57 +02:00
|
|
|
|
|
|
|
q_pred = q_act;
|
|
|
|
U_corr_history(:,:,1) = u_corr;
|
|
|
|
return
|
|
|
|
else
|
|
|
|
%if pred_hor > 1
|
|
|
|
% move the horizon over 1 step and add trailing zeroes
|
|
|
|
U_corr_history = cat(3, sim_data.U_corr_history(:,:, 2:end), zeros(2,1));
|
|
|
|
%end
|
2024-07-24 11:49:34 +02:00
|
|
|
|
2024-09-10 13:04:57 +02:00
|
|
|
%disp('start of simulation')
|
|
|
|
% for each step in the prediction horizon, integrate the system to
|
|
|
|
% predict its future state
|
|
|
|
|
|
|
|
for k = 1:pred_hor
|
|
|
|
% start from the old (known) state
|
|
|
|
|
|
|
|
% compute the inputs, based on the old state
|
|
|
|
|
|
|
|
% u_corr is the prediction done at some time in the past, as found in U_corr_history
|
|
|
|
u_corr_ = U_corr_history(:, :, k);
|
|
|
|
% u_track can be computed from q
|
|
|
|
t_ = t + tc * (k-1);
|
|
|
|
u_track_ = utrack(t_, q_act, sim_data);
|
|
|
|
|
|
|
|
T_inv = decouple_matrix(q_act, sim_data);
|
2024-09-11 19:34:41 +02:00
|
|
|
% compute inputs (v, w)/(wr, wl)
|
2024-09-16 18:04:18 +02:00
|
|
|
u_ = T_inv * (u_track_ + u_corr_);
|
2024-09-11 19:34:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
% if needed, map (wr, wl) to (v, w) for unicicle
|
|
|
|
if eq(sim_data.robot, 1)
|
|
|
|
u_ = diffdrive_to_uni(u_, sim_data);
|
|
|
|
end
|
2024-09-10 13:04:57 +02:00
|
|
|
|
|
|
|
% integrate unicycle
|
|
|
|
theta_new = q_act(3) + tc*u_(2);
|
|
|
|
% compute the state integrating with euler
|
|
|
|
%x_new = q_act(1) + tc*u_(1) * cos(q_act(3));
|
|
|
|
%y_new = q_act(2) + tc*u_(1) * sin(q_act(3));
|
|
|
|
% compute the state integrating via runge-kutta
|
|
|
|
x_new = q_act(1) + tc*u_(1) * cos(q_act(3) + 0.5*tc*u_(2));
|
|
|
|
y_new = q_act(2) + tc*u_(1) * sin(q_act(3) + 0.5*tc*u_(2));
|
|
|
|
|
|
|
|
q_new = [x_new; y_new; theta_new];
|
|
|
|
|
|
|
|
% save history
|
|
|
|
q_pred = [q_pred; q_new'];
|
|
|
|
u_track_pred(:,:,k) = u_track_;
|
|
|
|
T_inv_pred(:,:,k) = T_inv;
|
2024-07-24 11:49:34 +02:00
|
|
|
|
2024-09-10 13:04:57 +02:00
|
|
|
% Prepare old state for next iteration
|
|
|
|
q_act = q_new;
|
|
|
|
end
|
|
|
|
|
|
|
|
%{
|
|
|
|
Now setup the qp problem
|
|
|
|
It needs:
|
|
|
|
- Unknowns, u_corr at each timestep. Will be encoded as a vector of
|
|
|
|
vectors, in which every two elements is a u_j
|
|
|
|
i.e. (u_1; u_2; u_3; ...; u_C) = (v_1; w_1; v_2, w_2; v_3, w_3; ...
|
|
|
|
; v_C, w_C)
|
|
|
|
It is essential that the vector stays a column, so that u'u is the
|
|
|
|
sum of the squared norms of each u_j
|
|
|
|
|
|
|
|
- Box constraints: a constraint for each timestep in the horizon.
|
|
|
|
Calculated using the predicted state and inputs. They need to be
|
|
|
|
put in matrix (Ax <= b) form
|
|
|
|
%}
|
|
|
|
|
|
|
|
% box constrains
|
|
|
|
% A becomes sort of block-diagonal
|
|
|
|
% A will be at most PREDICTION_HORIZON * 2 * 2 (2: size of T_inv; 2:
|
|
|
|
% accounting for T_inv and -T_inv) by PREDICTION_HORIZON (number of
|
|
|
|
% vectors in u_corr times the number of elements [2] in each vector)
|
2024-09-16 18:04:18 +02:00
|
|
|
A_deq = [];
|
2024-09-10 13:04:57 +02:00
|
|
|
b_deq = [];
|
|
|
|
for k=1:pred_hor
|
|
|
|
T_inv = T_inv_pred(:,:,k);
|
|
|
|
u_track = u_track_pred(:,:,k);
|
|
|
|
d = T_inv*u_track;
|
2024-09-16 18:04:18 +02:00
|
|
|
|
|
|
|
A_deq = blkdiag(A_deq, [T_inv; -T_inv]);
|
2024-09-10 13:04:57 +02:00
|
|
|
b_deq = [b_deq; s_ - d; s_ + d];
|
|
|
|
end
|
|
|
|
|
2024-09-16 18:04:18 +02:00
|
|
|
%A_deq = kron(eye(pred_hor), [eye(2); -eye(2)]);
|
2024-09-10 13:04:57 +02:00
|
|
|
%A_deq
|
|
|
|
%b_deq
|
|
|
|
% unknowns
|
|
|
|
|
|
|
|
% squared norm of u_corr. H must be identity,
|
|
|
|
% PREDICTION_HORIZON*size(u_corr)
|
2024-09-13 20:26:01 +02:00
|
|
|
%H = eye(pred_hor*2)*2;
|
2024-09-16 18:27:41 +02:00
|
|
|
H = kron(eye(pred_hor), 2*eye(2));
|
|
|
|
%H = kron(eye(pred_hor), 2*ones(2,2));
|
2024-09-10 13:04:57 +02:00
|
|
|
% no linear terms
|
|
|
|
f = zeros(pred_hor*2, 1);
|
|
|
|
|
|
|
|
% solve qp problem
|
|
|
|
options = optimoptions('quadprog', 'Display', 'off');
|
|
|
|
U_corr = quadprog(H, f, A_deq, b_deq, [],[],[],[],[],options);
|
|
|
|
%U_corr = lsqnonlin(@(pred_hor) ones(pred_hor, 1), U_corr_history(:,:,1), [], [], A_deq, b_deq, [], []);
|
|
|
|
|
|
|
|
% reshape the vector of vectors to be an array, each element being
|
|
|
|
% u_corr_j as a 2x1 vector
|
|
|
|
% and add the prediction at t_k+C
|
|
|
|
U_corr_history = reshape(U_corr, [2,1,pred_hor]);
|
|
|
|
% first result is what to do now
|
|
|
|
u_corr=U_corr_history(:,:, 1);
|
|
|
|
end
|
2024-07-24 11:49:34 +02:00
|
|
|
end
|
|
|
|
|
2024-07-24 14:57:19 +02:00
|
|
|
function u_track = utrack(t, q, sim_data)
|
|
|
|
ref_s = double(subs(sim_data.ref, t));
|
|
|
|
dref_s = double(subs(sim_data.dref, t));
|
2024-07-24 11:49:34 +02:00
|
|
|
|
2024-07-24 14:57:19 +02:00
|
|
|
f = feedback(q, sim_data.b);
|
2024-07-24 11:49:34 +02:00
|
|
|
err = ref_s - f;
|
2024-07-24 14:57:19 +02:00
|
|
|
u_track = dref_s + sim_data.K*err;
|
2024-07-12 19:14:53 +02:00
|
|
|
end
|
|
|
|
|
2024-07-24 14:57:19 +02:00
|
|
|
function q_track = feedback(q, b)
|
2024-07-24 11:49:34 +02:00
|
|
|
q_track = [q(1) + b*cos(q(3)); q(2) + b*sin(q(3)) ];
|
|
|
|
end
|
|
|
|
|
2024-07-31 09:47:09 +02:00
|
|
|
function T_inv = decouple_matrix(q, sim_data)
|
2024-07-24 11:49:34 +02:00
|
|
|
theta = q(3);
|
2024-07-31 09:47:09 +02:00
|
|
|
|
2024-07-24 11:49:34 +02:00
|
|
|
st = sin(theta);
|
|
|
|
ct = cos(theta);
|
2024-07-31 09:47:09 +02:00
|
|
|
|
|
|
|
b = sim_data.b;
|
2024-09-11 19:34:41 +02:00
|
|
|
|
|
|
|
if eq(sim_data.robot, 0)
|
|
|
|
T_inv = [ct, st; -st/b, ct/b];
|
|
|
|
elseif eq(sim_data.robot, 1)
|
|
|
|
r = sim_data.r;
|
|
|
|
d = sim_data.d;
|
|
|
|
|
|
|
|
T_inv = [2*b*ct - d*st, d*ct + 2*b*st ; 2*b*ct + d*st, -d*ct+2*b*st] / (2*b*r);
|
|
|
|
end
|
2024-07-12 19:14:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|