//PING Sonar //equations were taken from here and not independently verified: //http://www.societyofrobots.com/member_tutorials/node/174 //http://www.societyofrobots.com/robotforum/index.php?topic=5123.msg40008 //http://www.societyofrobots.com/robotforum/index.php?topic=4656.30 //uses timer0 int sonar_Ping() { #define PINGPIN 0 // assign a pin to the Ping Sensor #undef DDR #undef PIN #define DDR DDRC #define PORT PORTC #define PIN PINC PORT_ON(DDR, PINGPIN); // Switch PingPin to OUPUT // ------Trigger Pulse-------------- PORT_OFF(PORT, PINGPIN); // Bring PingPin low before starting trigger pulse delay_us(2); // Wait for 2 microseconds PORT_ON(PORT, PINGPIN); // Bring PingPin High for 5us according to spec sheet. delay_us(5); // Wait for 5 microseconds PORT_OFF(PORT, PINGPIN);; // Bring PingPin Low and standby //--------End Trigger Pulse--------------------- FLIP_PORT(DDR, PINGPIN); // Switch PingPin to INPUT loop_until_bit_is_set(PIN, PINGPIN); // Loop until the the PingPin goes high (macro found in sfr_def.h) //clears timer, reset overflow counter reset_timer0(); //reset timer 0 loop_until_bit_is_clear(PIN, PINGPIN); // Loop until the the PingPin goes low (macro found in sfr_def.h) //read timer0's overflow counter //255 is count before overflow, dependent on clock return (get_timer0_overflow()*255+TCNT0) * 2.068965517;//elapsed time x conversion }
static void echo(double *ping_value,const uint8_t pingpin) { /*Round-trip-time, unit microseconds*/ uint16_t elapsed_time; /* ------Trigger Pulse--------------------------*/ PORT_ON(DDR, pingpin); PORT_OFF(PORT, pingpin); _delay_us(2); PORT_ON(PORT, pingpin); _delay_us(5); PORT_OFF(PORT, pingpin); /*--------End Trigger Pulse---------------------*/ /*--------Meassure pulse------------------------*/ FLIP_PORT(DDR, pingpin); loop_until_bit_is_set(PIN, pingpin); reset_timer_0(); loop_until_bit_is_clear(PIN, pingpin); /*--------Stop Meassure pulse-------------------*/ /* MAXCOUNTER is dependent on timer */ elapsed_time = (tot_overflow * MAXCOUNTER) + TCNT0; /*Simple band-pass filter*/ if (elapsed_time >= ping_distance_max) *ping_value = 0; else if (elapsed_time <= ping_distance_min) *ping_value = 0; else *ping_value = (elapsed_time * TO_CM); _delay_us(250); }
//Front and Back LEDS void Lite(unsigned int val) { if (val) PORT_ON(PORTD,5); else PORT_OFF(PORTD,5); }
void noconn_led(unsigned int val) { if (val) PORT_ON(PORTC,6); else PORT_OFF(PORTC,6); }
void locked_led(unsigned int val) { if (val) PORT_ON(PORTC,5); else PORT_OFF(PORTC,5); }
void repair_led(unsigned int val) { if (val) PORT_ON(PORTC,7); else PORT_OFF(PORTC,7); }
void pin_led(unsigned int val) { if (val) PORT_ON(PORTC,3); else PORT_OFF(PORTC,3); }
void hold_led(unsigned int val) { if (val) PORT_ON(PORTC,2); else PORT_OFF(PORTC,2); }
int main(void) { uint16_t adc_result0, adc_result1, adc_result2, adc_result3, adc_result4; DDRD = 0xff; //set PORTD for LED output PORTD = 0x00; // output low - pull up resistance DDRB = 0xff; //set PORTB for LED output PORTB = 0x00; // output low - pull up resistance //initialize adc adc_init(); while(1) { adc_result0 = adc_read(0); //read adc value at PC0 adc_result1 = adc_read(1); adc_result2 = adc_read(2); adc_result3 = adc_read(3); adc_result4 = adc_read(4); //condition for LED to be on if (adc_result0 > 200) { PORT_ON(PORTD,0); PORT_ON(PORTD,1); } else { PORT_OFF(PORTD,0); PORT_OFF(PORTD,1); } if (adc_result1 > 200) { PORT_ON(PORTD,2); PORT_ON(PORTD,3); } else { PORT_OFF(PORTD,2); PORT_OFF(PORTD,3); } if (adc_result2 > 200) PORT_ON(PORTD,4); else PORT_OFF(PORTD,4); if (adc_result3 > 200) { PORT_ON(PORTD,5); PORT_ON(PORTD,6); } else { PORT_OFF(PORTD,5); PORT_OFF(PORTD,6); } if (adc_result0 > 200) { PORT_ON(PORTD,7); PORT_ON(PORTB,0); } else { PORT_OFF(PORTD,7); PORT_OFF(PORTB,0); } } }
void control(void) { reset_timer5(); uartFlushReceiveBuffer(1); a2dSetReference(ADC_REFERENCE_256V); delay_us(100); PORT_OFF(PORTB,7); while(1) { char read = uart1GetByte(); if(read == 'U') { //Rangefinder only for the Hand! We only have 3 uart ports! uart0SendByte(0x22); //sends 4 bytes with the command to read distance (0x22) uart0SendByte(0x00); uart0SendByte(0x00);uart0SendByte(0x22);delay_us(50); uart0GetByte(); //we don't care about this byte rprintf("%d\r\n", 255*uart0GetByte() + uart0GetByte()); uart0GetByte(); //we don't care about this byte } else if(read == 'L') { rprintf("%d\r\n", 255 - a2dConvert8bit(0)); } else if(read == 'R') { rprintf("%d\r\n", 255 - a2dConvert8bit(1)); } else if(read == 'D') { int buff='#'; uart2SendByte('#'); uart2SendByte('o'); uart2SendByte('s'); uart2SendByte('#'); uart2SendByte('f'); delay_ms(60); // Do not change this delay or you'll get in trouble! while(1) { buff = uart2GetByte(); if(buff==10 || buff==13 || buff==-1) { uart1SendByte('\r'); uart1SendByte('\n'); break; } uart1SendByte(buff); } while((buff=uart2GetByte()) != -1); } else if(read == 'E') { int buff='#'; uart3SendByte('#'); uart3SendByte('o'); uart3SendByte('s'); uart3SendByte('#'); uart3SendByte('f'); delay_ms(60); // Do not change this delay or you'll get in trouble! while(1) { buff = uart3GetByte(); if(buff==10 || buff==13 || buff==-1) { uart1SendByte('\r'); uart1SendByte('\n'); break; } uart1SendByte(buff); } while((buff=uart3GetByte()) != -1); } else if(read == 'M') { int buff='#'; uart2SendByte('#'); uart2SendByte('o'); uart2SendByte('t'); uart2SendByte('#'); uart2SendByte('f'); delay_ms(60); // Do not change this delay or you'll get in trouble! while(1) { buff = uart2GetByte(); if(buff==10 || buff==13 || buff==-1) { uart1SendByte('\r'); uart1SendByte('\n'); break; } uart1SendByte(buff); } while((buff=uart2GetByte()) != -1); } else if(read == 'N') { int buff='#'; uart3SendByte('#'); uart3SendByte('o'); uart3SendByte('t'); uart3SendByte('#'); uart3SendByte('f'); delay_ms(60); // Do not change this delay or you'll get in trouble! while(1) { buff = uart3GetByte(); if(buff==10 || buff==13 || buff==-1) { uart1SendByte('\r'); uart1SendByte('\n'); break; } uart1SendByte(buff); } while((buff=uart3GetByte()) != -1); } else if(read == 'P') { PORT_ON(PORTB,6); } else if(read == 'p') { PORT_OFF(PORTB,6); } else if(read == 'Q') { PORT_ON(PORTB,7); } else if(read == 'q') { PORT_OFF(PORTB,7); } else if(read == 'X') { PORT_ON(PORTB,4); } else if(read == 'x') { PORT_OFF(PORTB,4); } else if(read == 'Y') { PORT_ON(PORTB,5); } else if(read == 'y') { PORT_OFF(PORTB,5); } delay_us(50); } }