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

eagler8, eagler8
帖子创建于2020年04月27日 基础使用 964 次浏览 3 个赞


掌控板载

三轴加速度计MSA300,测量范围:±2G

53 条评论

eagler8

2020年04月27日

MSA300



eagler8

2020年04月27日

MSA300



eagler8

2020年04月27日


1、掌控板加速度传感器

能够测量由于重力引起的加速度,传感器在加速过程中,通过对质量块所受惯性力的测量,利用牛顿第二定律获得加速度值。掌控板上的加速度计可测量加速度,测量范围为 -2g 到 +2g 之间。

掌控板的测量沿3个轴,每个轴的测量值是正数或负数,正轴越趋近重力加速度方向,其数值往正数方向增加,反之往负数方向减小,当读数为 0 时,表示沿着该特定轴“水平”放置。

X - 向前和向后倾斜。

Y - 向左和向右倾斜。

Z - 上下翻转。

eagler8

2020年04月27日

#MicroPython动手做(20)——掌控板之三轴加速度
#简单测试3个轴加速度值的变化

from mpython import *
while True:
oled.fill(0)
x1 = accelerometer.get_x()
y1 = accelerometer.get_y()
z1 = accelerometer.get_z()
oled.DispChar('加速度 x', 3, 11, 1)
oled.DispChar((str(x1)), 52, 11, 1)
oled.DispChar('加速度 y', 3, 22, 1)
oled.DispChar((str(y1)), 52, 22, 1)
oled.DispChar('加速度 z', 3, 33, 1)
oled.DispChar((str(z1)), 52, 33, 1)
oled.show()

使用前,导入mpython模块:

from mpython import *

获取X、Y、Z三轴的加速度:

x1 = accelerometer.get_x()

y1 = accelerometer.get_y()

z1 = accelerometer.get_z()

注解

通过 accelerometer.get_x() 获取3轴加速度。获取3轴加速度获取方法分别为 get_x() 、get_y() 、get_z() 。 每个轴的测量值根据方向是正数或负数,表示以克为单位的值。

可以尝试掌控板按以下放置,观察3轴数据:

平放桌面 --(0,0,-1)

翻转平放桌面 --(0,0,1)

掌控板下板边直立与桌面 --(1,0,0)

掌控板左板边直立与桌面 --(0,1,0)

注解

发现什么规律没有?当重力加速度与加速度轴方向一致时,即等于1g的地球重力加速度。正方向为+1g,反方向为-1g。 假如猛烈地摇动掌控板,就会看到加速度达到±2g,那是因为这个加速度计的最大测量值为±2g。

eagler8

2020年04月27日

mPython 图形编程


eagler8

2020年04月27日

简单测试3个轴加速度值的变化


eagler8

2020年04月27日

3、使用柱状条演示不同状态下的三轴加速度值

#MicroPython动手做(20)——掌控板之三轴加速度
#使用柱状条演示不同状态下的三轴加速度值

from mpython import *

myUI = UI(oled)
while True:
oled.fill(0)
x1 = ((100 - 0) / (1 - (-1))) * (accelerometer.get_x() - (-1)) + 0
oled.DispChar('加速度 X', 2, 11, 1)
myUI.stripBar(50, 13, 75, 10, x1, 1, 1)
y1 = ((100 - 0) / (1 - (-1))) * (accelerometer.get_y() - (-1)) + 0
oled.DispChar('加速度 Y', 2, 22, 1)
myUI.stripBar(50, 26, 75, 10, y1, 1, 1)
z1 = ((100 - 0) / (1 - (-1))) * (accelerometer.get_z() - (-1)) + 0
oled.DispChar('加速度 Z', 2, 33, 1)
myUI.stripBar(50, 39, 75, 10, z1, 1, 1)
oled.show()

eagler8

2020年04月27日

mPython 图形编程


eagler8

2020年04月27日

柱状条演示不同状态下的三轴加速度值


eagler8

2020年04月30日

4、用加速度制作一个上下左右各滚动的水平球(小点)

描述:OLED屏幕是128*64像素,OLED屏长为X轴,宽为Y轴。可以画一个圆,半径为31像素,让“点”不会超出这个范围,确定点的位置用加速度X、Y轴。小球不超出中心小圆圈为大致处于水平位置。

