-
Notifications
You must be signed in to change notification settings - Fork 8
/
run_dd.m
253 lines (219 loc) · 8.52 KB
/
run_dd.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
% main run for multi-robot collision avoidance using voronoic cell-based
% method, two-dimensional
%% clean workspace
clear
clc
% seed_num = 0;
% rng(seed_num, 'twister');
%% Choose mode
mode_sim = 1; % 0: experiment; 1: simulation
mode_region = 1; % 0: BVC; 1: BUAVC
mode_control = 0; % 0: reactive
%% initilization
initialize_dd;
if pr.simMode == 0 % exp
setROS;
end
%% create a multi-robot system
System = CSystem(nRobot, nBoxObs, pr);
%% robot initial state
for iRobot = 1 : nRobot
% pos
System.MultiRobot_{iRobot}.pos_real_ = robotStartPos(:, iRobot);
System.MultiRobot_{iRobot}.pos_est_ = robotStartPos(:, iRobot);
System.MultiRobot_{iRobot}.yaw_real_ = 0 + (iRobot-1)*2*pi/nRobot;
if pr.simMode == 0 % exp
System.MultiRobot_{iRobot}.initializeROS();
end
% pos cov
System.MultiRobot_{iRobot}.pos_measure_cov_ = robotPosNoise(:, :, iRobot);
% goal
System.MultiRobot_{iRobot}.goal_final_ = robotEndPos(:, iRobot);
System.MultiRobot_{iRobot}.goal_current_= robotEndPos(:, iRobot);
end
%% box obs initial state
for jBoxObs = 1 : nBoxObs
% pos
System.MultiBoxObs_{jBoxObs}.pos_real_ = boxPos(:, jBoxObs);
System.MultiBoxObs_{jBoxObs}.pos_est_ = boxPos(:, jBoxObs);
% pos cov
% TODO
% size
System.MultiBoxObs_{jBoxObs}.size_ = boxSize(:, jBoxObs);
% yaw
System.MultiBoxObs_{jBoxObs}.yaw_ = boxYaw(:, jBoxObs);
end
%% visulizaton initialization
for i = 1 % for wrapping up
color_robot = cell(1, nRobot);
if nRobot <= 7
color_robot(1, 1:7) = {'r', 'g', 'b', 'm' , 'c', 'k', 'y'};
else
color_robot(1, 1:7) = {'r', 'g', 'b', 'm' , 'c', 'k', 'y'};
for iRobot = 8 : nRobot
color_robot(1, iRobot) = {rand(1,3)};
end
end
fig_main = figure; % main figure
hold on;
axis equal
ax_main = fig_main.CurrentAxes;
if pr.dim == 2
axis([pr.ws(1,:), pr.ws(2,:)]);
elseif pr.dim == 3
axis([pr.ws(1,:), pr.ws(2,:), pr.ws(3,:)]);
view(ax_main, 3);
end
box on;
grid on;
% plot static obstacles
fig_box_obs = cell(nBoxObs, 1);
for jBoxObs = 1 : nBoxObs
fig_box_obs{jBoxObs} = plot_box(ax_main, pr.dim, boxPos(:, jBoxObs), ...
boxSize(:, jBoxObs), boxYaw(:, jBoxObs), ...
'FaceColor', [0.4 0.4 0.4], 'FaceAlpha', 0.6, ...
'EdgeColor', 'k', 'EdgeAlpha', 0.8);
end
% plot robot initial positions with figure handles
fig_robot_pos = cell(nRobot, 1);
for iRobot = 1 : nRobot
if pr.dim == 2
fig_robot_pos{iRobot} = plot_ellipse_2D(ax_main, System.MultiRobot_{iRobot}.pos_real_, ...
System.MultiRobot_{iRobot}.radius_.*[1;1], 0, ...
'EdgeColor', color_robot{iRobot}, ...
'FaceColor', color_robot{iRobot});
elseif pr.dim == 3
fig_robot_pos{iRobot} = plot_ellipsoid_3D(ax_main, System.MultiRobot_{iRobot}.pos_real_, ...
System.MultiRobot_{iRobot}.radius_.*[1;1;1], 0, ...
'EdgeColor', color_robot{iRobot}, ...
'FaceColor', color_robot{iRobot});
end
end
% plot robot obstacle-free convex region with figure handles
fig_robot_region = cell(nRobot, 1);
if cfg.ifShowSafeRegion
for iRobot = 1 : nRobot
pgon = polyshape(); % an empty polygon
fig_robot_region{iRobot} = plot(ax_main, pgon, 'FaceColor', 'none', 'EdgeColor', ...
color_robot{iRobot}, 'LineWidth', 1.0, 'LineStyle', ':');
end
end
% plot robot history trajectory with figure handles
fig_robot_his_tra = cell(nRobot, 1);
if cfg.ifShowHistoryTra == 1
robotHistoryTra_x = [];
robotHistoryTra_y = [];
robotHistoryTra_z = [];
for iRobot = 1 : nRobot
robotHistoryTra_x(1, iRobot) = System.MultiRobot_{iRobot}.pos_real_(1);
robotHistoryTra_y(1, iRobot) = System.MultiRobot_{iRobot}.pos_real_(2);
if pr.dim == 2
fig_robot_his_tra{iRobot} = plot(robotHistoryTra_x(:, iRobot), ...
robotHistoryTra_y(:, iRobot), 'Color', color_robot{iRobot}, ...
'LineWidth', 1.5, 'LineStyle', '-');
elseif pr.dim == 3
robotHistoryTra_z(1, iRobot) = System.MultiRobot_{iRobot}.pos_real_(3);
fig_robot_his_tra{iRobot} = plot3(robotHistoryTra_x(:, iRobot), ...
robotHistoryTra_y(:, iRobot), robotHistoryTra_z(:, iRobot), ...
'Color', color_robot{iRobot}, ...
'LineWidth', 1.5, 'LineStyle', '-');
end
end
end
end
%% loop preparation
iRobot = 0; % clear index
jBoxObs = 0;
n_loop = 0; % number of loops performed
exitflag = 0; % flags
infeasible = 0;
fprintf('[%s] MODE: Running as Simulation \n',datestr(now,'HH:MM:SS'));
fprintf('[%s] Looping... \n',datestr(now,'HH:MM:SS'));
% %% main loop
if_robots_arrived = zeros(nRobot, 1);
% pause;
% timers
dt_loop = pr.dtSim; % delta t of the loop
t_start = tic; % start time before the entire loop
while(true)
% elapsed time
t_elapsed = toc(t_start);
%% control loop
n_loop = n_loop + 1;
if(mod(n_loop, 10) == 0)
fprintf('[%s] Looping [%d] \n',datestr(now,'HH:MM:SS'), n_loop);
end
%% get system state
System.getSystemState();
%% update system visulization
for i = 1 % for wrapping up
% robot current position
for iRobot = 1 : nRobot
if pr.dim == 2
[X, Y] = ellipse(System.MultiRobot_{iRobot}.pos_real_, ...
System.MultiRobot_{iRobot}.radius_.*[1;1], 0);
set(fig_robot_pos{iRobot}, 'XData', X, 'YData', Y);
elseif pr.dim == 3
[X, Y, Z] = ellipsoid(System.MultiRobot_{iRobot}.pos_real_(1), ...
System.MultiRobot_{iRobot}.pos_real_(2), ...
System.MultiRobot_{iRobot}.pos_real_(3), ...
System.MultiRobot_{iRobot}.radius_, ...
System.MultiRobot_{iRobot}.radius_, ...
System.MultiRobot_{iRobot}.radius_);
set(fig_robot_pos{iRobot}, 'XData', X, 'YData', Y, 'ZData', Z);
end
end
% robot convex region
if cfg.ifShowSafeRegion == 1
for iRobot = 1 : nRobot
delete(fig_robot_region{iRobot});
fig_robot_region{iRobot} = plot_poly_vert(ax_main, pr.dim, ...
System.MultiRobot_{iRobot}.safe_region_.verts', ...
'FaceColor', color_robot{iRobot}, 'FaceAlpha', 0.1, ...
'EdgeColor', color_robot{iRobot}, 'EdgeAlpha', 0.6, ...
'LineWidth', 1.0, 'LineStyle', '-.');
end
end
% robot history trajectory
if cfg.ifShowHistoryTra == 1
robotHistoryTra_x(end+1, 1) = 0; % extend the array
robotHistoryTra_y(end+1, 1) = 0;
robotHistoryTra_z(end+1, 1) = 0;
for iRobot = 1 : nRobot
robotHistoryTra_x(end, iRobot) = System.MultiRobot_{iRobot}.pos_real_(1);
robotHistoryTra_y(end, iRobot) = System.MultiRobot_{iRobot}.pos_real_(2);
if pr.dim == 2
set(fig_robot_his_tra{iRobot}, 'XData', robotHistoryTra_x(:, iRobot), ...
'YData', robotHistoryTra_y(:, iRobot));
elseif pr.dim == 3
robotHistoryTra_z(end, iRobot) = System.MultiRobot_{iRobot}.pos_real_(3);
set(fig_robot_his_tra{iRobot}, 'XData', robotHistoryTra_x(:, iRobot), ...
'YData', robotHistoryTra_y(:, iRobot), ...
'ZData', robotHistoryTra_z(:, iRobot));
end
end
end
end
if sum(sum(System.collision_mtx_)) > 0
fprintf('Collision happens!\n')
% pause;
break;
end
%% simulate one step
System.simSystemOneStep();
%% if robot arrived
for iRobot = 1 : nRobot
if_robots_arrived(iRobot) = System.MultiRobot_{iRobot}.isArrived_;
end
%% collision checking
% System.collisionChecking();
drawnow limitrate
% pause(0.01);
% pause(pr.dtSim);
%% end of simulation
if sum(if_robots_arrived) == nRobot
fprintf('All robots arrived!\n');
% pause;
break;
end
end