-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmpu.py
More file actions
148 lines (116 loc) · 3.7 KB
/
mpu.py
File metadata and controls
148 lines (116 loc) · 3.7 KB
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
# 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.
# MPU-6000
# This code is designed to work with the MPU-6000_I2CS I2C Mini Module available from ControlEverything.com.
# https://www.controleverything.com/content/Accelorometer?sku=MPU-6000_I2CS#tabs-0-product_tabset-2
import os
import smbus
import sys
import time
import uuid
from datetime import datetime
from datetime import timedelta
def turn_on_act_led():
handle = os.open('/sys/class/leds/led0/brightness', os.O_RDWR)
os.write(handle, '1')
os.close(handle)
def turn_off_act_led():
handle = os.open('/sys/class/leds/led0/brightness', os.O_RDWR)
os.write(handle, '0')
os.close(handle)
def millis_since_epoch():
return int(time.time() * 1000)
turn_on_act_led()
# set up file to write to
filename = 'data/mpu-' + str(millis_since_epoch()) + '.dat'
output = open(filename, 'w')
# script, filename = argv, test.txt
# Get I2C bus
bus = smbus.SMBus(1)
# MPU-6000 address, 0x68(104)
# Select gyroscope configuration register, 0x1B(27)
# 0x18(24) Full scale range = 2000 dps
bus.write_byte_data(0x68, 0x1B, 0x18)
# MPU-6000 address, 0x68(104)
# Select accelerometer configuration register, 0x1C(28)
# 0x18(24) Full scale range = +/-16g
bus.write_byte_data(0x68, 0x1C, 0x18)
# MPU-6000 address, 0x68(104)
# Select power management register1, 0x6B(107)
# 0x01(01) PLL with xGyro reference
bus.write_byte_data(0x68, 0x6B, 0x01)
#suspends exection x seconds
time.sleep(0.8)
# MPU-6000 address, 0x68(104)
# Read data back from 0x3B(59), 6 bytes
# Accelerometer X-Axis MSB, X-Axis LSB, Y-Axis MSB, Y-Axis LSB, Z-Axis MSB, Z-Axis LSB
def readData():
data = bus.read_i2c_block_data(0x68, 0x3B, 6)
# Convert the data
xAccl = data[0] * 256 + data[1]
if xAccl > 32767 :
xAccl -= 65536
yAccl = data[2] * 256 + data[3]
if yAccl > 32767 :
yAccl -= 65536
zAccl = data[4] * 256 + data[5]
if zAccl > 32767 :
zAccl -= 65536
# MPU-6000 address, 0x68(104)
# Read data back from 0x43(67), 6 bytes
# Gyrometer X-Axis MSB, X-Axis LSB, Y-Axis MSB, Y-Axis LSB, Z-Axis MSB, Z-Axis LSB
data = bus.read_i2c_block_data(0x68, 0x43, 6)
# Convert the data
xGyro = data[0] * 256 + data[1]
if xGyro > 32767 :
xGyro -= 65536
yGyro = data[2] * 256 + data[3]
if yGyro > 32767 :
yGyro -= 65536
zGyro = data[4] * 256 + data[5]
if zGyro > 32767 :
zGyro -= 65536
# Save string data
#x = "Acceleration in X-Axis : %d" %xAccl + "\n"
#y = "Acceleration in Y-Axis : %d" %yAccl + "\n"
#z = "Acceleration in Z-Axis : %d" %zAccl + "\n"
#xr = "X-Axis of Rotation : %d" %xGyro + "\n"
#yr = "Y-Axis of Rotation : %d" %yGyro + "\n"
#zr = "Z-Axis of Rotation : %d" %zGyro + "\n"
#writeFile(x, y, z, xr, yr, zr)
writeFile(xAccl, yAccl, zAccl, xGyro, yGyro, zGyro)
return
def getDiskSpace():
p = os.popen("df -h /")
i = 0
while 1:
i = i +1
line = p.readline()
if i==2:
return(line.split()[1:5])
# Disk information
DISK_stats = getDiskSpace()
DISK_total = DISK_stats[0]
DISK_free = DISK_stats[1]
DISK_perc = DISK_stats[3]
# write to file
def writeFile(xAccl, yAccl, zAccl, xGyro, yGyro, zGyro):
ms = millis_since_epoch()
output.write('%d %d %d %d %d %d %d\n' % (ms, xAccl, yAccl, zAccl, xGyro, yGyro, zGyro))
return
# no idea why this works, but it seems it must be turned off before it will turn on
turn_off_act_led()
# run for ~3 minutes
#end_time = millis_since_epoch() + 180000
i = 0
#while (millis_since_epoch() < end_time):
while (i < 80810):
i = i + 1
turn_on_act_led()
readData()
turn_off_act_led()
print "Done!"
print DISK_total
print DISK_free
print DISK_perc
output.close()