This repository has been archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
105 lines (77 loc) · 3.12 KB
/
main.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
from enum import Enum
from utils import *
class ChiaDuhState(Enum):
JING_SHONG = 0
CHIA_DUH = 1
class BuaDuhState(Enum):
BO_DAI_JI = 0
BUA_DUH = 1
TOU_KA = 2 # unused
if __name__ == "__main__":
try:
MPU_Init()
print (" Reading Data of Gyroscope and Accelerometer")
post("今仔日")
sleep(0.1)
post("哇想講")
sleep(0.1)
post("欲來給恁講")
sleep(0.5)
post("台灣話")
chiaDuhState = ChiaDuhState.JING_SHONG
prevChiaDuhState = chiaDuhState
buaDuhState = BuaDuhState.BO_DAI_JI
prevBuaDuhState = buaDuhState
while True:
# Read Accelerometer raw value
acc_x = read_raw_data(ACCEL_XOUT_H)
acc_y = read_raw_data(ACCEL_YOUT_H)
acc_z = read_raw_data(ACCEL_ZOUT_H)
# Read Gyroscope raw value
gyro_x = read_raw_data(GYRO_XOUT_H)
gyro_y = read_raw_data(GYRO_YOUT_H)
gyro_z = read_raw_data(GYRO_ZOUT_H)
# Full scale range +/- 250 degree/C as per sensitivity scale factor
Gx = gyro_x / 131.0 - gx_off
Gy = gyro_y / 131.0 - gy_off
Gz = gyro_z / 131.0 - gz_off
Ax = acc_x/16384.0 - ax_off
Ay = acc_y/16384.0 - ay_off
Az = acc_z/16384.0 - az_off
# Ax = acc_x / 4096.0 - ax_off
# Ay = acc_y / 4096.0 - ay_off
# Az = acc_z / 4096.0 - az_off
gravity = (Ax ** 2 + Ay ** 2 + Az ** 2) ** 0.5
print(f"{gravity:.2f} {Ax:.2f} {Ay:.2f} {Az:.2f}", end="\r")
# print(f"{Ax:.2f} {Ay:.2f} {Az:.2f} {gravity:.2f}", end="\n")
# print(f"{chiaDuhState.name}", end="\r")
if Ax > 0.9:
chiaDuhState = ChiaDuhState.JING_SHONG
elif 0.9 < gravity < 1.1 and Ay ** 2 + Az ** 2 > 0.9:
chiaDuhState = ChiaDuhState.CHIA_DUH
if 1.1 > gravity > 0.9:
buaDuhState = BuaDuhState.BO_DAI_JI
elif gravity < 0.6:
buaDuhState = BuaDuhState.BUA_DUH
if chiaDuhState != prevChiaDuhState:
if chiaDuhState == ChiaDuhState.JING_SHONG:
post("甘溫!")
if chiaDuhState == ChiaDuhState.CHIA_DUH:
post("哇哄恰倒阿")
prevChiaDuhState = chiaDuhState
if buaDuhState != prevBuaDuhState:
if buaDuhState == BuaDuhState.BO_DAI_JI:
post("我猶未係")
if buaDuhState == BuaDuhState.BUA_DUH:
post("哇摔落去了")
if buaDuhState == BuaDuhState.TOU_KA:
...
prevBuaDuhState = buaDuhState
# system("clear")
# print(f"Gx={Gx:+03.2f} deg/s \nGy={Gy:+03.2f} deg/s \nGz={Gz:+03.2f} deg/s")
# print(f"Ax={Ax:+03.2f} g \nAy={Ay:+03.2f} g \nAz={Az:+03.2f} g", end="\n")
# sleep(0.05)
# if gravity > 5:
# print(f"gravity: {gravity:.2f}")
except KeyboardInterrupt:
exit()