geometry_msgs::Twist PoseFollower::diff2D(const tf::Pose& pose1, const tf::Pose& pose2)
  {
    geometry_msgs::Twist res;
    tf::Pose diff = pose2.inverse() * pose1;
    res.linear.x = diff.getOrigin().x();
    res.linear.y = diff.getOrigin().y();
    res.angular.z = tf::getYaw(diff.getRotation());

    if(holonomic_ || (fabs(res.linear.x) <= tolerance_trans_ && fabs(res.linear.y) <= tolerance_trans_))
      return res;

    //in the case that we're not rotating to our goal position and we have a non-holonomic robot
    //we'll need to command a rotational velocity that will help us reach our desired heading
    
    //we want to compute a goal based on the heading difference between our pose and the target
    double yaw_diff = headingDiff(pose1.getOrigin().x(), pose1.getOrigin().y(), 
        pose2.getOrigin().x(), pose2.getOrigin().y(), tf::getYaw(pose2.getRotation()));

    //we'll also check if we can move more effectively backwards
    if (allow_backwards_) 
    {
      double neg_yaw_diff = headingDiff(pose1.getOrigin().x(), pose1.getOrigin().y(), 
					pose2.getOrigin().x(), pose2.getOrigin().y(), M_PI + tf::getYaw(pose2.getRotation()));

      //check if its faster to just back up
      if(fabs(neg_yaw_diff) < fabs(yaw_diff)){
        ROS_DEBUG("Negative is better: %.2f", neg_yaw_diff);
        yaw_diff = neg_yaw_diff;
      }
    }

    //compute the desired quaterion
    tf::Quaternion rot_diff = tf::createQuaternionFromYaw(yaw_diff);
    tf::Quaternion rot = pose2.getRotation() * rot_diff;
    tf::Pose new_pose = pose1;
    new_pose.setRotation(rot);

    diff = pose2.inverse() * new_pose;
    res.linear.x = diff.getOrigin().x();
    res.linear.y = diff.getOrigin().y();
    res.angular.z = tf::getYaw(diff.getRotation());
    return res;
  }
