Esempio n. 1
0
int main(void) { 

	m_disableJTAG();
	m_clockdivide(2);
	setup_pins();
	if (debug_fire|| RF_debug) {setupUSB();}
	setup_timer_1();
	setup_timer_3();
	m_bus_init();
	m_rf_open(chan,RX_add,p_length);

	int timer_3_cnt = 0;

	//sei();
	set_motors(0,0);
	while (1){
		if (check(TIFR3,OCF3A)){
			set(TIFR3, OCF3A);
			timer_3_cnt++;
			
			if(fired){
				since_fired++;
				if (debug_fire){
					m_usb_tx_string(" its been\t");
					m_usb_tx_int(since_fired);
					m_usb_tx_string(" milisec\n\r");
				}
			}
			if (since_fired>10){ 
				clear(PORTF,5);
				since_fired=0;
				fired = false;
				if (debug_fire){m_usb_tx_string(" its been 100 sec\n\r");}
			} 
			if ( fire && check(PINB,3)){ 
				fire=false; 
				fired=true; 
				since_fired=0;
				if (debug_fire){m_usb_tx_string(" portb 3 is high\n\r");}
			}
			
//			m_rf_open(chan,RX_add,p_length);
			m_green(TOGGLE);
			m_rf_init();
			if ( timer_3_cnt == 10){
				m_red(2);
				timer_3_cnt=0;
				m_rf_open(chan,RX_add,p_length);
			}
		}
		if(new){ turretDrive();
			if(RF_debug){ debug_rf(); }
			if ((receive_buffer[0] == 1 || receive_buffer[1]==1) && !fire){FIRE();}
		}
		//TODO fill in timer code for the firing mechanism
	}
}
Esempio n. 2
0
int main(void) {
  
  //initialize mBUS
  	m_bus_init();
  	m_usb_init();
 /* 	m_port_init(port_num);
  	m_port_set(port_num,DDRG,PIN_Comm);
	m_port_set(port_num,DDRG,PIN_Play);
	m_port_set(port_num,DDRG,PIN_Stop);

 */ 
	set(DDRB,1);
	set(DDRB,2);
	set(DDRB,3);
	
  	m_clockdivide(0);

  	m_rf_open(chan,address,p_len);
	sei();
  	while (1) {
		if (comm_cmd  == true)	{
			get_command();
		}		
  	}
}
Esempio n. 3
0
// --------------------------------------------------------------
// Initialize M2
// --------------------------------------------------------------
void init(void){
    
    // Initialize RED LED as on for the Initialization of M2
    m_red(ON);
    
    // Set the System Clock at 16MHz
    m_clockdivide(0);
    
    // Initialize the mBus
    m_bus_init();
    
    
    
    
     if (USB) {
     m_usb_init();                // Initialize USB and
     while(!m_usb_isconnected()); // wait for a connection
     }
    
    m_rf_open(CHANNEL,RXADDRESS,PACKET_LENGTH);
    
    m_red(OFF);
    // Turn off the RED LED when done
    
}
Esempio n. 4
0
int main(void) {
	m_clockdivide(0);
	setup_pins();
	if ( RF_debug) {setupUSB();}
	setup_timer_1();
	setup_timer_3();
	m_bus_init();
	m_rf_open(chan,RX_add,p_length);

	int timer_3_cnt = 0;

	//sei();
	set_motors(0,0);
	while (1){
		if (check(TIFR3,OCF3A)){
			set(TIFR3, OCF3A);
			timer_3_cnt++;
			if ( timer_3_cnt % 10 ==0 ) {
				timer_3_cnt=0;
				m_green(TOGGLE);
			}
			if ( timer_3_cnt == 1000){ timer_3_cnt=0;}
		}
		if(new){
			switch ( receive_buffer[11] ) {
				case Single_Joy: single_joystick(); break;
				case Double_Joy: double_joystick(); break;
				case Tank_Mode: tank_driving(); break;
				case Mario_Kart: Mario_Drive(); break;
				default : set_motors(0,0); m_green(2);
			}
			if(RF_debug){ debug_rf(); }
		}
	}
}
Esempio n. 5
0
void main(void)
{
    sei();
    OCR0A = 50;
    m_usb_init();
    m_clockdivide(0);
    init();
    init2();
    m_bus_init();
    m_rf_open(1,0x4E,3); 
 
     while(1)
    {
   
    m_usb_tx_string("    j value ");
    m_usb_tx_int(j);
    m_usb_tx_string("    Buffer value ");
    m_usb_tx_int(buffer[2]);
    m_usb_tx_string("\n");
         
    if(j == 30000)
    {
        j = 15000;
    }
    
    
            
}
}
Esempio n. 6
0
// Public functions
// Initialize peripheral devices
void robockey_init(void)
{
	m_clockdivide(0);	// 16 MHz
	sei();				// Enable global interrupt
	
	timer1_init(0.5);	// kHz
	timer3_init(50);	// Hz
	//timer0_init(1);	// kHz
	adc_init();
	mode_init();
	
	m_rf_open(CHANNEL,RXADDRESS,PACKET_LENGTH_RF);	// Enable wireless
	m_usb_init();									// Enable usb communication
	m_bus_init();									// Enable mbus
	m_wii_open();									// Enable wii camera
}
Esempio n. 7
0
int main() {
    m_red(ON);

    m_clockdivide(0);
    m_disableJTAG();
    m_bus_init();

    m_rf_open(1, 0xD1, 10);

    m_red(OFF);

    char buf[10] = {0x08, 10, 10, 0, 0, 0, 0, 0, 0, 0};
    while(1) {
        m_green(ON);
        m_rf_send(0xDA, buf, 10);
        buf[1] += 5;
        if(buf[1] >= 220) buf[1] = 10;
        m_green(OFF);

        m_wait(500);
    }
    return 0;
}
Esempio n. 8
0
int main(void){
	m_clockdivide(0);
	int data[9] = {0,0,0,0,0,0,0,0,0};
	m_green(ON);
	m_bus_init();
	m_usb_init();
	while(!m_usb_isconnected());
	m_green(OFF);
	m_imu_init(accel_scale, gyro_scale);
	m_rf_open(CHANNEL, RXADDRESS, PACKET_LENGTH);

	while(1) {
		while	( !newPacket );
		int i,a;
		while (i=0; i<PACKET_LENGTH , i+=2 ){
			a = *(int*)&packet[i*2+2];
			m_usb_tx_int(a);
			m_usb_tx_char('\t');
		}
		m_usb_tx_char('\n');
		m_usb_tx_char('\r');
	}
}
Esempio n. 9
0
int main(void)
{
	
    sei(); //enable interrupts
    m_clockdivide(0); // 16 MHz
    m_bus_init(); // enable mBUS
    m_rf_open(CHANNEL,RXADDRESS,PACKET_LENGTH); // configure mRF
    //set the clock pre-scaler to make the clock very fast
    //set the clock counter
    OCR3A = 2500;
    //configure pin 6 for output
    set(DDRB,6);
    
    
    
    //set the mode of  timer 1
    set(TCCR1B,WGM13);
    set(TCCR1B,WGM12);
    set(TCCR1A,WGM11);
    set(TCCR1A,WGM10);
    //clear OCR1B at rollover
    set(TCCR1A,COM1B1);
    //timer 1 interrupt handle
    set(TIMSK1,OCIE1A);
    //set the mode of  timer 3
    set(TCCR3B,WGM32);
    //timer 3 interrupt handle
    set(TIMSK3,OCIE3A);
    
    while(1)
    {
//        n=100;
//        i=0;
    }
	
}
Esempio n. 10
0
/* Initialize the Wireless System */
void wireless_enable(void)
{
	m_bus_init(); // Enable mBUS
	m_rf_open(CHANNEL,RXADDRESS_1,PACKET_LENGTH); // Configure mRF
}
Esempio n. 11
0
int main(void)
{
    m_disableJTAG();
    m_red(ON);
    m_clockdivide(0); // clock speed 16 MHz
    sei(); // enable global interrups
    
    
    // initialize m_bus communications
    // start wireless communications
    // and open wii camera
    m_bus_init();
    m_wii_open();
    m_usb_init();
    m_rf_open(CHANNEL, RXADDRESS, PACKET_LENGTH);
    
    setup();    // motor setup
    ADC_setup();
    
    int position[3];        // array for robot position
    int x_curr;
    int y_curr;
    int theta_curr;
    int i;
    int a;
    
    int x_pos[11];
    int y_pos[11];
    int theta[11];
    
    // main LOOP
    while(1)
    {
        
        //        for (i=0; i < 11; i++)
        //        {
        //            current_location(position);
        //            x_pos[i] = position[0];
        //            y_pos[i] = position[1];
        //            theta[i] = position[2];
        //        }
        
        //        // calculate positions
        //        filter(x_pos,y_pos,theta);
        //        x_curr = x_pos[11];
        //        y_curr = y_pos[11];
        //        theta_curr = theta[11];
        
        // Read in values
        //        ADC0();
        //        int D6 = ADC;
        //
        //        ADC1();
        //        int F1 = ADC;
        //
        //        ADC4();
        //        int F4 = ADC;
        //
        //        ADC5();
        //        int F5 = ADC;
        //
        //        ADC6();
        //        int F6 = ADC;
        //
        //        ADC7();
        //        int F7 = ADC;
        //
        //        ADC8();
        //        int D7 = ADC;
        
        //
        follow_puck();
        m_green(TOGGLE);
        
        
        //        // usb prinitng
        //        m_usb_tx_string("D6: ");
        //        m_usb_tx_int(D6);
        //        m_usb_tx_string("   ");
        //        m_usb_tx_string("F1: ");
        //        m_usb_tx_int(F1);
        //        m_usb_tx_string("   ");
        //        m_usb_tx_string("F4: ");
        //        m_usb_tx_int(F4);
        //        m_usb_tx_string("   ");
        //        m_usb_tx_string("F5: ");
        //        m_usb_tx_int(F5);
        //        m_usb_tx_string("   ");
        //        m_usb_tx_string("F6: ");
        //        m_usb_tx_int(F6);
        //        m_usb_tx_string("   ");
        //        m_usb_tx_string("F7: ");
        //        m_usb_tx_int(F7);
        //        m_usb_tx_string("   ");
        //        m_usb_tx_string("D7: ");
        //        m_usb_tx_int(D7);
        //        m_usb_tx_string("   ");
        //        m_usb_tx_string("\n");
        
        a = state(buffer1);
        a = 2;
        
        // COMM TEST
        if (a == 1)
        {
            // ADD CODE
        }
        
        // PLAY
        if (a == 2) {
        }
        
        // PAUSE, HALF TIME, GAME OVER
        if (a == 5 || a == 6 || a == 7)
        {
            // STOP ROBOT
            turn_robot(0,0,0,0);
            m_red(OFF);
        }
        else
        {}
        
    }
}
Esempio n. 12
0
//c7 red
/// e6 blue
int main(void)
{
    m_disableJTAG();    // Turn off JTAG
    //m_red(ON);          // check code runs
    m_clockdivide(0);   // clock speed 16 MHz
    m_bus_init();       // initialize m_bus communications
    m_usb_init();       // USB COM
    setup();            // motor setup
    ADC_setup();        // setup anlog conversions

    // start wireless communications
    m_rf_open(CHANNEL, RXADDRESS, PACKET_LENGTH);
    m_wii_open();       // and open wii camera
    sei();              // enable global interrups

    set(DDRC,7);
    set(DDRB,7);
    
    int position[3];        // array for robot position
    int x_curr;             // current x position
    int y_curr;             // current y position
    int theta_curr;         // current angle
    int i;
    int a;
    
    // x, y and theta values
    int x_pos[11];
    int y_pos[11];
    int theta[11];
    
    char send[10];
    
    int side;
    int x1;
    // calculate initial positions

    int status;
//    int x;
//    int y;
//    int theta1;
    int hasPuck = 0;
    
    
    // main LOOP
    while(1)
    {
        if (flag)
        {
            m_rf_read(buffer1, PACKET_LENGTH);
            testing = (unsigned char)buffer1[0];
            a = state(testing);
//            m_rf_read(buffer, PACKET_LENGTH);

//            x = (unsigned char)buffer[0];
//            y = (unsigned char)buffer[1];
//            theta1 = (unsigned char)buffer[2];
            
            if (flag1){
            for(i = 0; i<10; i++)
            {
                current_location(position);
                x1 = position[0];
            }
            // figure out what side robot starts on
            if (x1 > 0)
            {
                side = -1;
            }
            else
            {
                side = 1;
            }
            }

            flag = 0;
        }
      //  m_rf_open(CHANNEL, RXADDRESS, PACKET_LENGTH);

        // store 10 most recent position readings
        for (i=0; i < 11; i++)
        {
            current_location(position);
            x_pos[i] = position[0];
            y_pos[i] = position[1];
            theta[i] = position[2];
        }
        
        // filter position
        filter(x_pos,y_pos,theta);
        
        // current position
        x_curr = x_pos[11];
        y_curr = y_pos[11];
        theta_curr = theta[11];
        
        if (x_curr > 0)
        {
            m_red(ON);
        }
        else if (x_curr<0)
        {
            m_red(OFF);
        }
        
        if (y_curr > 0)
        {
            m_green(ON);
        }
        else if (y_curr<0)
        {
            m_green(OFF);
        }
        
//        send[0] = x_curr;//(char) x_curr;
//        send[1] = y_curr;//(char) y_curr;
//        send[2] = theta_curr;//(char) theta_curr;
//        status = m_rf_send(TXaddress, send, PACKET_LENGTH);
//        if (status){m_red(TOGGLE)};

//        m_usb_tx_string("X Pos: ");
//        m_usb_tx_int(x);
//        m_usb_tx_string("   ");
//        m_usb_tx_string("Y POSITION: ");
//        m_usb_tx_int(y);
//        m_usb_tx_string("   ");
//        m_usb_tx_string("ANGLE: ");
//        m_usb_tx_int(theta1);
//        m_usb_tx_string("   ");
//        m_usb_tx_string("\n");
        a = 2;

        // COMM TEST
        if (a == 1)
        {
            // ADD CODE
           // m_red(ON);
            if (side == 1)
            {
                set(PORTC,7);
            }
            else {
                set(PORTB,7);
            }
        }
        
        // PLAY GAME
        else if (a == 2) {
           // m_red(OFF);

            //int hasPuck = follow_puck();
        //hasPuck = 1;
           // goalie(x_curr, y_curr, theta_curr, side);

            if(hasPuck)
            {
                //score_goal(x_curr, y_curr, theta_curr, side);
                //turn_robot(0,0,0,0);

            }
        }
        
        // GOAL R
        else if (a == 3)
        {
            //side = 1;
            //m_green(TOGGLE);
        }
        
        // GOAL B
        else if ( a == 4)
        {
            //side = -1;
            //m_green(TOGGLE);
        }
        else if(a == 5)
        {
            turn_robot(0,0,0,0);
          //  m_red(ON);
        }
        
        // PAUSE, HALF TIME, GAME OVER
        if (a == 6 || a == 7)
        {
            // STOP ROBOT
            turn_robot(0,0,0,0);
            m_red(ON);
        }
        else
        {}
//        // COMM PROTOCOL INSTRUCTIONS
//        m_usb_tx_string("STATE: ");
//        m_usb_tx_int(a);
//        m_usb_tx_string("   ");
//        m_usb_tx_string("FLAG: ");
//        m_usb_tx_int(flag);
//        m_usb_tx_string("   ");
//        m_usb_tx_string("TESTING: ");
//        m_usb_tx_int(testing);
//        m_usb_tx_string("   ");
//        m_usb_tx_string("\n");
    }
}
Esempio n. 13
0
int main(void) {
	m_clockdivide(0);

	gen_setup();

	unsigned int wii_data[12] = {0};

	sei();
	m_rf_open(chan,rf_address,p_len);
	m_wii_read(wii_data);
	localize( wii_data , position, b_angle); // <--- takes in wii_data and updates position in place to house {x,y,angle} of robot 

	int enemy_goalX; 
	int enemy_goalY = 0;
	int own_goalX;
	int own_goalY = 0;
	int goalXPosition = 120;

	if ((int)(position[0])<0){ 	enemy_goalX = goalXPosition; 
		own_goalX = -goalXPosition;}

	else { 			enemy_goalX = -goalXPosition; 
		own_goalX = goalXPosition;}


	find_puck();
	set_motors(800,800);
	//bool  toggle = true;

	int lastX = 0;
	int lastY = 0;
	int lastAng = 0;

	while (1) {
		//find angle to own Goal
		rxOG = own_goalX - (int)position[0];
		ryOG = own_goalY - (int)position[1];
		psiOG = (int)(b_angle[0] *  RAD_DEG_RATIO) - 90; 
		if (psiOG<-180) { psiOG +=360; }
		if (psiOG>180) { psiOG -=360; }

		//error in angle, factoring in rotational velocity should be taken care of by kalman filter
		phi_robotOG = (int)(atan2(ryOG,rxOG) * RAD_DEG_RATIO); // compute angle from robot to target (in global coordinates)	
		theta_angleOG = (psiOG-phi_robotOG);
		//make sure the data within -180 to 180 degrees for ease of calculation
		if (theta_angleOG>180) { theta_angleOG-=360; }
		if (theta_angleOG<=-180) { theta_angleOG+=360;}

		/*if (debug_goto) {
		  m_usb_tx_string("\n\r");
		  m_usb_tx_int(theta_angleOG);
		// m_usb_tx_string("\t");
		//m_usb_tx_int(OCR1B);
		m_usb_tx_string("\n\r"); 
		}*/

		last_Super_state = Super_state;
		if (comm_cmd  == true)	{ get_command(); }
		if (check(TIFR3,OCF3A)){		 ///// timer 3 runs at 100Hz
			set(TIFR3,OCF3A);			
			if (timer_3_cnt == 1000){ timer_3_cnt = 0 ;   } else{ timer_3_cnt++;} // timer_3_cnt counts 10 seconds and then rolls over

			m_wii_read(wii_data);
			localize( wii_data , position, b_angle); // <--- takes in wii_data and updates position in place to house {x,y,angle} of robot 
			m_rf_open(chan,rf_address,p_len);


			find_puck();
			if (get_unstuck && timer_3_cnt%100 == 0){
				if ( (( lastX < position[0]+pos_change) && lastX > position[0]-pos_change)   
						&& ( lastY < position[1]+pos_change && (lastY > position[1]-pos_change)  ) ){
					//	&& ( ((int)lastAng < (int)b_angle*100 +20) && ((int)lastAng > (int)b_angle*100 - 20) )){
					Super_state = New_Loc_super;
					set_motors(-700,-700);
					m_wait(200);
					set_motors(0,0);
				}
				lastX = (int)position[0]; 
				lastY = (int)position[1];
				lastAng = (int)b_angle*100.0;
				}

				switch (Super_state){			
					case PT_super: 
						break;
					case ADC_super: 
						ADC_go_to_puck();
						break;
					case To_Goal_super: 
						drivePuck(enemy_goalX,  enemy_goalY ,position, b_angle[0]);
						break; 
					case New_Loc_super:	 
						break;
				}
				if (Super_state != To_Goal_super) keepSpin = 0;

				if ( Matlab_pos_tracking ){ matlab_output( position, b_angle , wii_data ); }
			}
		}
	}