加速度Y轴倾斜的值是范围1至-1,向左倾斜往1增大,向右倾斜往-1增大。通过映射把Y轴加速度的取值范围变为32至92,可以让Y轴加速度的值在OLED屏幕的中心点显示位置。

加速度X轴倾斜的值是范围-1至1,向前倾斜往-1增大,向后倾斜往1增大。通过映射把Y轴加速度的取值范围变为2至62。可以让X轴加速度的值在OLED屏幕的中心点显示位置。

映射的值有小数点,OLED屏幕是无法识别小数点的,需要将映射后的值以整型输出。

#MicroPython动手做(20)——掌控板之三轴加速度
#用加速度制作一个上下左右各滚动的水平球(小点)

from mpython import *
while True:
oled.fill(0)
oled.circle(64, 32, 2, 1)
oled.circle(64, 32, 31, 1)
oled.pixel((int(((92 - 32) / ((-1) - 1)) * (accelerometer.get_y() - 1) + 32)), (int(((62 - 2) / (1 - (-1))) * (accelerometer.get_x() - (-1)) + 2)), 1)
oled.show()

eagler8

2020年04月30日

mPython 图形编程


eagler8

2020年04月30日

用加速度制作一个上下左右各滚动的水平球(小点)


eagler8

2020年04月30日

5、三轴绿灯水平测量仪

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

from mpython import * #导入mpython模块

Center_x=63 #设定中心点(原点)x的坐标
Center_y=31 #设定中心点(原点)y的坐标

while True:

x=accelerometer.get_x() #获取X轴的加速度
y=accelerometer.get_y() #获取Y轴的加速度

if y<=1 and y>=-1:
offsetX=int(numberMap(y,1,-1,-64,64)) #映射Y轴偏移值
if x<=1 and x>=-1:
offsetY=int(numberMap(x,1,-1,32,-32)) #映射X轴偏移值
move_x=Center_x+offsetX #水平球在X坐标上的移动
move_y=Center_y+offsetY #水平球在Y坐标上的移动

oled.circle(Center_x,Center_y,8,1) #画中心固定圆:空心
oled.fill_circle(move_x,move_y,6,1) #画移动的水平球:实心
oled.DispChar("%0.1f,%0.1f" %(x,y),75,0) #显示水平球在X、Y轴的加速度值

if offsetX==0 and offsetY==0:
rgb.fill((0,20,0)) #水平球在中心位置亮绿灯,亮度为20
rgb.write()
else:
rgb.fill((0,0,0)) #水平球不在中心位置灭灯
rgb.write()
oled.show()
oled.fill(0)

eagler8

2020年04月30日

当检测到掌控板在X轴和Y轴方向倾斜时(范围-1g 至+1g),将X轴、Y轴的偏移值也就是加速度值(范围-1至1)分别映射在以设定的中心点为原点的X坐标上的Y坐标(范围32至-32)、X坐标(范围-64至64)上:

if y<=1 and y>=-1:

offsetX=int(numberMap(y,1,-1,-64,64))

if x<=1 and x>=-1:

offsetY=int(numberMap(x,1,-1,32,-32))

注解

numberMap(inputNum, bMin, bMax, cMin, cMax) 是映射函数,inputNum 为需要映射的变量,bMin 为需要映射的最小值,bMax 为需要映射的最大值,cMin 为映射的最小值,cMax 为映射的最大值。

水平球在X、Y坐标上的移动:水平球在坐标上的移动 = 中心点位置 + 加速度的偏移值:

move_x=Center_x+offsetX

move_y=Center_y+offsetY

如果水平球移动到中心位置,则亮绿灯,否则不亮灯:

if offsetX==0 and offsetY==0:

rgb.fill((0,20,0)) #水平球在中心位置亮绿灯,亮度为20

rgb.write()

else:

rgb.fill((0,0,0)) #水平球不在中心位置灭灯

rgb.write()

eagler8

2020年04月30日

三轴绿灯水平测量仪


eagler8

2020年04月30日

6、尝试三轴加速度数据探究采集

#MicroPython动手做(20)——掌控板之三轴加速度
#尝试三轴加速度数据探究采集

from mpython import *

import time
oled.fill(0)
oled.DispChar('三轴加速度数据采集', 9, 16, 1)
oled.DispChar('按A键开始 按B键结束', 6, 32, 1)
oled.show()
time.sleep_ms(50);print(('__TITLE', '加速度 X', '加速度 Y', '加速度 Z'));time.sleep_ms(50)
while True:
while button_a.value() == 0:
while not button_b.value() == 0:
print((accelerometer.get_x(), accelerometer.get_y(), accelerometer.get_z()))
time.sleep_ms(100)

