IMU for turtle robot project

Dependencies:   mbed

Revision:
4:5002036c82df
Parent:
3:0d58dbc24178
--- a/main.cpp	Wed Jan 24 08:44:45 2018 +0000
+++ b/main.cpp	Sun Jan 28 09:02:04 2018 +0000
@@ -26,35 +26,18 @@
  We are also using the 400 kHz fast I2C mode by setting the TWI_FREQ  to 400000L /twi.h utility file.
  */
  
-//#include "ST_F401_84MHZ.h" 
-//F401_init84 myinit(0);
 #include "mbed.h"
 #include "MPU9250.h"
-//#include "N5110.h"
-
-// Using NOKIA 5110 monochrome 84 x 48 pixel display
-// pin 9 - Serial clock out (SCLK)
-// pin 8 - Serial data out (DIN)
-// pin 7 - Data/Command select (D/C)
-// pin 5 - LCD chip select (CS)
-// pin 6 - LCD reset (RST)
-//Adafruit_PCD8544 display = Adafruit_PCD8544(9, 8, 7, 5, 6);
 
 float sum = 0;
 uint32_t sumCount = 0;
 char buffer[14];
-
-   MPU9250 mpu9250;
-   
-   Timer t;
+float origin = 0;
 
-   Serial pc(USBTX, USBRX); // tx, rx
+MPU9250 mpu9250;
+Timer t;
+Serial pc(USBTX, USBRX); // tx, rx
 
-   //        VCC,   SCE,  RST,  D/C,  MOSI,S CLK, LED
-//   N5110 lcd(PA_8, PB_10, PA_9, PA_6, PA_7, PA_5, PC_7);
-   
-
-        
 int main()
 {
   pc.baud(9600);  
@@ -62,51 +45,43 @@
   //Set up I2C
   i2c.frequency(400000);  // use fast (400 kHz) I2C  
   
-  pc.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);   
+  //pc.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);   
   
   t.start();        
-  
-//  lcd.init();
-//  lcd.setBrightness(0.05);
-  
-    
+      
   // Read the WHO_AM_I register, this is a good test of communication
   uint8_t whoami = mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);  // Read WHO_AM_I register for MPU-9250
-  pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x71\n\r");
+  //pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x68\n\r");
   
