Example of python getting GY-85 9-axis module information in Raspberry Pi

Source: Internet
Author: User
Tags python curses
The content of this article is Raspberry Pi python get GY-85 nine axis module information example, here using Python curses package development cli window program, used to refresh the sensor reading in real time, see the code first

GY-85.py:

The code is 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 ):
"""
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 ()

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.