-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorDetectingVehicle_RPF511.c
More file actions
419 lines (361 loc) · 8.52 KB
/
ColorDetectingVehicle_RPF511.c
File metadata and controls
419 lines (361 loc) · 8.52 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
413
414
415
416
417
418
419
#pragma config(Sensor, S1, ts , sensorEV3_Touch)
#pragma config(Sensor, S2, gs , , sensorEV3_Gyro)
#pragma config(Sensor, S3, cs, sensorEV3_Color, modeEV3Color_Color)
#pragma config(Sensor, S4, ss, sensorEV3_Ultrasonic)
#pragma config(Motor, motorB, lm, tmotorEV3_Large, PIDControl, encoder)
#pragma config(Motor, motorC, rm, tmotorEV3_Large, PIDControl, encoder)
#define BLACK 1
#define BLUE 2
#define GREEN 3
#define YELLOW 4
#define RED 5
#define WHITE 6
#define BROWN 7
#define STARTCOLOR 4
#define FORWARD 0
#define ONCOLOR 1
#define TURNING 2
#define SLOWDOWN 3
#define OBSTACLE 4
#define BEEPBEEP 5
#define STARTED 6
#define TICKRATE 20
#define COLOR_QUEUE_SIZE 20
#define SPEED_MAX 8
char * num_to_color(int col);
void print_stat(void);
void init_color_queue(void);
int wait_touch(void);
void update_color(void);
int calculate_current_color(void);
void setstat(int s);
void addstat(int shiftleft);
void delstat(int shiftleft);
int checkstat(int shiftleft);
void setMotor(int leftspeed, int rightspeed);
void beepbeep(int count);
void point_turn_by_angle(int angle_ex, int degree);
void proceed_by_s(int sp, int s);
void distance_decision(void);
void color_decision(void);
void action(void);
// 0000000 : stop
// 0000001 : forward
// 0000010 : in some color
// 0000100 : turning
// 0001000 : slow down
// 0010000 : on obstacle
// 0100000 : beepbeep
// 1000000 : start
int stat = 0;
int color_queue[COLOR_QUEUE_SIZE];
int color_num[8];
int color_queue_index = 0;
int ex_color = 6;
int current_color = 6;
int black_count = 0;
//int beepbeep_count = 0;
//int beepbeep_cooldown = 0;
float time = 0;
int speed = SPEED_MAX;
int escape_dir = 1;
int distancein = 0;
task main()
{
//clearSounds();
resetGyro(gs);
while(1){
update_color();
print_stat();
color_decision();
print_stat();
if(!checkstat(TURNING)){
distance_decision();
}
action();
print_stat();
}
}
char * num_to_color(int col){
if(col == BLACK) return "BLACK";
if(col == BLUE) return "BLUE";
if(col == GREEN) return "GREEN";
if(col == YELLOW) return "YELLOW";
if(col == RED) return "RED";
if(col == WHITE) return "WHITE";
if(col == BROWN) return "BROWN";
return "NOTCOLOR";
}
void print_stat(){
displayBigTextLine(3, "%s %s %s %s %s %s %s",(checkstat(FORWARD)?"F":"-"),(checkstat(ONCOLOR)?"C":"-"),(checkstat(TURNING)?"T":"-"),(checkstat(SLOWDOWN)?"S":"-"),(checkstat(OBSTACLE)?"O":"-"),(checkstat(BEEPBEEP)?"B":"-"),(checkstat(STARTED)?"S":"-"));
displayBigTextLine(5, "e %s c %s",num_to_color(ex_color),num_to_color(current_color));
displayBigTextLine(7, "bc %d",black_count);
displayBigTextLine(9, "gyro %d",getGyroDegrees(gs));
}
void init_color_queue(){
for(int i = 0 ; i < COLOR_QUEUE_SIZE ; i++ ){
color_queue[i] = 6;
}
for(int i = 0 ; i < 8 ; i++ ){
color_num[i] = 0;
}
color_num[6] = COLOR_QUEUE_SIZE;
color_queue_index = 0;
current_color = 6;
black_count = 0;
//beepbeep_count = 0;
}
int wait_touch(void){
while(getTouchValue(ts) == 0){}
while(getTouchValue(ts) == 1){}
return 1;
}
void update_color(void){
int col = getColorName(cs);
//for green
/*
if(col == 3){
displayBigTextLine(15, "green");
updatestat(3);
current_color = GREEN;
return;
}*/
if(col > 7 || col == 0) return;
color_num[color_queue[color_queue_index]]--;
color_num[col]++;
if( color_queue[color_queue_index] != col){
color_queue[color_queue_index] = col;
current_color = calculate_current_color();
}
color_queue_index = (color_queue_index + 1) % COLOR_QUEUE_SIZE;
return;
}
int calculate_current_color(void) {
int max = 0;
int temp_color = 6;
for(int i = 1; i < 8; i++){
if(color_num[i] > max){
max = color_num[i];
temp_color = i;
}
}
return temp_color;
}
void setstat(int s){
stat = s;
return;
}
void addstat(int shiftleft){
int base = 1 << shiftleft;
stat = stat | base;
return;
}
void delstat(int shiftleft){
int base = ~(1 << shiftleft);
stat = stat & base;
return;
}
int checkstat(int shiftleft){
int base = (1 << shiftleft);
return stat & base;
}
void setMotor(int leftspeed, int rightspeed){
setMotorSpeed(lm, leftspeed);
setMotorSpeed(rm, rightspeed);
}
void beepbeep(int count) {
for(int i = 0 ; i< count ; i++){
displayBigTextLine(11, "beep loop %d to %d",i,count);
playSound(soundBeepBeep);
}
delstat(BEEPBEEP);
//clearSounds();
displayBigTextLine(11, "beep loop done");
/*
int cooldown = 0;
while (count > 0) {
if(cooldown){
cooldown = 0;
count--;
sleep(5);
}
else{
cooldown = 1;
playSound(soundBeepBeep);
sleep(2);
}
}*/
}
void point_turn_by_angle(int angle_ex, int degree){
if(degree > 0 ) {
/*
repeatUntil(getGyroDegrees(gs) < angle_ex + degree){
setMotor(-speed,speed);
}*/
setMotor(speed,-speed);
while(getGyroDegrees(gs) < angle_ex + degree){
displayBigTextLine(9, "g %d to %d ",getGyroDegrees(gs),angle_ex + degree);
sleep(TICKRATE);
}
return;
}else{
/*
repeatUntil(getGyroDegrees(gs) < angle_ex + degree){
setMotor(speed,-speed);
}*/
setMotor(-speed,speed);
while(getGyroDegrees(gs) > angle_ex + degree){
displayBigTextLine(9, "g %d to %d",getGyroDegrees(gs),angle_ex + degree);
sleep(TICKRATE);
}
return;
}
}
void proceed_by_s(int sp, int s){
setMotor(sp,sp);
sleep(1000*s / abs(sp));
}
void distance_decision(void){
if(getUSDistance(ss) > 20 && !checkstat(TURNING)){
delstat(SLOWDOWN);
distancein = 0;
} else if (!checkstat(TURNING) && checkstat(FORWARD)){
if(getUSDistance(ss) < 20){
if(!distancein){
beepbeep(1);
distancein = 1;
}
addstat(SLOWDOWN);
}
if(getUSDistance(ss) < 10){
distancein = 1;
addstat(SLOWDOWN);
addstat(TURNING);
delstat(ONCOLOR);
escape_dir = 2;
return;
}
}
}
void color_decision(void){
if(current_color != WHITE && current_color != 0){
ex_color = current_color;
addstat(ONCOLOR);
return;
}
if(checkstat(ONCOLOR) && current_color==WHITE){
if(ex_color == BLACK){
//if(++black_count == 10) delstat(FORWARD);
black_count++;
} else if(ex_color == BLUE){
addstat(BEEPBEEP);
//speed = 0;
} else if(ex_color == STARTCOLOR){
if(!checkstat(STARTED)){
addstat(ONCOLOR);
addstat(STARTED);
addstat(FORWARD);
clearTimer(T1);
setLEDColor(ledGreen);
} else{
delstat(FORWARD);
}
}
if(ex_color == RED){
addstat(ONCOLOR);
addstat(SLOWDOWN);
addstat(TURNING);
escape_dir = 1;
}
ex_color = WHITE;
delstat(ONCOLOR);
return;
}
/*
if(!checkstat(ONCOLOR)){
if(current_color == BLACK){
addstat(ONCOLOR);
if(++black_count == 10) delstat(FORWARD);
return;
}
if(current_color == BLUE){
addstat(ONCOLOR);
addstat(BEEPBEEP);
speed = 0;
return;
}
if(current_color == RED){
addstat(ONCOLOR);
addstat(SLOWDOWN);
addstat(TURNING);
escape_dir = 1;
return;
}
if(current_color == STARTCOLOR){
if(!checkstat(STARTED)){
addstat(ONCOLOR);
addstat(STARTED);
addstat(FORWARD);
clearTimer(T1);
setLEDColor(ledGreen);
return;
} else{
delstat(FORWARD);
return;
}
}
} else{
if(current_color == WHITE) delstat(ONCOLOR);
}*/
}
void action(void){
print_stat();
if(checkstat(SLOWDOWN)){
speed = SPEED_MAX/2;
}else{
speed = SPEED_MAX;
}
if(checkstat(TURNING)){
if(escape_dir == 1){
point_turn_by_angle(getGyroDegrees(gs), -90);
proceed_by_s(speed, 10*black_count);
point_turn_by_angle(getGyroDegrees(gs), -90);
delstat(TURNING);
delstat(SLOWDOWN);
delstat(ONCOLOR);
}
if(escape_dir == 2){
proceed_by_s(-speed, 20);
point_turn_by_angle(getGyroDegrees(gs), 90);
proceed_by_s(speed, 10*black_count);
point_turn_by_angle(getGyroDegrees(gs), 90);
delstat(TURNING);
delstat(SLOWDOWN);
}
}
if(checkstat(BEEPBEEP)){
//setMotor(0,0);
if(black_count==0) beepbeep(1);
else beepbeep(black_count);
speed = SPEED_MAX;
}
if(black_count == 10) delstat(FORWARD);
if(!checkstat(FORWARD)){
setMotor(0,0);
if(checkstat(STARTED)){
time = time1[T1];
displayBigTextLine (1, "record = %.2f sec", time);
delstat(STARTED);
}else{
init_color_queue();
black_count = 0;
if(wait_touch()){
addstat(FORWARD);
}
}
}
if(checkstat(FORWARD)&& !checkstat(TURNING)){
setMotor(speed,speed);
}
sleep(TICKRATE);
}