示例#2
0
int main ( void )
{

    //TRISA = 0b0010000000000000;
    TRISA = 0b00001111;
    TRISAbits.TRISA12 = 0;
    
    TRISCbits.TRISC1 = 0;
    TRISCbits.TRISC2 = 0;
    TRISCbits.TRISC3 = 0;
    TRISCbits.TRISC4 = 0;
    
    //TRISD = 0;
    TRISD = 0b0000000000010000; // RD11: DIR2; RD8: PWM2
    
    //TRISE = 0b00001000;
    TRISE = 0b00001000; // RE6: DIR1; RE0: PWM1
    TRISEbits.TRISE7 = 0; 
   
    //LCD an
    _PCFG16 = 1; // AN16 is digital
    _PCFG17 = 1; // AN17 is digital
    _PCFG18 = 1; // AN18 is digital
    _PCFG19 = 1; // AN19 is digital
    _PCFG20 =1;
    _PCFG31=1;
    
    //pwm analog select
    _PCFG24=1;
    _PCFG30=1;
    
    //RE4 for lcd r/s
     _PCFG28=1;
     TRISEbits.TRISE4 = 0;
    
    AD1PCFGHbits.PCFG28 = 1;
    AD1PCFGHbits.PCFG27 = 1;
    AD1PCFGHbits.PCFG30 = 1;
    AD1PCFGHbits.PCFG24 = 1;
    AD1PCFGH = 0x0020;
    TRISAbits.TRISA13 = 1;
    //set pwm low
    PWM1=0;
    PWM2=0;
    
    SYS_Initialize ( ) ;
    
    CLKDIVbits.FRCDIV = 0;
    CLKDIVbits.PLLPOST = 0;  // N2 = 2
    CLKDIVbits.PLLPRE = 0;  // N1 = 2
    PLLFBD = (Fosc*N1_default*N2_default/Ffrc) - M_default; // M = 8 -> Fosc = 7.3728 MHz * 8 / 2 / 2 = 16 MHz
    while(!OSCCONbits.LOCK);	// Wait for PLL to lock
	RCONbits.SWDTEN = 0;      // Disable Watch Dog Timer
    
    //TRISF = 0;
    gpsLock = 0;
   
    lcd_init();
    print_lcd("Initializing");
    //delay_ms(500);
    //lcd_clear();
    
    /*lcd test
    char line1[] = " On Route ";
    char line2[] = " Arrived ";
    //send_command_byte(0xFF);
    //while(1){send_command_byte(0xFF);send_data_byte(0);}
   // delay_ms(2);
    //send_command_byte(0x02); // Go to start of line 1
    //send_command_byte(0b00001111); // Display: display on, cursor on, blink on
    //send_command_byte(0b00000110); // Set entry mode: ID=1, S=0
    //send_command_byte(0b00000001); // Clear display
    
    print_lcd(line1);
    delay_ms(5000);
    lcd_clear();
    print_lcd(line2);
    //send_command_byte(0xC0); // Go to start of line 1
    //while(1){send_command_byte(0b00000001);}
    while(1);*/
    
    /*int a;
    long long int ct;
    int i;
    int j=0;*/
    //i2c init

    
    //uart init
    UART_Initialize();
    delayMs(100);
    
    
    /* while(1){//test for delay ms configuration
        //PORTDbits.RD1 = 1;
        //delayUs(10);
        //for(i = 0;i <7;i++);
        delayMs(10);
        PORTDbits.RD12 = 1;
        //for (i = 0; i < 1000; i++);
        //PORTDbits.RD1 = 0;
        //for(i = 0;i <7;i++);
        delayMs(10);
        
        PORTDbits.RD12 = 0;
        //for (i  = 0; i < 1000; i++);
    }*/
    
    //ultrasonic test
    
   /* while(1){
        long double x;
        delayMs(500);
        PORTEbits.RE4 = 1;                  //TRIGGER HIGH
        PORTDbits.RD5 = 1; 
        delay_us(10);                     //10uS Delay 
        lcd_clear();
        ultraSonicEn = 1;
        ultraSonicCount = 0;

        PORTEbits.RE4 = 0;                  //TRIGGER LOW
        PORTDbits.RD5 = 0; 
        
        
        while (!PORTDbits.RD4);              //Waiting for Echo
       IEC0bits.T2IE = 1; //enable timer
        while(PORTDbits.RD4);// 
        IEC0bits.T2IE = 0; //disable timer
        x = ultraSonicCount/TICKS_PER_METER;
        sprintf(outputBuf,"%lf",x); 
        print_lcd(outputBuf);
    }*/

    //TX_str(endGPS);
    //delayMs(3000);
    //TX_str(startGPS);
    /*TX_str(startGPShot);
    delayMs(2000);
    while ( !gpsLock ) {
        TX_str(getGPS);
        delayMs(500);
    }*/
    
   //TCP code
    
    TX_str(openNet);
    delayMs(5000);
    TX_str(openConnection);
    delayMs(5000);
 
    // while(!BUTTON_IsPressed ( BUTTON_S3 ));
    //TX_str(sprintf("%s%d\r",sendTCP, strlen("10")));
    sprintf(outputBuf2,"2\n%lf,%lf\n\r",roverLog,roverLat );
    sprintf(cmdBuf,"%s%d\r", sendTCP,strlen(outputBuf2));
         
    TX_str(cmdBuf);
    delayMs(3000);
    TX_str(outputBuf2);
    while(!waypointsReady || !gpsLock);
    
    
    //while(1);
    delayMs(7000);
    lcd_clear();
    print_lcd("waypoints locked");
    
    i2c_init();
    //i2c_write3(0x3c, 0x00, 0x70);
    i2c_write3(0x3c, 0x00, 0b00011000);
    //i2c_write3(0x3c, 0x01, 0b11000000);
    i2c_write3(0x3c, 0x01, 0xA0);
    i2c_write3(0x3c, 0x02, 0x00);
    
    //timer init, do this after other initializations
    motorDuty=0;
    tim1_init();
    tim2_init();
   
    /*
    double angleTolerance = 8.0;
     motorDuty=2;
 
    while (1){ //adjust


        double angleDiff = headingDiff(0,roverHeading );


        if (angleDiff > 0 ||abs(angleDiff) > 175 ){ //turn left

            PWM1_DIR = 0; //left            NOTE: 0 is forward, 1 is reverse
            PWM2_DIR = 1; //right
        } else{ // turn right

            PWM1_DIR = 1;
            PWM2_DIR = 0;
        }
        if (abs(angleDiff) < angleTolerance){
            motorDuty = 0;
            //break;
        }else{
            //motorDuty =4;
            motorDuty =2;
        }   
        delayMs(13);
        updateHeading();
    }*/
    
    //hardcoded gps for tcpip test
    
    
     /*while(1){
//        PORTDbits.RD5 = 1;
//        for (a = 0; a < (100/33); a++) {}
//        PORTDbits.RD5 = 0;
//        for (a = 0; a < (100/33); a++) {}
        ct = 0;
        //TMR2 = 0;//Timer Starts
        delayMs(60);
        PORTEbits.RE4 = 1;                  //TRIGGER HIGH
        PORTDbits.RD5 = 1; 
        delay_us(15);
                           //10uS Delay 
        PORTEbits.RE4 = 0;                  //TRIGGER LOW
        PORTDbits.RD5 = 0; 
        while (!PORTDbits.RD4){              //Waiting for Echo
            ct++;
        }
            //T2CONbits.TON = 1;
        while(PORTDbits.RD4) {
            ct++;
        }// {
        //T2CONbits.TON = 0;
        sprintf(outputBuf,"%lld", ct); 
        //a = TMR2;
        //a = a / 58.82;
        
        //long int p;
        //for(p = 0; p <100000; p++);
        
    }*/
    //tim1_init();
    //while ( 1 );
    //UART_Initialize();
    
    /* Infinite Loop */
    /*
     long int i;
    while ( 1 )
    {//test for delay ms configuration
        //PORTDbits.RD1 = 1;
        delayMs(10);
        PORTDbits.RD12 = 1;
        //for (i = 0; i < 1000; i++);
        //PORTDbits.RD1 = 0;
        delayMs(10);
        PORTDbits.RD12 = 0;
        //for (i = 0; i < 1000; i++);
    }*/
    //IEC0bits.T1IE = 0;
    
    //IEC0bits.T1IE = 1;
    //motorDuty =2;
    motorStopFlag = 0; //ready to go
    char tempBuf1[50];
    int wapointsVisited;
    double desiredHeading = 0;
    double dToWaypoint = 99999.9;
    double angleTolerance = 6.0;
    for (wapointsVisited =0; wapointsVisited<numGPSpoints;wapointsVisited++ )
    {
        dToWaypoint = 99999.9;
        while (1)
        {

            int f;
            roverLog = 0;
            roverLat = 0;
            for(f = 0; f < ROVER_LEN; f++){
                roverLog+=roverlog[f]/ROVER_LEN_D;
                roverLat+=roverlat[f]/ROVER_LEN_D;
            }
            
            
            dToWaypoint = dist(convertGPSToDeg(roverLat),convertGPSToDeg(roverLog),convertGPSToDeg(gpsLat[wapointsVisited]),convertGPSToDeg(gpsLonge[wapointsVisited]));
            if(dToWaypoint <= 3.0){break;} //go to next waypoint

            desiredHeading = 330.0;
            desiredHeading = bearing(convertGPSToDeg(roverLat),convertGPSToDeg(roverLog),convertGPSToDeg(gpsLat[wapointsVisited]),convertGPSToDeg(gpsLonge[wapointsVisited]));
            
            updateHeading();
            
                
            if (!(abs(headingDiff(desiredHeading,roverHeading )) < angleTolerance))
            {
                motorDuty = 0;  //stop
                delayMs(300);
                while (1) //ADJUSTMENT LOOP
                {
                   
                    double angleDiff = headingDiff(desiredHeading,roverHeading );

                    //double dist = ultraSonicPing();
                    /*sprintf(tempBuf1,"%f",dist);
                    lcd_clear();
                    print_lcd(tempBuf1);*/
                     if (abs(angleDiff) < angleTolerance){
                        motorDuty = 0;
                        /*sprintf(tempBuf1,"%f",roverHeading);
                        IEC0bits.T1IE = 0;
                        print_lcd(tempBuf1);
                        IEC0bits.T1IE = 1;*/
                        break;
                    }
                    if (angleDiff > 0 || abs(angleDiff) > 175 ){ //turn right
                        /*PORTDbits.RD4 = 1; //right 0 is forwards, 1 i backwards
                        PORTDbits.RD2 = 1;
                        PORTDbits.RD8 = 0; //left*/
                        PWM1_DIR = 0; //left            NOTE: 0 is forward, 1 is reverse
                        PWM2_DIR = 1; //right
                    } else{ // turn left
                        /*PORTDbits.RD4 = 0; //right 0 is forwards, 1 i backwards
                        PORTDbits.RD2 = 0;
                        PORTDbits.RD8 = 1; //left*/
                        PWM1_DIR = 1;
                        PWM2_DIR = 0;
                    }
                   
                       
                    motorDuty =3;
                     
                    delayMs(15);
                    updateHeading();
        //        sprintf(tempBuf1,"%f",roverHeading);
        //        IEC0bits.T1IE = 0;
        //        lcd_clear();
        //        print_lcd(tempBuf1);
        //        IEC0bits.T1IE = 1;
                //delayMs(1000);
                //for(p = 0; p <100000; p++);
                }
            }
            PWM1_DIR = 0; //left            NOTE: 0 is forward, 1 is reverse
            PWM2_DIR = 0;

            motorDuty = 5;
            ultraSonicDelayEnable = 1;
            //frequency is around 20kHz
            while (ultraSonicDelayCount < 60000){ //for 4 seconds poll ultrasonic and check for obsticles
                long double x;
                //PORTEbits.RE4 = 1;                  //TRIGGER HIGH
                PORTDbits.RD5 = 1; 
                delay_us(10);                     //10uS Delay 
                lcd_clear();
                ultraSonicEn = 1;
                ultraSonicCount = 0;

                //PORTEbits.RE4 = 0;                  //TRIGGER LOW
                PORTDbits.RD5 = 0; 


                while (!PORTDbits.RD4);              //Waiting for Echo
               IEC0bits.T2IE = 1; //enable timer
                while(PORTDbits.RD4);// 
                IEC0bits.T2IE = 0; //disable timer
                ultraSonicEn = 0;
                x = ultraSonicCount/TICKS_PER_METER;
                if (x <= 1.4){
                    motorDuty = 0;
                }else{
                    motorDuty = 5;
                }
                delayMs(200);
            }
            ultraSonicDelayEnable = 0;
            ultraSonicDelayCount = 0;
        }
        
        motorDuty = 0;
        delayMs(1000);
        //IEC0bits.T1IE = 0;
        lcd_clear();
        char buf3[40];
        sprintf(buf3, "reached %d", wapointsVisited);
        print_lcd(buf3);
        delayMs(3000);
        //IEC0bits.T1IE = 1;
        
    }
    //IEC0bits.T1IE = 0;
    lcd_clear();
    print_lcd("ARRIVED!!!");
    //IEC0bits.T1IE = 1;
    while (1);
}