This repository has been archived by the owner on Jun 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
robotcode.c
503 lines (420 loc) · 12 KB
/
robotcode.c
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#pragma config(Sensor, S1, touchCalibrate, sensorEV3_Touch)
#pragma config(Sensor, S2, touchReader, sensorEV3_Touch)
#pragma config(Sensor, S3, colorBill, sensorColorNxtFULL)
#pragma config(Sensor, S4, colorReader, sensorEV3_Color, modeEV3Color_Color)
#pragma config(Motor, motorA, traySlide, tmotorEV3_Large, PIDControl, encoder)
#pragma config(Motor, motorB, billReader, tmotorNXT, PIDControl, encoder)
#pragma config(Motor, motorC, armWeight, tmotorEV3_Large, PIDControl, encoder)
#pragma config(Motor, motorD, billOut, tmotorEV3_Medium, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/*
Joel Ruhland, George Cowan, Russell Wong, Justin Pezzak
****NOTE****
The code below is written for the EV3. RobotC has different functions and
syntax for different fuctionalities between the NXT platform and the EV3
platform. This code reflects that. Most functions names are self-explanatory,
but some may require lookup in the RobotC documentation.
****/
#include "accountdata.c"
const int NUMACCOUNTS = 10;
int const START_NUM_BILLS = 50;
//-----------------{1 ,5 ,10,20,100}
int numOfBills[] = {START_NUM_BILLS,START_NUM_BILLS,START_NUM_BILLS,START_NUM_BILLS,START_NUM_BILLS};
const float TRAY_DIST = 8.1 * 180 / PI ;
/*
Bills:
1 - White
5 - Blue
10 - Yellow
20 - Green
100 - Red
*/
struct BillsToOutput
{
int number[5];
};
int doesExist(string cardvalue, Account * account)
{
for (int i = 0; i<NUMACCOUNTS; i++)
{
if (account[i].cardNum == cardvalue)
return i;
}
return -1;
}
int getCardSwipe()
{
char values[4];
//Waits for card to be inserted
while (getTouchValue(touchReader) == false)
{}
//Waits until moved off black
while (SensorValue[S4] == 1 || SensorValue[S4] == 1)
{
wait1Msec(100);
}
//Detects and records new color when change is detected
for (int i = 0; i<4; i++)
{
values[i] = SensorValue[S4];
while (SensorValue[S4] == values[i])
{}
wait1Msec(200);
}
eraseDisplay();
displayString(0,"%i",values[0]*1000+values[1]*100+values[2]*10+values[3]);
wait1Msec(3000);
return (values[0]*1000+values[1]*100+values[2]*10+values[3]);
}
bool comparePin(string pin, Account * accountList)
{
for (int i = 0; i<NUMACCOUNTS; i++)
{
if (accountList[i].pin == pin)
return true;
}
return false;
}
bool checkPin(Account * accountList)
{
char pin[4] = {'0','0','0','0'};
int i = 0;
while (!getButtonPress(buttonEnter))
{
char pointer[4] = {' ',' ',' ',' '};
pointer[i] = '^';
displayString(1, "Enter PIN: %c%c%c%c",pin[0],pin[1],pin[2],pin[3]);
displayString(2, " %c%c%c%c",pointer[0],pointer[1],pointer[2],pointer[3]);
while (getButtonPress(buttonAny) == 0)
{}
//React to button Presses
while (getButtonPress(buttonAny) == 1)
{
if (getButtonPress(buttonLeft) == 1)
{
i--;
}
else if (getButtonPress(buttonRight) ==1 )
{
i++;
}
else if (getButtonPress(buttonUp) ==1)
{
pin[i]++;
}
else if (getButtonPress(buttonDown) ==1)
{
pin[i]--;
}
else
{
break;
}
if (i<0)
i++;
else if (i>3)
i--;
if (pin[i] >= ':')
pin[i]--;
else if (pin[i] <= '/')
pin[i]++;
wait1Msec(250);
eraseDisplay();
}
}
eraseDisplay();
string pinvalue = "";
for (int i = 0; i<4; i++)
{
pinvalue+=pin[i];
}
return comparePin(pinvalue, accountList);
}
//Russell Wong
//Tested
//Changed to use pointers (should re-test)
void billsToOutput(int amount, int * bills)
{
int denominations[5] = {1, 5, 10, 20, 100};
for (int i = 4; i >= 0; i--)
{
bills[i] = amount / denominations[i];
amount -= denominations[i]*bills[i];
}
}
//Joel Ruhland
//Fully Tested
int getAmount()
{
char amount[3] = {'0','0','0'};
int i = 0;
while (!getButtonPress(buttonEnter))
{
char pointer[3] = {' ',' ',' '};
pointer[i] = '^';
displayString(0, "Enter Amount: %c%c%c",amount[0],amount[1],amount[2]);
displayString(1, " %c%c%c",pointer[0],pointer[1],pointer[2]);
while (getButtonPress(buttonAny) == 0)
{}
//React to button Presses
while (getButtonPress(buttonAny) == 1)
{
if (getButtonPress(buttonLeft) == 1)
{
i--;
}
else if (getButtonPress(buttonRight) ==1)
{
i++;
}
else if (getButtonPress(buttonUp) ==1)
{
amount[i]++;
}
else if (getButtonPress(buttonDown) ==1)
{
amount[i]--;
}
else
{
break;
}
if (i<0)
i++;
else if (i>2)
i--;
if (amount[i] >= ':')
amount[i]--;
else if (amount[i] <= '/')
amount[i]++;
wait1Msec(250);
eraseDisplay();
}
}
int a0 = amount[0] - '0';
int a1 = amount[1] - '0';
int a2 = amount[2] - '0';
eraseDisplay();
return (a0*100+a1*10+a2);
}
//Russell Wong
//Tested
int getBill()
{
//Pulls in until color change is detected, then continues for 1/5
motor[billReader] = 50;
int billVal = 0;
int numEachColor[5] = {0, 0, 0, 0, 0};
int max = 0;
//while (getButtonPress(buttonEnter) != 1)
//{
while (SensorValue[colorBill] == 1)
wait1Msec(200);
//find the color that occurs the most
for (int i = 0; i < 10; i++)
{
int currentColor = SensorValue[colorBill];
int currentIndex = -1;
if(currentColor == 6)
currentIndex = 0;
else if (currentColor == 2)
currentIndex = 1;
else if (currentColor == 4)
currentIndex = 2;
else if (currentColor == 3)
currentIndex = 3;
else if (currentColor == 5)
currentIndex = 4;
if (currentIndex > -1)
{
numEachColor[currentIndex]++;
numOfBills[currentIndex]++;
if (numEachColor[currentIndex] > numEachColor[max])
max = currentIndex;
}
wait1Msec(100);
}
if (max == 0) //white
billVal = 1;
else if (max == 1) //blue
billVal = 5;
else if (max == 2) //yellow
billVal = 10;
else if (max == 3) //green
billVal = 20;
else //red
billVal = 100;
displayString(2, "%d", max);
wait1Msec(1000);
//}
return billVal;
}
int doDeposit()
{
int ammounttodeposit;
eraseDisplay();
displayString(0, "Press center button to exit");
while (getButtonPress(buttonEnter) != 1)
{
ammounttodeposit +=getBill();
eraseDisplay();
displayString(0, "Press center button to exit");
displayString(1,"%d", ammounttodeposit);
}
return ammounttodeposit;
eraseDisplay();
}
//Russell Wong
void doOutput(int * numBills)
{
//Move to location and output number of bills
motor[traySlide] = -50;
int colors[5] = {6, 2, 4, 3, 5}; //White Blue Yellow Green Red
//Assume start position in the centre
nMotorEncoder[traySlide] = 0;
while(nMotorEncoder[traySlide] > TRAY_DIST * -2)
{}
motor[traySlide] = 0;
motor[billReader] = -50;
motor[billOut] = 50;
for (int i = 0; i < 4; i++)
{
motor[armWeight] = 20;
wait1Msec(2000);
int numEachColor[5] = {0, 0, 0, 0, 0};
int max = 0;
motor[billOut] = 50;
for (int j = 0; j < numBills[i]; j++)
{
//wait until a bill is detected
while (SensorValue[colorBill] != colors[i])
{}
//wait until bill passes through
int billVal = colors[i];
while (billVal == colors[i])
{
//find the color that occurs the most
for (int k = 0; k < 10; k++)
{
int currentColor = SensorValue[colorBill];
int currentIndex = -1;
if(currentColor == 6)
currentIndex = 0;
else if (currentColor == 2)
currentIndex = 1;
else if (currentColor == 4)
currentIndex = 2;
else if (currentColor == 3)
currentIndex = 3;
else if (currentColor == 5)
currentIndex = 4;
if (currentIndex > -1)
{
numEachColor[currentIndex]++;
numOfBills[currentIndex]++;
if (numEachColor[currentIndex] > numEachColor[max])
max = currentIndex;
}
wait1Msec(100);
}
if (max == 0) //white
billVal = 1;
else if (max == 1) //blue
billVal = 5;
else if (max == 2) //yellow
billVal = 10;
else if (max == 3) //green
billVal = 20;
else //red
billVal = 100;
}
numOfBills[i]--;
wait1Msec(300);
}
//move arm up
motor[armWeight] = -20;
wait1Msec(2000);
//move tray to next slot
motor[traySlide] = 50;
nMotorEncoder[traySlide] = 0;
while (nMotorEncoder[traySlide] < TRAY_DIST)
{}
motor[traySlide] = 0;
}
//move back to starting position (centre)
motor[traySlide] = -50;
nMotorEncoder[traySlide] = 0;
while (nMotorEncoder[traySlide] > TRAY_DIST * -2)
{}
motor[traySlide] = 0;
}
int doWithdraw(Account accountList)
{
int amounttowithdraw = getAmount();
if (accountList.balance < amounttowithdraw)
{
eraseDisplay();
displayString(0,"Error, Insufficient Balance");
return -1;
}
int bills[5] = {0, 0, 0, 0, 0};
billsToOutput(amounttowithdraw, bills);
doOutput(bills);
return amounttowithdraw;
}
task main()
{
struct Account accountList[NUMACCOUNTS];
initAccounts(accountList, NUMACCOUNTS);
string account;
string cardnum;
string pin;
int accountindex;
bool pinCorrect;
while (getBatteryCurrent() > 0.02)
{
/*
displayString(0,"Swipe card to Begin");
do
{
cardnum = getCardSwipe();
accountindex = doesExist(cardnum, accountList);
displayString(0,"ERROR Card Not Found!!");
}while (accountindex == -1);
eraseDisplay();
do
{
pinCorrect = checkPin(accountList);
displayString(0,"ERROR Incorrect PIN");
}while (pinCorrect == false);
wait1Msec(100);
eraseDisplay();
wait1Msec(500);*/
//Selection Menu
while (getButtonPress(buttonEnter) != 1)
{
eraseDisplay();
displayString(1,"Press center button to close transaction");
displayString(2,"<---- ---->");
displayString(3,"Deposit Withdraw");
displayString(4," Balance: %f", accountList[accountindex].balance);
waitForButtonPress();
if (getButtonPress(buttonLeft) == 1)
{
int amount = doDeposit();
accountList[accountindex].balance += amount;
eraseDisplay();
displayString(0,"%d", amount);
}
else if (getButtonPress(buttonRight) ==1)
{
int amount = doWithdraw(accountList[accountindex]);
if (amount != -1)
{
accountList[accountindex].balance -= amount;
}
}
}
wait1Msec(5000);
}
}