void main(void){
    PWM_motor_setup();
    Inputs_motor_setup();
    ADC_setup();

    
    for(;;){
//    while(INTCONbits.TMR0IF==0){}   // busy loop
//    ADCON0bits.GO=1; 
//    while(PIR1bits.ADIF==0){}   // busy loop wait for conversion to finish
//    vel = ADRESH;
    turn();  // does the 8 bit to unsigned int work?
    
    }
    
}
예제 #2
0
파일: MAIN3.c 프로젝트: eloisab/robockey
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
        {}
        
    }
}
//#pragma code highPriorityInterruptAddress=0x0008
//void high_interrupt(void)
//{
//    _asm GOTO highPriorityIsr _endasm
//}
//
//#pragma code
void main(void){
  


  //Default
  TRISD = 0x00;         //set port D signals to output
  menu_ref_1=1;
  menu_ref_2=0;
  
  
  /*zone of disgust*/
  
  values[MAXSPEED]=100;        //Max speed
  values[PIDGAINS]=100;        //PID Gains
  values[MAXYAW]=100;        //Max Yaw
  values[IRSAMPE]=10;         //ir_samp_e;
  values[IRSAMPR]=20;         //ir_samp_r;
  values[IRRAW]=60;         //ir_raw
  values[IRAVG]=10;         //ir_avg;
  
  /*****************/
  
  setupSerial();
  Lcd_Init();
  ADC_setup();
  Button_Setup();
  welcome();
  LCD_disp(menu_ref_1, menu_ref_2);

  RUN=0;
  while(1){

  
//NORMAL OPERATION  
    while(RUN==0){
        
        
        if(PORTDbits.RD0 == 0)
        {
            mode_button = 1;
        }
        
        if(PORTDbits.RD1 == 0)
        {
            motor_button = 1;
        }
        
        


        if (mode_button==1){                  //Mode change sequence
                checkMode(menu);
                menu_ref_2=0;         //Default menu ref, should only be SPEED
                delayms(200);
                mode_button=0;
                LCD_disp(menu_ref_1, menu_ref_2);
        }

        delayms(200);       //Used to prevent double tapping
        //possibly new function
        switchChannels(0);
        joy_x = doADC();
        switchChannels(1);
        joy_y = doADC();   
                         
      
        if(menu_ref_1==MANUAL)   //IF IN MANUAL
        {
            
           
            if(joy_x<=LEFT){      //User pushes right joystick left
                if(values[menu_ref_2]>0){
                    values[menu_ref_2]=values[menu_ref_2]-5;          //decrement value by 5%
                    GLOBAL_MAX_SPEED = values[menu_ref_2];
                    sendMaxSpeed();
                }
                
                LCD_disp(menu_ref_1, menu_ref_2);
                delayms(200);     //Delay to prevent flickering, and also to prevent 100-0 increments

            }
            else if (joy_x>=RIGHT){     //User pushes right joystick right
                if(values[menu_ref_2]<100){
                
                    values[menu_ref_2]=values[menu_ref_2]+5;          //increment value by 5%
                    GLOBAL_MAX_SPEED = values[menu_ref_2];
                    sendMaxSpeed();
                }
                
                LCD_disp(menu_ref_1, menu_ref_2);
                delayms(200);
            }   
        }  

        else if(menu_ref_1==FACTORY){            //IF IN FACTORY MODE
            if (joy_y>=UP){            //User pushes left joystick UP  
                //Circular selection
                if(menu_ref_2==0){
                  menu_ref_2=4;
                }
                else{
                  menu_ref_2--;             //switched the order of this to prevent overflow
                }
                LCD_disp(menu_ref_1, menu_ref_2);
                delayms(250);               //Arbitrary delay of 250 milliseconds
            }
            else if (joy_y<=DOWN){      //User pushes left joystick DOWN
                menu_ref_2++;
                //Circular selection
                if(menu_ref_2>=7){
                  menu_ref_2=1;
                }
                LCD_disp(menu_ref_1, menu_ref_2);
                delayms(250);
                

            }
            else if (joy_x<=LEFT){      //User pushes right joystick left
                
                if(values[menu_ref_2]>0){
                    values[menu_ref_2]=values[menu_ref_2]-5;          //decrement value by 5%
                }
                
                LCD_disp(menu_ref_1, menu_ref_2);
                delayms(255);
                delayms(200);

            }
            else if (joy_x>=RIGHT){     //User pushes right joystick right
                
                if(values[menu_ref_2]<100){
                    values[menu_ref_2]=values[menu_ref_2]+5;          //decrement value by 5%
                }
                
                LCD_disp(menu_ref_1, menu_ref_2);
                delayms(255);
                delayms(200);

            }            
        }  
        
        //All other menu_ref_1's do not have values displayed       
        
        if (motor_button==1){
            motor_button=0;
            RUN=1;
            
        }
        //send here
    }
    
    /*Menu on setup*/
    if (RUN==1){
        on_setup();
        GLOBAL_RUN = RUN;
        sendRun();
        //MOTOR ON GOES AFTER
        motor_button=0;
    }
      
      /*MOTOR ON BEHAVIOUR*/
    while(RUN==1){
        if(PORTDbits.RD1 == 0)
        {
            motor_button = 1;
        }
        if (motor_button==1){      //At interrupt, stop motor and return to menu
            motor_button=0;
            RUN=0;
            GLOBAL_RUN = RUN;
            sendRun();
            
        }
        switchChannels(0);
        //GLOBAL_VELOCITY = doADC();    GLOBAL_VELOCITY = doADC() //Get joystick values
        joy_y = doADC();
        GLOBAL_VELOCITY = joy_y;
        
        switchChannels(1);
        joy_x = doADC();
        GLOBAL_OMEGA = joy_x;
        
        sendVelocity();
        sendOmega();
        
        //send command here
    }
      
      /*MOTOR OFF MESSAGE*/ 
    RUN=0;
      //MOTOR OFF FUNCTION GOES BEFORE MESSAGE
    LCD_title(t5);         //"Whoa!"
      
    delays(2);    //safety delay    

    LCD_disp(menu_ref_1, menu_ref_2); //Return to normal display
      
      
  }
  
  
  
}
void main(void)
{
    PWM_motor_setup();
    ADC_setup();
    setupSerial();
    IR_Setup();
    TMR2_INT_setup();
    while(1)
    {
        while(GLOBAL_MODE == MANUAL)
        {
          //turn(GLOBAL_VELOCITY,GLOBAL_OMEGA);
          noob_turn(GLOBAL_VELOCITY,GLOBAL_OMEGA);
            
        }
        
        while(GLOBAL_MODE == ASSIST)
        {
            IR_Calculate();                  

            if(dist_count == 2)
            {
                 new_turn(5, Get_dist_diff());
                newdistance = 0;
            }                       
                
        }
        
        while(GLOBAL_MODE == AUTO)
        {
            IR_Calculate();          
          
            if(newstate == 1)
            {
                    if(Get_S_IR_state() == IR_TOO_CLOSE)
                    {
//                        turn(AUTO_VEL, AUTO_OMEGA);
                        CCPR1L =  40;
                        CCPR2L =  50;
              
                    }
                    else if(Get_S_IR_state() == IR_TOO_FAR)
                    {
//                        turn(AUTO_VEL,255 -AUTO_OMEGA);
                        CCPR1L =  50;
                        CCPR2L =  40;
              
                    }
                    else if(Get_S_IR_state() == IR_ERROR)
                    {
                        turn(128,128);
              
                    }
                    newstate = 0;
            }
          
       }                   
        
        
    }
}
예제 #5
0
파일: main.c 프로젝트: eloisab/robockey
//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");
    }
}