Stabilizer

Dependencies:   BEAR_Protocol mbed Stabilizer iSerial

Fork of MPU9250AHRS by BE@R lab

Revision:
15:10939fd0eaac
Parent:
14:8101a48eb972
Child:
21:298aa522db64
--- a/main.cpp	Wed Dec 23 11:21:43 2015 +0000
+++ b/main.cpp	Thu Dec 24 13:49:20 2015 +0000
@@ -1,19 +1,33 @@
+#include "BEAR_Protocol.h"
 #include "Stabilizer.h"
 #include "Kinematic.h"
 #include "MPU9250.h"
 
+#include "param.h"
+
+void Init_IMU();
+void Init_Stabilizer();
+
+
 float sum = 0;
 uint32_t sumCount = 0;
 char buffer[14];
 
+
+//init class 
 MPU9250 mpu9250;
 Stabilizer Stabilize(5.0f,0.0f);
 Kinematic L('Z',10,10,30,30),R('Z',10,10,30,30);
 
+
 Timer t;
 
 Serial pc(USBTX, USBRX); // tx, rx
 
+Bear_Communicate bear(PA_15,PB_7,115200);
+
+
+
 float xmax = -4914.0f;
 float xmin = 4914.0f;
 
@@ -90,8 +104,168 @@
         while(1);
     }*/
 
