thanks to Zoltan Hudak publish the way to use stm32f103c8t6 on mbed. now you can use it with MPC4725 DAC

Dependencies:   mbed-STM32F103C8T6 mbed

Fork of Wii_IRCam_Test by Michael Shimniok

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "stm32f103c8t6.h"
00002 #include "mbed.h"
00003 #include "MPU9250.h"
00004 
00005 float sum = 0;
00006 uint32_t sumCount = 0;
00007 char buffer[14];
00008 
00009 MPU9250 mpu9250;
00010 
00011 Timer t;
00012 
00013 int main()
00014 {
00015 
00016     confSysClock();
00017     Serial pc(PA_2, PA_3);//pc(USBTX, USBRX); // tx, rx
00018     pc.baud(115200);
00019 
00020     //Set up I2C
00021     i2c.frequency(400000);  // use fast (400 kHz) I2C
00022 
00023     pc.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
00024 
00025     t.start();
00026 
00027     // Read the WHO_AM_I register, this is a good test of communication
00028     uint8_t whoami = mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);  // Read WHO_AM_I register for MPU-9250
00029     pc.printf("I AM 0x%x\n\r", whoami);
00030     pc.printf("I SHOULD BE 0x71\n\r");
00031 
00032     if (whoami == 0x73) { // WHO_AM_I should always be 0x68
00033         pc.printf("MPU9250 WHO_AM_I is 0x%x\n\r", whoami);
00034         pc.printf("MPU9250 is online...\n\r");
00035         sprintf(buffer, "0x%x", whoami);
00036         wait(1);
00037 
00038         mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration
00039         mpu9250.MPU9250SelfTest(SelfTest); // Start by performing self test and reporting values
00040         pc.printf("x-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[0]);
00041         pc.printf("y-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[1]);
00042         pc.printf("z-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[2]);
00043         pc.printf("x-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[3]);
00044         pc.printf("y-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[4]);
00045         pc.printf("z-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[5]);
00046         mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers
00047         pc.printf("x gyro bias = %f\n\r", gyroBias[0]);
00048         pc.printf("y gyro bias = %f\n\r", gyroBias[1]);
00049         pc.printf("z gyro bias = %f\n\r", gyroBias[2]);
00050         pc.printf("x accel bias = %f\n\r", accelBias[0]);
00051         pc.printf("y accel bias = %f\n\r", accelBias[1]);
00052         pc.printf("z accel bias = %f\n\r", accelBias[2]);
00053         wait(2);
00054 
00055         mpu9250.initMPU9250();
00056         pc.printf("MPU9250 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature
00057         mpu9250.initAK8963(magCalibration);
00058         pc.printf("AK8963 initialized for active data mode....\n\r"); // Initialize device for active mode read of magnetometer
00059         pc.printf("Accelerometer full-scale range = %f  g\n\r", 2.0f*(float)(1<<Ascale));
00060         pc.printf("Gyroscope full-scale range = %f  deg/s\n\r", 250.0f*(float)(1<<Gscale));
00061         if(Mscale == 0) pc.printf("Magnetometer resolution = 14  bits\n\r");
00062         if(Mscale == 1) pc.printf("Magnetometer resolution = 16  bits\n\r");
00063         if(Mmode == 2) pc.printf("Magnetometer ODR = 8 Hz\n\r");
00064         if(Mmode == 6) pc.printf("Magnetometer ODR = 100 Hz\n\r");
00065         wait(1);
00066     } else {
00067         pc.printf("Could not connect to MPU9250: \n\r");
00068         pc.printf("%#x \n",  whoami);
00069 
00070         sprintf(buffer, "WHO_AM_I 0x%x", whoami);
00071         while(1) ; // Loop forever if communication doesn't happen
00072     }
00073     mpu9250.getAres(); // Get accelerometer sensitivity
00074     mpu9250.getGres(); // Get gyro sensitivity
00075     mpu9250.getMres(); // Get magnetometer sensitivity
00076     pc.printf("Accelerometer sensitivity is %f LSB/g \n\r", 1.0f/aRes);
00077     pc.printf("Gyroscope sensitivity is %f LSB/deg/s \n\r", 1.0f/gRes);
00078     pc.printf("Magnetometer sensitivity is %f LSB/G \n\r", 1.0f/mRes);
00079     magbias[0] = +470.;  // User environmental x-axis correction in milliGauss, should be automatically calculated
00080     magbias[1] = +120.;  // User environmental x-axis correction in milliGauss
00081     magbias[2] = +125.;  // User environmental x-axis correction in milliGauss
00082 
00083 
00084     while(1) {
00085 
00086         // If intPin goes high, all data registers have new data
00087         if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) {  // On interrupt, check if data ready interrupt
00088 
00089             mpu9250.readAccelData(accelCount);  // Read the x/y/z adc values
00090             // Now we'll calculate the accleration value into actual g's
00091             ax = (float)accelCount[0]*aRes - accelBias[0];  // get actual g value, this depends on scale being set
00092             ay = (float)accelCount[1]*aRes - accelBias[1];
00093             az = (float)accelCount[2]*aRes - accelBias[2];
00094 
00095             mpu9250.readGyroData(gyroCount);  // Read the x/y/z adc values
00096             // Calculate the gyro value into actual degrees per second
00097             gx = (float)gyroCount[0]*gRes - gyroBias[0];  // get actual gyro value, this depends on scale being set
00098             gy = (float)gyroCount[1]*gRes - gyroBias[1];
00099             gz = (float)gyroCount[2]*gRes - gyroBias[2];
00100 
00101             mpu9250.readMagData(magCount);  // Read the x/y/z adc values
00102             // Calculate the magnetometer values in milliGauss
00103             // Include factory calibration per data sheet and user environmental corrections
00104             mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0];  // get actual magnetometer value, this depends on scale being set
00105             my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1];
00106             mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2];
00107         }
00108 
00109         Now = t.read_us();
00110         deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
00111         lastUpdate = Now;
00112 
00113         sum += deltat;
00114         sumCount++;
00115 
00116 //    if(lastUpdate - firstUpdate > 10000000.0f) {
00117 //     beta = 0.04;  // decrease filter gain after stabilized
00118 //     zeta = 0.015; // increasey bias drift gain after stabilized
00119 //   }
00120 
00121         // Pass gyro rate as rad/s
00122 //  mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f,  my,  mx, mz);
00123         mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);
00124 
00125         // Serial print and/or display at 0.5 s rate independent of data rates
00126         delt_t = t.read_ms() - count;
00127         if (delt_t > 500) { // update LCD once per half-second independent of read rate
00128 
00129             pc.printf("ax = %f", 1000*ax);
00130             pc.printf(" ay = %f", 1000*ay);
00131             pc.printf(" az = %f  mg\n\r", 1000*az);
00132 
00133             pc.printf("gx = %f", gx);
00134             pc.printf(" gy = %f", gy);
00135             pc.printf(" gz = %f  deg/s\n\r", gz);
00136 
00137             pc.printf("gx = %f", mx);
00138             pc.printf(" gy = %f", my);
00139             pc.printf(" gz = %f  mG\n\r", mz);
00140 
00141             tempCount = mpu9250.readTempData();  // Read the adc values
00142             temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade
00143             pc.printf(" temperature = %f  C\n\r", temperature);
00144 
00145             pc.printf("q0 = %f\n\r", q[0]);
00146             pc.printf("q1 = %f\n\r", q[1]);
00147             pc.printf("q2 = %f\n\r", q[2]);
00148             pc.printf("q3 = %f\n\r", q[3]);
00149 
00150             /*    lcd.clear();
00151                 lcd.printString("MPU9250", 0, 0);
00152                 lcd.printString("x   y   z", 0, 1);
00153                 sprintf(buffer, "%d %d %d mg", (int)(1000.0f*ax), (int)(1000.0f*ay), (int)(1000.0f*az));
00154                 lcd.printString(buffer, 0, 2);
00155                 sprintf(buffer, "%d %d %d deg/s", (int)gx, (int)gy, (int)gz);
00156                 lcd.printString(buffer, 0, 3);
00157                 sprintf(buffer, "%d %d %d mG", (int)mx, (int)my, (int)mz);
00158                 lcd.printString(buffer, 0, 4);
00159              */
00160             // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation.
00161             // In this coordinate system, the positive z-axis is down toward Earth.
00162             // 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.
00163             // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative.
00164             // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
00165             // These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
00166             // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be
00167             // applied in the correct order which for this configuration is yaw, pitch, and then roll.
00168             // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
00169             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]);
00170             pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
00171             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]);
00172             pitch *= 180.0f / PI;
00173             yaw   *= 180.0f / PI;
00174             yaw   -= 13.8f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04
00175             roll  *= 180.0f / PI;
00176 
00177             pc.printf("Yaw, Pitch, Roll: %f %f %f\n\r", yaw, pitch, roll);
00178             pc.printf("average rate = %f\n\r", (float) sumCount/sum);
00179 //    sprintf(buffer, "YPR: %f %f %f", yaw, pitch, roll);
00180 //    lcd.printString(buffer, 0, 4);
00181 //    sprintf(buffer, "rate = %f", (float) sumCount/sum);
00182 //    lcd.printString(buffer, 0, 5);
00183 
00184             myled= !myled;
00185             count = t.read_ms();
00186 
00187             if(count > 1<<21) {
00188                 t.start(); // start the timer over again if ~30 minutes has passed
00189                 count = 0;
00190                 deltat= 0;
00191                 lastUpdate = t.read_us();
00192             }
00193             sum = 0;
00194             sumCount = 0;
00195         }
00196     }
00197 }