Rohan-Code to measure the tilt of the ADXL355 using COG-AD4050.

Dependencies:   include ADXL355 ttmath

Fork of COG4050_adxl355_tilt by ADI_CAC

Revision:
1:ba6c18cce219
Parent:
0:e551dfd13154
Child:
2:13377441fbd8
diff -r e551dfd13154 -r ba6c18cce219 main.cpp
--- a/main.cpp	Tue Aug 21 13:26:31 2018 +0000
+++ b/main.cpp	Tue Sep 04 11:53:19 2018 +0000
@@ -1,5 +1,6 @@
 /*
-Created on: 15/08/2018
+
+Created on: 27/08/2018
 Author: Rohan Gurav
         
 Code: Use the following code to read the ADXL355 values connected to the SPI channel 
@@ -8,17 +9,15 @@
 */
 #include "mbed.h"
 #include "ADXL355.h"
-#include "complex.h"
-
 
 Serial pc(USBTX, USBRX);
 int axis = 0;
 
 ADXL355 accl(SPI1_CS0, SPI1_MOSI, SPI1_MISO, SPI1_SCLK);    // PMOD port
 
-float single_axis(float x);
-float dual_axis(float x, float y);
-float tri_axis(float x, float y, float z);
+float single_axis(float a);
+float dual_axis(float a, float b);
+float tri_axis(float a, float b, float c);
     
 int main()
 {
@@ -27,112 +26,134 @@
     pc.printf("GET device ID\r\n");
    
     accl.reset();
-    uint8_t d; 
+    uint8_t d, ad, mems, device, rev; 
     
     wait(0.2);
     
-    d=accl.read_reg(accl.DEVID_AD);
-    pc.printf("AD id = %x \r\n",d);
-    
-    d=accl.read_reg(accl.DEVID_MST);
-    pc.printf("MEMS id = %x \r\n",d);
-    
-    d=accl.read_reg(accl.PARTID);
-    pc.printf("device id = %x \r\n",d);
-    
-    d=accl.read_reg(accl.REVID);
-    pc.printf("revision id = %x \r\n",d);
+    ad = accl.read_reg(accl.DEVID_AD);
+    mems = accl.read_reg(accl.DEVID_MST);
+    device = accl.read_reg(accl.PARTID);
+    rev = accl.read_reg(accl.REVID);
     
     pc.printf("GET device data [x, y, z, t] \r\n");
+    
     accl.set_power_ctl_reg(accl.MEASUREMENT);
-    
     d=accl.read_reg(accl.POWER_CTL);
     pc.printf("power control on measurement mode = %x \r\n",d);
     
     float x,y,z,t;
+    float a,b,c;
         
-    double tilt_x, tilt_y,tilt_z;
+    double tilt_x, tilt_y, tilt_z;
+    double tilt_2x, tilt_2y;
+    double tilt_3x, tilt_3y, tilt_3z;
+        
+    //pc.printf("Enter no of axis for angle calculation (1/2/3):\r\n");
+    //pc.scanf("%d",&axis);
+    axis = 4;
+    pc.printf("||x_accl||y_accl||z_accl||x_tilt||y_tilt||z_tilt||\r\n");
+   
+    wait(1);
     
-    pc.printf("Enter no of axis for angle calculation (1/2/3):\r\n");
-    pc.scanf("%d",&axis);
-    
-    pc.printf("||x_accl||y_accl||z_accl||Temp||x_tilt||y_tilt||z_tilt||");
-    
+    //Device ID display
+        pc.printf("AD id = %x MEMS id = %x device=%x revision=%x \r\n", ad, mems, device, rev);
+        
     /*The following part is used to perform 2's complemient and then display the data*/
     while (1)
     {
-        
+                    
+        //*accl.axis355_sens
+        x = (accl.convert(accl.scanx()));
+        y = (accl.convert(accl.scany()));
+        z = (accl.convert(accl.scanz()));
+        t = 25+float(accl.scant()-1852)/(-9.05);
+                                  
+        //pc.printf("%d \t %d \t %d \r\n", accl.scanx(), accl.scany(), accl.scanz());  
+        //pc.printf("%f \t %f \t %f \r\n", x, y, z);  
+        //pc.printf("%f\r\n", asin(0.2)*57.2957);
+                                            
+        tilt_x = single_axis(x);
+        tilt_y = single_axis(y);
+        tilt_z = single_axis(z);
+    
+        tilt_2x = dual_axis(x,z);
+        tilt_2y = dual_axis(y,x);
+        tilt_2y = tilt_2y * (-1);
         
-        x = accl.convert(accl.scanx())*accl.axis355_sens;
-        y = accl.convert(accl.scany())*accl.axis355_sens;
-        z = accl.convert(accl.scanz())*accl.axis355_sens;
-        t = 25+float(accl.scant()-1852)/(-9.05);
-    
+        tilt_3x = tri_axis(x,y,z);
+        tilt_3y = tri_axis(y,x,z);
+        tilt_3z = sqrt((x*x)+(y*y));
+        tilt_3z = atan(tilt_3z/z);
+        tilt_3z = floor(tilt_3z*100)/100;
+                                    
         if (axis==1)
         {
-            tilt_x = single_axis(x);
-            tilt_y = single_axis(y);
-            tilt_z = single_axis(z);
-        
-            pc.printf("||%0.2f||%0.2f||%0.2f||%0.2f||%0.2f||%0.2f|| \r\n" , x,y,z,tilt_x,tilt_y,tilt_z);
-            wait(0.5);
-        }
+            pc.printf("||%0.2f||%0.2f||%0.2f||%0.2f||%0.2f||%0.2f|| \r\n" , x, y, z, tilt_x, tilt_y, tilt_z);
+            //pc.printf("%f", accl.axis355_sens);
+            wait(1.0);     }
         
         if (axis==2)
         {
-            tilt_x = dual_axis(x,z);
-            tilt_y = dual_axis(y,z);
-                    
-            pc.printf("||%0.2f||%0.2f||%0.2f||%0.2f||%0.2f|| \r\n" , x,y,z,tilt_x,tilt_y);
+            pc.printf("||%0.2f||%0.2f||%0.2f||%0.2f||%0.2f|| \r\n" , x*accl.axis355_sens, y*accl.axis355_sens, z*accl.axis355_sens, tilt_x, tilt_y);
             wait(0.5);
         }
       
         if (axis==3)
         {
-            tilt_x = tri_axis(x,y,z);
-            tilt_y = tri_axis(y,x,z);
-            
-            tilt_z = atan((sqrt((x*x)+(y*y)))/z);
-            tilt_z = floor(tilt_z*100)/100;
-                    
-            pc.printf("||%0.2f||%0.2f||%0.2f||%0.2f||%0.2f||%0.2f|| \r\n" , x,y,z,tilt_x,tilt_y, tilt_z);
-            wait(0.5);
+            pc.printf("||%0.2f||%0.2f||%0.2f||%0.2f||%0.2f||%0.2f|| \r\n" ,  x, y, z, tilt_x, tilt_y, tilt_z);
+            wait(1.0);
+        }
+        
+        if (axis == 4)
+        {
+            pc.printf("axis|%0.2f|%0.2f|%0.2f|%0.2f|%0.2f|%0.2f|%0.2f|%0.2f|%0.2f|%0.2f|%0.2f| \r\n" ,  x*accl.axis355_sens, y*accl.axis355_sens, z*accl.axis355_sens, tilt_x, tilt_y, tilt_z, tilt_2x, tilt_2y, tilt_3x, tilt_3y, tilt_3z);
+            wait(1.0);
         }
         
     }
 
 }
