Pico的征途——水平仪
本篇重点在于利用pico及拓展板,借助重力传感器和LCD屏幕,把实时的姿态的变化显示在屏幕上,从而达成水平仪的效果。
标签
PICO
Micopython
重力传感器
ICISTRUE
更新2021-03-24
1392

PI DAY!

首先按照常理,我们先放上pico和拓展板的资料图。

FpHeq4UpR1jWO1us9vul2yQAnCp_

Fg9iiiNvXcsA0u6SfchQzU4fPDLRFlH0JEP-WpcsprqrJpe9bRlfTICF

具体信息和上手方法可见:https://www.eetree.cn/project/detail/2351

适逢PI DAY,在三个项目中,我们选择了充满了挑战的、有趣的一个项目--水平仪。

在此过程中,我们经历诸多困难,但同时也获得了苏公雨老师和漂移菌老师的帮助,下面我们讲具体讲述一下我们的设计思路。

第一步我们完成的是LCDST7789的显示功能。st7789的库的解决方案之前的文章里都有提到,在这里就不多赘述了,就发一个显示圆的代码作为参考。

def draw_circle(xpos0, ypos0, rad, col=st7789.color565(255, 255, 255)):
    x = rad-1
    y = 0
    dx = 1
    dy = 1
    err = dx - (rad << 1)
    while x >= y:
        display.pixel(xpos0 + x, ypos0 + y, col)
        display.pixel(xpos0 + y, ypos0 + x, col)
        display.pixel(xpos0 - y, ypos0 + x, col)
        display.pixel(xpos0 - x, ypos0 + y, col)
        display.pixel(xpos0 - x, ypos0 - y, col)
        display.pixel(xpos0 - y, ypos0 - x, col)
        display.pixel(xpos0 + y, ypos0 - x, col)
        display.pixel(xpos0 + x, ypos0 - y, col)
        if err <= 0:
            y += 1
            err += dy
            dy += 2
        if err > 0:
            x -= 1
            dx += 2
            err += dx - (rad << 1)

第二步中在硬禾设计的拓展板上MMA7660传感器是一个三轴重力传感器,因此我们利用MMA7660传感器输出三个轴上的加速度。对于此类I2C的设备的读写还没有针对pico的库,因此这部分也困扰了很久。在PI DAY上,我们在漂移菌的帮助和指导下完成了对MMA7660的读写功能。

spi0 = SPI(0, baudrate=40000000, polarity=1, phase=0, sck=SCK,mosi=MOSI) 
mma7660 = I2C(BUS,scl=SCL, sda=SDA, freq=40000000) 
display = st7789.ST7789(spi0, width, height, reset=RST, dc=DC,xstart=0, ystart=0,rotation=0) 
display.fill(st7789.color565(255,255,0)) 
time.sleep(2) 
display.fill(st7789.BLACK) 
display.text(font2,"Hello MMA7660", 10, 10)
address = mma7660.scan() 
print("I2C address:%x", hex(address[1])) 
display.text(font2,"I2C addr: %x" %address[1] , 10, 40) 
print("激活 MMA7660") 
 # 向寄存器 0x07写入 1 
mma7660.writeto_mem(76, 7, b'1') 
 # The 6-bit measurement data is stored in the XOUT (0x00), YOUT (0x01), and ZOUT (0x02) registers  
try: 
    while True: 
        x= mma7660.readfrom_mem(76, 0, 8) 
        y= mma7660.readfrom_mem(76, 1, 8) 
        z= mma7660.readfrom_mem(76, 2, 8)
        xout = struct.unpack('<f', x)[0]
        yout = struct.unpack('<f', y)[0]
        zout = struct.unpack('<f', z)[0]
        print('x:{:+.10f},y:{:+.10f},z:{:+.10f}'.format(xout,yout,zout)) 
        display.text(font2,"x:{:.3f}".format(xout), 10, 80) 
        display.text(font2,"y:{:.3f}".format(yout), 10, 120) 
        display.text(font2,"z:{:.3f}".format(zout), 10, 160)
#         time.sleep(1)
except KeyboardInterrupt: 
    print("QUIT") 
    display.fill(st7789.color565(255,0,0)) 
    display.text(font2, "GOOD BYE", 80, 120) 

第三步:将传感器读到的数据作为判断屏幕上“液泡”的坐标的依据。因为读取速度有限,在一定时间内读取的数据量不够完成从加速度到路程的二重积分,于是我们暂时将读取的实时加速度当作移动方向,将加速度的大小分段处理。因为正常的移动在移动方向上一定会存在较大加速度,我们的处理方式能够较为有效地达成目的。

try: 
    while True: 

        a= mma7660.readfrom_mem(76, 0, 8)
        b= mma7660.readfrom_mem(76, 1, 8)
           
        x=a[0:5:1]
        y=b[1:6:1]
        xout = 100000*struct.unpack('>f', x)[0]
        yout = 100000*struct.unpack('>f', y)[0]
               
        if 0 < int(xout) <= 500:
            center_x += 30
        if int(xout) > 500:
            center_x -= 30
        if 0 < int(yout) <= 500:
            center_y += 30
        if int(yout) > 500:
            center_y -= 30
        
        while center_x > 209:
            center_x = 209
        while center_x <31:
            center_x = 31
        while center_y >209:
            center_y = 209
        while center_y <31:
            center_y = 31
            
        draw_circle(center_x,center_y,30)
        time.sleep(0.5)
        display.fill(st7789.BLACK)
        
        print('x:{:+.10f},y:{:+.10f}'.format(xout,yout))
        print(center_x)
        print(center_y)
        
except KeyboardInterrupt: 
    print("QUIT") 
    display.fill(st7789.color565(255,0,0)) 
    display.text(font2, "GOOD BYE", 80, 120)

 

至此,我们的主要设计大致完成,但是具体事项需要改进的地方还有许多。之后我们还会改进项目并发在进度里,以求到达最好的体验。

团队介绍
风趣幽默,博学多才的两个单身大帅哥!
团队成员
ICISTRUE
1234567888888
评论
0 / 100
查看更多
目录
硬禾服务号
关注最新动态
0512-67862536
info@eetree.cn
江苏省苏州市苏州工业园区新平街388号腾飞创新园A2幢815室
苏州硬禾信息科技有限公司
Copyright © 2023 苏州硬禾信息科技有限公司 All Rights Reserved 苏ICP备19040198号