Raspberry Pi uses python to get GY-85 9-Axis Module Information

Source: Internet
Author: User
Tags python curses
Connection Method please move here http://www.cnblogs.com/hangxin1940/archive/2013/04/05/3000395.html

The CLI window program is developed using the python curses package to refresh sensor readings in real time.

Final Effect
! [Gy85] (http://images.cnblogs.com/cnblogs_com/hangxin1940/466697/o_GY-85.jpg "gy85 ")

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 sensor monitoring
#===================================================== ==============================

Def displayitg3205 (screen, Col, temp, x, y, z ):
"""
How to display itg3205 readings
"""
Screen. addstr (1, Col, "%. 1f ° C" % 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 ):
"""
How to display the adxl345 reading
"""
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 ):
"""
How 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 () # initialize curses
Myscreen. Border (0)
(Screen_h, screen_w) = myscreen. getmaxyx () # obtain the screen height and width.
Curses. start_color () # Set the color
Curses. init_pair (1, curses. color_black, curses. color_green) # Black text
Curses. init_pair (2, curses. color_red, curses. color_black) # White-bottom blue
Curses. init_pair (3, curses. color_magenta, curses. color_black) # What is the Black Bottom?

Myscreen. Clear () # Clear the canvas

# Calculate the coordinates of each piece. The screen is divided into three columns, and 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 parts horizontally, with the title in the middle of each part
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 ))

# Draw a split line and divide the screen into three columns
For Col in range (1, screen_h ):
Myscreen. addstr (COL, INT (col2), "│ ")
Myscreen. addstr (COL, INT (col3), "│ ")

# Print the names of igt3205 values in advance
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 adxl345 in advance
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 names of hmc5883l values in advance
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 the Sensor
Itg3205 = i2c_itg3205.i2c_itg3205 (0)

Adxl345 = i2c_adxl345.i2c_adxl345 (0)

Hmc5883l = i2c_hmc5883l.i2c_hmc5883l (0)
Hmc5883l. setcontinuousmode () # Set it to the continuous update mode.
Hmc5883l. setdeclination () # sets the True North Magnetic offset compensation.

While true:
# Reading itg3205 data
(Itgready, dataready) = itg3205.getinterruptstatus ()
If dataready:
Temp = itg3205.getdietemperature ()
(X, y, z) = itg3205.getdegpersecaxes ()
Displayitg3205 (myscreen, 6, temp, x, y, z) # refresh the canvas

# Read adxl345 data
(X, y, z) = adxl345.getaxes ()
Displayadxl345 (myscreen, INT (col2) + 4, x, y, z) # refresh the canvas

# Reading hmc5883l data
(X, y, z) = hmc5883l. getaxes ()
Heading = hmc5883l. getheadingstring () # obtain the pointing Angle
Declination = hmc5883l. getdeclinationstring () # obtain the magnetic offset Compensation information
Displayhmc5883l (myscreen, INT (col3) + 13, heading, declination, x, y, z) # refresh the canvas

Myscreen. Refresh () # application canvas
Sleep (0.1) # Pause 0.1 seconds

Myscreen. getch ()

Finally:
Curses. endwin ()

Related Article

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.