樹莓派中python擷取GY-85九軸模組資訊樣本_python

來源:互聯網
上載者:User

先看效果圖

GY-85.py:

複製代碼 代碼如下:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import curses
from time import *
from i2clibraries import i2c_itg3205, i2c_adxl345, i2c_hmc5883l

#==========================================================
#                       GY-85感應器監控
#==========================================================


def displayITG3205(screen, col, temp, x, y, z):
    """
    顯示ITG3205讀數的方法
    """
    screen.addstr(1, col, "%.1f°℃    " % temp)
    screen.addstr(2, col, "%.1f°/s    " % x)
    screen.addstr(3, col, "%.1f°/s    " % y)
    screen.addstr(4, col, "%.1f°/s    " % z)

def displayADXL345(screen, col, x, y, z):
    """
    顯示ADXL345讀數的方法
    """
    screen.addstr(1, col, "%.2fmg    " % x)
    screen.addstr(2, col, "%.2fmg    " % y)
    screen.addstr(3, col, "%.2fmg    " % z)

def displayHMC5883L(screen, col, heading, declination, x, y, z):
    """
    顯示MC5883L讀數的方法
    """
    screen.addstr(1, col, heading + "   ")
    screen.addstr(2, col, declination + "   ")
    screen.addstr(3, col, "%.2f   " % x)
    screen.addstr(4, col, "%.2f   " % y)
    screen.addstr(5, col, "%.2f   " % z)


try:
    myscreen = curses.initscr() #初始化curses
    myscreen.border(0)
    (screen_h, screen_w) = myscreen.getmaxyx() #獲得螢幕高寬
    curses.start_color() #設定顏色
    curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_GREEN) #綠底黑字
    curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK) #白底藍字
    curses.init_pair(3, curses.COLOR_MAGENTA,curses.COLOR_BLACK) #黑底什麼字

    myscreen.clear() #清除畫布

    # 計算每塊的座標, 螢幕分3列, 每列顯示一個感應器
    col1 = screen_w / 3 * 0
    col2 = screen_w / 3 * 1
    col3 = screen_w / 3 * 2

    # 螢幕橫向分三塊,每塊中間寫上標題
    myscreen.addstr(0, int(col1 + screen_w / 3 / 2 - 3), "IGT3205", curses.color_pair(1))
    myscreen.addstr(0, int(col2 + screen_w / 3 / 2 - 4), "ADXL345", curses.color_pair(1))
    myscreen.addstr(0, int(col3 + screen_w / 3 / 2 - 4), "HMC5883L", curses.color_pair(1))


    #畫分割線,把螢幕分為3列
    for col in range(1, screen_h):
        myscreen.addstr(col, int(col2), "│")
        myscreen.addstr(col, int(col3), "│")

    # 事先列印IGT3205的各項值的名稱
    myscreen.addstr(1, int(col1), "Temp:", curses.color_pair(2))
    myscreen.addstr(2, int(col1), "X   :", curses.color_pair(2))
    myscreen.addstr(3, int(col1), "Y   :", curses.color_pair(2))
    myscreen.addstr(4, int(col1), "z   :", curses.color_pair(2))

    # 事先列印ADXL345的各項值的名稱
    myscreen.addstr(1, int(col2) + 1, "X:", curses.color_pair(2))
    myscreen.addstr(2, int(col2) + 1, "Y:", curses.color_pair(2))
    myscreen.addstr(3, int(col2) + 1, "z:", curses.color_pair(2))

    # 事先列印HMC5883L的各項值的名稱
    myscreen.addstr(1, int(col3) + 1, "Heading:    ", curses.color_pair(2))
    myscreen.addstr(2, int(col3) + 1, "Declination:", curses.color_pair(2))
    myscreen.addstr(3, int(col3) + 1, "X:          ", curses.color_pair(2))
    myscreen.addstr(4, int(col3) + 1, "Y:          ", curses.color_pair(2))
    myscreen.addstr(5, int(col3) + 1, "z:          ", curses.color_pair(2))

    # 初始化感應器
    itg3205 = i2c_itg3205.i2c_itg3205(0)

    adxl345 = i2c_adxl345.i2c_adxl345(0)

    hmc5883l = i2c_hmc5883l.i2c_hmc5883l(0)
    hmc5883l.setContinuousMode() #設定為持續更新模式
    hmc5883l.setDeclination(9,54) #設定真北磁偏角補償

    while True:
        #讀取itg3205資料
        (itgready, dataready) = itg3205.getInterruptStatus()   
        if dataready:
            temp = itg3205.getDieTemperature()
            (x, y, z) = itg3205.getDegPerSecAxes()
            displayITG3205(myscreen, 6, temp, x, y, z) #重新整理畫布

        #讀取adxl345資料
        (x, y, z) = adxl345.getAxes()
        displayADXL345(myscreen, int(col2) + 4, x, y, z) #重新整理畫布

        #讀取hmc5883l資料
        (x, y, z) = hmc5883l.getAxes()
        heading = hmc5883l.getHeadingString() #擷取指向角度
        declination = hmc5883l.getDeclinationString() #擷取磁偏角補償資訊
        displayHMC5883L(myscreen, int(col3) + 13, heading, declination, x, y, z) #重新整理畫布

        myscreen.refresh() #應用畫布
        sleep(0.1) #暫停0.1秒

    myscreen.getch()

finally:
    curses.endwin()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.