楼主: 驴友花雕
|
[MP动手做] MicroPython动手做(20)——掌控板之三轴加速度 |
15、倾斜和摇晃的一双眼睛 #MicroPython动手做(20)——掌控板之三轴加速度 #倾斜和摇晃的一双眼睛(应用字典函数)
字典 字典是一种可变容器模型,且可存储任意类型对象,格式如 d = {key1 : value1, key2 : value2},键必须是唯一的,但值则不必。 |
16、水平仪和测量角度 #MicroPython动手做(20)——掌控板之三轴加速度 #水平仪和测量角度 [mw_shl_code=python,false]#MicroPython动手做(20)——掌控板之三轴加速度 #水平仪和测量角度 from mpython import * import math import framebuf import font.dvsm_12 import time def get_tilt_angle(_axis): _Ax = accelerometer.get_x() _Ay = accelerometer.get_y() _Az = accelerometer.get_z() if 'X' == _axis: _T = math.sqrt(_Ay ** 2 + _Az ** 2) if _Az < 0: return math.degrees(math.atan2(_Ax , _T)) else: return 180 - math.degrees(math.atan2(_Ax , _T)) elif 'Y' == _axis: _T = math.sqrt(_Ax ** 2 + _Az ** 2) if _Az < 0: return math.degrees(math.atan2(_Ay , _T)) else: return 180 - math.degrees(math.atan2(_Ay , _T)) elif 'Z' == _axis: _T = math.sqrt(_Ax ** 2 + _Ay ** 2) if (_Ax + _Ay) < 0: return 180 - math.degrees(math.atan2(_T , _Az)) else: return math.degrees(math.atan2(_T , _Az)) - 180 return 0 def display_font(_font, _str, _x, _y, _wrap, _z=0): _start = _x for _c in _str: _d = _font.get_ch(_c) if _wrap and _x > 128 - _d[2]: _x = _start; _y += _d[1] if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d[2], _d[1], 0) oled.blit(framebuf.FrameBuffer(bytearray(_d[0]), _d[2], _d[1], framebuf.MONO_HLSB), (_x+int(_d[2]/_z)) if _c=='1' and _z>0 else _x, _y) _x += _d[2] while True: Tx = get_tilt_angle('X') oled.fill(0) display_font(font.dvsm_12, (str(" Angle :") + str(Tx)), 0, 0, False) oled.circle(64, 60, 46, 1) oled.fill_circle(64, 60, 5, 1) Dx = int((64 + math.cos(math.radians(Tx)) * 46)) Dy = int((60 - math.fabs(math.sin(math.radians(Tx)) * 46))) oled.hline(0, 60, 128, 1) oled.line(64, 60, Dx, Dy, 1) oled.fill_rect(0, 61, 128, 3, 0) oled.vline(64, 61, 2, 1) Lx = int(numberMap(accelerometer.get_y(),(-1),1,128,0)) oled.vline(Lx, 61, 3, 1) if Lx == 64: rgb.fill((int(0), int(51), int(0))) rgb.write() time.sleep_ms(1) oled.fill_circle(13, 20, 3, 1) oled.hline(7, 20, 13, 1) else: oled.fill_rect(7, 16, 13, 6, 0) rgb.fill( (0, 0, 0) ) rgb.write() time.sleep_ms(1) oled.show()[/mw_shl_code] |
17、掌控闪灯大量程计步器(十万步) #MicroPython动手做(20)——掌控板之三轴加速度 #掌控闪灯大量程计步器(十万步)
注解 使用摇晃模块,优点是算法简单,不足之处是触发计步的阙值是固定的,不能调整 |
© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed