MP3 PLAYER

Dependencies:   DebouncedInterrupt SDFileSystem SPI_TFT_ILI9341 ST_401_84MHZ TFT_fonts VS1053 mbed

Fork of MP3333 by FRA221_B18

Committer:
PKnevermind
Date:
Tue Dec 08 19:52:20 2015 +0000
Revision:
2:c4b198e96ded
....

Who changed what in which revision?

UserRevisionLine numberNew contents of line
PKnevermind 2:c4b198e96ded 1 /*****
PKnevermind 2:c4b198e96ded 2 Algorithm based on MPU-9250_Snowda program. It has been modified by Josué Olmeda Castelló for imasD Tecnología.
PKnevermind 2:c4b198e96ded 3
PKnevermind 2:c4b198e96ded 4 This algorithm calibrates and reads data from accelerometer, gyroscope, magnetometer and the
PKnevermind 2:c4b198e96ded 5 internal temperature sensor. The lecture is made each time has a new mesured value (both gyro and accel data).
PKnevermind 2:c4b198e96ded 6 A comunication with a computer is made using serial interface. The user can see the data measured with 1 second update rate.
PKnevermind 2:c4b198e96ded 7
PKnevermind 2:c4b198e96ded 8 This algorithm uses the STM32L152 development board and the MPU-9250 9-axis InvenSense movement sensor. The communication
PKnevermind 2:c4b198e96ded 9 between both devices is made through I2C serial interface.
PKnevermind 2:c4b198e96ded 10
PKnevermind 2:c4b198e96ded 11 AD0 should be connected to GND.
PKnevermind 2:c4b198e96ded 12
PKnevermind 2:c4b198e96ded 13 04/05/2015
PKnevermind 2:c4b198e96ded 14 *****/
PKnevermind 2:c4b198e96ded 15
PKnevermind 2:c4b198e96ded 16 #include "mbed.h"
PKnevermind 2:c4b198e96ded 17 #include "MPU9250.h"
PKnevermind 2:c4b198e96ded 18
PKnevermind 2:c4b198e96ded 19
PKnevermind 2:c4b198e96ded 20 Serial pc(SERIAL_TX, SERIAL_RX); // Huyperterminal default config: 9600 bauds, 8-bit data, 1 stop bit, no parity
PKnevermind 2:c4b198e96ded 21 MPU9250 mpu9250;
PKnevermind 2:c4b198e96ded 22 Timer t;
PKnevermind 2:c4b198e96ded 23 //DigitalOut myled(LED1);
PKnevermind 2:c4b198e96ded 24
PKnevermind 2:c4b198e96ded 25 float sum = 0;
PKnevermind 2:c4b198e96ded 26 uint32_t sumCount = 0;
PKnevermind 2:c4b198e96ded 27 char buffer[14];
PKnevermind 2:c4b198e96ded 28 uint8_t dato_leido[2];
PKnevermind 2:c4b198e96ded 29 uint8_t whoami;
PKnevermind 2:c4b198e96ded 30
PKnevermind 2:c4b198e96ded 31 int main() {
PKnevermind 2:c4b198e96ded 32
PKnevermind 2:c4b198e96ded 33 //___ Set up I2C: use fast (400 kHz) I2C ___
PKnevermind 2:c4b198e96ded 34 i2c.frequency(400000);
PKnevermind 2:c4b198e96ded 35
PKnevermind 2:c4b198e96ded 36 pc.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
PKnevermind 2:c4b198e96ded 37
PKnevermind 2:c4b198e96ded 38 t.start(); // Timer ON
PKnevermind 2:c4b198e96ded 39
PKnevermind 2:c4b198e96ded 40 // Read the WHO_AM_I register, this is a good test of communication
PKnevermind 2:c4b198e96ded 41 whoami = mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
PKnevermind 2:c4b198e96ded 42
PKnevermind 2:c4b198e96ded 43 pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x71\n\r");
PKnevermind 2:c4b198e96ded 44 if (I2Cstate != 0) // error on I2C
PKnevermind 2:c4b198e96ded 45 pc.printf("I2C failure while reading WHO_AM_I register");
PKnevermind 2:c4b198e96ded 46
PKnevermind 2:c4b198e96ded 47 if (whoami == 0x71) // WHO_AM_I should always be 0x71
PKnevermind 2:c4b198e96ded 48 {
PKnevermind 2:c4b198e96ded 49 pc.printf("MPU9250 WHO_AM_I is 0x%x\n\r", whoami);
PKnevermind 2:c4b198e96ded 50 pc.printf("MPU9250 is online...\n\r");
PKnevermind 2:c4b198e96ded 51 sprintf(buffer, "0x%x", whoami);
PKnevermind 2:c4b198e96ded 52 wait(1);
PKnevermind 2:c4b198e96ded 53
PKnevermind 2:c4b198e96ded 54 mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration
PKnevermind 2:c4b198e96ded 55
PKnevermind 2:c4b198e96ded 56 mpu9250.MPU9250SelfTest(SelfTest); // Start by performing self test and reporting values (accelerometer and gyroscope self test)
PKnevermind 2:c4b198e96ded 57 pc.printf("x-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[0]);
PKnevermind 2:c4b198e96ded 58 pc.printf("y-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[1]);
PKnevermind 2:c4b198e96ded 59 pc.printf("z-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[2]);
PKnevermind 2:c4b198e96ded 60 pc.printf("x-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[3]);
PKnevermind 2:c4b198e96ded 61 pc.printf("y-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[4]);
PKnevermind 2:c4b198e96ded 62 pc.printf("z-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[5]);
PKnevermind 2:c4b198e96ded 63
PKnevermind 2:c4b198e96ded 64 mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometer, load biases in bias registers
PKnevermind 2:c4b198e96ded 65 pc.printf("x gyro bias = %f\n\r", gyroBias[0]);
PKnevermind 2:c4b198e96ded 66 pc.printf("y gyro bias = %f\n\r", gyroBias[1]);
PKnevermind 2:c4b198e96ded 67 pc.printf("z gyro bias = %f\n\r", gyroBias[2]);
PKnevermind 2:c4b198e96ded 68 pc.printf("x accel bias = %f\n\r", accelBias[0]);
PKnevermind 2:c4b198e96ded 69 pc.printf("y accel bias = %f\n\r", accelBias[1]);
PKnevermind 2:c4b198e96ded 70 pc.printf("z accel bias = %f\n\r", accelBias[2]);
PKnevermind 2:c4b198e96ded 71 wait(2);
PKnevermind 2:c4b198e96ded 72
PKnevermind 2:c4b198e96ded 73 // Initialize device for active mode read of acclerometer, gyroscope, and temperature
PKnevermind 2:c4b198e96ded 74 mpu9250.initMPU9250();
PKnevermind 2:c4b198e96ded 75 pc.printf("MPU9250 initialized for active data mode....\n\r");
PKnevermind 2:c4b198e96ded 76
PKnevermind 2:c4b198e96ded 77 // Initialize device for active mode read of magnetometer, 16 bit resolution, 100Hz.
PKnevermind 2:c4b198e96ded 78 mpu9250.initAK8963(magCalibration);
PKnevermind 2:c4b198e96ded 79 pc.printf("AK8963 initialized for active data mode....\n\r");
PKnevermind 2:c4b198e96ded 80 pc.printf("Accelerometer full-scale range = %f g\n\r", 2.0f*(float)(1<<Ascale));
PKnevermind 2:c4b198e96ded 81 pc.printf("Gyroscope full-scale range = %f deg/s\n\r", 250.0f*(float)(1<<Gscale));
PKnevermind 2:c4b198e96ded 82 if(Mscale == 0) pc.printf("Magnetometer resolution = 14 bits\n\r");
PKnevermind 2:c4b198e96ded 83 if(Mscale == 1) pc.printf("Magnetometer resolution = 16 bits\n\r");
PKnevermind 2:c4b198e96ded 84 if(Mmode == 2) pc.printf("Magnetometer ODR = 8 Hz\n\r");
PKnevermind 2:c4b198e96ded 85 if(Mmode == 6) pc.printf("Magnetometer ODR = 100 Hz\n\r");
PKnevermind 2:c4b198e96ded 86 wait(1);
PKnevermind 2:c4b198e96ded 87 }
PKnevermind 2:c4b198e96ded 88
PKnevermind 2:c4b198e96ded 89 else // Connection failure
PKnevermind 2:c4b198e96ded 90 {
PKnevermind 2:c4b198e96ded 91 pc.printf("Could not connect to MPU9250: \n\r");
PKnevermind 2:c4b198e96ded 92 pc.printf("%#x \n", whoami);
PKnevermind 2:c4b198e96ded 93 sprintf(buffer, "WHO_AM_I 0x%x", whoami);
PKnevermind 2:c4b198e96ded 94 while(1) ; // Loop forever if communication doesn't happen
PKnevermind 2:c4b198e96ded 95 }
PKnevermind 2:c4b198e96ded 96
PKnevermind 2:c4b198e96ded 97 mpu9250.getAres(); // Get accelerometer sensitivity
PKnevermind 2:c4b198e96ded 98 mpu9250.getGres(); // Get gyro sensitivity
PKnevermind 2:c4b198e96ded 99 mpu9250.getMres(); // Get magnetometer sensitivity
PKnevermind 2:c4b198e96ded 100 pc.printf("Accelerometer sensitivity is %f LSB/g \n\r", 1.0f/aRes);
PKnevermind 2:c4b198e96ded 101 pc.printf("Gyroscope sensitivity is %f LSB/deg/s \n\r", 1.0f/gRes);
PKnevermind 2:c4b198e96ded 102 pc.printf("Magnetometer sensitivity is %f LSB/G \n\r", 1.0f/mRes);
PKnevermind 2:c4b198e96ded 103 magbias[0] = +470.; // User environmental x-axis correction in milliGauss, should be automatically calculated
PKnevermind 2:c4b198e96ded 104 magbias[1] = +120.; // User environmental x-axis correction in milliGauss
PKnevermind 2:c4b198e96ded 105 magbias[2] = +125.; // User environmental x-axis correction in milliGauss
PKnevermind 2:c4b198e96ded 106
PKnevermind 2:c4b198e96ded 107 while(1) {
PKnevermind 2:c4b198e96ded 108
PKnevermind 2:c4b198e96ded 109 // If intPin goes high, all data registers have new data
PKnevermind 2:c4b198e96ded 110 if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) { // On interrupt, check if data ready interrupt
PKnevermind 2:c4b198e96ded 111
PKnevermind 2:c4b198e96ded 112 mpu9250.readAccelData(accelCount); // Read the x/y/z adc values
PKnevermind 2:c4b198e96ded 113 // Now we'll calculate the accleration value into actual g's
PKnevermind 2:c4b198e96ded 114 if (I2Cstate != 0) //error on I2C
PKnevermind 2:c4b198e96ded 115 pc.printf("I2C error ocurred while reading accelerometer data. I2Cstate = %d \n\r", I2Cstate);
PKnevermind 2:c4b198e96ded 116 else{ // I2C read or write ok
PKnevermind 2:c4b198e96ded 117 I2Cstate = 1;
PKnevermind 2:c4b198e96ded 118 ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set
PKnevermind 2:c4b198e96ded 119 ay = (float)accelCount[1]*aRes - accelBias[1];
PKnevermind 2:c4b198e96ded 120 az = (float)accelCount[2]*aRes - accelBias[2];
PKnevermind 2:c4b198e96ded 121 }
PKnevermind 2:c4b198e96ded 122
PKnevermind 2:c4b198e96ded 123 mpu9250.readGyroData(gyroCount); // Read the x/y/z adc values
PKnevermind 2:c4b198e96ded 124 // Calculate the gyro value into actual degrees per second
PKnevermind 2:c4b198e96ded 125 if (I2Cstate != 0) //error on I2C
PKnevermind 2:c4b198e96ded 126 pc.printf("I2C error ocurred while reading gyrometer data. I2Cstate = %d \n\r", I2Cstate);
PKnevermind 2:c4b198e96ded 127 else{ // I2C read or write ok
PKnevermind 2:c4b198e96ded 128 I2Cstate = 1;
PKnevermind 2:c4b198e96ded 129 gx = (float)gyroCount[0]*gRes - gyroBias[0]; // get actual gyro value, this depends on scale being set
PKnevermind 2:c4b198e96ded 130 gy = (float)gyroCount[1]*gRes - gyroBias[1];
PKnevermind 2:c4b198e96ded 131 gz = (float)gyroCount[2]*gRes - gyroBias[2];
PKnevermind 2:c4b198e96ded 132 }
PKnevermind 2:c4b198e96ded 133
PKnevermind 2:c4b198e96ded 134 mpu9250.readMagData(magCount); // Read the x/y/z adc values
PKnevermind 2:c4b198e96ded 135 // Calculate the magnetometer values in milliGauss
PKnevermind 2:c4b198e96ded 136 // Include factory calibration per data sheet and user environmental corrections
PKnevermind 2:c4b198e96ded 137 if (I2Cstate != 0) //error on I2C
PKnevermind 2:c4b198e96ded 138 pc.printf("I2C error ocurred while reading magnetometer data. I2Cstate = %d \n\r", I2Cstate);
PKnevermind 2:c4b198e96ded 139 else{ // I2C read or write ok
PKnevermind 2:c4b198e96ded 140 I2Cstate = 1;
PKnevermind 2:c4b198e96ded 141 mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]; // get actual magnetometer value, this depends on scale being set
PKnevermind 2:c4b198e96ded 142 my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1];
PKnevermind 2:c4b198e96ded 143 mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2];
PKnevermind 2:c4b198e96ded 144 }
PKnevermind 2:c4b198e96ded 145
PKnevermind 2:c4b198e96ded 146 mpu9250.getCompassOrientation(orientation);
PKnevermind 2:c4b198e96ded 147 }
PKnevermind 2:c4b198e96ded 148
PKnevermind 2:c4b198e96ded 149 Now = t.read_us();
PKnevermind 2:c4b198e96ded 150 deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
PKnevermind 2:c4b198e96ded 151 lastUpdate = Now;
PKnevermind 2:c4b198e96ded 152 sum += deltat;
PKnevermind 2:c4b198e96ded 153 sumCount++;
PKnevermind 2:c4b198e96ded 154
PKnevermind 2:c4b198e96ded 155 // Pass gyro rate as rad/s
PKnevermind 2:c4b198e96ded 156 // mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);
PKnevermind 2:c4b198e96ded 157 mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);
PKnevermind 2:c4b198e96ded 158
PKnevermind 2:c4b198e96ded 159
PKnevermind 2:c4b198e96ded 160 // Serial print and/or display at 1.5 s rate independent of data rates
PKnevermind 2:c4b198e96ded 161 delt_t = t.read_ms() - count;
PKnevermind 2:c4b198e96ded 162 if (delt_t > 1500) { // update LCD once per half-second independent of read rate
PKnevermind 2:c4b198e96ded 163 pc.printf("ax = %f", 1000*ax);
PKnevermind 2:c4b198e96ded 164 pc.printf(" ay = %f", 1000*ay);
PKnevermind 2:c4b198e96ded 165 pc.printf(" az = %f mg\n\r", 1000*az);
PKnevermind 2:c4b198e96ded 166 pc.printf("gx = %f", gx);
PKnevermind 2:c4b198e96ded 167 pc.printf(" gy = %f", gy);
PKnevermind 2:c4b198e96ded 168 pc.printf(" gz = %f deg/s\n\r", gz);
PKnevermind 2:c4b198e96ded 169 pc.printf("mx = %f", mx);
PKnevermind 2:c4b198e96ded 170 pc.printf(" my = %f", my);
PKnevermind 2:c4b198e96ded 171 pc.printf(" mz = %f mG\n\r", mz);
PKnevermind 2:c4b198e96ded 172
PKnevermind 2:c4b198e96ded 173
PKnevermind 2:c4b198e96ded 174 tempCount = mpu9250.readTempData(); // Read the adc values
PKnevermind 2:c4b198e96ded 175 if (I2Cstate != 0) //error on I2C
PKnevermind 2:c4b198e96ded 176 pc.printf("I2C error ocurred while reading sensor temp. I2Cstate = %d \n\r", I2Cstate);
PKnevermind 2:c4b198e96ded 177 else{ // I2C read or write ok
PKnevermind 2:c4b198e96ded 178 I2Cstate = 1;
PKnevermind 2:c4b198e96ded 179 temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade
PKnevermind 2:c4b198e96ded 180 pc.printf(" temperature = %f C\n\r", temperature);
PKnevermind 2:c4b198e96ded 181 }
PKnevermind 2:c4b198e96ded 182 pc.printf("q0 = %f\n\r", q[0]);
PKnevermind 2:c4b198e96ded 183 pc.printf("q1 = %f\n\r", q[1]);
PKnevermind 2:c4b198e96ded 184 pc.printf("q2 = %f\n\r", q[2]);
PKnevermind 2:c4b198e96ded 185 pc.printf("q3 = %f\n\r", q[3]);
PKnevermind 2:c4b198e96ded 186
PKnevermind 2:c4b198e96ded 187 pc.printf("Compass orientation: %f\n", orientation[0]);
PKnevermind 2:c4b198e96ded 188
PKnevermind 2:c4b198e96ded 189
PKnevermind 2:c4b198e96ded 190
PKnevermind 2:c4b198e96ded 191
PKnevermind 2:c4b198e96ded 192 // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation.
PKnevermind 2:c4b198e96ded 193 // In this coordinate system, the positive z-axis is down toward Earth.
PKnevermind 2:c4b198e96ded 194 // 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.
PKnevermind 2:c4b198e96ded 195 // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative.
PKnevermind 2:c4b198e96ded 196 // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
PKnevermind 2:c4b198e96ded 197 // These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
PKnevermind 2:c4b198e96ded 198 // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be
PKnevermind 2:c4b198e96ded 199 // applied in the correct order which for this configuration is yaw, pitch, and then roll.
PKnevermind 2:c4b198e96ded 200 // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
PKnevermind 2:c4b198e96ded 201
PKnevermind 2:c4b198e96ded 202 yaw = atan2(2.0f * (q[1] * q[2] + q[0] * q[3]), q[0] * q[0] + q[1] * q[1] - q[2] * q[2] - q[3] * q[3]);
PKnevermind 2:c4b198e96ded 203 pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
PKnevermind 2:c4b198e96ded 204 roll = atan2(2.0f * (q[0] * q[1] + q[2] * q[3]), q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3]);
PKnevermind 2:c4b198e96ded 205 pitch *= 180.0f / PI;
PKnevermind 2:c4b198e96ded 206 yaw *= 180.0f / PI;
PKnevermind 2:c4b198e96ded 207 yaw -= 13.8f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04
PKnevermind 2:c4b198e96ded 208 roll *= 180.0f / PI;
PKnevermind 2:c4b198e96ded 209
PKnevermind 2:c4b198e96ded 210 /*
PKnevermind 2:c4b198e96ded 211 pc.printf("Yaw, Pitch, Roll: %f %f %f\n\r", yaw, pitch, roll);
PKnevermind 2:c4b198e96ded 212 pc.printf("average rate = %f\n\r", (float) sumCount/sum);
PKnevermind 2:c4b198e96ded 213 */
PKnevermind 2:c4b198e96ded 214
PKnevermind 2:c4b198e96ded 215
PKnevermind 2:c4b198e96ded 216 myled= !myled;
PKnevermind 2:c4b198e96ded 217 count = t.read_ms();
PKnevermind 2:c4b198e96ded 218
PKnevermind 2:c4b198e96ded 219 if(count > 1<<21) {
PKnevermind 2:c4b198e96ded 220 t.start(); // start the timer over again if ~30 minutes has passed
PKnevermind 2:c4b198e96ded 221 count = 0;
PKnevermind 2:c4b198e96ded 222 deltat= 0;
PKnevermind 2:c4b198e96ded 223 lastUpdate = t.read_us();
PKnevermind 2:c4b198e96ded 224 }
PKnevermind 2:c4b198e96ded 225 sum = 0;
PKnevermind 2:c4b198e96ded 226 sumCount = 0;
PKnevermind 2:c4b198e96ded 227 }
PKnevermind 2:c4b198e96ded 228 }
PKnevermind 2:c4b198e96ded 229 }