-  if (whoami == 0x71) // WHO_AM_I should always be 0x68
+  if (whoami == 0x68) // WHO_AM_I should always be 0x68
   {  
-    pc.printf("MPU9250 WHO_AM_I is 0x%x\n\r", whoami);
-    pc.printf("MPU9250 is online...\n\r");
-//    lcd.clear();
-//    lcd.printString("MPU9250 is", 0, 0);
-    sprintf(buffer, "0x%x", whoami);
-//    lcd.printString(buffer, 0, 1);
-//    lcd.printString("shoud be 0x71", 0, 2);  
+    //pc.printf("MPU9250 WHO_AM_I is 0x%x\n\r", whoami);
+    //pc.printf("MPU9250 is online...\n\r");
+    sprintf(buffer, "0x%x", whoami);  
     wait(1);
     
     mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration
     mpu9250.MPU9250SelfTest(SelfTest); // Start by performing self test and reporting values
-    pc.printf("x-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[0]);  
-    pc.printf("y-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[1]);  
-    pc.printf("z-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[2]);  
-    pc.printf("x-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[3]);  
-    pc.printf("y-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[4]);  
-    pc.printf("z-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[5]);  
+    //pc.printf("x-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[0]);  
+    //pc.printf("y-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[1]);  
+    //pc.printf("z-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[2]);  
+    //pc.printf("x-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[3]);  
+    //pc.printf("y-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[4]);  
+    //pc.printf("z-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[5]);  
     mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers  
-    pc.printf("x gyro bias = %f\n\r", gyroBias[0]);
-    pc.printf("y gyro bias = %f\n\r", gyroBias[1]);
-    pc.printf("z gyro bias = %f\n\r", gyroBias[2]);
-    pc.printf("x accel bias = %f\n\r", accelBias[0]);
-    pc.printf("y accel bias = %f\n\r", accelBias[1]);
-    pc.printf("z accel bias = %f\n\r", accelBias[2]);
+    //pc.printf("x gyro bias = %f\n\r", gyroBias[0]);
+    //pc.printf("y gyro bias = %f\n\r", gyroBias[1]);
+    //pc.printf("z gyro bias = %f\n\r", gyroBias[2]);
+    //pc.printf("x accel bias = %f\n\r", accelBias[0]);
+    //pc.printf("y accel bias = %f\n\r", accelBias[1]);
+    //pc.printf("z accel bias = %f\n\r", accelBias[2]);
     wait(2);
     mpu9250.initMPU9250(); 
-    pc.printf("MPU9250 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature
+    //pc.printf("MPU9250 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature
     mpu9250.initAK8963(magCalibration);
-    pc.printf("AK8963 initialized for active data mode....\n\r"); // Initialize device for active mode read of magnetometer
-    pc.printf("Accelerometer full-scale range = %f  g\n\r", 2.0f*(float)(1<<Ascale));
-    pc.printf("Gyroscope full-scale range = %f  deg/s\n\r", 250.0f*(float)(1<<Gscale));
+    //pc.printf("AK8963 initialized for active data mode....\n\r"); // Initialize device for active mode read of magnetometer
+    //pc.printf("Accelerometer full-scale range = %f  g\n\r", 2.0f*(float)(1<<Ascale));
+    //pc.printf("Gyroscope full-scale range = %f  deg/s\n\r", 250.0f*(float)(1<<Gscale));
     if(Mscale == 0) pc.printf("Magnetometer resolution = 14  bits\n\r");
     if(Mscale == 1) pc.printf("Magnetometer resolution = 16  bits\n\r");
     if(Mmode == 2) pc.printf("Magnetometer ODR = 8 Hz\n\r");
@@ -118,11 +93,7 @@
     pc.printf("Could not connect to MPU9250: \n\r");
     pc.printf("%#x \n",  whoami);
  
-//    lcd.clear();
-//    lcd.printString("MPU9250", 0, 0);
-//    lcd.printString("no connection", 0, 1);
-    sprintf(buffer, "WHO_AM_I 0x%x", whoami);
-//    lcd.printString(buffer, 0, 2); 
+    sprintf(buffer, "WHO_AM_I 0x%x", whoami); 
  
     while(1) ; // Loop forever if communication doesn't happen
     }
@@ -130,9 +101,9 @@
     mpu9250.getAres(); // Get accelerometer sensitivity
     mpu9250.getGres(); // Get gyro sensitivity
     mpu9250.getMres(); // Get magnetometer sensitivity
-    pc.printf("Accelerometer sensitivity is %f LSB/g \n\r", 1.0f/aRes);
-    pc.printf("Gyroscope sensitivity is %f LSB/deg/s \n\r", 1.0f/gRes);
-    pc.printf("Magnetometer sensitivity is %f LSB/G \n\r", 1.0f/mRes);
+    //pc.printf("Accelerometer sensitivity is %f LSB/g \n\r", 1.0f/aRes);
+    //pc.printf("Gyroscope sensitivity is %f LSB/deg/s \n\r", 1.0f/gRes);
+    //pc.printf("Magnetometer sensitivity is %f LSB/G \n\r", 1.0f/mRes);
     magbias[0] = +470.;  // User environmental x-axis correction in milliGauss, should be automatically calculated
     magbias[1] = +120.;  // User environmental x-axis correction in milliGauss
     magbias[2] = +125.;  // User environmental x-axis correction in milliGauss
@@ -180,39 +151,30 @@
 
     // Serial print and/or display at 0.5 s rate independent of data rates
     delt_t = t.read_ms() - count;
-    if (delt_t > 500) { // update LCD once per half-second independent of read rate
-
-    pc.printf("ax = %f", 1000*ax); 
-    pc.printf(" ay = %f", 1000*ay); 
-    pc.printf(" az = %f  mg\n\r", 1000*az); 
+    if (delt_t > 50) { // update LCD once per half-second independent of read rate
 
-    pc.printf("gx = %f", gx); 
-    pc.printf(" gy = %f", gy); 
-    pc.printf(" gz = %f  deg/s\n\r", gz); 
-    
-    pc.printf("gx = %f", mx); 
-    pc.printf(" gy = %f", my); 
-    pc.printf(" gz = %f  mG\n\r", mz); 
+    //pc.printf("ax = %f", 1000*ax); 
+    //pc.printf(" ay = %f", 1000*ay); 
+    //pc.printf(" az = %f  mg\n\r", 1000*az); 
+
+    //pc.printf("gx = %f", gx); 
+    //pc.printf(" gy = %f", gy); 
+    //pc.printf(" gz = %f  deg/s\n\r", gz); 
     
-    tempCount = mpu9250.readTempData();  // Read the adc values
-    temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade
-    pc.printf(" temperature = %f  C\n\r", temperature); 
-    
-    pc.printf("q0 = %f\n\r", q[0]);
-    pc.printf("q1 = %f\n\r", q[1]);
-    pc.printf("q2 = %f\n\r", q[2]);
-    pc.printf("q3 = %f\n\r", q[3]);      
+    //pc.printf("gx = %f", mx); 
+    //pc.printf(" gy = %f", my); 
+    //pc.printf(" gz = %f  mG\n\r", mz); 
     
-/*    lcd.clear();
-    lcd.printString("MPU9250", 0, 0);
-    lcd.printString("x   y   z", 0, 1);
-    sprintf(buffer, "%d %d %d mg", (int)(1000.0f*ax), (int)(1000.0f*ay), (int)(1000.0f*az));
-    lcd.printString(buffer, 0, 2);
-    sprintf(buffer, "%d %d %d deg/s", (int)gx, (int)gy, (int)gz);
-    lcd.printString(buffer, 0, 3);
-    sprintf(buffer, "%d %d %d mG", (int)mx, (int)my, (int)mz);
-    lcd.printString(buffer, 0, 4); 
- */  
+    //tempCount = mpu9250.readTempData();  // Read the adc values
+    //temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade
+    //pc.printf(" temperature = %f  C\n\r", temperature); 
+    
+    //pc.printf("q0 = %f\n\r", q[0]);
+    //pc.printf("q1 = %f\n\r", q[1]);
+    //pc.printf("q2 = %f\n\r", q[2]);
+    //pc.printf("q3 = %f\n\r", q[3]);      
+    
+ 
   // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation.
   // In this coordinate system, the positive z-axis is down toward Earth. 
   // 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.
@@ -231,7 +193,7 @@
     roll  *= 180.0f / PI;
 
     pc.printf("Yaw, Pitch, Roll: %f %f %f\n\r", yaw, pitch, roll);
-    pc.printf("average rate = %f\n\r", (float) sumCount/sum);
+    //pc.printf("average rate = %f\n\r", (float) sumCount/sum);
 //    sprintf(buffer, "YPR: %f %f %f", yaw, pitch, roll);
 //    lcd.printString(buffer, 0, 4);
 //    sprintf(buffer, "rate = %f", (float) sumCount/sum);