-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathECE_UAV.cpp
More file actions
412 lines (323 loc) · 11.9 KB
/
ECE_UAV.cpp
File metadata and controls
412 lines (323 loc) · 11.9 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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//
// Created by mehak on 12/6/2021.
//
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <ctime>
#include <thread>
#include <chrono>
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "ECE_UAV.h"
#define PI 3.14159265
const double normalX = (22.8/23.5);
const double normalY = (24.4/22.4);
const double normaldist = 90;
//Euclidean Distance between two vectors
double distance(glm::vec3 p1, glm::vec3 p2)
{
glm::vec3 d;
d.x = std::abs(p1.x - p2.x);
d.y = std::abs(p1.y - p2.y);
d.z = std::abs(p1.z - p2.z);
double dist = std::sqrt(std::pow(d.x, 2) + std::pow(d.y, 2) + std::pow(d.z, 2));
return dist;
}
//Direction vector form point 2 to point 1
glm::vec3 subtract(glm::vec3 p1, glm::vec3 p2)
{
glm::vec3 d;
d.x = p1.x - p2.x;
d.y = p1.y - p2.y;
d.z = p1.z - p2.z;
return d;
}
ECE_UAV::ECE_UAV():thread()
{
position = glm::vec3 (0.0f,0.0f,0.0f);
velocity = glm::vec3 (0.0f,0.0f,0.0f);
directionVector = glm::vec3 (0.0f,0.0f,0.0f);
tangentVector = glm::vec3 (0.0f,0.0f,0.0f);
mass = 1;
acceleration = glm::vec3 (0.0f,0.0f,0.0f);
pos = -1;
circularMotion = false;
startFlying = false;
allDone = false;
}
void ECE_UAV::start()
{
this->thread = std::thread(&threadFunction, this);
}
void threadFunction(ECE_UAV* pUAV)
{
do{
pUAV->updatePosition(0.01, 10);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}while(not pUAV->getAllDone());
}
bool ECE_UAV::getCircularStart()
{
return this->circularMotion;
}
void ECE_UAV::setCircularTime(double time)
{
this->circularStart = time;
}
void ECE_UAV::setFlight()
{
this->startFlying = true;
}
void ECE_UAV::setAllDone()
{
this->allDone = true;
}
bool ECE_UAV::getAllDone()
{
return this->allDone;
}
glm::vec3 ECE_UAV::getPosition()
{
return this->position;
}
glm::vec3 ECE_UAV::getVelocity()
{
return this->velocity;
}
void ECE_UAV::setPosition(glm::vec3 pos)
{
this->position = pos;
}
void ECE_UAV::setVelocity(glm::vec3 vel)
{
this->velocity = vel;
}
void ECE_UAV::initPos(int i)
{
glm::vec3 posSuzie;
if(i < 5)
{
posSuzie = glm::vec3(45.72f - i*23.5f, 24.4f, 0.0f);
}
else if( i < 10)
{
posSuzie = glm::vec3(45.72f - (i-5)*23.5f, -24.4f, 0.0f);
}
else
{
posSuzie = glm::vec3(45.72f - (i-10)*23.5f, 0.0f, 0.0f);
}
this->position = posSuzie;
this->pos = i;
updateVectors();
}
void ECE_UAV::updateVectors()
{
/// Function to find the direction vector - points from the uav to the center of the sphere
/// and to find a random normal vector to the direction vector
glm::vec3 posSuzie = this->position;
glm::vec3 target = glm::vec3(0,0,50);
glm::vec3 directionVector = subtract(target, posSuzie);
double distFromTarget = distance(target, posSuzie);
directionVector.x = directionVector.x / distFromTarget;
directionVector.y = directionVector.y / distFromTarget;
directionVector.z = directionVector.z / distFromTarget;
this->directionVector = directionVector;
std::srand(time(0)*this->pos);
glm::vec3 v = glm::vec3(std::rand() % 11, std::rand() % 11 , std::rand() % 11 );
glm::vec3 vv = subtract(v, posSuzie);
double distFromV = distance(v, posSuzie);
vv.x /= distFromV;
vv.y /= distFromV;
vv.z /= distFromV;
//Take cross product with v
glm::vec3 tangentVector;
tangentVector.x = vv.y * directionVector.z - vv.z * directionVector.y;
tangentVector.y = vv.z * directionVector.x - vv.x * directionVector.z;
tangentVector.z = vv.x * directionVector.y - vv.y * directionVector.x;
if (tangentVector.x == 0.0 && tangentVector.y == 0.0 && tangentVector.z == 0.0)
{
vv.x = vv.x + 1;
tangentVector.x = vv.y * directionVector.z - vv.z * directionVector.y;
tangentVector.y = vv.z * directionVector.x - vv.x * directionVector.z;
tangentVector.z = vv.x * directionVector.y - vv.y * directionVector.x;
}
double tMag = distance(tangentVector, glm::vec3 (0.0f, 0.0f, 0.0f));
this->tangentVector.x = tangentVector.x/tMag;
this->tangentVector.y = tangentVector.y/tMag;
this->tangentVector.z = tangentVector.z/tMag;
}
void ECE_UAV::updatePosition(double delta, double distanceThreshold)
{
/// Function that updates positions of uavs while moving
glm::vec3 curPos = this->position;
glm::vec3 target = glm::vec3(0,0,50);
double distFromTarget = distance(target, curPos);
double fMax = 20.0;
double k = 10;
glm::vec3 force;
glm::vec3 newVel;
glm::vec3 newPos;
if(this->startFlying) {
if (distFromTarget >= distanceThreshold and not circularMotion) {
double vMax = 2.0;
double phi = std::atan2(std::sqrt(std::pow(directionVector.x, 2) + std::pow(directionVector.y, 2)),
directionVector.z);
//Let angle between x and y component be theta and angle between z and xy direction vector be phi
force.x = this->directionVector.x * (fMax - 10 / cos(phi));
force.y = this->directionVector.y * (fMax - 10 / cos(phi));
force.z = this->directionVector.z * (fMax - 10 / cos(phi));
//Get acceleration from force vector
this->acceleration.x = force.x / this->mass;
this->acceleration.y = force.y / this->mass;
this->acceleration.z = force.z / this->mass;
//Get New Velocity
newVel = glm::vec3(this->velocity.x + this->acceleration.x * delta,
this->velocity.y + this->acceleration.y * delta,
this->velocity.z + this->acceleration.z * delta);
double vMag = distance(newVel, glm::vec3(0.0f, 0.0f, 0.0f));
if (vMag > vMax) {
newVel.x = (newVel.x / vMag) * 2;
newVel.y = (newVel.y / vMag) * 2;
newVel.z = (newVel.z / vMag) * 2;
this->acceleration = subtract(newVel, this->velocity);
this->acceleration.x /= delta;
this->acceleration.y /= delta;
this->acceleration.z /= delta;
}
newPos = glm::vec3(
((curPos.x / normalX) + this->velocity.x * delta + 0.5 * this->acceleration.x * delta * delta) *
normalX,
((curPos.y / normalY) + this->velocity.y * delta + 0.5 * this->acceleration.y * delta * delta) *
normalY,
((curPos.z) + this->velocity.z * delta + 0.5 * this->acceleration.z * delta * delta));
} else {
if (not circularMotion) {
circularMotion = true;
updateVectors();
this->velocity.x = 10 * this->tangentVector.x;
this->velocity.y = 10 * this->tangentVector.y;
this->velocity.z = 10 * this->tangentVector.z;
}
glm::vec3 posSuzie = this->position;
glm::vec3 target = glm::vec3(0, 0, 50);
glm::vec3 directionVector = subtract(target, posSuzie);
double distFromTarget = distance(target, posSuzie);
directionVector.x = -1 * directionVector.x / distFromTarget;
directionVector.y = -1 * directionVector.y / distFromTarget;
directionVector.z = -1 * directionVector.z / distFromTarget;
this->directionVector = directionVector;
double vMax = 10.0;
double vMin = 2.0;
double fSpring = k * (10 - distFromTarget);
//Get acceleration from force vector
this->acceleration.x = fSpring * this->directionVector.x / this->mass;
this->acceleration.y = fSpring * this->directionVector.y / this->mass;
this->acceleration.z = fSpring * this->directionVector.z / this->mass;
//Get New Velocity
newVel = glm::vec3(this->velocity.x + this->acceleration.x * delta,
this->velocity.y + this->acceleration.y * delta,
this->velocity.z + this->acceleration.z * delta);
double vMag = distance(newVel, glm::vec3(0.0f, 0.0f, 0.0f));
this->tangentVector.x = this->velocity.x;
this->tangentVector.x = this->velocity.x;
this->tangentVector.x = this->velocity.x;
if (vMag > vMax) {
newVel.x = (newVel.x / vMag) * 10;
newVel.y = (newVel.y / vMag) * 10;
newVel.z = (newVel.z / vMag) * 10;
this->acceleration = subtract(newVel, this->velocity);
this->acceleration.x /= delta;
this->acceleration.y /= delta;
this->acceleration.z /= delta;
} else if (vMag < vMin) {
newVel.x = (newVel.x / vMag) * 2;
newVel.y = (newVel.y / vMag) * 2;
newVel.z = (newVel.z / vMag) * 2;
this->acceleration = subtract(newVel, this->velocity);
this->acceleration.x /= delta;
this->acceleration.y /= delta;
this->acceleration.z /= delta;
}
newPos = glm::vec3(
((curPos.x / normalX) + this->velocity.x * delta + 0.5 * this->acceleration.x * delta * delta) *
normalX,
((curPos.y / normalY) + this->velocity.y * delta + 0.5 * this->acceleration.y * delta * delta) *
normalY,
((curPos.z) + this->velocity.z * delta + 0.5 * this->acceleration.z * delta * delta));
}
this->velocity = newVel;
this->position.x = newPos.x;
this->position.y = newPos.y;
this->position.z = newPos.z;
}
}
bool ECE_UAV::flySixty(double time)
{
///Checks if the uav has been in circular motion for 60 seconds
if(circularMotion && (time - circularStart) >= 60)
return true;
else
return false;
}
void doIfCollide(ECE_UAV &object1, ECE_UAV &object2)
{
/// Set velocity to the other uav's velocity and move the uavs double their current distance apart
glm::vec3 vel1 = object1.getVelocity();
glm::vec3 vel2 = object2.getVelocity();
object1.setVelocity(vel2);
object2.setVelocity(vel1);
glm::vec3 pos1 = object1.getPosition();
glm::vec3 pos2 = object2.getPosition();
double vmag1 = distance(vel1, glm::vec3(0,0,0));
double vmag2 = distance(vel2, glm::vec3(0,0,0));
double dist = distance(pos2, pos2)*10;
glm::vec3 dir = subtract(pos1, pos2);
glm::vec3 newPos1 = glm::vec3(pos1.x - dir.x , pos1.y - dir.y , pos1.z - dir.z);
glm::vec3 newPos2 = glm::vec3(pos2.x + dir.x , pos2.y + dir.x , pos2.z + dir.x);
object1.setPosition(newPos1);
object2.setPosition(newPos2);
}
int elasticCollision(ECE_UAV *uavs, int num)
{
/// Find the closest uav and check whther its 0.01 m apart
glm::vec3 pos = uavs[num].getPosition();
pos.x = pos.x / normalX;
pos.y = pos.y / normalY;
// then we find the cloeset uav, default is -1, meaning that closest uav is farther than threshold
int closestUav = -1;
// set default dist to infinity
double dist = std::numeric_limits<double>::max();
// use a for loop to traverse all uavs
for (int i = 0; i < 15; i++)
{
if (i == num)
{
// skip itself
continue;
}
// calculate distance
glm::vec3 pos2 = uavs[i].getPosition();
pos2.x = pos2.x / normalX;
pos2.y = pos2.y / normalY;
double tmpDist = distance(pos2, pos);
if (tmpDist <= dist)
{
// if closer than the distance ever found so far and closer than the threshold
// update the closest uav
closestUav = i;
dist = tmpDist;
}
}
if (dist <= 0.01*normaldist)
{
std::cout<<"Distance : "<<dist<<std::endl;
return closestUav;
}
else
{
return -1;
}
}