6405浏览
楼主: 驴友花雕

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

[复制链接]

驴友花雕  中级技神
 楼主|

发表于 2020-5-2 16:58:29

7、声光水平测量仪

#MicroPython动手做(20)——掌控板之三轴加速度
#声光水平测量仪

  1. #MicroPython动手做(20)——掌控板之三轴加速度
  2. #声光水平测量仪
  3. from mpython import *
  4. import time
  5. import music
  6. oled.fill(0)
  7. oled.DispChar('声光水平测量仪', 20, 22, 1)
  8. oled.show()
  9. time.sleep(3)
  10. while True:
  11.     xxx = int(((127 - 0) / (1 - (-1))) * (accelerometer.get_y() - (-1)) + 0)
  12.     yyy = int(((63 - 0) / (1 - (-1))) * (accelerometer.get_x() - (-1)) + 0)
  13.     oled.fill(0)
  14.     oled.circle(64, 32, 6, 1)
  15.     oled.circle(64, 32, 31, 1)
  16.     oled.hline(44, 32, 40, 1)
  17.     oled.vline(64, 12, 40, 1)
  18.     oled.fill_circle(xxx, yyy, 4, 1)
  19.     oled.show()
  20.     if xxx == 64 and yyy == 32:
  21.         rgb.fill((int(0), int(102), int(0)))
  22.         rgb.write()
  23.         time.sleep_ms(1)
  24.         music.pitch(294, 500)
  25.         music.pitch(494, 500)
  26.     else:
  27.         rgb.fill( (0, 0, 0) )
  28.         rgb.write()
  29.         time.sleep_ms(1)
复制代码

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-2 17:07:43

mPython 图形编程

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-2 18:14:36

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-2 18:18:32

8、简易计步器

在走路时通过串口查看加速度传感器的x、y、z和强度的值,会发现变化最明显的是强度值,因为强度值是综合x、y、z三个方向的值得到的矢量和,任一方向的值发生变化,强度值都会变化。所以我们选择强度值变化作为计步标准。Arduino程序如下:

  1. <blockquote>//MicroPython动手做(20)——掌控板之三轴加速度
  2. //简易计步器
  3. #include <MPython.h>
  4. // 动态变量
  5. volatile float mind_n_BuShu;
  6. // 主程序开始
  7. void setup() {
  8.         mPython.begin();
  9.         mind_n_BuShu = 0;
  10.         display.setCursor(0, 25);
  11.         display.print("计步器:");
  12. }
  13. void loop() {
  14.         if (((accelerometer.getStrength())>1500)) {
  15.                 mind_n_BuShu += 1;
  16.                 display.setCursor(65, 25);
  17.                 display.print("    ");
  18.                 display.setCursor(65, 25);
  19.                 display.print(mind_n_BuShu);
  20.         }
  21.         delay(300);
  22. }
复制代码

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-2 19:15:53

Mind+ 图形编程


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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-3 05:17:16

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-3 10:43:45

9、Mind+计步器2

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-3 10:49:45

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-3 13:20:08

10、使用“摇晃”指令的计步器

