Skip to content

Instantly share code, notes, and snippets.

@bboyho
Created December 16, 2020 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bboyho/374df58471544271a6ad9941e0492db8 to your computer and use it in GitHub Desktop.
Save bboyho/374df58471544271a6ad9941e0492db8 to your computer and use it in GitHub Desktop.
Combined Example for the Top pHAT and Qwiic ICM20948 Device. The WS2812 LEDs will change color based on the accelerometer's orientation.
#!/usr/bin/env python3
#
#-----------------------------------------------------------------------------
# 9DOF_ICM_Mod.py
#
# Combined Example for the Top pHAT and Qwiic ICM20948 Device.
# The WS2812 LEDs will change color based on the accelerometer's
# orientation.
#
#------------------------------------------------------------------------
#
# Written by SparkFun Electronics, March 2020
# This python library supports the SparkFun Electroncis qwiic
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
# board computers.
#
# More information on qwiic is at https://www.sparkfun.com/qwiic
#
# Do you like this library? Help support SparkFun. Buy a board!
#
#==================================================================================
# Copyright (c) 2019 SparkFun Electronics
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#==================================================================================
from __future__ import print_function
import qwiic_icm20948
import board
import neopixel
import time
import sys
# Create the class object
pixels = neopixel.NeoPixel(board.D12, 6)
#create global variable to keep track of color when debugging
ledColor = 1
def runExample():
print("\nSparkFun Top pHAT and 9DoF ICM-20948 Sensor Combined Example\n")
IMU = qwiic_icm20948.QwiicIcm20948()
if IMU.connected == False:
print("The Qwiic ICM20948 device isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
IMU.begin()
while True:
#declare ledColor a global variable inside here to access it
global ledColor
if IMU.dataReady():
IMU.getAgmt() # read all axis and temp from sensor, note this also updates all instance variables
#the following are the threshold values for each axis is pointing right-side up
# anything above IMU.azRaw > 16000 is red
# ledColor = 1
aZPos = 16000
# anything below IMU.azRaw < -16000 is blue
# ledColor = 2
aZNeg = -16000
# anything above IMU.ayRaw > 16100 is green
# ledColor = 3
ayPos = 16100
# anything below IMU.ayRaw < -16000 is green
# ledColor = 4
ayNeg = -16000
# anything above IMU.axRaw > 16000 is magenta
# ledColor = 5
axPos = 16000
# anything below IMU.axRaw < -16400 is cyan
# ledColor = 6
axNeg = -16400
#adjust color of the LED based on the accelerometer's reading
if IMU.azRaw > aZPos:
for i in range(6):
# Set LED red
pixels[i]=(10,0,0)
ledColor = 1
elif IMU.azRaw < aZNeg:
for i in range(6):
# Set LED blue
pixels[i]=(0,0,10)
ledColor = 2
elif IMU.ayRaw > ayPos:
for i in range(6):
# Set LED yellow
pixels[i]=(10,10,0)
ledColor = 3
elif IMU.ayRaw < ayNeg:
for i in range(6):
# Set LED green
pixels[i]=(0,10,0)
ledColor = 4
elif IMU.axRaw > axPos:
for i in range(6):
# Set LED magenta
pixels[i]=(10,0,10)
ledColor = 5
elif IMU.axRaw < axNeg:
for i in range(6):
# Set LED cyan
pixels[i]=(0,10,10)
ledColor = 6
if ledColor == 1:
print("ledColor = red" ,'\n', '\n')
elif ledColor == 2:
print("ledColor = blue" ,'\n', '\n')
elif ledColor == 3:
print("ledColor = yellow" ,'\n', '\n')
elif ledColor == 4:
print("ledColor = green" ,'\n', '\n')
elif ledColor == 5:
print("ledColor = magenta" ,'\n', '\n')
elif ledColor == 6:
print("ledColor = cyan" ,'\n', '\n')
print(\
' aX:', '{: 06d}'.format(IMU.axRaw)\
, 'aY:', '{: 06d}'.format(IMU.ayRaw)\
, 'aZ:', '{: 06d}'.format(IMU.azRaw)\
, '\n'
, '\n'
, 'gX:', '{: 06d}'.format(IMU.gxRaw)\
, 'gY:', '{: 06d}'.format(IMU.gyRaw)\
, 'gZ:', '{: 06d}'.format(IMU.gzRaw)\
, '\n'
, '\n'
, 'mX:', '{: 06d}'.format(IMU.mxRaw)\
, 'mY:', '{: 06d}'.format(IMU.myRaw)\
, 'mZ:', '{: 06d}'.format(IMU.mzRaw)\
, '\n'
, '\n'
, '\n'
, '\n'
)
time.sleep(0.03)
else:
print("Waiting for data")
time.sleep(0.5)
if __name__ == '__main__':
try:
runExample()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding Example 1")
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment