Accelerometer_Compass.ino 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* LSM303DLM Example Code base on LSM303DLH example code by Jim Lindblom SparkFun Electronics
  2. date: 9/6/11
  3. license: Creative commons share-alike v3.0
  4. Modified by:Frankie.Chu
  5. Modified by:Jacky.Zhang 2014-12-11: Ported to 6-Axis Accelerometer&Compass of Seeed Studio
  6. Modified by:Jacky.Zhang 2015-1-6: added SPI driver
  7. Summary:
  8. Show how to calculate level and tilt-compensated heading using
  9. the snazzy LSM303DLH 3-axis magnetometer/3-axis accelerometer.
  10. Firmware:
  11. You can set the accelerometer's full-scale range by setting
  12. the SCALE constant to either 2, 4, or 8. This value is used
  13. in the initLSM303() function. For the most part, all other
  14. registers in the LSM303 will be at their default value.
  15. Use the write() and read() functions to write
  16. to and read from the LSM303's internal registers.
  17. Use getLSM303_accel() and getLSM303_mag() to get the acceleration
  18. and magneto values from the LSM303. You'll need to pass each of
  19. those functions an array, where the data will be stored upon
  20. return from the void.
  21. getHeading() calculates a heading assuming the sensor is level.
  22. A float between 0 and 360 is returned. You need to pass it a
  23. array with magneto values.
  24. getTiltHeading() calculates a tilt-compensated heading.
  25. A float between 0 and 360 degrees is returned. You need
  26. to pass this function both a magneto and acceleration array.
  27. Headings are calculated as specified in AN3192:
  28. http://www.sparkfun.com/datasheets/Sensors/Magneto/Tilt%20Compensated%20Compass.pdf
  29. */
  30. /*
  31. hardware & software comment
  32. I2C mode:
  33. 1, solder the jumper "I2C EN" and the jumper of ADDR to 0x1E
  34. 2, use Lsm303d.initI2C() function to initialize the Grove by I2C
  35. SPI mode:
  36. 1, break the jumper "I2C_EN" and the jumper ADDR to any side
  37. 2, define a pin as chip select for SPI protocol.
  38. 3, use Lsm303d.initSPI(SPI_CS) function to initialize the Grove by SPI
  39. SPI.h sets these for us in arduino
  40. const int SDI = 11;
  41. const int SDO = 12;
  42. const int SCL = 13;
  43. */
  44. #include <LSM303D.h>
  45. #include <Wire.h>
  46. #include <SPI.h>
  47. /* Global variables */
  48. int accel[3]; // we'll store the raw acceleration values here
  49. int mag[3]; // raw magnetometer values stored here
  50. float realAccel[3]; // calculated acceleration values here
  51. float heading, titleHeading;
  52. #define SPI_CS 10
  53. void setup()
  54. {
  55. char rtn = 0;
  56. Serial.begin(9600); // Serial is used for debugging
  57. Serial.println("\r\npower on");
  58. //rtn = Lsm303d.initI2C();
  59. rtn = Lsm303d.initSPI(SPI_CS);
  60. if(rtn != 0) // Initialize the LSM303, using a SCALE full-scale range
  61. {
  62. Serial.println("\r\nLSM303D is not found");
  63. while(1);
  64. }
  65. else
  66. {
  67. Serial.println("\r\nLSM303D is found");
  68. }
  69. }
  70. void loop()
  71. {
  72. Serial.println("\r\n**************");
  73. //getLSM303_accel(accel); // get the acceleration values and store them in the accel array
  74. Lsm303d.getAccel(accel);
  75. while(!Lsm303d.isMagReady());// wait for the magnetometer readings to be ready
  76. Lsm303d.getMag(mag); // get the magnetometer values, store them in mag
  77. for (int i=0; i<3; i++)
  78. {
  79. realAccel[i] = accel[i] / pow(2, 15) * ACCELE_SCALE; // calculate real acceleration values, in units of g
  80. }
  81. heading = Lsm303d.getHeading(mag);
  82. titleHeading = Lsm303d.getTiltHeading(mag, realAccel);
  83. printValues();
  84. delay(200); // delay for serial readability
  85. }
  86. void printValues()
  87. {
  88. Serial.println("Acceleration of X,Y,Z is");
  89. for (int i=0; i<3; i++)
  90. {
  91. Serial.print(realAccel[i]);
  92. Serial.println("g");
  93. }
  94. /* print both the level, and tilt-compensated headings below to compare */
  95. Serial.println("The clockwise angle between the magnetic north and x-axis: ");
  96. Serial.print(heading, 3); // this only works if the sensor is level
  97. Serial.println(" degrees");
  98. Serial.print("The clockwise angle between the magnetic north and the projection");
  99. Serial.println(" of the positive x-axis in the horizontal plane: ");
  100. Serial.print(titleHeading, 3); // see how awesome tilt compensation is?!
  101. Serial.println(" degrees");
  102. }