#MicroPython动手做(20)——掌控板之三轴加速度
#使用“摇晃”指令的计步器

  1. #MicroPython动手做(20)——掌控板之三轴加速度
  2. #使用“摇晃”指令的计步器
  3. from mpython import *
  4. import framebuf
  5. import font.digiface_30
  6. import time
  7. def on_button_a_down(_):
  8.     global a, b
  9.     time.sleep_ms(10)
  10.     if button_a.value() == 1: return
  11.     a = 1
  12. def on_button_b_down(_):
  13.     global a, b
  14.     time.sleep_ms(10)
  15.     if button_b.value() == 1: return
  16.     a = 0
  17. from machine import Timer
  18. _is_shaked = _is_thrown = False
  19. _last_x = _last_y = _last_z = _count_shaked = _count_thrown = 0
  20. def on_shaked():pass
  21. def on_thrown():pass
  22. tim11 = Timer(11)
  23. def timer11_tick(_):
  24.     global _is_shaked, _is_thrown, _last_x, _last_y, _last_z, _count_shaked, _count_thrown
  25.     if _is_shaked:
  26.         _count_shaked += 1
  27.         if _count_shaked == 5: _count_shaked = 0
  28.     if _is_thrown:
  29.         _count_thrown += 1
  30.         if _count_thrown == 10: _count_thrown = 0
  31.         if _count_thrown > 0: return
  32.     x=accelerometer.get_x(); y=accelerometer.get_y(); z=accelerometer.get_z()
  33.     _is_thrown = (x * x + y * y + z * z < 0.25)
  34.     if _is_thrown: on_thrown();return
  35.     if _last_x == 0 and _last_y == 0 and _last_z == 0:
  36.         _last_x = x; _last_y = y; _last_z = z; return
  37.     diff_x = x - _last_x; diff_y = y - _last_y; diff_z = z - _last_z
  38.     _last_x = x; _last_y = y; _last_z = z
  39.     if _count_shaked > 0: return
  40.     _is_shaked = (diff_x * diff_x + diff_y * diff_y + diff_z * diff_z > 1)
  41.     if _is_shaked: on_shaked()
  42. tim11.init(period=100, mode=Timer.PERIODIC, callback=timer11_tick)
  43. def on_shaked():
  44.     global a, b
  45.     if a == 1:
  46.         b = b + 1
  47. def display_font(_font, _str, _x, _y, _wrap, _z=0):
  48.     _start = _x
  49.     for _c in _str:
  50.         _d = _font.get_ch(_c)
  51.         if _wrap and _x > 128 - _d[2]: _x = _start; _y += _d[1]
  52.         if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d[2], _d[1], 0)
  53.         oled.blit(framebuf.FrameBuffer(bytearray(_d[0]), _d[2], _d[1],
  54.         framebuf.MONO_HLSB), (_x+int(_d[2]/_z)) if _c=='1' and _z>0 else _x, _y)
  55.         _x += _d[2]
  56. button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
  57. button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)
  58. a = 0
  59. b = 0
  60. while True:
  61.     if a == 0:
  62.         oled.fill(0)
  63.         oled.DispChar('计步器', 0, 0, 1)
  64.         oled.DispChar('每天1万步,活出好身体', 0, 16, 1)
  65.         oled.DispChar('按下A键开始计步', 0, 32, 1)
  66.         oled.DispChar('按下B键步数清零', 0, 48, 1)
  67.         oled.show()
  68.         b = 0
  69.     elif a == 1:
  70.         oled.fill(0)
  71.         oled.DispChar('步数', 0, 15, 1)
  72.         display_font(font.digiface_30, (str(b)), 30, 10, False, 2)
  73.         oled.show()
  74.         if b <= 10000:
  75.             oled.fill(0)
  76.             oled.DispChar((''.join([str(x) for x in ['还差', 10000 - b, '加油加油!']])), 0, 48, 1)
  77.             oled.show()
  78.         else:
  79.             oled.fill(0)
  80.             oled.DispChar('目标完成,你真棒!', 0, 48, 1)
  81.             oled.show()
  82.         time.sleep(1)
复制代码

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-3 13:23:57

mPython 图形编程

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-3 13:30:20

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-7 07:30:02

11、可以调节感应灵敏度的计步器
调节变量 j 的条件阕值,即可灵活调整计步器的感应灵敏度,以适应不同情况。

#MicroPython动手做(20)——掌控板之三轴加速度
#可以调节感应灵敏度的计步器

  1. #MicroPython动手做(20)——掌控板之三轴加速度
  2. #可以调节感应灵敏度的计步器
  3. import math
  4. from mpython import *
  5. import time
  6. import framebuf
  7. import font.digiface_it_42
  8. def display_font(_font, _str, _x, _y, _wrap, _z=0):
  9.     _start = _x
  10.     for _c in _str:
  11.         _d = _font.get_ch(_c)
  12.         if _wrap and _x > 128 - _d[2]: _x = _start; _y += _d[1]
  13.         if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d[2], _d[1], 0)
  14.         oled.blit(framebuf.FrameBuffer(bytearray(_d[0]), _d[2], _d[1],
  15.         framebuf.MONO_HLSB), (_x+int(_d[2]/_z)) if _c=='1' and _z>0 else _x, _y)
  16.         _x += _d[2]
  17. k = 0
  18. while True:
  19.     j = math.sqrt(accelerometer.get_x() * accelerometer.get_x() + (accelerometer.get_y() * accelerometer.get_y() + accelerometer.get_z() * accelerometer.get_z()))
  20.     if j >= 1.2:
  21.         k = k + 1
  22.         rgb[1] = (int(0), int(51), int(0))
  23.         rgb.write()
  24.         time.sleep_ms(1)
  25.         oled.fill(0)
  26.         display_font(font.digiface_it_42, (str(k)), 4, 10, False, 2)
  27.         oled.DispChar('步', 113, 48, 1)
  28.         oled.show()
复制代码
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-7 07:42:21

mPython 图形编程

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-7 07:58:48

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-7 09:48:06

12、四方向动态体感灯

#MicroPython动手做(20)——掌控板之三轴加速度
#四方向动态体感灯



  1. #MicroPython动手做(20)——掌控板之三轴加速度
  2. #四方向动态体感灯
  3. from mpython import *
  4. import time
  5. while True:
  6.     oled.fill(0)
  7.     oled.DispChar("掌控体感灯", 36, 32, 1)
  8.     oled.show()
  9.     if accelerometer.get_x() < -0.3:
  10.         oled.fill(0)
  11.         oled.DispChar("向前倾斜", 38, 16, 1)
  12.         oled.show()
  13.         rgb.fill((int(153), int(0), int(0)))
  14.         rgb.write()
  15.         time.sleep_ms(1)
  16.         time.sleep_ms(1000)
  17.     if accelerometer.get_x() > 0.3:
  18.         oled.fill(0)
  19.         oled.DispChar("向后倾斜", 38, 16, 1)
  20.         oled.show()
  21.         rgb.fill((int(51), int(51), int(255)))
  22.         rgb.write()
  23.         time.sleep_ms(1)
  24.         time.sleep_ms(1000)
  25.     if accelerometer.get_y() > 0.3:
  26.         oled.fill(0)
  27.         oled.DispChar("向左倾斜", 38, 16, 1)
  28.         oled.show()
  29.         rgb.fill((int(0), int(153), int(0)))
  30.         rgb.write()
  31.         time.sleep_ms(1)
  32.         time.sleep_ms(1000)
  33.     if accelerometer.get_y() < -0.3:
  34.         oled.fill(0)
  35.         oled.DispChar("向右倾斜", 38, 16, 1)
  36.         oled.show()
  37.         rgb.fill((int(204), int(153), int(51)))
  38.         rgb.write()
  39.         time.sleep_ms(1)
  40.         time.sleep_ms(1000)
