Guest User

Untitled

a guest
Jun 17th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. /Codice scrittura solo dati accelerometro
  2. //proviamo con i micros()
  3.  
  4. #include <SPI.h>
  5. #include <SdFat.h>
  6.  
  7. #include <Wire.h>
  8.  
  9. #define SerialMonitor Serial
  10.  
  11.  
  12. #define ARDUINO_USD_CS 10 // uSD card CS pin (pin 10 on SparkFun GPS Logger Shield)
  13.  
  14. #define MAX_LOG_FILES 100 // Number of log files that can be made
  15. char logFileName[13] = "gpslogXX.csv";
  16.  
  17.  
  18.  
  19. #define LOG_COLUMN_HEADER \
  20. "time," "Acc.X," "Acc.Y," "Acc.Z," "Gy.X," "Gy.Y," "Gy.Z"
  21.  
  22. #define LOG_RATE 1000000 // Log every n microseconds
  23. unsigned long lastLog = 0; // Global var to keep of last time we logged
  24.  
  25. const int MPU=0x68; // I2C address of the MPU-6050
  26. int16_t AcX,AcY,AcZ,GyX,GyY,GyZ;
  27.  
  28.  
  29. // SD definition
  30. SdFat SD;
  31. File dataFile;
  32.  
  33. void setup()
  34. {
  35. Wire.begin();
  36. Wire.beginTransmission(MPU);
  37. Wire.write(0x6B); // PWR_MGMT_1 register
  38. Wire.write(0); // set to zero (wakes up the MPU-6050)
  39. Wire.endTransmission(true);
  40. SerialMonitor.begin(9600);
  41.  
  42. updateFileName(); // Each time we start, create a new file, increment the number
  43. // see if the card is present and can be initialized:
  44. if (!SD.begin(ARDUINO_USD_CS))
  45. {
  46. SerialMonitor.println( F("Error initializing SD card.") );
  47. } else {
  48. dataFile = SD.open( logFileName, FILE_WRITE );
  49. // Print a header at the top of the new file
  50. dataFile.println( F(LOG_COLUMN_HEADER) );
  51. }
  52.  
  53. }
  54.  
  55. void loop()
  56. {
  57. Wire.beginTransmission(MPU);
  58. Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
  59. Wire.endTransmission(false);
  60. Wire.requestFrom(MPU,14,true); // request a total of 14 registers
  61. AcX=Wire.read()<<8;
  62. AcX|= Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  63. AcY=Wire.read()<<8;
  64. AcY|=Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  65. AcZ=Wire.read()<<8;
  66. AcZ|=Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  67. GyX=Wire.read()<<8;
  68. GyX|=Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  69. GyY=Wire.read()<<8 ;
  70. GyY|=Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  71. GyZ=Wire.read()<<8;
  72. GyZ|=Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  73. if ((lastLog + LOG_RATE) <= micros()) {
  74.  
  75. printImuData(dataFile,lastLog, AcX, AcY, AcZ, GyX, GyY, GyZ);
  76. dataFile.flush();
  77. lastLog=micros();
  78. }
  79. }
  80.  
  81. static void printImuData(Print &printer, unsigned long lastLog,
  82. int16_t AcX, int16_t AcY, int16_t AcZ,
  83. int16_t GyX, int16_t GyY, int16_t GyZ)
  84. {//Printing the mpu6050 information
  85. printer.print(lastLog); printer.print(",");
  86. printer.print(AcX); printer.print(",");
  87. printer.print(AcY); printer.print(",");
  88. printer.print(AcZ); printer.print(",");
  89. printer.print(GyX); printer.print(",");
  90. printer.print(GyY); printer.print(",");
  91. printer.println(GyZ);
  92.  
  93. }
  94.  
  95. // updateFileName() - Looks through the log files already present on a card,
  96. // and creates a new file with an incremented file index.
  97. void updateFileName()
  98. {
  99. for (uint8_t i; i < MAX_LOG_FILES; i++)
  100. {
  101. // Set logFileName to "gpslogXX.csv":
  102. logFileName[6] = (i/10) + '0';
  103. logFileName[7] = (i%10) + '0';
  104. if (!SD.exists(logFileName))
  105. break; // We found our index
  106.  
  107. SerialMonitor.print(logFileName);
  108. SerialMonitor.println( F(" exists") );
  109. }
  110. SerialMonitor.print( F("File name: ") );
  111. SerialMonitor.println(logFileName);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment