-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccel_data.py
executable file
·71 lines (56 loc) · 1.88 KB
/
accel_data.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
# Distributed with a free-will license.
# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
# MMA8452Q
# This code is designed to work with the MMA8452Q_I2CS I2C Mini Module available from ControlEverything.com.
# https://www.controleverything.com/content/Accelorometer?sku=MMA8452Q_I2CS#tabs-0-product_tabset-2
import os
import smbus
import time
fname = 'accel_data_out'
try:
os.unlink(fname)
except:
print "no previous data file to remove"
# Get I2C bus
bus = smbus.SMBus(1)
# MMA8452Q address, 0x1D(28)
# Select Control register, 0x2A(42)
# 0x00(00) StandBy mode
bus.write_byte_data(0x1D, 0x2A, 0x00)
# MMA8452Q address, 0x1D(28)
# Select Control register, 0x2A(42)
# 0x01(01) Active mode
bus.write_byte_data(0x1D, 0x2A, 0x01)
# MMA8452Q address, 0x1D(28)
# Select Configuration register, 0x0E(14)
# 0x00(00) Set range to +/- 2g
bus.write_byte_data(0x1D, 0x0E, 0x00)
time.sleep(0.5)
i = 0
fout = open(fname, 'w')
fout.write( "time, X, Y, Z\n" )
while i < 6000: # 1 min runtime
# MMA8452Q address, 0x1D(28)
# Read data back from 0x00(0), 7 bytes
# Status register, X-Axis MSB, X-Axis LSB, Y-Axis MSB, Y-Axis LSB, Z-Axis MSB, Z-Axis LSB
data = bus.read_i2c_block_data(0x1D, 0x00, 7)
# Convert the data
xAccl = (data[1] * 256 + data[2]) / 16
if xAccl > 2047 :
xAccl -= 4096
yAccl = (data[3] * 256 + data[4]) / 16
if yAccl > 2047 :
yAccl -= 4096
zAccl = (data[5] * 256 + data[6]) / 16
if zAccl > 2047 :
zAccl -= 4096
# Output data to screen
ts = int(round(time.time() * 1000))
# print ts
# print "Acceleration in X-Axis : %d" %xAccl
# print "Acceleration in Y-Axis : %d" %yAccl
# print "Acceleration in Z-Axis : %d" %zAccl
fout.write( str(ts) + ", " + str(xAccl) + ", " + str(yAccl) + ", " + str(zAccl) + "\n" )
time.sleep(0.0086)
i = i + 1
fout.close()