-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrajectory_generator_apf_with_plot.m
319 lines (219 loc) · 8.94 KB
/
trajectory_generator_apf_with_plot.m
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
function trajectory= trajectory_generator_apf_with_plot(robot_vel, robot_pos, h)
% Generate a Universal Potential Field which directs a robot to its
% destination via an Attractive Potential Field (with its minimum at goal
% point) and keeps it away from obstacles via a Repulsive Potential Field
% (with its peaks at obstacle locations).
% Configuration Space: x-axis- 0...50
% y-axis- 0...50
% INPUTS
% robot_vel (1x1)- magnitude of longitudinal robot velocity
% robot_pos (2x1)- robot position
% h- Sampling period
%% Constant Definition
% Attractive Potential Constant
k_att= evalin('base','k_att');
% Repulsive Potential Constant
k_rep= evalin('base','k_rep');
% Obstacle Radius (Assuming circular obstacle of constant radius) (m)
obs_radius= evalin('base','obs_radius');
% Maximum Longitudinal Velocity
V_MAX= evalin('base','V_MAX');
% Obstacle Influence Range
rol_not= evalin('base','rol_not');
% Stopping Criteria
stopping_criteria= evalin('base','stopping_criteria');
% Robot Size Alllowance
robot_size_allowance= evalin('base','robot_size_allowance');
%% Define Goal
X_goal= evalin('base','X_goal');
%% Plot Attractive Potential
x_plot_sub= 0:0.2:50;
y_plot_sub= 0:0.2:50;
[x_plot, y_plot]= meshgrid(x_plot_sub, y_plot_sub);
z_plot_att= 0.5 .* k_att .* ( (x_plot-X_goal(1)).^2 + (y_plot-X_goal(2)).^2 );
% mesh(x_plot, y_plot, z_plot_att);
% Hold Plot
% hold on
%% Define Obstacle Positions
obstacles= evalin('base','obstacles');
%% Plot Universal Potential
% Maximum Repulsive Potential
max_rep_pot= max(max(z_plot_att/2));
% Initialize to Attractive Potential
z_plot_uni= z_plot_att;
% Initialize Obstacle Counter
obs_counter= 1;
% Get number of obstacles (number of columns of *obstacles*)
size_of_obstacles= size(obstacles);
no_of_obstacles= size_of_obstacles(2);
% Loop through obstacles
while (obs_counter <= no_of_obstacles)
% Current Obstacle
current_obs= obstacles(:, obs_counter);
% Loop through y_plot_sub
for y_index= 1:length(y_plot_sub)
% Loop through x_plot_sub
for x_index= 1:length(x_plot_sub)
% Calculate Minimum Distance to obstacle
min_dis_plot= sqrt( (x_plot_sub(x_index)-current_obs(1))^2 + ...
(y_plot_sub(y_index)-current_obs(2))^2 )- ...
(obs_radius + robot_size_allowance);
% If point is within obstacle range
if(min_dis_plot <= rol_not)
% If minimum obstacle distance is zero or negative (i.e. robot is at obstacle)
if(min_dis_plot <= 0)
%Set maximum repulsive potential
z_plot_temp= max_rep_pot;
% Add to Universal Potential
z_plot_uni(y_index, x_index)= z_plot_uni(y_index, x_index) + z_plot_temp;
% Otherwise
else
% Compute Repulsive Potential
z_plot_temp= 0.5 * k_rep * ( (1/min_dis_plot) -...
(1/rol_not) )^2;
% Clip Repulsive Potential to max of attractive potential
if(z_plot_temp > max_rep_pot)
z_plot_temp= max_rep_pot;
end
% Add to Universal Potential
z_plot_uni(y_index, x_index)= z_plot_uni(y_index, x_index) + z_plot_temp;
end
end
end
end
obs_counter= obs_counter + 1;
end
% Plot Universal Potential for obstacles
subplot(1, 2, 1);
mesh(x_plot, y_plot, z_plot_uni);
title('Universal Potential Field');
xlabel("Longitudinal Axis [fixed earth](m)");
ylabel("Lateral Axis [fixed earth](m)");
hold on
%% Robot Position
X_robot= robot_pos;
% Plot Robot initial position
pos_plot= plot3(X_robot(1), X_robot(2), max(max(z_plot_att)), '*m', 'MarkerSize', 13 );
hold on
% Plot marker at goal point
goal_plot= plot(X_goal(1), X_goal(2), '*r', 'MarkerSize', 13 );
legend([pos_plot goal_plot], "Start Position", "Goal Position");
legend("Location", "northwest");
% Initialize trajectory. Preallocate zeros to optimize run speed
trajectory= zeros(3, 1000);
yaw_ang= zeros(3, 10000);
% Initailize timestep
trajectory_index= 1;
timestep= trajectory_index-1;
% Plot Universal Potential upon which planned path will be plotted
subplot(1, 2, 2);
mesh(x_plot, y_plot, z_plot_uni);
title('Path Planning- Gradient Descent');
xlabel("Longitudinal Axis [fixed earth](m)");
ylabel("Lateral Axis [fixed earth](m)");
%colorbar
hold on
%% Forever Loop
iteration_count= 0;
max_iterations= evalin('base', 'max_iterations');
while 1
% Calculate Attractive Potential and Force
Ua= 0.5 .* k_att .* (X_robot - X_goal)' * (X_robot - X_goal);
%fprintf('Ua: %.2f\n', Ua);
Fa= -k_att .* (X_robot - X_goal);
%fprintf('Fa: %.2f\n', Fa);
% Initialize Repulsive Potential and Force to zero
Ur= 0;
Fr= 0;
% Initialize Obstacle Counter
obs_counter= 1;
% Loop through obstacles
while (obs_counter <= no_of_obstacles)
% Current Obstacle
current_obs= obstacles(:, obs_counter);
% Calculate minimum distance to obstacle center
obs_min_dis= sqrt( (X_robot - current_obs)' * ...
(X_robot - current_obs) ...
);
% Factor in obstacle radius
obs_min_dis= obs_min_dis - (obs_radius + robot_size_allowance);
% Check if Robot is within obstacle influence range
if(obs_min_dis <= rol_not)
% If yes, calculate Repulsive Potential for obstacle
Ur_obs= 0.5 * k_rep * ( (1/obs_min_dis) - (1/rol_not) )^2;
% Calculate Repulsive FOrce for obstacle
Fr_obs= k_rep .* ( (1/obs_min_dis) - (1/rol_not) ) .* ...
(1/(obs_min_dis^3)) .* (X_robot - current_obs);
% Update Repulsive Potential and Force
Ur= Ur + Ur_obs;
Fr= Fr + Fr_obs;
%fprintf('Fr: %.2f\n', Fr);
end
% Increment obstacle counter
obs_counter= obs_counter + 1;
end
% Evaluate Universal Potential
Uuni= Ua + Ur;
%fprintf('Uuni: %.2f\n', Uuni);
% Check if stopping criteria is satisfied
if(Ua <= stopping_criteria)
% break the forever loop
break;
end
iteration_count= iteration_count+1;
% Check if maximum number of iterations has been reached
if(iteration_count >= max_iterations)
fprintf('Number of maximum iterations reached\n')
% break the forever loop
break;
end
% If stopping criteria is not satisfied, Evaluate Universal Force
Funi= Fa + Fr;
% Log Current Robot Position into trajectory
trajectory(1:3,trajectory_index)= [(timestep*h) X_robot(1) X_robot(2)]';
%fprintf( 'Robot position: [%.2f,%.2f]\n\n', X_robot(1), X_robot(2) );
% Scale Movement
movement= scale_movement(Funi, V_MAX, h);
% Yaw Angle
yaw_ang(:, trajectory_index)= compute_yaw_angle(movement);
% Update Robot Position
X_robot= X_robot + movement;
% Update timestep
trajectory_index= trajectory_index + 1;
timestep= timestep + 1;
% Plot Robot Position and corresponding Universal Potential
plot3(X_robot(1), X_robot(2), Uuni, '*k', 'MarkerSize', 10);
% Plot Robot Position and corresponding Attractive Potential
% plot3(X_robot(1), X_robot(2), Ua, '*k', 'MarkerSize', 10);
% Repeat Loop
end
% TARGET CONVERGENCE ADJUSTMENT
% LOG FINAL POSITION AS LAST 5 POINTS ON TRAJECTORY TO ALLOW 2 EXTRA
% TIMESTEPS FOR CONVERGENCE
% iteration_count= 0;
%
% while(iteration_count <= 5)
%
% trajectory(1:3,trajectory_index)= [(timestep*h) X_goal(1) X_goal(2)]';
% % Update timestep
% trajectory_index= trajectory_index + 1;
% timestep= timestep + 1;
%
% iteration_count= iteration_count+1;
% end
last_pos= plot3(X_robot(1), X_robot(2), Uuni, '*k', 'MarkerSize', 10);
% Legend Robot Position
legend(last_pos, "Robot Position");
legend("Location", "northwest");
% Release Plot
hold off
% Set all zero columns to empty vectors, effectively removing them from the
% array
trajectory( :, all(~trajectory,1) )= [];
% Insert starting point back into the trajectory (in case it is [0 0] and gets removed)
if (robot_pos(1)==0) && (robot_pos(2)==0)
trajectory= [ [0 0 0]' trajectory ];
end
yaw_ang= yaw_ang(1, 1:size(trajectory,2));
assignin('base', 'yaw_ang', yaw_ang);
end