/********************************************/
/*Programmable Autonomous Creature On-wheels*/
/*      PACO99 Software V1.1 3-25 6:00 pm   */
/*      By Rick Hormigo for USM Robotics    */
/*Kentucky SECON99 IEEE Hardware Competition*/
/*           ***********************        */
/*           *This year we'll win!!*        */
/*           ***********************        */
/********************************************/

#define Motor 0x4000
#define Ctrl 0x5000
#define USound 0x4000
float error;
int left[]={
	0b10100000, 
	0b10010000, 
	0b01010000, 
	0b01100000};
int right[]={
	0b00000101, 
	0b00001001, 
	0b00001010, 
	0b00000110};
int ltorque[]={
	0b10000001, 
	0b00011000, 
	0b01000010, 
	0b00100100};
int stepr=0;
int stepl=0;
int miles=0;
/*long accel[]={3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,4L,4L,4L,4L,4L,4L,4L,4L,4L,
		5L,5L,5L,5L,5L,5L,6L,6L,6L,7L,7L,8L,10L,14L,16L,20L,24L};*/
long accel[]={4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,5L,5L,5L,5L,5L,5L,5L,
		5L,5L,5L,6L,6L,6L,6L,6L,7L,7L,7L,8L,10L,14L,16L,20L,24L};
long accel2[]={6L,6L,6L,6L,6L,6L,6L,6L,6L,6L,6L,6L,6L,6L,7L,7L,7L,7L,7L,7L,7L,
		7L,7L,7L,8L,8L,8L,8L,8L,9L,9L,9L,11L,14L,16L,20L,24L,28L};
/********************MAIN CORE**********************/
void main(){	
	int distance,wall;
	float speed,arg,n;
	/* init ports */
for(;;){
	poke(Motor,0);
	poke(Ctrl,0);
	printf("Let's win guys!  With PACO 99!!\n");
	wstart();
	servo_on();
	wtone();
	city();
	horse();
	mountain();
	mine();
	sleep(1.0);
	beep();
	poke(Motor,0);
	poke(Ctrl,2);
	}
}

/*********************************************************/
/*                    Functions                          */
/*********************************************************/

/**************** Wait you push Start Buttom *************/
void wstart(void){
	while(!start_button()){
		if(stop_button()){scan();}
	}
	beep();
	sleep(1.0);
}

/***** Waiting the buzzer tone to start or 30 seconds ****/
void wtone(void){
	int tone,time;
	tone=0;
	for(time=0;time<15000;time++){
		if (digital(10)){tone++;}
		if (tone>100){time=15000;}
	}
	beep();
}

/************************* city **************************/
void city(void){
	servo(3100);
	poke(Ctrl,2);           /* Select Left tracking    */
	acceleration();         /*Already 1.79 inches      */
	ahead(250);
	breaking();             /*Already 1.79 inches      */
	left_turn(82);          /*slighly less than 90 deg */
	poke(Ctrl,2);           /* Select Left tracking    */
	acceleration();
	ahead(67);
	twall(1000,5,6);
	ahead(99);
	breaking();
	left_turn(82);          /*left 90 deg*/
	back_ali();
	servo(2000);
	poke(Ctrl,2);           /* Select Left tracking */
	acceleration();         /*Already 1.79 inches*/
	ahead(265);
	breaking();             /*Already 1.79 inches*/
	right_turn(82);         /*right 90 deg*/
	servo(2500);
}
/********************** horse **************************/
void horse(void){
	back_ali();
	msleep(50L);
	acceleration();
	twallh(1000,14,15);
	ahead(45);
	servo(550);
	twall(355,22,23);
	poke(Ctrl,0b00001110);/*Energize Electromagnet Hi-Flux and track left wall*/
	twall(65,23,24);
	poke(Ctrl,0b00000110);/*Energize Electromagnet Lo-Flux and track left wall*/
	twall(415,20,22);
	rcurve(160);
	servo(4500);
	sleep(1.0);
	poke(Ctrl,2);         /*Release the ball and keep tracking wall */
	servo(2500);
	sleep(1.0);
	rcurve(164);
	servo_off();
}
/************************ Mountain*********************/
void mountain(void){
	twallh(500,15,16); /*look for end horse pavillion*/
	printf("wall \n");
	servo(3100);
	aheads(200);
	printf("turn \n");
	left1w(184);
	/*here is ready to go backwards until the top */
	wline(); /* go backwards a floor line */
	poke(Ctrl,1);
	uplinem();
	poke(Ctrl,2); /*Now on top change to US 2 to track left wall*/
	msleep(10L);
	/*printf("go down \n");
	acceleration2();*/
	tmountain_wall();
	printf("bottom \n");
	rcurve(40); /*arriving to bottom wall (compensation)*/
	aheadss(20);
	lb1w(178); /* ass toward ext. wall in the mine area */
	backs(290);
	back_ali();
	aheads(40);
	right1w(175);/***** ass towars the mine****** !! */
	printf("bottom wall\n");
	wline(); /* nop New wait line using calculus */
	poke(Ctrl,2);
	printf("bottom line\n");
	servo(2000);
	gomine(); /* used here to reach the mine*/
	
}
/************************ Mine *********************/
void mine(void){
	/*tmountain_wall2();
	printf("Close to door\n");
	door_approach();*/
	printf("Mine Door\n");
	servo(3100);
	/*lcurve(65);*/                /*Doorspring Compensation*/
	printf("In the Mine\n");
	backs(150);
	aheads(200);
	sleep(1.0);               
	left_turn(82);
	wline();
	poke(Ctrl,1);	
	rb1w(175);
	uplinem();
	poke(Ctrl,0);

}

