Skip to content

Commit 9c84ecc

Browse files
committed
add Direction
1 parent 526ad7c commit 9c84ecc

1 file changed

Lines changed: 124 additions & 13 deletions

File tree

10.microbit/accelerometer.py

Lines changed: 124 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from time import sleep_ms
12

23
class Accelerometer:
34

45
def __init__(self, sensor):
56
self.sensor = sensor
7+
self.old, self.new, self.eliminate = [0]*3, [0]*3, 2
68

79
def get_x(self):
810
return self.sensor.acceleration[0] * 5
@@ -14,7 +16,118 @@ def get_z(self):
1416
return self.sensor.acceleration[2] * 5
1517

1618
def get_values(self):
17-
return self.sensor.acceleration
19+
self.old = self.new
20+
self.new = self.sensor.acceleration
21+
return self.new
22+
23+
def get_state(self):
24+
tmp = [0]*3
25+
for i in range(3):
26+
tmp[i] = self.new[i] - self.old[i]
27+
tmp[i] = 0 if abs(tmp[i]) < self.eliminate else tmp[i]
28+
return tmp
29+
30+
31+
class Direction(Accelerometer):
32+
idle, ing, end, = 0, 1, 2
33+
34+
def __init__(self, sensor):
35+
Accelerometer.__init__(self, sensor)
36+
self.r_state, self.l_state, self.f_state, self.b_state, = Direction.idle, Direction.idle, Direction.idle, Direction.idle
37+
38+
def get_direction(self, delay=30):
39+
sleep_ms(delay)
40+
super().get_values()
41+
tem = super().get_state()
42+
x_state, y_state, z_state = tem[0], tem[1], tem[2]
43+
result = []
44+
if self.r_state == Direction.idle and x_state > 0 and self.new[0] > 2.5:
45+
self.r_count = 0
46+
self.r_state = Direction.ing
47+
48+
if self.r_state == Direction.ing:
49+
# print('Direction.ing')
50+
self.r_count += 1
51+
if self.r_count > 10:
52+
self.r_state = Direction.idle
53+
elif x_state < 0 and self.old[0] > 2.5:
54+
self.r_state = Direction.end
55+
56+
if self.r_state == Direction.end:
57+
# print('Direction.end')
58+
self.r_count += 1
59+
if self.r_count > 20:
60+
self.r_state = Direction.idle
61+
elif x_state > 0 and self.old[0] < -2.5:
62+
result.append('right')
63+
self.r_state = Direction.idle
64+
65+
if self.l_state == Direction.idle and x_state < 0 and self.new[0] < -2.5:
66+
self.l_count = 0
67+
self.l_state = Direction.ing
68+
69+
if self.l_state == Direction.ing:
70+
# print('Direction.ing')
71+
self.l_count += 1
72+
if self.l_count > 10:
73+
self.l_state = Direction.idle
74+
elif x_state > 0 and self.old[0] < -2.5:
75+
self.l_state = Direction.end
76+
77+
if self.l_state == Direction.end:
78+
# print('Direction.end')
79+
self.l_count += 1
80+
if self.l_count > 20:
81+
self.l_state = Direction.idle
82+
elif x_state < 0 and self.old[0] > 2.5:
83+
result.append('left')
84+
self.l_state = Direction.idle
85+
86+
if self.f_state == Direction.idle and y_state > 0 and self.new[1] > 2.5:
87+
self.f_count = 0
88+
self.f_state = Direction.ing
89+
90+
if self.f_state == Direction.ing:
91+
# print('Direction.ing')
92+
self.f_count += 1
93+
if self.f_count > 10:
94+
self.f_state = Direction.idle
95+
elif y_state < 0 and self.old[1] > 2.5:
96+
self.f_state = Direction.end
97+
98+
if self.f_state == Direction.end:
99+
# print('Direction.end')
100+
self.f_count += 1
101+
if self.f_count > 20:
102+
self.f_state = Direction.idle
103+
elif y_state > 0 and self.old[1] < -2.5:
104+
result.append('forward')
105+
self.f_state = Direction.idle
106+
107+
if self.b_state == Direction.idle and y_state < 0 and self.new[1] < -2.5:
108+
self.b_count = 0
109+
self.b_state = Direction.ing
110+
111+
if self.b_state == Direction.ing:
112+
# print('Direction.ing')
113+
self.b_count += 1
114+
if self.b_count > 10:
115+
self.b_state = Direction.idle
116+
elif y_state > 0 and self.old[1] < -2.5:
117+
self.b_state = Direction.end
118+
119+
if self.b_state == Direction.end:
120+
# print('Direction.end')
121+
self.b_count += 1
122+
if self.b_count > 20:
123+
self.b_state = Direction.idle
124+
elif y_state < 0 and self.old[1] > 2.5:
125+
result.append('backwards')
126+
self.b_state = Direction.idle
127+
128+
129+
130+
return None if len(result) != 1 else result[0]
18131

19132

20133
def unit_test():
@@ -28,18 +141,16 @@ def unit_test():
28141
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=200000)
29142
sensor = MPU9250(i2c)
30143
print('MPU9250 whoami: ' + hex(sensor.whoami))
31-
accelerometer = Accelerometer(sensor)
144+
gs = Direction(sensor)
145+
t = 0
32146
while True:
33-
sleep(0.1)
34-
reading = accelerometer.get_x()
35-
print(reading)
36-
print(accelerometer.get_values())
37-
if reading > 20:
38-
display.show("R")
39-
elif reading < -20:
40-
display.show("L")
41-
else:
42-
display.show("-")
147+
res = gs.get_direction()
148+
if res != None:
149+
print(res)
150+
t = 1+t
151+
print(t)
152+
153+
43154

44155
if __name__ == '__main__':
45-
unit_test()
156+
unit_test()

0 commit comments

Comments
 (0)