k

Dependencies:   mbed

main.cpp

Committer:
Suchakhree
Date:
2015-12-07
Revision:
0:238df339023b
Child:
1:426fbd0d126a

File content as of revision 0:238df339023b:

#include "mbed.h"
#include "MPU9250.h"
#include "TextLCD.h"

int gyrol_Break();

Serial pc(USBTX, USBRX); // Huyperterminal default config: 9600 bauds, 8-bit data, 1 stop bit, no parity
Serial mobile(D8,D2);
//mobile.baud(38400);

MPU9250 mpu9250;
Timer t;
//DigitalOut myled(LED1);

I2C i2c_lcd(D14,D15); // SDA, SCL
TextLCD_I2C lcd(&i2c_lcd, 0x4E, TextLCD::LCD20x4); // I2C bus, PCF8574 Slaveaddress, LCD Type

char pack[8]={0};

int Ay=0;
float sum=0;
uint32_t sumCount = 0;
char buffer[14];
uint8_t dato_leido[2];
uint8_t whoami;
int SendAcceloroY();

DigitalOut pinled0(PC_3);  DigitalOut pinled1(PC_2);  DigitalOut pinled2(PC_13);
DigitalOut pinled3(PB_7);  DigitalOut pinled4(PA_15); DigitalOut pinled5(PA_13);
DigitalOut pinled6(PC_12); DigitalOut pinled7(PC_10); DigitalOut pinled8(PB_4);
DigitalOut pinled9(PB_10); DigitalOut pinbuzz(PA_8);
void resetled();
void getdistance2led(char step);

DigitalIn switch1(D9);
DigitalIn switch2(D10);
DigitalIn switch3(D11);
void switch_turn();
int state = 0;

int main() {
    
  //___ Set up I2C: use fast (400 kHz) I2C ___
  i2c.frequency(400000);  
  // Read the WHO_AM_I register, this is a good test of communication
  whoami = mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
  pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x71\n\r");
  if (I2Cstate != 0) // error on I2C
    pc.printf("I2C failure while reading WHO_AM_I register");
  
  if (whoami == 0x71) // WHO_AM_I should always be 0x71
  {  
        pc.printf("MPU9250 is online...\n\r");
        sprintf(buffer, "0x%x", whoami);
        wait(1);
        mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration
        mpu9250.MPU9250SelfTest(SelfTest); // Start by performing self test and reporting values (accelerometer and gyroscope self test)
        mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometer, load biases in bias registers  
        wait(2);
        // Initialize device for active mode read of acclerometer, gyroscope, and temperature
        mpu9250.initMPU9250();
        pc.printf("MPU9250 initialized for active data mode....\n\r");
        // Initialize device for active mode read of magnetometer, 16 bit resolution, 100Hz.
        mpu9250.initAK8963(magCalibration);
        wait(1);
   }
   
   else // Connection failure
   {
    pc.printf("Could not connect to MPU9250: \n\r");
    pc.printf("%#x \n",  whoami);    
    sprintf(buffer, "WHO_AM_I 0x%x", whoami);
    while(1) ; // Loop forever if communication doesn't happen
    }
  
    mpu9250.getAres(); // Get accelerometer sensitivity
    mpu9250.getGres(); // Get gyro sensitivity
    mpu9250.getMres(); // Get magnetometer sensitivity
    magbias[0] = +470.;  // User environmental x-axis correction in milliGauss, should be automatically calculated
    magbias[1] = +120.;  // User environmental x-axis correction in milliGauss
    magbias[2] = +125.;  // User environmental x-axis correction in milliGauss
    
    lcd.locate(0,0);
    lcd.printf("FRA241 Software-Dev.");
    lcd.locate(0,1);
    lcd.printf("Bike_Light");
    resetled();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////         
    while(1) 
    {
      if(switch1 == 1 || switch2 == 1 || switch3 == 1) mobile.printf("#");
        if(switch1 == 1) {mobile.printf("#TR$"); lcd.locate(0,3); lcd.printf("TurnRight"); }
        if(switch2 == 1) {mobile.printf("#TL$"); lcd.locate(0,3); lcd.printf("TurnLeft.");}
        if(switch3 == 1) {mobile.printf("#BR$"); lcd.locate(0,3); lcd.printf("Break.");}
        else {mobile.printf("#MD$"); lcd.locate(0,3); lcd.printf("None.   ");}    
      
      //switch_turn();
      //Read data
     /* if(mobile.readable())
      {
            pack[0] = mobile.getc();
            for(int i=0;i<8;i++)
                pack[i] = mobile.getc();
              
              //Debug_Serial   
              //for(int i=0;i<8;i++) 
              //    pc.printf("%c",pack[i]); 
              //pc.printf("\n"); 

              lcd.locate(0,2); 
              for(int i=0;i<8;i++) lcd.printf("%c",pack[i]); 
              
              if(pack[1] == '#')
              {
                
                if(pack[2] == 'D' && pack[5] == 'S')
                {
                     getdistance2led(pack[4]);
                     lcd.locate(0,2);  
                     lcd.printf("SPEED : %c%c km/hr",pack[6],pack[7]);
                     
                     //switch1.rise(&switch_turn); 
                     
                     //for(int i; ((switch1 == 1 || switch2 == 1 || switch3 == 1 )&& state == 0);)  {mobile.printf("#"); break;}
                     //for(;switch1 == 1 && state == 0;) {mobile.printf("#TR$"); lcd.locate(0,3); lcd.printf("TurnRight"); state=1; break;}
                     //else if(switch2 == 1 && state == 0) {mobile.printf("#TL$"); lcd.locate(0,3); lcd.printf("TurnLeft."); state=1;}
                     //else if(switch3 == 1 && state == 0) {mobile.printf("#BR$"); lcd.locate(0,3); lcd.printf("Break.   "); state=1;}
                     //if((switch1 == 0 || switch2 == 0 || switch3 == 0 )&& state == 1){mobile.printf("#"); mobile.printf("#MD$"); lcd.locate(0,3); lcd.printf("Clear."); state=0;}
    
                }*/
                
              //}  
            
      }
        
    }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    
}

void getdistance2led(char step)
{
         resetled();
         if(step == '0'){
            pinled0=1; pinled1=0; pinled2=0; pinled3=0; pinled4=0; pinled5=0; pinled6=0; pinled7=0; pinled8=0; pinled9=0;}
         else if(step == '1'){
            pinled0=1; pinled1=1; pinled2=0; pinled3=0; pinled4=0; pinled5=0; pinled6=0; pinled7=0; pinled8=0; pinled9=0;}
         else if(step == '2'){
            pinled0=1; pinled1=1; pinled2=1; pinled3=0; pinled4=0; pinled5=0; pinled6=0; pinled7=0; pinled8=0; pinled9=0;}
         else if(step == '3'){
            pinled0=1; pinled1=1; pinled2=1; pinled3=1; pinled4=0; pinled5=0; pinled6=0; pinled7=0; pinled8=0; pinled9=0;}
         else if(step == '4'){
            pinled0=1; pinled1=1; pinled2=1; pinled3=1; pinled4=1; pinled5=0; pinled6=0; pinled7=0; pinled8=0; pinled9=0;}
         else if(step == '5'){
            pinled0=1; pinled1=1; pinled2=1; pinled3=1; pinled4=1; pinled5=1; pinled6=0; pinled7=0; pinled8=0; pinled9=0;}
         else if(step == '6'){
            pinled0=1; pinled1=1; pinled2=1; pinled3=1; pinled4=1; pinled5=1; pinled6=1; pinled7=0; pinled8=0; pinled9=0;}
         else if(step == '7'){
            pinled0=1; pinled1=1; pinled2=1; pinled3=1; pinled4=1; pinled5=1; pinled6=1; pinled7=1; pinled8=0; pinled9=0; pinbuzz=1;}
         else if(step == '8'){
            pinled0=1; pinled1=1; pinled2=1; pinled3=1; pinled4=1; pinled5=1; pinled6=1; pinled7=1; pinled8=1; pinled9=0; pinbuzz=1;}
         else if(step == '9'){
            pinled0=1; pinled1=1; pinled2=1; pinled3=1; pinled4=1; pinled5=1; pinled6=1; pinled7=1; pinled8=1; pinled9=1; pinbuzz=1;}
         else { pinled0=0; pinled1=0; pinled2=0; pinled3=0; pinled4=0; pinled5=0; pinled6=0; pinled7=0; pinled8=0; pinled9=0; pinbuzz=0; }
}
void resetled()
{
         //reset
         pinled0=0; pinled1=0; pinled2=0; pinled3=0; pinled4=0;
         pinled5=0; pinled6=0; pinled7=0; pinled8=0; pinled9=0;
         pinbuzz=0;
}


//Function 4 SenAcceloroY
void switch_turn()
{
        if(switch1 == 1 || switch2 == 1 || switch3 == 1) mobile.printf("#");
        if(switch1 == 1) {mobile.printf("#TR$"); lcd.locate(0,3); lcd.printf("TurnRight"); }
        if(switch2 == 1) {mobile.printf("#TL$"); lcd.locate(0,3); lcd.printf("TurnLeft.");}
        if(switch3 == 1) {mobile.printf("#BR$"); lcd.locate(0,3); lcd.printf("Break.");}
        else {mobile.printf("#MD$"); lcd.locate(0,3); lcd.printf("None.   ");}    
}
int SendAcceloroY()
{
         // If intPin goes high, all data registers have new data
        if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) {  // On interrupt, check if data ready interrupt   
            mpu9250.readAccelData(accelCount);  // Read the x/y/z adc values   
            // Now we'll calculate the accleration value into actual g's
            if (I2Cstate != 0) //error on I2C
                pc.printf("I2C error ocurred while reading accelerometer data. I2Cstate = %d \n\r", I2Cstate);
            else{ // I2C read or write ok
                I2Cstate = 1;
                ax = (float)accelCount[0]*aRes - accelBias[0];  // get actual g value, this depends on scale being set
                ay = (float)accelCount[1]*aRes - accelBias[1];
                az = (float)accelCount[2]*aRes - accelBias[2];
            }   
            
            mpu9250.readGyroData(gyroCount);  // Read the x/y/z adc values
            // Calculate the gyro value into actual degrees per second
            if (I2Cstate != 0) //error on I2C
                pc.printf("I2C error ocurred while reading gyrometer data. I2Cstate = %d \n\r", I2Cstate);
            else{ // I2C read or write ok
                I2Cstate = 1;
                gx = (float)gyroCount[0]*gRes - gyroBias[0];  // get actual gyro value, this depends on scale being set
                gy = (float)gyroCount[1]*gRes - gyroBias[1]; 
                gz = (float)gyroCount[2]*gRes - gyroBias[2];
            }
            
            mpu9250.readMagData(magCount);  // Read the x/y/z adc values   
            // Calculate the magnetometer values in milliGauss
            // Include factory calibration per data sheet and user environmental corrections
            if (I2Cstate != 0) //error on I2C
                pc.printf("I2C error ocurred while reading magnetometer data. I2Cstate = %d \n\r", I2Cstate);
            else{ // I2C read or write ok
                I2Cstate = 1;
                mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0];  // get actual magnetometer value, this depends on scale being set
                my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1];  
                mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2];
            }
                       
            mpu9250.getCompassOrientation(orientation);
        }
   
        Now = t.read_us();
        deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
        lastUpdate = Now;    
        sum += deltat;
        sumCount++;
    
        // Pass gyro rate as rad/s
        // mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f,  my,  mx, mz);
         mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);

        // Serial print and/or display at 1.5 s rate independent of data rates
        delt_t = t.read_ms() - count;
        if (delt_t > 500) { // update LCD once per half-second independent of read rate
            mpu9250.MadgwickQuaternionUpdate(ax,ay,az,gx,gy,gz,mx,my,mz);  

            pc.printf(" ay = %.2f\n", 1000*ay);

            tempCount = mpu9250.readTempData();  // Read the adc values
            if (I2Cstate != 0) //error on I2C
                pc.printf("I2C error ocurred while reading sensor temp. I2Cstate = %d \n\r", I2Cstate);
            else{ // I2C read or write ok                
                I2Cstate = 1;
                temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade
                //pc.printf(" temperature = %f  C\n\r", temperature);
            }


            myled= !myled;
            count = t.read_ms(); 

            if(count > 1<<21) {
                t.start(); // start the timer over again if ~30 minutes has passed
                count = 0;
                deltat= 0;
                lastUpdate = t.read_us();
            }
        sum = 0;
        sumCount = 0; 
        }
        return ay*1000;
}