樹莓派使用python獲取GY-85九軸模塊信息
連接方法請移步這里 http://www.rzrgm.cn/hangxin1940/archive/2013/04/05/3000395.html
這里使用Python的curses包開發(fā)cli窗口程序,用來實(shí)時(shí)刷新傳感器的讀數(shù)
最終的效果

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傳感器監(jiān)控
#==========================================================
def displayITG3205(screen, col, temp, x, y, z):
"""
顯示ITG3205讀數(shù)的方法
"""
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讀數(shù)的方法
"""
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讀數(shù)的方法
"""
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() #設(shè)置顏色
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_GREEN) #綠底黑字
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK) #白底藍(lán)字
curses.init_pair(3, curses.COLOR_MAGENTA,curses.COLOR_BLACK) #黑底什么字
myscreen.clear() #清除畫布
# 計(jì)算每塊的坐標(biāo), 屏幕分3列, 每列顯示一個(gè)傳感器
col1 = screen_w / 3 * 0
col2 = screen_w / 3 * 1
col3 = screen_w / 3 * 2
# 屏幕橫向分三塊,每塊中間寫上標(biāo)題
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的各項(xiàng)值的名稱
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的各項(xiàng)值的名稱
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的各項(xiàng)值的名稱
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() #設(shè)置為持續(xù)更新模式
hmc5883l.setDeclination(9,54) #設(shè)置真北磁偏角補(bǔ)償
while True:
#讀取itg3205數(shù)據(jù)
(itgready, dataready) = itg3205.getInterruptStatus()
if dataready:
temp = itg3205.getDieTemperature()
(x, y, z) = itg3205.getDegPerSecAxes()
displayITG3205(myscreen, 6, temp, x, y, z) #刷新畫布
#讀取adxl345數(shù)據(jù)
(x, y, z) = adxl345.getAxes()
displayADXL345(myscreen, int(col2) + 4, x, y, z) #刷新畫布
#讀取hmc5883l數(shù)據(jù)
(x, y, z) = hmc5883l.getAxes()
heading = hmc5883l.getHeadingString() #獲取指向角度
declination = hmc5883l.getDeclinationString() #獲取磁偏角補(bǔ)償信息
displayHMC5883L(myscreen, int(col3) + 13, heading, declination, x, y, z) #刷新畫布
myscreen.refresh() #應(yīng)用畫布
sleep(0.1) #暫停0.1秒
myscreen.getch()
finally:
curses.endwin()
posted on 2013-04-06 01:28 黑暗伯爵 閱讀(1811) 評(píng)論(1) 收藏 舉報(bào)
浙公網(wǎng)安備 33010602011771號(hào)