6391浏览
楼主: 驴友花雕

[MP动手做] MicroPython动手做(20)——掌控板之三轴加速度

[复制链接]

驴友花雕  中级技神
 楼主|

发表于 2020-5-8 11:40:35

mPython X 图形编程

MicroPython动手做(20)——掌控板之三轴加速度图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-8 12:47:47

MicroPython动手做(20)——掌控板之三轴加速度图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-8 19:10:49

15、倾斜和摇晃的一双眼睛

#MicroPython动手做(20)——掌控板之三轴加速度
#倾斜和摇晃的一双眼睛(应用字典函数)

  1. #MicroPython动手做(20)——掌控板之三轴加速度
  2. #倾斜和摇晃的一双眼睛(应用字典函数)
  3. from mpython import *
  4. from machine import Timer
  5. import time
  6. _is_shaked = _is_thrown = False
  7. _last_x = _last_y = _last_z = _count_shaked = _count_thrown = 0
  8. def on_shaked():pass
  9. def on_thrown():pass
  10. tim11 = Timer(11)
  11. def timer11_tick(_):
  12.     global _is_shaked, _is_thrown, _last_x, _last_y, _last_z, _count_shaked, _count_thrown
  13.     if _is_shaked:
  14.         _count_shaked += 1
  15.         if _count_shaked == 5: _count_shaked = 0
  16.     if _is_thrown:
  17.         _count_thrown += 1
  18.         if _count_thrown == 10: _count_thrown = 0
  19.         if _count_thrown > 0: return
  20.     x=accelerometer.get_x(); y=accelerometer.get_y(); z=accelerometer.get_z()
  21.     _is_thrown = (x * x + y * y + z * z < 0.25)
  22.     if _is_thrown: on_thrown();return
  23.     if _last_x == 0 and _last_y == 0 and _last_z == 0:
  24.         _last_x = x; _last_y = y; _last_z = z; return
  25.     diff_x = x - _last_x; diff_y = y - _last_y; diff_z = z - _last_z
  26.     _last_x = x; _last_y = y; _last_z = z
  27.     if _count_shaked > 0: return
  28.     _is_shaked = (diff_x * diff_x + diff_y * diff_y + diff_z * diff_z > 1)
  29.     if _is_shaked: on_shaked()
  30. tim11.init(period=100, mode=Timer.PERIODIC, callback=timer11_tick)
  31. _dir = ''
  32. def on_tilt_forward():pass
  33. def on_tilt_back():pass
  34. def on_tilt_right():pass
  35. def on_tilt_left():pass
  36. def on_tilt_none():pass
  37. tim14 = Timer(14)
  38. def timer14_tick(_):
  39.     global _dir
  40.     if accelerometer.get_x() < -0.3:
  41.         if 'F' != _dir:_dir = 'F';on_tilt_forward()
  42.     elif accelerometer.get_x() > 0.3:
  43.         if 'B' != _dir:_dir = 'B';on_tilt_back()
  44.     elif accelerometer.get_y() < -0.3:
  45.         if 'R' != _dir:_dir = 'R';on_tilt_right()
  46.     elif accelerometer.get_y() > 0.3:
  47.         if 'L' != _dir:_dir = 'L';on_tilt_left()
  48.     else:
  49.         if '' != _dir:_dir = '';on_tilt_none()
  50. tim14.init(period=200, mode=Timer.PERIODIC, callback=timer14_tick)
  51. def on_tilt_forward():
  52.     global face, dt_faces
  53.     face = dt_faces.get("Up")
  54. def on_tilt_back():
  55.     global face, dt_faces
  56.     face = dt_faces.get("Down")
  57. def on_tilt_left():
  58.     global face, dt_faces
  59.     face = dt_faces.get("Left")
  60. def on_tilt_right():
  61.     global face, dt_faces
  62.     face = dt_faces.get("Right")
  63. def on_tilt_none():
  64.     global face, dt_faces
  65.     face = dt_faces.get("Neutral")
  66. image_picture = Image()
  67. dt_faces = {"Neutral":image_picture.load('face/Eyes/Neutral.pbm', 0), "Up":image_picture.load('face/Eyes/Up.pbm', 0), "Down":image_picture.load('face/Eyes/Down.pbm', 0), "Left":image_picture.load('face/Eyes/Middle left.pbm', 0), "Right":image_picture.load('face/Eyes/Middle right.pbm', 0), "Dizzy":image_picture.load('face/Eyes/Dizzy.pbm', 0)}
  68. face = dt_faces.get("Neutral")
  69. while True:
  70.     oled.fill(0)
  71.     if _is_shaked:
  72.         oled.blit(dt_faces.get("Dizzy"), 20, 0)
  73.         oled.show()
  74.         time.sleep_ms(2000)
  75.     else:
  76.         oled.blit(face, 20, 0)
  77.         oled.show()
复制代码

字典
字典是一种可变容器模型,且可存储任意类型对象,格式如 d = {key1 : value1, key2 : value2},键必须是唯一的,但值则不必。

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-8 19:15:34

