ENSMM / Mbed 2 deprecated IMU

Dependencies:   mbed USBDevice

Files at this revision

API Documentation at this revision

Comitter:
zmoutaou
Date:
Mon Jan 27 10:47:24 2020 +0000
Parent:
3:fd549671e512
Commit message:
LSM9DS1 first example

Changed in this revision

LSM9DS1.cpp Show annotated file Show diff for this revision Revisions of this file
LSM9DS1.h Show annotated file Show diff for this revision Revisions of this file
PinDetect.lib Show diff for this revision Revisions of this file
USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/LSM9DS1.cpp	Sun Mar 13 21:40:10 2016 +0000
+++ b/LSM9DS1.cpp	Mon Jan 27 10:47:24 2020 +0000
@@ -41,8 +41,9 @@
 LSM9DS1::LSM9DS1(PinName sda, PinName scl, uint8_t xgAddr, uint8_t mAddr)
     :i2c(sda, scl)
 {
-    init(IMU_MODE_I2C, xgAddr, mAddr); // dont know about 0xD6 or 0x3B
-}
+I2C i2c(PB_9, PB_8);
+init(IMU_MODE_I2C, xgAddr, mAddr);} // dont know about 0xD6 or 0x3B
+
 /*
 LSM9DS1::LSM9DS1()
 {
@@ -55,6 +56,7 @@
 }
 */
 
+
 void LSM9DS1::init(interface_mode interface, uint8_t xgAddr, uint8_t mAddr)
 {
     settings.device.commInterface = interface;
@@ -170,7 +172,7 @@
     // each device. Store those in a variable so we can return them.
     uint8_t mTest = mReadByte(WHO_AM_I_M);      // Read the gyro WHO_AM_I
     uint8_t xgTest = xgReadByte(WHO_AM_I_XG);   // Read the accel/mag WHO_AM_I
-    pc.printf("%x, %x, %x, %x\n\r", mTest, xgTest, _xgAddress, _mAddress);
+    ////pc.printf("%x, %x, %x, %x\n\r", mTest, xgTest, _xgAddress, _mAddress);
     uint16_t whoAmICombined = (xgTest << 8) | mTest;
     
     if (whoAmICombined != ((WHO_AM_I_AG_RSP << 8) | WHO_AM_I_M_RSP))
@@ -1195,3 +1197,5 @@
     }
     return count;
 }
+
+
--- a/LSM9DS1.h	Sun Mar 13 21:40:10 2016 +0000
+++ b/LSM9DS1.h	Mon Jan 27 10:47:24 2020 +0000
@@ -34,8 +34,11 @@
 #include "LSM9DS1_Registers.h"
 #include "LSM9DS1_Types.h"
 
-#define LSM9DS1_AG_ADDR(sa0)    ((sa0) == 0 ? 0x6A : 0x6B)
-#define LSM9DS1_M_ADDR(sa1)     ((sa1) == 0 ? 0x1C : 0x1E)
+#define LSM9DS1_AG_ADDR 0xD4  //  Device address when ADO = 1
+#define LSM9DS1_M_ADDR  0x38  //  Address of magnetometer
+
+//#define LSM9DS1_AG_ADDR(sa0)    ((sa0) == 0 ? 0x6A : 0x6B)
+//#define LSM9DS1_M_ADDR(sa1)     ((sa1) == 0 ? 0x1C : 0x1E)
 
 enum lsm9ds1_axis {
     X_AXIS,
@@ -83,7 +86,8 @@
     * in the IMUSettings struct will take effect after calling this function.
     */
     uint16_t begin();
-    
+    void selftestLSM9DS1();
+    void accelgyrocalLSM9DS1(float * dest1, float * dest2);
     void calibrate(bool autoCalc = true);
     void calibrateMag(bool loadIn = true);
     void magOffset(uint8_t axis, int16_t offset);
@@ -550,6 +554,7 @@
     //      all stored in the *dest array given.
     uint8_t I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count);
     
+    
 private:
     I2C i2c;
 };
--- a/PinDetect.lib	Sun Mar 13 21:40:10 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Mon Jan 27 10:47:24 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/USBDevice/#53949e6131f6
--- a/main.cpp	Sun Mar 13 21:40:10 2016 +0000
+++ b/main.cpp	Mon Jan 27 10:47:24 2020 +0000
@@ -1,84 +1,53 @@
 #include "LSM9DS1.h"
 #include "MadgwickUpdate.h"
 
-Serial pc(USBTX, USBRX);
-
+#include "USBSerial.h"
+USBSerial pc(0x1f00, 0x2012, 0x0001, false);
 Timer t;
 int lastPrint = 0;
-int printInterval = 500;
-
-RawSerial dev( p13, p14 );
-
-int calibrate = 1;
-
-void dev_recv()
+int printInterval = 3;
+// https://github.com/kriswiner/LSM9DS1/blob/master/LSM9DS1_MS5611_BasicAHRS_t3.ino
+int main()
 {
-    while(dev.readable()) {
-        if ( dev.getc() == 'r' ){
-                calibrate = 1;
-        }
-    }
-}
 
-int main() {
-    
-    LSM9DS1 lol(p28, p27, 0xD6, 0x3C);
-    
+    wait(1);
+    pc.printf("HOLLA ! .\n");
+    LSM9DS1 lol(PB_9, PB_8, 0xD4, 0x38);
     t.start();
-
-    dev.baud(9600);
-    dev.attach(&dev_recv, Serial::RxIrq);
-
     lol.begin();
     if (!lol.begin()) {
         pc.printf("Failed to communicate with LSM9DS1.\n");
     }
-
+    lol.calibrate();
     while(1) {
+        firstUpdate = t.read_ms();
+        lastUpdate = firstUpdate;
+        q[0] = 1.0f;q[1] = 0.0f;q[2] = 0.0f;q[3] = 0.0f;
+        if ( lol.accelAvailable() || lol.gyroAvailable() ) {
 
-        if( calibrate ){
-            lol.calibrate();
-            firstUpdate = t.read_us();
-            lastUpdate = firstUpdate;
-            q[0] = 1.0f; q[1] = 0.0f; q[2] = 0.0f; q[3] = 0.0f; 
-            calibrate = 0;
-        }
-        
-        if ( lol.accelAvailable() || lol.gyroAvailable() ) {
-         
             lol.readAccel();
             lol.readGyro();
-            
-            Now = t.read_us();
+
+            Now = t.read_ms();
             deltat = (float)((Now - lastUpdate)/1000000.0f);
             lastUpdate = Now;
-            
-            float ax = lol.calcAccel(lol.ax);
-            float ay = lol.calcAccel(lol.ay);
-            float az = lol.calcAccel(lol.az);
-            
-            float gx = lol.calcGyro(lol.gx);
-            float gy = lol.calcGyro(lol.gy);
-            float gz = lol.calcGyro(lol.gz);
-            
-            
+
+            float ax = lol.calcAccel(lol.ax);float ay = lol.calcAccel(lol.ay);float az = lol.calcAccel(lol.az);
+            float gx = lol.calcGyro(lol.gx);float gy = lol.calcGyro(lol.gy);float gz = lol.calcGyro(lol.gz);
+            float mx = lol.calcMag(lol.mx);float my = lol.calcMag(lol.my);float mz = lol.calcMag(lol.mz);
             // switch x and y to convert to right handed coordinate system
-            MadgwickQuaternionUpdate( ay, ax, az, gy*PI/180.0f, gx*PI/180.0f, gz*PI/180.0f );
-            
+            //MadgwickQuaternionUpdate( ay, ax, az, gy*PI/180.0f, gx*PI/180.0f, gz*PI/180.0f );
+
             int nowMs = t.read_ms();
-            if( nowMs - lastPrint > printInterval ){
+            if( nowMs - lastPrint > printInterval ) {
                 lastPrint = nowMs;
-                pc.printf("gyro: %f %f %f\n\r", gx, gy, gz);
-                pc.printf("accel: %f %f %f\n\r", ax, ay, az);
-                pc.printf("quat: %f %f %f %f\n\r", q[0], q[1], q[2], q[3]);
-                char* str = (char*) &q;
-                int i = 0;
-                while (i < 16){
-                    dev.putc( str[i] );
-                    i += 1;   
-                }
+                //pc.printf("gyro: %f %f %f\n\r", gx, gy, gz);
+                //pc.printf("accel: %f %f %f\n\r", ax, ay, az);
+                //pc.printf("quat: %f %f %f %f\n\r", q[0], q[1], q[2], q[3]);
+                pc.printf("%d %f,%f,%f,%f,%f,%f\n",nowMs,ax,ay,az,gx,gy,gz);
+
             }
-            
         }
     }
 }
+
--- a/mbed.bld	Sun Mar 13 21:40:10 2016 +0000
+++ b/mbed.bld	Mon Jan 27 10:47:24 2020 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/f141b2784e32
\ No newline at end of file
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file