Guest User

Untitled

a guest
Jun 17th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. //Codice scrittura solo dati accelerometro
  2.  
  3. #include <SPI.h>
  4. #include <SdFat.h>
  5. #include <Wire.h>
  6. #include <NMEAGPS.h>
  7.  
  8. #define ARDUINO_USD_CS 10 // uSD card CS pin (pin 10 on SparkFun GPS Logger Shield)
  9.  
  10.  
  11. //Definizione delle librerie e oggetti necessarie all'acquisizione dei dati da GPS
  12.  
  13.  
  14. NMEAGPS gps; // NeoGPS object to be used throughout
  15. gps_fix fix; // The latest GPS information received from the gpsPort
  16. #define GPS_BAUD 9600 // GPS module's default baud rate
  17.  
  18. #include <AltSoftSerial.h>
  19. AltSoftSerial gpsPort; // Always on pins 8 & 9
  20. #include <SoftwareSerial.h>
  21.  
  22. #define SerialMonitor Serial
  23.  
  24. #define ARDUINO_GPS_RX 9 // GPS TX, Arduino RX pin
  25. #define ARDUINO_GPS_TX 8 // GPS RX, Arduino TX pin
  26.  
  27.  
  28. #define MAX_LOG_FILES 100 // Number of log files that can be made
  29. char logFileName[13] = "gpslogXX.csv";
  30.  
  31.  
  32. #define LOG_COLUMN_HEADER \
  33. "longitude," "latitude," "altitude," "speed," "course," "date," "time," \
  34. "satellites," "LastLog," "Acc.X," "Acc.Y," "Acc.Z," "Gy.X," "Gy.Y," "Gy.Z,"
  35.  
  36. #define LOG_RATE 1000 // Log every 1 seconds
  37. #define LOG_RATE 1000000
  38. unsigned long lastLog = 0; // Global var to keep of last time we logged
  39. unsigned long lastLogGPS = 0; // Global var to keep of last time we logged
  40. const int MPU=0x68; // I2C address of the MPU-6050
  41. int16_t AcX,AcY,AcZ,GyX,GyY,GyZ;
  42.  
  43.  
  44. // SD definition
  45. SdFat SD;
  46. File dataFile;
  47.  
  48. void setup()
  49. {
  50. Wire.begin();
  51. Wire.beginTransmission(MPU);
  52. Wire.write(0x6B); // PWR_MGMT_1 register
  53. Wire.write(0); // set to zero (wakes up the MPU-6050)
  54. Wire.endTransmission(true);
  55. SerialMonitor.begin(9600);
  56. gpsPort.begin(GPS_BAUD);
  57.  
  58. SerialMonitor.println( F("Setting up SD card.") );
  59.  
  60. updateFileName(); // Each time we start, create a new file, increment the number
  61. // see if the card is present and can be initialized:
  62. if (!SD.begin(ARDUINO_USD_CS))
  63. {
  64. SerialMonitor.println( F("Error initializing SD card.") );
  65. } else {
  66. dataFile = SD.open( logFileName, FILE_WRITE );
  67. // Print a header at the top of the new file
  68. dataFile.println( F(LOG_COLUMN_HEADER) );
  69. }
  70.  
  71. }
  72.  
  73. void loop()
  74. {
  75. Wire.beginTransmission(MPU);
  76. Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
  77. Wire.endTransmission(false);
  78. Wire.requestFrom(MPU,14,true); // request a total of 14 registers
  79. AcX=Wire.read()<<8;
  80. AcX|= Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  81. AcY=Wire.read()<<8;
  82. AcY|=Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  83. AcZ=Wire.read()<<8;
  84. AcZ|=Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  85. GyX=Wire.read()<<8;
  86. GyX|=Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  87. GyY=Wire.read()<<8 ;
  88. GyY|=Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  89. GyZ=Wire.read()<<8;
  90. GyZ|=Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  91. if ((lastLogGPS + LOG_RATE_GPS) <= micros()) {
  92. while (gps.available( gpsPort )) {
  93. fix = gps.read(); // get the entire fix structure, once per second
  94.  
  95. if (logGPSData()) { // Log the GPS data
  96. SerialMonitor.println( F("GPS logged.") ); // Print a debug message
  97. } else {// If we failed to log GPS
  98. // Print an error, don't update lastLog
  99. SerialMonitor.println( F("Failed to log new GPS data.") );
  100. }
  101. }
  102. }
  103. else{
  104. if ((lastLog + LOG_RATE) <= micros()) {
  105.  
  106. printImuData(dataFile,lastLog, AcX, AcY, AcZ, GyX, GyY, GyZ);
  107. dataFile.flush();
  108. }
  109. }
  110. lastLog=micros();
  111. lastLogGPS=micros();
  112. }
  113. }
  114.  
  115. static void printImuData(Print &printer, unsigned long lastLog,
  116. int16_t AcX, int16_t AcY, int16_t AcZ,
  117. int16_t GyX, int16_t GyY, int16_t GyZ)
  118. {//Printing the mpu6050 information
  119.  
  120. printer.print("0"); printer.print(","); //0 longitude
  121. printer.print("0"); printer.print(","); //0 latitude
  122. printer.print("0"); printer.print(","); //0 altitude
  123. printer.print("0"); printer.print(","); //0 speed
  124. printer.print("0"); printer.print(","); //0 course
  125. printer.print("0"); printer.print(","); //0 date
  126. printer.print("0"); printer.print(","); //0 time
  127. printer.print("0"); printer.print(","); //0 satellites
  128. printer.print(lastLog); printer.print(",");
  129. printer.print(AcX); printer.print(",");
  130. printer.print(AcY); printer.print(",");
  131. printer.print(AcZ); printer.print(",");
  132. printer.print(GyX); printer.print(",");
  133. printer.print(GyY); printer.print(",");
  134. printer.println(GyZ);
  135.  
  136. }
  137.  
  138. byte logGPSData()
  139. {
  140. if (logFile.isOpen())
  141. { // Print longitude, latitude, altitude (in feet), speed (in mph), course
  142. // in (degrees), date, time, and number of satellites.
  143.  
  144. if (fix.valid.location)
  145. logFile.print(fix.longitude(), 6);
  146. logFile.print(',');
  147. if (fix.valid.location)
  148. logFile.print(fix.latitude(), 6);
  149. logFile.print(',');
  150. if (fix.valid.altitude)
  151. logFile.print(fix.altitude() * 3.2808, 1);
  152. logFile.print(',');
  153. if (fix.valid.speed)
  154. logFile.print(fix.speed_mph(), 1);
  155. logFile.print(',');
  156. if (fix.valid.heading)
  157. logFile.print(fix.heading(), 1);
  158. logFile.print(',');
  159.  
  160. if (fix.valid.date) {
  161. logFile.print( fix.dateTime.full_year() );
  162. if (fix.dateTime.month < 10)
  163. logFile.print( '0' );
  164. logFile.print( fix.dateTime.month );
  165. if (fix.dateTime.date < 10)
  166. logFile.print( '0' );
  167. logFile.print( fix.dateTime.date );
  168. }
  169. logFile.print(',');
  170.  
  171. if (fix.valid.time) {
  172. if (fix.dateTime.hours < 10)
  173. logFile.print( '0' );
  174. logFile.print( fix.dateTime.hours );
  175. if (fix.dateTime.minutes < 10)
  176. logFile.print( '0' );
  177. logFile.print( fix.dateTime.minutes );
  178. if (fix.dateTime.seconds < 10)
  179. logFile.print( '0' );
  180. logFile.print( fix.dateTime.seconds );
  181. }
  182. logFile.print(',');
  183.  
  184. if (fix.valid.satellites)
  185. logFile.print(fix.satellites);
  186. logFile.print(',');
  187. logFile.print(lastLogGPS);
  188. logFile.print(',');
  189. logFile.print(AcX);
  190. logFile.print(',');
  191. logFile.print(AcY);
  192. logFile.print(',');
  193. logFile.print(AcZ);
  194. logFile.print(',');
  195. logFile.print(GyX);
  196. logFile.print(',');
  197. logFile.print(GyY);
  198. logFile.print(',');
  199. logFile.print(GyZ);
  200.  
  201. logFile.println();
  202. logFile.flush(); // make sure the file contains at least this much
  203.  
  204. return 1; // Return success
  205. }
  206.  
  207. return 0; // If we failed to open the file, return fail
  208. }
  209.  
  210. // updateFileName() - Looks through the log files already present on a card,
  211. // and creates a new file with an incremented file index.
  212. void updateFileName()
  213. {
  214. for (uint8_t i; i < MAX_LOG_FILES; i++)
  215. {
  216. // Set logFileName to "gpslogXX.csv":
  217. logFileName[6] = (i/10) + '0';
  218. logFileName[7] = (i%10) + '0';
  219. if (!SD.exists(logFileName))
  220. break; // We found our index
  221.  
  222. SerialMonitor.print(logFileName);
  223. SerialMonitor.println( F(" exists") );
  224. }
  225. SerialMonitor.print( F("File name: ") );
  226. SerialMonitor.println(logFileName);
  227. }
Advertisement
Add Comment
Please, Sign In to add comment