/
robot_team.hpp
392 lines (333 loc) · 13.5 KB
/
robot_team.hpp
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include "robot.hpp"
template <int Dim>
class HomogeneousRobotTeam {
public:
HomogeneousRobotTeam(decimal_t ddt = 0) : ddt_(ddt) {}
/// Set max velocity for planner
void set_v_max(decimal_t v) { v_max_ = v; }
/// Set max acceleration for planner
void set_a_max(decimal_t a) { a_max_ = a; }
/// Set primitive dt
void set_dt(decimal_t dt) { dt_ = dt; }
/// Set control input for planner
void set_u(const vec_E<VecDf>& U) { U_ = U; }
/// Set bounding box for the map
void set_map(const Vecf<Dim>& origin, const Vecf<Dim>& dim) {
origin_ = origin;
dim_ = dim;
}
/// Set planner versbose for individual robot
void set_verbose(bool verbose) { verbose_ = verbose; }
/// Set geometry of robot
void set_geometry(const Polyhedron<Dim>& rec) { rec_ = rec; }
/// Set obstacles for each robot
vec_E<Polyhedron<Dim>> set_obs(decimal_t time) {
vec_E<Polyhedron<Dim>> poly_obs;
for (size_t i = 0; i < robots_.size(); i++) {
vec_E<PolyhedronLinearObstacle<Dim>> linear_obs;
vec_E<PolyhedronNonlinearObstacle<Dim>> nonlinear_obs;
for (size_t j = 0; j < robots_.size(); j++) {
if (i != j)
nonlinear_obs.push_back(
robots_[j]->get_nonlinear_obstacle(time, traj_time_));
// linear_obs.push_back(robots_[j]->get_linear_obstacle(time));
}
robots_[i]->set_static_obs(static_obs_);
robots_[i]->set_linear_obs(linear_obs);
robots_[i]->set_nonlinear_obs(nonlinear_obs);
poly_obs.push_back(robots_[i]->get_nonlinear_obstacle(time).poly(0));
}
for (const auto& it : static_obs_) poly_obs.push_back(it.poly(0));
return poly_obs;
}
/// Get robots
std::vector<std::shared_ptr<Robot<Dim>>> get_robots() { return robots_; }
/// Get current obstacles
vec_E<Polyhedron<Dim>> get_obs() { return curr_obs_; }
/// Plan decentralized
bool update_decentralized(decimal_t time) {
curr_obs_ = set_obs(time);
for (auto& it : robots_)
if (!it->plan(time)) return false;
return true;
}
/// Plan decentralized
bool update_centralized(decimal_t time) {
curr_obs_.clear();
for (const auto& it : robots_)
curr_obs_.push_back(it->get_nonlinear_obstacle(time).poly(0));
for (const auto& it : static_obs_) curr_obs_.push_back(it.poly(0));
static bool planned = false;
if (planned) return true;
for (size_t i = 0; i < robots_.size(); i++) {
vec_E<PolyhedronLinearObstacle<Dim>> linear_obs;
vec_E<PolyhedronNonlinearObstacle<Dim>> nonlinear_obs;
for (size_t j = 0; j < i; j++) {
nonlinear_obs.push_back(robots_[j]->get_nonlinear_obstacle(time, 0));
// linear_obs.push_back(robots_[j]->get_linear_obstacle(time));
}
robots_[i]->set_static_obs(static_obs_);
robots_[i]->set_linear_obs(linear_obs);
robots_[i]->set_nonlinear_obs(nonlinear_obs);
robots_[i]->set_traj_t(-1);
if (!robots_[i]->plan(0)) return false;
}
planned = true;
return true;
}
/// Check if all robot reaches the goal
bool finished(decimal_t time) {
bool reached = true;
for (const auto& it : robots_)
if (!it->reached(time)) return false;
return true;
}
virtual void init(){};
protected:
/// Planning gap
decimal_t ddt_{0};
/// Max velocity
decimal_t v_max_{1};
/// Max acceleration
decimal_t a_max_{1};
/// Primitive dt
decimal_t dt_{1};
/// Control input
vec_E<VecDf> U_;
/// Map origin
Vecf<Dim> origin_;
/// Map dimension
Vecf<Dim> dim_;
/// Verbose
bool verbose_{true};
/// Trajectory duration for each robot's nonlinear obstacle
decimal_t traj_time_{4.0};
/// Robot shape
Polyhedron<Dim> rec_;
/// Obstacle course
vec_E<Polyhedron<Dim>> curr_obs_;
/// Robot array
std::vector<std::shared_ptr<Robot<Dim>>> robots_;
/// Static Obstacle array
vec_E<PolyhedronObstacle2D> static_obs_;
};
/*********************** Team 1: 10 robots **************************/
/********************************************************************/
/****************** | | ********************/
/****************** o | | o ********************/
/****************** | | ********************/
/****************** | | ********************/
/****************** o | | o ********************/
/****************** | | ********************/
/****************** |____| ********************/
/****************** o ____ o ********************/
/****************** | | ********************/
/****************** | | ********************/
/****************** o | | o ********************/
/****************** | | ********************/
/****************** | | ********************/
/****************** o | | o ********************/
/****************** | | ********************/
/********************************************************************/
class Team1 : public HomogeneousRobotTeam<2> {
public:
Team1(decimal_t ddt) : HomogeneousRobotTeam<2>(ddt) {}
void init() {
traj_time_ = v_max_ / U_.front().lpNorm<Eigen::Infinity>() / dt_;
printf("traj time: %f\n", traj_time_);
std::shared_ptr<Robot2D> robot0 =
std::make_shared<Robot2D>(rec_, "robot0", verbose_);
robot0->set_start(Vec2f(0, -5));
robot0->set_goal(Vec2f(10, -5));
std::shared_ptr<Robot2D> robot1 =
std::make_shared<Robot2D>(rec_, "robot1", verbose_);
robot1->set_start(Vec2f(0, -2.5));
robot1->set_goal(Vec2f(10, -2.5));
std::shared_ptr<Robot2D> robot2 =
std::make_shared<Robot2D>(rec_, "robot2", verbose_);
robot2->set_start(Vec2f(0, 0));
robot2->set_goal(Vec2f(10, 0));
std::shared_ptr<Robot2D> robot3 =
std::make_shared<Robot2D>(rec_, "robot3", verbose_);
robot3->set_start(Vec2f(0, 2.5));
robot3->set_goal(Vec2f(10, 2.5));
std::shared_ptr<Robot2D> robot4 =
std::make_shared<Robot2D>(rec_, "robot4", verbose_);
robot4->set_start(Vec2f(0, 5));
robot4->set_goal(Vec2f(10, 5));
std::shared_ptr<Robot2D> robot5 =
std::make_shared<Robot2D>(rec_, "robot5", verbose_);
robot5->set_start(Vec2f(10, 0));
robot5->set_goal(Vec2f(0, 0));
std::shared_ptr<Robot2D> robot6 =
std::make_shared<Robot2D>(rec_, "robot6", verbose_);
robot6->set_start(Vec2f(10, 2.5));
robot6->set_goal(Vec2f(0, 2.5));
std::shared_ptr<Robot2D> robot7 =
std::make_shared<Robot2D>(rec_, "robot7", verbose_);
robot7->set_start(Vec2f(10, 5));
robot7->set_goal(Vec2f(0, 5));
std::shared_ptr<Robot2D> robot8 =
std::make_shared<Robot2D>(rec_, "robot8", verbose_);
robot8->set_start(Vec2f(10, -2.5));
robot8->set_goal(Vec2f(0, -2.5));
std::shared_ptr<Robot2D> robot9 =
std::make_shared<Robot2D>(rec_, "robot9", verbose_);
robot9->set_start(Vec2f(10, -5));
robot9->set_goal(Vec2f(0, -5));
robots_.push_back(robot0);
robots_.push_back(robot1);
robots_.push_back(robot2);
robots_.push_back(robot3);
robots_.push_back(robot4);
robots_.push_back(robot5);
robots_.push_back(robot6);
robots_.push_back(robot7);
robots_.push_back(robot8);
robots_.push_back(robot9);
decimal_t ddt = 0;
for (auto robot : robots_) {
robot->set_v_max(v_max_);
robot->set_a_max(a_max_);
robot->set_u(U_);
robot->set_dt(dt_);
robot->set_map(origin_, dim_);
robot->plan(ddt); // need to set a small difference in the starting time
ddt += ddt_;
}
// Build the obstacle course
Polyhedron2D rec2;
rec2.add(Hyperplane2D(Vec2f(4, 0), -Vec2f::UnitX()));
rec2.add(Hyperplane2D(Vec2f(6, 0), Vec2f::UnitX()));
rec2.add(Hyperplane2D(Vec2f(5, 0.2), -Vec2f::UnitY()));
rec2.add(Hyperplane2D(Vec2f(5, 5.5), Vec2f::UnitY()));
static_obs_.push_back(PolyhedronObstacle2D(rec2, Vec2f::Zero()));
Polyhedron2D rec3;
rec3.add(Hyperplane2D(Vec2f(4, 0), -Vec2f::UnitX()));
rec3.add(Hyperplane2D(Vec2f(6, 0), Vec2f::UnitX()));
rec3.add(Hyperplane2D(Vec2f(5, -5.5), -Vec2f::UnitY()));
rec3.add(Hyperplane2D(Vec2f(5, -0.2), Vec2f::UnitY()));
static_obs_.push_back(PolyhedronObstacle2D(rec3, Vec2f::Zero()));
printf("Team1 Initialized!\n\n");
}
};
/************************ Team 2: 16 robots **************************/
/********************************************************************/
/****************** *********************/
/****************** o o o o o *********************/
/****************** *********************/
/****************** o o *********************/
/****************** _ *********************/
/****************** o |_| o *********************/
/****************** *********************/
/****************** o o *********************/
/****************** *********************/
/****************** o o o o o *********************/
/****************** *********************/
/********************************************************************/
class Team2 : public HomogeneousRobotTeam<2> {
public:
Team2(decimal_t ddt) : HomogeneousRobotTeam<2>(ddt) {}
void init() {
// traj_time_ = v_max_ / U_.front().lpNorm<Eigen::Infinity>() / dt_ + dt_;
traj_time_ = 0;
printf("traj time: %f\n", traj_time_);
std::shared_ptr<Robot2D> robot0 =
std::make_shared<Robot2D>(rec_, "robot0", verbose_);
robot0->set_start(Vec2f(0, -5));
robot0->set_goal(Vec2f(10, 5));
std::shared_ptr<Robot2D> robot1 =
std::make_shared<Robot2D>(rec_, "robot1", verbose_);
robot1->set_start(Vec2f(0, -2.5));
robot1->set_goal(Vec2f(10, 2.5));
std::shared_ptr<Robot2D> robot2 =
std::make_shared<Robot2D>(rec_, "robot2", verbose_);
robot2->set_start(Vec2f(0, 0));
robot2->set_goal(Vec2f(10, 0));
std::shared_ptr<Robot2D> robot3 =
std::make_shared<Robot2D>(rec_, "robot3", verbose_);
robot3->set_start(Vec2f(0, 2.5));
robot3->set_goal(Vec2f(10, -2.5));
std::shared_ptr<Robot2D> robot4 =
std::make_shared<Robot2D>(rec_, "robot4", verbose_);
robot4->set_start(Vec2f(0, 5));
robot4->set_goal(Vec2f(10, -5));
std::shared_ptr<Robot2D> robot5 =
std::make_shared<Robot2D>(rec_, "robot5", verbose_);
robot5->set_start(Vec2f(2.5, 5));
robot5->set_goal(Vec2f(7.5, -5));
std::shared_ptr<Robot2D> robot6 =
std::make_shared<Robot2D>(rec_, "robot6", verbose_);
robot6->set_start(Vec2f(5, 5));
robot6->set_goal(Vec2f(5, -5));
std::shared_ptr<Robot2D> robot7 =
std::make_shared<Robot2D>(rec_, "robot7", verbose_);
robot7->set_start(Vec2f(7.5, 5));
robot7->set_goal(Vec2f(2.5, -5));
std::shared_ptr<Robot2D> robot8 =
std::make_shared<Robot2D>(rec_, "robot8", verbose_);
robot8->set_start(Vec2f(10, 5));
robot8->set_goal(Vec2f(0, -5));
std::shared_ptr<Robot2D> robot9 =
std::make_shared<Robot2D>(rec_, "robot9", verbose_);
robot9->set_start(Vec2f(10, 2.5));
robot9->set_goal(Vec2f(0, -2.5));
std::shared_ptr<Robot2D> robot10 =
std::make_shared<Robot2D>(rec_, "robot10", verbose_);
robot10->set_start(Vec2f(10, 0));
robot10->set_goal(Vec2f(0, 0));
std::shared_ptr<Robot2D> robot11 =
std::make_shared<Robot2D>(rec_, "robot11", verbose_);
robot11->set_start(Vec2f(10, -2.5));
robot11->set_goal(Vec2f(0, 2.5));
std::shared_ptr<Robot2D> robot12 =
std::make_shared<Robot2D>(rec_, "robot12", verbose_);
robot12->set_start(Vec2f(10, -5));
robot12->set_goal(Vec2f(0, 5));
std::shared_ptr<Robot2D> robot13 =
std::make_shared<Robot2D>(rec_, "robot13", verbose_);
robot13->set_start(Vec2f(7.5, -5));
robot13->set_goal(Vec2f(2.5, 5));
std::shared_ptr<Robot2D> robot14 =
std::make_shared<Robot2D>(rec_, "robot14", verbose_);
robot14->set_start(Vec2f(5, -5));
robot14->set_goal(Vec2f(5, 5));
std::shared_ptr<Robot2D> robot15 =
std::make_shared<Robot2D>(rec_, "robot15", verbose_);
robot15->set_start(Vec2f(2.5, -5));
robot15->set_goal(Vec2f(7.5, 5));
robots_.push_back(robot0);
robots_.push_back(robot1);
robots_.push_back(robot2);
robots_.push_back(robot3);
robots_.push_back(robot4);
robots_.push_back(robot5);
robots_.push_back(robot6);
robots_.push_back(robot7);
robots_.push_back(robot8);
robots_.push_back(robot9);
robots_.push_back(robot10);
robots_.push_back(robot11);
robots_.push_back(robot12);
robots_.push_back(robot13);
robots_.push_back(robot14);
robots_.push_back(robot15);
decimal_t ddt = 0;
for (auto robot : robots_) {
robot->set_v_max(v_max_);
robot->set_a_max(a_max_);
robot->set_u(U_);
robot->set_dt(dt_);
robot->set_map(origin_, dim_);
robot->plan(ddt); // need to set a small difference in the starting time
ddt += ddt_;
}
Polyhedron2D rec1;
rec1.add(Hyperplane2D(Vec2f(4, 0), -Vec2f::UnitX()));
rec1.add(Hyperplane2D(Vec2f(6, 0), Vec2f::UnitX()));
rec1.add(Hyperplane2D(Vec2f(5, -1), -Vec2f::UnitY()));
rec1.add(Hyperplane2D(Vec2f(5, 1), Vec2f::UnitY()));
static_obs_.push_back(PolyhedronObstacle2D(rec1, Vec2f::Zero()));
printf("Team2 Initialized!\n\n");
}
};