int main(void) { uart_init(); stdout = stdin = &uart_str;
int mid=5,Error=0,Derivative=0,Integral=0,PrevError=0,Timer=0; int Ki=90; int Kp=1,Kd=70,PowerMotors=100;
printf("Linetracer ready 1!\n");
DDRC=0x20; DDRD=0x30;
Stop();
_delay_ms(100); //PWM, Phase and frequency correct, TOP=ICR1 TCCR1A=0xA0; //clear on up-counting, set on down-counting TCCR1B=0x12; //Clock prescaler 1/8 ICR1=9216; //Set 50Hz frequency Stop();
while(1==1){ DDRA=0xFF; PORTA=0xFF; DDRC|=0xC0; PORTC|=0xC0; _delay_us(1000); DDRA=0x00; PORTA=0x00; DDRC&=~0xC0; PORTC&=~0xC0; PORTC|=0x20; _delay_us(500); int x=PINA; int y=PINC>>6; if(y==1){ y=2; }else if (y==2){y=1;}; x=x+(y<<8); int pos1=-1; int pos2=10;
//PID-controller int DOT=0; switch (pos){ case 9: DOT=-50;PowerMotors=15; break; case 8: DOT=-40;PowerMotors=50; break; case 7: DOT=-30;PowerMotors=85; break; case 6: DOT=-20;PowerMotors=90; break; case 5: DOT=-10;PowerMotors=100; break; case 4: DOT=10;PowerMotors=100; break; case 3: DOT=20;PowerMotors=90; break; case 2: DOT=30;PowerMotors=85; break; case 1: DOT=40;PowerMotors=50; break; case 0: DOT=50;PowerMotors=15; break; default: break;} Error=mid-pos; Derivative=Error-PrevError; Integral+=Error;