Raspberry pie python get GY-85 Nine-axis module information Sample _python

Source: Internet
Author: User

Look at the effect chart first

gy-85.py:

Copy Code code as follows:

#!/usr/bin/python3
#-*-Coding:utf-8-*-
Import Curses
From time Import *
From i2clibraries import i2c_itg3205, i2c_adxl345, i2c_hmc5883l

#==========================================================
# GY-85 Sensor monitoring
#==========================================================


def displayITG3205 (screen, col, temp, x, Y, z):
"""
Ways to display ITG3205 readings
"""
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):
"""
Ways to display ADXL345 readings
"""
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):
"""
Ways to display mc5883l readings
"""
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 () #清除画布

# calculates the coordinates of each block, the screen is divided into 3 columns, each column displays a sensor
col1 = SCREEN_W/3 * 0
col2 = SCREEN_W/3 * 1
col3 = SCREEN_W/3 * 2

# The screen is divided into three pieces, each piece of the middle write the title
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))


#画分割线, divide the screen into 3 columns
For Col in range (1, screen_h):
Myscreen.addstr (col, int (col2), "│")
Myscreen.addstr (col, int (col3), "│")

# Print the name of each value of the IGT3205 beforehand
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))

# Print the name of each value of the ADXL345 beforehand
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))

# Print the name of each value of the hmc5883l beforehand
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))

# Initialize Sensor
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) +, heading, declination, X, Y, z) #刷新画布

Myscreen.refresh () #应用画布
Sleep (0.1) #暂停0.1 seconds

Myscreen.getch ()

Finally
Curses.endwin ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.