+    Init_IMU();
+
+    float temp_time;
+
+    while(1) {
+        temp_time = t.read();
+        // If intPin goes high, all data registers have new data
+        if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) {  // On interrupt, check if data ready interrupt
+
+            mpu9250.readAccelData(accelCount);  // Read the x/y/z adc values
+            // Now we'll calculate the accleration value into actual g's
+            ax = (float)accelCount[0]*aRes - accelBias[0];  // get actual g value, this depends on scale being set
+            ay = (float)accelCount[1]*aRes - accelBias[1];
+            az = (float)accelCount[2]*aRes - accelBias[2];
+
+            mpu9250.readGyroData(gyroCount);  // Read the x/y/z adc values
+            // Calculate the gyro value into actual degrees per second
+            gx = (float)gyroCount[0]*gRes - gyroBias[0];  // get actual gyro value, this depends on scale being set
+            gy = (float)gyroCount[1]*gRes - gyroBias[1];
+            gz = (float)gyroCount[2]*gRes - gyroBias[2];
+
+            mpu9250.readMagData(magCount);  // Read the x/y/z adc values
+            // Calculate the magnetometer values in milliGauss
+            // Include factory calibration per data sheet and user environmental corrections
+            /*   if(magCount[0]<xmin)
+                   xmin = magCount[0];
+               if(magCount[0]>xmax)
+                   xmax = magCount[0];
+
+               if(magCount[1]<ymin)
+                   ymin = magCount[1];
+               if(magCount[1]>ymax)
+                   ymax = magCount[1];
+
+               if(magCount[2]<zmin)
+                   zmin = magCount[2];
+               if(mz>zmax)
+                   zmax = mz;
+               wait_ms(1);
+            */
+            // pc.printf("FINISH scan\r\n\r\n");
+
+//            mx = (float)magCount[0]*mRes*magCalibration[0] + magbias[0];  // get actual magnetometer value, this depends on scale being set
+//            my = (float)magCount[1]*mRes*magCalibration[1] + magbias[1];
+//            mz = (float)magCount[2]*mRes*magCalibration[2] + magbias[2];
+
+            mx = ((float)magCount[0]-xmin)*magCalibration[0] + magbias[0];  // get actual magnetometer value, this depends on scale being set
+            my = ((float)magCount[1]-ymin)*magCalibration[1] + magbias[1];
+            mz = ((float)magCount[2]-zmin)*magCalibration[2] + magbias[2];
+
+            // mx = (float)magCount[0]*1.499389499f - magbias[0];  // get actual magnetometer value, this depends on scale being set
+            // my = (float)magCount[1]*1.499389499f - magbias[1];
+            // mz = (float)magCount[2]*1.499389499f - magbias[2];
+
+
 
 
+        }
+
+        Now = t.read_us();
+        deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
+        lastUpdate = Now;
+
+        sum += deltat;
+        sumCount++;
+
+//    if(lastUpdate - firstUpdate > 10000000.0f) {
+//     beta = 0.04;  // decrease filter gain after stabilized
+//     zeta = 0.015; // increasey bias drift gain after stabilized
+//   }
+
+        // Pass gyro rate as rad/s
+        //mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f,  my,  mx, mz);
+        mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);
+
+        // Serial print and/or display at 0.5 s rate independent of data rates
+        delt_t = t.read_ms() - count;
+        if(temp_time > 8) {
+            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);
+
+                pc.printf("gx = %f", gx);
+                pc.printf(" gy = %f", gy);
+                pc.printf(" gz = %f  deg/s\n\r", gz);
+
+                pc.printf("mx = %f", mx);
+                pc.printf(" my = %f", my);
+                pc.printf(" mz = %f  mG\n\r", mz);*/
+
+                uint8_t whoami = mpu9250.readByte(AK8963_ADDRESS, AK8963_ST2);  // Read WHO_AM_I register for MPU-9250
+                // pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x10\n\r");
+                if(whoami == 0x14) {
+                    pc.printf("I AM 0x%x\n\r", whoami);
+                    while(1);
+                }
+
+
+                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.
+                // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative.
+                // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
+                // These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
+                // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be
+                // applied in the correct order which for this configuration is yaw, pitch, and then roll.
+                // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
+                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]);
+                pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
+                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]);
+
+                float Xh = mx*cos(pitch)+my*sin(roll)*sin(pitch)-mz*cos(roll)*sin(pitch);
+                float Yh = my*cos(roll)+mz*sin(roll);
+
+                float yawmag = atan2(Yh,Xh)+PI;
+                //pc.printf("Xh= %f Yh= %f ",Xh,Yh);
+                //pc.printf("Yaw[mag]= %f\n\r",yawmag*180.0f/PI);
+
+
+
+                pitch *= 180.0f / PI;
+                yaw   *= 180.0f / PI;
+                yaw   += 180.0f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04
+                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);
+
+
+                WheelChair();
+
+
+                myled= !myled;
+                count = t.read_ms();
+
+                if(count > 1<<21) {
+                    t.start(); // start the timer over again if ~30 minutes has passed
+                    count = 0;
+                    deltat= 0;
+                    lastUpdate = t.read_us();
+                }
+                sum = 0;
+                sumCount = 0;
+            }
+        }
+    }
+}
+
+void Init_IMU()
+{
+    
     //Set up I2C
     i2c.frequency(400000);  // use fast (400 kHz) I2C
 
@@ -236,160 +410,13 @@
     pc.printf("mag[2] %f\n\r",magbias[2]);
     //     resalt = atan(magY+((yMin-yMax)/2),magX+(xMin-xMax)/2))*180/PI;
 
-
-    float temp_time;
-
-    while(1) {
-        temp_time = t.read();
-        // If intPin goes high, all data registers have new data
-        if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) {  // On interrupt, check if data ready interrupt
-
-            mpu9250.readAccelData(accelCount);  // Read the x/y/z adc values
-            // Now we'll calculate the accleration value into actual g's
-            ax = (float)accelCount[0]*aRes - accelBias[0];  // get actual g value, this depends on scale being set
-            ay = (float)accelCount[1]*aRes - accelBias[1];
-            az = (float)accelCount[2]*aRes - accelBias[2];
-
-            mpu9250.readGyroData(gyroCount);  // Read the x/y/z adc values
-            // Calculate the gyro value into actual degrees per second
-            gx = (float)gyroCount[0]*gRes - gyroBias[0];  // get actual gyro value, this depends on scale being set
-            gy = (float)gyroCount[1]*gRes - gyroBias[1];
-            gz = (float)gyroCount[2]*gRes - gyroBias[2];
-
-            mpu9250.readMagData(magCount);  // Read the x/y/z adc values
-            // Calculate the magnetometer values in milliGauss
-            // Include factory calibration per data sheet and user environmental corrections
-            /*   if(magCount[0]<xmin)
-                   xmin = magCount[0];
-               if(magCount[0]>xmax)
-                   xmax = magCount[0];
-
-               if(magCount[1]<ymin)
-                   ymin = magCount[1];
-               if(magCount[1]>ymax)
-                   ymax = magCount[1];
-
-               if(magCount[2]<zmin)
-                   zmin = magCount[2];
-               if(mz>zmax)
-                   zmax = mz;
-               wait_ms(1);
-            */
-            // pc.printf("FINISH scan\r\n\r\n");
-
-//            mx = (float)magCount[0]*mRes*magCalibration[0] + magbias[0];  // get actual magnetometer value, this depends on scale being set
-//            my = (float)magCount[1]*mRes*magCalibration[1] + magbias[1];
-//            mz = (float)magCount[2]*mRes*magCalibration[2] + magbias[2];
-
-            mx = ((float)magCount[0]-xmin)*magCalibration[0] + magbias[0];  // get actual magnetometer value, this depends on scale being set
-            my = ((float)magCount[1]-ymin)*magCalibration[1] + magbias[1];
-            mz = ((float)magCount[2]-zmin)*magCalibration[2] + magbias[2];
-
-            // mx = (float)magCount[0]*1.499389499f - magbias[0];  // get actual magnetometer value, this depends on scale being set
-            // my = (float)magCount[1]*1.499389499f - magbias[1];
-            // mz = (float)magCount[2]*1.499389499f - magbias[2];
-
-
+}
 
 
-        }
-
-        Now = t.read_us();
-        deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
-        lastUpdate = Now;
-
-        sum += deltat;
-        sumCount++;
-
-//    if(lastUpdate - firstUpdate > 10000000.0f) {
-//     beta = 0.04;  // decrease filter gain after stabilized
-//     zeta = 0.015; // increasey bias drift gain after stabilized
-//   }
-
-        // Pass gyro rate as rad/s
-        //mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f,  my,  mx, mz);
-        mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);
-
-        // Serial print and/or display at 0.5 s rate independent of data rates
-        delt_t = t.read_ms() - count;
-        if(temp_time > 8) {
-            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);
-
-                pc.printf("gx = %f", gx);
-                pc.printf(" gy = %f", gy);
-                pc.printf(" gz = %f  deg/s\n\r", gz);
-
-                pc.printf("mx = %f", mx);
-                pc.printf(" my = %f", my);
-                pc.printf(" mz = %f  mG\n\r", mz);*/
-
-                whoami = mpu9250.readByte(AK8963_ADDRESS, AK8963_ST2);  // Read WHO_AM_I register for MPU-9250
-                // pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x10\n\r");
-                if(whoami == 0x14) {
-                    pc.printf("I AM 0x%x\n\r", whoami);
-                    while(1);
-                }
-
-
-                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.
-                // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative.
-                // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
-                // These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
-                // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be
-                // applied in the correct order which for this configuration is yaw, pitch, and then roll.
-                // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
-                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]);
-                pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
-                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]);
-
-                float Xh = mx*cos(pitch)+my*sin(roll)*sin(pitch)-mz*cos(roll)*sin(pitch);
-                float Yh = my*cos(roll)+mz*sin(roll);
-
-                float yawmag = atan2(Yh,Xh)+PI;
-                //pc.printf("Xh= %f Yh= %f ",Xh,Yh);
-                //pc.printf("Yaw[mag]= %f\n\r",yawmag*180.0f/PI);
-
-
-
-                pitch *= 180.0f / PI;
-                yaw   *= 180.0f / PI;
-                yaw   += 180.0f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04
-                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);
-
-
-                WheelChair();
-
-
-                myled= !myled;
-                count = t.read_ms();
-
-                if(count > 1<<21) {
-                    t.start(); // start the timer over again if ~30 minutes has passed
-                    count = 0;
-                    deltat= 0;
-                    lastUpdate = t.read_us();
-                }
-                sum = 0;
-                sumCount = 0;
-            }
-        }
-    }
-}
+void Init_Stabilizer()
+{
+    
+    
+    
+    
+    }
\ No newline at end of file