-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
345 lines (311 loc) · 8.81 KB
/
main.cpp
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
// TODO: function for resetting orientation back to neutral after sample drop off
// TODO: more effective search algorithm; current one is too fuel-consuming
// TODO: faster geyser escape algorithm
//State flags
#define ANALYSER 0
#define GETTOSEARCH 1
#define INITSEARCH 2
#define DRILL 3
#define RISE 4
#define RETURN 5
#define EVAC 6 // Geyser escape
#define EVAC2 7
#define DESCEND 8// Geyser escape: Phase 2; resets drone to neutral
//Satellite direction: THESE ARE NOT STATE FLAGS
//For search algorithm
#define LEFT 9
#define RIGHT 10
#define UP 11
#define DOWN 12
int locationC; //State flag holder
int direction; //Direction flag holder
float distInt;
float position[3];
bool isTop;
//Tracks samples
float concentrations[5];
//Current slot filled
int invIndex;
void init()
{
float myZRState[12];
api.getMyZRState(myZRState);
position[0] = myZRState[0];
position[1] = myZRState[1];
float tmpPos[] = {position[0],position[1]};
position[2] = 0.60f;
direction = LEFT;
distInt = 0.16f;
locationC = ANALYSER;
isTop = true;
}
// For finding closer analyser
int findClosestItem() {
float myZRState[12];
api.getMyZRState(myZRState);
int closestItem = -1;
float closestDist = 10000.0f;
//Get Satellite Location
float satLoc[] = {myZRState[0],myZRState[1],myZRState[2]};
for(int i = 0; i < 9; i++) {
}
return closestItem;
}
// Calculates distance between pos and satellite
float getDistance (float pos[3]){
float myZRState[12];
ZeroRoboticsGame game = ZeroRoboticsGame::instance();
api.getMyZRState(myZRState);
//Get Satellite Locatio
float satLoc[] = {myZRState[0],myZRState[1],myZRState[2]};
//Get resultant vector
float rVector[3];
mathVecSubtract(rVector, pos, satLoc, 3);
//Get distance between satellite and item
float distance = mathVecMagnitude(rVector, 3);
return distance;
}
bool withinBoundary(float pos[3]) {
if(pos[0] < -0.48 || pos[0] > 0.48) {
return false;
}
if(pos[1] < -0.64 || pos[1] > 0.64) {
return false;
}
return true;
}
bool moveTo(float pos[3])
{
float myZRState[12];
api.getMyZRState(myZRState);
//Get Satellite Location
float satLoc[] = {myZRState[0],myZRState[1],myZRState[2]};
//Get Satellite Velocity
float satVelocity[] = {myZRState[3],myZRState[4],myZRState[5]};
//Get resultant vector
float rVector[3];
mathVecSubtract(rVector, pos, satLoc, 3);
//Get distance between satellite and item
float distance = mathVecMagnitude(rVector, 3);
//
if(distance < mathVecMagnitude(satVelocity, 3) * 6.0f) {
api.setPositionTarget(pos);
}
else {
api.setVelocityTarget(rVector);
}
if(distance < 0.01f && mathVecMagnitude(satVelocity, 3) < 0.01f ) {
return true;
}
return false;
}
bool drill() {
float myZRState[12];
ZeroRoboticsGame game = ZeroRoboticsGame::instance();
api.getMyZRState(myZRState);
float velocityT[3] = {0.0f, 0.0f, 0.0f};
api.setVelocityTarget(velocityT);
float attitudeVector[3];
attitudeVector[0] = 0;
attitudeVector[1] = 0;
attitudeVector[2] = 1.8;
if(game.getDrillError()) {
game.stopDrill();
locationC = GETTOSEARCH;
return false;
}
if(!game.getDrillEnabled()) {
if(game.startDrill()){
api.setAttRateTarget(attitudeVector);
}
else{
DEBUG(("Drill not starting!!!!"));
}
}
else {
if(game.checkSample()){
invIndex = game.pickupSample();
return true;
}
}
return false;
}
void search() {
DEBUG(("INITSEARCH"));
if(direction == LEFT) {
DEBUG(("LEFT"));
position[0] = position[0] - distInt;
if(!withinBoundary(position)) {
if(isTop) {
direction = DOWN;
}
else {
direction = UP;
}
position[0] = position[0] + distInt;
}
}
if(direction == RIGHT) {
DEBUG(("RIGHT"));
position[0] = position[0] + distInt;
if(!withinBoundary(position)) {
if(isTop) {
direction = DOWN;
}
else {
direction = UP;
}
position[0] = position[0] - distInt;
}
}
if(direction == UP) {
DEBUG(("UP"));
position[1] = position[1] + distInt;
if(!withinBoundary(position)) {
position[1] = position[1] - distInt;
direction = DOWN;
}
else {
if(getDistance(position) < 0.1f) {
float tmp[] = {position[0] + distInt, position[1], position[2]};
if(withinBoundary(tmp)) {
direction = RIGHT;
}
tmp[0] = position[0] - distInt;
tmp[1] = position[1];
tmp[2] = position[2];
if(withinBoundary(tmp)) {
direction = LEFT;
}
}
}
}
if(direction == DOWN) {
DEBUG(("DOWN"));
position[1] = position[1] - distInt;
if(!withinBoundary(position)) {
position[1] = position[1] + distInt;
direction = UP;
}
else {
if(getDistance(position) < 0.1f) {
float tmp[] = {position[0] + distInt, position[1], position[2]};
if(withinBoundary(tmp)) {
direction = RIGHT;
}
tmp[0] = position[0] - distInt;
tmp[1] = position[1];
tmp[2] = position[2];
if(withinBoundary(tmp)) {
direction = LEFT;
}
}
}
}
else if(game.getNumSamplesHeld() == 3) {
game.stopDrill();
float origin[] = {0,0,0};
moveTo(origin);
if(getDistance(origin) < 0.05f) {
for(int i = 0; i < 3; i++) {
game.dropSample(i);
}
}
}
}
bool rise() {
float myZRState[12];
ZeroRoboticsGame game = ZeroRoboticsGame::instance();
api.getMyZRState(myZRState);
position[2] = 0.20f;
float attitude[] = {0.0f,0.0f,0.0f};
api.setAttRateTarget(attitude);
if(moveTo(position)) {
return true;
}
return false;
}
void loop(){
float myZRState[12];
ZeroRoboticsGame game = ZeroRoboticsGame::instance();
api.getMyZRState(myZRState);
if (locationC == EVAC) {
search();
position[2] = 0.40f;
locationC = EVAC2;
DEBUG(("EVAC2"));
}
if (locationC == EVAC2) {
float attitude[] = {0.0f,0.0f,0.0f};
api.setAttRateTarget(attitude);
if(moveTo(position)){
locationC = RISE;
DEBUG(("RISE"));
}
}
//Right now ANALYSER jumps straight to GETTOSEARCH i.e is useless
if (locationC == ANALYSER) {
DEBUG(("ANALYSER"));
locationC = GETTOSEARCH;
}
if (locationC == RISE) {
if(rise()) {
locationC = GETTOSEARCH;
position[2] = 0.20f;
}
}
if (locationC == RETURN) {
float basePos[] = {0,0,0};
float attitude[] = {0.0f,0.0f,-1.0f};
api.setAttitudeTarget(attitude);
if(moveTo(basePos) && game.atBaseStation()) {
for(int i = 0; i < 5; i++) {
game.dropSample(i);
DEBUG(("GETTOSEARCH"));
locationC = GETTOSEARCH;
}
}
}
if (locationC == GETTOSEARCH) {
DEBUG(("GETTOSEARCH"));
search();
float tmpPos[] = {position[0],position[1]};
position[2] = 0.20f;
locationC = INITSEARCH;
}
if(locationC == INITSEARCH) {
DEBUG(("INITSEARCH"));
if(moveTo(position)){
locationC = DESCEND;
DEBUG(("DESCEND"));
}
}
if(locationC == DESCEND) {
DEBUG(("DESCEND"));
float tmpPos[] = {position[0],position[1]};
position[2] = game.getTerrainHeight(tmpPos)- 0.14f;
if(moveTo(position)){
locationC = DRILL;
DEBUG(("DRILL"));
}
}
if(locationC == DRILL) {
bool gotSample = drill();
float tmpPos[] = {position[0],position[1]};
if(game.isGeyserHere(tmpPos)) {
game.stopDrill();
locationC = EVAC;
DEBUG(("EVAC"));
}
else if(gotSample == true) {
if(game.getNumSamplesHeld() >= 3) {
game.stopDrill();
locationC = RETURN;
}
else if (game.getDrills(tmpPos) > 2) {
game.stopDrill();
locationC = RISE;
}
}
}
}