-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotDriving.c
483 lines (423 loc) · 10.4 KB
/
botDriving.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
// global constants for ports
const string COLOR_PORT = "S1";
const string GYRO_PORT = "S2";
const string TOUCH_PORT = "S3";
const string ULTRASONIC_PORT = "S4";
//constant for array
const QUESTIONS_NUM = 10;
// main function .....
#include “PC_FileIO.c”
// function to configure all sensors
void configureAllSensors ()
{
SensorType[TOUCH_PORT] = sensorEV3_Touch;
wait1Msec(50);
SensorType[ULTRASONIC_PORT] = sensorEV3_Ultrasonic;
wait1Msec(50);
SensorType[COLOR_PORT] = sensorEV3_Color;
wait1Msec(50);
SensorType[GYRO_PORT] = sensorEV3_Gyro;
wait1Msec(50);
SensorMode[COLOR_PORT] = modeEV3Color_Color;
wait1Msec(50);
SensorMode[GYRO_PORT] = modeEV3Gyro_Calibration;
wait1Msec(50);
SensorMode[GYRO_PORT] = modeEV3Gyro_RateAndAngle;
wait1Msec(50);
return;
}
// drive function
void drivePower(int motorPower)
{
motor[motorA] = motorPower;
motor[motorD] = -motorPower;
return;
}
// drive a certain distance in cm function
void driveDist(float distCM, int motorPow)
{
const int encoderLimit=distCM*180/(PI*2.75);
nMotorEncoder[motorB]=0;
motor[motorB]=motor[motorC]=motorPow;
while (nMotorEncoder[motorB]<encoderLimit)
{ }
motor[motorB]=motor[motorC]=0;
}
// stop driving function
void driveStop()
{
motor[motorA]=motor[motorD]=0;
return;
}
// turn clockwise function
void turnCW(int angle, int motorPower) {
resetGyro(S2);
motor[motorA] = -motorPower;
motor[motorD] = -motorPower;
while(SensorValue[S2] < angle)
{ }
driveStop();
wait1Msec(2000);
return;
}
// function to choose the mode
int modeChoose()
{
int mode = 0;
// input verification
while(!(mode==1||mode==2))
{
// Mode menu
displayCenteredBigTextLine (2, "Mode Menu");
displayBigTextLine (5, "Press LEFT for");
displayBigTextLine (7, "Study Mode");
displayBigTextLine (10, "Press RIGHT for");
displayBigTextLine (12, "Test Mode");
// study mode=1, test mode=2
if (getButtonPress(LEFT_BUTTON))
{
mode=1;
}
if (getButtonPress(RIGHT_BUTTON))
{
mode=2;
}
}
eraseDisplay();
return mode;
}
//resets the variables for the skittle menu
void reset_char(string & var1, string & var2, string & var3, string & var4, string & var5)
{
var1 = ">";
var2 = " ";
var3 = " ";
var4 = " ";
var5 = " ";
}
//changes the selected menu option for skittles
void change_select(int & choice, string & Yellow_char, string & Red_char, string & Green_char,string & Purple_char,string & Orange_char)
{
if(choice < 0)
choice = 4;
if(choice > 4)
choice = 0;
if(choice == 0)
reset_char(Yellow_char, Red_char, Green_char, Purple_char, Orange_char);
else if (choice == 1)
reset_char(Red_char, Green_char, Purple_char, Orange_char, Yellow_char);
else if (choice == 2)
reset_char(Green_char, Purple_char, Orange_char, Yellow_char,Red_char);
else if (choice == 3)
reset_char(Purple_char, Orange_char, Yellow_char,Red_char,Green_char);
else if (choice == 4)
reset_char(Orange_char, Yellow_char,Red_char,Green_char,Purple_char);
}
//menu for selecting the skittles
int skittleSelect()
{
int choice = 0;
string Yellow_char = " ";
string Red_char= " ";
string Green_char = " ";
string Purple_char = " " ;
string Orange_char = " ";
while(!getButtonPress(ENTER_BUTTON))
{
if (getButtonPress(DOWN_BUTTON))
{
choice++;
while(getButtonPress(DOWN_BUTTON))
{
}
}
if(getButtonPress(UP_BUTTON))
{
choice--;
while(getButtonPress(UP_BUTTON))
{
}
}
change_select(choice, Yellow_char, Red_char, Green_char, Purple_char, Orange_char);
displayCenteredBigTextLine(2, "Skittle Menu");
displayBigTextLine(5,"%s Yellow", Yellow_char);
displayBigTextLine(7,"%s Red", Red_char);
displayBigTextLine(9,"%s Green", Green_char);
displayBigTextLine(11,"%s Purple", Purple_char);
displayBigTextLine(13,"%s Orange", Orange_char);
}
return choice;
}
// function to open the flap
void open_flap(int time_open)
{
nMotorEncoder(motorB) = 0;
motor[motorB] = -100;
while (nMotorEncoder(motorB) > -1500)
{
}
motor[motorB] = 0;
time1[T1]=0;
int shake_degree = nMotorEncoder(motorC);
while(time1[T1] <=time_open)
{
motor[motorC] = 25;
while (nMotorEncoder(motorC) < (shake_degree +10) && time1[T1] <=time_open)
{
}
motor[motorC]= -25;
while (nMotorEncoder(motorC) > (shake_degree-10) && time1[T1] <=time_open)
{
}
}
motor[motorC]= -15;
while (nMotorEncoder(motorC) > (shake_degree-10) && time1[T1] <=time_open)
{
}
motor[motorC]=0;
motor[motorB] = 100;
while (nMotorEncoder(motorB) < 0)
{
}
motor[motorB] = 0;
}
// asks user if they want to keep selecting skittles
bool keepdispencing();
{
eraseDisplay();
displayBigTextLine(2,"Need More?")
displayBigTextLine(4,"Press Up For Yes ");
displayBigTextLine(6,"Press Down For No");
while(true)
{
if (getButtonPress(UP_BUTTON))
{
while(getButtonPress(UP_BUTTON))
{
}
return true;
}
if (getButtonPress(DOWN_BUTTON))
{
while(getButtonPress(DOWN_BUTTON))
{
}
return false;
}
}
}
// dispensing fucntion
void dispense()
{
const int TIME_OPEN_FLAP_M = 2000;
int degree_deviation = 0;
//const TIME_OPEN_FLAP_M = 2000;
bool dispensing = true;
while(dispensing == true)
{
int skittle_selected = skittleSelect();
int rotate = -1*skittle_selected*60;
nMotorEncoder(motorC) = 0;
motor[motorC]= -15;
while(nMotorEncoder(motorC) > rotate)
{
}
motor[motorC]=0;
open_flap(TIME_OPEN_FLAP_M);
motor[motorC]= 15;
while(nMotorEncoder(motorC) <(0 + degree_deviation))
{
}
motor[motorC]=0;
dispensing = keepdispencing();
}
}
// study mode IT WORKS but fix something!!
void studyMode (int tapeColour, int turnColour)
{
int studyLoop=0; // # of loops it has done
while (studyLoop<=2) // if loops>2, then it exits the function
{
for (int turnCount=0;turnCount<=4;turnCount++) // 4 turns in one loop
{
drivePower(20);
while (SensorValue(COLOR_PORT)==tapeColour||SensorValue(COLOR_PORT)==1) // while it senses the tape colour or black because of colour variation
{
if (SensorValue(TOUCH_PORT)==1) // if the touch button is pushed
{
driveStop(); // stop
dispense() // dispense a skittle of choosing
drivePower(20); // continue driving
studyLoop=0; // set loop count back to 0
}
}
if (SensorValue(COLOR_PORT)==turnColour) // turns at the corners
{
driveStop(); // stops
turnCW(87,20); // turns at 87 because 90 over rotates the bot
drivePower(20); // drive forward a bit so it doesnt sense white anymore
wait1Msec(1000);
}
else // if it sees an unrelated colour, fix this cuz if it senses this it adds to turn loop
{
driveStop(); // stop
setSoundVolume(100);
playSoundFile("Uh-oh.rsf"); // play uh oh sound
while (!(SensorValue(COLOR_PORT)==tapeColour||SensorValue(COLOR_PORT)==1)) // wait until it gets placed back on track
{
}
}
}
studyLoop++;
}
}
// hourglass spinning
void hourglassVisual()
{
time1[T1] = 0;
drawBmpfile(0,127,"Hourglass 0"); // top full
while(time1[T1] <= 2000 && (!getButtonPress(UP_BUTTON)))
{
}
drawBmpfile(0,127,"Hourglass 1"); // bottom full
while(time1[T1] <= 4000 && (!getButtonPress(UP_BUTTON)))
{
}
drawBmpfile(0,127,"Hourglass 2"); // turn
while(time1[T1] <= 4800 && (!getButtonPress(UP_BUTTON)))
{
}
}
// test timer
void timer()
{
displayCenteredBigTextLine(3,"Press up to");
displayCenteredBigTextLine(6,"begin the timer");
displayCenteredBigTextLine(9,"and");
displayCenteredBigTextLine(12,"end the timer");
while(!getButtonPress(UP_BUTTON))
{
}
while(getButtonPress(UP_BUTTON))
{
}
time1[T2]=0;
eraseDisplay();
while(!getButtonPress(UP_BUTTON))
{
hourglassVisual();
}
while(getButtonPress(UP_BUTTON))
{
}
int totalSeconds = time1[T2]/1000.0;
int testSeconds = 0, testMinutes = 0, testHours = 0;
eraseDisplay();
testMinutes = totalSeconds / 60;
testSeconds = totalSeconds % 60;
testHours = testMinutes / 60;
testMinutes = testMinutes % 60;
displayCenteredBigTextLine(2,"Your time is:");
displayBigTextLine (6, "%d hours", testHours);
displayBigTextLine (9, "%d minutes", testMinutes);
displayBigTextLine (12, "%d seconds", testSeconds);
wait1Msec(5000);
}
// calculates test score
float calculateGrade (int writtenAnswer, int trueAnswer)
{
int grade=(writtenAnswer/trueAnswer)*100;
return grade;
}
// test mode function
void testMode()
{
// timer
timer();
// file for answers
TFileHandle fileIn;
bool fileCondition = openReadPC(fileIn, "answers.txt");
if (!fileCondition)
{
displayString(5,"Error!");
wait1Msec(5000);
}
//Array for collected answer
string inputAns [QUESTIONS_NUM-1] = {""};
else
{
//readCharPC (fileIn,questionNum);
// putting answers into an array
char answers[QUESTIONS_NUM-1]={''};
int questionNum=0;
while (readCharPC (fileIn,answers[questionNum]))
{
questionNum++
}
//drive and scan
driveStop();
drivePower(20);
int answerNum = 0;
//Drive until black color
while ( SensorValue[COLOR_PORT] != (int)colorBlack )
{
//Need to save colors into an array when its specific 1/4 (RGBY)
if(SensorValue[COLOR_PORT] == (int)colorRed ||SensorValue[COLOR_PORT] == (int)colorBlue ||SensorValue[COLOR_PORT] == (int)colorGreen ||SensorValue[COLOR_PORT] == (int)colorYellow)
{
inputAns[i] == SensorValue[COLOR_PORT].color;
answerNum++;
}
}
driveStop();
int correctAnswers = 0;
for (int count = 0; count < QUESTIONS_NUM ; count++)
{
if(inputAns[count] == answers[count])
{
correctAnswers++;
}
}
float grade = calculateGrade(correctAnswers, QUESTIONS_NUM);
displayCenteredBigTextLine (5, "You scored %d/%d", correctAnswers, QUESTIONS_NUM);
displayCenteredBigTextLine (9, "%f percent", grade);
if (grade<60)
{
playSoundFile("Boo.rsf");
wait1Msec(5000);
}
else if (grade=>60)
{
playSoundFile("Good job.rsf");
wait1Msec(5000);
}
else
{
playSoundFile("Error.rsf");
wait1Msec(5000);
}
}
}
task main ()
{
configureAllSensors();
int done=1;
int mode = -1;
while (done!=0)
{
while (!(mode==1||mode==2))
{
mode = modeChoose();
}
if (mode == 1)
{
studyMode((int)colorGreen, (int)colorWhite);
done=0;
}
else if (mode==2)
{
testMode();
done=0;
}
}
return;
}