検索
ホームページバックエンド開発Python チュートリアルRaspberry PiでPythonを使用してGY-85の9軸モジュール情報を取得する例

先看效果图

Raspberry PiでPythonを使用してGY-85の9軸モジュール情報を取得する例

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:
#Read itg3205 data
(itgready, dataready) = itg3205.getInterruptStatus()
if dataready:
temp = itg3205.getD ieTemperature()
(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) #キャンバスを更新

スルー ’ s ’ s ’ s ’ s ‐ ‐ ‐ ‐ to DeclinationString() #磁気偏角を取得補正情報
displayHMC5883L(myscreen, int(col3) + 13, 方位, 赤緯, x, y, z) #キャンバスを更新

myscreen.refresh() #アプリケーションキャンバス
sleep(0.1) #0.1 秒間一時停止

myscreen.getch()

最後に:

curses.endwin()


声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
Pythonを使用した科学コンピューティングでアレイはどのように使用されていますか?Pythonを使用した科学コンピューティングでアレイはどのように使用されていますか?Apr 25, 2025 am 12:28 AM

Arraysinpython、特にvianumpy、arecrucialinscientificComputing fortheirefficienty andversitility.1)彼らは、fornumericaloperations、data analysis、andmachinelearning.2)numpy'simplementation incensuresfasteroperationsthanpasteroperations.3)arayableminablecickick

同じシステムで異なるPythonバージョンをどのように処理しますか?同じシステムで異なるPythonバージョンをどのように処理しますか?Apr 25, 2025 am 12:24 AM

Pyenv、Venv、およびAnacondaを使用して、さまざまなPythonバージョンを管理できます。 1)Pyenvを使用して、複数のPythonバージョンを管理します。Pyenvをインストールし、グローバルバージョンとローカルバージョンを設定します。 2)VENVを使用して仮想環境を作成して、プロジェクトの依存関係を分離します。 3)Anacondaを使用して、データサイエンスプロジェクトでPythonバージョンを管理します。 4)システムレベルのタスク用にシステムPythonを保持します。これらのツールと戦略を通じて、Pythonのさまざまなバージョンを効果的に管理して、プロジェクトのスムーズな実行を確保できます。

標準のPythonアレイでnumpyアレイを使用することの利点は何ですか?標準のPythonアレイでnumpyアレイを使用することの利点は何ですか?Apr 25, 2025 am 12:21 AM

numpyarrayshaveveraladvantages-averstandardpythonarrays:1)thealmuchfasterduetocベースのインプレンテーション、2)アレモレメモリ効率、特にlargedatasets、および3)それらは、拡散化された、構造化された形成術科療法、

アレイの均質な性質はパフォーマンスにどのように影響しますか?アレイの均質な性質はパフォーマンスにどのように影響しますか?Apr 25, 2025 am 12:13 AM

パフォーマンスに対する配列の均一性の影響は二重です。1)均一性により、コンパイラはメモリアクセスを最適化し、パフォーマンスを改善できます。 2)しかし、タイプの多様性を制限し、それが非効率につながる可能性があります。要するに、適切なデータ構造を選択することが重要です。

実行可能なPythonスクリプトを作成するためのベストプラクティスは何ですか?実行可能なPythonスクリプトを作成するためのベストプラクティスは何ですか?Apr 25, 2025 am 12:11 AM

craftexecutablepythonscripts、次のようになります

numpyアレイは、アレイモジュールを使用して作成された配列とどのように異なりますか?numpyアレイは、アレイモジュールを使用して作成された配列とどのように異なりますか?Apr 24, 2025 pm 03:53 PM

numpyarraysarasarebetterfornumeroperations andmulti-dimensionaldata、whilethearraymoduleissuitable forbasic、1)numpyexcelsinperformance and forlargedatasentassandcomplexoperations.2)thearraymuremememory-effictientivearientfa

Numpyアレイの使用は、Pythonで配列モジュール配列の使用と比較してどのように比較されますか?Numpyアレイの使用は、Pythonで配列モジュール配列の使用と比較してどのように比較されますか?Apr 24, 2025 pm 03:49 PM

NumPyArraySareBetterforHeavyNumericalComputing、whilethearrayarayismoreSuitableformemory-constrainedprojectswithsimpledatatypes.1)numpyarraysofferarays andatiledance andpeperancedatasandatassandcomplexoperations.2)thearraymoduleisuleiseightweightandmemememe-ef

CTypesモジュールは、Pythonの配列にどのように関連していますか?CTypesモジュールは、Pythonの配列にどのように関連していますか?Apr 24, 2025 pm 03:45 PM

ctypesallowsinging andmanipulatingc-stylearraysinpython.1)usectypestointerfacewithclibrariesforperformance.2)createc-stylearraysfornumericalcomputations.3)passarraystocfunctions foreffientientoperations.how、how、becuutiousmorymanagemation、performanceo

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

mPDF

mPDF

mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境