eagler8

2020年04月30日

尝试三轴加速度数据探究采集



eagler8

2020年04月30日

mPython 图形编程


eagler8

2020年04月30日

探究


eagler8

2020年05月02日

7、声光水平测量仪

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

#声光水平测量仪

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

from mpython import *

import time

import music
oled.fill(0)
oled.DispChar('声光水平测量仪', 20, 22, 1)
oled.show()
time.sleep(3)
while True:
xxx = int(((127 - 0) / (1 - (-1))) * (accelerometer.get_y() - (-1)) + 0)
yyy = int(((63 - 0) / (1 - (-1))) * (accelerometer.get_x() - (-1)) + 0)
oled.fill(0)
oled.circle(64, 32, 6, 1)
oled.circle(64, 32, 31, 1)
oled.hline(44, 32, 40, 1)
oled.vline(64, 12, 40, 1)
oled.fill_circle(xxx, yyy, 4, 1)
oled.show()
if xxx == 64 and yyy == 32:
rgb.fill((int(0), int(102), int(0)))
rgb.write()
time.sleep_ms(1)
music.pitch(294, 500)
music.pitch(494, 500)
else:
rgb.fill( (0, 0, 0) )
rgb.write()
time.sleep_ms(1)

eagler8

2020年05月02日

mPython 图形编程


eagler8

2020年05月02日

声光水平测量仪


eagler8

2020年05月02日

8、简易计步器

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

//MicroPython动手做(20)——掌控板之三轴加速度
//简易计步器

#include <MPython.h>

// 动态变量
volatile float mind_n_BuShu;


// 主程序开始
void setup() {
mPython.begin();
mind_n_BuShu = 0;
display.setCursor(0, 25);
display.print("计步器:");
}
void loop() {
if (((accelerometer.getStrength())>1500)) {
mind_n_BuShu += 1;
display.setCursor(65, 25);
display.print(" ");
display.setCursor(65, 25);
display.print(mind_n_BuShu);
}
delay(300);
}

eagler8

2020年05月02日

Mind+ 图形编程


eagler8

2020年05月02日

计步器


eagler8

2020年05月03日

9、Mind+计步器2


eagler8

2020年05月03日

Mind+计步器2


eagler8

2020年05月03日

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

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

from mpython import *

import framebuf

import font.digiface_30

import time

def on_button_a_down(_):
global a, b
time.sleep_ms(10)
if button_a.value() == 1: return
a = 1

def on_button_b_down(_):
global a, b
time.sleep_ms(10)
if button_b.value() == 1: return
a = 0

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 a, b
if a == 1:
b = b + 1

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]

button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)

button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)
a = 0
b = 0
while True:
if a == 0:
oled.fill(0)
oled.DispChar('计步器', 0, 0, 1)
oled.DispChar('每天1万步,活出好身体', 0, 16, 1)
oled.DispChar('按下A键开始计步', 0, 32, 1)
oled.DispChar('按下B键步数清零', 0, 48, 1)
oled.show()
b = 0
elif a == 1:
oled.fill(0)
oled.DispChar('步数', 0, 15, 1)
display_font(font.digiface_30, (str(b)), 30, 10, False, 2)
oled.show()
if b <= 10000:
oled.fill(0)
oled.DispChar((''.join([str(x) for x in ['还差', 10000 - b, '加油加油!']])), 0, 48, 1)
oled.show()
else:
oled.fill(0)
oled.DispChar('目标完成,你真棒!', 0, 48, 1)
oled.show()
time.sleep(1)

eagler8

2020年05月03日

mPython 图形编程


eagler8

2020年05月03日

计步器


eagler8

2020年05月07日

11、可以调节感应灵敏度的计步器

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

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

import math

from mpython import *

import time

import framebuf

import font.digiface_it_42

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]
k = 0
while True:
j = math.sqrt(accelerometer.get_x() * accelerometer.get_x() + (accelerometer.get_y() * accelerometer.get_y() + accelerometer.get_z() * accelerometer.get_z()))
if j >= 1.2:
k = k + 1
rgb[1] = (int(0), int(51), int(0))
rgb.write()
time.sleep_ms(1)
oled.fill(0)
display_font(font.digiface_it_42, (str(k)), 4, 10, False, 2)
oled.DispChar('步', 113, 48, 1)
oled.show()

