-
Notifications
You must be signed in to change notification settings - Fork 0
/
Final.c
1373 lines (1162 loc) · 34.5 KB
/
Final.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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* EURECOM Operating Systems Robot Challenge 2017
* Delivery: 15/01/2018
* Group Number 5 SegFault
* @version 5.0 14/01/2017
* @author Berkay, Jermaine, Ariane, Yasmine
* Disclaimer: Since we all live together, all of our meetings were done with all of us involved.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "ev3.h"
#include "ev3_port.h"
#include "ev3_tacho.h"
#include "ev3_sensor.h"
#include "coroutine.h"
#include "ev3_servo.h"
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
// WIN32 /////////////////////////////////////////
#ifdef __WIN32__
#include <windows.h>
// UNIX //////////////////////////////////////////
#else
#include <unistd.h>
#define Sleep( msec ) usleep(( msec ) * 1000 )
#define SERV_ADDR "40:E2:30:8E:5D:DC" /* Whatever the address of the server is */
#define TEAM_ID 5 /* Your team ID */
#define MSG_ACK 0
#define MSG_START 1
#define MSG_STOP 2
#define MSG_KICK 3
#define MSG_POSITION 4
#define MSG_MAPDATA 5
#define MSG_MAPDONE 6
#define MSG_OBSTACLE 7
#define MAP_WIDTH 80
#define MAP_HEIGHT 80
//////////////////////////////////////////////////
#endif
const char const *color[] = { "?", "BLACK", "BLUE", "GREEN", "YELLOW", "RED", "WHITE", "BROWN" };
#define COLOR_COUNT (( int )( sizeof( color ) / sizeof( color[ 0 ])))
// GLOBAL VARIABLES
time_t start, end; //Start and End times of the program
double dif; //Time passed since the start of the challenge
char ROBOT_DIRECTION = 'E'; //Default starting direction
int ROBOT_X = 10;//starting point X
int ROBOT_Y = 2;//starting point Y
bool releaseobject = true;//Can I release an obstacle? Or did I released before?
bool releaseobjectmessagesent = false;//Did I notify the server for the obstacle I dropped?
char map[256][256] = { ' ' };//The map array. 256x256 because that is the maximum size allowed for the server.
//Bluetooth Server veriables
char string[58];
int s;
uint16_t msgId = 0;
// WIN32 /////////////////////////////////////////
#ifdef __WIN32__
#include <windows.h>
// UNIX //////////////////////////////////////////
#else
#include <unistd.h>
#define Sleep( msec ) usleep(( msec ) * 1000 )
#endif
#define L_MOTOR_PORT OUTPUT_C
#define L_MOTOR_EXT_PORT EXT_PORT__NONE_
#define R_MOTOR_PORT OUTPUT_B
#define R_MOTOR_EXT_PORT EXT_PORT__NONE_
#define IR_CHANNEL 0
#define SPEED_LINEAR 75 /* Motor speed for linear motion, in percents */
#define SPEED_CIRCULAR 50 /* ... for circular motion */
int max_speed; /* Motor maximal speed */
#define DEGREE_TO_COUNT( d ) (( d ) * 260 / 90 )
int app_alive;
//The type of movements the robot is capable of
enum {
//Non Turning movements
MOVE_NONE,
MOVE_FORWARD,
MOVE_BACKWARD,
STEP_BACKWARD,
//Turning movements
TURN_LEFT,//90 degrees
TURN_RIGHT,//90 degrees
TURN_ANGLE,//xx degrees
//Customised movements
MOVE_BACKWARDS_RANDOMLY,//Moves backwards right or left(Undewr control of the touch sensor)
RELEASE_OBJECT,//Releases an object using the engine
TURN_TO_TARGET,//Scan 10 degrees in front, turn to the target, get close to it, read its color, save the map values, go backwards to original spot, fix orientation
};
int moving; /* Current moving */
int command; /* Command for the 'drive' coroutine */
int angle; /* Angle of rotation */
enum { L, R };
uint8_t motor[3] = { DESC_LIMIT, DESC_LIMIT, DESC_LIMIT }; /* Sequence numbers of motors */
//Sensor variables
FLAGS_T state;
uint8_t sn;
uint8_t sn_touch;
uint8_t sn_color;
uint8_t sn_compass;
uint8_t sn_sonar;
uint8_t sn_mag;
uint8_t sn_gyro;
//Custom functions from tester.c and robotclient.c
int read_from_server(int sock, char *buffer, size_t maxSize) {
int bytes_read = read(sock, buffer, maxSize);
if (bytes_read <= 0) {
fprintf(stderr, "Server unexpectedly closed connection...\n");
close(s);
exit(EXIT_FAILURE);
}
printf("[DEBUG] received %d bytes\n", bytes_read);
return bytes_read;
}
static void _run_forever(int l_speed, int r_speed)
{
set_tacho_speed_sp(motor[L], l_speed);
set_tacho_speed_sp(motor[R], r_speed);
multi_set_tacho_command_inx(motor, TACHO_RUN_FOREVER);
}
static void _run_to_rel_pos(int l_speed, int l_pos, int r_speed, int r_pos)
{
set_tacho_speed_sp(motor[L], l_speed);
set_tacho_speed_sp(motor[R], r_speed);
set_tacho_position_sp(motor[L], l_pos);
set_tacho_position_sp(motor[R], r_pos);
multi_set_tacho_command_inx(motor, TACHO_RUN_TO_REL_POS);
}
static void _run_timed(int l_speed, int r_speed, int ms)
{
set_tacho_speed_sp(motor[L], l_speed);
set_tacho_speed_sp(motor[R], r_speed);
multi_set_tacho_time_sp(motor, ms);
multi_set_tacho_command_inx(motor, TACHO_RUN_TIMED);
}
static int _is_running(void)
{
FLAGS_T state = TACHO_STATE__NONE_;
get_tacho_state_flags(motor[L], &state);
if (state != TACHO_STATE__NONE_) return (1);
get_tacho_state_flags(motor[R], &state);
if (state != TACHO_STATE__NONE_) return (1);
return (0);
}
static void _stop(void)
{
multi_set_tacho_command_inx(motor, TACHO_STOP);
}
/*--------------app_init -----
| Function app_init
| Purpose: Initialises the tacho engines (right and left wheel) connected.
| Returns: 0 for success and 1 for failure
*------------------------------*/
int app_init(void)
{
char s[16];
if (ev3_search_tacho_plugged_in(L_MOTOR_PORT, L_MOTOR_EXT_PORT, motor + L, 0)) {
get_tacho_max_speed(motor[L], &max_speed);
/* Reset the motor */
set_tacho_command_inx(motor[L], TACHO_RESET);
}
else {
printf("LEFT motor (%s) is NOT found.\n", ev3_port_name(L_MOTOR_PORT, L_MOTOR_EXT_PORT, 0, s));
/* Inoperative without left motor */
return (0);
}
if (ev3_search_tacho_plugged_in(R_MOTOR_PORT, R_MOTOR_EXT_PORT, motor + R, 0)) {
/* Reset the motor */
set_tacho_command_inx(motor[R], TACHO_RESET);
}
else {
printf("RIGHT motor (%s) is NOT found.\n", ev3_port_name(R_MOTOR_PORT, R_MOTOR_EXT_PORT, 0, s));
/* Inoperative without right motor */
return (0);
}
command = moving = MOVE_NONE;
return(1);
}
/*
Turn right and Turn left Attempts:
1) Compass Failure: Compass doesnt work when close to the board. Compass was unable to operate well during movement. Removed compass (Berkay)
2) Timed Turns: Timed turns are not working properly when battery is low. Not always reliable. (Yasmine)
3) Gyro Turns: Gyrosensor works great on turns. With almost zero tolerance. Current reaction time 50ms. (Jermaine)
*/
/*--------------turnright -----
| Function turnright
| Purpose: Turns the robot to 90 degrees right using the Gyro sensor value
*------------------------------*/
void turnright()
{
printf("turning right\n");
float current_pos;
get_sensor_value0(sn_gyro, ¤t_pos);
float final_pos = current_pos + 90.0;
int diff = (int)(final_pos - current_pos);
bool turn_right;
//printf("CURRENTPOS:%f FINALPOS:%f DIFF:%d\n", current_pos, final_pos, diff);
while (abs(current_pos - final_pos) != 0)
{
turn_right = (diff > 0);
uint8_t right_wheel, left_wheel;
int max_speed;
ev3_search_tacho_plugged_in(67, 0, &right_wheel, 0);
ev3_search_tacho_plugged_in(66, 0, &left_wheel, 0);
get_tacho_max_speed(right_wheel, &max_speed);
set_tacho_stop_action_inx(right_wheel, TACHO_COAST);
set_tacho_stop_action_inx(left_wheel, TACHO_COAST);
set_tacho_speed_sp(right_wheel, max_speed * (turn_right ? 1 : -1) * 0.1);
set_tacho_speed_sp(left_wheel, max_speed * (turn_right ? -1 : 1) * 0.1);
set_tacho_time_sp(right_wheel, 50);
set_tacho_time_sp(left_wheel, 50);
set_tacho_command_inx(right_wheel, TACHO_RUN_TIMED);
set_tacho_command_inx(left_wheel, TACHO_RUN_TIMED);
get_sensor_value0(sn_gyro, ¤t_pos);
diff = (int)(final_pos - current_pos);
//printf("CURRENTPOS:%f FINALPOS:%f DIFF:%d\n", current_pos, final_pos, diff);
}
}
/*--------------turnleft -----
| Function turnleft
| Purpose: Turns the robot to 90 degrees left using the Gyro sensor value
*------------------------------*/
void turnleft()
{
printf("turning left\n");
float current_pos;
get_sensor_value0(sn_gyro, ¤t_pos);
float final_pos = current_pos - 90.0;
int diff = (int)(final_pos - current_pos);
bool turn_right;
//printf("CURRENTPOS:%f FINALPOS:%f DIFF:%d\n", current_pos, final_pos, diff);
while (abs(current_pos - final_pos) != 0)
{
turn_right = (diff > 0);
uint8_t right_wheel, left_wheel;
int max_speed;
ev3_search_tacho_plugged_in(67, 0, &right_wheel, 0);
ev3_search_tacho_plugged_in(66, 0, &left_wheel, 0);
get_tacho_max_speed(right_wheel, &max_speed);
// get_tacho_max_speed( left_wheel, &max_speed );
set_tacho_stop_action_inx(right_wheel, TACHO_COAST);
set_tacho_stop_action_inx(left_wheel, TACHO_COAST);
set_tacho_speed_sp(right_wheel, max_speed * (turn_right ? 1 : -1) * 0.1);
set_tacho_speed_sp(left_wheel, max_speed * (turn_right ? -1 : 1) * 0.1);
set_tacho_time_sp(right_wheel, 50);
set_tacho_time_sp(left_wheel, 50);
// set_tacho_ramp_up_sp( right_wheel, 100 );
// set_tacho_ramp_up_sp( left_wheel,100 );
set_tacho_command_inx(right_wheel, TACHO_RUN_TIMED);
set_tacho_command_inx(left_wheel, TACHO_RUN_TIMED);
get_sensor_value0(sn_gyro, ¤t_pos);
diff = (int)(final_pos - current_pos);
//printf("CURRENTPOS:%f FINALPOS:%f DIFF:%d\n", current_pos, final_pos, diff);
}
}
//CORO functions defined that will handle the movement decisions
CORO_CONTEXT(handle_ir_proximity);//handles proximity and color sensor
CORO_CONTEXT(drive);//drives the engines and executes the incoming commands from other CORO routines
CORO_CONTEXT(handle_brick_control);//handles brick board keyboard inputs. (i.e Back button exits the program)
/*--------------drive -----
| CORO drive
| Purpose: Executes the incoming commands. Alloved commands: MOVE_NONE,MOVE_FORWARD,MOVE_BACKWARD,STEP_BACKWARD,TURN_LEFT,TURN_RIGHT,TURN_ANGLE,MOVE_BACKWARDS_RANDOMLY,RELEASE_OBJECT,TURN_TO_TARGET
*------------------------------*/
CORO_DEFINE(drive)
{
//local variables of the coro routine for sensor reads
CORO_LOCAL float value_gyro;
CORO_LOCAL int speed_linear, speed_circular, speed_release, speed_linear2;
CORO_LOCAL int _wait_stopped, value_color;
CORO_LOCAL uint8_t release_engine;
CORO_LOCAL uint8_t proximitysensor, gyrosensor, colorsensor;
CORO_LOCAL float minimumdistance, currentdistance, currentpoint, targetpoint, startgyro;
CORO_BEGIN();
//initialises speed of engines for turning and moving
speed_linear = max_speed * SPEED_LINEAR / 400;
speed_circular = max_speed * SPEED_CIRCULAR / 400;
speed_linear2 = max_speed * SPEED_LINEAR / 1600;
for (; ; ) {
/* Waiting new command */
CORO_WAIT(moving != command);
_wait_stopped = 0;
//Execute command
switch (command) {
/*
| Do nothing
*/
case MOVE_NONE:
_stop();
_wait_stopped = 1;
break;
/*
| Moves engines to go forward forever
*/
case MOVE_FORWARD:
_run_forever(-speed_linear, -speed_linear);
break;
/*
| Moves engines to go backwards forever
*/
case MOVE_BACKWARD:
_run_forever(speed_linear, speed_linear);
break;
/*
| Moves engines to go backwards but turning one side slowly
*/
case MOVE_BACKWARDS_RANDOMLY:
_run_forever(0.5*speed_linear, speed_linear);
break;
/*
| Turn left 90 degrees
*/
case TURN_LEFT:
get_sensor_value0(sn_gyro, &value_gyro);
turnleft();
break;
/*
| Turn right 90 degrees
*/
case TURN_RIGHT:
get_sensor_value0(sn_gyro, &value_gyro);
turnright();
break;
/*
| Turn to angle X
*/
case TURN_ANGLE:
_run_to_rel_pos(speed_circular, DEGREE_TO_COUNT(-angle)
, speed_circular, DEGREE_TO_COUNT(angle));
_wait_stopped = 1;
break;
/*
| Move(step) backwards for 1 second
*/
case STEP_BACKWARD:
_run_timed(speed_linear, speed_linear, 1000);
_wait_stopped = 1;
break;
/*
| Release an obstacle. Lower the ramp, wait, bring it back up
| Author: Ariane
*/
case RELEASE_OBJECT:
//initialise the releaser engine
ev3_search_tacho_plugged_in(65, 0, &release_engine, 0);
//release the ramp
get_tacho_max_speed(release_engine, &speed_release);
set_tacho_stop_action_inx(release_engine, TACHO_COAST);
set_tacho_speed_sp(release_engine, -(0.05)*speed_release);
set_tacho_time_sp(release_engine, 800);
set_tacho_command_inx(release_engine, TACHO_RUN_TIMED);
Sleep(800);
//bring the ramp back up
set_tacho_stop_action_inx(release_engine, TACHO_COAST);
set_tacho_speed_sp(release_engine, (0.05)*speed_release);
set_tacho_time_sp(release_engine, 800);
set_tacho_command_inx(release_engine, TACHO_RUN_TIMED);
break;
/*
| Authors: Berkay, Ariane, Yasmine, Jermaine.
| We worked together on this one. Lots of movements and errors to fix
*/
/*
| 1) Scan the -5/+5 degrees of gyro values and collect all distance values
| 2) Turn towards the value of gyro where distance was minimum
| 3) Get closer to the target till distance is 5cm
| 4) Read the color of the target object
| 5) Assign the values of the map with the obstacle being movable or not
| 6) Go backwards to your old position
| 7) (Optional) Fix back to your starting orientation
*/
case TURN_TO_TARGET:
//initialise the sensors needed (One gyro, one proximity and one color)
ev3_search_sensor(LEGO_EV3_GYRO, &gyrosensor, 0);
ev3_search_sensor(LEGO_EV3_US, &proximitysensor, 0);
ev3_search_sensor(LEGO_EV3_COLOR, &colorsensor, 0);
get_sensor_value0(gyrosensor, ¤tpoint);
startgyro = currentpoint;
Sleep(100);
//Turn 5 degree left
targetpoint = currentpoint - 5.0;
while (currentpoint > targetpoint)
{
get_sensor_value0(gyrosensor, ¤tpoint);
_run_forever(-speed_linear2, speed_linear2);
}
//Turn 10 degree right and find the minimum distance
get_sensor_value0(gyrosensor, ¤tpoint);
targetpoint = currentpoint + 10.0;
minimumdistance = 99999;
currentdistance = 99999;
while (currentpoint < targetpoint)
{
get_sensor_value0(gyrosensor, ¤tpoint);
_run_forever(speed_linear2, -speed_linear2);
get_sensor_value0(proximitysensor, ¤tdistance);
if (currentdistance < minimumdistance)
{
minimumdistance = currentdistance;
}
}
printf("Minimum distance: %f \n", minimumdistance);
//Turn left until the minimum distance is seen again
printf("Turning until minimum distance is achieved\n");
get_sensor_value0(gyrosensor, &targetpoint);
get_sensor_value0(proximitysensor, ¤tdistance);
targetpoint = currentpoint - 10;
//NOTE: there is a 0.2 cm tolerance on the distance. The distance does not measure the same everytime for the same object.
while ((currentdistance > minimumdistance + 2))
{
//printf("CurrentDistance: %f DesiredDistance: %f Diff: %f\n", currentdistance, minimumdistance, currentdistance - minimumdistance);
get_sensor_value0(gyrosensor, ¤tpoint);
get_sensor_value0(proximitysensor, ¤tdistance);
if (currentpoint < (startgyro - 5.0))
{
printf("distance not found. Break\n");
break;
}
_run_forever(-(0.4)*speed_linear2, (0.4)*speed_linear2);
}
//Go forward to get closer.
//NOTE: In case the object in front of you has disapeared (it might have been another robot passing by). The moment is disapears break the loop and skip this part!
while (currentdistance > 50.0 && currentdistance < 120)
{
//printf("Forward\n");
//printf("CurrentDistance: %f DesiredDistance: %f Diff: %f\n", currentdistance, minimumdistance, currentdistance - minimumdistance);
get_sensor_value0(proximitysensor, ¤tdistance);
_run_forever(-(0.4)*speed_linear2, -(0.4)*speed_linear2);
}
//Read the color of the object in front of you
if (!get_sensor_value(0, sn_color, &value_color) || (value_color < 0) || (value_color >= COLOR_COUNT)) {
value_color = 0;
}
//If RED mark the map R
if (value_color == 5)
{
printf("RED Movable Obstacle detected.\n");
if (ROBOT_DIRECTION == 'E')
{
map[ROBOT_X + 1][ROBOT_Y] = 'R';
}
else if (ROBOT_DIRECTION == 'W')
{
if (ROBOT_X > 1) {
map[ROBOT_X - 1][ROBOT_Y] = 'R';
}
else
{
map[0][ROBOT_Y] = 'R';
}
}
else if (ROBOT_DIRECTION == 'N')
{
map[ROBOT_X][ROBOT_Y + 1] = 'R';
}
else if (ROBOT_DIRECTION == 'S')
{
if (ROBOT_Y > 1)
{
map[ROBOT_X][ROBOT_Y - 1] = 'R';
}
else
{
map[ROBOT_X][0] = 'R';
}
}
}
//If NOT Red mark the map B
else
{
printf("NON Movable Obstacle detected.\n");
if (ROBOT_DIRECTION == 'E')
{
map[ROBOT_X + 1][ROBOT_Y] = 'B';
}
else if (ROBOT_DIRECTION == 'W')
{
if (ROBOT_X > 1) {
map[ROBOT_X - 1][ROBOT_Y] = 'B';
}
else
{
map[0][ROBOT_Y] = 'B';
}
}
else if (ROBOT_DIRECTION == 'N')
{
map[ROBOT_X][ROBOT_Y + 1] = 'B';
}
else if (ROBOT_DIRECTION == 'S')
{
if (ROBOT_Y > 1)
{
map[ROBOT_X][ROBOT_Y - 1] = 'B';
}
else
{
map[ROBOT_X][0] = 'B';
}
}
}
//Go back to your previous distance before the engage
while (currentdistance < minimumdistance)
{
//printf("Backwards\n");
//printf("CurrentDistance: %f DesiredDistance: %f Diff: %f\n", currentdistance, minimumdistance, currentdistance - minimumdistance);
get_sensor_value0(proximitysensor, ¤tdistance);
_run_forever((0.6)*speed_linear2, (0.6)*speed_linear2);
}
/*
// Author: Yasmine. Fixing the orientation missorientates the robot when obstaclt is put with a weird angle. Not necessary
// (OPTIONAL) Fix your orientation back to the start up position.
targetpoint = startgyro
if(currentpoint > targetpoint)
{
while (currentpoint > targetpoint)
{
get_sensor_value0(gyrosensor, ¤tpoint);
_run_forever(-speed_linear2, speed_linear2);
}
}
else{
while (currentpoint < targetpoint)
{
get_sensor_value0(gyrosensor, ¤tpoint);
_run_forever(speed_linear2, -speed_linear2);
}
}
*/
//Stop moving
_run_forever(0, 0);
break;
}
moving = command;
if (_wait_stopped) {
/* Waiting the command is completed */
CORO_WAIT(!_is_running());
command = moving = MOVE_NONE;
}
}
CORO_END();
}
/*--------------handle_brick_control -----
| CORO handle_brick_control
| Purpose: Executes the commands coming to the keyboard of the EV3. Back button to kill the app without using the SSH
| Author: Jermaine
*------------------------------*/
CORO_DEFINE(handle_brick_control)
{
CORO_LOCAL uint8_t keys, pressed = EV3_KEY__NONE_;
CORO_BEGIN();
for (; ; ) {
/* Waiting any key is pressed or released */
CORO_WAIT(ev3_read_keys(&keys) && (keys != pressed));
pressed = keys;
if (pressed & EV3_KEY_BACK) {
printf("Brick Command: BACK BUTTON\n");
/* Stop the vehicle */
command = MOVE_NONE;
/* Quit the program */
app_alive = 0;
}
CORO_YIELD();
}
CORO_END();
}
/*
| Authors: Berkay, Ariane, Yasmine, Jermaine.
| Again everyone worked on this CORO because it is the base of our robot. Details given below in the code
*/
/*--------------handle_ir_proximity -----
| CORO handle_ir_proximity
| Purpose: Main decision making of the navigation of the robot across the map
| 1) Check the touch sensor
| a) If touched go backwards a bit to fix it
| 2) Check the proximity sensor
| a) If nothing on the way check if we are on the edge of the platform (X and Y control for the open fence)
| i) If on the edge turn and do not go forward
| ii) if not just move forward as usual
| b) If there is something on the way turn towards the target and identify its type (movable or not)
| i) Check if we are at the edge of the map (topright, topleft, bottomright or bottomleft)
| . if yes make logical decision to turn
| ii) If not turn randomly to right or left
|
| 3) With 10% probability decide to release an object. (Only if there is nothing infront of you to not get stuck)
*------------------------------*/
CORO_DEFINE(handle_ir_proximity)
{
CORO_LOCAL float prox;
CORO_LOCAL int touch_state, randoma, value_color;
CORO_LOCAL const char const *colorarray[] = { "?", "BLACK", "BLUE", "GREEN", "YELLOW", "RED", "WHITE", "BROWN" };
CORO_BEGIN();
ev3_search_sensor(LEGO_EV3_TOUCH, &sn_touch, 0);
for (; ; ) {
get_sensor_value(0, sn_touch, &touch_state);
//Touch Author: Jermaine
//If something is touched go backwards
if (touch_state != 0)
{
printf("Touched something I am going one step backwards.\n");
command = MOVE_BACKWARDS_RANDOMLY;
}
//Nothing is touching me
else
{
if (!get_sensor_value(0, sn_color, &value_color) || (value_color < 0) || (value_color >= COLOR_COUNT)) {
value_color = 0;
}
get_sensor_value0(sn_sonar, &prox);
printf("\r Distance: %f Color: (%s) \n", prox, colorarray[value_color]);
if (prox == 0) {
command = MOVE_NONE;
}
//There is an object in front of me
else if (prox < 120)
{
//Turn towards it read its color and all the work
command = TURN_TO_TARGET;
CORO_CALL(drive);
//Position Frames Author: Yasmine
//Depending on where you currently are either make a logical decision to turn or go full random
if ((ROBOT_X < 6) && (ROBOT_Y < 6))
{
if (ROBOT_DIRECTION == 'E')
{
command = TURN_LEFT;
}
else if (ROBOT_DIRECTION == 'W')
{
command = TURN_RIGHT;
}
else if (ROBOT_DIRECTION == 'N')
{
command = TURN_RIGHT;
}
else if (ROBOT_DIRECTION == 'S')
{
command = TURN_LEFT;
}
}
else if ((ROBOT_X < 6) && (ROBOT_Y > (MAP_HEIGHT-6)))
{
if (ROBOT_DIRECTION == 'E')
{
command = TURN_RIGHT;
}
else if (ROBOT_DIRECTION == 'W')
{
command = TURN_LEFT;
}
else if (ROBOT_DIRECTION == 'N')
{
command = TURN_RIGHT;
}
else if (ROBOT_DIRECTION == 'S')
{
command = TURN_LEFT;
}
}
else if ((ROBOT_X > (MAP_WIDTH - 6)) && (MAP_HEIGHT - 6))
{
if (ROBOT_DIRECTION == 'E')
{
command = TURN_LEFT;
}
else if (ROBOT_DIRECTION == 'W')
{
command = TURN_RIGHT;
}
else if (ROBOT_DIRECTION == 'N')
{
command = TURN_LEFT;
}
else if (ROBOT_DIRECTION == 'S')
{
command = TURN_RIGHT;
}
}
else if ((ROBOT_X > (MAP_WIDTH - 6)) && (ROBOT_Y > (MAP_HEIGHT - 6)))
{
if (ROBOT_DIRECTION == 'E')
{
command = TURN_RIGHT;
}
else if (ROBOT_DIRECTION == 'W')
{
command = TURN_LEFT;
}
else if (ROBOT_DIRECTION == 'N')
{
command = TURN_LEFT;
}
else if (ROBOT_DIRECTION == 'S')
{
command = TURN_RIGHT;
}
}
//Random Turns Author: Berkay
//Somewhere else in the map. I can make a random turn! WOHOO
else
{
randoma = rand() % 100;
if (randoma < 50)
{
command = TURN_RIGHT;
}
else
{
command = TURN_LEFT;
}
}
}
//Road seems clear!
else
{
//Current location is green
map[ROBOT_X][ROBOT_Y] = 'G';
//No wall limitation author: Berkay
//Stadium 2 Update: If on the edge of the stadium, even if there is nothing on the way DO NOT GO NEGATIVE Y
if (ROBOT_Y < 2 && ROBOT_DIRECTION == 'S')
{
randoma = rand() % 100;
if (randoma < 50)
{
command = TURN_RIGHT;
}
else
{
command = TURN_LEFT;
}
}
//If not on the bototm edge of the map feel free to move forward
else
{
command = MOVE_FORWARD;
}
}
//Release obstacle author: Ariane
//Can I release an obstacle please?
if (releaseobject)
{
randoma = rand() % 100;
//Release chance: 10%
if (randoma < 10)
{
command = RELEASE_OBJECT;
CORO_CALL(drive);
Sleep(200);
command = MOVE_NONE;
CORO_CALL(drive);
releaseobject = false;
}
else
{
//Okay maybe next time
}
}
}
CORO_YIELD();
}
CORO_END();
}
/*--------------setCoordinates -----
| Function setCoordinates
| Purpose: Depending on the direction of the movement sents the new X and Y of the robot.
| Update V2:Coordinates cannot overpass the height and width of the map. Or go negative. (Automatic recalibration to 0:0)
| Main Author: Jermaine.
| Map Height Width Limits Author: Berkay
*------------------------------*/
void setCoordinates()
{
if (command == MOVE_FORWARD)
{
if (ROBOT_DIRECTION == 'N')
{
ROBOT_Y++;
if (ROBOT_Y > MAP_HEIGHT) { ROBOT_Y = MAP_HEIGHT; }
}
else if (ROBOT_DIRECTION == 'S')
{
ROBOT_Y--;
if (ROBOT_Y < 1) { ROBOT_Y = 0; }
}
else if (ROBOT_DIRECTION == 'E')
{
ROBOT_X++;
if (ROBOT_X > MAP_WIDTH) { ROBOT_X = MAP_WIDTH; }
}
else if (ROBOT_DIRECTION == 'W')
{
ROBOT_X--;
if (ROBOT_X < 1) { ROBOT_X = 0; }
}
}
else if (command == MOVE_BACKWARD || command == MOVE_BACKWARDS_RANDOMLY)
{
if (ROBOT_DIRECTION == 'N')
{
ROBOT_Y--;
if (ROBOT_Y < 1) { ROBOT_Y = 0; }
}
else if (ROBOT_DIRECTION == 'S')
{
ROBOT_Y++;
if (ROBOT_Y > MAP_HEIGHT) { ROBOT_Y = MAP_HEIGHT; }
}
else if (ROBOT_DIRECTION == 'E')
{
ROBOT_X--;
if (ROBOT_X < 1) { ROBOT_X = 0; }
}
else if (ROBOT_DIRECTION == 'W')
{
ROBOT_X++;
if (ROBOT_X > MAP_WIDTH) { ROBOT_X = MAP_WIDTH; }
}
}
}
/*--------------changeDirection -----
| Function changeDirection
| Purpose: Depending on the command direction changes the robots direction
| Note: Only left and right turns changes direction. The forward and backwards does not!
| Author: Jermaine
*------------------------------*/
void changeDirection()
{
if (command == TURN_LEFT) {
if (ROBOT_DIRECTION == 'N')
{
ROBOT_DIRECTION = 'W';
}
else if (ROBOT_DIRECTION == 'S')
{
ROBOT_DIRECTION = 'E';
}
else if (ROBOT_DIRECTION == 'E')
{
ROBOT_DIRECTION = 'N';
}
else if (ROBOT_DIRECTION == 'W')
{
ROBOT_DIRECTION = 'S';
}