FifoExample.ino 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <LSM6DS3.h>
  2. #include <Wire.h>
  3. //Create a instance of class LSM6DS3
  4. LSM6DS3 myIMU(I2C_MODE, 0x6A); //I2C device address 0x6A
  5. void setup( void ) {
  6. //Over-ride default settings if desired
  7. myIMU.settings.gyroEnabled = 1; //Can be 0 or 1
  8. myIMU.settings.gyroRange = 2000; //Max deg/s. Can be: 125, 245, 500, 1000, 2000
  9. myIMU.settings.gyroSampleRate = 833; //Hz. Can be: 13, 26, 52, 104, 208, 416, 833, 1666
  10. myIMU.settings.gyroBandWidth = 200; //Hz. Can be: 50, 100, 200, 400;
  11. myIMU.settings.gyroFifoEnabled = 1; //Set to include gyro in FIFO
  12. myIMU.settings.gyroFifoDecimation = 1; //set 1 for on /1
  13. myIMU.settings.accelEnabled = 1;
  14. myIMU.settings.accelRange = 16; //Max G force readable. Can be: 2, 4, 8, 16
  15. myIMU.settings.accelSampleRate = 833; //Hz. Can be: 13, 26, 52, 104, 208, 416, 833, 1666, 3332, 6664, 13330
  16. myIMU.settings.accelBandWidth = 200; //Hz. Can be: 50, 100, 200, 400;
  17. myIMU.settings.accelFifoEnabled = 1; //Set to include accelerometer in the FIFO
  18. myIMU.settings.accelFifoDecimation = 1; //set 1 for on /1
  19. myIMU.settings.tempEnabled = 1;
  20. //Non-basic mode settings
  21. myIMU.settings.commMode = 1;
  22. myIMU.settings.timestampEnabled=1; // 1: enable timestamp ; 0: disable timestamp
  23. myIMU.settings.timestampFifoEnabled=1;// 1: enable write timestamp into fifo ; 0: disable
  24. myIMU.settings.timestampResolution=1; // 1: Set timestamp resolution ; 0: 6.4ms 1: 25us
  25. //FIFO control settings
  26. myIMU.settings.fifoThreshold = 100; //Can be 0 to 4096 (16 bit bytes)
  27. myIMU.settings.fifoSampleRate = 50; //Hz. Can be: 10, 25, 50, 100, 200, 400, 800, 1600, 3300, 6600
  28. myIMU.settings.fifoModeWord = 6; //FIFO mode.
  29. //FIFO mode. Can be:
  30. // 0 (Bypass mode, FIFO off)
  31. // 1 (Stop when full)
  32. // 3 (Continuous during trigger)
  33. // 4 (Bypass until trigger)
  34. // 6 (Continous mode)
  35. Serial.begin(57600); // start serial for output
  36. delay(1000); //relax...
  37. Serial.println("Processor came out of reset.\n");
  38. //Call .begin() to configure the IMUs
  39. if( myIMU.begin() != 0 )
  40. {
  41. Serial.println("Problem starting the sensor with CS @ Pin 10.");
  42. }
  43. else
  44. {
  45. Serial.println("Sensor with CS @ Pin 10 started.");
  46. }
  47. Serial.print("Configuring FIFO with no error checking...");
  48. myIMU.fifoBegin();
  49. Serial.print("Done!\n");
  50. Serial.print("Clearing out the FIFO...");
  51. myIMU.fifoClear();
  52. Serial.print("Done!\n");
  53. }
  54. void loop()
  55. {
  56. float temp; //This is to hold read data
  57. uint16_t tempUnsigned;
  58. while( ( myIMU.fifoGetStatus() & 0x8000 ) == 0 ) {}; //Wait for watermark
  59. //Now loop until FIFO is empty. NOTE: As the FIFO is only 8 bits wide,
  60. //the channels must be synchronized to a known position for the data to align
  61. //properly. Emptying the fifo is one way of doing this (this example)
  62. while( ( myIMU.fifoGetStatus() & 0x1000 ) == 0 ) {
  63. temp = myIMU.calcGyro(myIMU.fifoRead());
  64. Serial.print(temp);
  65. Serial.print(",");
  66. temp = myIMU.calcGyro(myIMU.fifoRead());
  67. Serial.print(temp);
  68. Serial.print(",");
  69. temp = myIMU.calcGyro(myIMU.fifoRead());
  70. Serial.print(temp);
  71. Serial.print(",");
  72. temp = myIMU.calcAccel(myIMU.fifoRead());
  73. Serial.print(temp);
  74. Serial.print(",");
  75. temp = myIMU.calcAccel(myIMU.fifoRead());
  76. Serial.print(temp);
  77. Serial.print(",");
  78. temp = myIMU.calcAccel(myIMU.fifoRead());
  79. Serial.print(temp);
  80. // The third dataset corresponds to external sensor data.
  81. // You need to read the third dataset first before you can read the timestamp data.
  82. // Therefore, you can ignore this dataset.
  83. uint16_t thirdData = myIMU.fifoTimestamp();
  84. // The third dataset
  85. // timestamp in fifo
  86. uint32_t fifoTimestamp = myIMU.fifoTimestamp();
  87. Serial.print(",");
  88. Serial.print("FIFO time: ");
  89. Serial.print(fifoTimestamp);
  90. Serial.print("\n");
  91. delay(10); //Wait for the serial buffer to clear (~50 bytes worth of time @ 57600baud)
  92. }
  93. tempUnsigned = myIMU.fifoGetStatus();
  94. Serial.print("\nFifo Status 1 and 2 (16 bits): 0x");
  95. Serial.println(tempUnsigned, HEX);
  96. Serial.print("\n");
  97. }