IL programma di acquisizione dati dall'accelerometro. Acquisisce 5000 campioni al secondo. Bisogna fare la parte di scrittura su SD. Per la board Nucleo!

Dependencies:   SDFileSystemFunzionante mbed

Fork of Nucleo_rtos_basic_copyProva_Quirino by Unina_corse

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "header.h"
00003 #include <time.h>
00004 #include "MPU6050.h"
00005 
00006 int main()
00007 {
00008     i2c.frequency(400000);
00009     MPU6050 mpu6050;
00010     CANMessage msg;
00011     uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050);  // Read WHO_AM_I register for MPU-6050
00012     //pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x68\n\r");
00013   
00014     if (whoami == 0x68) // WHO_AM_I should always be 0x68
00015     {  
00016     //pc.printf("MPU6050 is online...");
00017     //wait(1);
00018      
00019         mpu6050.MPU6050SelfTest(SelfTest); // Start by performing self test and reporting values
00020     
00021         if(SelfTest[0] < 1.0f && SelfTest[1] < 1.0f && SelfTest[2] < 1.0f && SelfTest[3] < 1.0f && SelfTest[4] < 1.0f && SelfTest[5] < 1.0f){
00022             mpu6050.resetMPU6050(); // Reset registers to default in preparation for device calibration
00023             mpu6050.calibrateMPU6050(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers  
00024             mpu6050.initMPU6050(); pc.printf("MPU6050 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature
00025         }
00026         else
00027             pc.printf("Device did not the pass self-test!\n\r");
00028     }
00029     else{
00030     pc.printf("Could not connect to MPU6050: \n\r");
00031     pc.printf("%#x \n",  whoami);
00032  
00033     while(1) ; // Loop forever if communication doesn't happen
00034     }
00035     
00036     int i;
00037     for(i = 0; i < BLOCCO; i++){
00038         
00039             vettore[i].x = 0;
00040             vettore[i].y = 0;
00041             vettore[i].z = 0;
00042             vettore[i].xx = 0;
00043             vettore[i].yy = 0;
00044             vettore[i].zz = 0;
00045      
00046     }
00047     
00048    while (true) {
00049      // Read the WHO_AM_I register, this is a good test of communication
00050     srand(time(NULL));
00051     int i;
00052     static const int off_set_a=400;
00053     //time_t rawtime;
00054     //struct tm* timeinfo;
00055     //Timer temp3;
00056     //temp3.start();
00057     for(i = 0; i < BLOCCO; i++){
00058        
00059         // If data ready bit set, all data registers have new data
00060         if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) {  // check if data ready interrupt
00061                 mpu6050.readAccelData(accelCount);  // Read the x/y/z adc values
00062                 mpu6050.getAres();
00063     
00064             // Now we'll calculate the accleration value into actual g's
00065                 vettore[i].x = (float)accelCount[0]*aRes;  // get actual g value, this depends on scale being set
00066                 vettore[i].y = (float)accelCount[1]*aRes;   
00067                 vettore[i].z = (float)accelCount[2]*aRes;  
00068             
00069                 mpu6050.readGyroData(gyroCount);  // Read the x/y/z adc values
00070                 mpu6050.getGres();
00071  
00072             // Calculate the gyro value into actual degrees per second
00073                 vettore[i].xx = (float)gyroCount[0]*gRes; // - gyroBias[0];  // get actual gyro value, this depends on scale being set
00074                 vettore[i].yy = (float)gyroCount[1]*gRes; // - gyroBias[1];  
00075                 vettore[i].zz = (float)gyroCount[2]*gRes; // - gyroBias[2]; 
00076                 
00077                 //invio i dati sul CAN
00078                 can1.write(CANMessage(1337,reinterpret_cast<char*>(&vettore[i].x),sizeof(vettore[i].x)));
00079                 can1.write(CANMessage(1337,reinterpret_cast<char*>(&vettore[i].y),sizeof(vettore[i].y)));
00080                 can1.write(CANMessage(1337,reinterpret_cast<char*>(&vettore[i].z),sizeof(vettore[i].z)));
00081                 can1.write(CANMessage(1337,reinterpret_cast<char*>(&vettore[i].xx),sizeof(vettore[i].xx)));
00082                 can1.write(CANMessage(1337,reinterpret_cast<char*>(&vettore[i].yy),sizeof(vettore[i].yy)));
00083                 can1.write(CANMessage(1337,reinterpret_cast<char*>(&vettore[i].zz),sizeof(vettore[i].zz))); 
00084                              
00085             }  
00086    
00087             pc.printf("%03.0f %03.0f %03.0f %03.0f %03.0f %03.0f\n\r",100*vettore[i].x+off_set_a,100*vettore[i].y+off_set_a,100*vettore[i].z+off_set_a,100*vettore[i].xx+off_set_a,100*vettore[i].yy+off_set_a,100*vettore[i].zz+off_set_a);
00088             wait(0.02);
00089         } 
00090         //temp3.stop();
00091         //pc.printf("Tempo impiegato per l'acquisizione di 1000 elementi: %f\n\r",temp3.read());     
00092     }
00093 }
00094