樹(shù)莓派中python獲取GY-85九軸模塊信息示例
先看效果圖
![]() |
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() #清除畫(huà)布
# 計(jì)算每塊的坐標(biāo), 屏幕分3列, 每列顯示一個(gè)傳感器
col1 = screen_w / 3 * 0
col2 = screen_w / 3 * 1
col3 = screen_w / 3 * 2
# 屏幕橫向分三塊,每塊中間寫(xiě)上標(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))
#畫(huà)分割線,把屏幕分為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) #刷新畫(huà)布
#讀取adxl345數(shù)據(jù)
(x, y, z) = adxl345.getAxes()
displayADXL345(myscreen, int(col2) + 4, x, y, z) #刷新畫(huà)布
#讀取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) #刷新畫(huà)布
myscreen.refresh() #應(yīng)用畫(huà)布
sleep(0.1) #暫停0.1秒
myscreen.getch()
finally:
curses.endwin()
- 在樹(shù)莓派2或樹(shù)莓派B+上安裝Python和OpenCV的教程
- 使用Python簡(jiǎn)單的實(shí)現(xiàn)樹(shù)莓派的WEB控制
- Python+樹(shù)莓派+YOLO打造一款人工智能照相機(jī)
- Ubuntu16.04/樹(shù)莓派Python3+opencv配置教程(分享)
- Python實(shí)現(xiàn)樹(shù)莓派WiFi斷線自動(dòng)重連的實(shí)例代碼
- 樹(shù)莓派用python中的OpenCV輸出USB攝像頭畫(huà)面
- 樹(shù)莓派與PC端在局域網(wǎng)內(nèi)運(yùn)用python實(shí)現(xiàn)即時(shí)通訊
- python樹(shù)莓派紅外反射傳感器
- 樹(shù)莓派使用python-librtmp實(shí)現(xiàn)rtmp推流h264的方法
- 樹(shù)莓派采用socket方式文件傳輸(python)
- 樹(shù)莓派4B+opencv4+python 打開(kāi)攝像頭的實(shí)現(xiàn)方法
- Python樹(shù)莓派學(xué)習(xí)筆記之UDP傳輸視頻幀操作詳解
相關(guān)文章
Python網(wǎng)絡(luò)編程中urllib2模塊的用法總結(jié)
使用urllib2模塊進(jìn)行基于url的HTTP請(qǐng)求等操作大家也許都比較熟悉,這里我們?cè)偕钊雭?lái)了解一下urllib2針對(duì)HTTP的異常處理相關(guān)功能,一起來(lái)看一下Python網(wǎng)絡(luò)編程中urllib2模塊的用法總結(jié):2016-07-07教你十行代碼實(shí)現(xiàn)python向手機(jī)推送通知功能
這篇文章主要介紹了十行代碼實(shí)現(xiàn)python向手機(jī)推送通知,這里使用的是pushplus的服務(wù),代碼也很簡(jiǎn)單,運(yùn)行代碼后也是很快就可以收到消息推送,需要的朋友可以參考下2022-04-04詳解Django 時(shí)間與時(shí)區(qū)設(shè)置問(wèn)題
這篇文章主要介紹了Django 時(shí)間與時(shí)區(qū)設(shè)置問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07對(duì)Python 3.5拼接列表的新語(yǔ)法詳解
今天小編就為大家分享一篇對(duì)Python 3.5拼接列表的新語(yǔ)法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11python with statement 進(jìn)行文件操作指南
在Python中,with關(guān)鍵字是一個(gè)替你管理實(shí)現(xiàn)上下文協(xié)議對(duì)象的好東西。例如:file等。在file的結(jié)束,會(huì)自動(dòng)關(guān)閉該文件句柄。而這正是本文所需要的2014-08-08Python實(shí)現(xiàn)Word表格轉(zhuǎn)成Excel表格的示例代碼
這篇文章主要介紹了Python實(shí)現(xiàn)Word表格轉(zhuǎn)成Excel表格的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04