I would really appreciate any help with this. I have a project due in a couple of weeks and I just need to get in a position where I can log some useful data...
Individually the sketch I use to log GPS data succesfully stores the passed information on a .csv file on my SD card using SdFat. Likewise, the sketch I use to log data from the acc,gyr,mag also stores the information successfully. Combined, there appears to be no data logged whatsoever.
My code is shown below:
#include <Adafruit_GPS.h> //Load the GPS Library. Make sure you have installed the library form the adafruit site above
#include <SoftwareSerial.h> //Load the Software Serial Library. This library in effect gives the arduino additional serial ports
#include <Wire.h>
#include <Adafruit_LSM9DS1.h>
#include <SdFat.h>
#define LSM9DS1_SCK A5 // a/g/m
#define LSM9DS1_MISO 12 // a/g/m
#define LSM9DS1_MOSI A4 // a/g/m
#define LSM9DS1_XGCS 6 // a/g/m
#define LSM9DS1_MCS 5 // a/g/m
SoftwareSerial mySerial(3, 2); //Initialize SoftwareSerial, and tell it you will be connecting through pins 2 and 3
Adafruit_GPS GPS(&mySerial); //Create GPS object
Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1();
SdFat sd;
SdFile mySensorData;
int chipSelect = 4; //File gpsData; //Variable for SD cards Object
String NMEA1; //We will use this variable to hold our first NMEA sentence
String NMEA2; //We will use this variable to hold our second NMEA sentence
char c; //Used to read the characters spewing from the GPS module
void setup()
{
Serial.begin(115200);
GPS.begin(9600); //Turn GPS on at baud rate of 9600
pinMode(10,OUTPUT);
if (sd.begin(chipSelect, SPI_HALF_SPEED))
{
Serial.print(F("Ok now what"));
mySensorData.open("NMEA.CSV", O_WRITE);
mySensorData.remove();
mySensorData.open("GPSData.CSV", O_WRITE);
mySensorData.remove();
mySensorData.open("AGM.CSV", O_WRITE);
mySensorData.remove();
}
else {
Serial.print(F("Looks like an error"));
}
GPS.sendCommand("$PGCMD,33,0*6D"); // Turn Off GPS Antenna Update
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //Tell GPS we want only $GPRMC and $GPGGA NMEA sentences
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
delay(1000); //Pause
lsm.begin();
setupSensor();
}
void loop()
{
readGPS();
lsm.read(); /* ask it to read in the data */
sensors_event_t a, m, g, temp; /* Get a new sensor event */
lsm.getEvent(&a, &m, &g, &temp);
if (GPS.fix == 1){ //only log data after we have a fix
mySensorData.open("NMEA.CSV", O_WRITE | O_CREAT | O_APPEND);
//mySensorData = SD.open("NMEA.txt", FILE_WRITE); //Open file on SD card for writing
mySensorData.println(NMEA1); //Write first NMEA to SD card
mySensorData.println(NMEA2); //Write Second NMEA to SD card
mySensorData.close(); //Close the file
mySensorData.open("GPSData.CSV", O_WRITE | O_CREAT | O_APPEND);
//mySensorData = SD.open("GPSData.txt", FILE_WRITE);
mySensorData.print(GPS.latitude,4); //Write measured latitude to file
mySensorData.print(GPS.lat); //Which hemisphere N or S
mySensorData.print(F(","));
mySensorData.print(GPS.longitude,4); //Write measured longitude to file
mySensorData.print(GPS.lon); //Which Hemisphere E or W
mySensorData.print(F(","));
mySensorData.print(GPS.altitude);
mySensorData.print(F(","));
mySensorData.print(GPS.speed);
mySensorData.print(F(","));
mySensorData.println(GPS.angle);
mySensorData.close();
mySensorData.open("AGM.CSV", O_WRITE | O_CREAT | O_APPEND); //Open file on SD card for writing
mySensorData.print(a.acceleration.x);
mySensorData.print(F(","));
mySensorData.print(m.magnetic.x);
mySensorData.print(F(","));
mySensorData.println(g.gyro.x);
mySensorData.print(a.acceleration.y); //Which hemisphere N or S
mySensorData.print(F(","));
mySensorData.print(m.magnetic.y);
mySensorData.print(F(","));
mySensorData.println(g.gyro.y);
mySensorData.print(a.acceleration.z);
mySensorData.print(F(","));
mySensorData.print(m.magnetic.z);
mySensorData.print(F(","));
mySensorData.println(g.gyro.z);
mySensorData.close();
}
}
void readGPS(){ //This function will read and remember two NMEA sentences from GPS
clearGPS(); //Serial port probably has old or corrupt data, so begin by clearing it all out
while(!GPS.newNMEAreceived()) { //Keep reading characters in this loop until a good NMEA sentence is received
c=GPS.read(); //read a character from the GPS
}
GPS.parse(GPS.lastNMEA()); //Once you get a good NMEA, parse it
NMEA1=GPS.lastNMEA(); //Once parsed, save NMEA sentence into NMEA1
while(!GPS.newNMEAreceived()) { //Go out and get the second NMEA sentence, should be different type than the first one read above.
c=GPS.read();
}
GPS.parse(GPS.lastNMEA());
NMEA2=GPS.lastNMEA();
// Serial.println(NMEA1);
// Serial.println(NMEA2);
// Serial.println("");
}
void clearGPS() { //Since between GPS reads, we still have data streaming in, we need to clear the old data by reading a few sentences, and discarding these
while(!GPS.newNMEAreceived()) {
c=GPS.read();
}
GPS.parse(GPS.lastNMEA());
while(!GPS.newNMEAreceived()) {
c=GPS.read();
}
GPS.parse(GPS.lastNMEA());
}
void setupSensor()
{
// 1.) Set the accelerometer range
lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_2G);
//lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_4G);
//lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_8G);
//lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_16G);
// 2.) Set the magnetometer sensitivity
lsm.setupMag(lsm.LSM9DS1_MAGGAIN_4GAUSS);
//lsm.setupMag(lsm.LSM9DS1_MAGGAIN_8GAUSS);
//lsm.setupMag(lsm.LSM9DS1_MAGGAIN_12GAUSS);
//lsm.setupMag(lsm.LSM9DS1_MAGGAIN_16GAUSS);
// 3.) Setup the gyroscope
lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_245DPS);
//lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_500DPS);
//lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_2000DPS);
}
Upon compilation, it shows that I am occupying alot of memory space with these combined sketches.(86% PROGRAM STORAGE SPACE and 90% DYNAMIC MEMORY)
This is also troublesome as I would like to add my tft display to show information from the data on the display which I know would occupy a fair amount of memory in addition to this.
I was advised to use a char array instead of String for the NMEA sentences defined in my sketch. However, I am skeptical that this may be the root of this issue and I don't think this would significantly clear up memory space enough for me to add the tft sketch I have also written...
Despite my doubts about the char array being the saviour, I am also lost as to how I would go about incorporating it into my code.
Is someone able to identify why the data is not being logged?
Please..Any help would be appreciated.