复制代码

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-7 09:57:14

mPython X 图形编程

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-7 10:45:00

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-8 09:18:53

13、摇出好心情

#MicroPython动手做(20)——掌控板之三轴加速度
#摇出好心情

[mw_shl_code=applescript,false]#MicroPython动手做(20)——掌控板之三轴加速度
#摇出好心情

from mpython import *

import time

from machine import Timer

_is_shaked = _is_thrown = False
_last_x = _last_y = _last_z = _count_shaked = _count_thrown = 0
def on_shaked():pass
def on_thrown():pass

tim11 = Timer(11)

def timer11_tick(_):
    global _is_shaked, _is_thrown, _last_x, _last_y, _last_z, _count_shaked, _count_thrown
    if _is_shaked:
        _count_shaked += 1
        if _count_shaked == 5: _count_shaked = 0
    if _is_thrown:
        _count_thrown += 1
        if _count_thrown == 10: _count_thrown = 0
        if _count_thrown > 0: return
    x=accelerometer.get_x(); y=accelerometer.get_y(); z=accelerometer.get_z()
    _is_thrown = (x * x + y * y + z * z < 0.25)
    if _is_thrown: on_thrown();return
    if _last_x == 0 and _last_y == 0 and _last_z == 0:
        _last_x = x; _last_y = y; _last_z = z; return
    diff_x = x - _last_x; diff_y = y - _last_y; diff_z = z - _last_z
    _last_x = x; _last_y = y; _last_z = z
    if _count_shaked > 0: return
    _is_shaked = (diff_x * diff_x + diff_y * diff_y + diff_z * diff_z > 1)
    if _is_shaked: on_shaked()

tim11.init(period=100, mode=Timer.PERIODIC, callback=timer11_tick)

def on_shaked():
    global yao
    yao = yao + 10

myUI = UI(oled)

image_picture = Image()
yao = 0
oled.fill(0)
oled.DispChar('我们准备了一个小惊喜', 3, 0, 1)
oled.DispChar('你想知道是什么吗?', 8, 16, 1)
oled.show()
time.sleep_ms(1500)
while True:
    oled.fill(0)
    oled.DispChar('请大力摇晃吧', 30, 8, 1)
    myUI.ProgressBar(30, 35, 70, 8, yao)
    oled.show()
    if yao >= 100:
        break
oled.fill(0)
oled.DispChar('祝你每天有个好心情', 10, 16, 1)
oled.DispChar('笑脸常开', 40, 32, 1)
oled.show()
time.sleep_ms(2000)
oled.fill(0)
oled.blit(image_picture.load('face/4.pbm', 0), 32, 0)
oled.show()[/mw_shl_code]
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-8 09:23:55

mPython 图形编程

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-8 09:55:07

#MicroPython动手做(20)——掌控板之三轴加速度
#摇出好心情(演示视频)


https://v.youku.com/v_show/id_XN ... oneSokuUgc_1.dtitle



回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-5-8 11:30:54

14、三轴X、Y加速度水平测量仪

#MicroPython动手做(20)——掌控板之三轴加速度
#三轴X、Y加速度水平测量仪
[mw_shl_code=applescript,false]#MicroPython动手做(20)——掌控板之三轴加速度
#三轴X、Y加速度水平测量仪

from mpython import *
import framebuf
import font.dvsm_12

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:
    oled.fill(0)
    oled.vline(64, 0, 64, 1)
    oled.hline(32, 32, 64, 1)
    oled.circle(64, 32, 31, 1)
    oled.circle(64, 32, 18, 1)
    oled.circle(64, 32, 5, 1)
    oled.fill_circle(64, 32, 4, 0)
    x = int(numberMap(accelerometer.get_y(),1,(-1),92,32))
    y = int(numberMap(accelerometer.get_x(),1,(-1),2,62))
    oled.fill_circle(x, y, 4, 1)
    oled.DispChar("水平仪", 0, 0, 1)
    display_font(font.dvsm_12, (str("x:") + str(x - 64)), 92, 40, False)
    display_font(font.dvsm_12, (str("y:") + str(32 - y)), 92, 52, False)
    oled.show()[/mw_shl_code]


回复

使用道具 举报

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

本版积分规则

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

硬件清单

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

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

mail