General IMU Library

Dependencies:   mbed LSM9DS1_Library

Files at this revision

API Documentation at this revision

Comitter:
jcallahan1
Date:
Thu Feb 16 18:25:00 2017 +0000
Parent:
1:419c1baddb68
Child:
3:3b572eec3f20
Commit message:
Error in constructor

Changed in this revision

IMU.cpp Show annotated file Show diff for this revision Revisions of this file
IMU.h Show annotated file Show diff for this revision Revisions of this file
LSM9DS1_Library.lib Show annotated file Show diff for this revision Revisions of this file
--- a/IMU.cpp	Thu Feb 09 20:14:25 2017 +0000
+++ b/IMU.cpp	Thu Feb 16 18:25:00 2017 +0000
@@ -1,1 +1,66 @@
-//edit
\ No newline at end of file
+#include "mbed.h"
+#include "LSM9DS1.h"
+#include "IMU.h"
+
+#define PI 3.1514
+
+IMU::IMU() {
+
+    LSM9DS1 I(p9, p10, 0xD6, 0x3C);
+    I.begin();
+    if (!I.begin()) {
+        printf("Failed to communicate with LSM9DS1.\n");
+    }
+    I.calibrate(1);
+    bool state;
+    float ax01 = I.calcAccel(I.ax);
+    float ay01 = I.calcAccel(I.ay);
+    float az01 = I.calcAccel(I.az);
+    float ax0 = ax01;
+    float ay0 = ay01;
+    float az0 = az01; 
+    float ax;
+    float ay;
+    float az;
+    float theta;
+}
+    
+void IMU::trackBed()
+{
+    if (state != 0) {
+        return;
+    }
+    else {
+        while(state == 0) {
+        
+            while(!I.accelAvailable());
+            I.readAccel();
+        
+             // current a = <ax,ay,az> 
+            ax = I.calcAccel(I.ax);
+            ay = I.calcAccel(I.ay);
+            az = I.calcAccel(I.az);
+            // theta in degrees between a0 and a
+            theta = acos((ax0*ax + ay0*ay + az0*az)/(sqrt(ax0*ax0+ay0*ay0+az0*az0)*sqrt(ax*ax+ay*ay+az*az)))*180/PI;
+        }
+    }
+}
+        
+void IMU::trackHeading()
+{
+    if (state != 1) {
+        return;
+    }
+    else {
+        while (state == 1) {
+            
+            while(!I.accelAvailable());
+            
+            I.readAccel();
+            ax0 = I.calcAccel(I.ax);
+            ay0 = I.calcAccel(I.ay);
+            az0 = I.calcAccel(I.az);
+        }
+    }
+}
+            
\ No newline at end of file
--- a/IMU.h	Thu Feb 09 20:14:25 2017 +0000
+++ b/IMU.h	Thu Feb 16 18:25:00 2017 +0000
@@ -0,0 +1,49 @@
+/**
+*   @file   IMU.h
+*
+*   @brief  Class for reading the accelerometer
+*
+*   @author Terrabots Team
+*
+*/
+
+#ifndef IMU_H
+#define IMU_H
+ 
+#include "mbed.h"
+#include "LSM9DS1.h"
+
+class IMU {
+    public:
+        /**
+        *   Constructor for the IMU
+        *
+        *   @param[in] _state
+        */
+        IMU();
+        IMU() : _I(p9, p10, 0xD6, 0x3C){};
+        /**
+        *   Track the angle of the truck bed
+        */
+        void trackBed();
+        /**
+        *   Track the angle of the truck as it moves
+        */
+        void trackHeading();
+        
+    private:
+        bool state;
+        float ax01;
+        float ay01;
+        float az01;
+        float ax0;
+        float ay0;
+        float az0; 
+        float ax;
+        float ay;
+        float az;
+        float theta;
+        LSM9DS1 _I;
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LSM9DS1_Library.lib	Thu Feb 16 18:25:00 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/jmar7/code/LSM9DS1_Library/#93b89ce73b68