forked from theSpeare/RPi_ADXL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
barPrintTest.py
79 lines (64 loc) · 1.6 KB
/
barPrintTest.py
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
from i2clibraries import i2c_adxl345
from time import *
import os
while True:
try:
adxl345 = i2c_adxl345.i2c_adxl345(1)
break
except IOError:
print("error")
x_fix = 0
y_fix = 0
z_fix = 0
def printBar(axis, axisValue):
print(axis + ": ", end="")
for n in range (0, abs(int(round(axisValue*60,0)))):
print("=", end="")
print("\n")
def getCalibrationOffsets ():
# Collect data for a certain amount of time to get averages (for subtraction)
counterCalibrate = 0
counterCalibrateMax = 100
calibrateList = []
calibrateValues = []
calibrateLoop = True
x_average = 0
y_average = 0
z_average = 0
while counterCalibrate <= counterCalibrateMax:
counterCalibrate += 1
#getAxes
(accel_x, accel_y, accel_z) = adxl345.getAxes()
calibrateValues = [accel_x, accel_y, accel_z]
calibrateList.append(calibrateValues)
for dataValues in calibrateList:
x_average += dataValues[0]
y_average += dataValues[1]
z_average += dataValues[2]
x_fix = (x_average / len(calibrateList))*-1
y_fix = (y_average / len(calibrateList))*-1
z_fix = ((z_average / len(calibrateList))*-1)
return (x_fix, y_fix, z_fix)
def calibrateAxesValues(x, y, z):
global x_fix
global y_fix
global z_fix
x += x_fix
y += y_fix
z += z_fix
return (x,y,z)
(x_fix, y_fix, z_fix) = getCalibrationOffsets()
while True:
try:
(x,y,z) = adxl345.getAxes()
(x,y,z) = calibrateAxesValues(x,y,z)
print(x)
print(y)
print(z)
printBar("x", x)
printBar("y", y)
printBar("z", z)
sleep(0.02)
os.system("clear")
except IOError:
print("")