-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPU6050-trigger.py
executable file
·105 lines (76 loc) · 2.84 KB
/
MPU6050-trigger.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
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
#!/usr/bin/env python3
# -*- coding: utf8 -*-
from mpu6050 import mpu6050
import time
import os
import logging
import logging.config
import subprocess
import json
from enum import Enum, unique
logging.config.fileConfig(os.path.dirname(__file__) + '/logging.ini')
logging.info("Start")
@unique
class AccelCalibration(Enum):
X = -0.375
Y = -0.25
Z = 9.3
THRESHOLD_X_FORWARD = AccelCalibration.X.value - 3.5
THRESHOLD_X_BACK = AccelCalibration.X.value + 3.5
TOLERANCE_Y = 0.45
MIN_Y = AccelCalibration.Y.value - TOLERANCE_Y
MAX_Y = AccelCalibration.Y.value + TOLERANCE_Y
MIN_Z = 0
MAX_Z = AccelCalibration.Z.value - 1.0
def volumio_status():
process = subprocess.Popen(["/usr/local/bin/volumio", "status"], stdout=subprocess.PIPE)
return json.loads(str(process.stdout.read(), "utf-8"))
def next():
logging.info("next")
volumio = volumio_status()
if volumio['status'] != "play":
return
subprocess.call(['volumio', 'next'], shell=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
def prev():
logging.info("prev")
volumio = volumio_status()
if volumio['status'] != "play":
return
subprocess.call(['volumio', 'previous'], shell=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
subprocess.call(['volumio', 'previous'], shell=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
def in_range(value, min, max):
return min < value < max
def log_raw_data(accel_data, gyro_data = None, temp = None):
logging.debug("Accelerometer data")
logging.debug("x: " + str(accel_data["x"]))
logging.debug("y: " + str(accel_data["y"]) + "[" + str(MIN_Y) + ", " + str(MAX_Y) + "]")
logging.debug("z: " + str(accel_data["z"]) + "[" + str(MIN_Z) + ", " + str(MAX_Z) + "]")
if gyro_data is not None:
logging.debug("Gyroscope data")
logging.debug("gyro x: " + str(gyro_data["x"]))
logging.debug("gyro y: " + str(gyro_data["y"]))
logging.debug("gyro z: " + str(gyro_data["z"]))
if temp is not None:
logging.debug("Temp: " + str(temp) + " C")
sensor = mpu6050(0x68)
accel_data = sensor.get_accel_data()
lastX = accel_data['x']
lastY = accel_data['y']
lastZ = accel_data['z']
while True:
accel_data = sensor.get_accel_data()
# gyro_data = sensor.get_gyro_data()
# temp = sensor.get_temp()
currentX = accel_data['x']
currentY = accel_data['y']
currentZ = accel_data['z']
log_raw_data(accel_data)
# log_raw_data(accel_data, gyro_data, temp)
if in_range(THRESHOLD_X_FORWARD, currentX, lastX) and in_range(currentY, MIN_Y, MAX_Y) and in_range(currentZ, MIN_Z, MAX_Z):
prev()
elif in_range(THRESHOLD_X_BACK, lastX, currentX) and in_range(currentY, MIN_Y, MAX_Y) and in_range(currentZ, MIN_Z, MAX_Z):
next()
lastX = currentX
lastY = currentY
lastZ = currentZ
time.sleep(0.1)