FormationControlSimulation/SOURCE/SM_2015_Rozenheck.m

158 lines
4.5 KiB
Matlab
Raw Normal View History

clear -all
2020-07-28 13:32:25 +07:00
graphics_toolkit('gnuplot')
2019-12-18 14:23:12 +07:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% config nang kene
algo = 0;
d = [[]];
ang = [];
save_state = [];
save algo_info.m algo;
save algo_info.m "-append" d ang save_state;
bParam = struct();
bParam.model = omnidirectional(0.94, 0.96, 0.01, 2.2, 1.5,0.099,0.1,1.5,0.025,30,0.035,19,0.00011,1.69,0.0059,0.0059,0.9,9.1,6,'./');
bParam.edgeL = [1 2 1;
3 2 1;
3 1 1;
];
bParam.dScale = 3;
2021-04-08 19:37:14 +07:00
bParam.kp1 = 80;
bParam.kp2 = 7;
bParam.ki1 = 0;
bParam.ki2 = 0;
bParam.nNodeVref = 1;
corRobot = [0; 0;0;
2021-04-08 19:37:14 +07:00
1; 2;0;
3; -3;0;
]; % xy xy xy
robStateLen = size(bParam.model.origin.A,1);
2019-06-20 14:36:48 +07:00
B = [
1 0; 0 1;
0 0; 0 0;
0 0; 0 0;
];
2020-07-29 11:31:38 +07:00
% fvref = fncSpeedRef('ysin',50,2,robStateLen);
% fvref = fncSpeedRef('xsin',50,2,robStateLen);
% fvref = fncSpeedRef('cw',-300,1,robStateLen);
% fvref = fncSpeedRef('s',50,50,robStateLen);
fvref = fncSpeedRef('s',0,0,robStateLen);
h = .01;
2021-04-08 19:37:14 +07:00
tspan = 1:h:20;
2021-04-08 19:37:14 +07:00
## sys = formationControl2ndBuilder(bParam);
% sys = formationControl1stBuilder(bParam);
sys = formationControl2ndModelBuilder(bParam);
% sys = formationControl1stModelBuilder(bParam);
2019-12-18 14:23:12 +07:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% config nang nduwur
2021-01-29 10:29:16 +07:00
_zero = zeros(size(sys.R(corRobot,sys.K),1),1);
2019-07-10 13:34:08 +07:00
sInit = [corRobot; _zero;];
s1stInit = [corRobot; zeros(size(corRobot)); zeros(size(sys.d))];
2021-01-29 10:29:16 +07:00
s2ndInit = [corRobot;zeros(length(corRobot),1); zeros(length(corRobot),1); _zero;];
s2ndModelInit = [corRobot; zeros(length(corRobot),1); zeros(length(corRobot),1); _zero; zeros(length(corRobot),1);];
2021-04-08 19:37:14 +07:00
## state_init = s2ndInit;
% state_init = sInit;
state_init = s2ndModelInit;
% state_init = s1stInit;
2019-12-18 14:23:12 +07:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% start solving
printf("Mulai memecakan masalah \n\n");
startExe = tic;
% continue
dydt = @(t, y) algo_init(sys,y,fvref(t));
2021-04-08 19:37:14 +07:00
##dydt = @(t, y) sys.ss(y,fvref(t));
2021-01-29 10:29:16 +07:00
[t,y] = ode45(dydt, tspan, state_init);
% discrate taylor methode
2021-04-08 19:37:14 +07:00
## dydt = @(t, y) sys.dss(y, h, fvref(t));
## t(1,:) = tspan(1);
## y(1,:) = dydt(t(1), state_init);
## for i = 2:length(tspan)
## t(i,:) = tspan(i);
## y(i,:) = dydt(t(i),y(i-1,:)');
## endfor
2021-01-29 10:29:16 +07:00
Dd(1,:) = [norm((sys.K*y(1,1:9)')(1:3)) norm((sys.K*y(1,1:9)')(4:6)) norm((sys.K*y(1,1:9)')(7:9))]';
for i = 2:length(tspan)
2021-01-29 10:29:16 +07:00
Dd(i,:) = [norm((sys.K*y(i,1:9)')(1:3)) norm((sys.K*y(i,1:9)')(4:6)) norm((sys.K*y(i,1:9)')(7:9))]';
endfor
2019-07-10 13:34:08 +07:00
2019-12-18 14:23:12 +07:00
endExe = toc(startExe);
printf("Membutuhkan waktu %i menit, %i detik \n untuk memecahkan masalah mu \n\n",
floor(endExe/60), rem(endExe,60))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% End Solving
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% start plot it
close all
2019-07-10 13:34:08 +07:00
figure(1)
2019-12-18 14:23:12 +07:00
% plot trayektori dari setiap robot
str_tmp = "plot(";
2020-07-29 11:31:38 +07:00
xi = 1:robStateLen:length(corRobot)-1
yi = 2:robStateLen:length(corRobot)
for i = 1:sys.n-1
str_tmp = strcat(str_tmp,sprintf("y'(%i,:),y'(%i,:),",xi(i),yi(i)));
endfor
2020-07-29 11:31:38 +07:00
str_tmp = strcat(str_tmp,sprintf("y'(%i,:),y'(%i,:));",xi(i+1),yi(i+1)));
eval(str_tmp);
2019-06-20 14:36:48 +07:00
hold on
2019-12-18 14:23:12 +07:00
% membuat fungsi plot robot di waktu
% tertentu
2019-06-20 14:36:48 +07:00
str_tmp = "@(t) plot( [";
2020-07-29 11:31:38 +07:00
for i = 1:robStateLen:length(corRobot)
str_tmp = strcat( str_tmp, sprintf("y'(%i,t), ",i));
endfor
str_tmp = strcat( str_tmp, sprintf("], ["));
2020-07-29 11:31:38 +07:00
for i = 2:robStateLen:length(corRobot)
str_tmp = strcat( str_tmp, sprintf("y'(%i,t), ",i));
endfor
str_tmp = strcat( str_tmp, sprintf("], \"^\");"));
2019-06-20 14:36:48 +07:00
plot_rb = eval(str_tmp);
2020-07-29 11:31:38 +07:00
xrb = 1:robStateLen:length(corRobot);
yrb = 2:robStateLen:length(corRobot);
2019-12-18 14:23:12 +07:00
% fungsi untuk plot coneksi di waktu
% tertentu
2019-06-20 14:36:48 +07:00
function plot_con (pltRb, yOut, conIn,xm,ym, time)
pltRb(time);
for i = 1:length(conIn)
plot([yOut(xm(conIn(i, 1)),time), yOut(xm(conIn(i, 2)),time)],
[yOut(ym(conIn(i,1)),time), yOut(ym(conIn(i,2)),time)], "r" )
endfor
endfunction
plot_con(plot_rb, y', bParam.edgeL, xrb, yrb, 1);
% plot_con(plot_rb, y', bParam.edgeL, xrb, yrb, round(length(tspan)/2));
plot_con(plot_rb, y', bParam.edgeL, xrb, yrb, length(tspan));
2019-07-10 13:34:08 +07:00
str_tmp = "legend(";
for i = 1:numNodes(edgeL2adjL(bParam.edgeL))-1;
2019-12-18 14:23:12 +07:00
str_tmp =strcat(str_tmp,sprintf("\"R%i \", ",i));
2019-07-10 13:34:08 +07:00
endfor
2019-12-18 14:23:12 +07:00
str_tmp =strcat(str_tmp,sprintf("\"R%i \" )",++i));
2019-07-10 13:34:08 +07:00
eval(str_tmp)
2019-12-18 14:23:12 +07:00
title("Motion dari Robot");
2019-07-10 13:34:08 +07:00
figure(2)
plot(t,Dd(:,1),t,Dd(:,2),t,Dd(:,3))
title("distance error")
legend()
2021-04-08 19:37:14 +07:00
csvwrite("DataOutMotion.csv", [t y(:,1:9)])
csvwrite("DataErrorEdge.csv", [t Dd])
save DataOutMotion.data y(:,1:9)
save DataErrorEdge.data Dd