int main(void)
{	m_clockdivide(0);
	m_bus_init();
	m_usb_init();
	m_rf_open(CHANNEL,RXADDRESS,LENGTH);
	m_wii_open();
	sei();
	
	OCR1A = 31999;
	OCR1B = 0;
	OCR1C = 0;
	
	m_disableJTAG();

	
	clear(ADMUX,REFS1); // Setting the reference voltage for the ADC to Vcc
	set(ADMUX,REFS0); // ^
	
	set(ADCSRA,ADPS2); // set ADC prescaler to 128
	set(ADCSRA,ADPS1); // ^
	set(ADCSRA,ADPS0); //^
	
	set(DIDR0,ADC0D); // disable digital on F0 (ADC0)
	set(DIDR0,ADC1D); // disable digital to F1
	set(DIDR0,ADC4D); //disable digital to F4
	set(DIDR0,ADC5D); //disable digital to F5
	set(DIDR0,ADC6D); //disable digital to F6
	set(DIDR0,ADC7D); //disable digital to F7
	
	set(ADMUX,REFS0); // set ADC reference to Vcc
	
	set(ADCSRA,ADIE); //Enables interrups when conversion finishes
	//	clear(ADMUX,MUX2); // This is the front right. Range 50-1023. Port is F1
	//	set(ADMUX,MUX0);
	
	set(TCCR1B,WGM13);          // Mode 15: up to OCR1A, PWM, single slope
	set(TCCR1B,WGM12);          // ^
	set(TCCR1A,WGM11);          // ^
	set(TCCR1A,WGM10);          // ^
	
	set(TCCR1A,COM1B1);			//clear at OCR1B, set at rollover
	clear(TCCR1A,COM1B0);			// ^
	
	set(TCCR1A,COM1C1); // clear at OCR1C, set at rollover
	clear(TCCR1A,COM1C0);
	
	clear(TCCR1B,CS12);       // set clock pre-scaler to /1
	clear(TCCR1B,CS11);          // ^
	set(TCCR1B,CS10);         // ^
	
	
	m_port_init(0x20); // Initializes the port expander and sets all of the G pins as outputs
	m_port_set(0x20, DDRG,0);	
	m_port_set(0x20, DDRG,1);
	m_port_set(0x20, DDRG,2);
	m_port_set(0x20, DDRG,3);
	m_port_set(0x20, DDRG,4);
	m_port_set(0x20, DDRG,5);
	m_port_set(0x20, DDRG,6);
	m_port_set(0x20, DDRG,7);
	
	clearDisplay();
	// Set motor output ports
	set(DDRB,6);
	set(DDRB,7);
	set(DDRB,5);
	set(DDRB,3);
	
	// Turn off motors
	clear(PORTB,6);
	clear(PORTB,7);
	
	set(ADCSRA,ADEN); // enable ADC subsystem
	set(ADCSRA,ADSC); // start ADC conversion
	
	
	while(1){
		switch(state){
			case COMM:
				commtestf();
				break;
			case PAUSE:
				pausef();
				break;
			case PLAY:
				playf();
				localizef();
				defendf();
				//puckfindf();
				break;
			case GOTOGOAL:
				gotogoalf();
			case HALFTIME:
				pausef();
				break;
			case GOALA:
				pausef();
				break;
			case GOALB:
				pausef();
				break;
			case GAMEOVER:
				pausef();
				break;
			default:
				break;
		}
		defendf();
		m_usb_tx_int(photo1); 
		m_usb_tx_string(" ");
		m_usb_tx_int(photo2);
		m_usb_tx_string(" ");
		m_usb_tx_int(photo3);
		m_usb_tx_string(" ");
		m_usb_tx_int(photo4);
		m_usb_tx_string(" ");
		m_usb_tx_int(photo5);
		m_usb_tx_string("\n");
		
	}
}	
Esempio n. 15
0
int main(void) {
	m_clockdivide(0);
	m_bus_init();
	setup_timer_3();
	m_rf_open(chan,TX_add,p_length);
	setup_pins();
	if (debug_ADC){ setupUSB();}

	long VLeftSum=0;
	long HLeftSum=0;
	long VRightSum=0;
	long HRightSum=0;
//	int  VLeftSum=0;
//	int  HLeftSum=0;
//	int  VRightSum=0;
//	int  HRightSum=0;


	int cnt_RB = 0;
	int cnt_LB = 0;


	/////////////////////////////////////////////////////////////////////////////////
	//
	// give me a half second to go from switching on the m2 to holding the config buttons.
	//
	//////////////////////////////////////////////////////////////////////////////////

	m_green(1);
	int timer_3_cnt=0;
	int Calibration = 50;
	for (timer_3_cnt=0 ; timer_3_cnt< Calibration ; ){
		if(check(TIFR3,OCF3A)){		 ///// timer 3 runs at 100Hz
			set(TIFR3,OCF3A );
			timer_3_cnt++;
		}
	}

	m_red(ON);

	for (timer_3_cnt=0 ; timer_3_cnt<Calibration ; ){
		if(check(TIFR3,OCF3A)){		 ///// timer 3 runs at 100Hz
			set(TIFR3,OCF3A );
			timer_3_cnt++;


		update_ADC(0,0,0,0);

		check_buttons();
		if (send_buffer[1]==1){ cnt_RB++ ;}
		if (send_buffer[0]==1){ cnt_LB++ ;}

		VLeftSum  += *(int*)(&send_buffer[2]);
		HLeftSum  += *(int*)(&send_buffer[6]);
		VRightSum += *(int*)(&send_buffer[4]);
		HRightSum += *(int*)(&send_buffer[8]);

//		if (debug_ADC){while(!m_usb_rx_available()); m_usb_rx_flush();}
		if (debug_ADC){debug_ADC_sums( timer_3_cnt,  VRightSum, VLeftSum,  HLeftSum,  HRightSum );}
		}
	}

	// if (debug_ADC){while(!m_usb_rx_available()); m_usb_rx_flush();}

	int VLeftOffset = (int)(VLeftSum/Calibration);
	int HLeftOffset = (int)(HLeftSum/Calibration);
	int VRightOffset= (int)(VRightSum/Calibration);
	int HRightOffset= (int)(HRightSum/Calibration);

	int VLeftOffset = 512;//(int)(VLeftSum/Calibration);
	int HLeftOffset = 512;//(int)(HLeftSum/Calibration);
	int VRightOffset= 512;//(int)(VRightSum/Calibration);
	int HRightOffset= 512;//(int)(HRightSum/Calibration);

	bool L_bump  = ( cnt_LB > Calibration/2 );
	bool R_bump = ( cnt_RB > Calibration/2 );


	if (debug_ADC){while(!m_usb_rx_available()); m_usb_rx_flush();}
	if (debug_ADC){debug_ADC_vals( Calibration,   VRightSum, VLeftSum,  HLeftSum,  HRightSum );}
	m_green(0);
	m_red(OFF);

	set_drive_mode( L_bump , R_bump );

	while (1){
		check_buttons();
		update_ADC( VLeftOffset,HLeftOffset,VRightOffset,HRightOffset );
		TX_comm();
		if (debug_ADC){deal_with_new();}
	}
}
Esempio n. 16
0
void m_rf_init(){
	m_rf_open(CHANNEL,RXADDRESS,PACKET_LENGTH);
}
Esempio n. 17
0
int main(void)
{
    m_clockdivide(0); // clock speed 16 MHz
    sei(); // enable global interrups
    int derivative = 0;
    
    // initialize m_bus communications
    // start wireless communications
    // and open wii camera
    m_bus_init();
    m_wii_open();
    m_usb_init();
    m_rf_open(CHANNEL, RXADDRESS, PACKET_LENGTH);
    
    setup();    // motor setup
    
    int side;   // integer to figure out which side we're on
    
    while (1)
    {
        // USB print testing
        m_usb_tx_string("testing: ");
        m_usb_tx_int(testing);
        m_usb_tx_string("   ");
        m_usb_tx_string("\n");
        
        // continue code if ply command is issued
        if ((unsigned char)buffer1[0] == 0xA1){
            break;
        }
    }
    
    // turn red light on if play command is issued
    m_red(ON);
    
    // wait 10 seconds to let M_Wii start recording stars
    m_wait(1000);
    
    int position[3];        // array for robot position
    int x1;                 // x position
    int i;
    
    // calculate initial positions
    for(i = 0; i<1000; i++)
    {
        current_location(position);
        x1 = position[0];
    }
    
    // figure out what side robot starts on
    if (x1 > 0)
    {
        side = -1;
    }
    else
    {
        side = 1;
    }
    
    // array to hold constants
    float constants[4];
    //goal();
    
    // main LOOP
    while(1)
    {
        // calculate positions
        int b = current_location(position);
        int x1 = position[0];
        int y1 = position[1];
        int angle = position[2];
        
        // usb prinitng
        m_usb_tx_string("LOCALIZATION: ");
        m_usb_tx_int(b);
        m_usb_tx_string("   ");
        m_usb_tx_string("X Position: ");
        m_usb_tx_int(x1);
        m_usb_tx_string("   ");
        m_usb_tx_string("Y Position: ");
        m_usb_tx_int(y1);
        m_usb_tx_string("   ");
        m_usb_tx_string("Angle: ");
        m_usb_tx_int(angle);
        m_usb_tx_string("   ");
        m_usb_tx_string("\n");
        
        // get constants
        if (side>0){
            get_constants1(constants, -115, 0, x1, y1, angle, side, derivative);
        }
        else
        {
            get_constants1(constants, 115, 0, x1, y1, angle, side, derivative);
        }
        
        
        float p = constants[0]; // speed constant
        int d = (int)constants[1]; // direction constant
        float j = constants[2]; // how much turn constant
        derivative = constants[3];
        
        turn_robot( d, 1, .9, j); // DRIVE THE BOT
    }
}
Esempio n. 18
0
/*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;
        }
        
    }
}
Esempio n. 19
0
int main(void)
{
    init();                             // Run Timer Initializacion Code
    m_bus_init();
	m_usb_init();                       // Initialize the USB connection.
    m_rf_open(1, 0x15, 3);
    set(DDRB, 0);
    set(DDRB, 1);
    set(DDRB, 2);
    set(DDRB, 3);
    set(DDRB, 7);
    set(DDRD, 3);
    set(DDRD, 4);
    set(DDRD, 5);
    set(DDRD, 6);
    set(DDRD, 7);
    
    //while(!m_usb_isconnected());      // wait for a connection

    while(1){
        
        if(m_usb_rx_available())
        {
            input = m_usb_rx_char();
            //m_usb_tx_int(input);
            m_usb_tx_string(" ");
            if (input == 119) {
                OCR3A = OCR3A - 1;
            }
            //if (input == 115) {
            //    OCR3A = OCR3A + 1;
            //}
            //if (input == 105) {
            //    OCR0A = OCR0A - 1;
            //}
            if (input == 107) {
                OCR0A = OCR0A + 1;
            }
            if (input == 105) {
                m_red(TOGGLE);
                //des_freq = 100;
                OCR3A = (float)62500/des_freq;
                duration = 100;
                flag_2 = 1;
                flag = 0;
                ;
            }
            if (input == 115) {
                des_freq += 10;
            }
            m_usb_tx_int(des_freq);
        }
        
        if (flag) {
            m_red(TOGGLE);
            m_rf_read(commands,3);
            des_freq = *(int*)&commands[0];
            des_freq /= 10;
            // Measured: 130 is 60 Hz. 20 is 370 Hz. THIS IS INT.
            OCR3A = 130 - (float)(des_freq - 60)/(float)2.8;
            duration = commands[2];
            flag_2 = 1;
            flag = 0;
        }
        
        // Max Sound duration is 255 centiseconds, which is 2.55 sec.
        if (flag_2) {
            // TCNT0 = 0;
            conta = 0;
            m_green(ON);
            set(DDRB, 6);
            clear(PORTD, 4);
            flag_2 = 0;
        }
        if (conta == (4*duration)) {
            m_green(OFF);
            clear(DDRB, 6);
            clear(PORTB, 6);
            set(PORTD, 4);
        }
        
        //if (TCNT0 == OCR0A) {
        if (check(TIFR0, OCF0A)){
            conta += 1;
            set(TIFR0,OCF0A);
        }
        
        /*
        if (check(ADCSRA,ADIF)) {
            set(ADCSRA, ADIF);
            if (ADC>100) {
                set(PORTB, 0);
            }
            else{
                clear(PORTB, 0);
            }
            if (ADC>200) {
                set(PORTB, 1);
            }
            else{
                clear(PORTB, 1);
            }
            if (ADC>300) {
                set(PORTB, 2);
            }
            else{
                clear(PORTB, 2);
            }
            if (ADC>400) {
                set(PORTB, 3);
            }
            else{
                clear(PORTB, 3);
            }
            if (ADC>500) {
                set(PORTB, 7);
            }
            else{
                clear(PORTB, 7);
            }
            if (ADC>600) {
                set(PORTD, 3);
            }
            else{
                clear(PORTD, 3);
            }
            if (ADC>700) {
                set(PORTD, 4);
            }
            else{
                clear(PORTD, 4);
            }
            if (ADC>800) {
                set(PORTD, 5);
            }
            else{
                clear(PORTD, 5);
            }
            if (ADC>900) {
                set(PORTD, 6);
            }
            else{
                clear(PORTD, 6);
            }
            if (ADC>1000) {
                set(PORTD, 7);
            }
            else{
                clear(PORTD, 7);
            }
        }
        */
        
        //m_usb_tx_string("\r");      // Carriage Return
        //m_usb_tx_uint(ADC);
        
        //m_usb_tx_string("\r");      // Carriage Return
        //m_usb_tx_hex(commands[0]);
        //m_usb_tx_uint(des_freq);
        //m_usb_tx_string(" ");
        //m_usb_tx_char(commands[2]);
        //m_usb_tx_string(" OCR3A: ");
        //m_usb_tx_uint(OCR3A);
        //m_usb_tx_string("             ");
        
        // Measured: 0x81 (129, we can use 130) is 60 Hz. 46 (70) is 110 Hz.
        // I get values from 60 to 110
        
        
        
        // Do only every time the Timer Overflows
        //while(!check(TIFR3, OCF3A));
        if (check(TIFR3, OCF3A)) {
            if(indx<255){
                indx += 1;
            }
            else{
                indx = 0;
            }
            //Define new value for Duty Cycle According to Sine Table
            OCR1B = sinewave_data[indx];
            set(TIFR3, OCF3A);              //Clear Flag
        }
        
        

        
        // Display information for the SCREEN.
       /*
        m_usb_tx_string("ADC Input Value: ");      //
        m_usb_tx_uint(ADC);         //transmit over USB
        m_usb_tx_string("    DIP Switch Inputs:");      //
        //m_usb_tx_uint(check(PIND,4));
        //m_usb_tx_string("  "); m_usb_tx_uint(check(PIND,5));
        //m_usb_tx_string("  "); m_usb_tx_uint(check(PIND,6));
        //m_usb_tx_string("  "); m_usb_tx_uint(check(PIND,7));
        m_usb_tx_string(" "); m_usb_tx_uint((check(PIND, 4) * 1 + check(PIND, 5) * 2 + check(PIND, 6) * 4 + check(PIND, 7) * 8 + 1));
        m_usb_tx_string("   Duty Cycle "); m_usb_tx_uint(((OCR1A - OCR1B)/OCR1A)*100);
        m_usb_tx_string("\r");      // Carriage Return
        //m_usb_tx_string("\n");      // Enter
        //m_usb_tx_string("\f");      // Form Feed
        //m_usb_tx_string("\f");      // Tab
        //m_usb_tx_string("\v");      // Vertical Tab
        
*/
    }
    return 0;                       /* never reached */
}
Esempio n. 20
0
int main(void)
{
    m_clockdivide(0);
    m_bus_init();
    m_usb_init();
	m_rf_open(CHANNEL, RXADD, PACKET_LENGTH);  // Configure the RF module to listen on CHANNEL for RXADD
	sei(); //Enable global interrupt
	
	unsigned int star[12]={0};
    int i, j, k =0;
    int maxD, minD, dist, p, q, r, s=0; // p.q.r.s -> co-ordinates between which the midpoint is to be found(x2, x4)[p.q.r.s=x2.y2.x4.y4]
    int cx, cy, cxx, cyy, cX, cY=0; 
	int count=0;
    int pmax, qmax, rmax, smax, pmin, qmin, rmin, smin, pp, qq, rr, ss=0;
    float theta=0;
    float to_angle, to_angle_1, to_angle_2, to_angle_c=0;
    float temp1=0;
	float LESS2=0;
	float temp_theta=0;
	int trans_c,trans_fl,trans_fr,trans_bl,trans_br=0; 
	int puck_in_hand, puck_in_line=0;
	
    set(DDRD,7);    // Set as Comm test Led output
	set(DDRD,6);    // Set as PAUSE Led output
    /*
	//ADC initialization
	clear(ADMUX,REFS1); //Set voltage reference for ADC as Vcc
	set(ADMUX,REFS0);   
	
	set(ADCSRA,ADPS2); //Prescale ADC to 250 Khz
	set(ADCSRA,ADPS1);
	clear(ADCSRA,ADPS0);
	 
	set(DIDR0,ADC1D); //Disabling Digital inputs for ADC pins
	set(DIDR0,ADC4D);
	set(DIDR0,ADC5D);
	set(DIDR0,ADC6D);
	set(DIDR0,ADC7D);
	*/	
    while  (m_wii_open()!=1){}
    
    //Timer Initialization
    set(DDRB, 6); // Set (Reg B. Bit 6) as timer output. PWM 1
    set(DDRB, 7); // Set (Reg B. Bit 7) as timer output. PWM 2
   
    set(TCCR1B, WGM13); // Set Timer 1 Mode 15 (UP to OCR1A, PWM mode)
    set(TCCR1B, WGM12);
    set(TCCR1A, WGM11);
    set(TCCR1A, WGM10);
   
    set(TCCR1A, COM1C1); // Clear at OCR1B. Set at rollover |Timer B.6
    clear(TCCR1A, COM1C0);

    set(TCCR1A, COM1B1); // Clear at OCR1C. Set at rollover |Timer B.7
    clear(TCCR1A, COM1B0);

    OCR1A=1600; //1Khz Motor
	OCR1B=0;
	OCR1C=0;
	
	clear(TCCR1B, CS12); // Initialize timer & Prescale /1 (Start Timer)
    clear(TCCR1B, CS11);
    set(TCCR1B, CS10);
   
    while(1)
	{

		while(!play) //PAUSE
		{
			m_green(OFF); 
			set(PORTD, 6); //Switch on RED LED
			OCR1B=0; //Stop Motor
			OCR1C=0; //Stop Motor
			clear(PORTB,6); //Stop Motor
			clear(PORTB,7); //Stop Motor
		}
		/*	
		//Trans_C
		clear(ADCSRA,ADEN); // Disable ADC system to change MUX bit
		
		clear(ADCSRB,MUX5); //Set MUX bit to F1
		clear(ADMUX,MUX2);
		clear(ADMUX,MUX1);
		set(ADMUX,MUX0);
		 
		set(ADCSRA,ADEN); //Enable the system
		set(ADCSRA,ADSC); //Start the conversion
	
		while(!check(ADCSRA,ADIF)){} //ADC conversion ongoing
		set(ADCSRA,ADIF);// Clear the flag
		 
		trans_c=ADC;
	
		//Trans_F_LEFT
		clear(ADCSRA,ADEN); // Disable ADC system to change MUX bit
		 
		clear(ADCSRB,MUX5); //set MUX bit to F4
		set(ADMUX,MUX2);
		clear(ADMUX,MUX1);
		clear(ADMUX,MUX0);
		 
		set(ADCSRA,ADEN); //Enable the system
		set(ADCSRA,ADSC); //Start the conversion
		 
		while(!check(ADCSRA,ADIF)){} //ADC conversion ongoing
		set(ADCSRA,ADIF);// Clear the flag
		 
		trans_fl=ADC;
		 
		//TRANS_F_RIGHT
		clear(ADCSRA,ADEN); // Disable ADC system to change MUX bit
		 
		clear(ADCSRB,MUX5); //Set MUX bit to F5
		set(ADMUX,MUX2);
		clear(ADMUX,MUX1);
		set(ADMUX,MUX0);
		 
		set(ADCSRA,ADEN); //Enable the system
		set(ADCSRA,ADSC); //Start the conversion
		 
		while(!check(ADCSRA,ADIF)){} //ADC conversion ongoing
		set(ADCSRA,ADIF);// Clear the flag
		 
		trans_fr=ADC;
	
		//TRANS_B_LEFT
		clear(ADCSRA,ADEN); // Disable ADC system to change MUX bit
		 
		clear(ADCSRB,MUX5); //Set MUX bit to F6
		set(ADMUX,MUX2);
		set(ADMUX,MUX1);
		clear(ADMUX,MUX0);
		 		 
		set(ADCSRA,ADEN); //Enable the system
		set(ADCSRA,ADSC); //Start the conversion
	
		while(!check(ADCSRA,ADIF)){} //ADC conversion ongoing
		set(ADCSRA,ADIF);// Clear the flag
		 
		trans_bl=ADC;
		 
		//TRANS_B_RIGHT
		clear(ADCSRA,ADEN); // Disable ADC system to change MUX bit
		 
		clear(ADCSRB,MUX5); //set MUX bit to F7
		set(ADMUX,MUX2);
		set(ADMUX,MUX1);
		set(ADMUX,MUX0);
		 
		set(ADCSRA,ADEN); //Enable the system
		set(ADCSRA,ADSC); //Start the conversion
	
		while(!check(ADCSRA,ADIF)){} //ADC conversion ongoing
		set(ADCSRA,ADIF);// Clear the flag
		 
		trans_br=ADC;
		*/	
	
		clear(PORTD,6); //Switch OFF RED LED
	
		cli();
		m_wii_read(star);
		sei();
		count=0;
		//Localization of bot w.r.t constellation
		//Reading no. of detectable stars
		for(k=0;k<=3;++k)
		{ 
			if((int)star[3*k]==1023 && (int)star[3*k+1]==1023)
				{count++;} //Count = No. of stars lost
		}
		if(count<=1) //Enter this only if <= 1 star has been lost. 
		{   
			m_red(OFF);    
			//Finding max and min distance
			pmax=0; qmax=3; rmax=1; smax=4;  //p.r=(x1. y1)
			pmin=0; qmin=3; rmin=1; smin=4;  //q.s=(x2. y2)
			maxD=pow(((int)star[pmax]-(int)star[qmax]), 2) + pow(((int)star[rmax]-(int)star[smax]), 2);
			minD=maxD;
			for (i=0; i<3; i++)
			{
				for(j=i+1; j<=3; j++)
				{
					dist=pow(((int)star[i*3]-(int)star[j*3]), 2) + pow(((int)star[i*3+1]-(int)star[j*3+1]), 2);
					if(dist>maxD)
					{
						maxD=dist;
						pmax=i*3; qmax=j*3; rmax=pmax+1; smax=qmax+1;
					}
					if(dist<minD)
					{
						minD=dist;
						pmin=i*3; qmin=j*3; rmin=pmin+1; smin=qmin+1  ;
					}
				}   //for j
			}//for i

			//Check for intersection between the max & min points to find the top x.y
			if((int)star[pmax]==(int)star[pmin] || (int)star[pmax]==(int)star[qmin])
			{
				//m_red(OFF);
				p=pmax; r=rmax; q=qmax; s=smax;
				pp=p; rr=r; qq=q; ss=s;
			}
			else if ((int)star[qmax]==(int)star[pmin] || (int)star[qmax]==(int)star[qmin])
			{
				//m_red(OFF);            
				p=qmax; r=smax; q=pmax; s=rmax; 
				pp=p; rr=r; qq=q; ss=s;
			}
			else //Retain the previous co-ordinates if it can't find intersection
			{
			   p=pp; q=qq; r=rr; s=ss;
			   //m_red(ON);
			}
        
			//Center Point
			cx=((int)star[p]+(int)star[q])/2-512;
			cy=((int)star[r]+(int)star[s])/2-384;
			//Theta
			theta=3.14/2-atan2((int)star[s]-(int)star[r], (int)star[q]-(int)star[p]);
			cxx=1*(cx*cos(theta) - cy*sin(theta));
			cyy=1*(cx*sin(theta) + cy*cos(theta));
			//Center offset
			cxx=cxx-65-8;
			cyy=cyy+25-105;
			//Center in (cm)
			cX=-cxx/4;
			cY=cyy/4;
         
			m_usb_tx_string("  Theta_old: ");
			m_usb_tx_int(theta*57.6);
			//
			if(theta*57.6>=180)
			{	
				theta=theta-2*3.14;
			}
			/* if(theta*57.6<-180) // Why is this there?
			 { theta=2*3.14-theta;
			 }
			 */
	theta=-theta;
	/*
		//Puck detection
			//trans_c|trans_fl|trans_fr|trans_bl|trans_br| 
		while(!puck_in_hand)
		{
			if(trans_c>trans_fl && trans_c>trans_fr && trans_c>trans_bl && trans_c>trans_br) //Puck found! Go straight!
			{
				puck_in_line=1;
				temp1=MORE*OCR1A; //Straight!
				OCR1B=temp1;
				temp1=MORE*OCR1A;
				OCR1C=temp1;
			}
			else if( (trans_fr > trans_fl && trans_fr > trans_bl) || (trans_br >trans_fl && trans_br > trans_bl) ) //If right p_t> left p_t, Rotate Clockwise
			{
				temp1=LESS*OCR1A; //Clockwise
				OCR1B=temp1;
				temp1=MORE*OCR1A;
				OCR1C=temp1;
			}
			else //Rotate anti-clockwise
			{
				temp1=MORE*OCR1A;
				OCR1B=temp1;
				temp1=LESS*OCR1A;
				OCR1C=temp1;
				
			}
		
			}
	
		}*/
		
	
	
		//Goal detection (Activated once puck is detected <puck_in_hand>)
		to_angle_c=atan2(((goal_1+goal_2)/2-cY),(115-cX)); //Angle between the goal and current position
		to_angle_1=atan2((goal_1-cY),(115-cX)); //Angle between goal_1 (+y) and current position
		to_angle_2=atan2((goal_2-cY),(115-cX)); //Angle between goal_2 (-y) and current position
	
		
		if ( (to_angle_1*57.6>0 && to_angle_2*57.6>0) || (to_angle_1*57.6<0 && to_angle_2*57.6<0) ) //If both the angles are + or - (Out of scope of goal)
		{
			to_angle= (abs(to_angle_1*57.6) < abs(to_angle_2*57.6) ) ? to_angle_1 : to_angle_2; //to_angle = whichever is lesser 
		}
		else //Opposite sign (Within scope of goal)
		{
			to_angle=atan2(0,(115-cX)); //Go straight
		}
        //Difference 
		 m_usb_tx_string("  Theta-to_angle: ");
		 m_usb_tx_int((theta*57.6)-(to_angle*57.6));
		// temp_theta=(abs(theta*57.6)-abs(to_angle*57.6);
		// LESS2=( (MORE-LESS)/(10-temp_theta) )*(temp_theta-10) + MORE; 
		 
		if (theta*57.6<=to_angle*57.6+10 && theta*57.6>=to_angle*57.6-10) // Pointing to goal. Go straight.
		{
			temp1=MORE*OCR1A;
			OCR1B=temp1;
			temp1=MORE*OCR1A;
			OCR1C=temp1;
			m_green(ON);
			m_usb_tx_string("  ||STRAIGHT||");
		}
		else if( theta*57.6<(to_angle*57.6-10) ) //Rotate anti-clockwise
		{ 
			temp1=LESS*OCR1A;
			OCR1B=temp1;
			temp1=MORE*OCR1A;
			OCR1C=temp1;
			m_green(OFF);
			m_usb_tx_string("  ||ANTI-CLOCKWISE||");
		}
		   else if ( theta*57.6>(to_angle*57.6+10) ) //Rotate clockwise
		   {
				temp1=MORE*OCR1A;
				OCR1B=temp1;
				temp1=LESS*OCR1A;
				OCR1C=temp1;
				m_green(OFF);
			m_usb_tx_string("  ||CLOCKWISE||");
		   }
              

	set(PORTB,6);
	set(PORTB,7);
      
	}//end if (<= 1 star has been lost.)
   
    else //If more than 1 star has been lost:
    {   
        m_red(ON);
		OCR1B=0; //Stop the motor
		OCR1C=0;
        clear(PORTB,6); 
        clear(PORTB,7); 
        p=-1; q=-1; r=-1; s=-1;
        cxx=-1; cyy=-1;
        theta=0;
    }
    
    /*    
    m_usb_tx_string("X1: ");
    m_usb_tx_uint(star[0]);
    m_usb_tx_string("  Y1: ");
    m_usb_tx_uint(star[1]);
    m_usb_tx_string("  X2: ");
    m_usb_tx_uint(star[3]);
    m_usb_tx_string("  Y2: ");
    m_usb_tx_uint(star[4]);
    m_usb_tx_string("  X3: ");
    m_usb_tx_uint(star[6]);
    m_usb_tx_string("  Y3: ");
    m_usb_tx_uint(star[7]);
    m_usb_tx_string("  X4: ");
    m_usb_tx_uint(star[9]);
    m_usb_tx_string("  Y4: ");
    m_usb_tx_uint(star[10]);

m_usb_tx_string("  P: ");
    m_usb_tx_int(p);
m_usb_tx_string("  R: ");
    m_usb_tx_int(r);
m_usb_tx_string("  Q: ");
    m_usb_tx_int(q);
m_usb_tx_string("  S: ");
    m_usb_tx_int(s);
*/
    m_usb_tx_string("  Theta: ");
    m_usb_tx_int(theta*57.6);
	
    m_usb_tx_string("  CX (cm): ");
    m_usb_tx_int(cX);
    m_usb_tx_string("  CY (cm): ");
    m_usb_tx_int(cY);
    m_usb_tx_string("   Count: ");
    m_usb_tx_int(count);
    m_usb_tx_string("   Toangle_C: ");
    m_usb_tx_int(to_angle_c*57.6);
	m_usb_tx_string("   Toangle_1: ");
	m_usb_tx_int(to_angle_1*57.6);
	m_usb_tx_string("   Toangle_2: ");
	m_usb_tx_int(to_angle_2*57.6);
	m_usb_tx_string("   Toangle: ");
	m_usb_tx_int(to_angle*57.6);
    m_usb_tx_string("\n");
   m_wait(10);
   
    } ///while (1)
 }// main void()
