Rohan-Code to measure the tilt of the ADXL355 using COG-AD4050.
Dependencies: include ADXL355 ttmath
main.cpp@1:ba6c18cce219, 2018-09-04 (annotated)
- Committer:
- RGurav
- Date:
- Tue Sep 04 11:53:19 2018 +0000
- Revision:
- 1:ba6c18cce219
- Parent:
- 0:e551dfd13154
- Child:
- 2:13377441fbd8
Rohan-Code to measure the tilt of the ADXL355 using COG-AD4050.
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 | 1:ba6c18cce219 | 19 | float dual_axis(float a, float b); |
RGurav | 1:ba6c18cce219 | 20 | float tri_axis(float a, float b, float c); |
RGurav | 0:e551dfd13154 | 21 | |
RGurav | 0:e551dfd13154 | 22 | int main() |
RGurav | 0:e551dfd13154 | 23 | { |
RGurav | 0:e551dfd13154 | 24 | pc.baud(9600); |
RGurav | 0:e551dfd13154 | 25 | pc.printf("SPI ADXL355 and ADXL357 Demo\r\n"); |
RGurav | 0:e551dfd13154 | 26 | pc.printf("GET device ID\r\n"); |
RGurav | 0:e551dfd13154 | 27 | |
RGurav | 0:e551dfd13154 | 28 | accl.reset(); |
RGurav | 1:ba6c18cce219 | 29 | uint8_t d, ad, mems, device, rev; |
RGurav | 0:e551dfd13154 | 30 | |
RGurav | 0:e551dfd13154 | 31 | wait(0.2); |
RGurav | 0:e551dfd13154 | 32 | |
RGurav | 1:ba6c18cce219 | 33 | ad = accl.read_reg(accl.DEVID_AD); |
RGurav | 1:ba6c18cce219 | 34 | mems = accl.read_reg(accl.DEVID_MST); |
RGurav | 1:ba6c18cce219 | 35 | device = accl.read_reg(accl.PARTID); |
RGurav | 1:ba6c18cce219 | 36 | rev = accl.read_reg(accl.REVID); |
RGurav | 0:e551dfd13154 | 37 | |
RGurav | 0:e551dfd13154 | 38 | pc.printf("GET device data [x, y, z, t] \r\n"); |
RGurav | 1:ba6c18cce219 | 39 | |
RGurav | 0:e551dfd13154 | 40 | accl.set_power_ctl_reg(accl.MEASUREMENT); |
RGurav | 0:e551dfd13154 | 41 | d=accl.read_reg(accl.POWER_CTL); |
RGurav | 0:e551dfd13154 | 42 | pc.printf("power control on measurement mode = %x \r\n",d); |
RGurav | 0:e551dfd13154 | 43 | |
RGurav | 0:e551dfd13154 | 44 | float x,y,z,t; |
RGurav | 1:ba6c18cce219 | 45 | float a,b,c; |
RGurav | 0:e551dfd13154 | 46 | |
RGurav | 1:ba6c18cce219 | 47 | double tilt_x, tilt_y, tilt_z; |
RGurav | 1:ba6c18cce219 | 48 | double tilt_2x, tilt_2y; |
RGurav | 1:ba6c18cce219 | 49 | double tilt_3x, tilt_3y, tilt_3z; |
RGurav | 1:ba6c18cce219 | 50 | |
RGurav | 1:ba6c18cce219 | 51 | //pc.printf("Enter no of axis for angle calculation (1/2/3):\r\n"); |
RGurav | 1:ba6c18cce219 | 52 | //pc.scanf("%d",&axis); |
RGurav | 1:ba6c18cce219 | 53 | axis = 4; |
RGurav | 1:ba6c18cce219 | 54 | pc.printf("||x_accl||y_accl||z_accl||x_tilt||y_tilt||z_tilt||\r\n"); |
RGurav | 1:ba6c18cce219 | 55 | |
RGurav | 1:ba6c18cce219 | 56 | wait(1); |
RGurav | 0:e551dfd13154 | 57 | |
RGurav | 1:ba6c18cce219 | 58 | //Device ID display |
RGurav | 1:ba6c18cce219 | 59 | pc.printf("AD id = %x MEMS id = %x device=%x revision=%x \r\n", ad, mems, device, rev); |
RGurav | 1:ba6c18cce219 | 60 | |
RGurav | 0:e551dfd13154 | 61 | /*The following part is used to perform 2's complemient and then display the data*/ |
RGurav | 0:e551dfd13154 | 62 | while (1) |
RGurav | 0:e551dfd13154 | 63 | { |
RGurav | 1:ba6c18cce219 | 64 | |
RGurav | 1:ba6c18cce219 | 65 | //*accl.axis355_sens |
RGurav | 1:ba6c18cce219 | 66 | x = (accl.convert(accl.scanx())); |
RGurav | 1:ba6c18cce219 | 67 | y = (accl.convert(accl.scany())); |
RGurav | 1:ba6c18cce219 | 68 | z = (accl.convert(accl.scanz())); |
RGurav | 1:ba6c18cce219 | 69 | t = 25+float(accl.scant()-1852)/(-9.05); |
RGurav | 1:ba6c18cce219 | 70 | |
RGurav | 1:ba6c18cce219 | 71 | //pc.printf("%d \t %d \t %d \r\n", accl.scanx(), accl.scany(), accl.scanz()); |
RGurav | 1:ba6c18cce219 | 72 | //pc.printf("%f \t %f \t %f \r\n", x, y, z); |
RGurav | 1:ba6c18cce219 | 73 | //pc.printf("%f\r\n", asin(0.2)*57.2957); |
RGurav | 1:ba6c18cce219 | 74 | |
RGurav | 1:ba6c18cce219 | 75 | tilt_x = single_axis(x); |
RGurav | 1:ba6c18cce219 | 76 | tilt_y = single_axis(y); |
RGurav | 1:ba6c18cce219 | 77 | tilt_z = single_axis(z); |
RGurav | 1:ba6c18cce219 | 78 | |
RGurav | 1:ba6c18cce219 | 79 | tilt_2x = dual_axis(x,z); |
RGurav | 1:ba6c18cce219 | 80 | tilt_2y = dual_axis(y,x); |
RGurav | 1:ba6c18cce219 | 81 | tilt_2y = tilt_2y * (-1); |
RGurav | 0:e551dfd13154 | 82 | |
RGurav | 1:ba6c18cce219 | 83 | tilt_3x = tri_axis(x,y,z); |
RGurav | 1:ba6c18cce219 | 84 | tilt_3y = tri_axis(y,x,z); |
RGurav | 1:ba6c18cce219 | 85 | tilt_3z = sqrt((x*x)+(y*y)); |
RGurav | 1:ba6c18cce219 | 86 | tilt_3z = atan(tilt_3z/z); |
RGurav | 1:ba6c18cce219 | 87 | tilt_3z = floor(tilt_3z*100)/100; |
RGurav | 1:ba6c18cce219 | 88 | |
RGurav | 0:e551dfd13154 | 89 | if (axis==1) |
RGurav | 0:e551dfd13154 | 90 | { |
RGurav | 1:ba6c18cce219 | 91 | 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 | 92 | //pc.printf("%f", accl.axis355_sens); |
RGurav | 1:ba6c18cce219 | 93 | wait(1.0); } |
RGurav | 0:e551dfd13154 | 94 | |
RGurav | 0:e551dfd13154 | 95 | if (axis==2) |
RGurav | 0:e551dfd13154 | 96 | { |
RGurav | 1:ba6c18cce219 | 97 | 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); |
RGurav | 0:e551dfd13154 | 98 | wait(0.5); |
RGurav | 0:e551dfd13154 | 99 | } |
RGurav | 0:e551dfd13154 | 100 | |
RGurav | 0:e551dfd13154 | 101 | if (axis==3) |
RGurav | 0:e551dfd13154 | 102 | { |
RGurav | 1:ba6c18cce219 | 103 | 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 | 104 | wait(1.0); |
RGurav | 1:ba6c18cce219 | 105 | } |
RGurav | 1:ba6c18cce219 | 106 | |
RGurav | 1:ba6c18cce219 | 107 | if (axis == 4) |
RGurav | 1:ba6c18cce219 | 108 | { |
RGurav | 1:ba6c18cce219 | 109 | 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 | 110 | wait(1.0); |
RGurav | 0:e551dfd13154 | 111 | } |
RGurav | 0:e551dfd13154 | 112 | |
RGurav | 0:e551dfd13154 | 113 | } |
RGurav | 0:e551dfd13154 | 114 | |
RGurav | 0:e551dfd13154 | 115 | } |
RGurav | 1:ba6c18cce219 | 116 | //the addition of accl should be equal to 1 i.e. ( x+y+z = 1 ) |
RGurav | 0:e551dfd13154 | 117 | |
RGurav | 1:ba6c18cce219 | 118 | //single axis arcsin (0.2) |
RGurav | 1:ba6c18cce219 | 119 | float single_axis(float a) |
RGurav | 0:e551dfd13154 | 120 | { |
RGurav | 1:ba6c18cce219 | 121 | a = a*accl.axis355_sens; |
RGurav | 1:ba6c18cce219 | 122 | |
RGurav | 1:ba6c18cce219 | 123 | if (a>1.0f) |
RGurav | 1:ba6c18cce219 | 124 | { a = 1; |
RGurav | 1:ba6c18cce219 | 125 | } |
RGurav | 1:ba6c18cce219 | 126 | else if (a<-1) |
RGurav | 1:ba6c18cce219 | 127 | { a = -1; |
RGurav | 1:ba6c18cce219 | 128 | } |
RGurav | 1:ba6c18cce219 | 129 | else |
RGurav | 1:ba6c18cce219 | 130 | { a = a; |
RGurav | 1:ba6c18cce219 | 131 | } |
RGurav | 1:ba6c18cce219 | 132 | |
RGurav | 0:e551dfd13154 | 133 | double Y; |
RGurav | 1:ba6c18cce219 | 134 | |
RGurav | 1:ba6c18cce219 | 135 | Y = asin(a); |
RGurav | 1:ba6c18cce219 | 136 | Y = floor((57.2957f*Y)*100)/100; |
RGurav | 0:e551dfd13154 | 137 | return Y; |
RGurav | 0:e551dfd13154 | 138 | |
RGurav | 0:e551dfd13154 | 139 | } |
RGurav | 0:e551dfd13154 | 140 | |
RGurav | 1:ba6c18cce219 | 141 | //Dual Axis |
RGurav | 1:ba6c18cce219 | 142 | float dual_axis(float a, float b) |
RGurav | 0:e551dfd13154 | 143 | { |
RGurav | 0:e551dfd13154 | 144 | double Y; |
RGurav | 1:ba6c18cce219 | 145 | Y = 57.2957f * (atan(a/b)); |
RGurav | 0:e551dfd13154 | 146 | Y = floor(Y*100)/100; |
RGurav | 0:e551dfd13154 | 147 | return Y; |
RGurav | 0:e551dfd13154 | 148 | } |
RGurav | 0:e551dfd13154 | 149 | |
RGurav | 0:e551dfd13154 | 150 | //Triaxial |
RGurav | 1:ba6c18cce219 | 151 | float tri_axis(float a, float b, float c) |
RGurav | 0:e551dfd13154 | 152 | { |
RGurav | 0:e551dfd13154 | 153 | double Y; |
RGurav | 0:e551dfd13154 | 154 | double X; |
RGurav | 1:ba6c18cce219 | 155 | X = (a)/(sqrt((b*b)+(c*c))); |
RGurav | 0:e551dfd13154 | 156 | Y= atan(X); |
RGurav | 1:ba6c18cce219 | 157 | Y = floor(Y*57.2957f*100)/100; |
RGurav | 0:e551dfd13154 | 158 | return Y; |
RGurav | 0:e551dfd13154 | 159 | } |