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

Committer:
rattokiller
Date:
Sun Nov 05 14:20:26 2017 +0000
Revision:
0:a9753886e1e0
librerie utili

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rattokiller 0:a9753886e1e0 1
rattokiller 0:a9753886e1e0 2 void calcola_dati(){
rattokiller 0:a9753886e1e0 3 while(true){
rattokiller 0:a9753886e1e0 4 wait_us(20);
rattokiller 0:a9753886e1e0 5 // If data ready bit set, all data registers have new data
rattokiller 0:a9753886e1e0 6 if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) { // check if data ready interrupt
rattokiller 0:a9753886e1e0 7 mpu6050.readAccelData(accelCount); // Read the x/y/z adc values
rattokiller 0:a9753886e1e0 8 mpu6050.getAres();
rattokiller 0:a9753886e1e0 9
rattokiller 0:a9753886e1e0 10 // Now we'll calculate the accleration value into actual g's
rattokiller 0:a9753886e1e0 11 ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set
rattokiller 0:a9753886e1e0 12 ay = (float)accelCount[1]*aRes - accelBias[1];
rattokiller 0:a9753886e1e0 13 az = (float)accelCount[2]*aRes - accelBias[2];
rattokiller 0:a9753886e1e0 14
rattokiller 0:a9753886e1e0 15 mpu6050.readGyroData(gyroCount); // Read the x/y/z adc values
rattokiller 0:a9753886e1e0 16 mpu6050.getGres();
rattokiller 0:a9753886e1e0 17
rattokiller 0:a9753886e1e0 18 // Calculate the gyro value into actual degrees per second
rattokiller 0:a9753886e1e0 19 gx = (float)gyroCount[0]*gRes; // - gyroBias[0]; // get actual gyro value, this depends on scale being set
rattokiller 0:a9753886e1e0 20 gy = (float)gyroCount[1]*gRes; // - gyroBias[1];
rattokiller 0:a9753886e1e0 21 gz = (float)gyroCount[2]*gRes; // - gyroBias[2];
rattokiller 0:a9753886e1e0 22
rattokiller 0:a9753886e1e0 23 tempCount = mpu6050.readTempData(); // Read the x/y/z adc values
rattokiller 0:a9753886e1e0 24 temperature = (tempCount) / 340. + 36.53; // Temperature in degrees Centigrade
rattokiller 0:a9753886e1e0 25 }
rattokiller 0:a9753886e1e0 26
rattokiller 0:a9753886e1e0 27 Now = t.read_us();
rattokiller 0:a9753886e1e0 28 deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
rattokiller 0:a9753886e1e0 29 lastUpdate = Now;
rattokiller 0:a9753886e1e0 30
rattokiller 0:a9753886e1e0 31 sum += deltat;
rattokiller 0:a9753886e1e0 32 sumCount++;
rattokiller 0:a9753886e1e0 33
rattokiller 0:a9753886e1e0 34 if(lastUpdate - firstUpdate > 10000000.0f) {
rattokiller 0:a9753886e1e0 35 beta = 0.04; // decrease filter gain after stabilized
rattokiller 0:a9753886e1e0 36 zeta = 0.015; // increasey bias drift gain after stabilized
rattokiller 0:a9753886e1e0 37 }
rattokiller 0:a9753886e1e0 38
rattokiller 0:a9753886e1e0 39 // Pass gyro rate as rad/s
rattokiller 0:a9753886e1e0 40 gx=(int)gx;gy=(int)gy;gz=(int)gz;
rattokiller 0:a9753886e1e0 41
rattokiller 0:a9753886e1e0 42 ax=((int)10000*ax)/10000;
rattokiller 0:a9753886e1e0 43 ay=((int)10000*ay)/10000;
rattokiller 0:a9753886e1e0 44 az=((int)10000*az)/10000;
rattokiller 0:a9753886e1e0 45
rattokiller 0:a9753886e1e0 46 q[0]=((int)10000*q[0])/10000;
rattokiller 0:a9753886e1e0 47 q[1]=((int)10000*q[1])/10000;
rattokiller 0:a9753886e1e0 48 q[2]=((int)10000*q[2])/10000;
rattokiller 0:a9753886e1e0 49 q[3]=((int)10000*q[3])/10000;
rattokiller 0:a9753886e1e0 50
rattokiller 0:a9753886e1e0 51 mpu6050.MadgwickQuaternionUpdate(ax, ay,az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f);
rattokiller 0:a9753886e1e0 52
rattokiller 0:a9753886e1e0 53 // Serial print and/or display at 0.5 s rate independent of data rates
rattokiller 0:a9753886e1e0 54 delt_t = t.read_ms() - count;
rattokiller 0:a9753886e1e0 55 if (delt_t > 500) { // update LCD once per half-second independent of read rate
rattokiller 0:a9753886e1e0 56 #if false
rattokiller 0:a9753886e1e0 57 pc.printf("\tax = %6.1f", 1000*ax);
rattokiller 0:a9753886e1e0 58 pc.printf(" ay = %6.1f", 1000*ay);
rattokiller 0:a9753886e1e0 59 pc.printf(" az = %6.1f mg\t\t", 1000*az);
rattokiller 0:a9753886e1e0 60
rattokiller 0:a9753886e1e0 61 pc.printf("gx = %6.1f", gx);
rattokiller 0:a9753886e1e0 62 pc.printf(" gy = %6.1f", gy);
rattokiller 0:a9753886e1e0 63 pc.printf(" gz = %6.1f deg/s\t\t\t", gz);
rattokiller 0:a9753886e1e0 64 pc.printf("\t\t temperature = %f C\n\r", temperature);
rattokiller 0:a9753886e1e0 65
rattokiller 0:a9753886e1e0 66
rattokiller 0:a9753886e1e0 67 // pc.printf("q0 = %f\tq1 = %f\tq2 = %f\tq3 = %f\n\r", q[0],q[1],q[2],q[3]);
rattokiller 0:a9753886e1e0 68 #endif
rattokiller 0:a9753886e1e0 69 // pc.printf("q1 = %f\n\r", q[1]); pc.printf("q2 = %f\n\r", q[2]); pc.printf("q3 = %f\n\r", q[3]);
rattokiller 0:a9753886e1e0 70
rattokiller 0:a9753886e1e0 71
rattokiller 0:a9753886e1e0 72
rattokiller 0:a9753886e1e0 73
rattokiller 0:a9753886e1e0 74
rattokiller 0:a9753886e1e0 75 // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation.
rattokiller 0:a9753886e1e0 76 // In this coordinate system, the positive z-axis is down toward Earth.
rattokiller 0:a9753886e1e0 77 // 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.
rattokiller 0:a9753886e1e0 78 // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative.
rattokiller 0:a9753886e1e0 79 // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
rattokiller 0:a9753886e1e0 80 // These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
rattokiller 0:a9753886e1e0 81 // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be
rattokiller 0:a9753886e1e0 82 // applied in the correct order which for this configuration is yaw, pitch, and then roll.
rattokiller 0:a9753886e1e0 83 // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
rattokiller 0:a9753886e1e0 84
rattokiller 0:a9753886e1e0 85 //sbagliato -> da fare tutto da capo. usare solo l' accelerometro per pich e rol, lo yaw non serve.
rattokiller 0:a9753886e1e0 86
rattokiller 0:a9753886e1e0 87 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
rattokiller 0:a9753886e1e0 88 pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
rattokiller 0:a9753886e1e0 89 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]));
rattokiller 0:a9753886e1e0 90 pitch *= 180.0f / PI;
rattokiller 0:a9753886e1e0 91 yaw *= 180.0f / PI;
rattokiller 0:a9753886e1e0 92 roll *= 180.0f / PI;
rattokiller 0:a9753886e1e0 93
rattokiller 0:a9753886e1e0 94
rattokiller 0:a9753886e1e0 95 // pc.printf("Yaw, Pitch, Roll: %.2f %.2f %.2f", yaw, pitch, roll);
rattokiller 0:a9753886e1e0 96 // pc.printf("\taverage rate = %f\n\r", (float) sumCount/sum);
rattokiller 0:a9753886e1e0 97
rattokiller 0:a9753886e1e0 98
rattokiller 0:a9753886e1e0 99 count = t.read_ms();
rattokiller 0:a9753886e1e0 100 sum = 0;
rattokiller 0:a9753886e1e0 101 sumCount = 0;
rattokiller 0:a9753886e1e0 102 }
rattokiller 0:a9753886e1e0 103 }
rattokiller 0:a9753886e1e0 104 }