Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1.  
  2. #include <Wire.h>
  3. #include <Adafruit_Sensor.h>
  4. #include <Adafruit_LSM303_U.h>
  5. #include <Adafruit_L3GD20_U.h>
  6. #include <Adafruit_9DOF.h>
  7.  
  8. /* Assign a unique ID to the sensors */
  9. Adafruit_9DOF dof = Adafruit_9DOF();
  10. Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301);
  11. Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(30302);
  12.  
  13. /* Update this with the correct SLP for accurate altitude measurements */
  14. float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
  15.  
  16. /**************************************************************************/
  17. /*!
  18. @brief Initialises all the sensors used by this example
  19. */
  20. /**************************************************************************/
  21. void initSensors()
  22. {
  23. if(!accel.begin())
  24. {
  25. /* There was a problem detecting the LSM303 ... check your connections */
  26. Serial.println(F("Ooops, no LSM303 detected ... Check your wiring!"));
  27. while(1);
  28. }
  29. if(!mag.begin())
  30. {
  31. /* There was a problem detecting the LSM303 ... check your connections */
  32. Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
  33. while(1);
  34. }
  35. }
  36.  
  37. /**************************************************************************/
  38. /*!
  39.  
  40. */
  41. /**************************************************************************/
  42. void setup(void)
  43. {
  44. Serial.begin(115200);
  45. Serial.println(F("Adafruit 9 DOF Pitch/Roll/Heading Example")); Serial.println("");
  46.  
  47. /* Initialise the sensors */
  48. initSensors();
  49. }
  50.  
  51. /**************************************************************************/
  52. /*!
  53. @brief Constantly check the roll/pitch/heading/altitude/temperature
  54. */
  55. /**************************************************************************/
  56. void loop(void)
  57. {
  58. sensors_event_t accel_event;
  59. sensors_event_t mag_event;
  60. sensors_vec_t orientation;
  61.  
  62. /* Calculate pitch and roll from the raw accelerometer data */
  63. accel.getEvent(&accel_event);
  64. if (dof.accelGetOrientation(&accel_event, &orientation))
  65. {
  66. /* 'orientation' should have valid .roll and .pitch fields */
  67. Serial.print(F("Roll: "));
  68. Serial.print(orientation.roll);
  69. Serial.print(F("; "));
  70. Serial.print(F("Pitch: "));
  71. Serial.print(orientation.pitch);
  72. Serial.print(F("; "));
  73. }
  74.  
  75. /* Calculate the heading using the magnetometer */
  76. mag.getEvent(&mag_event);
  77. if (dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation))
  78. {
  79. /* 'orientation' should have valid .heading data now */
  80. Serial.print(F("Heading: "));
  81. Serial.print(orientation.heading);
  82. Serial.print(F("; "));
  83. }
  84.  
  85. Serial.println(F(""));
  86. delay(1000);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement