06.07.2013 Views

Final Report - Claymore - Grand Valley State University

Final Report - Claymore - Grand Valley State University

Final Report - Claymore - Grand Valley State University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

eturn diff;<br />

}<br />

//......................................................................................<br />

int controller(int Cd, int Cf)<br />

{<br />

int Ce; // Error variable<br />

int Cw; // Corrected count<br />

}<br />

Ce = Cd - Cf;<br />

Cw = (Kp * Ce) + (Ki * integrate(Ce) / 1000) + (Kd * derivitive(Ce));<br />

if(US_DEF == 1) {<br />

if((rpos[THEM_OFF] < THRESHOLD) && (rpos[US_DEF] < THRESHOLD)) Cw = 0;<br />

else Cw *= -1;<br />

}<br />

return Cw;<br />

//......................................................................................<br />

void AD_setup()<br />

{<br />

ADCSRA = 0x80;// turn on ADC<br />

DDRA = (unsigned char)0; // set port A to input<br />

}<br />

//......................................................................................<br />

void AD_read(void) { // interrupt driven encoder update<br />

int count;<br />

for (count = 0; count < 4; count++) {<br />

ADMUX = (0x40 + count); // REFS1=0,REFS0=1,ADLAR=0,MUX4=0 |<br />

MUX3=0,MUX2=0,MUX1=0,MUX0=1<br />

ADCSRA = 0xC0;<br />

while ((ADCSRA & _BV(ADSC)) != 0);<br />

ADCSRA = 0xC0;<br />

while ((ADCSRA & _BV(ADSC)) != 0);<br />

rpos[count + 1] = ADCW >> 2;<br />

}<br />

}<br />

if(pos_last != rpos[US_DEF])<br />

{<br />

moving = 1;<br />

}<br />

else<br />

{<br />

moving = 0;<br />

}<br />

//......................................................................................<br />

int deadband(int c_wanted)<br />

{// Call this routine when updating<br />

int c_pos;<br />

int c_neg;<br />

int c_adjusted;<br />

if(moving == 1) // If moving -> kinetic<br />

{<br />

c_pos = c_kinetic_pos;<br />

c_neg = c_kinetic_neg;<br />

}<br />

else // Not moving -> static<br />

{<br />

c_pos = c_static_pos;<br />

c_neg = c_static_neg;<br />

}<br />

46

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!