1#!/usr/bin/python 2 3# Author: Brendan Le Foll <brendan.le.foll@intel.com> 4# Contributions: Zion Orent <zorent@ics.com> 5# Copyright (c) 2014 Intel Corporation. 6# 7# Permission is hereby granted, free of charge, to any person obtaining 8# a copy of this software and associated documentation files (the 9# "Software"), to deal in the Software without restriction, including 10# without limitation the rights to use, copy, modify, merge, publish, 11# distribute, sublicense, and/or sell copies of the Software, and to 12# permit persons to whom the Software is furnished to do so, subject to 13# the following conditions: 14# 15# The above copyright notice and this permission notice shall be 16# included in all copies or substantial portions of the Software. 17# 18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE 25 26import time, sys, signal, atexit 27import pyupm_lsm303 as lsm303 28 29# Instantiate LSM303 compass on I2C 30myAccelrCompass = lsm303.LSM303(0) 31 32 33## Exit handlers ## 34# This stops python from printing a stacktrace when you hit control-C 35def SIGINTHandler(signum, frame): 36 raise SystemExit 37 38# This lets you run code on exit, 39# including functions from myAccelrCompass 40def exitHandler(): 41 print "Exiting" 42 sys.exit(0) 43 44# Register exit handlers 45atexit.register(exitHandler) 46signal.signal(signal.SIGINT, SIGINTHandler) 47 48 49while(1): 50 # Load coordinates into LSM303 object 51 successFail = myAccelrCompass.getCoordinates() 52 # in XYZ order. The sensor returns XZY, 53 # but the driver compensates and makes it XYZ 54 coords = myAccelrCompass.getRawCoorData() 55 56 # Print out the X, Y, and Z coordinate data 57 # using two different methods 58 outputStr = "coor: rX {0} - rY {1} - rZ {2}".format( 59 coords.__getitem__(0), coords.__getitem__(1), 60 coords.__getitem__(2)) 61 print outputStr 62 63 outputStr = "coor: gX {0} - gY {1} - gZ {2}".format( 64 myAccelrCompass.getCoorX(), myAccelrCompass.getCoorY(), 65 myAccelrCompass.getCoorZ()) 66 print outputStr 67 68 # Get and print out the heading 69 print "heading:", myAccelrCompass.getHeading() 70 71 # Get the acceleration 72 myAccelrCompass.getAcceleration(); 73 accel = myAccelrCompass.getRawAccelData(); 74 75 # Print out the X, Y, and Z acceleration data 76 # using two different methods 77 outputStr = "acc: rX {0} - rY {1} - Z {2}".format( 78 accel.__getitem__(0), accel.__getitem__(1), accel.__getitem__(2)) 79 print outputStr 80 81 outputStr = "acc: gX {0} - gY {1} - gZ {2}".format( 82 myAccelrCompass.getAccelX(), myAccelrCompass.getAccelY(), 83 myAccelrCompass.getAccelZ()) 84 print outputStr 85 86 print " " 87 time.sleep(1) 88