int beginConsoleInput() { int userchoice = 0; while ((userchoice = chooseInput() ) >= 0) { system("clear"); /*according to the result of chooseInput, execute next task*/ switch (userchoice) { case LED_EXAMPLES : exampleMenu(); break; case LED_MANUAL : // launch method for manual input; break; case LED_FILE : fileMenu(); break; case LED_NETWORK : // launch method for network input; break; default : fprintf(stderr,"Wrong user choice! closing program...\n"); return 1; } } return 0; }
void getADC() { if (conversion) { switch (flag) { case 0: ADC0 = ADC; break; case 1: ADC1 = ADC; break; case 2: ADC2 = ADC; break; case 3: ADC3 = ADC; break; case 4: ADC4 = ADC; break; case 5: ADC5 = ADC; break; case 6: ADC6 = ADC; break; case 7: ADC7 = ADC; break; } clear(ADCSRA, ADEN); //Enable/Start conversion clear(ADCSRA, ADSC); //^ clear(ADCSRA, ADATE); clear(ADCSRA, ADIF); if (flag >= 0 && flag < 8) { flag = (flag + 1) % 8; chooseInput(flag); } set(ADCSRA, ADATE); //Set trigger to free-running mode set(ADCSRA, ADEN); //Enable/Start conversion set(ADCSRA, ADSC); //^ set(ADCSRA, ADIF); //Enable reading results conversion = 0; } ADCarr[0] = ADC0; ADCarr[1] = ADC1; ADCarr[2] = ADC2; ADCarr[3] = ADC3; ADCarr[4] = ADC4; ADCarr[5] = ADC5; ADCarr[6] = ADC6; ADCarr[7] = ADC7; }
/*char isStuck() { m_wait(100); //localize(data); distfrombound = data[1] + ((float)BOUNDARYTHRESHOLD)*sin((data[2]+90.0)*3.14/180.0*-1.0); if (distfrombound > 60.0 || distfrombound < -60.0) { return 1; } m_usb_tx_int((int)distfrombound); m_usb_tx_char('\n'); } long loccounter = 0; float prevx = 0.0; float prevy = 0.0; */ int main(void) { //goal side clear(DDRB,1); clear(PORTB,1); if (check(PINB,1)) { goal = 1; } set(DDRB,0); set(PORTB,0); set(DDRD,5); set(DDRD,3); //wireless stuffs m_bus_init(); m_rf_open(CHANNEL, RXADDRESS, PACKET_LENGTH); int counter = 0; // //m_num_init(); int flag; m_clockdivide(0); m_disableJTAG(); //TIMER 0: For Controlling the solenoid // // set(TCCR0B, WGM02); // set(TCCR0A, WGM01); // set(TCCR0A, WGM01); // // set(TCCR0A, COM0B1); // clear(TCCR0A, COM0B0); // // set(TCCR0B, CS02); // set(TCCR0B, CS01); // set(TCCR0B, CS00); // set(DDRB,7); // OCR0A = 0xFF; // OCR0B = 0xff; // //TIMER 1: For Controlling the left wheel set(TCCR1B, WGM13); set(TCCR1B, WGM12); set(TCCR1A, WGM11); set(TCCR1A, WGM10); set(TCCR1A, COM1B1); clear(TCCR1A, COM1B0); clear(TCCR1B, CS12); clear(TCCR1B, CS11); set(TCCR1B, CS10); set(DDRB,6); OCR1A = 0xFFFF; OCR1B = 0; //TIMER 3: For Controlling the right wheel //up to ICR3, clear at OCR3A & set at rollover set(TCCR3B, WGM33); set(TCCR3B, WGM32); set(TCCR3A, WGM31); clear(TCCR3A, WGM30); set(TCCR3A, COM3A1); clear(TCCR3A, COM3A0); clear(TCCR3B, CS32); clear(TCCR3B, CS31); set(TCCR3B, CS30); ICR3 = 0xFFFF; OCR3A = 0; // //Set TCNT0 to go up to OCR0A // clear(TCCR0B, WGM02); // set (TCCR0A, WGM01); // clear(TCCR0A, WGM00); // // // Don't change pin upon hitting OCR0B // clear(TCCR0A, COM0A1); // clear(TCCR0A, COM0A0); // // // Set clock scale to /1024 // set(TCCR0B, CS02); // clear(TCCR0B, CS01); // set(TCCR0B, CS00); // // // Set Frequency of readings to 1/SAMPLERATE; dt = 1/SAMPLERATE // OCR0A = (unsigned int) ((float) F_CPU / 1024 / REPORTRATE); // OCR0B = 1; // Set up interrupt for reading values at sampling rate //Pin for controlling solenoid pulse set(DDRB,7); //Pins for controlling speed of left and right wheel set(DDRB,6); set(DDRC,6); //Pins for determining direction of wheels set(DDRB,2); set(DDRB,3); //Blue LED for Comm Test //set(DDRB,5); //ADC's sei(); //Set up interrupts set(ADCSRA, ADIE); clear(ADMUX, REFS1); //Voltage reference is AR pin (5V) clear(ADMUX, REFS0); //^ set(ADCSRA, ADPS2); //Set scale to /128 set(ADCSRA, ADPS1); //^ set(ADCSRA, ADPS0); //^ set(DIDR0, ADC0D); //Disable digital input for F0 set(DIDR0, ADC1D), set(DIDR0, ADC4D); set(DIDR0, ADC5D); set(DIDR0, ADC6D); set(DIDR0, ADC7D); set(DIDR2, ADC8D); set(DIDR2, ADC9D); set(ADCSRA, ADATE); //Set trigger to free-running mode chooseInput(0); set(ADCSRA, ADEN); //Enable/Start conversion set(ADCSRA, ADSC); //^ set(ADCSRA, ADIF); //Enable reading results //Limit Switch stuffs // clear(DDRB,0); //set to input, RIGHT LIMIT SWITCH // clear(DDRB,1); //set to input, LEFT LIMIT SWITCH // // clear(PORTB,0); //disable internal pull up resistor // clear(PORTB,1); //disable internal pull up resistor //int state; // state variable state = 0; //set state play = 0; long count = 0; if (state == 70) { m_usb_init(); // connect usb while(!m_usb_isconnected()); //wait for connection } //m_bus_init(); m_wii_open(); //m_usb_init(); m_red(ON); local_init(); localize(data); m_red(OFF); //set(TIMSK0, OCIE0A); while(1) { if (play) {m_red(ON);} else {m_red(OFF);} // m_wait(100); // m_red(TOGGLE); // localize(data); /*if (loccounter == LOCCOUNT) { if (sqrt((data[0]-prevx)*(data[0]-prevx)+(data[1]-prevy)-(data[1]-prevy)) < 1.0) { m_red(ON); reverse(); m_wait(100); state = 6; } else { m_red(OFF); state = 2; } prevx = data[0]; prevy = data[1]; loccounter = 0; }*/ //loccounter++; // localize(data); // locdata[1] = (char)data[0]; // locdata[2] = (char)data[1]; // toggle(PORTD,3); changedState = 0; angle_dif = 0; //Detect weather we have the puck getADC(); //if (!play) game_pause(); if (ADCarr[7] > 500) { //set(PORTD,5); //set(PORTD,5); iHaveThePuck = 1; m_green(ON); if (play && goal == A) state = 3; if (play && goal == B) state = 4; } else { //clear(PORTD,5); iHaveThePuck = 0; m_green(OFF); if (play) state = 2; } if(iHaveThePuck && state == 2) { //set(PORTB,5); //drive_to_goalA(); } else { //clear(PORTB,5); //if (state != 0) state = 2; } // if(isStuck()) { // //set(PORTD,5);u // } // else { // //clear(PORTD,5); // } //localize(data); // if (check(PINB,0)) { // set(PORTD,5); // state = 0x1A; // } else { // clear(PORTD,5); // state = 2; // } if (!play) state = buffer[0]; //switch states switch (state) { case -2: getADC(); if (ADCarr[0] > 500) { m_green(ON); } else {m_green(OFF)} break; case -1: //test Limit switches //m_green(ON); if (check(PINB,1)) { m_green(ON); } else if (check(PINB,0)) { m_red(ON); } else { m_red(OFF); m_green(OFF); } break; case 0: //drive_to_point2(-100,0); game_pause(); break; case 1: findPuck(); break; case 2: //m_red(ON); if (!iHaveThePuck) { if (play) drive_to_puck(); } break; case 3: if (play) drive_to_goalA(); break; case 4: if (play) drive_to_goalB(); break; case 5: getADC(); if (ADCarr[7] > 500) { //set(PORTD,5); } else { //clear(PORTD,5); } break; case 6: if (data[2] > 0) { rotate(RIGHT,1); } else { rotate(LEFT,1); } break; case 7: drive_to_point2(-50,0); break; case 0xA0: comm_test(); break; case 0xA1: play = 1; drive_to_puck(); break; case 0xA2: play = 0; game_pause(); break; case 0xA3: play = 0; game_pause(); break; case 0xA4: play = 0; game_pause(); break; case 0xA6: play = 0; game_pause(); break; case 0xA7: game_pause(); break; case 69: set(PORTB,2); set(PORTB,3); OCR1B = OCR1A; OCR3A = ICR3; break; case 70: reportADC(); break; default: game_pause(); break; } } }