ADI_CAC / Mbed OS COG4050_adxl355_tilt

Dependencies:   ADXL355 include ttmath

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 Created on: 15/08/2018
00003 Author: Rohan Gurav
00004         
00005 Code: Use the following code to read the ADXL355 values connected to the SPI channel 
00006       of the EV-COG4050-Expander board port0. Check the readme.md for connection info  
00007       
00008 */
00009 #include "mbed.h"
00010 #include "ADXL355.h"
00011 #include "complex.h"
00012 
00013 
00014 Serial pc(USBTX, USBRX);
00015 int axis = 0;
00016 
00017 ADXL355 accl(SPI1_CS0, SPI1_MOSI, SPI1_MISO, SPI1_SCLK);    // PMOD port
00018 
00019 float single_axis(float x);
00020 float dual_axis(float x, float y);
00021 float tri_axis(float x, float y, float z);
00022     
00023 int main()
00024 {
00025     pc.baud(9600);
00026     pc.printf("SPI ADXL355 and ADXL357 Demo\r\n");
00027     pc.printf("GET device ID\r\n");
00028    
00029     accl.reset();
00030     uint8_t d; 
00031     
00032     wait(0.2);
00033     
00034     d=accl.read_reg(accl.DEVID_AD);
00035     pc.printf("AD id = %x \r\n",d);
00036     
00037     d=accl.read_reg(accl.DEVID_MST);
00038     pc.printf("MEMS id = %x \r\n",d);
00039     
00040     d=accl.read_reg(accl.PARTID);
00041     pc.printf("device id = %x \r\n",d);
00042     
00043     d=accl.read_reg(accl.REVID);
00044     pc.printf("revision id = %x \r\n",d);
00045     
00046     pc.printf("GET device data [x, y, z, t] \r\n");
00047     accl.set_power_ctl_reg(accl.MEASUREMENT);
00048     
00049     d=accl.read_reg(accl.POWER_CTL);
00050     pc.printf("power control on measurement mode = %x \r\n",d);
00051     
00052     float x,y,z,t;
00053         
00054     double tilt_x, tilt_y,tilt_z;
00055     
00056     pc.printf("Enter no of axis for angle calculation (1/2/3):\r\n");
00057     pc.scanf("%d",&axis);
00058     
00059     pc.printf("||x_accl||y_accl||z_accl||Temp||x_tilt||y_tilt||z_tilt||");
00060     
00061     /*The following part is used to perform 2's complemient and then display the data*/
00062     while (1)
00063     {
00064         
00065         
00066         x = accl.convert(accl.scanx())*accl.axis355_sens;
00067         y = accl.convert(accl.scany())*accl.axis355_sens;
00068         z = accl.convert(accl.scanz())*accl.axis355_sens;
00069         t = 25+float(accl.scant()-1852)/(-9.05);
00070     
00071         if (axis==1)
00072         {
00073             tilt_x = single_axis(x);
00074             tilt_y = single_axis(y);
00075             tilt_z = single_axis(z);
00076         
00077             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);
00078             wait(0.5);
00079         }
00080         
00081         if (axis==2)
00082         {
00083             tilt_x = dual_axis(x,z);
00084             tilt_y = dual_axis(y,z);
00085                     
00086             pc.printf("||%0.2f||%0.2f||%0.2f||%0.2f||%0.2f|| \r\n" , x,y,z,tilt_x,tilt_y);
00087             wait(0.5);
00088         }
00089       
00090         if (axis==3)
00091         {
00092             tilt_x = tri_axis(x,y,z);
00093             tilt_y = tri_axis(y,x,z);
00094             
00095             tilt_z = atan((sqrt((x*x)+(y*y)))/z);
00096             tilt_z = floor(tilt_z*100)/100;
00097                     
00098             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);
00099             wait(0.5);
00100         }
00101         
00102     }
00103 
00104 }
00105 
00106 
00107 //single axis
00108     float single_axis(float x)
00109     {
00110         double Y;
00111         //int a=4;
00112         Y = floor(asin(x)*100)/100;
00113         //void arm_cmplx_mag_f32  (double *Y, double *X, int32_t a);       
00114 
00115         Y = floor(((57.2957f)*(Y))*100)/100;
00116         return Y;
00117                     
00118     }
00119 
00120 //Dual Axis
00121     float dual_axis(float x, float y)
00122     {
00123         double Y;
00124         Y = 57.2957f * (atan(x/y));
00125         Y = floor(Y*100)/100;
00126         return Y;    
00127     }
00128 
00129 //Triaxial
00130     float tri_axis(float x, float y, float z)
00131     {
00132         double Y;
00133         double X;
00134         X = (x)/(sqrt((y*y)+(z*z)));
00135         Y= atan(X);
00136         Y = floor(Y*57.2957*100)/100;
00137         return Y;    
00138     }