FormationControlSimulation/SOURCE/SM_2015_Rozenheck.m

118 lines
3.3 KiB
Mathematica
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
conRobot = [1 2 1;
3 2 1;
3 1 1;
2019-07-10 13:34:08 +07:00
];
length_d = .5;
2019-07-10 13:34:08 +07:00
dRobot = [ 1;
1;
1;
];
2020-07-29 11:31:38 +07:00
corRobot = [1.5; 1.7;0;
2; 2.5;0;
2.5; 2.8;0;
]; % xy xy xy
2020-07-29 11:31:38 +07:00
robStateLen = length(corRobot)/length(dRobot)
2019-06-20 14:36:48 +07:00
B = [
1 0; 0 1;
0 0; 0 0;
0 0; 0 0;
];
kp = 80;
ki = 1;
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);
2020-07-28 13:32:25 +07:00
h = .1;
tspan = 1:h:10;
2020-07-29 11:31:38 +07:00
sys = formationControl2ndBuilder(conRobot,robStateLen,kp,kp,ki,ki,length_d,1);
2019-12-18 14:23:12 +07:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% config nang nduwur
2020-07-29 11:31:38 +07:00
[R,K,d] = rigidityMatrixFnc(conRobot,robStateLen);
2019-07-10 13:34:08 +07:00
_zero = zeros(size(R(corRobot,K),1),1);
sInit = [corRobot; _zero;];
sAnsInit =[_zero; _zero;];
s2ndInit = [corRobot; %x1
zeros(length(corRobot),1); %x2
zeros(length(corRobot),1); %xi1
_zero]; %xi2
2019-12-18 14:23:12 +07:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% start solving
printf("Mulai memecakan masalah \n\n");
startExe = tic;
2020-07-28 13:32:25 +07:00
dydt = @(t, y) sys.ss(y,fvref(t));
[t,y] = ode45(dydt, tspan, s2ndInit);
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
2019-06-20 14:36:48 +07:00
plot_con(plot_rb, y', conRobot, xrb, yrb, 1);
% plot_con(plot_rb, y', conRobot, xrb, yrb, round(length(tspan)/2));
2019-06-20 14:36:48 +07:00
plot_con(plot_rb, y', conRobot, xrb, yrb, length(tspan));
2019-07-10 13:34:08 +07:00
str_tmp = "legend(";
for i = 1:numNodes(edgeL2adjL(conRobot))-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
2020-07-28 13:32:25 +07:00
csvwrite("DataOutMotion.csv", [t y])
% save DataOutMotion.data y
% save DataErrorEdge.data yans