Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: include ADXL355 ttmath
Fork of COG4050_adxl355_tilt by
main.cpp
00001 /* 00002 00003 Created on: 27/08/2018 00004 Author: Rohan Gurav 00005 00006 Code: Use the following code to read the ADXL355 values connected to the SPI channel 00007 of the EV-COG4050-Expander board port0. Check the readme.md for connection info 00008 00009 */ 00010 #include "mbed.h" 00011 #include "ADXL355.h" 00012 00013 Serial pc(USBTX, USBRX); 00014 int axis = 0; 00015 00016 ADXL355 accl(SPI1_CS0, SPI1_MOSI, SPI1_MISO, SPI1_SCLK); // PMOD port 00017 00018 float single_axis(float a); 00019 float single_axisz(float a); 00020 float dual_axis(float a, float b); 00021 float tri_axis(float a, float b, float c); 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, ad, mems, device, rev; 00031 00032 wait(0.2); 00033 00034 ad = accl.read_reg(accl.DEVID_AD); 00035 mems = accl.read_reg(accl.DEVID_MST); 00036 device = accl.read_reg(accl.PARTID); 00037 rev = accl.read_reg(accl.REVID); 00038 00039 pc.printf("GET device data [x, y, z, t] \r\n"); 00040 00041 accl.set_power_ctl_reg(accl.MEASUREMENT); 00042 d=accl.read_reg(accl.POWER_CTL); 00043 pc.printf("power control on measurement mode = %x \r\n",d); 00044 00045 float x,y,z,t; 00046 float a,b,c; 00047 00048 double tilt_x, tilt_y, tilt_z; 00049 double tilt_2x, tilt_2y; 00050 double tilt_3x, tilt_3y, tilt_3z; 00051 00052 //pc.printf("Enter no of axis for angle calculation (1/2/3):\r\n"); 00053 //pc.scanf("%d",&axis); 00054 axis = 2; 00055 pc.printf("||x_accl||y_accl||z_accl||x_tilt||y_tilt||z_tilt||\r\n"); 00056 00057 wait(1); 00058 00059 //Device ID display 00060 pc.printf("AD id = %x MEMS id = %x device=%x revision=%x \r\n", ad, mems, device, rev); 00061 00062 /*The following part is used to perform 2's complemient and then display the data*/ 00063 while (1) 00064 { 00065 00066 //*accl.axis355_sens 00067 x = (accl.convert(accl.scanx())); 00068 y = (accl.convert(accl.scany())); 00069 z = (accl.convert(accl.scanz())); 00070 t = 25+float(accl.scant()-1852)/(-9.05); 00071 00072 00073 00074 if (axis==1) 00075 { 00076 char in = pc.getc(); 00077 if(in == 'x') 00078 {pc.printf("%0.2f" ,x); 00079 } 00080 00081 else if(in == 'y') 00082 {pc.printf("%0.2f" ,y); 00083 } 00084 00085 else if(in == 'z') 00086 {pc.printf("%0.2f" ,z); 00087 } 00088 00089 } 00090 00091 if (axis==2) 00092 { 00093 pc.printf("||%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); 00094 wait(0.5); 00095 } 00096 00097 if (axis==3) 00098 { 00099 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); 00100 wait(1.0); 00101 } 00102 00103 if (axis == 4) 00104 { 00105 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); 00106 wait(1.0); 00107 } 00108 00109 } 00110 00111 } 00112 //the addition of accl should be equal to 1 i.e. ( x+y+z = 1 ) 00113 00114 //single axis arcsin (0.2) 00115 float single_axis(float a) 00116 { 00117 a = a*accl.axis355_sens; 00118 00119 if (a>1.0f) 00120 { a = 1; 00121 } 00122 else if (a<-1) 00123 { a = -1; 00124 } 00125 else 00126 { a = a; 00127 } 00128 00129 double Y; 00130 00131 Y = asin(a); 00132 Y = floor((57.2957f*Y)*100)/100; 00133 return Y; 00134 00135 } 00136 //particullary for z axis single 00137 float single_axisz(float a) 00138 { 00139 a = a*accl.axis355_sens; 00140 00141 if (a>1.0f) 00142 { a = 1; 00143 } 00144 else if (a<-1) 00145 { a = -1; 00146 } 00147 else 00148 { a = a; 00149 } 00150 00151 double Y; 00152 00153 Y = acos(a); 00154 Y = floor((57.2957f*Y)*100)/100; 00155 return Y; 00156 00157 } 00158 00159 //Dual Axis 00160 float dual_axis(float a, float b) 00161 { 00162 double Y; 00163 Y = 57.2957f * (atan(a/b)); 00164 Y = floor(Y*100)/100; 00165 return Y; 00166 } 00167 00168 //Triaxial 00169 float tri_axis(float a, float b, float c) 00170 { 00171 double Y; 00172 double X; 00173 X = (a)/(sqrt((b*b)+(c*c))); 00174 Y= atan(X); 00175 Y = floor(Y*57.2957f*100)/100; 00176 return Y; 00177 }
Generated on Sat Jul 16 2022 12:36:08 by
1.7.2
