main_imu, MPU6050 , racolta_dati sono per il funzionamento dell' accelerometro. my_img_sd è una libreria per gestire i dati su un sd sulla quale vengono forniti solamente le funzioni di lettura e scrittura a blocchi i file trasmetti sono la definizione e implementazione delle funzioni del protoccolo per la gestione dell' invio dei dati con il relativo formato

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers racolta_dati.h Source File

racolta_dati.h

00001 
00002 void calcola_dati(){
00003     while(true){
00004     wait_us(20);
00005   // If data ready bit set, all data registers have new data
00006   if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) {  // check if data ready interrupt
00007     mpu6050.readAccelData(accelCount);  // Read the x/y/z adc values
00008     mpu6050.getAres();
00009     
00010     // Now we'll calculate the accleration value into actual g's
00011     ax = (float)accelCount[0]*aRes - accelBias[0];  // get actual g value, this depends on scale being set
00012     ay = (float)accelCount[1]*aRes - accelBias[1];   
00013     az = (float)accelCount[2]*aRes - accelBias[2];  
00014    
00015     mpu6050.readGyroData(gyroCount);  // Read the x/y/z adc values
00016     mpu6050.getGres();
00017  
00018     // Calculate the gyro value into actual degrees per second
00019     gx = (float)gyroCount[0]*gRes; // - gyroBias[0];  // get actual gyro value, this depends on scale being set
00020     gy = (float)gyroCount[1]*gRes; // - gyroBias[1];  
00021     gz = (float)gyroCount[2]*gRes; // - gyroBias[2];   
00022 
00023     tempCount = mpu6050.readTempData();  // Read the x/y/z adc values
00024     temperature = (tempCount) / 340. + 36.53; // Temperature in degrees Centigrade
00025    }  
00026    
00027     Now = t.read_us();
00028     deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
00029     lastUpdate = Now;
00030     
00031     sum += deltat;
00032     sumCount++;
00033     
00034     if(lastUpdate - firstUpdate > 10000000.0f) {
00035      beta = 0.04;  // decrease filter gain after stabilized
00036      zeta = 0.015; // increasey bias drift gain after stabilized
00037     }
00038     
00039    // Pass gyro rate as rad/s
00040   gx=(int)gx;gy=(int)gy;gz=(int)gz;
00041   
00042   ax=((int)10000*ax)/10000;
00043   ay=((int)10000*ay)/10000;
00044   az=((int)10000*az)/10000;
00045   
00046   q[0]=((int)10000*q[0])/10000;
00047     q[1]=((int)10000*q[1])/10000;
00048   q[2]=((int)10000*q[2])/10000;
00049   q[3]=((int)10000*q[3])/10000;
00050    
00051     mpu6050.MadgwickQuaternionUpdate(ax, ay,az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f);
00052 
00053     // Serial print and/or display at 0.5 s rate independent of data rates
00054     delt_t = t.read_ms() - count;
00055     if (delt_t > 500) { // update LCD once per half-second independent of read rate
00056 #if false
00057     pc.printf("\tax = %6.1f", 1000*ax); 
00058     pc.printf(" ay = %6.1f", 1000*ay); 
00059     pc.printf(" az = %6.1f  mg\t\t", 1000*az); 
00060 
00061     pc.printf("gx = %6.1f", gx); 
00062     pc.printf(" gy = %6.1f", gy); 
00063     pc.printf(" gz = %6.1f  deg/s\t\t\t", gz); 
00064      pc.printf("\t\t temperature = %f  C\n\r", temperature);     
00065 
00066     
00067  //   pc.printf("q0 = %f\tq1 = %f\tq2 = %f\tq3 = %f\n\r", q[0],q[1],q[2],q[3]);
00068  #endif
00069  //   pc.printf("q1 = %f\n\r", q[1]); pc.printf("q2 = %f\n\r", q[2]); pc.printf("q3 = %f\n\r", q[3]);      
00070     
00071 
00072      
00073 
00074     
00075   // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation.
00076   // In this coordinate system, the positive z-axis is down toward Earth. 
00077   // Yaw is the angle between Sensor x-axis and Earth magnetic North (or true North if corrected for local declination, looking down on the sensor positive yaw is counterclockwise.
00078   // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative.
00079   // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
00080   // These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
00081   // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be
00082   // applied in the correct order which for this configuration is yaw, pitch, and then roll.
00083   // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
00084   
00085   //sbagliato -> da fare tutto da capo. usare solo l' accelerometro per pich e rol, lo yaw non serve.
00086   
00087     yaw   = atan2(2.0f * (q[1] * q[2] + q[0] * q[3]),2.0f *(q[0] * q[0] + q[1] * q[1] - q[2] * q[2] - q[3] * q[3]));   //<--- quel coglione ha sbagliato a scrive l' equazione con i quaternioni
00088     pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
00089     roll  = atan2(2.0f * (q[0] * q[1] + q[2] * q[3]),2.0f* (q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3]));
00090     pitch *= 180.0f / PI;
00091     yaw   *= 180.0f / PI; 
00092     roll  *= 180.0f / PI;
00093     
00094 
00095  // pc.printf("Yaw, Pitch, Roll: %.2f %.2f %.2f", yaw, pitch, roll);
00096    //  pc.printf("\taverage rate = %f\n\r", (float) sumCount/sum);
00097       
00098   
00099     count = t.read_ms(); 
00100     sum = 0;
00101     sumCount = 0; 
00102     }
00103     }
00104 }