Esempio n. 21
0
void init(void)
{
	m_red(ON);
	m_clockdivide(0);
	m_bus_init();
	m_usb_init();
	m_rf_open(CHANNEL, RXADDRESS, PACKET_LENGTH);
	WAIT(5); // wait for user to pull up log for connection	
	
	send(0,0,0,0); //USB and wireless initialized
	
	while(!m_usb_isconnected());

	m_red(OFF);
	
	//m_usb_tx_string("Waiting for robot to respond...");
	send(0,0,0,1); //Waiting for robot to respond
	while(!connected)
	{
		
		m_rf_send(TXADDRESS, in_buffer, PACKET_LENGTH);
		BLINK(100);
	}
	//m_usb_tx_string("Robot responded. \nRequsting PID variables.\n");	
	send(0,1,0,2); // Robot responded. Requesting PID variables
	BLINK(500);BLINK(500);

	// Set up ADC for PID

	// Set to external AREF
	clear(ADMUX, REFS1);
	set(ADMUX, REFS0);

	// Set ADC prescaler
	set(ADCSRA, ADPS2);
	set(ADCSRA, ADPS1);
	set(ADCSRA, ADPS0);
	
	set(DIDR0, ADC4D);
	set(DIDR0, ADC5D);
	set(DIDR0, ADC6D);
	
	sei();
	set(ADCSRA, ADIE);


	clear(ADCSRA, ADATE);

	clear(ADCSRA, ADEN);
	
	// MUX selection
	clear(ADCSRB, MUX5);
	set(ADMUX, MUX2);
	clear(ADMUX, MUX1);
	clear(ADMUX, MUX0);

	// enables the system
	set(ADCSRA, ADEN);

	// starts the conversion
	set(ADCSRA, ADSC);
	


	m_red(OFF);
	m_green(OFF);
	
	
}