Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Codice scrittura solo dati accelerometro
- #include <SPI.h>
- #include <SdFat.h>
- #include <Wire.h>
- #include <NMEAGPS.h>
- #define ARDUINO_USD_CS 10 // uSD card CS pin (pin 10 on SparkFun GPS Logger Shield)
- //Definizione delle librerie e oggetti necessarie all'acquisizione dei dati da GPS
- NMEAGPS gps; // NeoGPS object to be used throughout
- gps_fix fix; // The latest GPS information received from the gpsPort
- #define GPS_BAUD 9600 // GPS module's default baud rate
- #include <AltSoftSerial.h>
- AltSoftSerial gpsPort; // Always on pins 8 & 9
- #include <SoftwareSerial.h>
- #define SerialMonitor Serial
- #define ARDUINO_GPS_RX 9 // GPS TX, Arduino RX pin
- #define ARDUINO_GPS_TX 8 // GPS RX, Arduino TX pin
- #define MAX_LOG_FILES 100 // Number of log files that can be made
- char logFileName[13] = "gpslogXX.csv";
- #define LOG_COLUMN_HEADER \
- "longitude," "latitude," "altitude," "speed," "course," "date," "time," \
- "satellites," "LastLog," "Acc.X," "Acc.Y," "Acc.Z," "Gy.X," "Gy.Y," "Gy.Z,"
- #define LOG_RATE 1000 // Log every 1 seconds
- #define LOG_RATE 1000000
- unsigned long lastLog = 0; // Global var to keep of last time we logged
- unsigned long lastLogGPS = 0; // Global var to keep of last time we logged
- const int MPU=0x68; // I2C address of the MPU-6050
- int16_t AcX,AcY,AcZ,GyX,GyY,GyZ;
- // SD definition
- SdFat SD;
- File dataFile;
- void setup()
- {
- Wire.begin();
- Wire.beginTransmission(MPU);
- Wire.write(0x6B); // PWR_MGMT_1 register
- Wire.write(0); // set to zero (wakes up the MPU-6050)
- Wire.endTransmission(true);
- SerialMonitor.begin(9600);
- gpsPort.begin(GPS_BAUD);
- SerialMonitor.println( F("Setting up SD card.") );
- updateFileName(); // Each time we start, create a new file, increment the number
- // see if the card is present and can be initialized:
- if (!SD.begin(ARDUINO_USD_CS))
- {
- SerialMonitor.println( F("Error initializing SD card.") );
- } else {
- dataFile = SD.open( logFileName, FILE_WRITE );
- // Print a header at the top of the new file
- dataFile.println( F(LOG_COLUMN_HEADER) );
- }
- }
- void loop()
- {
- Wire.beginTransmission(MPU);
- Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
- Wire.endTransmission(false);
- Wire.requestFrom(MPU,14,true); // request a total of 14 registers
- AcX=Wire.read()<<8;
- AcX|= Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
- AcY=Wire.read()<<8;
- AcY|=Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
- AcZ=Wire.read()<<8;
- AcZ|=Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
- GyX=Wire.read()<<8;
- GyX|=Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
- GyY=Wire.read()<<8 ;
- GyY|=Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
- GyZ=Wire.read()<<8;
- GyZ|=Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
- if ((lastLogGPS + LOG_RATE_GPS) <= micros()) {
- while (gps.available( gpsPort )) {
- fix = gps.read(); // get the entire fix structure, once per second
- if (logGPSData()) { // Log the GPS data
- SerialMonitor.println( F("GPS logged.") ); // Print a debug message
- } else {// If we failed to log GPS
- // Print an error, don't update lastLog
- SerialMonitor.println( F("Failed to log new GPS data.") );
- }
- }
- }
- else{
- if ((lastLog + LOG_RATE) <= micros()) {
- printImuData(dataFile,lastLog, AcX, AcY, AcZ, GyX, GyY, GyZ);
- dataFile.flush();
- }
- }
- lastLog=micros();
- lastLogGPS=micros();
- }
- }
- static void printImuData(Print &printer, unsigned long lastLog,
- int16_t AcX, int16_t AcY, int16_t AcZ,
- int16_t GyX, int16_t GyY, int16_t GyZ)
- {//Printing the mpu6050 information
- printer.print("0"); printer.print(","); //0 longitude
- printer.print("0"); printer.print(","); //0 latitude
- printer.print("0"); printer.print(","); //0 altitude
- printer.print("0"); printer.print(","); //0 speed
- printer.print("0"); printer.print(","); //0 course
- printer.print("0"); printer.print(","); //0 date
- printer.print("0"); printer.print(","); //0 time
- printer.print("0"); printer.print(","); //0 satellites
- printer.print(lastLog); printer.print(",");
- printer.print(AcX); printer.print(",");
- printer.print(AcY); printer.print(",");
- printer.print(AcZ); printer.print(",");
- printer.print(GyX); printer.print(",");
- printer.print(GyY); printer.print(",");
- printer.println(GyZ);
- }
- byte logGPSData()
- {
- if (logFile.isOpen())
- { // Print longitude, latitude, altitude (in feet), speed (in mph), course
- // in (degrees), date, time, and number of satellites.
- if (fix.valid.location)
- logFile.print(fix.longitude(), 6);
- logFile.print(',');
- if (fix.valid.location)
- logFile.print(fix.latitude(), 6);
- logFile.print(',');
- if (fix.valid.altitude)
- logFile.print(fix.altitude() * 3.2808, 1);
- logFile.print(',');
- if (fix.valid.speed)
- logFile.print(fix.speed_mph(), 1);
- logFile.print(',');
- if (fix.valid.heading)
- logFile.print(fix.heading(), 1);
- logFile.print(',');
- if (fix.valid.date) {
- logFile.print( fix.dateTime.full_year() );
- if (fix.dateTime.month < 10)
- logFile.print( '0' );
- logFile.print( fix.dateTime.month );
- if (fix.dateTime.date < 10)
- logFile.print( '0' );
- logFile.print( fix.dateTime.date );
- }
- logFile.print(',');
- if (fix.valid.time) {
- if (fix.dateTime.hours < 10)
- logFile.print( '0' );
- logFile.print( fix.dateTime.hours );
- if (fix.dateTime.minutes < 10)
- logFile.print( '0' );
- logFile.print( fix.dateTime.minutes );
- if (fix.dateTime.seconds < 10)
- logFile.print( '0' );
- logFile.print( fix.dateTime.seconds );
- }
- logFile.print(',');
- if (fix.valid.satellites)
- logFile.print(fix.satellites);
- logFile.print(',');
- logFile.print(lastLogGPS);
- logFile.print(',');
- logFile.print(AcX);
- logFile.print(',');
- logFile.print(AcY);
- logFile.print(',');
- logFile.print(AcZ);
- logFile.print(',');
- logFile.print(GyX);
- logFile.print(',');
- logFile.print(GyY);
- logFile.print(',');
- logFile.print(GyZ);
- logFile.println();
- logFile.flush(); // make sure the file contains at least this much
- return 1; // Return success
- }
- return 0; // If we failed to open the file, return fail
- }
- // updateFileName() - Looks through the log files already present on a card,
- // and creates a new file with an incremented file index.
- void updateFileName()
- {
- for (uint8_t i; i < MAX_LOG_FILES; i++)
- {
- // Set logFileName to "gpslogXX.csv":
- logFileName[6] = (i/10) + '0';
- logFileName[7] = (i%10) + '0';
- if (!SD.exists(logFileName))
- break; // We found our index
- SerialMonitor.print(logFileName);
- SerialMonitor.println( F(" exists") );
- }
- SerialMonitor.print( F("File name: ") );
- SerialMonitor.println(logFileName);
- }
Advertisement
Add Comment
Please, Sign In to add comment