mPython X 图形编程

MicroPython动手做(20)——掌控板之三轴加速度图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-8 19:23:21

MicroPython动手做(20)——掌控板之三轴加速度图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-8 19:30:40

MicroPython动手做(20)——掌控板之三轴加速度图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-8 19:49:52

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]

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-8 20:06:33

mPython X 图形编程

MicroPython动手做(20)——掌控板之三轴加速度图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-8 20:21:13

MicroPython动手做(20)——掌控板之三轴加速度图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-9 09:48:49

17、掌控闪灯大量程计步器(十万步)

#MicroPython动手做(20)——掌控板之三轴加速度
#掌控闪灯大量程计步器(十万步)

  1. #MicroPython动手做(20)——掌控板之三轴加速度
  2. #掌控闪灯大量程计步器(十万步)
  3. from mpython import *
  4. from machine import Timer
  5. _is_shaked = _is_thrown = False
  6. _last_x = _last_y = _last_z = _count_shaked = _count_thrown = 0
  7. def on_shaked():pass
  8. def on_thrown():pass
  9. tim11 = Timer(11)
  10. def timer11_tick(_):
  11.     global _is_shaked, _is_thrown, _last_x, _last_y, _last_z, _count_shaked, _count_thrown
  12.     if _is_shaked:
  13.         _count_shaked += 1
  14.         if _count_shaked == 5: _count_shaked = 0
  15.     if _is_thrown:
  16.         _count_thrown += 1
  17.         if _count_thrown == 10: _count_thrown = 0
  18.         if _count_thrown > 0: return
  19.     x=accelerometer.get_x(); y=accelerometer.get_y(); z=accelerometer.get_z()
  20.     _is_thrown = (x * x + y * y + z * z < 0.25)
  21.     if _is_thrown: on_thrown();return
  22.     if _last_x == 0 and _last_y == 0 and _last_z == 0:
  23.         _last_x = x; _last_y = y; _last_z = z; return
  24.     diff_x = x - _last_x; diff_y = y - _last_y; diff_z = z - _last_z
  25.     _last_x = x; _last_y = y; _last_z = z
  26.     if _count_shaked > 0: return
  27.     _is_shaked = (diff_x * diff_x + diff_y * diff_y + diff_z * diff_z > 1)
  28.     if _is_shaked: on_shaked()
  29. tim11.init(period=100, mode=Timer.PERIODIC, callback=timer11_tick)
  30. import time
  31. import framebuf
  32. import font.digiface_30
  33. def display_font(_font, _str, _x, _y, _wrap, _z=0):
  34.     _start = _x
  35.     for _c in _str:
  36.         _d = _font.get_ch(_c)
  37.         if _wrap and _x > 128 - _d[2]: _x = _start; _y += _d[1]
  38.         if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d[2], _d[1], 0)
  39.         oled.blit(framebuf.FrameBuffer(bytearray(_d[0]), _d[2], _d[1],
  40.         framebuf.MONO_HLSB), (_x+int(_d[2]/_z)) if _c=='1' and _z>0 else _x, _y)
  41.         _x += _d[2]
  42. bbb = 0
  43. while True:
  44.     oled.fill(0)
  45.     oled.DispChar('掌控计步器', 35, 2, 1)
  46.     oled.DispChar('步', 115, 40, 1)
  47.     if _is_shaked:
  48.         rgb.fill((int(0), int(102), int(0)))
  49.         rgb.write()
  50.         time.sleep_ms(1)
  51.         time.sleep_ms(60)
  52.         rgb.fill((int(0), int(0), int(0)))
  53.         rgb.write()
  54.         time.sleep_ms(1)
  55.         bbb = bbb + 1
  56.     else:
  57.         rgb.fill((int(153), int(0), int(0)))
  58.         rgb.write()
  59.         time.sleep_ms(1)
  60.     if bbb >= 0 and bbb <= 9:
  61.         display_font(font.digiface_30, (str(bbb)), 55, 24, False, 2)
  62.     elif bbb >= 9 and bbb <= 99:
  63.         display_font(font.digiface_30, (str(bbb)), 45, 24, False, 2)
  64.     elif bbb >= 99 and bbb <= 999:
  65.         display_font(font.digiface_30, (str(bbb)), 35, 24, False, 2)
  66.     elif bbb >= 999 and bbb <= 9999:
  67.         display_font(font.digiface_30, (str(bbb)), 20, 24, False, 2)
  68.     elif bbb >= 9999 and bbb <= 99999:
  69.         display_font(font.digiface_30, (str(bbb)), 10, 24, False, 2)
  70.     oled.show()
复制代码

注解
使用摇晃模块,优点是算法简单,不足之处是触发计步的阙值是固定的,不能调整

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-9 10:07:48

mPython  图形编程

MicroPython动手做(20)——掌控板之三轴加速度图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-9 11:34:07

MicroPython动手做(20)——掌控板之三轴加速度图1
回复

使用道具 举报

123
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail