Final Competition Source Code

February 2, 2012Posted by Maddy

 
This is the final source code after the rebuild (umain.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
// Include headers from OS
#include <joyos.h>

//Math Stuff
#include <stdio.h>
#include <math.h>
#include <happylib.h>

#define PI 3.14159265358979323846
#define RADTODEG(RAD) (int)((RAD)*(180.0/PI))
#define THETATODEG(THETA) (int)((THETA*180.0)/2047.0)

#define SHARP_M 14150 
#define SHARP_C 10


#define gyroPort 23
#define LSB_US_PER_DEG 1048830

//VPS
#define VPS_PER_FOOT 682.6666
extern volatile uint8_t robot_id;


#define LRDistancePort 22


#define gateServoPort 0

#define leftMotorPort 0
#define rightMotorPort 1
#define leverMotorPort 2
#define captureMotorPort 3

int frobPos = 0;
int bodyPos = 0;
int captureDir = -1;
int8_t gyroInitPosition = 0;
uint8_t minedResources = 0;

int8_t invert = 0;

int16_t points[][2] = {{2047,0},{1791,443},{1280,1330},{1024,1773},{512,1773},{-512,1773},{-1024,1773},{-1280,1330},{-1791,443},{-2047,0},{-1791,-443},{-1280,-1330},{-1024,-1773},{-512,-1773},{512,-1773},{1024,-1773},{1280,-1330},{1791,-443},{443,256},{0,512},{-443,256},{-443,-256},{0,-512},{443,-256}};
char alphabet[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

// usetup is called during the calibration period. 
int usetup (void) {
	invert = frob_read_range(-1,1);
	printf("Invert: %d", invert);
	robot_id = 6;
	printf("Stabilizing \n");
	pause(1000);
	gyro_init(gyroPort, LSB_US_PER_DEG, 500);
	irdist_set_calibration (SHARP_M, SHARP_C);
	bodyPos = gyro_get_degrees() - gyroInitPosition;
	copy_objects();
	if(game.coords[0].x < 0){
		captureDir = 1;
	}
	servo_set_pos(gateServoPort,375);
    return 0;
}


//General Functions
int MIN(int x, int y){
	if(x >= y){
		return x;
	}
	else{
		return y;
	}
}
int MAX(int x, int y){
	if(x >= y){
		return y;
	}
	else{
		return x;
	}
}
uint16_t limit_vel(int vel){
	int MAX_VEL = 255;
	if (vel > MAX_VEL){
		return MAX_VEL;
	}
	else if (vel < -MAX_VEL){
		return -MAX_VEL;
	}
	else{
		return (int16_t)vel;
	}
}
int16_t limit_theta(int16_t theta){
	while ((theta > 360) || (theta < -360)){
		if(theta > 360){
			theta -= 360;
		}
		else if(theta < -360){
			theta += 360;
		}
	}
	return theta;
}
int8_t limit_sector(int8_t num){
	while((num > 5) || (num < 0)){
		if(num > 5){
			num -= 6;
		}
		else if(num < 0){
			num += 6;
		}
	}
	return num;
}
int16_t fixAngle(int16_t theta){
	if(theta < 0){
		return 360 + theta;
	}
	else{
		return theta;
	}
}

int16_t getPoint(char letter, uint8_t pos){
	int len=sizeof(alphabet)/sizeof(int);
	for(uint8_t i = 0; i<len; i++){
		if (letter == alphabet[i]){
			return points[i][pos];
		}
	}
	return 0;
}


//Sensors

int16_t gyroDegrees(){
	return gyro_get_degrees() - gyroInitPosition;
}
void setBodyPos(){
	bodyPos = gyroDegrees();
	while (bodyPos > 360){
		bodyPos -= 360;
	}
	while (bodyPos < -360){
		bodyPos += 360;
	}
}
void resetGyroWithTheta(){
	copy_objects();
	bodyPos = (int)THETATODEG(game.coords[0].theta);
	gyroInitPosition = gyro_get_degrees() - bodyPos;
	
	
}
float sensors_LRDistance(){
	return irdist_read(LRDistancePort)/2.54; //Converts to inches
}

//Motors
uint8_t motorPortList[] = {0,1};
int leftMotorVel = 0;
int rightMotorVel = 0;

uint8_t isRobotStopped(){
	return ((leftMotorVel == 0) && (rightMotorVel == 0));
}

void setDriveMotorVels(){
	motor_set_vel(leftMotorPort,leftMotorVel);
	motor_set_vel(rightMotorPort,rightMotorVel);
}
void motors_stopDriveMotors(){
	rightMotorVel = 0;
	leftMotorVel = 0;
	setDriveMotorVels();
}
void motors_brakeDriveMotors(){
	leftMotorVel = 0;
	rightMotorVel = 0;
	int len = sizeof(motorPortList)/sizeof(int);
 	for (int i = 0; i<len; i++){
		motor_brake(motorPortList[i]);
	}
}
void motors_turnLeft(uint8_t speed){
	leftMotorVel = -limit_vel(speed);
	rightMotorVel = limit_vel(speed);
	setDriveMotorVels(leftMotorVel, rightMotorVel);
}
void motors_turnRight(uint8_t speed){
	leftMotorVel = limit_vel(speed);
	rightMotorVel = -limit_vel(speed);
	setDriveMotorVels(leftMotorVel, rightMotorVel);
}

void motors_adjustLeft(uint8_t speed, uint8_t correction){
	leftMotorVel = limit_vel(speed - correction);
	rightMotorVel = limit_vel(speed + correction);
	setDriveMotorVels(leftMotorVel, rightMotorVel);
	pause(250);
}
void motors_adjustRight(uint8_t speed, uint8_t correction){
	leftMotorVel = limit_vel(speed + correction);
	rightMotorVel = limit_vel(speed - correction);
	setDriveMotorVels(leftMotorVel, rightMotorVel);
}

//This sets a given heading but doesn't drive along it
void motors_setHeading(uint8_t speed, int16_t heading){
	heading = fixAngle(heading);
	uint8_t stopped = isRobotStopped();
	uint32_t timer = get_time();
	while ((abs(fixAngle(bodyPos)-heading)>2) && (get_time()-timer < 5000)){
		if (heading < bodyPos){
			if(stopped){
				motors_turnRight(speed);
			}
			else{
				motors_adjustRight(speed,abs(bodyPos-heading)*2);
			}
		}
		else{
			if(stopped){
				motors_turnLeft(speed);
			}
			else{
				motors_adjustLeft(speed,abs(bodyPos-heading)*2);
			}
		}
		setBodyPos();
	}
	motors_stopDriveMotors();
}

//This will move along a certain heading
//If the robot is already moving this will be a slower adjustment
//To go straight - put heading in as bodyPos
void motors_followHeading(int16_t speed, int16_t heading){
	setBodyPos();
	float error = MIN((fixAngle(heading)-fixAngle(bodyPos)), (heading-bodyPos));
	float gain = 2; //**adjust this**
	int16_t correction = (int16_t)(error * gain);
	if (speed<0){
		correction = -correction;
	}
	leftMotorVel = limit_vel(speed -= correction);
	rightMotorVel = limit_vel(speed += correction);
	//printf("Heading: %d bodyPos: %d Error: %f Speed: %d LMV: %d RMV %d \n",heading, bodyPos, error, speed, leftMotorVel, rightMotorVel);
	setDriveMotorVels(leftMotorVel, rightMotorVel);

}
// void motors_thetaSetHeading3(int16_t speed, int16_t heading){
// 	copy_objects();
// 	int16_t theta = THETATODEG(game.coords[0].theta);
// 	while(abs(theta-heading)>2){
// 		motors_turnLeft(speed);
// 		copy_objects();
// 		theta = THETATODEG(game.coords[0].theta);
// 	}
// 	motors_stopDriveMotors();
// }
void motors_thetaSetHeading3(int16_t speed, int16_t heading){
	copy_objects();
	int16_t theta = THETATODEG(game.coords[0].theta);
	if(heading<-170 || heading > 170){
		heading = fixAngle(heading);
		theta = fixAngle(theta);
	}
	//uint32_t timer = get_time();
	while(abs(theta-heading)>2){
		if(fixAngle(heading)<fixAngle(theta)){
			motors_turnRight(speed);
		}
		else{
			motors_turnLeft(speed);
		}
		copy_objects();
		theta = THETATODEG(game.coords[0].theta);
		if(heading<-170 || heading > 170){
			theta = fixAngle(theta);
		}
	}
	motors_stopDriveMotors();
}
void motors_thetaSetHeading(int16_t speed, int16_t heading){
	copy_objects();
	heading = fixAngle(heading);
	uint8_t stopped = isRobotStopped();
	int16_t theta = THETATODEG(game.coords[0].theta);
	int timer = get_time();
	while(abs(fixAngle(theta)-heading)>2 && (get_time()-timer < 5000)){
		if (heading < theta){
			if(stopped){
				motors_turnRight(speed);
			}
			else{
				motors_adjustRight(speed,abs(theta-heading)*2);
			}
		}
		else{
			if(stopped){
				motors_turnLeft(speed);
			}
			else{
				motors_adjustLeft(speed,abs(theta-heading)*2);
			}
		}
		copy_objects();
		theta = THETATODEG(game.coords[0].theta);
	}
	motors_stopDriveMotors();
}
void motors_thetaFollowHeading(int16_t speed, int16_t heading){
	copy_objects();
	int16_t theta = THETATODEG(game.coords[0].theta);
	float error = MIN((fixAngle(heading)-fixAngle(theta)), (heading-theta));
	float gain = 2; //**adjust this**
	int16_t correction = (int16_t)(error * gain);
	if (speed<0){
		correction = -correction;
	}
	leftMotorVel = limit_vel(speed -= correction);
	rightMotorVel = limit_vel(speed += correction);
	setDriveMotorVels(leftMotorVel, rightMotorVel);
		
}
//setBodyPos() must be called before calling this
void motors_gyroDriveStraight(int16_t speed, uint16_t milliseconds){
	uint32_t timer = get_time();
	setBodyPos();
	int16_t curBodyPos = bodyPos;
 	while(get_time() - timer < milliseconds){
		motors_followHeading(speed,curBodyPos);
	}
}
void motors_thetaDriveStraight(int16_t speed, uint16_t milliseconds){
	uint32_t timer = get_time();
	copy_objects();
	int16_t curBodyPos = THETATODEG(game.coords[0].theta);
 	while(get_time() - timer < milliseconds){
		motors_thetaFollowHeading(speed,curBodyPos);
	}
}
uint8_t motor_isStalled(uint8_t motor){
 	return (motor_get_current(motor) > 20);
 }
void motors_stopOnStall(){
 	for (int8_t i = 0; i<2; i++){
 		if (motor_isStalled(i)){
 			motor_brake(i);
 		}
 	}
 	leftMotorVel = -100;
 	rightMotorVel = -100;
 	setDriveMotorVels();
 	pause(1000);
 	motors_stopDriveMotors();
}


//Collision avoidance

void simpleAvoidCollisions(int16_t speed){
	if(sensors_LRDistance()<12.0){
		motors_adjustLeft(speed,(12-sensors_LRDistance())*25);
	}
}
void avoidOtherRobot(int16_t speed){
	copy_objects();
	int16_t dy = game.coords[1].x - game.coords[0].x;
	int16_t dx = game.coords[1].y - game.coords[0].y;
	int16_t ourTheta = fixAngle(THETATODEG(game.coords[0].theta));
	int16_t theirTheta = fixAngle(THETATODEG(game.coords[1].theta));
	int16_t collisionTheta = fixAngle(RADTODEG(atan2(dy,dx)));
	if((abs(dy) < 1000 || abs(dx) < 1000)){
		if(abs(limit_theta(theirTheta + 180)-collisionTheta)<5){
			int16_t diff = limit_theta(theirTheta + 180)-collisionTheta;
			if(diff < 0){
				motors_adjustRight(speed, MAX(50-diff,0));
			}
			else{
				motors_adjustLeft(speed, MAX(50-diff,0));
			}
		}
		else if(abs(ourTheta-collisionTheta)<5){
			int16_t diff = ourTheta-collisionTheta;
			if(limit_theta(theirTheta + 180) > ourTheta){
				motors_adjustLeft(speed, MAX(50-diff,0));
			}
			else{
				motors_adjustRight(speed, MAX(50-diff,0));
			}
		}
	}
}
void avoidWalls(int16_t speed){
	//**not complete
	copy_objects();
	int16_t curPos[] = {game.coords[0].x,game.coords[0].y,THETATODEG(game.coords[0].theta)};
	int8_t dist = 100;
	
	if((curPos[0]<getPoint('S',0) + dist) && (curPos[0]>getPoint('U',0) - dist) && (curPos[1]<getPoint('T',1)+dist) && (curPos[1]>getPoint('W',1) - dist)){
		if(curPos[0]>getPoint('S',0)){
			//check the angle
		}
		else if(curPos[0]<getPoint('U',0)){
		
		}
		else if(curPos[1]>getPoint('T',1)){
		
		}
		else if(curPos[1]<getPoint('W',1)){
			
		}
	}
	if((abs(curPos[0] - getPoint('U',0))<dist) && (curPos[1]<getPoint('T',1)) && (curPos[1]>getPoint('W',1))){
		if((curPos[2]<180) && (curPos[2]>0)){
			motors_adjustLeft(speed,abs(curPos[2]-90)*2);
		}
		else{
			motors_adjustRight(speed,abs(curPos[2]-270)*2);
		}
	}
}

//Navigation



void navigateToPoint(int16_t speed, int16_t goalX, int16_t goalY, int16_t err){ 
	
	//Get current position
	copy_objects();
	int16_t curX = game.coords[0].x;
	int16_t curY = game.coords[0].y;
	int16_t dy = goalY-curY;
	int16_t dx = goalX-curX;
	int16_t curTheta = THETATODEG(game.coords[0].theta);
	int16_t goalTheta = (int16_t)RADTODEG(atan2(dy,dx));	
	uint32_t timer = get_time();
	//While the difference between goal and current position is greater than allowed err
	while (((abs(dx)>err) ||(abs(dy)>err)) && (get_time()-timer<3000)){
//		avoidOtherRobot(speed);
		copy_objects();
		curX = game.coords[0].x;
		curY = game.coords[0].y;
		dy = goalY-curY;
		dx = goalX-curX;
		curTheta = THETATODEG(game.coords[0].theta);
		goalTheta = (int16_t)RADTODEG(atan2(dy,dx));
		//if goal and cur theta are in 2nd/3rd quadrant
		if((curTheta < -90 || curTheta > 90) && (goalTheta < -90 || goalTheta > 90)){
			curTheta = fixAngle(curTheta);
			goalTheta = fixAngle(goalTheta);
		}
		float error = goalTheta - curTheta;
		float gain = 1.25; //**adjust this**
		int16_t correction = (int16_t)(error * gain);
		leftMotorVel = limit_vel(speed - correction);
		rightMotorVel = limit_vel(speed + correction);
		setDriveMotorVels(leftMotorVel, rightMotorVel);
		pause(100);
	}
	if(get_time() - timer >= 3000){
		leftMotorVel = -100;
 		rightMotorVel = -100;
		setDriveMotorVels();
		pause(1000);
		motors_stopDriveMotors();
	}
}
int8_t getSector(){
	copy_objects();
	int16_t curX = game.coords[0].x;
	int16_t curY = game.coords[0].y;
	int8_t curSector = 0;
	if(curX<0 && curY < getPoint('K',1)){
		curSector = 4;
	}
	else if(curX<0 && curY > getPoint('B',1)){
		curSector = 2;
	}
	else if(curX<0){
		curSector = 3;
	}
	else if(curX >= 0 && curY < getPoint('K',1)){
		curSector = 5;
	}
	else if(curX >= 0 && curY > getPoint('B',1)){
		curSector = 1;
	}
	//else curSector remains 0
	return curSector;
}
int8_t getOpponentSector(){
	copy_objects();
	int16_t curX = game.coords[1].x;
	int16_t curY = game.coords[1].y;
	int8_t curSector = 0;
	if(curX<0 && curY < getPoint('K',1)){
		curSector = 4;
	}
	else if(curX<0 && curY > getPoint('B',1)){
		curSector = 2;
	}
	else if(curX<0){
		curSector = 3;
	}
	else if(curX >= 0 && curY < getPoint('K',1)){
		curSector = 5;
	}
	else if(curX >= 0 && curY > getPoint('B',1)){
		curSector = 1;
	}
	//else curSector remains 0
	return curSector;
}
void navigateToSector(int16_t speed, int8_t sector){
	int8_t curSector = getSector();
	int8_t nextSector = -1;
	if(curSector == sector){
		return;
	}
	else if (abs(limit_sector(curSector + 1) - sector) > abs(limit_sector(curSector - 1) - sector)){
		nextSector = limit_sector(curSector - 1);
	}
	else{
		nextSector = limit_sector(curSector + 1);
	}
	
	if(nextSector == 0){
		//0
		navigateToPoint(speed,getPoint('C',0), getPoint('A',1),300);
		navigateToSector(speed,sector);
	}
	if(nextSector == 1){
		//1
		navigateToPoint(speed,getPoint('E',0), getPoint('C',1)-200,300);
		navigateToSector(speed,sector);
	}
	if(nextSector == 2){
		//2
		navigateToPoint(speed,getPoint('F',0), getPoint('C',1)-200,300);
		navigateToSector(speed,sector);
	}
	if(nextSector == 3){
		//3
		navigateToPoint(speed,getPoint('H',0),getPoint('J',1),300);
		navigateToSector(speed,sector);
	}
	if(nextSector == 4){
		//4
		navigateToPoint(speed,getPoint('F',0), getPoint('L',1),300);
		navigateToSector(speed,sector);
	}
	if(nextSector == 5){
		//5
		navigateToPoint(speed,getPoint('E',0), getPoint('L',1),300);
		navigateToSector(speed,sector);
	}

	
}

//Interaction

void captureTerritory(int8_t num){
	pause(1000);
	leftMotorVel = -175;
	rightMotorVel = -175;
	setDriveMotorVels();
	uint32_t timer = get_time();
	while(get_time() - timer < 3500 && game.territories[num].owner != 6){
		copy_objects();
		int speed = -150;
		if(invert != 0){
			speed = -speed;
		}
		motor_set_vel(captureMotorPort, speed*captureDir);
	}
	motors_stopDriveMotors();
	motor_set_vel(captureMotorPort, 0);
}

void mineResources(){
	leftMotorVel = 175;
	rightMotorVel = 175;
	setDriveMotorVels();
	motor_set_vel(leverMotorPort,75);
	pause(4000);
	motors_stopDriveMotors();
	minedResources = 4;
	motor_set_vel(leverMotorPort,0);
}
void releaseResources(){
	//**Not right! Can't navigate around walls....
	if(captureDir == 1){
		navigateToSector(175,0);
		motors_stopDriveMotors();
		motors_thetaSetHeading(100,00);
	}
	else{
		navigateToSector(175,3);
		motors_stopDriveMotors();
		motors_thetaSetHeading(100,180);		
	}
	leftMotorVel = -175;
	rightMotorVel = -175;
	setDriveMotorVels();
	servo_set_pos(gateServoPort,275);
	pause(750*minedResources); 
	motors_stopDriveMotors();
	servo_set_pos(gateServoPort,375); 
	minedResources = 0;
}





//Exploration period

void explore(int16_t speed){
	uint32_t timer = get_time();
	copy_objects();
	if(game.coords[0].x < 0){
		navigateToPoint(speed,getPoint('H',0),getPoint('J',1),300);
		if(get_time() - timer < 10000){
			navigateToPoint(speed,getPoint('F',0), getPoint('L',1),300);
		}
		if(get_time() - timer < 10000){
			navigateToPoint(speed,getPoint('E',0), getPoint('L',1),300);
		}
		if(get_time() - timer < 10000){
			navigateToPoint(speed,getPoint('C',0), getPoint('A',1),300);
		}
		if(get_time() - timer < 10000){
			navigateToPoint(speed,getPoint('E',0), getPoint('C',1)-200,300);
		}
		if(get_time() - timer < 10000){
			navigateToPoint(speed,getPoint('F',0), getPoint('C',1)-200,300);
		}
		if(get_time() - timer < 10000){
			navigateToPoint(speed,getPoint('H',0), getPoint('J',1),300);
 		}
 	}
 	else{
 		navigateToPoint(speed,getPoint('C',0), getPoint('A',1),300);
 		if(get_time() - timer < 10000){
			navigateToPoint(speed,getPoint('E',0), getPoint('C',1)-200,300);
		}
		if(get_time() - timer < 10000){
			navigateToPoint(speed,getPoint('F',0), getPoint('C',1)-200,300);
		}
		if(get_time() - timer < 10000){
			navigateToPoint(speed,getPoint('H',0), getPoint('J',1),300);
		}
		if(get_time() - timer < 10000){
			navigateToPoint(speed,getPoint('F',0), getPoint('L',1),300);
		}
		if(get_time() - timer < 10000){
			navigateToPoint(speed,getPoint('E',0), getPoint('L',1),300);
		}
		if(get_time() - timer < 10000){
			navigateToPoint(speed,getPoint('C',0), getPoint('A',1),300);
		}
		
 	}
}

void capture(int8_t point){
	copy_objects();
	if(game.territories[point].owner == 6){
		return;
	}
	navigateToSector(150,point);
	motors_stopDriveMotors();
	if(getSector() != point){
		return;
	}
	
	for(int8_t i = 5; i >= 0; i-=5){
		int8_t corr = i;
		if(point == 4){
				navigateToPoint(100, getPoint('F',0),getPoint('L',1)+200,150);
				motors_stopDriveMotors();
				copy_objects();
				int dy = 1773 - game.coords[0].y;
				//int dy = -1773 - game.coords[0].y;
				int dx = -512  - game.coords[0].x;
				int16_t goalTheta = RADTODEG(atan2(dy,dx));
				//motors_setHeading(100,90 + i);
				motors_thetaSetHeading3(50,goalTheta+corr);
				if(getSector() == point){
					captureTerritory(point);
					copy_objects();
					if(game.territories[point].owner == 6){
						break;
					}
				}
		}
		if(point == 3){
			navigateToPoint(100, getPoint('H',0),getPoint('A',1),150);
			motors_stopDriveMotors();
			copy_objects();
			int dy = 1330  - game.coords[0].y;
			int dx = 1280 - game.coords[0].x;
			// int dy = -443  - game.coords[0].y;
// 			int dx = -1791 - game.coords[0].x;
			int16_t goalTheta = RADTODEG(atan2(dy,dx));
			//motors_setHeading(100,30 + i);
			motors_thetaSetHeading3(50,goalTheta+corr);
			if(getSector() == point){
				captureTerritory(point);	
				copy_objects();
				if(game.territories[point].owner == 6){
					break;
				}
			}
		}
		if(point == 2){
			navigateToPoint(100, getPoint('F',0),getPoint('C',1)-200,150);
			motors_stopDriveMotors();
			copy_objects();
			int dy = 512  - game.coords[0].y;
			int dx = 0  - game.coords[0].x;
			// int dy = 1330  - game.coords[0].y;
// 			int dx = -1280  - game.coords[0].x;
			int16_t goalTheta = RADTODEG(atan2(dy,dx));
			//motors_setHeading(100,-30 + i);
			motors_thetaSetHeading3(50,goalTheta+corr);
			if(getSector() == point){
				captureTerritory(point);
				copy_objects();
				if(game.territories[point].owner == 6){
						break;
				}
			}
		}
		if(point == 1){
			navigateToPoint(100, getPoint('E',0),getPoint('C',1)-200,150);
			motors_stopDriveMotors();
			copy_objects();
			int dy = -1773  - game.coords[0].y;
			int dx = 512  - game.coords[0].x;
			int16_t goalTheta = RADTODEG(atan2(dy,dx));
			//motors_setHeading(100,-90 + i);
			motors_thetaSetHeading3(50,goalTheta+corr);
			if(getSector() == point){
				captureTerritory(point);
				copy_objects();
				if(game.territories[point].owner == 6){
						break;
				}
			}
		}
		if(point == 0){
			navigateToPoint(100, getPoint('C',0),getPoint('A',1),150);
			motors_stopDriveMotors();
			copy_objects();
			int dy = -256 - game.coords[0].y;
			int dx = 443  - game.coords[0].x;
// 			int dy = 443  - game.coords[0].y;
// 			int dx = 1791 - game.coords[0].x;
			int16_t goalTheta = RADTODEG(atan2(dy,dx));
			//motors_setHeading(100,-150 + i );
			motors_thetaSetHeading3(50,goalTheta+corr);
			if(getSector() == point){
				captureTerritory(point);
				copy_objects();
				if(game.territories[point].owner == 6){
						break;
				}
			}
		}
		if(point == 5){
			navigateToPoint(100, getPoint('E',0),getPoint('L',1)+200,150);
			motors_stopDriveMotors();
			copy_objects();
			int dy = -512  - game.coords[0].y;
			int dx = 0  - game.coords[0].x;
			// int dy = -1330 - game.coords[0].y;
// 			int dx = 1280 - game.coords[0].x;
			int16_t goalTheta = RADTODEG(atan2(dy,dx));
			//motors_setHeading(100,150 + i);
			motors_thetaSetHeading3(50,goalTheta+corr);
			if(getSector() == point){
				captureTerritory(point);
				copy_objects();
				if(game.territories[point].owner == 6){
						break;
				}
			}
		}
	}
}

void mine(int8_t point){
	copy_objects();
	for(int8_t i = -5; i <= 0; i+=5){	
		int8_t corr = -5;
		if(point == 4){
			navigateToPoint(100, getPoint('F',0),getPoint('L',1)+200,150);
			motors_stopDriveMotors();
			copy_objects();
			int dy = getPoint('L',1)  - game.coords[0].y;
			int dx = getPoint('L',0)  - game.coords[0].x;
			int16_t goalTheta = RADTODEG(atan2(dy,dx));
			motors_thetaSetHeading(50,goalTheta+corr);
			//motors_thetaSetHeading(100,-150+i);
			mineResources();
		}
		if(point == 3){
			navigateToPoint(100, getPoint('H',0),getPoint('A',1),150);
			motors_stopDriveMotors();
			copy_objects();
			int dy = getPoint('I',1)  - game.coords[0].y;
			int dx = getPoint('I',0)  - game.coords[0].x;
			int16_t goalTheta = RADTODEG(atan2(dy,dx));
			motors_thetaSetHeading(50,goalTheta+corr);
			//motors_thetaSetHeading(100,150+i);
			mineResources();
		}
		if(point == 2){
			navigateToPoint(100, getPoint('F',0),getPoint('C',1)-200,150);
			motors_stopDriveMotors();
			copy_objects();
			int dy = getPoint('F',1)  - game.coords[0].y;
			int dx = getPoint('F',0)  - game.coords[0].x;
			int16_t goalTheta = RADTODEG(atan2(dy,dx));
			motors_thetaSetHeading(50,goalTheta+corr);
			//motors_thetaSetHeading(100,90+i);
			mineResources();
		}
		if(point == 1){
			navigateToPoint(100, getPoint('E',0),getPoint('C',1)-200,150);
			motors_stopDriveMotors();
			copy_objects();
			int dy = getPoint('C',1)  - game.coords[0].y;
			int dx = getPoint('C',0)  - game.coords[0].x;
			int16_t goalTheta = RADTODEG(atan2(dy,dx));
			motors_thetaSetHeading(50,goalTheta+corr);
			//motors_thetaSetHeading(100,30+i);
			mineResources();
		}
		if(point == 0){
			navigateToPoint(100, getPoint('C',0),getPoint('A',1),150);
			motors_stopDriveMotors();
			copy_objects();
			int dy = getPoint('R',1)  - game.coords[0].y;
			int dx = getPoint('R',0)  - game.coords[0].x;
			int16_t goalTheta = RADTODEG(atan2(dy,dx));
			motors_thetaSetHeading(50,goalTheta+corr);
			//motors_thetaSetHeading(100,-30+i);
			mineResources();
		}
		if(point == 5){
			navigateToPoint(100, getPoint('E',0),getPoint('L',1)+200,150);
			motors_stopDriveMotors();
			copy_objects();
			int dy = getPoint('O',1)  - game.coords[0].y;
			int dx = getPoint('O',0)  - game.coords[0].x;
			int16_t goalTheta = RADTODEG(atan2(dy,dx));
			motors_thetaSetHeading(50,goalTheta+corr);
			//motors_thetaSetHeading(100,-90+i);
			mineResources();
		}
	}
}

//Main routine
int umain (void) {

	explore(175); 	

 	
	while(1){
		copy_objects();
		for(int8_t i = 0; i<6; i++){
			int8_t j = i;
			if(captureDir == 1){
				j = limit_sector(i+3);
			}
			copy_objects();
			if(game.territories[j].owner != 6 && getOpponentSector() != j){
				capture(j);
				copy_objects(); 
				if(game.territories[j].owner == 6){
					mine(j);
				}
			}
		}
		copy_objects();
		releaseResources();
	}
	return 0;
}

Pre-Rebuild Source Code

February 2, 2012Posted by Maddy

 
This was the source code before the rebuild - it includes some sensor functions and code for the rotating head (umain.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
// Include headers from OS
#include <joyos.h>

//Math Stuff
#include <stdio.h>
#include <math.h>
#include <happylib.h>

#define PI 3.14159265358979323846
#define RADTODEG(RAD) ((RAD)*(180.0/PI))
#define THETATODEG(THETA) (THETA*180.0)/2047.0

#define SHARP_M 14150 
#define SHARP_C 10


//Include custom headers 
//#include <sensorsTeam6.h>
//#include <motorsTeam6.h>
//#include <collisionAvoidanceTeam6.h>

#define gyroPort 23
#define LSB_US_PER_DEG 1048830

//Positioning thing
#define VPS_PER_FOOT 682.6666
extern volatile uint8_t robot_id;


#define LRDistancePort 22
#define SRDistancePort 21 //8-23 pull-up ON
#define sideSRPort 20 //8-23 pull-up ON
#define leverBreakbeamPort 10 //8-23 pull-up ON

#define headServoPort 0
uint16_t headServoPos = 250;

#define leverServoPort 1
#define gateServoPort 2

#define leftMotorPort 0
#define rightMotorPort 1
#define sideMotorPort 2

int frobPos = 0;
int bodyPos = 0;
int captureDir = 0;
int8_t gyroInitPosition = 0;
uint8_t minedResources = 0;
uint32_t motorStartingTime = 0;

int16_t points[][2] = {{2047,0},{1791,443},{1280,1330},{1024,1773},{512,1773},{-512,1773},{-1024,1773},{-1280,1330},{-1791,443},{-2047,0},{-1791,-443},{-1280,-1330},{-1024,-1773},{-512,-1773},{512,-1773},{1024,-1773},{1280,-1330},{1791,-443},{443,256},{0,512},{-443,256},{-443,-256},{0,-512},{443,-256}};
char alphabet[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

// usetup is called during the calibration period. 
int usetup (void) {
	robot_id = 6;
	printf("Stabilizing \n");
	pause(1000);
	gyro_init(gyroPort, LSB_US_PER_DEG, 500);
	irdist_set_calibration (SHARP_M, SHARP_C);
	//Initialize some variables
	gyroInitPosition = gyro_get_degrees();
	bodyPos = gyro_get_degrees() - gyroInitPosition;
	//**Use wireless data to set captureDir as 1 or -1**
	captureDir = 1;
	
    return 0;
}


//General Functions
int MIN(int x, int y){
	if(x >= y){
		return x;
	}
	else{
		return y;
	}
}
uint16_t limit_vel(int vel){
	int MAX_VEL = 255;
	if (vel > MAX_VEL){
		return MAX_VEL;
	}
	else if (vel < -MAX_VEL){
		return -MAX_VEL;
	}
	else{
		return (int16_t)vel;
	}
}
int16_t limit_theta(int16_t theta){
	while ((theta > 360) || (theta < -360)){
		if(theta > 360){
			theta -= 360;
		}
		else if(theta < -360){
			theta += 360;
		}
	}
	return theta;
}
int16_t fixAngle(int16_t theta){
	if(theta < 0){
		return 360 + theta;
	}
	else{
		return theta;
	}
}
//Digital and Analog Reads
//Note: if you use digital_read() on an analog port -> returns 0/1 for < or > 2.5 V
void printDigitalReads(){
	for (int i = 0; i<=7; i++){
			printf("Digital Reads: %d",digital_read(i));
		}
	printf("\n");
}	
void printAnalogReads(){
	for (int i = 8; i<=23; i++){
		printf("Analog Reads: %d",analog_read(i));
	}
}
int16_t getPoint(char letter, uint8_t pos){
	int len=sizeof(alphabet)/sizeof(int);
	for(uint8_t i = 0; i<len; i++){
		if (letter == alphabet[i]){
			return points[i][pos];
		}
	}
	return 0;
}


//Sensors

int16_t gyroDegrees(){
	return gyro_get_degrees() - gyroInitPosition;
}
//**Needs to reset better**
void setBodyPos(){
	bodyPos = gyroDegrees();
	while (bodyPos > 360){
		bodyPos -= 360;
	}
	while (bodyPos < -360){
		bodyPos += 360;
	}
}
void resetGyroWithTheta(){
	copy_objects();
	bodyPos = (int)THETATODEG(game.coords[0].theta);
	gyroInitPosition = gyro_get_degrees() - bodyPos;
	
	
}
void sensors_turnTo(int8_t degrees){
	if(degrees < -90){
		degrees = -90;
	}
	else if(degrees > 90){
		degrees = 90;
	}

	servo_set_pos(headServoPort,(int)(300 + degrees*(200.0/90.0)));
	
}
float sensors_LRDistance(){
	return irdist_read(LRDistancePort)/2.54; //Converts to inches
}
float sensors_SRDistance(uint8_t port){
	return analog_read(port); //**convert this to inches**
}
uint8_t sensors_SRIsWithin(uint8_t port, float inches){
	return (sensors_SRDistance(port) < inches); //returns 1 or 0
}
uint8_t sensors_breakbeamIsBroken(uint8_t port){
 return (analog_read(port) > 500); //**500 is arbitrary, find actual value**
}

void sensors_signal(){
	sensors_turnTo(45);
	pause(200);
	sensors_turnTo(0);
	pause(200);
}


//Motors
uint8_t motorPortList[] = {0,1};
int leftMotorVel = 0;
int rightMotorVel = 0;

uint8_t isRobotStopped(){
	return ((leftMotorVel == 0) && (rightMotorVel == 0));
}
uint8_t isRobotStarting(){
	return ((get_time() - motorStartingTime) < 200);
}
void setDriveMotorVels(){
	motor_set_vel(leftMotorPort,leftMotorVel);
	motor_set_vel(rightMotorPort,rightMotorVel);
}
void motors_stopDriveMotors(){
	rightMotorVel = 0;
	leftMotorVel = 0;
	setDriveMotorVels();
}
void motors_brakeDriveMotors(){
	leftMotorVel = 0;
	rightMotorVel = 0;
	int len = sizeof(motorPortList)/sizeof(int);
 	for (int i = 0; i<len; i++){
		motor_brake(motorPortList[i]);
	}
}
void motors_turnLeft(uint8_t speed){
	leftMotorVel = -limit_vel(speed);
	rightMotorVel = limit_vel(speed);
	setDriveMotorVels(leftMotorVel, rightMotorVel);
}
void motors_turnRight(uint8_t speed){
	leftMotorVel = limit_vel(speed);
	rightMotorVel = -limit_vel(speed);
	setDriveMotorVels(leftMotorVel, rightMotorVel);
}

void motors_adjustLeft(uint8_t speed, uint8_t correction){
	leftMotorVel = limit_vel(speed - correction);
	rightMotorVel = limit_vel(speed + correction);
	setDriveMotorVels(leftMotorVel, rightMotorVel);
	pause(250);
}
void motors_adjustRight(uint8_t speed, uint8_t correction){
	leftMotorVel = limit_vel(speed + correction);
	rightMotorVel = limit_vel(speed - correction);
	setDriveMotorVels(leftMotorVel, rightMotorVel);
}

//This sets a given heading but doesn't drive along it
void motors_setHeading(uint8_t speed, int16_t heading){
	heading = fixAngle(heading);
	uint8_t stopped = isRobotStopped();
	uint32_t timer = get_time();
	while ((abs(fixAngle(bodyPos)-heading)>2) && (get_time()-timer < 5000)){
		if (heading < bodyPos){
			if(stopped){
				motors_turnRight(speed);
			}
			else{
				motors_adjustRight(speed,abs(bodyPos-heading)*2);
			}
		}
		else{
			if(stopped){
				motors_turnLeft(speed);
			}
			else{
				motors_adjustLeft(speed,abs(bodyPos-heading)*2);
			}
		}
		setBodyPos();
	}
	motors_stopDriveMotors();
}

//This will move along a certain heading
//If the robot is already moving this will be a slower adjustment
//To go straight - put heading in as bodyPos
void motors_followHeading(int16_t speed, int16_t heading){
	setBodyPos();
	float error = MIN((fixAngle(heading)-fixAngle(bodyPos)), (heading-bodyPos));
	float gain = 1.5; //**adjust this**
	int16_t correction = (int16_t)(error * gain);
	if (speed<0){
		correction = -correction;
	}
	leftMotorVel = limit_vel(speed -= correction);
	rightMotorVel = limit_vel(speed += correction);
	//printf("Heading: %d bodyPos: %d Error: %f Speed: %d LMV: %d RMV %d \n",heading, bodyPos, error, speed, leftMotorVel, rightMotorVel);
	setDriveMotorVels(leftMotorVel, rightMotorVel);

}
void motors_thetaSetHeading(int16_t speed, int16_t heading){
	copy_objects();
	int16_t theta = THETATODEG(game.coords[0].theta);
	int timer = get_time();
	while(abs(heading-theta)>2){
		copy_objects();
		theta = THETATODEG(game.coords[0].theta);
		motors_turnLeft(speed);
		if((get_time() - timer) > 2000){
			break;
		}
	}
	motors_stopDriveMotors();
}
void motors_thetaFollowHeading(int16_t speed, int16_t heading){
	if(isRobotStopped()){
		motors_setHeading(200,heading);
		leftMotorVel = limit_vel(speed);
		rightMotorVel = limit_vel(speed);
		setDriveMotorVels(leftMotorVel, rightMotorVel);
	}
	copy_objects();
	int16_t theta = THETATODEG(game.coords[0].theta);
	uint32_t startTime = get_time();
	float error = heading-theta;
	//printf("Heading: %d bodyPos: %d Error: %f LMV: %d RMV %d \n",heading, bodyPos, error, leftMotorVel, rightMotorVel);
	while ((error>2) || (error<-2)){
		copy_objects();
		theta = THETATODEG(game.coords[0].theta);
		error = heading - theta;
		float gain = 1.5; //**adjust this**
		int16_t correction = (int16_t)(error * gain);
		if (speed<0){
			correction = -correction;
		}
		leftMotorVel = limit_vel(speed -= correction);
		rightMotorVel = limit_vel(speed += correction);
		//printf("Heading: %d bodyPos: %d Error: %f Speed: %d LMV: %d RMV %d \n",heading, bodyPos, error, speed, leftMotorVel, rightMotorVel);
		setDriveMotorVels(leftMotorVel, rightMotorVel);
		
		if ((get_time() - startTime) > 50000){
			break;
		}
		
	}
}
//setBodyPos() must be called before calling this
void motors_gyroDriveStraight(int16_t speed, uint16_t seconds){
	uint32_t timer = get_time();
	setBodyPos();
	int16_t curBodyPos = bodyPos;
 	while(get_time() - timer < seconds){
		motors_followHeading(speed,curBodyPos);
	}
}
uint8_t motor_isStalled(uint8_t motor){
 	uint16_t current = motor_get_current(motor);
 	if ((current > frobPos) && (!isRobotStarting())){ //**Need to check value - this isn't right**
 		return 1;
 	}
 	else{
 		return 0;
 	}
 }
void motors_stopOnStall(){
 	int len = sizeof(motorPortList)/sizeof(int);
 	for (int8_t i = 0; i<len; i++){
 		if (motor_isStalled(motorPortList[i])){
 			motor_brake(motorPortList[i]);
 		}
 	}
}
void motors_stopOnLift(){
 	for (int8_t i = 0; i<2; i++){
 		uint16_t current = motor_get_current(i);
 		if (current < 3){ //**Need to get actual value**
 			motor_brake(motorPortList[i]);
 		}
 	}
}


//Collision avoidance
void avoidStaticObject(){
	//avoid non-moving things
}
void avoidMovingObject(){
	//keep track of object movement
	//different strategy to avoid moving objects - keeps track of their position
}
void simpleAvoidCollisions(int16_t speed){
	if(sensors_LRDistance()<12.0){
		motors_adjustLeft(speed,(12-sensors_LRDistance())*25);
	}
}
void avoidCollisions(int16_t speed){
	copy_objects();
	int16_t curPos[] = {game.coords[0].x,game.coords[0].y,THETATODEG(game.coords[0].theta)};
	
	//check if anything in range of SRDistance -> stop immediately, back up
	//check if anything in range of the LRDistance -> change path to avoid
//	printf("%.2f in\n",sensors_LRDistance());
	float dist = 10.0;
	if(sensors_LRDistance() < dist){
		motors_stopDriveMotors();
		for(int8_t i = -90; i<=90; i+=5){
			sensors_turnTo(i);
//			printf("%.2f in\n",sensors_LRDistance());
			pause(50);
			if(sensors_LRDistance() > dist){
				//motors_setHeading(150,i);
				sensors_turnTo(0);
				pause(50);
				if(i<0){
					motors_turnRight(speed);
					pause(500);
				}
				else{
//					printf("Turning Left");
					motors_turnLeft(speed);
					pause(500);
				}
				
				break;
			}
		}
		
	}
	dist = 24.0;
	if(sensors_LRDistance() < dist){
		for(int8_t i = -90; i<=90; i+=5){
			sensors_turnTo(i);
//			printf("%.2f in\n",sensors_LRDistance());
			pause(50);
			if(sensors_LRDistance() > dist){
				//motors_setHeading(150,i);
				sensors_turnTo(0);
				pause(50);
				if(i<0){
					motors_adjustRight(speed,50);
				}
				else{
//					printf("Turning Left");
					motors_adjustLeft(speed,50);
				}
				
				break;
			}
		}
		
	}

	

	//Turn the robot back straight 
// 	leftMotorVel = speed;
// 	rightMotorVel = speed;
// 	setDriveMotorVels(leftMotorVel, rightMotorVel);
	
	//use LR sensor when going straight -> when it finds something, scan 
	//a few degrees to see if it ends (if so, it's probably the other robot)
	//check if in map, if is 
	//identify type of object (static,moving) in path -> use correct object
}
void avoidWalls(int16_t speed){
	copy_objects();
	int16_t curPos[] = {game.coords[0].x,game.coords[0].y,THETATODEG(game.coords[0].theta)};
	int8_t dist = 100;
	
	if((curPos[0]<getPoint('S',0) + dist) && (curPos[0]>getPoint('U',0) - dist) && (curPos[1]<getPoint('T',1)+dist) && (curPos[1]>getPoint('W',1) - dist)){
		if(curPos[0]>getPoint('S',0)){
			//check the angle
		}
		else if(curPos[0]<getPoint('U',0)){
		
		}
		else if(curPos[1]>getPoint('T',1)){
		
		}
		else if(curPos[1]<getPoint('W',1)){
			
		}
	}
	if((abs(curPos[0] - getPoint('U',0))<dist) && (curPos[1]<getPoint('T',1)) && (curPos[1]>getPoint('W',1))){
		if((curPos[2]<180) && (curPos[2]>0)){
			motors_adjustLeft(speed,abs(curPos[2]-90)*2);
			avoidCollisions(speed); 
		}
		else{
			motors_adjustRight(speed,abs(curPos[2]-270)*2);
			avoidCollisions(speed);
		}
	}
}
//Interaction

void captureTerritory(){
	//**SR Navigation to line up the motor**
	
	//**Until the territory is captured - feedback from wireless chip**
	motor_set_vel(sideMotorPort, 100*captureDir);
}
//returns 0 on failure to mine, 1 on success
uint8_t mineResource(){
	uint32_t time = get_time();
	while(!sensors_breakbeamIsBroken(leverBreakbeamPort)){
		//**SR Navigation (drive in an arc)**
		if((get_time() - time) > 5000){
			return 0;
		}
	}
	servo_set_pos(leverServoPort,400); //**Arbitrary value - fix**
	pause(500); //**Arbitrary value - fix**
	servo_set_pos(leverServoPort,200); //**Arbitrary value - fix**
	minedResources++;
	return 1;
}
void releaseResources(){
	//**SR Navigation
	servo_set_pos(leverServoPort,400); //**Arbitrary value - fix**
	pause(500*minedResources); //**Arbitrary value - fix**
	servo_set_pos(leverServoPort,200); //**Arbitrary value - fix**
	minedResources = 0;
}
void maximizePoints(){
	//**Not void - should return point to navigate to and action to take**
}



//Navigation

// void resetGyro(){
// 	//**MOVE HEAD SERVO TO START POSITION
// 	copy_objects();
// 	float theta = THETATODEG(game.coords[0].theta);
// 	while((theta>1.5) || (theta < -1.5)){
// 		copy_objects();
// 		float theta = THETATODEG(game.coords[0].theta);
// 		if(theta>1.5){
// 			motors_turnRight();
// 		}
// 		else if(theta < -1.5){
// 			motors_turnLeft();
// 		}
// 	}
// 	motors_stopDriveMotors();
// 	pause(100);
// 	gyroInitPosition = gyro_get_degrees();
// 	
// }

void navigateToPoint(int16_t speed, int16_t goalX, int16_t goalY){ 
	
	//Get current position
	copy_objects();
	int16_t curX = game.coords[0].x;
	int16_t curY = game.coords[0].y;
	int16_t dy = goalY-curY;
	int16_t dx = goalX-curX;
	int16_t curTheta = THETATODEG(game.coords[0].theta);
	int16_t goalTheta = (int16_t)RADTODEG(atan2(dy,dx));	

	int16_t err = 300; //**CHANGE**
	//While the difference between goal and current position is greater than allowed err
	while ((abs(dx)>err) ||(abs(dy)>err)){
//		simpleAvoidCollisions(speed);
		copy_objects();
		curX = game.coords[0].x;
		curY = game.coords[0].y;
		dy = goalY-curY;
		dx = goalX-curX;
		curTheta = THETATODEG(game.coords[0].theta);
		goalTheta = (int16_t)RADTODEG(atan2(dy,dx));	
		
//		printf("Cur Theta: %d Goal Theta: %d\n", curTheta, goalTheta);
		float error = goalTheta - curTheta;
		float gain = 2; //**adjust this**
		int16_t correction = (int16_t)(error * gain);
		leftMotorVel = limit_vel(speed - correction);
		rightMotorVel = limit_vel(speed + correction);
		setDriveMotorVels(leftMotorVel, rightMotorVel);
		

		//motors_thetaFollowHeading(200,goalTheta);

	}
//motors_stopDriveMotors();
//	motors_stopOnStall();
}

void navigateToPoints(){
	copy_objects();
	int16_t goalX = game.coords[2].x;
	int16_t goalY = game.coords[2].y;
	int16_t curX = game.coords[0].x;
	int16_t curY = game.coords[0].y;
	int16_t dy = goalY-curY;
	int16_t dx = goalX-curX;
	int16_t curTheta = THETATODEG(game.coords[0].theta);
	int16_t goalTheta = (int16_t)RADTODEG(atan2(dy,dx));	
	int16_t speed = 200;
	float error = goalTheta - curTheta;
	float gain = 2; //**adjust this**
	int16_t correction = (int16_t)(error * gain);
	leftMotorVel = limit_vel(speed - correction);
	rightMotorVel = limit_vel(speed + correction);
	setDriveMotorVels(leftMotorVel, rightMotorVel);
	avoidCollisions(speed);
}

void driveAroundAvoidingThings(){
	leftMotorVel = 100;
	rightMotorVel = 100;
	setDriveMotorVels(leftMotorVel, rightMotorVel);
	avoidCollisions(100);
}

//Exploration period

void explore(int16_t speed){
//	motors_thetaSetHeading(speed,0);
	navigateToPoint(speed,getPoint('H',0),getPoint('J',1));
	navigateToPoint(speed,getPoint('F',0), getPoint('L',1));
	navigateToPoint(speed,getPoint('E',0), getPoint('L',1));
	navigateToPoint(speed,getPoint('C',0), getPoint('A',1));
	navigateToPoint(speed,getPoint('E',0), getPoint('C',1));
	navigateToPoint(speed,getPoint('F',0), getPoint('H',1));
 	navigateToPoint(speed,getPoint('H',0), getPoint('J',1));


	

	//use differential to detect where the gearboxes are 
}

//Tests

void testGyro(){
	printf("Gyro Init Pos: %d Body Pos: %d ",gyroInitPosition, bodyPos);
	setBodyPos();
	printf("Gyro degrees: %d\n",bodyPos);
}
void testFrob(uint8_t range){
	if(frob_read_range(0,range) != frobPos){
			frobPos = frob_read_range(0,range);
			printf("frob pos: %d\n",frobPos);
	}
}
void testLRDistanceSensor(){
	printf("LR Distance: %d\n",sensors_LRDistance()); //converted to inches
}
void testSRDistanceSensors(){
	printf("SR Distance: %d ", sensors_SRDistance(SRDistancePort));
	printf("Side: %d\n", sensors_SRDistance(sideSRPort));
	pause(1000);
}
void testBreakbeams(){

}
void testNavigation(){
	motors_setHeading(100,90);
	uint32_t startTime = get_time();
	motorStartingTime = get_time();
	while ((get_time() - startTime) < 5000){
		motors_gyroDriveStraight(200,bodyPos);
		motors_stopOnStall();
	}
	motorStartingTime = get_time();
	while ((get_time() - startTime) < 10000){
		motors_followHeading(200, 0);
		motors_stopOnStall();
	}
	motors_stopDriveMotors();
}

//Main routine
int umain (void) {
	//For first 10 seconds, run explore

	//Check Inputs, Do while robot running

// 	copy_objects();	
//  	explore(175);
//  	motors_stopDriveMotors();
//  	sensors_signal();
//  	navigateToPoint(150,getPoint('M',0), getPoint('L',1)-200);
//  	motors_stopDriveMotors();
//  	sensors_signal();
//  	motors_thetaSetHeading(100,0);
//  	captureTerritory();
//  	sensors_signal();
//  	pause(1000);
//  	copy_objects();
//  	navigateToPoint(100,getPoint('N',0),game.coords[0].y);
//  	motors_stopDriveMotors();

//  	motors_gyroDriveStraight(150,5000);
//  	motors_stopDriveMotors();
//  	motors_setHeading(100,180);
//  	motors_gyroDriveStraight(150,5000);
//  	motors_stopDriveMotors();
	
// 		motors_setHeading(100,90);
// 		motors_setHeading(100,0);
// 		motors_setHeading(100,-90);
// 		motors_setHeading(100,-180);
// 		motors_setHeading(100,180);
// 		motors_setHeading(100,0);
 	
 	
	while(1){
// 		testGyro();
 		testSRDistanceSensors();		
//		printf("LR Distance: %f\n SR Distance: %f\n",sensors_LRDistance(),sensors_SRDistance());

// 		frobPos = frob_read_range(100,400);
		

//		printf("%.2f in\n",sensors_LRDistance());

// 
// 		servo_set_pos(0,frobPos);

		// **Ask about threading**
// 		Should thread the navigation and keep object avoidance in the while loop?
// 		For threading -> function must be of type 'int'
// 		
// 		create_thread(&navigateToPoint(game.coords[2].x,game.coords[2].y),STACK_DEFAULT,1,"toPoint");


		
	}


    return 0;
}