-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunAPF.m
More file actions
143 lines (116 loc) · 4.27 KB
/
RunAPF.m
File metadata and controls
143 lines (116 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
clear all
close all
clc
%
% rng(7,"twister");
omap3D = occupancyMap3D(1); %keep this as 1 to work
mapWidth = 30; %meters
mapLength = 30;
numberOfObstacles = 45;
obstacleNumber = 1;
while obstacleNumber <= numberOfObstacles
width = randi([1 5],1);
lengthh = randi([1 5],1); % can be changed as necessary to create different occupancy maps.
height = randi([1 5],1);
xPosition = randi([0 mapWidth-width],1);
yPosition = randi([0 mapLength-lengthh],1);
[xObstacle,yObstacle,zObstacle] = meshgrid(xPosition:xPosition+width,yPosition:yPosition+lengthh,0:height);
xyzObstacles = [xObstacle(:) yObstacle(:) zObstacle(:)];
checkIntersection = false;
for i = 1:size(xyzObstacles,1)
if checkOccupancy(omap3D,xyzObstacles(i,:)) == 1
checkIntersection = true;
break
end
end
if checkIntersection
continue
end
setOccupancy(omap3D,xyzObstacles,1)
obstacleNumber = obstacleNumber + 1;
end
% ground as obstacle
[xGround,yGround,zGround] = meshgrid(0:mapWidth,0:mapLength,0);
xyzGround = [xGround(:) yGround(:) zGround(:)];
setOccupancy(omap3D,xyzGround,1)
rho0 = 0.3; %distance of influence, adaptative given velocity and freespace?
ka = 1;
krep = 1;
goal.pos=[20 20 6];
box = collisionBox(0.4,0.4,0.1); % drone as a box XYZ
currentpos=[0.5 0.5 2];
box.Pose = trvec2tform(currentpos);
movement=[0 0 0];
Xhistory=0;
Yhistory=0;
Zhistory=0;
fh = figure();
fh.WindowState = 'maximized';
pause(1)
while norm(goal.pos-currentpos)>0.1
bpOpts = occupancyMap3DCollisionOptions(ReturnDistance=true);
[collisionStatus,details] = checkMapCollision(omap3D,box,bpOpts);
rho = details.DistanceInfo.Distance;
h=0.01;
trvec = tform2trvec(box.Pose);
box.Pose = trvec2tform([trvec(1) trvec(2) trvec(3)]);
newX = trvec2tform([trvec(1)+h trvec(2) trvec(3)]);
boxCopyx = collisionBox(box.X,box.Y,box.Z);
boxCopyx.Pose=box.Pose;
boxCopyx.Pose=newX;
[collisionStatus,detailsX] = checkMapCollision(omap3D,boxCopyx,bpOpts);
rhoX = detailsX.DistanceInfo.Distance;
newY = trvec2tform([trvec(1) trvec(2)+h trvec(3)]);
boxCopyy = collisionBox(box.X,box.Y,box.Z);
boxCopyy.Pose=box.Pose;
boxCopyy.Pose=newY;
[collisionStatus,detailsY] = checkMapCollision(omap3D,boxCopyy,bpOpts);
rhoY = detailsY.DistanceInfo.Distance;
newZ = trvec2tform([trvec(1) trvec(2) trvec(3)+h]);
boxCopyz = collisionBox(box.X,box.Y,box.Z);
boxCopyz.Pose=box.Pose;
boxCopyz.Pose=newZ;
[collisionStatus,detailsZ] = checkMapCollision(omap3D,boxCopyz,bpOpts);
rhoZ = detailsZ.DistanceInfo.Distance;
% forward difference for gradient
gradrho = [(rhoX-rho)/h (rhoY-rho)/h (rhoZ-rho)/h];
bpWitnessptsSphere = details.DistanceInfo.WitnessPoints;
show(omap3D)
hold on
show(box)
plot3(bpWitnessptsSphere(1,:),bpWitnessptsSphere(2,:),bpWitnessptsSphere(3,:),LineWidth=2,Color='r')
state.pos=[trvec(1) trvec(2) trvec(3)];
Fa = -ka*(state.pos-goal.pos);
if rho>rho0
Fr = [0 0 0];
else
Fr = krep*(((1/rho)-(1/rho0))*(1/rho)^2)*gradrho;
end
% centerobstacle = bpWitnessptsSphere(:,1)';
% Rspin = cross((goal.pos-state.pos),(centerobstacle-state.pos));
%plot3([box.X box.X+10*Fr(1)],[box.Y box.Y+10*Fr(2)],[box.Z box.Z+10*Fr(3)],LineWidth=2,Color='k')
[X,Y,Z] = sphere;
r = 0.2;
X = X * r;
Y = Y * r;
Z = Z * r;
surf(X+goal.pos(1),Y+goal.pos(2),Z+goal.pos(3),'FaceColor','k','EdgeColor','none')
quiver3(trvec(1),trvec(2),trvec(3),Fr(1)/norm(Fr),Fr(2)/norm(Fr),Fr(3)/norm(Fr),LineWidth=3,Color='k')
quiver3(trvec(1),trvec(2),trvec(3),Fa(1)/norm(Fa),Fa(2)/norm(Fa),Fa(3)/norm(Fa),LineWidth=3,Color='b')
quiver3(trvec(1),trvec(2),trvec(3),(Fr(1)+Fa(1))/norm(Fa+Fr),(Fr(2)+Fa(2))/norm(Fa+Fr),(Fr(3)+Fa(3))/norm(Fa+Fr),LineWidth=3,Color='y')
% quiver3(trvec(1),trvec(2),trvec(3),Rspin(1)/norm(Rspin),Rspin(2)/norm(Rspin),Rspin(3)/norm(Rspin),LineWidth=3,Color='g')
axis equal
plot3(Xhistory,Yhistory,Zhistory,'r')
view(-15.6658,30.0343)
legend({'Obstacle','Obstacle','Nearest Obstacle','Goal','Repulsive Force','Attractive Force','Total Force','Path'})
hold off
movement = (Fr+Fa);
movement = 0.1*movement/norm(movement);
trvec = trvec+movement;
currentpos = [trvec(1) trvec(2) trvec(3)];
box.Pose=trvec2tform([trvec(1) trvec(2) trvec(3)]);
Xhistory=[Xhistory trvec(1)];
Yhistory=[Yhistory trvec(2)];
Zhistory=[Zhistory trvec(3)];
pause(0.001)
end