2015-03-26 15:58:10 -04:00
|
|
|
#!/usr/bin/python
|
2014-09-05 18:57:56 +01:00
|
|
|
|
|
|
|
# Author: Brendan Le Foll <brendan.le.foll@intel.com>
|
2015-03-26 15:58:10 -04:00
|
|
|
# Contributions: Zion Orent <zorent@ics.com>
|
2014-09-05 18:57:56 +01:00
|
|
|
# Copyright (c) 2014 Intel Corporation.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2016-10-10 14:48:42 -07:00
|
|
|
from __future__ import print_function
|
2015-03-26 15:58:10 -04:00
|
|
|
import time, sys, signal, atexit
|
2016-10-10 14:48:42 -07:00
|
|
|
from upm import pyupm_lsm303 as lsm303
|
2014-09-05 18:57:56 +01:00
|
|
|
|
2016-09-29 18:24:19 -07:00
|
|
|
def main():
|
|
|
|
# Instantiate LSM303 compass on I2C
|
|
|
|
myAccelrCompass = lsm303.LSM303(0)
|
|
|
|
|
|
|
|
## Exit handlers ##
|
|
|
|
# This stops python from printing a stacktrace when you hit control-C
|
|
|
|
def SIGINTHandler(signum, frame):
|
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
# This lets you run code on exit,
|
|
|
|
# including functions from myAccelrCompass
|
|
|
|
def exitHandler():
|
2016-10-10 14:48:42 -07:00
|
|
|
print("Exiting")
|
2016-09-29 18:24:19 -07:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
# Register exit handlers
|
|
|
|
atexit.register(exitHandler)
|
|
|
|
signal.signal(signal.SIGINT, SIGINTHandler)
|
|
|
|
|
|
|
|
while(1):
|
|
|
|
# Load coordinates into LSM303 object
|
|
|
|
successFail = myAccelrCompass.getCoordinates()
|
|
|
|
# in XYZ order. The sensor returns XZY,
|
|
|
|
# but the driver compensates and makes it XYZ
|
|
|
|
coords = myAccelrCompass.getRawCoorData()
|
|
|
|
|
|
|
|
# Print out the X, Y, and Z coordinate data
|
|
|
|
# using two different methods
|
|
|
|
outputStr = "coor: rX {0} - rY {1} - rZ {2}".format(
|
|
|
|
coords.__getitem__(0), coords.__getitem__(1),
|
|
|
|
coords.__getitem__(2))
|
2016-10-10 14:48:42 -07:00
|
|
|
print(outputStr)
|
2016-09-29 18:24:19 -07:00
|
|
|
|
|
|
|
outputStr = "coor: gX {0} - gY {1} - gZ {2}".format(
|
|
|
|
myAccelrCompass.getCoorX(), myAccelrCompass.getCoorY(),
|
|
|
|
myAccelrCompass.getCoorZ())
|
2016-10-10 14:48:42 -07:00
|
|
|
print(outputStr)
|
2016-09-29 18:24:19 -07:00
|
|
|
|
|
|
|
# Get and print out the heading
|
2016-10-10 14:48:42 -07:00
|
|
|
print("heading:", myAccelrCompass.getHeading())
|
2016-09-29 18:24:19 -07:00
|
|
|
|
|
|
|
# Get the acceleration
|
|
|
|
myAccelrCompass.getAcceleration();
|
|
|
|
accel = myAccelrCompass.getRawAccelData();
|
|
|
|
|
|
|
|
# Print out the X, Y, and Z acceleration data
|
|
|
|
# using two different methods
|
|
|
|
outputStr = "acc: rX {0} - rY {1} - Z {2}".format(
|
|
|
|
accel.__getitem__(0), accel.__getitem__(1), accel.__getitem__(2))
|
2016-10-10 14:48:42 -07:00
|
|
|
print(outputStr)
|
2016-09-29 18:24:19 -07:00
|
|
|
|
|
|
|
outputStr = "acc: gX {0} - gY {1} - gZ {2}".format(
|
|
|
|
myAccelrCompass.getAccelX(), myAccelrCompass.getAccelY(),
|
|
|
|
myAccelrCompass.getAccelZ())
|
2016-10-10 14:48:42 -07:00
|
|
|
print(outputStr)
|
2016-09-29 18:24:19 -07:00
|
|
|
|
2016-10-10 14:48:42 -07:00
|
|
|
print(" ")
|
2016-09-29 18:24:19 -07:00
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|