eagler8

2020年05月07日

mPython 图形编程


eagler8

2020年05月07日

可以调节感应灵敏度的计步器


eagler8

2020年05月07日

12、四方向动态体感灯

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

from mpython import *
import time


while True:
oled.fill(0)
oled.DispChar("掌控体感灯", 36, 32, 1)
oled.show()
if accelerometer.get_x() < -0.3:
oled.fill(0)
oled.DispChar("向前倾斜", 38, 16, 1)
oled.show()
rgb.fill((int(153), int(0), int(0)))
rgb.write()
time.sleep_ms(1)
time.sleep_ms(1000)
if accelerometer.get_x() > 0.3:
oled.fill(0)
oled.DispChar("向后倾斜", 38, 16, 1)
oled.show()
rgb.fill((int(51), int(51), int(255)))
rgb.write()
time.sleep_ms(1)
time.sleep_ms(1000)
if accelerometer.get_y() > 0.3:
oled.fill(0)
oled.DispChar("向左倾斜", 38, 16, 1)
oled.show()
rgb.fill((int(0), int(153), int(0)))
rgb.write()
time.sleep_ms(1)
time.sleep_ms(1000)
if accelerometer.get_y() < -0.3:
oled.fill(0)
oled.DispChar("向右倾斜", 38, 16, 1)
oled.show()
rgb.fill((int(204), int(153), int(51)))
rgb.write()
time.sleep_ms(1)
time.sleep_ms(1000)

eagler8

2020年05月07日

mPython X 图形编程


eagler8

2020年05月07日

四方向动态体感灯


忆梦

2020年05月07日

这个很棒哦

有 1 条回复

eagler8

2020年05月08日

13、摇出好心情

#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()

eagler8

2020年05月08日

mPython 图形编程


eagler8

2020年05月08日

摇出好心情


eagler8

2020年05月08日

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

#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()

eagler8

2020年05月08日

mPython X 图形编程


eagler8

2020年05月08日

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


eagler8

2020年05月08日

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

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

#倾斜和摇晃的一双眼睛(应用字典函数)

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

from mpython import *
from machine import Timer
import time

_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)

_dir = ''
def on_tilt_forward():pass
def on_tilt_back():pass
def on_tilt_right():pass
def on_tilt_left():pass
def on_tilt_none():pass

tim14 = Timer(14)

def timer14_tick(_):
global _dir
if accelerometer.get_x() < -0.3:
if 'F' != _dir:_dir = 'F';on_tilt_forward()
elif accelerometer.get_x() > 0.3:
if 'B' != _dir:_dir = 'B';on_tilt_back()
elif accelerometer.get_y() < -0.3:
if 'R' != _dir:_dir = 'R';on_tilt_right()
elif accelerometer.get_y() > 0.3:
if 'L' != _dir:_dir = 'L';on_tilt_left()
else:
if '' != _dir:_dir = '';on_tilt_none()

tim14.init(period=200, mode=Timer.PERIODIC, callback=timer14_tick)

def on_tilt_forward():
global face, dt_faces
face = dt_faces.get("Up")

def on_tilt_back():
global face, dt_faces
face = dt_faces.get("Down")

def on_tilt_left():
global face, dt_faces
face = dt_faces.get("Left")

def on_tilt_right():
global face, dt_faces
face = dt_faces.get("Right")

def on_tilt_none():
global face, dt_faces
face = dt_faces.get("Neutral")

image_picture = Image()


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)}
face = dt_faces.get("Neutral")
while True:
oled.fill(0)
if _is_shaked:
oled.blit(dt_faces.get("Dizzy"), 20, 0)
oled.show()
time.sleep_ms(2000)
else:
oled.blit(face, 20, 0)
oled.show()

字典

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

eagler8

2020年05月08日

mPython X 图形编程


eagler8

2020年05月08日

mPython X 图形编程


eagler8

2020年05月08日

倾斜和摇晃的一双眼睛(应用字典函数)


eagler8

2020年05月08日

16、水平仪和测量角度

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

#水平仪和测量角度

#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()

eagler8

2020年05月08日

mPython X 图形编程


eagler8

2020年05月08日

水平仪和测量角度


还有 3 条评论,查看全部