-
+//the addition of accl should be equal to 1 i.e. ( x+y+z = 1 )
 
-//single axis
-    float single_axis(float x)
+//single axis arcsin (0.2)
+    float single_axis(float a)
     {
+        a = a*accl.axis355_sens;
+        
+        if (a>1.0f)
+        { a = 1;
+        }
+        else if (a<-1)
+        { a = -1;
+        }
+        else 
+        { a = a;    
+        }
+        
         double Y;
-        //int a=4;
-        Y = floor(asin(x)*100)/100;
-        //void arm_cmplx_mag_f32  (double *Y, double *X, int32_t a);       
-
-        Y = floor(((57.2957f)*(Y))*100)/100;
+        
+        Y = asin(a);
+        Y = floor((57.2957f*Y)*100)/100;
         return Y;
                     
     }
 
-//Dual Axis
-    float dual_axis(float x, float y)
+//Dual Axis 
+    float dual_axis(float a, float b)
     {
         double Y;
-        Y = 57.2957f * (atan(x/y));
+        Y = 57.2957f * (atan(a/b));
         Y = floor(Y*100)/100;
         return Y;    
     }
 
 //Triaxial
-    float tri_axis(float x, float y, float z)
+    float tri_axis(float a, float b, float c)
     {
         double Y;
         double X;
-        X = (x)/(sqrt((y*y)+(z*z)));
+        X = (a)/(sqrt((b*b)+(c*c)));
         Y= atan(X);
-        Y = floor(Y*57.2957*100)/100;
+        Y = floor(Y*57.2957f*100)/100;
         return Y;    
     }