forked from andras-adam/zumo-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vasilydavydov.c
657 lines (521 loc) · 17.2 KB
/
vasilydavydov.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/* ========================================
* Vasily Davydov
* Zumo Robot Project
* Projects made with András Ádám
* Metropolia
* 2020
* ========================================
*/
#include <vasilydavydov.h>
/*
List of contains in this file:
- Tasks (lines 21-321)
- Projects (lines 335-468)
- Functions for Tasks & Projects (lines 487-657)
- Project Maze is contained in a separate file called "maze.c"
*/
// TASKS START HERE
void assignment_week3_1(void) {
motor_start();
motor_forward(0, 0);
// Track navigation
motor_forward(255, 4000);
motor_turn(200, 0, 262);
motor_forward(255, 1020);
motor_turn(200, 0, 262);
motor_forward(255, 1040);
motor_turn(255, 0, 250);
motor_forward(255, 250);
motor_turn(255, 200, 600);
motor_turn(255, 0, 31);
motor_forward(255, 390);
// Shut down robot
shut();
}
void assignment_week3_2 (void){
//starting motors and Ultrasonic sensor
motor_start();
Ultra_Start();
//starting from a zero speed
motor_forward(0, 0);
//entering an infinite loop
while (true) {
motor_forward(120, 50); //giving speed to motors
// vTaskDelay(100);
if (Ultra_GetDistance() < 11){ //assigning the value of the function (10cm) to an if-statement
obstacle();
}
}
}
//These functions allow to make a tankturn either left or right:
//200 speed 262 delay is 90 degrees
void tank_turn_right(uint8 speed,uint32 delay){
// SetMotors(0,0, l_speed, r_speed, delay);
SetMotors(0,1, speed, speed, delay);
}
void tank_turn_left(uint8 speed,uint32 delay){
// SetMotors(0,0, l_speed, r_speed, delay);
SetMotors(1,0, speed, speed, delay);
}
void assignment_week3_3 ()
{
launch_system(true, true, true, true);
//entering an infinite loop
while (SW1_Read()==1) {
// vTaskDelay(100);
if (Ultra_GetDistance() < 11){ //assigning the value of the function (10cm) to an if-statement
motor_backward(100,150);
int angle = rand()%524+262;
if (rand()%2 == 1){
tank_turn_right(200, angle);
}else {
tank_turn_left(200, angle);
}
}
motor_forward(200, 50); //giving speed to motors
}
motor_forward(0,0);//stoppimg the motor
motor_stop();
}
void assignment_week4_1()
{
//variables declaration and engine launch
int lines = 0;
struct sensors_ sensors;
launch_system(true, true, true, true);
//Enter to loops of intersections
while(lines <5)
{
line_follower(&sensors);
lines++;
printf("We on line %d\n", lines);
//Witing for the IR-signal
if(lines == 1)
{
IR_flush();
IR_wait();
}
}
}
void assignment_week4_2()
{
//variables declaration and engine launch
int lines = 0;
struct sensors_ sensors;
launch_system(true, true, true, true);
//Switching the LED on
BatteryLed_Write(1);
//Starting the function, when the button is pressed
while(SW1_Read() == 1);
BatteryLed_Write(0);
vTaskDelay(1000);
while(lines <3)
{
line_follower(&sensors);
lines++;
//Checking on which line we are
printf("We on line %d\n", lines);
//Witing for the IR-signal
if(lines == 1)
{
IR_flush();
IR_wait();
}
}
shut();
}
void assignment_week4_3()
{
//variables declaration and engine launch
int lines = 0;
struct sensors_ sensors;
launch_system(true, true, true, true);
//Switching the LED on
BatteryLed_Write(1);
//Starting the function, when the button is pressed
while(SW1_Read() == 1);
BatteryLed_Write(0);
vTaskDelay(1000);
//stop on the last intersection
while(lines < 5)
{
line_follower(&sensors);
lines++;
//Wait for the signal on the first intersection
if(lines == 1)
{
IR_flush();
IR_wait();
//turn left on the second intersection
} else if(lines == 2)
{
while(!getRefValues(&sensors, 0,0,1,1,0,0))
{
motor_turn(10,200,10);
reflectance_digital(&sensors);
}
//turn right on the third intersection
}else if(lines == 3)
{
while(!getRefValues(&sensors, 0,0,1,1,0,0))
{
motor_turn(200,10,10);
reflectance_digital(&sensors);
}//turn right on the fourth intersection
}else if(lines == 4)
{
while(!getRefValues(&sensors, 0,0,1,1,0,0))
{
motor_turn(200,10,10);
reflectance_digital(&sensors);
}
}
}
//shuttin the system
shut();
}
void assignment_week5_1()
{
TickType_t press1= xTaskGetTickCount();
//Switching the LED on
BatteryLed_Write(1);
//Starting the function, when the button is pressed
while(SW1_Read() == 1);
BatteryLed_Write(0);
vTaskDelay(1000);
while(1)
{
//button is not pressed
while(SW1_Read() == 1)vTaskDelay(10);
BatteryLed_Write(0);
TickType_t press2= xTaskGetTickCount();
//getting the difference
int difference = (int)(press2) - (int)(press1);
//Assigning first press value to the next one, so the start point is always new
press1 = press2;
printf("\nMilliseconds from the previous button press: %d\n", difference);
print_mqtt("Zumo99/button", "%d", difference);
//button is pressed
while(SW1_Read() == 0)vTaskDelay(10);
BatteryLed_Write(1);
}
}
void assignment_week5_2()
{
launch_system(true, true, false, true);
//eternal loop
while(1)
{
//sensor gets the obstacle
if(Ultra_GetDistance() < 11){
motor_forward(0,0);
vTaskDelay(500);
motor_backward(100,300);
//making a decision
if(rand()%2 == 1)
{
tank_turn_right(100,262);
print_mqtt("Zumo99/turn: ", "%s", "right");
}
else
{
tank_turn_left(100,262);
print_mqtt("Zumo99/turn", "%s", "left");
}
}
//continues to roll
motor_forward(200,10);
}
}
void assignment_week5_3()
{
struct sensors_ sensors;
TickType_t line1 = 0;
TickType_t line2 = 0;
int difference;
launch_system(true, true, true, true);
BatteryLed_Write(1);
//Starting the function, when the button is pressed
while(SW1_Read() == 1);
BatteryLed_Write(0);
vTaskDelay(1000);
while (SW1_Read() == 1)
{
line_follower(&sensors);
if(line1)
{
line2 = xTaskGetTickCount();
difference = (int)(line2)-(int)(line1);
print_mqtt("Zumo99/lap", "%d", difference);
}
IR_flush();
IR_wait();
line1 = xTaskGetTickCount();
}
}
// TASKS END HERE
// PROJECTS START HERE
void project_line()
{
//variables declaration and engine launch
int lines = 0;
struct sensors_ sensors;
TickType_t launch = 0;
TickType_t shutt = 0;
launch_system(true, true, true, true);
//Switching the LED on
BatteryLed_Write(1);
//Starting the function, when the button is pressed
while(SW1_Read() == 1);
BatteryLed_Write(0);
vTaskDelay(1000);
while(lines <3)
{
line_follower_bonus(&sensors, &launch);
lines++;
//Checking on which line we are
printf("We on line %d\n", lines);
//Waiting for the IR-signal
if(lines == 1)
{
print_mqtt("Zumo99/ready", "line");
IR_flush();
IR_wait();
launch = xTaskGetTickCount();
print_mqtt("Zumo99/start","%d", launch);
}else if(lines == 3)
{
shutt = xTaskGetTickCount();
print_mqtt("Zumo99/stop","%d", shutt);
int difference = (int)(shutt)-(int)(launch);
print_mqtt("Zumo99/time", "%d", difference);
//bonus features
}
}
shut();
}
void project_sumo()
{
//variables declaration and engine launch
int turn_dir = rand()%2;
struct sensors_ sensors;
TickType_t launch = 0;
TickType_t shutt = 0;
launch_system(true, true, true, true);
//Switching the LED on
BatteryLed_Write(1);
//Starting the function, when the button is pressed
while(SW1_Read() == 1);
BatteryLed_Write(0);
vTaskDelay(1000);
launch = xTaskGetTickCount();
line_follower(&sensors);
print_mqtt("Zumo99/ready","zumo");
IR_flush();
IR_wait();
print_mqtt("Zumo99/start", "%d", launch);
while(getRefValues(&sensors, 1,1,1,1,1,1))
{
motor_forward(200,150);
reflectance_digital(&sensors);
}
motor_forward(0,0);
//allow this function to work while the button is not pressed
while(SW1_Read() == 1)
{
//recognizing the obstacle
if(Ultra_GetDistance() < 10)
{
print_mqtt("Zumo99/obstacle", "%d", xTaskGetTickCount());
motor_forward(0,0);
if(turn_dir == 1)
{
tank_turn_left(150, 262);
motor_forward(200,1);
reflectance_digital(&sensors);
}else
{
tank_turn_right(150, 262);
motor_forward(200,1);
reflectance_digital(&sensors);
}
}
//Turning back from edges
if(sensors.R3 == 1)
{
motor_forward(0,0);
while(!getRefValues(&sensors,0,0,0,0,0,0))
{
tank_turn_left(255,rand()%150+50);
reflectance_digital(&sensors);
}
}else if(sensors.L3 == 1)
{
motor_forward(0,0);
while(!getRefValues(&sensors,0,0,0,0,0,0))
{
tank_turn_right(255,rand()%150+50);
reflectance_digital(&sensors);
}
}
motor_forward(200,1);
reflectance_digital(&sensors);
}
BatteryLed_Write(1);
motor_forward(0, 0);
shutt = xTaskGetTickCount();
print_mqtt("Zumo99/stop", "%d", shutt);
print_mqtt("Zumo99/time", "%d", shutt - launch);
//shutting down
shut();
}
// PROJECTS END HERE (MAZE IS IN "maze.c")
// FUNCTIONS START HERE
// function to launch the engine
void launch_system(bool motor, bool IR, bool reflectance, bool ultrasonic)
{
printf("\nPreparing for a stratup\n");
//motor starts
if (motor)
{
motor_start();
motor_forward(0, 0);
}
//IR starts
if (IR)
{
IR_Start();
IR_flush();
}
//reflectance startup
if (reflectance)
{
reflectance_start();
reflectance_set_threshold(15000, 15000, 18000, 18000, 15000, 15000);
}
//ultrasonic starts
if (ultrasonic)
{
Ultra_Start();
}
printf("\nStartup Done Successfully!\n");
}
// function that shuts the robot
void shut(void){
motor_forward(0,0);
motor_stop();
}
// function for turn after the obstacle
void obstacle (){
motor_forward(0,10);
motor_backward(100, 150);
motor_turn(0, 150, 462);
}
//Function that allows to follow the line
void line_follower(struct sensors_ *sensors)
{
reflectance_digital(sensors);
//Going through the intersection
while(getRefValues(sensors, 1, 1,1,1,1,1))
{
motor_forward(100,10);
reflectance_digital(sensors);
}
while(!getRefValues(sensors, 1, 1, 1, 1, 1, 1))
{
//Left Turn
while(sensors->R2 == 0 && sensors->L2 == 1)
{
tank_turn_left(255,1);
reflectance_digital(sensors);
}
//Right Turn
while(sensors->R2 == 1 && sensors->L2 == 0)
{
tank_turn_right(255, 1);
reflectance_digital(sensors);
}
//These are bonus features, for example on a track "rectangle", the third turn is over 90 degrees
//Left turn over 90 degrees
while(sensors->R2 == 1 && sensors->R3 == 0 && sensors->L2 == 1 && sensors->L3 == 1)
{
tank_turn_left(255, 1);
reflectance_digital(sensors);
}
//Right turn over 90 degrees
while(sensors->R2 == 1 && sensors->R3 == 1 && sensors->L2 == 1 && sensors->L3 == 0)
{
tank_turn_right(255, 1);
reflectance_digital(sensors);
}
motor_forward(150, 10);
reflectance_digital(sensors);
}
motor_forward(0,0);
}
//Function that allows to follow the line of the project
void line_follower_bonus (struct sensors_ *sensors, TickType_t *launch)
{
bool on_line = true;
reflectance_digital(sensors);
//Going through the intersection
while(getRefValues(sensors, 1, 1,1,1,1,1))
{
motor_forward(100,10);
reflectance_digital(sensors);
}
while(!getRefValues(sensors, 1, 1, 1, 1, 1, 1))
{
//Left Turn
while(sensors->R2 == 0 && sensors->L2 == 1)
{
tank_turn_left(255,1);
reflectance_digital(sensors);
}
//Right Turn
while(sensors->R2 == 1 && sensors->L2 == 0)
{
tank_turn_right(255, 1);
reflectance_digital(sensors);
}
//These are bonus features, for example on a track "rectangle", the third turn is over 90 degrees
//Left turn over 90 degrees
while(sensors->R2 == 1 && sensors->R3 == 0 && sensors->L2 == 1 && sensors->L3 == 1)
{
tank_turn_left(255, 1);
reflectance_digital(sensors);
}
//Right turn over 90 degrees
while(sensors->R2 == 1 && sensors->R3 == 1 && sensors->L2 == 1 && sensors->L3 == 0)
{
tank_turn_right(255, 1);
reflectance_digital(sensors);
}
//bonus features for the project
if(on_line == true && getRefValues(sensors, 0,0,0,0,0,0))
{
on_line = false;
print_mqtt("Zumo99/miss", "%d", xTaskGetTickCount()-*launch);
} else if(on_line == false && getRefValues(sensors, 0,0,1,1,0,0))
{
on_line = true;
print_mqtt("Zumo99/line", "%d", xTaskGetTickCount()-*launch);
}
motor_forward(100, 10);
reflectance_digital(sensors);
}
//Stopping the motors
motor_forward(0,0);
}
int getRefValues (struct sensors_ *sensors, int L3, int L2, int L1, int R1, int R2, int R3)
{
if (sensors->L1 == L1 && sensors->L2 == L2 && sensors->L3 == L3 && sensors->R1 == R1 && sensors->R2 == R2 &&sensors->R3 == R3 )
{
return 1;
}else
{
return 0;
}
}
// FUNCTIONS END HERE
/* [] END OF FILE */