2019-12-18 11:25:45 +00:00
|
|
|
% ---------------------------------------------------------------------
|
|
|
|
|
% In this script trajectory optimization otherwise called experiment
|
|
|
|
|
% design for dynamic paramters identification is carried out.
|
|
|
|
|
% ---------------------------------------------------------------------
|
|
|
|
|
run('main_ur.m'); % get robot description
|
|
|
|
|
|
2019-12-19 10:31:18 +00:00
|
|
|
load('baseQR.mat'); % load mapping from full parameters to base parameters
|
|
|
|
|
|
2020-03-06 12:55:03 +00:00
|
|
|
% Choose optimization algorithm: 'patternsearch', 'ga'
|
2019-12-19 06:48:18 +00:00
|
|
|
optmznAlgorithm = 'patternsearch';
|
|
|
|
|
|
2019-12-18 11:25:45 +00:00
|
|
|
% Getting limits on posistion and velocities. Moreover we get some
|
|
|
|
|
% constant parmeters of the robot that allow us to accelerate computation
|
|
|
|
|
% of the robot regressor.
|
|
|
|
|
q_min = zeros(6,1); q_max = zeros(6,1); qd_max = zeros(6,1);
|
|
|
|
|
for i = 1:6
|
|
|
|
|
rot_axes(:,i) = str2num(ur10.robot.joint{i}.axis.Attributes.xyz)';
|
|
|
|
|
R_pj = RPY(str2num(ur10.robot.joint{i}.origin.Attributes.rpy));
|
|
|
|
|
p_pj = str2num(ur10.robot.joint{i}.origin.Attributes.xyz)';
|
|
|
|
|
T_pj(:,:,i) = [R_pj, p_pj; zeros(1,3), 1];
|
|
|
|
|
r_com(:,i) = str2num(ur10.robot.link{i+1}.inertial.origin.Attributes.xyz)';
|
|
|
|
|
|
|
|
|
|
q_min(i) = str2double(ur10.robot.joint{i}.limit.Attributes.lower);
|
|
|
|
|
q_max(i) = str2double(ur10.robot.joint{i}.limit.Attributes.upper);
|
|
|
|
|
qd_max(i) = str2double(ur10.robot.joint{i}.limit.Attributes.velocity);
|
|
|
|
|
end
|
|
|
|
|
ur10.rot_axes = rot_axes; % axis of rotation of each joint
|
|
|
|
|
ur10.T_pj = T_pj;
|
|
|
|
|
ur10.r_com = r_com;
|
2019-12-19 10:31:18 +00:00
|
|
|
|
2019-12-18 11:25:45 +00:00
|
|
|
|
|
|
|
|
% Trajectory parameters
|
2020-02-05 04:55:09 +00:00
|
|
|
traj_par.T = 25; % period of signal
|
2019-12-18 11:25:45 +00:00
|
|
|
traj_par.wf = 2*pi/traj_par.T; % fundamental frequency
|
2020-02-05 04:55:09 +00:00
|
|
|
traj_par.t_smp = 2e-1; % sampling time
|
2019-12-18 11:25:45 +00:00
|
|
|
traj_par.t = 0:traj_par.t_smp:traj_par.T; % time
|
2020-02-05 04:55:09 +00:00
|
|
|
traj_par.N = 7; % number of harmonics
|
2019-12-19 10:31:18 +00:00
|
|
|
traj_par.q0 = deg2rad([0 -90 0 -90 0 0 ]');
|
|
|
|
|
% Use different limit for positions for safety
|
2020-02-05 04:55:09 +00:00
|
|
|
traj_par.q_min = -deg2rad([180 180 100 180 90 90]');
|
|
|
|
|
traj_par.q_max = deg2rad([180 0 100 0 90 90]');
|
2019-12-19 10:31:18 +00:00
|
|
|
traj_par.qd_max = qd_max;
|
2020-02-05 04:55:09 +00:00
|
|
|
traj_par.q2d_max = [2 1 1 1 1 2.5]';
|
2019-12-18 11:25:45 +00:00
|
|
|
|
2019-12-19 06:48:18 +00:00
|
|
|
% ----------------------------------------------------------------------
|
2019-12-18 11:25:45 +00:00
|
|
|
% Otimization
|
2019-12-19 06:48:18 +00:00
|
|
|
% -----------------------------------------------------------------------
|
2019-12-18 11:25:45 +00:00
|
|
|
A = []; b = [];
|
|
|
|
|
Aeq = []; beq = [];
|
|
|
|
|
lb = []; ub = [];
|
|
|
|
|
|
2019-12-19 06:48:18 +00:00
|
|
|
if strcmp(optmznAlgorithm, 'patternsearch')
|
2020-03-06 12:55:03 +00:00
|
|
|
x0 = rand(6*2*traj_par.N,1);
|
2020-02-05 04:55:09 +00:00
|
|
|
x0 = reshape([a b], [6*2*traj_par.N, 1]);
|
2019-12-19 06:48:18 +00:00
|
|
|
optns_pttrnSrch = optimoptions('patternsearch');
|
|
|
|
|
optns_pttrnSrch.Display = 'iter';
|
2020-02-05 04:55:09 +00:00
|
|
|
optns_pttrnSrch.StepTolerance = 1e-1;
|
|
|
|
|
optns_pttrnSrch.FunctionTolerance = 10;
|
|
|
|
|
optns_pttrnSrch.ConstraintTolerance = 1e-6;
|
2019-12-23 05:55:18 +00:00
|
|
|
optns_pttrnSrch.MaxTime = inf;
|
2019-12-19 10:31:18 +00:00
|
|
|
optns_pttrnSrch.MaxFunctionEvaluations = 1e+6;
|
2019-12-23 05:55:18 +00:00
|
|
|
|
2019-12-19 10:31:18 +00:00
|
|
|
[x,fval] = patternsearch(@(x)traj_cost_lgr(x,traj_par,baseQR), x0, ...
|
2019-12-19 06:48:18 +00:00
|
|
|
A, b, Aeq, beq, lb, ub, ...
|
2019-12-19 10:31:18 +00:00
|
|
|
@(x)traj_cnstr(x,traj_par), optns_pttrnSrch);
|
2019-12-19 06:48:18 +00:00
|
|
|
elseif strcmp(optmznAlgorithm, 'ga')
|
|
|
|
|
optns_ga = optimoptions('ga');
|
|
|
|
|
optns_ga.Display = 'iter';
|
|
|
|
|
optns_ga.PlotFcn = 'gaplotbestf'; % {'gaplotbestf', 'gaplotscores'}
|
|
|
|
|
optns_ga.MaxGenerations = 50;
|
|
|
|
|
optns_ga.PopulationSize = 1e+3; % in each generation.
|
|
|
|
|
optns_ga.InitialPopulationRange = [-100; 100];
|
|
|
|
|
optns_ga.SelectionFcn = 'selectionroulette';
|
2019-12-18 11:25:45 +00:00
|
|
|
|
2019-12-19 10:31:18 +00:00
|
|
|
[x,fval] = ga(@(x)traj_cost_lgr(x,traj_par,baseQR), 6*2*traj_par.N,...
|
2019-12-19 06:48:18 +00:00
|
|
|
A, b, Aeq, beq, lb, ub, ...
|
2019-12-23 05:55:18 +00:00
|
|
|
@(x)traj_cnstr(x,traj_par), optns_ga);
|
2019-12-19 06:48:18 +00:00
|
|
|
else
|
|
|
|
|
error('Chosen algorithm is not found among implemented ones');
|
|
|
|
|
end
|
2019-12-23 05:55:18 +00:00
|
|
|
|
2020-03-06 12:55:03 +00:00
|
|
|
% ------------------------------------------------------------------------
|
|
|
|
|
% Plotting obtained trajectory
|
2019-12-18 11:25:45 +00:00
|
|
|
% ------------------------------------------------------------------------
|
|
|
|
|
ab = reshape(x,[12,traj_par.N]);
|
|
|
|
|
a = ab(1:6,:); % sin coeffs
|
|
|
|
|
b = ab(7:12,:); % cos coeffs
|
2019-12-23 05:55:18 +00:00
|
|
|
c_pol = getPolCoeffs(traj_par.T, a, b, traj_par.wf, traj_par.N, traj_par.q0);
|
2019-12-19 06:48:18 +00:00
|
|
|
[q,qd,q2d] = mixed_traj(traj_par.t, c_pol, a, b, traj_par.wf, traj_par.N);
|
|
|
|
|
|
2019-12-18 11:25:45 +00:00
|
|
|
figure
|
|
|
|
|
subplot(3,1,1)
|
|
|
|
|
plot(traj_par.t,q)
|
2019-12-19 06:48:18 +00:00
|
|
|
ylabel('$q$','interpreter','latex')
|
|
|
|
|
grid on
|
2019-12-18 11:25:45 +00:00
|
|
|
legend('q1','q2','q3','q4','q5','q6')
|
|
|
|
|
subplot(3,1,2)
|
|
|
|
|
plot(traj_par.t,qd)
|
2019-12-19 06:48:18 +00:00
|
|
|
ylabel('$\dot{q}$','interpreter','latex')
|
|
|
|
|
grid on
|
2019-12-18 11:25:45 +00:00
|
|
|
legend('qd1','qd2','qd3','qd4','qd5','qd6')
|
|
|
|
|
subplot(3,1,3)
|
|
|
|
|
plot(traj_par.t,q2d)
|
2019-12-19 06:48:18 +00:00
|
|
|
ylabel('$\ddot{q}$','interpreter','latex')
|
|
|
|
|
grid on
|
|
|
|
|
legend('q2d1','q2d2','q2d3','q2d4','q2d5','q2d6')
|
|
|
|
|
|
2020-03-06 12:55:03 +00:00
|
|
|
% ----------------------------------------------------------------------
|
|
|
|
|
% Saving parameters of the optimized trajectory
|
|
|
|
|
% ----------------------------------------------------------------------
|
2020-02-05 04:55:09 +00:00
|
|
|
% %{
|
2020-03-06 12:55:03 +00:00
|
|
|
pathToFolder = 'trajectory_optmzn/optimal_trjctrs/';
|
2019-12-23 05:55:18 +00:00
|
|
|
t1 = strcat('N',num2str(traj_par.N),'T',num2str(traj_par.T));
|
2019-12-19 06:48:18 +00:00
|
|
|
if strcmp(optmznAlgorithm, 'patternsearch')
|
2020-02-05 04:55:09 +00:00
|
|
|
filename = strcat(pathToFolder,'ptrnSrch_',t1,'QR.mat');
|
2019-12-19 06:48:18 +00:00
|
|
|
elseif strcmp(optmznAlgorithm, 'ga')
|
|
|
|
|
filename = strcat(pathToFolder,'ga_',t1,'.mat');
|
2019-12-23 05:55:18 +00:00
|
|
|
elseif strcmp(optmznAlgorithm, 'fmincon')
|
|
|
|
|
filename = strcat(pathToFolder,'fmncn_',t1,'.mat');
|
2019-12-19 06:48:18 +00:00
|
|
|
end
|
|
|
|
|
save(filename,'a','b','c_pol','traj_par')
|
2020-02-05 04:55:09 +00:00
|
|
|
%}
|