/**************** Other Subfunctions *************/

/*Acceleration and Breaking Proccesses*/
void acceleration(void){
	int s;
	for(s=37;s>=0;s--){
		poke(Motor,(right[stepr]|left[stepl]));
		stepr++;
		stepl++;
		if(stepr==4){stepr=0;}
		if(stepl==4){stepl=0;}
		msleep(accel[s]);
	}
}
/*Acceleration and Breaking Proccesses*/
void acceleration2(void){
	int s;
	for(s=37;s>=0;s--){
		poke(Motor,(right[stepr]|left[stepl]));
		stepr++;
		stepl++;
		if(stepr==4){stepr=0;}
		if(stepl==4){stepl=0;}
		msleep(accel[s]);
	}
}
void breaking(void){
	int s;
	for(s=0;s<=37;s++){
		poke(Motor,(right[stepr]|left[stepl]));
		stepr++;
		stepl++;
		if(stepr==4){stepr=0;}
		if(stepl==4){stepl=0;}
		msleep(accel[s]);
	}
}
/*Go ahead without acceleration or breaking for (int s) steps at speed 3L */
void ahead(int s){
	for(;s>0;s--){
		poke(Motor,(right[stepr]|left[stepl]));
		stepr++;
		stepl++;
		if(stepr==4){stepr=0;}
		if(stepl==4){stepl=0;}
		msleep(3L);
	}
}
/*Go ahead without acceleration or breaking for (int s) steps at speed 3L */
void aheads(int s){
	for(;s>0;s--){
		poke(Motor,(right[stepr]|left[stepl]));
		stepr++;
		stepl++;
		if(stepr==4){stepr=0;}
		if(stepl==4){stepl=0;}
		msleep(6L);
	}
}
/*Go ahead s steps low torque to alin */
void aheadss(int d){
	int s;
	for(;d>=0;d--){
		for(s=0;s<=3;s++){
			poke(Motor,(ltorque[s]));
			msleep(6L);
		}
	}
}
/*Go backwards without acceleration or breaking for (int s) steps at speed 3L */
void backs(int s){
	for(;s>0;s--){
		poke(Motor,(right[stepr]|left[stepl]));
		stepr--;
		stepl--;
		if(stepr==-1){stepr=3;}
		if(stepl==-1){stepl=3;}
		msleep(6L);
	}
}


/*Make right curve*/
void rcurve(int s){
	int evens=0;
	for(;s>0;s--){
		poke(Motor,(right[stepr]|left[stepl]));
		if (evens & 1){stepl++;}
		evens++;
		stepr++;
		if(stepr==4){stepr=0;}
		if(stepl==4){stepl=0;}
		msleep(5L);
	}
}
/*Make left curve*/
void lcurve(int s){
	int evens=0;
	for(;s>0;s--){
		poke(Motor,(right[stepr]|left[stepl]));
		if (evens & 1){stepr++;}
		evens++;
		stepl++;
		if(stepr==4){stepr=0;}
		if(stepl==4){stepl=0;}
		msleep(8L);
	}
}
/*Go backwards to hit the wall*/
void back_ali(void){
	int d,s;
	for(d=28;d>=0;d--){
		for(s=3;s>=0;s--){
			poke(Motor,(ltorque[s]));
			msleep(10L);
		}
	}
}
/*Go ahead traking wall from min_dis to max_dis without acceleration
or breaking for (int s) steps at speed supposed 1L*/
void twall(int s,int min_dist,int max_dist){
	int wall;
	int rerror=0; /* error due to stop right motor */
	int lerror=0; /* error due to stop left motor */
	for(;s>0;s--){
		wall=peek(USound);
		if (wall<min_dist){poke(Motor,right[stepr]);rerror++;stepr++;}
		if (wall>max_dist){poke(Motor,left[stepl]);lerror++;stepl++;}
		if (wall>=min_dist && wall<=max_dist){poke(Motor,(right[stepr]|left[stepl]));stepr++;stepl++;}
		if (wall==128){s=0;}
		if(stepr==4){stepr=0;}
		if(stepl==4){stepl=0;}
		msleep(2L);
	}
	/* Load average error in rerror */
	error=((((float)(int)(rerror))+((float)(int)(lerror)))/6.0); 
}

