![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Rohan-Code to measure the tilt of the ADXL355 using COG-AD4050.
Dependencies: include ADXL355 ttmath
main.cpp@2:13377441fbd8, 2019-06-14 (annotated)
- Committer:
- RGurav
- Date:
- Fri Jun 14 10:56:53 2019 +0000
- Revision:
- 2:13377441fbd8
- Parent:
- 1:ba6c18cce219
- Child:
- 3:6af4defb733f
Cog4050 + ADXL355
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RGurav | 0:e551dfd13154 | 1 | /* |
RGurav | 1:ba6c18cce219 | 2 | |
RGurav | 1:ba6c18cce219 | 3 | Created on: 27/08/2018 |
RGurav | 0:e551dfd13154 | 4 | Author: Rohan Gurav |
RGurav | 0:e551dfd13154 | 5 | |
RGurav | 0:e551dfd13154 | 6 | Code: Use the following code to read the ADXL355 values connected to the SPI channel |
RGurav | 0:e551dfd13154 | 7 | of the EV-COG4050-Expander board port0. Check the readme.md for connection info |
RGurav | 0:e551dfd13154 | 8 | |
RGurav | 0:e551dfd13154 | 9 | */ |
RGurav | 0:e551dfd13154 | 10 | #include "mbed.h" |
RGurav | 0:e551dfd13154 | 11 | #include "ADXL355.h" |
RGurav | 0:e551dfd13154 | 12 | |
RGurav | 0:e551dfd13154 | 13 | Serial pc(USBTX, USBRX); |
RGurav | 0:e551dfd13154 | 14 | int axis = 0; |
RGurav | 0:e551dfd13154 | 15 | |
RGurav | 0:e551dfd13154 | 16 | ADXL355 accl(SPI1_CS0, SPI1_MOSI, SPI1_MISO, SPI1_SCLK); // PMOD port |
RGurav | 0:e551dfd13154 | 17 | |
RGurav | 1:ba6c18cce219 | 18 | float single_axis(float a); |
RGurav | 2:13377441fbd8 | 19 | float single_axisz(float a); |
RGurav | 1:ba6c18cce219 | 20 | float dual_axis(float a, float b); |
RGurav | 1:ba6c18cce219 | 21 | float tri_axis(float a, float b, float c); |
RGurav | 0:e551dfd13154 | 22 | |
RGurav | 0:e551dfd13154 | 23 | int main() |
RGurav | 0:e551dfd13154 | 24 | { |
RGurav | 0:e551dfd13154 | 25 | pc.baud(9600); |
RGurav | 0:e551dfd13154 | 26 | pc.printf("SPI ADXL355 and ADXL357 Demo\r\n"); |
RGurav | 0:e551dfd13154 | 27 | pc.printf("GET device ID\r\n"); |
RGurav | 0:e551dfd13154 | 28 | |
RGurav | 0:e551dfd13154 | 29 | accl.reset(); |
RGurav | 1:ba6c18cce219 | 30 | uint8_t d, ad, mems, device, rev; |
RGurav | 0:e551dfd13154 | 31 | |
RGurav | 0:e551dfd13154 | 32 | wait(0.2); |
RGurav | 0:e551dfd13154 | 33 | |
RGurav | 1:ba6c18cce219 | 34 | ad = accl.read_reg(accl.DEVID_AD); |
RGurav | 1:ba6c18cce219 | 35 | mems = accl.read_reg(accl.DEVID_MST); |
RGurav | 1:ba6c18cce219 | 36 | device = accl.read_reg(accl.PARTID); |
RGurav | 1:ba6c18cce219 | 37 | rev = accl.read_reg(accl.REVID); |
RGurav | 0:e551dfd13154 | 38 | |
RGurav | 0:e551dfd13154 | 39 | pc.printf("GET device data [x, y, z, t] \r\n"); |
RGurav | 1:ba6c18cce219 | 40 | |
RGurav | 0:e551dfd13154 | 41 | accl.set_power_ctl_reg(accl.MEASUREMENT); |
RGurav | 0:e551dfd13154 | 42 | d=accl.read_reg(accl.POWER_CTL); |
RGurav | 0:e551dfd13154 | 43 | pc.printf("power control on measurement mode = %x \r\n",d); |
RGurav | 0:e551dfd13154 | 44 | |
RGurav | 0:e551dfd13154 | 45 | float x,y,z,t; |
RGurav | 1:ba6c18cce219 | 46 | float a,b,c; |
RGurav | 0:e551dfd13154 | 47 | |
RGurav | 1:ba6c18cce219 | 48 | double tilt_x, tilt_y, tilt_z; |
RGurav | 1:ba6c18cce219 | 49 | double tilt_2x, tilt_2y; |
RGurav | 1:ba6c18cce219 | 50 | double tilt_3x, tilt_3y, tilt_3z; |
RGurav | 1:ba6c18cce219 | 51 | |
RGurav | 1:ba6c18cce219 | 52 | //pc.printf("Enter no of axis for angle calculation (1/2/3):\r\n"); |
RGurav | 1:ba6c18cce219 | 53 | //pc.scanf("%d",&axis); |
RGurav | 2:13377441fbd8 | 54 | axis = 2; |
RGurav | 1:ba6c18cce219 | 55 | pc.printf("||x_accl||y_accl||z_accl||x_tilt||y_tilt||z_tilt||\r\n"); |
RGurav | 1:ba6c18cce219 | 56 | |
RGurav | 1:ba6c18cce219 | 57 | wait(1); |
RGurav | 0:e551dfd13154 | 58 | |
RGurav | 1:ba6c18cce219 | 59 | //Device ID display |
RGurav | 1:ba6c18cce219 | 60 | pc.printf("AD id = %x MEMS id = %x device=%x revision=%x \r\n", ad, mems, device, rev); |
RGurav | 1:ba6c18cce219 | 61 | |
RGurav | 0:e551dfd13154 | 62 | /*The following part is used to perform 2's complemient and then display the data*/ |
RGurav | 0:e551dfd13154 | 63 | while (1) |
RGurav | 0:e551dfd13154 | 64 | { |
RGurav | 1:ba6c18cce219 | 65 | |
RGurav | 1:ba6c18cce219 | 66 | //*accl.axis355_sens |
RGurav | 1:ba6c18cce219 | 67 | x = (accl.convert(accl.scanx())); |
RGurav | 1:ba6c18cce219 | 68 | y = (accl.convert(accl.scany())); |
RGurav | 1:ba6c18cce219 | 69 | z = (accl.convert(accl.scanz())); |
RGurav | 1:ba6c18cce219 | 70 | t = 25+float(accl.scant()-1852)/(-9.05); |
RGurav | 1:ba6c18cce219 | 71 | |
RGurav | 0:e551dfd13154 | 72 | |
RGurav | 1:ba6c18cce219 | 73 | |
RGurav | 0:e551dfd13154 | 74 | if (axis==1) |
RGurav | 0:e551dfd13154 | 75 | { |
RGurav | 2:13377441fbd8 | 76 | char in = pc.getc(); |
RGurav | 2:13377441fbd8 | 77 | if(in == 'x') |
RGurav | 2:13377441fbd8 | 78 | {pc.printf("%0.2f" ,x); |
RGurav | 2:13377441fbd8 | 79 | } |
RGurav | 2:13377441fbd8 | 80 | |
RGurav | 2:13377441fbd8 | 81 | else if(in == 'y') |
RGurav | 2:13377441fbd8 | 82 | {pc.printf("%0.2f" ,y); |
RGurav | 2:13377441fbd8 | 83 | } |
RGurav | 2:13377441fbd8 | 84 | |
RGurav | 2:13377441fbd8 | 85 | else if(in == 'z') |
RGurav | 2:13377441fbd8 | 86 | {pc.printf("%0.2f" ,z); |
RGurav | 2:13377441fbd8 | 87 | } |
RGurav | 2:13377441fbd8 | 88 | |
RGurav | 2:13377441fbd8 | 89 | } |
RGurav | 0:e551dfd13154 | 90 | |
RGurav | 0:e551dfd13154 | 91 | if (axis==2) |
RGurav | 0:e551dfd13154 | 92 | { |
RGurav | 2:13377441fbd8 | 93 | 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); |
RGurav | 0:e551dfd13154 | 94 | wait(0.5); |
RGurav | 0:e551dfd13154 | 95 | } |
RGurav | 0:e551dfd13154 | 96 | |
RGurav | 0:e551dfd13154 | 97 | if (axis==3) |
RGurav | 0:e551dfd13154 | 98 | { |
RGurav | 1:ba6c18cce219 | 99 | 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); |
RGurav | 1:ba6c18cce219 | 100 | wait(1.0); |
RGurav | 1:ba6c18cce219 | 101 | } |
RGurav | 1:ba6c18cce219 | 102 | |
RGurav | 1:ba6c18cce219 | 103 | if (axis == 4) |
RGurav | 1:ba6c18cce219 | 104 | { |
RGurav | 1:ba6c18cce219 | 105 | 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); |
RGurav | 1:ba6c18cce219 | 106 | wait(1.0); |
RGurav | 0:e551dfd13154 | 107 | } |
RGurav | 0:e551dfd13154 | 108 | |
RGurav | 0:e551dfd13154 | 109 | } |
RGurav | 0:e551dfd13154 | 110 | |
RGurav | 0:e551dfd13154 | 111 | } |
RGurav | 1:ba6c18cce219 | 112 | //the addition of accl should be equal to 1 i.e. ( x+y+z = 1 ) |
RGurav | 0:e551dfd13154 | 113 | |
RGurav | 1:ba6c18cce219 | 114 | //single axis arcsin (0.2) |
RGurav | 1:ba6c18cce219 | 115 | float single_axis(float a) |
RGurav | 0:e551dfd13154 | 116 | { |
RGurav | 1:ba6c18cce219 | 117 | a = a*accl.axis355_sens; |
RGurav | 1:ba6c18cce219 | 118 | |
RGurav | 1:ba6c18cce219 | 119 | if (a>1.0f) |
RGurav | 1:ba6c18cce219 | 120 | { a = 1; |
RGurav | 1:ba6c18cce219 | 121 | } |
RGurav | 1:ba6c18cce219 | 122 | else if (a<-1) |
RGurav | 1:ba6c18cce219 | 123 | { a = -1; |
RGurav | 1:ba6c18cce219 | 124 | } |
RGurav | 1:ba6c18cce219 | 125 | else |
RGurav | 1:ba6c18cce219 | 126 | { a = a; |
RGurav | 1:ba6c18cce219 | 127 | } |
RGurav | 1:ba6c18cce219 | 128 | |
RGurav | 0:e551dfd13154 | 129 | double Y; |
RGurav | 1:ba6c18cce219 | 130 | |
RGurav | 1:ba6c18cce219 | 131 | Y = asin(a); |
RGurav | 1:ba6c18cce219 | 132 | Y = floor((57.2957f*Y)*100)/100; |
RGurav | 0:e551dfd13154 | 133 | return Y; |
RGurav | 0:e551dfd13154 | 134 | |
RGurav | 0:e551dfd13154 | 135 | } |
RGurav | 2:13377441fbd8 | 136 | //particullary for z axis single |
RGurav | 2:13377441fbd8 | 137 | float single_axisz(float a) |
RGurav | 2:13377441fbd8 | 138 | { |
RGurav | 2:13377441fbd8 | 139 | a = a*accl.axis355_sens; |
RGurav | 2:13377441fbd8 | 140 | |
RGurav | 2:13377441fbd8 | 141 | if (a>1.0f) |
RGurav | 2:13377441fbd8 | 142 | { a = 1; |
RGurav | 2:13377441fbd8 | 143 | } |
RGurav | 2:13377441fbd8 | 144 | else if (a<-1) |
RGurav | 2:13377441fbd8 | 145 | { a = -1; |
RGurav | 2:13377441fbd8 | 146 | } |
RGurav | 2:13377441fbd8 | 147 | else |
RGurav | 2:13377441fbd8 | 148 | { a = a; |
RGurav | 2:13377441fbd8 | 149 | } |
RGurav | 2:13377441fbd8 | 150 | |
RGurav | 2:13377441fbd8 | 151 | double Y; |
RGurav | 2:13377441fbd8 | 152 | |
RGurav | 2:13377441fbd8 | 153 | Y = acos(a); |
RGurav | 2:13377441fbd8 | 154 | Y = floor((57.2957f*Y)*100)/100; |
RGurav | 2:13377441fbd8 | 155 | return Y; |
RGurav | 2:13377441fbd8 | 156 | |
RGurav | 2:13377441fbd8 | 157 | } |
RGurav | 0:e551dfd13154 | 158 | |
RGurav | 1:ba6c18cce219 | 159 | //Dual Axis |
RGurav | 1:ba6c18cce219 | 160 | float dual_axis(float a, float b) |
RGurav | 0:e551dfd13154 | 161 | { |
RGurav | 0:e551dfd13154 | 162 | double Y; |
RGurav | 1:ba6c18cce219 | 163 | Y = 57.2957f * (atan(a/b)); |
RGurav | 0:e551dfd13154 | 164 | Y = floor(Y*100)/100; |
RGurav | 0:e551dfd13154 | 165 | return Y; |
RGurav | 0:e551dfd13154 | 166 | } |
RGurav | 0:e551dfd13154 | 167 | |
RGurav | 0:e551dfd13154 | 168 | //Triaxial |
RGurav | 1:ba6c18cce219 | 169 | float tri_axis(float a, float b, float c) |
RGurav | 0:e551dfd13154 | 170 | { |
RGurav | 0:e551dfd13154 | 171 | double Y; |
RGurav | 0:e551dfd13154 | 172 | double X; |
RGurav | 1:ba6c18cce219 | 173 | X = (a)/(sqrt((b*b)+(c*c))); |
RGurav | 0:e551dfd13154 | 174 | Y= atan(X); |
RGurav | 1:ba6c18cce219 | 175 | Y = floor(Y*57.2957f*100)/100; |
RGurav | 0:e551dfd13154 | 176 | return Y; |
RGurav | 0:e551dfd13154 | 177 | } |