from pyb import Accel, Servo
import time
acc = Accel()
s_l = Servo(1)
s_r = Servo(2)
xs = [0] * 40
ys = [0] * 40
while True:
xs.pop(0)
ys.pop(0)
xs.append(-acc.z()*5)
ys.append(acc.x()*6+(acc.y()-20)*12)
x = y = 0
for i in xs:
x += i
for i in ys:
y += i
x /= len(xs)
y /= len(ys)
s_l.angle(max(-120, min(120, x - y)))
s_r.angle(max(-120, min(120, x + y)))
time.sleep_ms(10)
网友评论