/*Go ahead traking wall from min_dis to max_dis without acceleration
or breaking for (int s) steps at speed supposed 1L*/
void twallh(int s,int min_dist,int max_dist){
	int wall,rwall;
	int count=0;
	for(;s>0;s--){
		if (count==3){
			rwall=peek(USound);
			if (rwall<30 && rwall>0) {s=0;}		
			poke(Ctrl,2);
		}
		if (count==0){
			count=6;
			wall=peek(USound);		
			poke(Ctrl,1);
		}
		count--;
		if (wall<min_dist){poke(Motor,right[stepr]|left[stepl]);stepr++;}
		if (wall>max_dist){poke(Motor,right[stepr]|left[stepl]);stepl++;}
		if (wall>=min_dist && wall<=max_dist){
			poke(Motor,(right[stepr]|left[stepl]));
			stepr++;
			stepl++;
		}
		if(stepr==4){stepr=0;}
		if(stepl==4){stepl=0;}
		msleep(6L);
	}
}

/*turns*/
void right_turn(int s){
	for(;s>0;s--){
		poke(Motor,(right[stepr]|left[stepl]));
		stepr++;
		stepl--;
		if(stepr==4){stepr=0;}
		if(stepl==-1){stepl=3;}
		msleep(6L);
	}
}

/*turns*/
void syncro(void){
	int s=0;
	while(digital(11) || digital(12)&& s<40){
		s++;
		poke(Motor,(right[stepr]|left[stepl]));
		stepr++;
		stepl--;
		if(stepr==4){stepr=0;}
		if(stepl==-1){stepl=3;}
		msleep(15L);
	}
	s=0;
	while(digital(11) || digital(12)&& s<80){
		s++;
		poke(Motor,(right[stepr]|left[stepl]));
		stepr--;
		stepl++;
		if(stepr==-1){stepr=3;}
		if(stepl==4){stepl=0;}
		msleep(15L);
	}
	s=0;
	while(digital(11) || digital(12)&& s<40){
		s++;
		poke(Motor,(right[stepr]|left[stepl]));
		stepr++;
		stepl--;
		if(stepr==4){stepr=0;}
		if(stepl==-1){stepl=3;}
		msleep(15L);
	}
}
void 	left1w(int s){
	for(;s>0;s--){
		poke(Motor,(right[stepr]|left[stepl]));
		stepr--;
		if(stepr==-1){stepr=3;}
		msleep(5L);
	}
}
void 	right1w(int s){
	for(;s>0;s--){
		poke(Motor,(right[stepr]|left[stepl]));
		stepr++;
		if(stepr==4){stepr=0;}
		msleep(5L);
	}
}
void 	lb1w(int s){
	for(;s>0;s--){
		poke(Motor,(right[stepr]|left[stepl]));
		stepr--;
		if(stepr==-1){stepr=3;}
		msleep(5L);
	}
}
void 	rb1w(int s){
	for(;s>0;s--){
		poke(Motor,(right[stepr]|left[stepl]));
		stepl--;
		if(stepl==-1){stepl=3;}
		msleep(5L);
	}
}
void left_turn(int s){
	for(;s>0;s--){
		poke(Motor,(right[stepr]|left[stepl]));
		stepr--;
		stepl++;
		if(stepr==-1){stepr=3;}
		if(stepl==4){stepl=0;}
		msleep(6L);
	}
}

/*Go backwards traking mountain line w/o acceleration
or breaking untill top */
void uplinem(void){
	int counts=1200;
	int s,rline,lline,c;
	for(s=3,c=0;s>0;c++){
		rline=!digital(11);
		lline=!digital(12);
		poke(Motor,(right[stepr]|left[stepl]));
		if (rline){stepl--;}
		if (lline){stepr--;}
		if (!lline && !rline){stepr--; stepl--;}
		if (((peek(USound)>30)&& (c>600))||counts<0){s--;}
		if(stepr==-1){stepr=3;}
		if(stepl==-1){stepl=3;}
		msleep(10L);
		counts--;
	}
}

