No Description

lsm303.py 2.8KB

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