Skip to content

Commit d4413d4

Browse files
committed
add Direction
1 parent 526ad7c commit d4413d4

1 file changed

Lines changed: 134 additions & 13 deletions

File tree

10.microbit/accelerometer.py

Lines changed: 134 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
from time import sleep_ms
2+
<<<<<<< HEAD
3+
4+
=======
5+
>>>>>>> 9c84ecc58c88a86411117dae28d3e69d002fe426
16

27
class Accelerometer:
38

49
def __init__(self, sensor):
510
self.sensor = sensor
11+
self.old, self.new, self.eliminate = [0]*3, [0]*3, 2
612

713
def get_x(self):
814
return self.sensor.acceleration[0] * 5
@@ -14,7 +20,121 @@ def get_z(self):
1420
return self.sensor.acceleration[2] * 5
1521

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

19139

20140
def unit_test():
@@ -28,18 +148,19 @@ def unit_test():
28148
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=200000)
29149
sensor = MPU9250(i2c)
30150
print('MPU9250 whoami: ' + hex(sensor.whoami))
31-
accelerometer = Accelerometer(sensor)
151+
gs = Direction(sensor)
152+
t = 0
32153
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("-")
154+
res = gs.get_direction()
155+
if res != None:
156+
print(res)
157+
t = 1+t
158+
print(t)
159+
<<<<<<< HEAD
160+
=======
161+
162+
>>>>>>> 9c84ecc58c88a86411117dae28d3e69d002fe426
163+
43164

44165
if __name__ == '__main__':
45-
unit_test()
166+
unit_test()

0 commit comments

Comments
 (0)