MPU6050 with I2C. NO LCD

Dependencies:   mbed

Committer:
HarryKeane
Date:
Tue May 05 13:09:37 2020 +0000
Revision:
4:0bbb594dd933
Parent:
3:02533f8628e4
qwertgcxsertfserfcxzwef

Who changed what in which revision?

UserRevisionLine numberNew contents of line
onehorse 0:65aa78c10981 1
onehorse 0:65aa78c10981 2 /* MPU6050 Basic Example Code
onehorse 0:65aa78c10981 3 by: Kris Winer
onehorse 0:65aa78c10981 4 date: May 1, 2014
onehorse 0:65aa78c10981 5 license: Beerware - Use this code however you'd like. If you
onehorse 0:65aa78c10981 6 find it useful you can buy me a beer some time.
onehorse 0:65aa78c10981 7
onehorse 0:65aa78c10981 8 Demonstrate MPU-6050 basic functionality including initialization, accelerometer trimming, sleep mode functionality as well as
onehorse 0:65aa78c10981 9 parameterizing the register addresses. Added display functions to allow display to on breadboard monitor.
onehorse 0:65aa78c10981 10 No DMP use. We just want to get out the accelerations, temperature, and gyro readings.
onehorse 0:65aa78c10981 11
onehorse 0:65aa78c10981 12 SDA and SCL should have external pull-up resistors (to 3.3V).
onehorse 0:65aa78c10981 13 10k resistors worked for me. They should be on the breakout
onehorse 0:65aa78c10981 14 board.
onehorse 0:65aa78c10981 15
onehorse 0:65aa78c10981 16 Hardware setup:
onehorse 0:65aa78c10981 17 MPU6050 Breakout --------- Arduino
onehorse 0:65aa78c10981 18 3.3V --------------------- 3.3V
onehorse 0:65aa78c10981 19 SDA ----------------------- A4
onehorse 0:65aa78c10981 20 SCL ----------------------- A5
onehorse 0:65aa78c10981 21 GND ---------------------- GND
onehorse 0:65aa78c10981 22
onehorse 0:65aa78c10981 23 Note: The MPU6050 is an I2C sensor and uses the Arduino Wire library.
onehorse 0:65aa78c10981 24 Because the sensor is not 5V tolerant, we are using a 3.3 V 8 MHz Pro Mini or a 3.3 V Teensy 3.1.
onehorse 0:65aa78c10981 25 We have disabled the internal pull-ups used by the Wire library in the Wire.h/twi.c utility file.
onehorse 0:65aa78c10981 26 We are also using the 400 kHz fast I2C mode by setting the TWI_FREQ to 400000L /twi.h utility file.
onehorse 0:65aa78c10981 27 */
onehorse 0:65aa78c10981 28
onehorse 1:cea9d83b8636 29 #include "mbed.h"
onehorse 1:cea9d83b8636 30 #include "MPU6050.h"
HarryKeane 4:0bbb594dd933 31 #include "stdio.h"
HarryKeane 3:02533f8628e4 32 //#include "N5110.h"
onehorse 0:65aa78c10981 33
onehorse 0:65aa78c10981 34 // Using NOKIA 5110 monochrome 84 x 48 pixel display
onehorse 0:65aa78c10981 35 // pin 9 - Serial clock out (SCLK)
onehorse 0:65aa78c10981 36 // pin 8 - Serial data out (DIN)
onehorse 0:65aa78c10981 37 // pin 7 - Data/Command select (D/C)
onehorse 0:65aa78c10981 38 // pin 5 - LCD chip select (CS)
onehorse 0:65aa78c10981 39 // pin 6 - LCD reset (RST)
onehorse 0:65aa78c10981 40 //Adafruit_PCD8544 display = Adafruit_PCD8544(9, 8, 7, 5, 6);
onehorse 0:65aa78c10981 41
HarryKeane 4:0bbb594dd933 42
HarryKeane 4:0bbb594dd933 43 //as been converted to not use LCD and to print to putty
HarryKeane 4:0bbb594dd933 44
HarryKeane 4:0bbb594dd933 45 int sleepDuration = 5;
HarryKeane 4:0bbb594dd933 46
HarryKeane 4:0bbb594dd933 47 void mpuLoop(void);
HarryKeane 4:0bbb594dd933 48
HarryKeane 4:0bbb594dd933 49 volatile float yawLow;
HarryKeane 4:0bbb594dd933 50 volatile float yawHigh;
HarryKeane 4:0bbb594dd933 51
HarryKeane 4:0bbb594dd933 52 volatile float pitchLow;
HarryKeane 4:0bbb594dd933 53 volatile float pitchHigh;
HarryKeane 4:0bbb594dd933 54
HarryKeane 4:0bbb594dd933 55 volatile float rollLow;
HarryKeane 4:0bbb594dd933 56 volatile float rollHigh;
HarryKeane 4:0bbb594dd933 57
HarryKeane 4:0bbb594dd933 58 void buttonISR(const char*);
HarryKeane 4:0bbb594dd933 59
HarryKeane 4:0bbb594dd933 60 InterruptIn Zero_system(PD_0);
HarryKeane 4:0bbb594dd933 61 InterruptIn Off_standby(PD_1);
HarryKeane 4:0bbb594dd933 62
HarryKeane 4:0bbb594dd933 63 static const char* zero_system_notification = "systemZero";
HarryKeane 4:0bbb594dd933 64 static const char* off_standby_notification = "systemStandby";
HarryKeane 4:0bbb594dd933 65
onehorse 1:cea9d83b8636 66 float sum = 0;
onehorse 1:cea9d83b8636 67 uint32_t sumCount = 0;
onehorse 1:cea9d83b8636 68
onehorse 1:cea9d83b8636 69 MPU6050 mpu6050;
onehorse 1:cea9d83b8636 70
onehorse 1:cea9d83b8636 71 Timer t;
onehorse 1:cea9d83b8636 72
onehorse 1:cea9d83b8636 73 Serial pc(USBTX, USBRX); // tx, rx
onehorse 1:cea9d83b8636 74
HarryKeane 3:02533f8628e4 75
onehorse 1:cea9d83b8636 76
onehorse 1:cea9d83b8636 77 int main()
onehorse 1:cea9d83b8636 78 {
onehorse 1:cea9d83b8636 79 pc.baud(9600);
onehorse 0:65aa78c10981 80
onehorse 1:cea9d83b8636 81 //Set up I2C
onehorse 1:cea9d83b8636 82 i2c.frequency(400000); // use fast (400 kHz) I2C
onehorse 1:cea9d83b8636 83
HarryKeane 4:0bbb594dd933 84
HarryKeane 4:0bbb594dd933 85 // Put in main
HarryKeane 4:0bbb594dd933 86 Zero_system.rise(&buttonISR,zero_system_notification); // attach the address of the flip function to the rising edge
HarryKeane 4:0bbb594dd933 87 Off_standby.rise(&buttonISR,off_standby_notification); // attach the address of the flip function to the rising edge
HarryKeane 4:0bbb594dd933 88
HarryKeane 4:0bbb594dd933 89
HarryKeane 4:0bbb594dd933 90
onehorse 1:cea9d83b8636 91 t.start();
onehorse 1:cea9d83b8636 92
HarryKeane 4:0bbb594dd933 93
onehorse 1:cea9d83b8636 94
onehorse 1:cea9d83b8636 95
onehorse 1:cea9d83b8636 96 // Read the WHO_AM_I register, this is a good test of communication
onehorse 1:cea9d83b8636 97 uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050); // Read WHO_AM_I register for MPU-6050
onehorse 1:cea9d83b8636 98 pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x68\n\r");
onehorse 1:cea9d83b8636 99
onehorse 1:cea9d83b8636 100 if (whoami == 0x68) // WHO_AM_I should always be 0x68
onehorse 1:cea9d83b8636 101 {
onehorse 1:cea9d83b8636 102 pc.printf("MPU6050 is online...");
onehorse 1:cea9d83b8636 103 wait(1);
HarryKeane 3:02533f8628e4 104
onehorse 0:65aa78c10981 105
onehorse 1:cea9d83b8636 106
onehorse 1:cea9d83b8636 107 mpu6050.MPU6050SelfTest(SelfTest); // Start by performing self test and reporting values
onehorse 1:cea9d83b8636 108 pc.printf("x-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[0]); pc.printf("% of factory value \n\r");
onehorse 1:cea9d83b8636 109 pc.printf("y-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[1]); pc.printf("% of factory value \n\r");
onehorse 1:cea9d83b8636 110 pc.printf("z-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[2]); pc.printf("% of factory value \n\r");
onehorse 1:cea9d83b8636 111 pc.printf("x-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[3]); pc.printf("% of factory value \n\r");
onehorse 1:cea9d83b8636 112 pc.printf("y-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[4]); pc.printf("% of factory value \n\r");
onehorse 1:cea9d83b8636 113 pc.printf("z-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[5]); pc.printf("% of factory value \n\r");
onehorse 1:cea9d83b8636 114 wait(1);
onehorse 0:65aa78c10981 115
onehorse 1:cea9d83b8636 116 if(SelfTest[0] < 1.0f && SelfTest[1] < 1.0f && SelfTest[2] < 1.0f && SelfTest[3] < 1.0f && SelfTest[4] < 1.0f && SelfTest[5] < 1.0f)
onehorse 1:cea9d83b8636 117 {
onehorse 1:cea9d83b8636 118 mpu6050.resetMPU6050(); // Reset registers to default in preparation for device calibration
onehorse 1:cea9d83b8636 119 mpu6050.calibrateMPU6050(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers
onehorse 1:cea9d83b8636 120 mpu6050.initMPU6050(); pc.printf("MPU6050 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature
onehorse 0:65aa78c10981 121
HarryKeane 3:02533f8628e4 122
onehorse 1:cea9d83b8636 123 wait(2);
onehorse 1:cea9d83b8636 124 }
onehorse 1:cea9d83b8636 125 else
onehorse 1:cea9d83b8636 126 {
onehorse 1:cea9d83b8636 127 pc.printf("Device did not the pass self-test!\n\r");
HarryKeane 3:02533f8628e4 128
onehorse 1:cea9d83b8636 129 }
onehorse 1:cea9d83b8636 130 }
onehorse 1:cea9d83b8636 131 else
onehorse 1:cea9d83b8636 132 {
onehorse 1:cea9d83b8636 133 pc.printf("Could not connect to MPU6050: \n\r");
onehorse 1:cea9d83b8636 134 pc.printf("%#x \n", whoami);
onehorse 1:cea9d83b8636 135
HarryKeane 3:02533f8628e4 136
onehorse 1:cea9d83b8636 137
onehorse 1:cea9d83b8636 138 while(1) ; // Loop forever if communication doesn't happen
onehorse 0:65aa78c10981 139 }
onehorse 0:65aa78c10981 140
onehorse 0:65aa78c10981 141
onehorse 0:65aa78c10981 142
HarryKeane 4:0bbb594dd933 143 buttonISR(zero_system_notification);
HarryKeane 4:0bbb594dd933 144
HarryKeane 4:0bbb594dd933 145
HarryKeane 4:0bbb594dd933 146
onehorse 1:cea9d83b8636 147 while(1) {
onehorse 0:65aa78c10981 148
HarryKeane 4:0bbb594dd933 149
HarryKeane 4:0bbb594dd933 150
HarryKeane 4:0bbb594dd933 151 mpuLoop();
HarryKeane 4:0bbb594dd933 152
HarryKeane 4:0bbb594dd933 153
HarryKeane 4:0bbb594dd933 154
HarryKeane 4:0bbb594dd933 155
HarryKeane 4:0bbb594dd933 156 }
HarryKeane 4:0bbb594dd933 157
HarryKeane 4:0bbb594dd933 158
HarryKeane 4:0bbb594dd933 159
HarryKeane 4:0bbb594dd933 160 }
HarryKeane 4:0bbb594dd933 161
HarryKeane 4:0bbb594dd933 162
HarryKeane 4:0bbb594dd933 163 void mpuLoop(void){
onehorse 1:cea9d83b8636 164 // If data ready bit set, all data registers have new data
onehorse 1:cea9d83b8636 165 if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) { // check if data ready interrupt
onehorse 1:cea9d83b8636 166 mpu6050.readAccelData(accelCount); // Read the x/y/z adc values
onehorse 1:cea9d83b8636 167 mpu6050.getAres();
onehorse 0:65aa78c10981 168
onehorse 0:65aa78c10981 169 // Now we'll calculate the accleration value into actual g's
onehorse 0:65aa78c10981 170 ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set
onehorse 0:65aa78c10981 171 ay = (float)accelCount[1]*aRes - accelBias[1];
onehorse 0:65aa78c10981 172 az = (float)accelCount[2]*aRes - accelBias[2];
onehorse 0:65aa78c10981 173
onehorse 1:cea9d83b8636 174 mpu6050.readGyroData(gyroCount); // Read the x/y/z adc values
onehorse 1:cea9d83b8636 175 mpu6050.getGres();
onehorse 0:65aa78c10981 176
onehorse 0:65aa78c10981 177 // Calculate the gyro value into actual degrees per second
onehorse 1:cea9d83b8636 178 gx = (float)gyroCount[0]*gRes; // - gyroBias[0]; // get actual gyro value, this depends on scale being set
onehorse 1:cea9d83b8636 179 gy = (float)gyroCount[1]*gRes; // - gyroBias[1];
onehorse 1:cea9d83b8636 180 gz = (float)gyroCount[2]*gRes; // - gyroBias[2];
onehorse 0:65aa78c10981 181
onehorse 1:cea9d83b8636 182 tempCount = mpu6050.readTempData(); // Read the x/y/z adc values
onehorse 0:65aa78c10981 183 temperature = (tempCount) / 340. + 36.53; // Temperature in degrees Centigrade
onehorse 0:65aa78c10981 184 }
onehorse 0:65aa78c10981 185
onehorse 0:65aa78c10981 186 Now = t.read_us();
onehorse 1:cea9d83b8636 187 deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
onehorse 0:65aa78c10981 188 lastUpdate = Now;
onehorse 1:cea9d83b8636 189
onehorse 1:cea9d83b8636 190 sum += deltat;
onehorse 1:cea9d83b8636 191 sumCount++;
onehorse 1:cea9d83b8636 192
onehorse 0:65aa78c10981 193 if(lastUpdate - firstUpdate > 10000000.0f) {
onehorse 1:cea9d83b8636 194 beta = 0.04; // decrease filter gain after stabilized
onehorse 1:cea9d83b8636 195 zeta = 0.015; // increasey bias drift gain after stabilized
onehorse 0:65aa78c10981 196 }
onehorse 1:cea9d83b8636 197
onehorse 0:65aa78c10981 198 // Pass gyro rate as rad/s
onehorse 1:cea9d83b8636 199 mpu6050.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f);
onehorse 0:65aa78c10981 200
HarryKeane 4:0bbb594dd933 201 // Serial print and/or display at 0.5 s rate independent of data rates
HarryKeane 3:02533f8628e4 202 // delt_t = t.read_ms() - count;
HarryKeane 4:0bbb594dd933 203 // if (delt_t > 500) { // update LCD once per half-second independent of read rate
onehorse 1:cea9d83b8636 204
onehorse 1:cea9d83b8636 205 pc.printf("ax = %f", 1000*ax);
onehorse 1:cea9d83b8636 206 pc.printf(" ay = %f", 1000*ay);
onehorse 1:cea9d83b8636 207 pc.printf(" az = %f mg\n\r", 1000*az);
onehorse 0:65aa78c10981 208
onehorse 1:cea9d83b8636 209 pc.printf("gx = %f", gx);
onehorse 1:cea9d83b8636 210 pc.printf(" gy = %f", gy);
onehorse 1:cea9d83b8636 211 pc.printf(" gz = %f deg/s\n\r", gz);
onehorse 1:cea9d83b8636 212
onehorse 1:cea9d83b8636 213 pc.printf(" temperature = %f C\n\r", temperature);
onehorse 0:65aa78c10981 214
onehorse 1:cea9d83b8636 215 pc.printf("q0 = %f\n\r", q[0]);
onehorse 1:cea9d83b8636 216 pc.printf("q1 = %f\n\r", q[1]);
onehorse 1:cea9d83b8636 217 pc.printf("q2 = %f\n\r", q[2]);
onehorse 1:cea9d83b8636 218 pc.printf("q3 = %f\n\r", q[3]);
onehorse 1:cea9d83b8636 219
HarryKeane 3:02533f8628e4 220
onehorse 1:cea9d83b8636 221
onehorse 0:65aa78c10981 222
onehorse 0:65aa78c10981 223 // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation.
onehorse 0:65aa78c10981 224 // In this coordinate system, the positive z-axis is down toward Earth.
onehorse 0:65aa78c10981 225 // 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.
onehorse 0:65aa78c10981 226 // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative.
onehorse 0:65aa78c10981 227 // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
onehorse 0:65aa78c10981 228 // These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
onehorse 0:65aa78c10981 229 // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be
onehorse 0:65aa78c10981 230 // applied in the correct order which for this configuration is yaw, pitch, and then roll.
onehorse 0:65aa78c10981 231 // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
onehorse 0:65aa78c10981 232 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]);
onehorse 0:65aa78c10981 233 pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
onehorse 0:65aa78c10981 234 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]);
onehorse 0:65aa78c10981 235 pitch *= 180.0f / PI;
onehorse 0:65aa78c10981 236 yaw *= 180.0f / PI;
onehorse 0:65aa78c10981 237 roll *= 180.0f / PI;
onehorse 0:65aa78c10981 238
HarryKeane 4:0bbb594dd933 239 pc.printf("Yaw, Pitch, Roll: \n\r");
HarryKeane 4:0bbb594dd933 240 pc.printf("%f", yaw);
HarryKeane 4:0bbb594dd933 241 pc.printf(", ");
HarryKeane 4:0bbb594dd933 242 pc.printf("%f", pitch);
HarryKeane 4:0bbb594dd933 243 pc.printf(", ");
HarryKeane 4:0bbb594dd933 244 pc.printf("%f\n\r", roll);
HarryKeane 4:0bbb594dd933 245 pc.printf("average rate = "); pc.printf("%f", (sumCount/sum)); pc.printf(" Hz\n\r");
onehorse 0:65aa78c10981 246
onehorse 1:cea9d83b8636 247 pc.printf("Yaw, Pitch, Roll: %f %f %f\n\r", yaw, pitch, roll);
onehorse 1:cea9d83b8636 248 pc.printf("average rate = %f\n\r", (float) sumCount/sum);
onehorse 1:cea9d83b8636 249
onehorse 1:cea9d83b8636 250 myled= !myled;
HarryKeane 3:02533f8628e4 251 // count = t.read_ms();
onehorse 1:cea9d83b8636 252 sum = 0;
onehorse 1:cea9d83b8636 253 sumCount = 0;
HarryKeane 3:02533f8628e4 254
HarryKeane 3:02533f8628e4 255 //wait(0.5); //So i can read the data
HarryKeane 4:0bbb594dd933 256
HarryKeane 4:0bbb594dd933 257
HarryKeane 4:0bbb594dd933 258
HarryKeane 4:0bbb594dd933 259 if(yaw > yawHigh || yaw < yawLow || pitch > pitchHigh || pitch < pitchLow || roll > rollHigh || roll < rollLow ){
HarryKeane 4:0bbb594dd933 260
HarryKeane 4:0bbb594dd933 261 printf("\n\nTHEFT ATTEMPT\n\n");
HarryKeane 4:0bbb594dd933 262
HarryKeane 4:0bbb594dd933 263 }
HarryKeane 4:0bbb594dd933 264 }
HarryKeane 4:0bbb594dd933 265
HarryKeane 4:0bbb594dd933 266
HarryKeane 4:0bbb594dd933 267
HarryKeane 4:0bbb594dd933 268
HarryKeane 4:0bbb594dd933 269
HarryKeane 4:0bbb594dd933 270
HarryKeane 4:0bbb594dd933 271
HarryKeane 4:0bbb594dd933 272
HarryKeane 4:0bbb594dd933 273 void buttonISR(const char* parameter) {
HarryKeane 4:0bbb594dd933 274 printf("\n\r%s",parameter);
HarryKeane 4:0bbb594dd933 275 if(parameter == zero_system_notification){
HarryKeane 4:0bbb594dd933 276 printf("\n\n\nZERO\n\n\n");
HarryKeane 4:0bbb594dd933 277
HarryKeane 4:0bbb594dd933 278 yawLow = yaw - 30;
HarryKeane 4:0bbb594dd933 279 yawHigh = yaw + 30;
HarryKeane 4:0bbb594dd933 280
HarryKeane 4:0bbb594dd933 281 pitchLow = pitch - 30;
HarryKeane 4:0bbb594dd933 282 pitchHigh = pitch + 30;
HarryKeane 4:0bbb594dd933 283
HarryKeane 4:0bbb594dd933 284 rollLow = roll - 30;
HarryKeane 4:0bbb594dd933 285 rollHigh = roll + 30;
HarryKeane 4:0bbb594dd933 286
HarryKeane 4:0bbb594dd933 287
HarryKeane 4:0bbb594dd933 288 //steps
HarryKeane 4:0bbb594dd933 289 //will have to create a relica variable of yaw pitch roll and then compare in an iff statement statement.
HarryKeane 4:0bbb594dd933 290 //e.g yaw 2+ and yaw2- ==== yaw2+ = yaw +10. & yaw 2- = yaw-10. if(yaw > yaw2+ || yaw <yaw2-){ trigger alarm }
HarryKeane 4:0bbb594dd933 291
HarryKeane 4:0bbb594dd933 292
HarryKeane 4:0bbb594dd933 293 //if statemenet for yaw,pitch,roll. if statment where is greater or smaller than 10 either way a light is lit.
HarryKeane 4:0bbb594dd933 294 //light becomes text message
HarryKeane 4:0bbb594dd933 295 //zero is done with button
HarryKeane 4:0bbb594dd933 296
HarryKeane 4:0bbb594dd933 297 }
HarryKeane 4:0bbb594dd933 298 //static const char* off_standby_notification = "systemStandby";
HarryKeane 4:0bbb594dd933 299 if(parameter == off_standby_notification){
HarryKeane 4:0bbb594dd933 300 printf("\n\n\nSTANDBY\n\n\n");
HarryKeane 4:0bbb594dd933 301 }
onehorse 0:65aa78c10981 302 }
HarryKeane 4:0bbb594dd933 303
HarryKeane 4:0bbb594dd933 304
HarryKeane 4:0bbb594dd933 305
HarryKeane 3:02533f8628e4 306