Appendix: MATLAB Simulation Code: All All 'Temp - Avi'
Appendix: MATLAB Simulation Code: All All 'Temp - Avi'
Appendix: MATLAB Simulation Code: All All 'Temp - Avi'
% end
% for x = 55:3:70
% paramR(k,:) = [x y 2 1];
% k = k + 1;
% end
% m = 32;
% for n = 0:3:16
% paramR(k,:) = [m n 2 1];
% k = k + 1;
% end
% for m = 34:3:50
% paramR(k,:) = [m n 2 1];
% k = k + 1;
% end
% for m = 60:3:70
% paramR(k,:) = [m n 2 1];
% k = k + 1;
% end
% for n = 18:3:70
% paramR(k,:) = [m n 2 1];
% k = k + 1;
% end
% x = 30;
% for y = 30:3:50
% paramR(k,:) = [x y 2 1];
% k = k + 1;
% end
% o = 40;
% for p = 30:3:50
% paramR(k,:) = [o p 2 1];
% k = k + 1;
% end
% o = 50;
% for p = 0:3:15
% paramR(k,:) = [o p 2 1];
% k = k + 1;
% end
% o = 60;
% for p = 0:3:15
% paramR(k,:) = [o p 2 1];
% k = k + 1;
% end
% numRIn = k-1;
%============================%
83
target_location = [1 1];
target_location(1) = paramA(1,1);
target_location(2) = paramA(1,2);
% Link parameters
%========================================%
linkLen = 5;
linkLen12 = 5;
linkLen13 = 5;
varHead =50;
%========================================%
flag = 0;
tp0 = 1;
flag_two = 0;
stop1 = 0;
stop2 = 0;
km= 0.3; %angular velocity coefficient
%Simulation parameters: initial conditions
%========================================%
NSTEP = 600;
stepSize = 0.2; %====== Speed (rate)
x0 = [50 20]; %====== Start point
theta0 = pi*(-105/180.0);
theta012 = pi*(-120/180.0);
theta013 = pi*(-105/180.0);
%========================================%
x1 = x0;
theta1 = theta0;
theta12 = theta012;
theta13 = theta013;
%==== Position of links with respect to head =====%
xHead = x1;
xTail = xHead + [linkLen*cos(theta1), linkLen*sin(theta1)];
xTail12 = xTail + [linkLen12*cos(theta12), linkLen12*sin(theta12)];
xTail13 = xTail12 + [linkLen13*cos(theta13), linkLen13*sin(theta13)];
xtg(1,:) = x1;
xtgT(1,:) = xTail;
xtgT12(1,:) = xTail12;
xtgT13(1,:) = xTail13;
xtgTheta(1) = theta1;
xtgTheta12(1) = theta12;
84
xtgTheta13(1) = theta13;
flagr=0;
for k = 1:NSTEP
z = k;
% ===== for more links use loop
xHead = x1;
xTail = xHead + [linkLen*cos(theta1), linkLen*sin(theta1)];
xTail12 = xTail + [linkLen12*cos(theta12), linkLen12*sin(theta12)];
%=======for one link separation=== for more than one location or link use
loop=======%
if x1(1) < 57 && x1(1) > 53
if x1(2) <52 && x1(2) > 48
flagr = 1;
end
end
if flagr == 0
xTail13 = xTail12 + [linkLen13*cos(theta13), linkLen13*sin(theta13)];
end
%====Find Next Location and heading direction======%
if stop1 == 0
[theta1, theta12, theta13, x1] = Next_location(x1, theta1, theta12, theta13, paramA,
paramR, numRIn);
xtgTheta(k+1) = theta1;
xtgTheta12(k+1) = theta12;
xtgTheta13(k+1) = theta13;
xtg(k+1,:) = x1;
xtgT(k+1,:) = x1 + [linkLen*cos(theta1), linkLen*sin(theta1)];
xtgT12(k+1,:) = xtgT(k+1,:) + [linkLen12*cos(theta12), linkLen12*sin(theta12)];
if flagr == 0
xtgT13(k+1,:) = xtgT12(k+1,:) + [linkLen13*cos(theta13), linkLen13*sin(theta13)];
end
%====To find whether or not it is stuck.. at local minima===%
xtg3 = xtg(k+1,:);
xtg2 = xtg(k,:);
tp1 = xtg2(1);
85
tp2 = xtg3(1);
tp3 = xtg2(2);
tp4 = xtg3(2);
tp5(tp0) = tp2 - tp1;
tp6(tp0) = tp4 - tp3;
tp0 = tp0 + 1;
%=====target reached??!!====%
if abs(target_location(1) - tp1) < 0.1
if abs(target_location(2) - tp3) < 0.1
flag = 5;
end
end
if tp0 == 11
tp0 = 1;
[flag_two] = local_minima(flag_two,tp5,tp6);
end
if flag_two == 2
flag_two = 0;
[x1, theta1, theta12, theta13] = Simulated_annealing(numAIn, numRIn, xtg(k-1,:),
paramA, paramR, theta1, theta12, theta13);
end
end
figure(1)
xTrans = 0;
xmin=0;
xmax=100;
ymin=0;
ymax=100;
dx = 1;
dy = 1;
[xx,yy] = meshgrid(xmin:dx:xmax,ymin:dy:ymax);
V3 = V3Generate_field(numAIn, numRIn, xx,yy,paramA,paramR);
%========= To Increase speed of simulation =========%
if k > 250
if k < 300
hold on
contourf(xx + xTrans,yy,V3,1);%colormap
colormap cool
86
grid
end
end
hold off
%==============================%
plot(xtg(:,1)+xTrans,xtg(:,2),'r','linewidth',2)
hold on
%====show the position of robots at each step====%
if stop1 == 0
text(xtg(k,1), xtg(k,2), sprintf('%s%d','R',k),'FontSize',15)
end
hold on
plot([xTail(1),xHead(1)], [xTail(2),xHead(2)], 'bl','linewidth',5)
grid
plot([xTail12(1),xTail(1)], [xTail12(2),xTail(2)], 'bl','linewidth',5)
grid
if flagr == 0
plot([xTail13(1),xTail12(1)], [xTail13(2),xTail12(2)], 'bl', 'linewidth',5)
grid
end
if flagr == 1
plot([xTail13(1),(xTail13(1)+1)],[xTail13(2),(xTail13(2)+1)],'bl','linewidth',5)
grid
end
for variable_x = 1:numAIn
text(paramA(variable_x,1), paramA(variable_x,2), '@')
end
text(60,38,'R')
hold on
contour(xx + xTrans,yy,V3,1,'g','linewidth',2);%colormap
%colormap cool %====(to change color map, contour plots)====%
grid
hold on
plot(xtgT(:,1) + xTrans,xtgT(:,2),'g','linewidth',1)
grid
hold off
hold on
plot(xtgT12(:,1) + xTrans,xtgT12(:,2),'g','linewidth',1)
grid
hold off
87
hold on
plot(xtgT13(:,1) + xTrans,xtgT13(:,2),'g','linewidth',1)
grid
hold off
axis([20 70 0 70])
Mt = getframe;
if flag == 5
flag = 0;
stop1 = 1;
end
end
mov = close(mov)
88
89
BT2(2,1) = BT2(1,2);
[fTail13,gTail13,HTail13] = fnPF(numAInTail13, numRIn, xTail13, paramATail13,
paramR);
epslon = max( 0, delta - 0.5*( HTail13(1,1)+HTail13(2,2) - sqrt((HTail13(1,1)HTail13(2,2))^2+4*HTail13(1,2)^2) ) );
dtm = (epslon+HTail13(1,1))*(epslon+HTail13(2,2)) - HTail13(1,2)^2;
BT3(1,1) = (epslon+HTail13(2,2))/dtm;
BT3(2,2) = (epslon+HTail13(1,1))/dtm;
BT3(1,2) = -HTail13(1,2)/dtm;
BT3(2,1) = BT3(1,2);
% Orientation as the reference direction
refDirFlag = 1;
if refDirFlag == 1
refVect = [cos(theta1), sin(theta1)];
coordAngle = pi/2;
end
% Negative gradient of head as the reference direction
if refDirFlag ~= 1
%refVect = -g;
refVect = - (BT*gTail')';
coordAngle = -pi/2;
end
refRot = [cos(coordAngle), -sin(coordAngle); sin(coordAngle), cos(coordAngle)];
refVect2 = refRot*refVect';
inProd = -gTail(1)*refVect2(1) - gTail(2)*refVect2(2);
if inProd >= 0
omegaDir = 1;
else
omegaDir = -1;
end
%dk= -g; %Gradient
90
91
92
93
94
95
end
return
96
if (flag_three == 1)
continue
else
if f_random(S,:) <= f_best
f_best = f_random(S,:);
R = S;
end
end
end
%====== finding the delta E , to check 0.2 <= e^(-deltaE / T) ======%
delta_f = f_best - f_minima;
f_temp = delta_f / temp;
x_random(R,:)
if delta_f < 0
x21 = x1;
x1 = x_random(R,:);
hold off
%=== Move towards the local target ===%
[theta1,theta12,theta13] = local_goal(x1, x21, theta1, theta12, theta13, paramR);
no_solution = 1;
break
elseif (0 <= delta_f) && (delta_f <= 0.99)
if (f_temp <= 1.60) && (f_best < 1)
x21 = x1;
x1 = x_random(R,:);
hold off
%=== Move towards the local target ===%
[theta1,theta12,theta13] = local_goal(x1, x21, theta1, theta12, theta13, paramR);
else
no_solution1 = 1;
break
end
else
no_solution2 = 1;
break
end
end
97
return
98
99
100
xtgTheta(1) = theta1;
xtgTheta12(1)=theta12;
xtgTheta13(1)=theta13;
for k2 = 1:NSTEP2
xHead = x1;
xTail = xHead + [linkLen*cos(theta1), linkLen*sin(theta1)];
xTail12 = xTail + [linkLen12*cos(theta12), linkLen12*sin(theta12)];
xTail13 = xTail12 + [linkLen13*cos(theta13), linkLen13*sin(theta13)];
[f,g,H] = fnPF(numAIn, numRIn, x1,paramA,paramR);
paramATail = [xHead(1),xHead(2),varHead];
numAInTail = 1;
[fTail,gTail,HTail] = fnPF(numAInTail, numRIn, xTail, paramATail, paramR);
paramATail12 = [xTail(1),xTail(2),varHead];
numAInTail12 = 1;
[fTail12,gTail12,HTail12] = fnPF(numAInTail12, numRIn, xTail12, paramATail12,
paramR);
paramATail13 = [xTail12(1),xTail12(2),varHead];
numAInTail13 = 1;
[fTail13,gTail13,HTail13] = fnPF(numAInTail13, numRIn, xTail13, paramATail13,
paramR);
% Orientation as the reference direction
refDirFlag = 1;
if refDirFlag == 1
refVect = [cos(theta1), sin(theta1)];
coordAngle = pi/2;
end
% Negative gradient of head as the reference direction
if refDirFlag ~= 1
refVect = -g;
coordAngle = -pi/2;
end
refRot = [cos(coordAngle), -sin(coordAngle); sin(coordAngle), cos(coordAngle)];
refVect2 = refRot*refVect';
inProd = -gTail(1)*refVect2(1) - gTail(2)*refVect2(2);
if inProd >= 0
omegaDir = 1;
101
else
omegaDir = -1;
end
dk= -g; %Gradient
% Orientation as the reference direction Link 2
refDirFlag12 = 1;
if refDirFlag12 == 1
refVect12 = [cos(theta12), sin(theta12)];
coordAngle12 = pi/2;
end
% Negative gradient of head as the reference direction
if refDirFlag12 ~= 1
refVect12 = -g;
coordAngle12 = -pi/2;
end
refRot12 = [cos(coordAngle12), -sin(coordAngle12); sin(coordAngle12),
cos(coordAngle12)];
refVect122 = refRot12*refVect12';
inProd12 = -gTail12(1)*refVect122(1) - gTail12(2)*refVect122(2);
if inProd12 >= 0
omegaDir12 = 1;
else
omegaDir12 = -1;
end
dk= -g; %Gradient
% Orientation as the reference direction Link 3
refDirFlag13 = 1;
if refDirFlag13 == 1
refVect13 = [cos(theta13), sin(theta13)];
coordAngle13 = pi/2;
end
% Negative gradient of head as the reference direction
if refDirFlag13 ~= 1
refVect13 = -g;
coordAngle13 = -pi/2;
end
refRot13 = [cos(coordAngle13), -sin(coordAngle13); sin(coordAngle13),
cos(coordAngle13)];
refVect123 = refRot13*refVect13';
102
xtg(k2+1,:) = x1;
xtgT(k2+1,:) = x1 + [linkLen*cos(theta1), linkLen*sin(theta1)];
xtgT12(k2+1,:) = xtgT(k2+1,:) + [linkLen12*cos(theta12), linkLen12*sin(theta12)];
xtgT13(k2+1,:) = xtgT12(k2+1,:) + [linkLen13*cos(theta13), linkLen13*sin(theta13)];
figure(1)
xTrans = 0;
xmin=0;
xmax=100;
ymin=0;
ymax=100;
dx = 1;
dy = 1;
[xx,yy] = meshgrid(xmin:dx:xmax,ymin:dy:ymax);
V3 = V3Generate_field(numAIn, numRIn, xx,yy,paramA2,paramR);
103
hold on
contourf(xx + xTrans,yy,V3,1);%colormap
colormap cool
grid
%hold off
plot(xtg(:,1)+xTrans,xtg(:,2),'r','linewidth',2)
hold on
%show the position of robots at each step
text(xtg(k2,1), xtg(k2,2), sprintf('%s%d','R',k2),'FontSize',15)
hold on
plot([xTail(1),xHead(1)], [xTail(2),xHead(2)], 'bl','linewidth',5)
grid
plot([xTail12(1),xTail(1)], [xTail12(2),xTail(2)], 'bl','linewidth',5)
grid
plot([xTail13(1),xTail12(1)], [xTail13(2),xTail12(2)], 'bl','linewidth',5)
grid
for variable_x = 1:numAIn
text(paramA2(variable_x,1), paramA2(variable_x,2), '@')
end
hold on
plot(xtgT(:,1) + xTrans,xtgT(:,2),'g','linewidth',1)
grid
hold off
hold on
plot(xtgT12(:,1) + xTrans,xtgT12(:,2),'g','linewidth',1)
grid
hold off
hold on
plot(xtgT13(:,1) + xTrans,xtgT13(:,2),'g','linewidth',1)
grid
hold off
axis([20 70 0 70])
Mt = getframe;
end
return
104
105
106