A class to receive data from the SparkFun 9DOF Razor IMU. It can be easily adapted to work with IMUs with different data formats.

Dependencies:   mbed

Revision:
2:d8b182fbe018
Parent:
1:fdfa313b9cc3
Child:
4:8f63393d49fb
--- a/main.cpp	Mon Oct 17 15:19:01 2011 +0000
+++ b/main.cpp	Fri Nov 04 22:24:02 2011 +0000
@@ -1,9 +1,9 @@
 /*
  * Demo to relay I/O between a computer and the IMU. Make sure to connect the GND wire on the IMU to pin 1 (GND) on the mbed so there's a return current
  * Updated to use interrupts - this will help when we intergrate this code into AVNavControl
- * 9dof razor from sparkfun
+ * 9dof razor from sparkfun, http://www.sparkfun.com/products/10736
  */
-
+/*
 #define GYRO_SCALE 14.375  // ticks per degree, http://www.sparkfun.com/datasheets/Sensors/Gyro/PS-ITG-3200-00-01.4.pdf 
 
 #include "mbed.h"
@@ -29,7 +29,10 @@
 int i_IMU;
 char bufIMU[21];
 
+AnalogOut aout(p18);
+
 int main() {
+    aout = 1.0;
     // Set up the connection. Read up about parity and stop bits if this is confusing.
     IMU.format(8, Serial::None, 1);
     PC.format(8, Serial::None, 1);
@@ -69,23 +72,41 @@
                 }
 
                 // End of the set of data. Parse the buffer
-                else if (i_IMU == 21 /*&& bufIMU[0] == '$' && bufIMU[20] == '\n' data == '\n' && i_IMU == 19*/) {
+                else if (i_IMU == 21) { && bufIMU[0] == '$' && bufIMU[20] == '\n' data == '\n' && i_IMU == 19) {
                     printf("Parsing\n\r");
                     
+                    //This should be easier to understand than bitshifts. bufIMU is a pointer (arrays are pointers).
+                    //We offset it to the start of the desired value. We then typecast bufIMU into a short (16 bit).
+                    //This turns bufIMU into a pointer to the desired value. Now we dereference it.
+                    //I'm not sure if we'll still need makeCorrect. Adit, check plz <3
+                    accX = *((short*)(bufIMU + 1));
+                    accY = *((short*)(bufIMU + 3));
+                    accZ = *((short*)(bufIMU + 5));
+                    
+                    gyrX = *((short*)(bufIMU + 7));
+                    gyrY = *((short*)(bufIMU + 9));
+                    gyrZ = *((short*)(bufIMU + 11));
+                    
+                    magX = *((short*)(bufIMU + 13));
+                    magY = *((short*)(bufIMU + 15));
+                    magZ = *((short*)(bufIMU + 17));
+                    
                     //bufIMU contains binary data. Each variable sent by the IMU is a 16-bit integer
                     //broken down into two characters (in bufIMU[]). Here, we reconstitute the original integer by
                     //left-shifting the first character by 8 bits and ORing it with the second character.
-                    accX = (bufIMU[1]<<8 | bufIMU[2]);
-                    accY = (bufIMU[3]<<8 | bufIMU[4]);
-                    accZ = (bufIMU[5]<<8 | bufIMU[6]);
+                    
+                    
+                    //accX = (bufIMU[1]<<8 | bufIMU[2]);
+                    //accY = (bufIMU[3]<<8 | bufIMU[4]);
+                    //accZ = (bufIMU[5]<<8 | bufIMU[6]);
 
-                    gyrX = (bufIMU[7]<<8 | bufIMU[8]);
-                    gyrY = (bufIMU[9]<<8 | bufIMU[10]);
-                    gyrZ = (bufIMU[11]<<8 | bufIMU[12]);
+                    //gyrX = (bufIMU[7]<<8 | bufIMU[8]);
+                    //gyrY = (bufIMU[9]<<8 | bufIMU[10]);
+                    //gyrZ = (bufIMU[11]<<8 | bufIMU[12]);
 
-                    magX = (bufIMU[13]<<8 | bufIMU[14]);
-                    magY = (bufIMU[15]<<8 | bufIMU[16]);
-                    magZ = (bufIMU[17]<<8 | bufIMU[18]);
+                    //magX = (bufIMU[13]<<8 | bufIMU[14]);
+                    //magY = (bufIMU[15]<<8 | bufIMU[16]);
+                    //magZ = (bufIMU[17]<<8 | bufIMU[18]);
                     
                     makeCorrect(&accX);
                     makeCorrect(&accY);
@@ -104,12 +125,12 @@
 
                     //accX = accY = accZ = gyrX = gyrY = gyrZ = magX = magY = magZ = 0;
 
-                    /*
-                    for (int i = 0; i < 21; i++) {
-                        PC.printf("%d  ", bufIMU[i]);
-                    }
-                    PC.printf("\n\r");
-                    */
+                    
+                    //for (int i = 0; i < 21; i++) {
+                    //    PC.printf("%d  ", bufIMU[i]);
+                    //}
+                    //PC.printf("\n\r");
+                    
                     //newIMUData = 1; // Update the flag
                 }
 
@@ -127,11 +148,11 @@
             PCreadable = false;
             myled1 = 0;
         }
-        /*if (parseNow) {
-            parse(buffer);
-            buffer.clear();
-            parseNow = false;
-        }*/
+        //if (parseNow) {
+        //    parse(buffer);
+        //    buffer.clear();
+        //    parseNow = false;
+        //}
     }
 }
 
@@ -150,10 +171,12 @@
 }
 
 
-//The bitshift performed always creates a positive integer. Sometimes, the IMU
-//values are negative. This fixes that - it centers the value around 512. That is,
-//512 is 0 for the IMU variables. Values above it are positive, and values below it are negative.
+//So negative numbers that are transferred are in twos complement form
+// and the compiler seems to like to use things created by bitwise operators
+// in unsigned form so all of the bits are switched and we switch it back and center
+// it around 512 which we are using as out zero value
 inline void makeCorrect (short* i) {
     if ((*i)>>15) *i = 512 - (~(*i));
     else *i = 512 + *i;
-}
\ No newline at end of file
+}
+*/
\ No newline at end of file