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
main.cpp@3:37fb1e2aacf3, 2017-05-25 (annotated)
- Committer:
- Zeran
- Date:
- Thu May 25 16:49:26 2017 +0000
- Revision:
- 3:37fb1e2aacf3
- Parent:
- 2:2b68d1d14aca
STM32f103c8t6 work with GY91(mpu9250 only on bmp280) Example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Zeran | 2:2b68d1d14aca | 1 | #include "stm32f103c8t6.h" |
shimniok | 0:cf1bc7c313b4 | 2 | #include "mbed.h" |
Zeran | 3:37fb1e2aacf3 | 3 | #include "MPU9250.h" |
shimniok | 0:cf1bc7c313b4 | 4 | |
Zeran | 3:37fb1e2aacf3 | 5 | float sum = 0; |
Zeran | 3:37fb1e2aacf3 | 6 | uint32_t sumCount = 0; |
Zeran | 3:37fb1e2aacf3 | 7 | char buffer[14]; |
shimniok | 0:cf1bc7c313b4 | 8 | |
Zeran | 3:37fb1e2aacf3 | 9 | MPU9250 mpu9250; |
shimniok | 0:cf1bc7c313b4 | 10 | |
Zeran | 3:37fb1e2aacf3 | 11 | Timer t; |
Zeran | 2:2b68d1d14aca | 12 | |
Zeran | 2:2b68d1d14aca | 13 | int main() |
shimniok | 0:cf1bc7c313b4 | 14 | { |
Zeran | 3:37fb1e2aacf3 | 15 | |
Zeran | 2:2b68d1d14aca | 16 | confSysClock(); |
Zeran | 3:37fb1e2aacf3 | 17 | Serial pc(PA_2, PA_3);//pc(USBTX, USBRX); // tx, rx |
shimniok | 0:cf1bc7c313b4 | 18 | pc.baud(115200); |
Zeran | 3:37fb1e2aacf3 | 19 | |
Zeran | 3:37fb1e2aacf3 | 20 | //Set up I2C |
Zeran | 3:37fb1e2aacf3 | 21 | i2c.frequency(400000); // use fast (400 kHz) I2C |
Zeran | 3:37fb1e2aacf3 | 22 | |
Zeran | 3:37fb1e2aacf3 | 23 | pc.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock); |
Zeran | 3:37fb1e2aacf3 | 24 | |
Zeran | 3:37fb1e2aacf3 | 25 | t.start(); |
Zeran | 3:37fb1e2aacf3 | 26 | |
Zeran | 3:37fb1e2aacf3 | 27 | // Read the WHO_AM_I register, this is a good test of communication |
Zeran | 3:37fb1e2aacf3 | 28 | uint8_t whoami = mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); // Read WHO_AM_I register for MPU-9250 |
Zeran | 3:37fb1e2aacf3 | 29 | pc.printf("I AM 0x%x\n\r", whoami); |
Zeran | 3:37fb1e2aacf3 | 30 | pc.printf("I SHOULD BE 0x71\n\r"); |
Zeran | 3:37fb1e2aacf3 | 31 | |
Zeran | 3:37fb1e2aacf3 | 32 | if (whoami == 0x73) { // WHO_AM_I should always be 0x68 |
Zeran | 3:37fb1e2aacf3 | 33 | pc.printf("MPU9250 WHO_AM_I is 0x%x\n\r", whoami); |
Zeran | 3:37fb1e2aacf3 | 34 | pc.printf("MPU9250 is online...\n\r"); |
Zeran | 3:37fb1e2aacf3 | 35 | sprintf(buffer, "0x%x", whoami); |
Zeran | 3:37fb1e2aacf3 | 36 | wait(1); |
shimniok | 0:cf1bc7c313b4 | 37 | |
Zeran | 3:37fb1e2aacf3 | 38 | mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration |
Zeran | 3:37fb1e2aacf3 | 39 | mpu9250.MPU9250SelfTest(SelfTest); // Start by performing self test and reporting values |
Zeran | 3:37fb1e2aacf3 | 40 | pc.printf("x-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[0]); |
Zeran | 3:37fb1e2aacf3 | 41 | pc.printf("y-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[1]); |
Zeran | 3:37fb1e2aacf3 | 42 | pc.printf("z-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[2]); |
Zeran | 3:37fb1e2aacf3 | 43 | pc.printf("x-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[3]); |
Zeran | 3:37fb1e2aacf3 | 44 | pc.printf("y-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[4]); |
Zeran | 3:37fb1e2aacf3 | 45 | pc.printf("z-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[5]); |
Zeran | 3:37fb1e2aacf3 | 46 | mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers |
Zeran | 3:37fb1e2aacf3 | 47 | pc.printf("x gyro bias = %f\n\r", gyroBias[0]); |
Zeran | 3:37fb1e2aacf3 | 48 | pc.printf("y gyro bias = %f\n\r", gyroBias[1]); |
Zeran | 3:37fb1e2aacf3 | 49 | pc.printf("z gyro bias = %f\n\r", gyroBias[2]); |
Zeran | 3:37fb1e2aacf3 | 50 | pc.printf("x accel bias = %f\n\r", accelBias[0]); |
Zeran | 3:37fb1e2aacf3 | 51 | pc.printf("y accel bias = %f\n\r", accelBias[1]); |
Zeran | 3:37fb1e2aacf3 | 52 | pc.printf("z accel bias = %f\n\r", accelBias[2]); |
Zeran | 3:37fb1e2aacf3 | 53 | wait(2); |
Zeran | 3:37fb1e2aacf3 | 54 | |
Zeran | 3:37fb1e2aacf3 | 55 | mpu9250.initMPU9250(); |
Zeran | 3:37fb1e2aacf3 | 56 | pc.printf("MPU9250 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature |
Zeran | 3:37fb1e2aacf3 | 57 | mpu9250.initAK8963(magCalibration); |
Zeran | 3:37fb1e2aacf3 | 58 | pc.printf("AK8963 initialized for active data mode....\n\r"); // Initialize device for active mode read of magnetometer |
Zeran | 3:37fb1e2aacf3 | 59 | pc.printf("Accelerometer full-scale range = %f g\n\r", 2.0f*(float)(1<<Ascale)); |
Zeran | 3:37fb1e2aacf3 | 60 | pc.printf("Gyroscope full-scale range = %f deg/s\n\r", 250.0f*(float)(1<<Gscale)); |
Zeran | 3:37fb1e2aacf3 | 61 | if(Mscale == 0) pc.printf("Magnetometer resolution = 14 bits\n\r"); |
Zeran | 3:37fb1e2aacf3 | 62 | if(Mscale == 1) pc.printf("Magnetometer resolution = 16 bits\n\r"); |
Zeran | 3:37fb1e2aacf3 | 63 | if(Mmode == 2) pc.printf("Magnetometer ODR = 8 Hz\n\r"); |
Zeran | 3:37fb1e2aacf3 | 64 | if(Mmode == 6) pc.printf("Magnetometer ODR = 100 Hz\n\r"); |
Zeran | 3:37fb1e2aacf3 | 65 | wait(1); |
Zeran | 3:37fb1e2aacf3 | 66 | } else { |
Zeran | 3:37fb1e2aacf3 | 67 | pc.printf("Could not connect to MPU9250: \n\r"); |
Zeran | 3:37fb1e2aacf3 | 68 | pc.printf("%#x \n", whoami); |
Zeran | 3:37fb1e2aacf3 | 69 | |
Zeran | 3:37fb1e2aacf3 | 70 | sprintf(buffer, "WHO_AM_I 0x%x", whoami); |
Zeran | 3:37fb1e2aacf3 | 71 | while(1) ; // Loop forever if communication doesn't happen |
Zeran | 3:37fb1e2aacf3 | 72 | } |
Zeran | 3:37fb1e2aacf3 | 73 | mpu9250.getAres(); // Get accelerometer sensitivity |
Zeran | 3:37fb1e2aacf3 | 74 | mpu9250.getGres(); // Get gyro sensitivity |
Zeran | 3:37fb1e2aacf3 | 75 | mpu9250.getMres(); // Get magnetometer sensitivity |
Zeran | 3:37fb1e2aacf3 | 76 | pc.printf("Accelerometer sensitivity is %f LSB/g \n\r", 1.0f/aRes); |
Zeran | 3:37fb1e2aacf3 | 77 | pc.printf("Gyroscope sensitivity is %f LSB/deg/s \n\r", 1.0f/gRes); |
Zeran | 3:37fb1e2aacf3 | 78 | pc.printf("Magnetometer sensitivity is %f LSB/G \n\r", 1.0f/mRes); |
Zeran | 3:37fb1e2aacf3 | 79 | magbias[0] = +470.; // User environmental x-axis correction in milliGauss, should be automatically calculated |
Zeran | 3:37fb1e2aacf3 | 80 | magbias[1] = +120.; // User environmental x-axis correction in milliGauss |
Zeran | 3:37fb1e2aacf3 | 81 | magbias[2] = +125.; // User environmental x-axis correction in milliGauss |
Zeran | 3:37fb1e2aacf3 | 82 | |
Zeran | 3:37fb1e2aacf3 | 83 | |
shimniok | 0:cf1bc7c313b4 | 84 | while(1) { |
Zeran | 3:37fb1e2aacf3 | 85 | |
Zeran | 3:37fb1e2aacf3 | 86 | // If intPin goes high, all data registers have new data |
Zeran | 3:37fb1e2aacf3 | 87 | if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) { // On interrupt, check if data ready interrupt |
Zeran | 3:37fb1e2aacf3 | 88 | |
Zeran | 3:37fb1e2aacf3 | 89 | mpu9250.readAccelData(accelCount); // Read the x/y/z adc values |
Zeran | 3:37fb1e2aacf3 | 90 | // Now we'll calculate the accleration value into actual g's |
Zeran | 3:37fb1e2aacf3 | 91 | ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set |
Zeran | 3:37fb1e2aacf3 | 92 | ay = (float)accelCount[1]*aRes - accelBias[1]; |
Zeran | 3:37fb1e2aacf3 | 93 | az = (float)accelCount[2]*aRes - accelBias[2]; |
Zeran | 3:37fb1e2aacf3 | 94 | |
Zeran | 3:37fb1e2aacf3 | 95 | mpu9250.readGyroData(gyroCount); // Read the x/y/z adc values |
Zeran | 3:37fb1e2aacf3 | 96 | // Calculate the gyro value into actual degrees per second |
Zeran | 3:37fb1e2aacf3 | 97 | gx = (float)gyroCount[0]*gRes - gyroBias[0]; // get actual gyro value, this depends on scale being set |
Zeran | 3:37fb1e2aacf3 | 98 | gy = (float)gyroCount[1]*gRes - gyroBias[1]; |
Zeran | 3:37fb1e2aacf3 | 99 | gz = (float)gyroCount[2]*gRes - gyroBias[2]; |
Zeran | 3:37fb1e2aacf3 | 100 | |
Zeran | 3:37fb1e2aacf3 | 101 | mpu9250.readMagData(magCount); // Read the x/y/z adc values |
Zeran | 3:37fb1e2aacf3 | 102 | // Calculate the magnetometer values in milliGauss |
Zeran | 3:37fb1e2aacf3 | 103 | // Include factory calibration per data sheet and user environmental corrections |
Zeran | 3:37fb1e2aacf3 | 104 | mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]; // get actual magnetometer value, this depends on scale being set |
Zeran | 3:37fb1e2aacf3 | 105 | my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1]; |
Zeran | 3:37fb1e2aacf3 | 106 | mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2]; |
Zeran | 3:37fb1e2aacf3 | 107 | } |
Zeran | 3:37fb1e2aacf3 | 108 | |
Zeran | 3:37fb1e2aacf3 | 109 | Now = t.read_us(); |
Zeran | 3:37fb1e2aacf3 | 110 | deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update |
Zeran | 3:37fb1e2aacf3 | 111 | lastUpdate = Now; |
Zeran | 3:37fb1e2aacf3 | 112 | |
Zeran | 3:37fb1e2aacf3 | 113 | sum += deltat; |
Zeran | 3:37fb1e2aacf3 | 114 | sumCount++; |
Zeran | 3:37fb1e2aacf3 | 115 | |
Zeran | 3:37fb1e2aacf3 | 116 | // if(lastUpdate - firstUpdate > 10000000.0f) { |
Zeran | 3:37fb1e2aacf3 | 117 | // beta = 0.04; // decrease filter gain after stabilized |
Zeran | 3:37fb1e2aacf3 | 118 | // zeta = 0.015; // increasey bias drift gain after stabilized |
Zeran | 3:37fb1e2aacf3 | 119 | // } |
Zeran | 3:37fb1e2aacf3 | 120 | |
Zeran | 3:37fb1e2aacf3 | 121 | // Pass gyro rate as rad/s |
Zeran | 3:37fb1e2aacf3 | 122 | // mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); |
Zeran | 3:37fb1e2aacf3 | 123 | mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); |
Zeran | 3:37fb1e2aacf3 | 124 | |
Zeran | 3:37fb1e2aacf3 | 125 | // Serial print and/or display at 0.5 s rate independent of data rates |
Zeran | 3:37fb1e2aacf3 | 126 | delt_t = t.read_ms() - count; |
Zeran | 3:37fb1e2aacf3 | 127 | if (delt_t > 500) { // update LCD once per half-second independent of read rate |
Zeran | 3:37fb1e2aacf3 | 128 | |
Zeran | 3:37fb1e2aacf3 | 129 | pc.printf("ax = %f", 1000*ax); |
Zeran | 3:37fb1e2aacf3 | 130 | pc.printf(" ay = %f", 1000*ay); |
Zeran | 3:37fb1e2aacf3 | 131 | pc.printf(" az = %f mg\n\r", 1000*az); |
Zeran | 3:37fb1e2aacf3 | 132 | |
Zeran | 3:37fb1e2aacf3 | 133 | pc.printf("gx = %f", gx); |
Zeran | 3:37fb1e2aacf3 | 134 | pc.printf(" gy = %f", gy); |
Zeran | 3:37fb1e2aacf3 | 135 | pc.printf(" gz = %f deg/s\n\r", gz); |
Zeran | 3:37fb1e2aacf3 | 136 | |
Zeran | 3:37fb1e2aacf3 | 137 | pc.printf("gx = %f", mx); |
Zeran | 3:37fb1e2aacf3 | 138 | pc.printf(" gy = %f", my); |
Zeran | 3:37fb1e2aacf3 | 139 | pc.printf(" gz = %f mG\n\r", mz); |
Zeran | 3:37fb1e2aacf3 | 140 | |
Zeran | 3:37fb1e2aacf3 | 141 | tempCount = mpu9250.readTempData(); // Read the adc values |
Zeran | 3:37fb1e2aacf3 | 142 | temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade |
Zeran | 3:37fb1e2aacf3 | 143 | pc.printf(" temperature = %f C\n\r", temperature); |
Zeran | 3:37fb1e2aacf3 | 144 | |
Zeran | 3:37fb1e2aacf3 | 145 | pc.printf("q0 = %f\n\r", q[0]); |
Zeran | 3:37fb1e2aacf3 | 146 | pc.printf("q1 = %f\n\r", q[1]); |
Zeran | 3:37fb1e2aacf3 | 147 | pc.printf("q2 = %f\n\r", q[2]); |
Zeran | 3:37fb1e2aacf3 | 148 | pc.printf("q3 = %f\n\r", q[3]); |
Zeran | 3:37fb1e2aacf3 | 149 | |
Zeran | 3:37fb1e2aacf3 | 150 | /* lcd.clear(); |
Zeran | 3:37fb1e2aacf3 | 151 | lcd.printString("MPU9250", 0, 0); |
Zeran | 3:37fb1e2aacf3 | 152 | lcd.printString("x y z", 0, 1); |
Zeran | 3:37fb1e2aacf3 | 153 | sprintf(buffer, "%d %d %d mg", (int)(1000.0f*ax), (int)(1000.0f*ay), (int)(1000.0f*az)); |
Zeran | 3:37fb1e2aacf3 | 154 | lcd.printString(buffer, 0, 2); |
Zeran | 3:37fb1e2aacf3 | 155 | sprintf(buffer, "%d %d %d deg/s", (int)gx, (int)gy, (int)gz); |
Zeran | 3:37fb1e2aacf3 | 156 | lcd.printString(buffer, 0, 3); |
Zeran | 3:37fb1e2aacf3 | 157 | sprintf(buffer, "%d %d %d mG", (int)mx, (int)my, (int)mz); |
Zeran | 3:37fb1e2aacf3 | 158 | lcd.printString(buffer, 0, 4); |
Zeran | 3:37fb1e2aacf3 | 159 | */ |
Zeran | 3:37fb1e2aacf3 | 160 | // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation. |
Zeran | 3:37fb1e2aacf3 | 161 | // In this coordinate system, the positive z-axis is down toward Earth. |
Zeran | 3:37fb1e2aacf3 | 162 | // 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. |
Zeran | 3:37fb1e2aacf3 | 163 | // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative. |
Zeran | 3:37fb1e2aacf3 | 164 | // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll. |
Zeran | 3:37fb1e2aacf3 | 165 | // These arise from the definition of the homogeneous rotation matrix constructed from quaternions. |
Zeran | 3:37fb1e2aacf3 | 166 | // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be |
Zeran | 3:37fb1e2aacf3 | 167 | // applied in the correct order which for this configuration is yaw, pitch, and then roll. |
Zeran | 3:37fb1e2aacf3 | 168 | // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links. |
Zeran | 3:37fb1e2aacf3 | 169 | 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]); |
Zeran | 3:37fb1e2aacf3 | 170 | pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2])); |
Zeran | 3:37fb1e2aacf3 | 171 | 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]); |
Zeran | 3:37fb1e2aacf3 | 172 | pitch *= 180.0f / PI; |
Zeran | 3:37fb1e2aacf3 | 173 | yaw *= 180.0f / PI; |
Zeran | 3:37fb1e2aacf3 | 174 | yaw -= 13.8f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04 |
Zeran | 3:37fb1e2aacf3 | 175 | roll *= 180.0f / PI; |
Zeran | 3:37fb1e2aacf3 | 176 | |
Zeran | 3:37fb1e2aacf3 | 177 | pc.printf("Yaw, Pitch, Roll: %f %f %f\n\r", yaw, pitch, roll); |
Zeran | 3:37fb1e2aacf3 | 178 | pc.printf("average rate = %f\n\r", (float) sumCount/sum); |
Zeran | 3:37fb1e2aacf3 | 179 | // sprintf(buffer, "YPR: %f %f %f", yaw, pitch, roll); |
Zeran | 3:37fb1e2aacf3 | 180 | // lcd.printString(buffer, 0, 4); |
Zeran | 3:37fb1e2aacf3 | 181 | // sprintf(buffer, "rate = %f", (float) sumCount/sum); |
Zeran | 3:37fb1e2aacf3 | 182 | // lcd.printString(buffer, 0, 5); |
Zeran | 3:37fb1e2aacf3 | 183 | |
Zeran | 3:37fb1e2aacf3 | 184 | myled= !myled; |
Zeran | 3:37fb1e2aacf3 | 185 | count = t.read_ms(); |
Zeran | 3:37fb1e2aacf3 | 186 | |
Zeran | 3:37fb1e2aacf3 | 187 | if(count > 1<<21) { |
Zeran | 3:37fb1e2aacf3 | 188 | t.start(); // start the timer over again if ~30 minutes has passed |
Zeran | 3:37fb1e2aacf3 | 189 | count = 0; |
Zeran | 3:37fb1e2aacf3 | 190 | deltat= 0; |
Zeran | 3:37fb1e2aacf3 | 191 | lastUpdate = t.read_us(); |
Zeran | 2:2b68d1d14aca | 192 | } |
Zeran | 3:37fb1e2aacf3 | 193 | sum = 0; |
Zeran | 3:37fb1e2aacf3 | 194 | sumCount = 0; |
Zeran | 2:2b68d1d14aca | 195 | } |
shimniok | 0:cf1bc7c313b4 | 196 | } |
Zeran | 3:37fb1e2aacf3 | 197 | } |