/*Go backwards traking line until mine w/o acceleration */
void gomine(void){
	int s,rline,lline,c;
	for(s=3,c=0;s>0;c++){
		rline=!digital(11);
		lline=!digital(12);
		poke(Motor,(right[stepr]|left[stepl]));
		if (rline){stepl--;}
		if (lline){stepr--;}
		if (!rline && !lline && (c>400)){
			s--;
		}
		if (!rline && !lline && (c<401)){
			syncro();	
		}
		if(stepr==-1){stepr=3;}
		if(stepl==-1){stepl=3;}
		msleep(7L);
	}
}

/*Go ahead down traking wall mountain*/
void tmountain_wall(void){
	int wall,s,c,counts;
	for(s=2;s>0;){
		/*if (c==2) {wall=peek(USound);c=0;}*/
		wall=peek(USound);
		if (counts>85 && counts<127){wall=0;}
		if (wall<9 && wall >0){poke(Motor,right[stepr]|left[stepl]);stepr++;}
		if (wall>11 && wall <17){poke(Motor,right[stepr]|left[stepl]);stepl++;}
		if ((wall>=9 && wall<=11)||wall==0){poke(Motor,(right[stepr]|left[stepl]));stepr++;stepl++;}
		if (wall>16){s--;}
		if(stepr==4){stepr=0;}
		if(stepl==4){stepl=0;}
		msleep(10L);
		counts++;
	}
}

/*Go ahead in bottom wall mountain*/
void tmountain_wall2(void){
	int wall,s;
	for(s=500;s>0;){
		wall=peek(USound);
		if (wall<12 && wall >0){poke(Motor,right[stepr]|left[stepl]);stepl++;}
		if (wall>13 && wall <128){poke(Motor,right[stepr]|left[stepl]);stepr++;}
		if ((wall>=12 && wall<=13)||wall==0){poke(Motor,(right[stepr]|left[stepl]));stepr++;stepl++;}
		s--;
		if(stepr==4){stepr=0;}
		if(stepl==4){stepl=0;}
		msleep(10L);
	}
}

/*Go ahead traking wall mountain until mine door*/
void door_approach(void){
	int wall,s;
	for(s=3;s>0;){
		wall=peek(USound);
		if (wall<7 && wall > 0){poke(Motor,right[stepr]|left[stepl]);stepl++;}
		if (wall>8 && wall <128){poke(Motor,right[stepr]|left[stepl]);stepr++;}
		if ((wall>=7 && wall<=8)||wall==0){poke(Motor,(right[stepr]|left[stepl]));stepr++;stepl++;}
		if (wall>50){s--;}
		if(stepr==4){stepr=0;}
		if(stepl==4){stepl=0;}
		msleep(6L);
	}
}

void wline(void){
	int count=50;
	int rir,lir;
	/*printf("%d %d\n",digital(11),digital(12));*/
	rir=1;
	lir=1;
	while(rir || lir){
		rir=digital(11);
		lir=digital(12);
		if (count>0){rir=1;lir=1;}
		poke(Motor,(right[stepr]|left[stepl]));
		count--;
		stepr--;	
		stepl--;
		if(stepr==-1){stepr=3;}
		if(stepl==-1){stepl=3;}
		msleep(10L);
	}
}

void wlineb(void){
	int count=0;
	int f=0;
	int rir,lir,wall,sum,ref,avg;
	/*printf("%d %d\n",digital(11),digital(12));*/
	rir=1;
	lir=1;
	ref=peek(USound);
	sum=0;
	while(rir || lir){
		wall=peek(USound);
		if (count<49){sum=sum+wall;}
		if (count=49){avg=sum/50;sum=0;}
		if (count>49){sum=sum +(avg-wall);}
		/*Integral of this area */
		rir=digital(11);
		lir=digital(12);
		if (count<50){rir=1;lir=1;}
		if (sum>-100 && count>340 && f=0){scurve(); f=1;}
		poke(Motor,(right[stepr]|left[stepl]));
		count++;
		stepr--;	
		stepl--;
		if(stepr==-1){stepr=3;}
		if(stepl==-1){stepl=3;}
		msleep(10L);
	}
}

void scurve(void){
	right_turn(30);
	backs(50);
	left_turn(25);
}

void scan(void){
	int right,left,irr,irl;
	for(;;){
		poke(Ctrl,1);
		irr=!digital(11);
		msleep(100L);
		right=peek(USound);
		poke(Ctrl,2);
		irl=!digital(12); /* left seen from front */
		msleep(100L);
		left=peek(USound);
		printf("USR %d USL %d IRL %d IRR %d\n",right,left,irl,irr);
	}
}