-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadDataFromADXL345 (update)
416 lines (356 loc) · 9.21 KB
/
ReadDataFromADXL345 (update)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include <Wire.h>
#include <math.h>
#define First_DEVICE (0x53) //first ADXL345 device address
#define Second_DEVICE (0x1D) //second ADXL345 device address
#define TO_READ (6) //num of bytes we are going to read each time (two bytes for each axis)
byte buff[TO_READ] ; //6 bytes buffer for saving data read from the device
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
//Turning on the ADXL345
writeTo(First_DEVICE, 0x2D, 0);
writeTo(First_DEVICE, 0x2D, 16);
writeTo(First_DEVICE, 0x2D, 8);
writeTo(Second_DEVICE, 0x2D, 0);
writeTo(Second_DEVICE, 0x2D, 16);
writeTo(Second_DEVICE, 0x2D, 8);
}
//**************global variable*******************//
int x, y, z;
double accl_x,accl_y,accl_z;
double mag;
double a, b, c;
int x2, y2, z2;
double accl_x2,accl_y2,accl_z2;
double mag2;
double a2, b2, c2;
int count = 0;
int old_x, old_y, old_z;
int old_x2, old_y2, old_z2;
//**********************************************//
void loop()
{
int regAddress = 0x32; //register address for first devie and second device
readFrom(First_DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
//each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
//thus we are converting both bytes in to one int
x = (((int)buff[1]) << 8) | buff[0];
y = (((int)buff[3])<< 8) | buff[2];
z = (((int)buff[5]) << 8) | buff[4];
readFrom(Second_DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
//each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
//thus we are converting both bytes in to one int
x2 = (((int)buff[1]) << 8) | buff[0];
y2 = (((int)buff[3])<< 8) | buff[2];
z2 = (((int)buff[5]) << 8) | buff[4];
//calculate all information from first device
//offset raw datas
x = x-20;
y = y-6;
z = z+4;
//if raw datas are negative
if(x>10000)
{
x = x-65536;
}
if(y>10000)
{
y = y-65536;
}
if(z>10000)
{
z = z-65536;
}
//convert raw datas to G values
accl_x = 0.00391*x;
accl_y = 0.00391*y;
accl_z = 0.00391*z;
//find magnitde of accelration
mag = sqrt(pow(accl_x,2) + pow(accl_y,2) + pow(accl_z,2));
//find angels
a = atan(accl_x / sqrt(sq(accl_y) + sq(accl_z))) * 180 / PI;
b = atan(accl_y / sqrt(sq(accl_x) + sq(accl_z))) * 180 / PI;
c = atan(sqrt(sq(accl_x) + sq(accl_y)) / accl_z) * 180 / PI;
if(a >= 0 && b >= 0 && c>=0)
{
a = a;
b= b;
c = c;
}
else if(a >= 0 && b >= 0 && c<0)
{
a = 180.00-a;
b= 180.00-b;
c = c+180.00;
}
else if(a >= 0 && b < 0 && c>=0)
{
a = a;
b= b+360.00;
c = c;
}
else if(a >= 0 && b < 0 && c<0)
{
a = 180.00-a;
b= 180.00-b;
c = c+180.00;
}
else if(a < 0 && b >= 0 && c>=0)
{
a = a+360;
b= b;
c = c;
}
else if(a < 0 && b >= 0 && c<0)
{
a = 180.00-a;
b= 180.00-b;
c = 180.00-c;
}
else if(a < 0 && b < 0 && c>=0)
{
a = a+360.00;
b= b+360.00;
c = 360.00-c;
}
else
{
a = 180.00-a;
b= 180.00-b;
c = c+180.00;
}
//calculate all information from second device
//offset raw datas
x2 = x2-20;
y2 = y2;
z2 = z2+8;
//if raw datas are negative
if(x2>10000)
{
x2 = x2-65536;
}
if(y2>10000)
{
y2 = y2-65536;
}
if(z2>10000)
{
z2 = z2-65536;
}
//convert raw datas to G values
accl_x2 = 0.00391*x2;
accl_y2 = 0.00391*y2;
accl_z2 = 0.00391*z2;
//find magnitde of accelration
mag2 = sqrt(pow(accl_x2,2) + pow(accl_y2,2) + pow(accl_z2,2));
//find angels
a2 = atan(x2 / sqrt(sq(y2) + sq(z2))) * 180 / PI;
b2 = atan(y2 / sqrt(sq(x2) + sq(z2))) * 180 / PI;
c2 = atan(sqrt(sq(x2) + sq(y2)) / z2) * 180 / PI;
if(a2 >= 0 && b2 >= 0 && c2 >=0)
{
a2 = a2;
b2 = b2;
c2 = c2;
}
else if(a2 >= 0 && b2 >= 0 && c2 <0)
{
a2 = 180.00-a2;
b2 = 180.00-b2;
c2 = c2+180.00;
}
else if(a2 >= 0 && b2 < 0 && c2 >=0)
{
a2 = a2;
b2 = b2+360.00;
c2 = c2;
}
else if(a2 >= 0 && b2 < 0 && c2 <0)
{
a2 = 180.00-a2;
b2 = 180.00-b2;
c2 = c2+180.00;
}
else if(a2 < 0 && b2 >= 0 && c2 >=0)
{
a2 = a2+360;
b2 = b2;
c2 = c2;
}
else if(a2 < 0 && b2 >= 0 && c2<0)
{
a2 = 180.00-a2;
b2 = 180.00-b2;
c2 = 180.00-c2;
}
else if(a2 < 0 && b2 < 0 && c2 >=0)
{
a2 = a2+360.00;
b2 = b2+360.00;
c2 = 360.00-c2;
}
else
{
a2 = 180.00-a2;
b2 = 180.00-b2;
c2 = c2+180.00;
}
display_second_device();
}
void writeTo(int device, byte address, byte val) {
Wire.beginTransmission(device); //start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); //end transmission
}
void readFrom(int device, byte address, int num, byte buff[]) {
Wire.beginTransmission(device); //start transmission to device
Wire.write(address); //sends address to read from
Wire.endTransmission(); //end transmission
Wire.beginTransmission(device); //start transmission to device (initiate again)
Wire.requestFrom(device, num); // request 6 bytes from device
int i = 0;
while(Wire.available()) //device may send less than requested (abnormal)
{
buff[i] = Wire.read(); // receive a byte
i++;
}
Wire.endTransmission(); //end transmission
}
void FindQuandrant(int x, int y, int z)
{
if(x >= 0 && y >= 0 && z >=0) //first quardrant with positive z direction
{
Serial.println("first quardrant with positive z direction");
}
if(x < 0 && y >= 0 && z >= 0) //second quardrant with positive z direction
{
Serial.println("second quardrant with positive z direction");
}
if(x < 0 && y < 0 && z >= 0) //thrid quardrant with positive z direction
{
Serial.println("thrid quardrant with positive z direction");
}
if(x >= 0 && y < 0 && z >=0) //fourth quardrant with positive z direction
{
Serial.println("fourth quardrant with positive z direction");
}
if(x >= 0 && y >= 0 && z < 0) //first quardrant with negative z direction
{
Serial.println("first quardrant with negative z direction");
}
if(x < 0 && y >= 0 && z < 0) //second quardrant with negative z direction
{
Serial.println("second quardrant with negative z direction");
}
if(x < 0 && y < 0 && z < 0) //thrid quardrant with negative z direction
{
Serial.println("thrid quardrant with negative z direction");
}
if(x >= 0 && y < 0 && z < 0) //fourth quardrant with negative z direction
{
Serial.println("fourth quardrant with negative z direction");
}
}
void display_first_device()
{
//we send the x y z values as a string to the serial port
Serial.print("Raw:\t");
Serial.print(x);
Serial.print(" ");
Serial.print(y);
Serial.print(" ");
Serial.print(z);
// Tell us about the this data, but scale it into useful units (G).
Serial.print("\tScaled:\t");
Serial.print(accl_x);
Serial.print("G ");
Serial.print(accl_y);
Serial.print("G ");
Serial.print(accl_z);
Serial.print("G");
//Display Magnitude
Serial.print("\tMagnitude:\t");
Serial.print(mag);
Serial.print("\ta:\t");
Serial.print(a);
Serial.print("\tb:\t");
Serial.print(b);
Serial.print("\ty:\t");
Serial.println(c);
}
void display_second_device()
{
//we send the x y z values as a string to the serial port
Serial.print("Raw:\t");
Serial.print(x2);
Serial.print(" ");
Serial.print(y2);
Serial.print(" ");
Serial.print(z2);
// Tell us about the this data, but scale it into useful units (G).
Serial.print("\tScaled:\t");
Serial.print(accl_x2);
Serial.print("G ");
Serial.print(accl_y2);
Serial.print("G ");
Serial.print(accl_z2);
Serial.print("G");
//Display Magnitude
Serial.print("\tMagnitude:\t");
Serial.print(mag2);
Serial.print("\ta:\t");
Serial.print(a2);
Serial.print("\tb:\t");
Serial.print(b2);
Serial.print("\ty:\t");
Serial.println(c2);
}
void check_moving(double magnitude)
{
double num = magnitude;
if(num >= 0.90 && num <= 1.10)
{
Serial.println("No movement_______________No movement");
}
else
{
Serial.println("Hand Move");
}
}
void hand_position()
{
if(a2 >= 350 || a2 < 10)
{
Serial.println("palm down");
}
else if(a2 >= 10 && a2 <80)
{
Serial.println("plam down right");
}
else if(a2 >= 80 && a2 <100)
{
Serial.println("plam right");
}
else if(a2 >= 100 && a2 < 170)
{
Serial.println("palm up right");
}
else if(a2 >= 170 && a2 < 190)
{
Serial.println("palm up");
}
else if(a2 >= 190 && a2 < 260)
{
Serial.println("palm up left");
}
else if(a2 >= 260 && a2 < 280)
{
Serial.println("palm left");
}
else
{
Serial.println("palm down left");
}
}