Ultimate GPS shield can't run on more than 1hz with MEGA

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
thegunman
 
Posts: 54
Joined: Mon Jun 09, 2014 7:43 pm

Re: Ultimate GPS shield can't run on more than 1hz with MEGA

Post by thegunman »

it is a mess

so many times changed it so I guess I have to rewrite it

Code: Select all

// Test code for Adafruit GPS modules using MTK3329/MTK3339 driver
//
// This code shows how to listen to the GPS module in an interrupt
// which allows the program to have more 'freedom' - just parse
// when a new NMEA sentence is available! Then access data when
// desired.
//
// Tested and works great with the Adafruit Ultimate GPS module
// using MTK33x9 chipset
//    ------> http://www.adafruit.com/products/746
// Pick one up today at the Adafruit electronics shop
// and help support open source hardware & software! -ada

#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <SPI.h>
#include <math.h>
#include <Wire.h>
#include <LSM303.h>
#include <LiquidCrystal.h>
#include <floatToString.h>

#include <SdFat.h>
#include <SdFatUtil.h>


int zeroSixtyState = 0;
  boolean zeroSixtyDone = false;
  long timerBegin = 0;
  float time = 0.0;
  float maxspeed = 0.0;
    float maxspeedold = 0.0;

LiquidCrystal lcd(3, 2, 4);

uint8_t lcdLine = 0;
uint8_t clock[8] = {0x0, 0xe, 0x15, 0x17, 0x11, 0xe, 0x0};
uint8_t fix[8] = {0x0,0xe,0x11,0x15,0x11,0xe,0x0};
uint8_t nofix[8] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0};

long blink_previousMillis = 0;
long blink_interval = 500;
int blink_state = 0;


LSM303 compass;
// these are my values, they most likely will not work for you
// run the calibrate sketch from the LSM303 library to get your
// maximum and minimum values for x, y, z
int xRawMin = -16240;
int xRawMax = 16000;
int yRawMin = -17216;
int yRawMax = 17250;
int zRawMin = -14560;
int zRawMax = 16481;
//by increasing alphaAccel the response will become faster
//but the noise will increae [alpha must be between 0 and 1]
//values for digital lowpass
float alphaAccel = 0.4;
float alphaMagnet = 0.4;
unsigned int xOffset=0; 
unsigned int yOffset=0; 
unsigned int zOffset=0;                      
float Pitch=0;
float Roll=0;
float Yaw=0;
int xRaw=0; 
int yRaw=0; 
int zRaw=0;
float xFiltered=0; 
float yFiltered=0; 
float zFiltered=0;
float xFilteredOld=0; 
float yFilteredOld=0; 
float zFilteredOld=0;
float xAccel=0; 
float yAccel=0; 
float zAccel=0;


// If you're using a GPS module:
// Connect the GPS Power pin to 5V
// Connect the GPS Ground pin to ground
// If using software serial (sketch example default):
//   Connect the GPS TX (transmit) pin to Digital 3
//   Connect the GPS RX (receive) pin to Digital 2
// If using hardware serial (e.g. Arduino Mega):
//   Connect the GPS TX (transmit) pin to Arduino RX1, RX2 or RX3
//   Connect the GPS RX (receive) pin to matching TX1, TX2 or TX3

// If you're using the Adafruit GPS shield, change
// SoftwareSerial mySerial(3, 2); -> SoftwareSerial mySerial(8, 7);
// and make sure the switch is set to SoftSerial

// If using software serial, keep these lines enabled
// (you can change the pin numbers to match your wiring):
//SoftwareSerial mySerial(8, 7);

#define mySerial Serial1

Adafruit_GPS GPS(&mySerial);
// If using hardware serial (e.g. Arduino Mega), comment
// out the above six lines and enable this line instead:
//Adafruit_GPS GPS(&Serial1);


// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences.
#define GPSECHO  false
#define LOG_FIXONLY true 

// this keeps track of whether we're using the interrupt
// off by default!
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
// Set the pins used
#define chipSelect 10
#define ledPin 13

File logfile;

// read a Hex value and return the decimal equivalent
uint8_t parseHex(char c) {
  if (c < '0')
    return 0;
  if (c <= '9')
    return c - '0';
  if (c < 'A')
    return 0;
  if (c <= 'F')
    return (c - 'A')+10;
}

// blink out an error code
void error(uint8_t errno) {
  /*
  if (SD.errorCode()) {
   putstring("SD error: ");
   //    Serial.print(card.errorCode(), HEX);
   //    Serial.print(',');
   //    Serial.println(card.errorData(), HEX);
   }
   */

  /* Show on display only is logging
   while(1) {
   uint8_t i;
   for (i=0; i<errno; i++) {
   digitalWrite(ledPin, HIGH);
   delay(100);
   digitalWrite(ledPin, LOW);
   delay(100);
   }
   for (i=errno; i<10; i++) {
   delay(200);
   }
   }
   */
}

void setup(){
  {
    lcd.begin(20, 4);
    lcd.createChar(0, clock);
    lcd.createChar(1, fix);
    lcd.createChar(2, nofix);

  }


  {
    Serial.begin(115200);       //initialize serial port
    analogReference(EXTERNAL);  //use external reference voltage (3,3V)
    delay(2000);  //calibrate sensor after a short delay
    Wire.begin();
    compass.init();
    compass.enableDefault();
    // getAccelOffset();           //keep it flat and non moving on the table
    //there are other ways to calibrate the offset, each has some advantes
    //and disadvantes..
    //compare application note AN3447
    //http://www.freescale.com/files/sensors/doc/app_note/AN3447.pdf
  }


  {

    // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
    // also spit it out
    Serial.begin(115200);
    Serial.println("Adafruit GPS library basic test!");
    pinMode(ledPin, OUTPUT);

    // make sure that the default chip select pin is set to
    // output, even if you don't use it:
    pinMode(53, OUTPUT); 

    // see if the card is present and can be initialized:
    //if (!SD.begin(chipSelect, 11, 12, 13)) {
    if (!SD.begin(chipSelect)) {      // Use hardware SPI

      error(2);
    }
    char filename[15];
    strcpy(filename, "GPSLOG00.CSV");
    for (uint8_t i = 0; i < 100; i++) {
      filename[6] = '0' + i/10;
      filename[7] = '0' + i%10;
      // create if does not exist, do not open existing, write, sync after write
      if (! SD.exists(filename)) {
        break;
      }
    }

    logfile = SD.open(filename, FILE_WRITE);
    if( ! logfile ) {
      //    Serial.print("Couldnt create "); //    Serial.println(filename);
      error(3);
    }
    Serial.print("Writing to ");     
    Serial.println(filename);
   
    logfile.println("Time,Date,Latitude,Longitude, Elevation,Speed (MPH), Angle,Sat,G x,G y,G z,Total G's,Pitch,Roll,Heading,Compass");
    logfile.flush();
  }

  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(19200);
  //      GPS.sendCommand("$PMTK251,38400*27");  //set baud rate to 19200
  //    //  GPS.sendCommand("$PMTK251,9600*17");  //set baud rate to 9600
  //      mySerial.end();
  //      GPS.begin(38400);

  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
//   GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  // the parser doesn't care about other sentences at this time

  // Set the update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
  // For the parsing code to work nicely and have time to sort thru the data, and
  // print it out we don't suggest using anything higher than 1 Hz

  // Request updates on antenna status, comment out to keep quiet
  GPS.sendCommand(PGCMD_NOANTENNA);

  lcd.setCursor(0, 0);
  lcd.print("S");

  lcd.setCursor(13, 0);
  lcd.print("C");

  lcd.setCursor(0, 1);
  lcd.print("A");

  lcd.setCursor(13, 1);
  lcd.print("P");

  lcd.setCursor(0, 2);
  lcd.print("H");

  lcd.setCursor(13, 2);
  lcd.print("R");

  lcd.setCursor(0, 3);
  lcd.print("S");

  lcd.setCursor(13, 3);
  lcd.print("G");


  // the nice thing about this code is you can have a timer0 interrupt go off
  // every 1 millisecond, and read data from the GPS for you. that makes the
  // loop code a heck of a lot easier!
  useInterrupt(true);

 // delay(1000);
  // Ask for firmware version
 // GPS.sendCommand(PMTK_Q_RELEASE);
}


// Interrupt is called once a millisecond, looks for any new GPS data, and stores it
SIGNAL(TIMER0_COMPA_vect) {
  char c;

  while (mySerial.available())
  {
    c = GPS.read();
    // if you want to debug, this is a good time to do it!
    if (GPSECHO)
#ifdef UDR0
      if (c) UDR0 = c; 
    // writing direct to UDR0 is much much faster than Serial.print
    // but only one character can be written at a time.
#else
    if (c) Serial.write(c);
#endif
  }
}

void useInterrupt(boolean v) {
  if (v) {
    // Timer0 is already used for millis() - we'll just interrupt somewhere
    // in the middle and call the "Compare A" function above
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
    usingInterrupt = true;
  } 
  else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);
    usingInterrupt = false;
  }
}

uint32_t timer = millis();


void FilterAD()
{
  // read from AD and subtract the offset
  xRaw=compass.a.x-xOffset;
  yRaw=compass.a.y-yOffset;
  zRaw=compass.a.z-zOffset; 
  //Digital Low Pass - compare: (for accelerometer)
  //http://en.wikipedia.org/wiki/Low-pass_filter
  xFiltered= xFilteredOld + alphaAccel * (xRaw - xFilteredOld);
  yFiltered= yFilteredOld + alphaAccel * (yRaw - yFilteredOld);
  zFiltered= zFilteredOld + alphaAccel * (zRaw - zFilteredOld); 
  xFilteredOld = xFiltered;
  yFilteredOld = yFiltered;
  zFilteredOld = zFiltered;   
}


void AD2Degree()
{
  // 3.3 = Vref; 1023 = 10Bit AD; 0.8 = Sensivity Accelerometer
  // (compare datasheet of your accelerometer)
  // the *Accel must be between -1 and 1; you may have to
  // to add/subtract +1 depending on the orientation of the accelerometer
  // (like me on the zAccel)
  // they are not necessary, but are useful for debugging
  xAccel=xFiltered *3.3 / (1023.0*0.8);      
  yAccel=yFiltered *3.3 / (1023.0*0.8);      
  zAccel=zFiltered *3.3 / (1023.0*0.8)+1.0;

  // Calculate Pitch and Roll (compare Application Note AN3461 from Freescale
  // http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf
  // Microsoft Excel switches the values for atan2
  // -> this info can make your life easier :-D
  //angled are radian, for degree (* 180/3.14159)
  Roll   = atan2(  yAccel ,  sqrt(sq(xAccel)+sq(zAccel))); 
  Pitch  = atan2(  xAccel ,   sqrt(sq(yAccel)+sq(zAccel)));  
}

String degToCardinal(int header)
{

  if (header > 337.5)
    return "N";
  if (header > 292.5)
    return "NW";
  if (header > 247.5)
    return "W";
  if (header > 202.5)
    return "SW";
  if (header > 157.5)
    return "S";
  if (header > 112.5)
    return "SE";
  if (header > 67.5)
    return "E";
  if (header > 22.5)
    return "NE";
  if (header >= 0)
    return "N";
  return "?"; //If, somehow, we didn't find a valid heading (less than zero, perhaps)

}

void loop()   {                  // run over and over again

 int test= millis();
  
    compass.read();
    FilterAD();
    AD2Degree(); 
    long xRaw = compass.a.x;
    long yRaw = compass.a.y;
    long zRaw = compass.a.z;
    int mx = compass.m.x;
    int my = compass.m.y;

    // Convert raw values to 'milli-Gs"
    long xScaled = map(xRaw, xRawMin, xRawMax, -1000, 1000);
    long yScaled = map(yRaw, yRawMin, yRawMax, -1000, 1000);
    long zScaled = map(zRaw, zRawMin, zRawMax, -1000, 1000);

    // re-scale to fractional Gs
    float xAccel = xScaled / 1000.0;
    float yAccel = yScaled / 1000.0;
    float zAccel = zScaled / 1000.0;

    float Pi = 3.14159;
    float heading = (atan2(my,mx) * 180) / Pi; 
    // Normalize to 0-360
    if (heading < 0)
    {
      heading = 360 + heading;
    }
  
  


    float totalG = (sqrt(sq(xAccel)+sq(yAccel)+sq(zAccel)));

  
  


    {
      // in case you are not using the interrupt above, you'll
      // need to 'hand query' the GPS, not suggested :(
      if (! usingInterrupt) {
        // read data from the GPS in the 'main loop'
        while (mySerial.available())
        {
          char c = GPS.read();
          // if you want to debug, this is a good time to do it!
          if (GPSECHO)
            if (c) Serial.write(c);
        }
      } 

      // if a sentence is received, we can check the checksum, parse it...
      if (GPS.newNMEAreceived()) {
        // a tricky thing here is if we print the NMEA sentence, or data
        // we end up not listening and catching other sentences!
        // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
//        Serial.println(GPS.lastNMEA());   // this also sets the newNMEAreceived() flag to false

        if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
          return;  // we can fail to parse a sentence in which case we should just wait for another
          
              compass.read();
    FilterAD();
    AD2Degree(); 
    long xRaw = compass.a.x;
    long yRaw = compass.a.y;
    long zRaw = compass.a.z;
    int mx = compass.m.x;
    int my = compass.m.y;

    // Convert raw values to 'milli-Gs"
    long xScaled = map(xRaw, xRawMin, xRawMax, -1000, 1000);
    long yScaled = map(yRaw, yRawMin, yRawMax, -1000, 1000);
    long zScaled = map(zRaw, zRawMin, zRawMax, -1000, 1000);

    // re-scale to fractional Gs
    float xAccel = xScaled / 1000.0;
    float yAccel = yScaled / 1000.0;
    float zAccel = zScaled / 1000.0;

    float Pi = 3.14159;
    float heading = (atan2(my,mx) * 180) / Pi; 
    // Normalize to 0-360
    if (heading < 0)
    {
      heading = 360 + heading;
    }
  
  


    float totalG = (sqrt(sq(xAccel)+sq(yAccel)+sq(zAccel)));
          
      }
     
     
//       Serial.println(GPS.parse(GPS.lastNMEA())); //debug////////////
 if(zeroSixtyState == 0){//stop state
             //digitalWrite(3, HIGH);
             if((GPS.speed*1.15078) >= 1){
               timerBegin = millis();
               //delay(5000);
               zeroSixtyState = 2;
             }
         }
         else if(zeroSixtyState == 3){
           zeroSixtyState = 1;
         }
         
         else if(zeroSixtyState == 2){//wait for the speed to reach 60
            //digitalWrite(4, HIGH);
            if((GPS.speed*1.15078) >= 20){
              long timerEnd = millis();
              time = (timerEnd-timerBegin);
              zeroSixtyState = 3;
            }
         }

//      // if millis() or timer wraps around, we'll just reset it
//      if (timer > millis())  timer = millis();
//
//      // approximately every 2 seconds or so, print out the current stats
//      if (millis() - timer > 5) {
//        timer = millis(); // reset the timer
//      }  
        // Rad. lets log it!
        //    Serial.println("Log");





  


        logfile.print(GPS.hour, DEC);
        logfile.print(':');
        logfile.print(GPS.minute, DEC);
        logfile.print(':');
        logfile.print(GPS.seconds, DEC);
        logfile.print('.');
        logfile.print(GPS.milliseconds);
        logfile.print(",");

        logfile.print(GPS.month, DEC); 
        logfile.print('/');
        logfile.print(GPS.day, DEC);
        logfile.print("/20");
        logfile.print(GPS.year, DEC);
        logfile.print(",");

        logfile.print(GPS.latitude, 4);
        logfile.print(GPS.lat);
        logfile.print(",");
        logfile.print(GPS.longitude, 4);
        logfile.print(GPS.lon);
        logfile.print(",");
        logfile.print(GPS.altitude);
        logfile.print(",");
        logfile.print(GPS.speed*1.15078);
        logfile.print(",");
        logfile.print(GPS.angle, 0);
        logfile.print(",");
        logfile.print((int)GPS.satellites);
        logfile.print(",");
//        logfile.print(xAccel);
//        logfile.print("G");
//        logfile.print(",");
//        logfile.print(yAccel);
//        logfile.print("G");
//        logfile.print(",");
//        logfile.print(zAccel);
//        logfile.print("G");
//        logfile.print(",");
//        logfile.print(totalG);
//        logfile.print("G");
//        logfile.print(",");
//        logfile.print(int(Pitch*180/PI));
//        logfile.print(",");
//        logfile.print(int(Roll*180/PI));
//        logfile.print(",");
////        logfile.print((int)heading);
//        logfile.print(",");
        logfile.println( degToCardinal(GPS.angle) );
        logfile.flush();



//            Serial.print(GPS.hour, DEC);
//            Serial.print(':');
//            Serial.print(GPS.minute, DEC);
//            Serial.print(':');
//            Serial.print(GPS.seconds, DEC);
//            Serial.print('.');
//            Serial.print(GPS.milliseconds);
//            Serial.print(",");
//        
//            Serial.print(GPS.month, DEC); 
//            Serial.print('/');
//            Serial.print(GPS.day, DEC);
//            Serial.print("/20");
//            Serial.print(GPS.year, DEC);
//            Serial.print(",");
//        
//            Serial.print(GPS.latitude, 4);
//            Serial.print(GPS.lat);
//            Serial.print(",");
//            Serial.print(GPS.longitude, 4);
//            Serial.print(GPS.lon);
//            Serial.print(",");
//            Serial.print(GPS.altitude);
//            Serial.print(",");
//            Serial.print(GPS.speed*1.15078);
//            Serial.print(",");
//            Serial.print(GPS.angle, 0);
//            Serial.print(",");
//            Serial.print(GPS.satellites);
//            Serial.print(",");
//            Serial.print(xAccel);
//            Serial.print("G,");
//            Serial.print(yAccel);
//            Serial.print("G,");
//            Serial.print(zAccel);
//            Serial.print("G");
//            Serial.print(",");
//            Serial.print(totalG);
//            Serial.print("G");
//            Serial.print(",");
//        //      Serial.print("Pitch: ");
//          Serial.print(int(Pitch*180/PI));
//              Serial.print(",");
//        //  Serial.print(" Roll: ");
//          Serial.print(int(Roll*180/PI));
//              Serial.print(",");
//            Serial.print((int)heading); 
//            Serial.print(",");
//            Serial.println( degToCardinal(heading) );

      

               maxspeedold = maxspeed;
               
               int flag;
               
               if (maxspeedold < maxspeed) {
                 
                 flag = '1';
               }
                 else {
                   flag = '0';
                 }

         if (maxspeed < (GPS.speed*1.15078))
       {
         maxspeed = (GPS.speed*1.15078);
       }


//FLOAT TO STRING CONVERTION

//SPEED
       float spd = (GPS.speed*1.15078);
       char cspd[6];

          dtostrf(spd, 5, 1, cspd);

        String s;
        s = String(cspd);
        
//MAX SPEED     
      // float mspd = (maxspeed);
       char cmspd[6];

          dtostrf(maxspeed, 5, 1, cmspd);

        String ms;
        ms = String(cmspd);
       
// ALTITUDE       
       float alt = (GPS.altitude);
       char calt[6];

          dtostrf(alt, 5, 0, calt);

        String a;
        a = String(calt);
        
//   PITCH
        
       float pitch = ((Pitch*180/PI));
       char cpitch[6];

          dtostrf(pitch, 3, 0, cpitch);

        String p;
        p = String(cpitch);
        
//   HEADING
        
       float head = (GPS.angle);
       char chead[6];

          dtostrf(head, 5, 0, chead);

        String h;
        h = String(chead);
        
//   ROLL
        
       float roll = ((Roll*180/PI));
       char croll[6];

          dtostrf(roll, 3, 0, croll);

        String r;
        r = String(croll);
        
//   SAT #
        
       float sat = (GPS.satellites);
       char csat[6];

          dtostrf(sat, 5, 0, csat);

        String st;
        st = String(csat);

//   TOTAL G #
        
       float tg = (totalG);
       char ctg[6];

          dtostrf(tg, 4, 1, ctg);

        String tgs;
        tgs = String(ctg);       
        
        

//int c = (degToCardinal(heading))
String comp;
comp = String(degToCardinal(GPS.angle));

while (comp.length() < 2){
  
  comp=" "+comp;
}


        switch (lcdLine) {
        case 0:
          lcd.setCursor(1, 0);
          lcd.print(s);//Serial.println(GPS.speed*1.15078);
          
           if (flag = 1) {
             
              lcd.setCursor(7, 1);
              lcd.print(ms);


        
            }
//          else {
//            }
         lcd.setCursor(15, 0);
         lcd.print(comp);
         

          
//LOCK ICON
          
                if (LOG_FIXONLY && !GPS.fix) {
         unsigned long blink_currentMillis = millis();
        if(blink_currentMillis - blink_previousMillis > blink_interval){
          blink_previousMillis = blink_currentMillis;  
          if (blink_state == 0){
            blink_state = 1; 
          }
          else{
            blink_state = 0;
          }

}
           if (blink_state == 0){                                   
           lcd.setCursor(19, 0);
           lcd.write(2);
          }
          else{
        lcd.setCursor(19, 0);
        lcd.write(1);
          }
      }
      else {
        lcd.setCursor(19, 0);
        lcd.write(1);
      }
          break;

        case 1:
          lcd.setCursor(1, 1);
          lcd.print(a); //Serial.println(GPS.altitude);
          lcd.setCursor(14, 1);
          lcd.print(p);
          
          break;

        case 2:
          lcd.setCursor(1, 2);
          lcd.print(h); //Serial.println(GPS.angle, 0);
          lcd.setCursor(14, 2);
          lcd.print(r);
          
          if (zeroSixtyState == 3){
              lcd.setCursor(8, 2);
              lcd.print(time/1000);
          }
          
          break;

        case 3:
          lcd.setCursor(1, 3);
          lcd.print(st); //Serial.println((int)GPS.satellites);
          lcd.setCursor(15, 3);
          lcd.print(tgs);
          break;


        default:
          break;

        }
        lcdLine++;
        lcdLine &= 3;  //increment lcdLine, but keep in range 0..3
                         
                            Serial.println(millis()-test); 
      }

    
}

this is the log with accelerometer data not logged, gps 1hz

Code: Select all

Time,Date,Latitude,Longitude, Elevation,Speed (MPH), Angle,Sat,Compass
23:52:38.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.61,212,6,SW
23:52:38.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.61,212,6,SW
23:52:38.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.61,212,6,SW
23:52:38.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.61,212,6,SW
23:52:38.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.61,212,6,SW
23:52:38.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.61,212,6,SW
23:52:38.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.61,212,6,SW
23:52:38.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.61,212,6,SW
23:52:38.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.61,212,6,SW
23:52:38.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.61,212,6,SW
23:52:38.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.61,212,6,SW
23:52:38.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.61,212,6,SW
23:52:38.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.61,212,6,SW
23:52:39.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.61,212,7,SW
23:52:39.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.64,201,7,S
23:52:39.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.64,201,7,S
23:52:39.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.64,201,7,S
23:52:39.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.64,201,7,S
23:52:39.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.64,201,7,S
23:52:39.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.64,201,7,S
23:52:39.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.64,201,7,S
23:52:39.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.64,201,7,S
23:52:39.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.64,201,7,S
23:52:39.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.64,201,7,S
23:52:39.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.64,201,7,S
23:52:39.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.64,201,7,S
23:52:39.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.64,201,7,S
23:52:40.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.68,194,7,S
23:52:40.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.68,194,7,S
23:52:40.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.68,194,7,S
23:52:40.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.68,194,7,S
23:52:40.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.68,194,7,S
23:52:40.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.68,194,7,S
23:52:40.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.68,194,7,S
23:52:40.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.68,194,7,S
23:52:40.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.68,194,7,S
23:52:40.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.68,194,7,S
23:52:40.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.68,194,7,S
23:52:40.0,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.68,194,7,S
23:52:40.984,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.68,194,7,S
23:52:40.984,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.69,189,7,S
23:52:40.984,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.69,189,7,S
23:52:40.984,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.69,189,7,S
23:52:40.984,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.69,189,7,S
23:52:40.984,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.69,189,7,S
23:52:40.984,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.69,189,7,S
23:52:40.984,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.69,189,7,S
23:52:40.984,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.69,189,7,S
23:52:40.984,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.69,189,7,S
23:52:40.984,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.69,189,7,S
23:52:40.984,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.69,189,7,S
23:52:40.984,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.69,189,7,S
23:52:40.984,7/17/2014,3605.XXXXN,11518.XXXXW,811.80,0.69,189,7,S

thegunman
 
Posts: 54
Joined: Mon Jun 09, 2014 7:43 pm

Re: Ultimate GPS shield can't run on more than 1hz with MEGA

Post by thegunman »

UPDATE:

It looks that for some reason it decides to write line again and when this happens the time jumps because it writes multiple lines. It looks like it repeats few time from time to time the part with logfile.print's

I am begeiner ant totaly lost :(

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Ultimate GPS shield can't run on more than 1hz with MEGA

Post by adafruit_support_rick »

You had a misplaced curly bracket - the one that closes if (GPS.newNMEAreceived()). It was located right after float totalG = (sqrt(sq(xAccel)+sq(yAccel)+sq(zAccel)));. That meant that, even if you didn't get a new NMEA sentence, you would go on to log the old stale GPS data.

I moved the curly bracket to the end of loop().


Try this:

Code: Select all

// Test code for Adafruit GPS modules using MTK3329/MTK3339 driver
//
// This code shows how to listen to the GPS module in an interrupt
// which allows the program to have more 'freedom' - just parse
// when a new NMEA sentence is available! Then access data when
// desired.
//
// Tested and works great with the Adafruit Ultimate GPS module
// using MTK33x9 chipset
//    ------> http://www.adafruit.com/products/746
// Pick one up today at the Adafruit electronics shop
// and help support open source hardware & software! -ada

#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <SPI.h>
#include <math.h>
#include <Wire.h>
#include <LSM303.h>
#include <LiquidCrystal.h>
#include <floatToString.h>

#include <SdFat.h>
#include <SdFatUtil.h>


int zeroSixtyState = 0;
boolean zeroSixtyDone = false;
long timerBegin = 0;
float time = 0.0;
float maxspeed = 0.0;
float maxspeedold = 0.0;

LiquidCrystal lcd(3, 2, 4);

uint8_t lcdLine = 0;
uint8_t clock[8] = {
  0x0, 0xe, 0x15, 0x17, 0x11, 0xe, 0x0};
uint8_t fix[8] = {
  0x0,0xe,0x11,0x15,0x11,0xe,0x0};
uint8_t nofix[8] = {
  0x0,0x0,0x0,0x0,0x0,0x0,0x0};

long blink_previousMillis = 0;
long blink_interval = 500;
int blink_state = 0;


LSM303 compass;
// these are my values, they most likely will not work for you
// run the calibrate sketch from the LSM303 library to get your
// maximum and minimum values for x, y, z
int xRawMin = -16240;
int xRawMax = 16000;
int yRawMin = -17216;
int yRawMax = 17250;
int zRawMin = -14560;
int zRawMax = 16481;
//by increasing alphaAccel the response will become faster
//but the noise will increae [alpha must be between 0 and 1]
//values for digital lowpass
float alphaAccel = 0.4;
float alphaMagnet = 0.4;
unsigned int xOffset=0; 
unsigned int yOffset=0; 
unsigned int zOffset=0;                      
float Pitch=0;
float Roll=0;
float Yaw=0;
int xRaw=0; 
int yRaw=0; 
int zRaw=0;
float xFiltered=0; 
float yFiltered=0; 
float zFiltered=0;
float xFilteredOld=0; 
float yFilteredOld=0; 
float zFilteredOld=0;
float xAccel=0; 
float yAccel=0; 
float zAccel=0;


// If you're using a GPS module:
// Connect the GPS Power pin to 5V
// Connect the GPS Ground pin to ground
// If using software serial (sketch example default):
//   Connect the GPS TX (transmit) pin to Digital 3
//   Connect the GPS RX (receive) pin to Digital 2
// If using hardware serial (e.g. Arduino Mega):
//   Connect the GPS TX (transmit) pin to Arduino RX1, RX2 or RX3
//   Connect the GPS RX (receive) pin to matching TX1, TX2 or TX3

// If you're using the Adafruit GPS shield, change
// SoftwareSerial mySerial(3, 2); -> SoftwareSerial mySerial(8, 7);
// and make sure the switch is set to SoftSerial

// If using software serial, keep these lines enabled
// (you can change the pin numbers to match your wiring):
//SoftwareSerial mySerial(8, 7);

#define mySerial Serial1

Adafruit_GPS GPS(&mySerial);
// If using hardware serial (e.g. Arduino Mega), comment
// out the above six lines and enable this line instead:
//Adafruit_GPS GPS(&Serial1);


// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences.
#define GPSECHO  false
#define LOG_FIXONLY true 

// this keeps track of whether we're using the interrupt
// off by default!
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
// Set the pins used
#define chipSelect 10
#define ledPin 13

File logfile;

// read a Hex value and return the decimal equivalent
uint8_t parseHex(char c) {
  if (c < '0')
    return 0;
  if (c <= '9')
    return c - '0';
  if (c < 'A')
    return 0;
  if (c <= 'F')
    return (c - 'A')+10;
}

// blink out an error code
void error(uint8_t errno) {
  /*
  if (SD.errorCode()) {
   putstring("SD error: ");
   //    Serial.print(card.errorCode(), HEX);
   //    Serial.print(',');
   //    Serial.println(card.errorData(), HEX);
   }
   */

  /* Show on display only is logging
   while(1) {
   uint8_t i;
   for (i=0; i<errno; i++) {
   digitalWrite(ledPin, HIGH);
   delay(100);
   digitalWrite(ledPin, LOW);
   delay(100);
   }
   for (i=errno; i<10; i++) {
   delay(200);
   }
   }
   */
}

void setup(){
  {
    lcd.begin(20, 4);
    lcd.createChar(0, clock);
    lcd.createChar(1, fix);
    lcd.createChar(2, nofix);

  }


  {
    Serial.begin(115200);       //initialize serial port
    analogReference(EXTERNAL);  //use external reference voltage (3,3V)
    delay(2000);  //calibrate sensor after a short delay
    Wire.begin();
    compass.init();
    compass.enableDefault();
    // getAccelOffset();           //keep it flat and non moving on the table
    //there are other ways to calibrate the offset, each has some advantes
    //and disadvantes..
    //compare application note AN3447
    //http://www.freescale.com/files/sensors/doc/app_note/AN3447.pdf
  }


  {

    // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
    // also spit it out
    Serial.begin(115200);
    Serial.println("Adafruit GPS library basic test!");
    pinMode(ledPin, OUTPUT);

    // make sure that the default chip select pin is set to
    // output, even if you don't use it:
    pinMode(53, OUTPUT); 

    // see if the card is present and can be initialized:
    //if (!SD.begin(chipSelect, 11, 12, 13)) {
    if (!SD.begin(chipSelect)) {      // Use hardware SPI

      error(2);
    }
    char filename[15];
    strcpy(filename, "GPSLOG00.CSV");
    for (uint8_t i = 0; i < 100; i++) {
      filename[6] = '0' + i/10;
      filename[7] = '0' + i%10;
      // create if does not exist, do not open existing, write, sync after write
      if (! SD.exists(filename)) {
        break;
      }
    }

    logfile = SD.open(filename, FILE_WRITE);
    if( ! logfile ) {
      //    Serial.print("Couldnt create "); //    Serial.println(filename);
      error(3);
    }
    Serial.print("Writing to ");     
    Serial.println(filename);

    logfile.println("Time,Date,Latitude,Longitude, Elevation,Speed (MPH), Angle,Sat,G x,G y,G z,Total G's,Pitch,Roll,Heading,Compass");
    logfile.flush();
  }

  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(19200);
  //      GPS.sendCommand("$PMTK251,38400*27");  //set baud rate to 19200
  //    //  GPS.sendCommand("$PMTK251,9600*17");  //set baud rate to 9600
  //      mySerial.end();
  //      GPS.begin(38400);

  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
  //   GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  // the parser doesn't care about other sentences at this time

  // Set the update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
  // For the parsing code to work nicely and have time to sort thru the data, and
  // print it out we don't suggest using anything higher than 1 Hz

  // Request updates on antenna status, comment out to keep quiet
  GPS.sendCommand(PGCMD_NOANTENNA);

  lcd.setCursor(0, 0);
  lcd.print("S");

  lcd.setCursor(13, 0);
  lcd.print("C");

  lcd.setCursor(0, 1);
  lcd.print("A");

  lcd.setCursor(13, 1);
  lcd.print("P");

  lcd.setCursor(0, 2);
  lcd.print("H");

  lcd.setCursor(13, 2);
  lcd.print("R");

  lcd.setCursor(0, 3);
  lcd.print("S");

  lcd.setCursor(13, 3);
  lcd.print("G");


  // the nice thing about this code is you can have a timer0 interrupt go off
  // every 1 millisecond, and read data from the GPS for you. that makes the
  // loop code a heck of a lot easier!
  useInterrupt(true);

  // delay(1000);
  // Ask for firmware version
  // GPS.sendCommand(PMTK_Q_RELEASE);
}


// Interrupt is called once a millisecond, looks for any new GPS data, and stores it
SIGNAL(TIMER0_COMPA_vect) {
  char c;

  while (mySerial.available())
  {
    c = GPS.read();
    // if you want to debug, this is a good time to do it!
    if (GPSECHO)
#ifdef UDR0
      if (c) UDR0 = c; 
    // writing direct to UDR0 is much much faster than Serial.print
    // but only one character can be written at a time.
#else
    if (c) Serial.write(c);
#endif
  }
}

void useInterrupt(boolean v) {
  if (v) {
    // Timer0 is already used for millis() - we'll just interrupt somewhere
    // in the middle and call the "Compare A" function above
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
    usingInterrupt = true;
  } 
  else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);
    usingInterrupt = false;
  }
}

uint32_t timer = millis();


void FilterAD()
{
  // read from AD and subtract the offset
  xRaw=compass.a.x-xOffset;
  yRaw=compass.a.y-yOffset;
  zRaw=compass.a.z-zOffset; 
  //Digital Low Pass - compare: (for accelerometer)
  //http://en.wikipedia.org/wiki/Low-pass_filter
  xFiltered= xFilteredOld + alphaAccel * (xRaw - xFilteredOld);
  yFiltered= yFilteredOld + alphaAccel * (yRaw - yFilteredOld);
  zFiltered= zFilteredOld + alphaAccel * (zRaw - zFilteredOld); 
  xFilteredOld = xFiltered;
  yFilteredOld = yFiltered;
  zFilteredOld = zFiltered;   
}


void AD2Degree()
{
  // 3.3 = Vref; 1023 = 10Bit AD; 0.8 = Sensivity Accelerometer
  // (compare datasheet of your accelerometer)
  // the *Accel must be between -1 and 1; you may have to
  // to add/subtract +1 depending on the orientation of the accelerometer
  // (like me on the zAccel)
  // they are not necessary, but are useful for debugging
  xAccel=xFiltered *3.3 / (1023.0*0.8);      
  yAccel=yFiltered *3.3 / (1023.0*0.8);      
  zAccel=zFiltered *3.3 / (1023.0*0.8)+1.0;

  // Calculate Pitch and Roll (compare Application Note AN3461 from Freescale
  // http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf
  // Microsoft Excel switches the values for atan2
  // -> this info can make your life easier :-D
  //angled are radian, for degree (* 180/3.14159)
  Roll   = atan2(  yAccel ,  sqrt(sq(xAccel)+sq(zAccel))); 
  Pitch  = atan2(  xAccel ,   sqrt(sq(yAccel)+sq(zAccel)));  
}

String degToCardinal(int header)
{

  if (header > 337.5)
    return "N";
  if (header > 292.5)
    return "NW";
  if (header > 247.5)
    return "W";
  if (header > 202.5)
    return "SW";
  if (header > 157.5)
    return "S";
  if (header > 112.5)
    return "SE";
  if (header > 67.5)
    return "E";
  if (header > 22.5)
    return "NE";
  if (header >= 0)
    return "N";
  return "?"; //If, somehow, we didn't find a valid heading (less than zero, perhaps)

}

void loop()   {                  // run over and over again

  int test= millis();

  compass.read();
  FilterAD();
  AD2Degree(); 
  long xRaw = compass.a.x;
  long yRaw = compass.a.y;
  long zRaw = compass.a.z;
  int mx = compass.m.x;
  int my = compass.m.y;

  // Convert raw values to 'milli-Gs"
  long xScaled = map(xRaw, xRawMin, xRawMax, -1000, 1000);
  long yScaled = map(yRaw, yRawMin, yRawMax, -1000, 1000);
  long zScaled = map(zRaw, zRawMin, zRawMax, -1000, 1000);

  // re-scale to fractional Gs
  float xAccel = xScaled / 1000.0;
  float yAccel = yScaled / 1000.0;
  float zAccel = zScaled / 1000.0;

  float Pi = 3.14159;
  float heading = (atan2(my,mx) * 180) / Pi; 
  // Normalize to 0-360
  if (heading < 0)
  {
    heading = 360 + heading;
  }




  float totalG = (sqrt(sq(xAccel)+sq(yAccel)+sq(zAccel)));





  {
    // in case you are not using the interrupt above, you'll
    // need to 'hand query' the GPS, not suggested :(
    if (! usingInterrupt) {
      // read data from the GPS in the 'main loop'
      while (mySerial.available())
      {
        char c = GPS.read();
        // if you want to debug, this is a good time to do it!
        if (GPSECHO)
          if (c) Serial.write(c);
      }
    } 

    // if a sentence is received, we can check the checksum, parse it...
    if (GPS.newNMEAreceived()) {
      // a tricky thing here is if we print the NMEA sentence, or data
      // we end up not listening and catching other sentences!
      // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
      //        Serial.println(GPS.lastNMEA());   // this also sets the newNMEAreceived() flag to false

      if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
        return;  // we can fail to parse a sentence in which case we should just wait for another

      compass.read();
      FilterAD();
      AD2Degree(); 
      long xRaw = compass.a.x;
      long yRaw = compass.a.y;
      long zRaw = compass.a.z;
      int mx = compass.m.x;
      int my = compass.m.y;

      // Convert raw values to 'milli-Gs"
      long xScaled = map(xRaw, xRawMin, xRawMax, -1000, 1000);
      long yScaled = map(yRaw, yRawMin, yRawMax, -1000, 1000);
      long zScaled = map(zRaw, zRawMin, zRawMax, -1000, 1000);

      // re-scale to fractional Gs
      float xAccel = xScaled / 1000.0;
      float yAccel = yScaled / 1000.0;
      float zAccel = zScaled / 1000.0;

      float Pi = 3.14159;
      float heading = (atan2(my,mx) * 180) / Pi; 
      // Normalize to 0-360
      if (heading < 0)
      {
        heading = 360 + heading;
      }




      float totalG = (sqrt(sq(xAccel)+sq(yAccel)+sq(zAccel)));




      //       Serial.println(GPS.parse(GPS.lastNMEA())); //debug////////////
      if(zeroSixtyState == 0){//stop state
        //digitalWrite(3, HIGH);
        if((GPS.speed*1.15078) >= 1){
          timerBegin = millis();
          //delay(5000);
          zeroSixtyState = 2;
        }
      }
      else if(zeroSixtyState == 3){
        zeroSixtyState = 1;
      }

      else if(zeroSixtyState == 2){//wait for the speed to reach 60
        //digitalWrite(4, HIGH);
        if((GPS.speed*1.15078) >= 20){
          long timerEnd = millis();
          time = (timerEnd-timerBegin);
          zeroSixtyState = 3;
        }
      }

      //      // if millis() or timer wraps around, we'll just reset it
      //      if (timer > millis())  timer = millis();
      //
      //      // approximately every 2 seconds or so, print out the current stats
      //      if (millis() - timer > 5) {
      //        timer = millis(); // reset the timer
      //      }  
      // Rad. lets log it!
      //    Serial.println("Log");








      logfile.print(GPS.hour, DEC);
      logfile.print(':');
      logfile.print(GPS.minute, DEC);
      logfile.print(':');
      logfile.print(GPS.seconds, DEC);
      logfile.print('.');
      logfile.print(GPS.milliseconds);
      logfile.print(",");

      logfile.print(GPS.month, DEC); 
      logfile.print('/');
      logfile.print(GPS.day, DEC);
      logfile.print("/20");
      logfile.print(GPS.year, DEC);
      logfile.print(",");

      logfile.print(GPS.latitude, 4);
      logfile.print(GPS.lat);
      logfile.print(",");
      logfile.print(GPS.longitude, 4);
      logfile.print(GPS.lon);
      logfile.print(",");
      logfile.print(GPS.altitude);
      logfile.print(",");
      logfile.print(GPS.speed*1.15078);
      logfile.print(",");
      logfile.print(GPS.angle, 0);
      logfile.print(",");
      logfile.print((int)GPS.satellites);
      logfile.print(",");
      //        logfile.print(xAccel);
      //        logfile.print("G");
      //        logfile.print(",");
      //        logfile.print(yAccel);
      //        logfile.print("G");
      //        logfile.print(",");
      //        logfile.print(zAccel);
      //        logfile.print("G");
      //        logfile.print(",");
      //        logfile.print(totalG);
      //        logfile.print("G");
      //        logfile.print(",");
      //        logfile.print(int(Pitch*180/PI));
      //        logfile.print(",");
      //        logfile.print(int(Roll*180/PI));
      //        logfile.print(",");
      ////        logfile.print((int)heading);
      //        logfile.print(",");
      logfile.println( degToCardinal(GPS.angle) );
      logfile.flush();



      //            Serial.print(GPS.hour, DEC);
      //            Serial.print(':');
      //            Serial.print(GPS.minute, DEC);
      //            Serial.print(':');
      //            Serial.print(GPS.seconds, DEC);
      //            Serial.print('.');
      //            Serial.print(GPS.milliseconds);
      //            Serial.print(",");
      //        
      //            Serial.print(GPS.month, DEC); 
      //            Serial.print('/');
      //            Serial.print(GPS.day, DEC);
      //            Serial.print("/20");
      //            Serial.print(GPS.year, DEC);
      //            Serial.print(",");
      //        
      //            Serial.print(GPS.latitude, 4);
      //            Serial.print(GPS.lat);
      //            Serial.print(",");
      //            Serial.print(GPS.longitude, 4);
      //            Serial.print(GPS.lon);
      //            Serial.print(",");
      //            Serial.print(GPS.altitude);
      //            Serial.print(",");
      //            Serial.print(GPS.speed*1.15078);
      //            Serial.print(",");
      //            Serial.print(GPS.angle, 0);
      //            Serial.print(",");
      //            Serial.print(GPS.satellites);
      //            Serial.print(",");
      //            Serial.print(xAccel);
      //            Serial.print("G,");
      //            Serial.print(yAccel);
      //            Serial.print("G,");
      //            Serial.print(zAccel);
      //            Serial.print("G");
      //            Serial.print(",");
      //            Serial.print(totalG);
      //            Serial.print("G");
      //            Serial.print(",");
      //        //      Serial.print("Pitch: ");
      //          Serial.print(int(Pitch*180/PI));
      //              Serial.print(",");
      //        //  Serial.print(" Roll: ");
      //          Serial.print(int(Roll*180/PI));
      //              Serial.print(",");
      //            Serial.print((int)heading); 
      //            Serial.print(",");
      //            Serial.println( degToCardinal(heading) );



      maxspeedold = maxspeed;

      int flag;

      if (maxspeedold < maxspeed) {

        flag = '1';
      }
      else {
        flag = '0';
      }

      if (maxspeed < (GPS.speed*1.15078))
      {
        maxspeed = (GPS.speed*1.15078);
      }


      //FLOAT TO STRING CONVERTION

      //SPEED
      float spd = (GPS.speed*1.15078);
      char cspd[6];

      dtostrf(spd, 5, 1, cspd);

      String s;
      s = String(cspd);

      //MAX SPEED     
      // float mspd = (maxspeed);
      char cmspd[6];

      dtostrf(maxspeed, 5, 1, cmspd);

      String ms;
      ms = String(cmspd);

      // ALTITUDE       
      float alt = (GPS.altitude);
      char calt[6];

      dtostrf(alt, 5, 0, calt);

      String a;
      a = String(calt);

      //   PITCH

      float pitch = ((Pitch*180/PI));
      char cpitch[6];

      dtostrf(pitch, 3, 0, cpitch);

      String p;
      p = String(cpitch);

      //   HEADING

      float head = (GPS.angle);
      char chead[6];

      dtostrf(head, 5, 0, chead);

      String h;
      h = String(chead);

      //   ROLL

      float roll = ((Roll*180/PI));
      char croll[6];

      dtostrf(roll, 3, 0, croll);

      String r;
      r = String(croll);

      //   SAT #

      float sat = (GPS.satellites);
      char csat[6];

      dtostrf(sat, 5, 0, csat);

      String st;
      st = String(csat);

      //   TOTAL G #

      float tg = (totalG);
      char ctg[6];

      dtostrf(tg, 4, 1, ctg);

      String tgs;
      tgs = String(ctg);       



      //int c = (degToCardinal(heading))
      String comp;
      comp = String(degToCardinal(GPS.angle));

      while (comp.length() < 2){

        comp=" "+comp;
      }


      switch (lcdLine) {
      case 0:
        lcd.setCursor(1, 0);
        lcd.print(s);//Serial.println(GPS.speed*1.15078);

        if (flag = 1) {

          lcd.setCursor(7, 1);
          lcd.print(ms);



        }
        //          else {
        //            }
        lcd.setCursor(15, 0);
        lcd.print(comp);



        //LOCK ICON

        if (LOG_FIXONLY && !GPS.fix) {
          unsigned long blink_currentMillis = millis();
          if(blink_currentMillis - blink_previousMillis > blink_interval){
            blink_previousMillis = blink_currentMillis;  
            if (blink_state == 0){
              blink_state = 1; 
            }
            else{
              blink_state = 0;
            }

          }
          if (blink_state == 0){                                   
            lcd.setCursor(19, 0);
            lcd.write(2);
          }
          else{
            lcd.setCursor(19, 0);
            lcd.write(1);
          }
        }
        else {
          lcd.setCursor(19, 0);
          lcd.write(1);
        }
        break;

      case 1:
        lcd.setCursor(1, 1);
        lcd.print(a); //Serial.println(GPS.altitude);
        lcd.setCursor(14, 1);
        lcd.print(p);

        break;

      case 2:
        lcd.setCursor(1, 2);
        lcd.print(h); //Serial.println(GPS.angle, 0);
        lcd.setCursor(14, 2);
        lcd.print(r);

        if (zeroSixtyState == 3){
          lcd.setCursor(8, 2);
          lcd.print(time/1000);
        }

        break;

      case 3:
        lcd.setCursor(1, 3);
        lcd.print(st); //Serial.println((int)GPS.satellites);
        lcd.setCursor(15, 3);
        lcd.print(tgs);
        break;


      default:
        break;

      }
      lcdLine++;
      lcdLine &= 3;  //increment lcdLine, but keep in range 0..3

        Serial.println(millis()-test); 
    }

  }
}
By the way, there's an Auto Format command in the Tools menu. It will standardize your indenting, making things like this easier to find.

thegunman
 
Posts: 54
Joined: Mon Jun 09, 2014 7:43 pm

Re: Ultimate GPS shield can't run on more than 1hz with MEGA

Post by thegunman »

Tnak you.

The code you returned again duplicate the lines

it looks like when it is set to RMCGGA it outputs two lines, one for $GPRMC and the second for $GPGGA at the same time

this is the output using Serial.println(GPS.lastNMEA()); for debug and GPS at 1hz RMCGGA /the code you returned/

Code: Select all

$GPGGA,165226.000,3605.XXXX,N,11518.XXXX,W,1,10,0.93,818.3,M,-24.6,M,,*59

77
$GPRMC,165226.000,A,3605.XXXX,N,11518.XXXX,W,0.04,80.65,180714,,,A*4B

88
$GPGGA,165227.000,3605.XXXX,N,11518.XXXX,W,1,10,0.93,818.3,M,-24.6,M,,*58

80
$GPRMC,165227.000,A,3605.XXXX,N,11518.XXXX,W,0.03,80.65,180714,,,A*4D

101
$GPGGA,165228.000,3605.XXXX,N,11518.XXXX,W,1,10,0.93,818.3,M,-24.6,M,,*57

76
$GPRMC,165228.000,A,3605.XXXX,N,11518.XXXX,W,0.03,80.65,180714,,,A*42

75

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Ultimate GPS shield can't run on more than 1hz with MEGA

Post by adafruit_support_rick »

thegunman wrote:it looks like when it is set to RMCGGA it outputs two lines, one for $GPRMC and the second for $GPGGA at the same time
Yes, that's right. If you only want RMC, then uncomment the line:

Code: Select all

  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  // GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
     GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);

thegunman
 
Posts: 54
Joined: Mon Jun 09, 2014 7:43 pm

Re: Ultimate GPS shield can't run on more than 1hz with MEGA

Post by thegunman »

I want RMCGGA, I need the data for sats and altitude on a top of RMC

If I choose to have RMCGGA does it mean that it will log two lines, one for GGA and one for RMC?

thanks

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Ultimate GPS shield can't run on more than 1hz with MEGA

Post by adafruit_support_rick »

That's pretty much true.

You can try putting in a test for RMC, and only write to your logfile if you have an RMC sentence

Code: Select all

// Test code for Adafruit GPS modules using MTK3329/MTK3339 driver
//
// This code shows how to listen to the GPS module in an interrupt
// which allows the program to have more 'freedom' - just parse
// when a new NMEA sentence is available! Then access data when
// desired.
//
// Tested and works great with the Adafruit Ultimate GPS module
// using MTK33x9 chipset
//    ------> http://www.adafruit.com/products/746
// Pick one up today at the Adafruit electronics shop
// and help support open source hardware & software! -ada

#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <SPI.h>
#include <math.h>
#include <Wire.h>
#include <LSM303.h>
#include <LiquidCrystal.h>
#include <floatToString.h>

#include <SdFat.h>
#include <SdFatUtil.h>


int zeroSixtyState = 0;
boolean zeroSixtyDone = false;
long timerBegin = 0;
float time = 0.0;
float maxspeed = 0.0;
float maxspeedold = 0.0;

LiquidCrystal lcd(3, 2, 4);

uint8_t lcdLine = 0;
uint8_t clock[8] = {
  0x0, 0xe, 0x15, 0x17, 0x11, 0xe, 0x0};
uint8_t fix[8] = {
  0x0,0xe,0x11,0x15,0x11,0xe,0x0};
uint8_t nofix[8] = {
  0x0,0x0,0x0,0x0,0x0,0x0,0x0};

long blink_previousMillis = 0;
long blink_interval = 500;
int blink_state = 0;


LSM303 compass;
// these are my values, they most likely will not work for you
// run the calibrate sketch from the LSM303 library to get your
// maximum and minimum values for x, y, z
int xRawMin = -16240;
int xRawMax = 16000;
int yRawMin = -17216;
int yRawMax = 17250;
int zRawMin = -14560;
int zRawMax = 16481;
//by increasing alphaAccel the response will become faster
//but the noise will increae [alpha must be between 0 and 1]
//values for digital lowpass
float alphaAccel = 0.4;
float alphaMagnet = 0.4;
unsigned int xOffset=0; 
unsigned int yOffset=0; 
unsigned int zOffset=0;                      
float Pitch=0;
float Roll=0;
float Yaw=0;
int xRaw=0; 
int yRaw=0; 
int zRaw=0;
float xFiltered=0; 
float yFiltered=0; 
float zFiltered=0;
float xFilteredOld=0; 
float yFilteredOld=0; 
float zFilteredOld=0;
float xAccel=0; 
float yAccel=0; 
float zAccel=0;


// If you're using a GPS module:
// Connect the GPS Power pin to 5V
// Connect the GPS Ground pin to ground
// If using software serial (sketch example default):
//   Connect the GPS TX (transmit) pin to Digital 3
//   Connect the GPS RX (receive) pin to Digital 2
// If using hardware serial (e.g. Arduino Mega):
//   Connect the GPS TX (transmit) pin to Arduino RX1, RX2 or RX3
//   Connect the GPS RX (receive) pin to matching TX1, TX2 or TX3

// If you're using the Adafruit GPS shield, change
// SoftwareSerial mySerial(3, 2); -> SoftwareSerial mySerial(8, 7);
// and make sure the switch is set to SoftSerial

// If using software serial, keep these lines enabled
// (you can change the pin numbers to match your wiring):
//SoftwareSerial mySerial(8, 7);

#define mySerial Serial1

Adafruit_GPS GPS(&mySerial);
// If using hardware serial (e.g. Arduino Mega), comment
// out the above six lines and enable this line instead:
//Adafruit_GPS GPS(&Serial1);


// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences.
#define GPSECHO  false
#define LOG_FIXONLY true 

// this keeps track of whether we're using the interrupt
// off by default!
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
// Set the pins used
#define chipSelect 10
#define ledPin 13

File logfile;

// read a Hex value and return the decimal equivalent
uint8_t parseHex(char c) {
  if (c < '0')
    return 0;
  if (c <= '9')
    return c - '0';
  if (c < 'A')
    return 0;
  if (c <= 'F')
    return (c - 'A')+10;
}

// blink out an error code
void error(uint8_t errno) {
  /*
  if (SD.errorCode()) {
   putstring("SD error: ");
   //    Serial.print(card.errorCode(), HEX);
   //    Serial.print(',');
   //    Serial.println(card.errorData(), HEX);
   }
   */

  /* Show on display only is logging
   while(1) {
   uint8_t i;
   for (i=0; i<errno; i++) {
   digitalWrite(ledPin, HIGH);
   delay(100);
   digitalWrite(ledPin, LOW);
   delay(100);
   }
   for (i=errno; i<10; i++) {
   delay(200);
   }
   }
   */
}

void setup(){
  {
    lcd.begin(20, 4);
    lcd.createChar(0, clock);
    lcd.createChar(1, fix);
    lcd.createChar(2, nofix);

  }


  {
    Serial.begin(115200);       //initialize serial port
    analogReference(EXTERNAL);  //use external reference voltage (3,3V)
    delay(2000);  //calibrate sensor after a short delay
    Wire.begin();
    compass.init();
    compass.enableDefault();
    // getAccelOffset();           //keep it flat and non moving on the table
    //there are other ways to calibrate the offset, each has some advantes
    //and disadvantes..
    //compare application note AN3447
    //http://www.freescale.com/files/sensors/doc/app_note/AN3447.pdf
  }


  {

    // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
    // also spit it out
    Serial.begin(115200);
    Serial.println("Adafruit GPS library basic test!");
    pinMode(ledPin, OUTPUT);

    // make sure that the default chip select pin is set to
    // output, even if you don't use it:
    pinMode(53, OUTPUT); 

    // see if the card is present and can be initialized:
    //if (!SD.begin(chipSelect, 11, 12, 13)) {
    if (!SD.begin(chipSelect)) {      // Use hardware SPI

      error(2);
    }
    char filename[15];
    strcpy(filename, "GPSLOG00.CSV");
    for (uint8_t i = 0; i < 100; i++) {
      filename[6] = '0' + i/10;
      filename[7] = '0' + i%10;
      // create if does not exist, do not open existing, write, sync after write
      if (! SD.exists(filename)) {
        break;
      }
    }

    logfile = SD.open(filename, FILE_WRITE);
    if( ! logfile ) {
      //    Serial.print("Couldnt create "); //    Serial.println(filename);
      error(3);
    }
    Serial.print("Writing to ");     
    Serial.println(filename);

    logfile.println("Time,Date,Latitude,Longitude, Elevation,Speed (MPH), Angle,Sat,G x,G y,G z,Total G's,Pitch,Roll,Heading,Compass");
    logfile.flush();
  }

  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(19200);
  //      GPS.sendCommand("$PMTK251,38400*27");  //set baud rate to 19200
  //    //  GPS.sendCommand("$PMTK251,9600*17");  //set baud rate to 9600
  //      mySerial.end();
  //      GPS.begin(38400);

  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
  //   GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  // the parser doesn't care about other sentences at this time

  // Set the update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
  // For the parsing code to work nicely and have time to sort thru the data, and
  // print it out we don't suggest using anything higher than 1 Hz

  // Request updates on antenna status, comment out to keep quiet
  GPS.sendCommand(PGCMD_NOANTENNA);

  lcd.setCursor(0, 0);
  lcd.print("S");

  lcd.setCursor(13, 0);
  lcd.print("C");

  lcd.setCursor(0, 1);
  lcd.print("A");

  lcd.setCursor(13, 1);
  lcd.print("P");

  lcd.setCursor(0, 2);
  lcd.print("H");

  lcd.setCursor(13, 2);
  lcd.print("R");

  lcd.setCursor(0, 3);
  lcd.print("S");

  lcd.setCursor(13, 3);
  lcd.print("G");


  // the nice thing about this code is you can have a timer0 interrupt go off
  // every 1 millisecond, and read data from the GPS for you. that makes the
  // loop code a heck of a lot easier!
  useInterrupt(true);

  // delay(1000);
  // Ask for firmware version
  // GPS.sendCommand(PMTK_Q_RELEASE);
}


// Interrupt is called once a millisecond, looks for any new GPS data, and stores it
SIGNAL(TIMER0_COMPA_vect) {
  char c;

  while (mySerial.available())
  {
    c = GPS.read();
    // if you want to debug, this is a good time to do it!
    if (GPSECHO)
#ifdef UDR0
      if (c) UDR0 = c; 
    // writing direct to UDR0 is much much faster than Serial.print
    // but only one character can be written at a time.
#else
    if (c) Serial.write(c);
#endif
  }
}

void useInterrupt(boolean v) {
  if (v) {
    // Timer0 is already used for millis() - we'll just interrupt somewhere
    // in the middle and call the "Compare A" function above
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
    usingInterrupt = true;
  } 
  else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);
    usingInterrupt = false;
  }
}

uint32_t timer = millis();


void FilterAD()
{
  // read from AD and subtract the offset
  xRaw=compass.a.x-xOffset;
  yRaw=compass.a.y-yOffset;
  zRaw=compass.a.z-zOffset; 
  //Digital Low Pass - compare: (for accelerometer)
  //http://en.wikipedia.org/wiki/Low-pass_filter
  xFiltered= xFilteredOld + alphaAccel * (xRaw - xFilteredOld);
  yFiltered= yFilteredOld + alphaAccel * (yRaw - yFilteredOld);
  zFiltered= zFilteredOld + alphaAccel * (zRaw - zFilteredOld); 
  xFilteredOld = xFiltered;
  yFilteredOld = yFiltered;
  zFilteredOld = zFiltered;   
}


void AD2Degree()
{
  // 3.3 = Vref; 1023 = 10Bit AD; 0.8 = Sensivity Accelerometer
  // (compare datasheet of your accelerometer)
  // the *Accel must be between -1 and 1; you may have to
  // to add/subtract +1 depending on the orientation of the accelerometer
  // (like me on the zAccel)
  // they are not necessary, but are useful for debugging
  xAccel=xFiltered *3.3 / (1023.0*0.8);      
  yAccel=yFiltered *3.3 / (1023.0*0.8);      
  zAccel=zFiltered *3.3 / (1023.0*0.8)+1.0;

  // Calculate Pitch and Roll (compare Application Note AN3461 from Freescale
  // http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf
  // Microsoft Excel switches the values for atan2
  // -> this info can make your life easier :-D
  //angled are radian, for degree (* 180/3.14159)
  Roll   = atan2(  yAccel ,  sqrt(sq(xAccel)+sq(zAccel))); 
  Pitch  = atan2(  xAccel ,   sqrt(sq(yAccel)+sq(zAccel)));  
}

String degToCardinal(int header)
{

  if (header > 337.5)
    return "N";
  if (header > 292.5)
    return "NW";
  if (header > 247.5)
    return "W";
  if (header > 202.5)
    return "SW";
  if (header > 157.5)
    return "S";
  if (header > 112.5)
    return "SE";
  if (header > 67.5)
    return "E";
  if (header > 22.5)
    return "NE";
  if (header >= 0)
    return "N";
  return "?"; //If, somehow, we didn't find a valid heading (less than zero, perhaps)

}

void loop()   {                  // run over and over again

  int test= millis();

  compass.read();
  FilterAD();
  AD2Degree(); 
  long xRaw = compass.a.x;
  long yRaw = compass.a.y;
  long zRaw = compass.a.z;
  int mx = compass.m.x;
  int my = compass.m.y;

  // Convert raw values to 'milli-Gs"
  long xScaled = map(xRaw, xRawMin, xRawMax, -1000, 1000);
  long yScaled = map(yRaw, yRawMin, yRawMax, -1000, 1000);
  long zScaled = map(zRaw, zRawMin, zRawMax, -1000, 1000);

  // re-scale to fractional Gs
  float xAccel = xScaled / 1000.0;
  float yAccel = yScaled / 1000.0;
  float zAccel = zScaled / 1000.0;

  float Pi = 3.14159;
  float heading = (atan2(my,mx) * 180) / Pi; 
  // Normalize to 0-360
  if (heading < 0)
  {
    heading = 360 + heading;
  }




  float totalG = (sqrt(sq(xAccel)+sq(yAccel)+sq(zAccel)));





  {
    // in case you are not using the interrupt above, you'll
    // need to 'hand query' the GPS, not suggested :(
    if (! usingInterrupt) {
      // read data from the GPS in the 'main loop'
      while (mySerial.available())
      {
        char c = GPS.read();
        // if you want to debug, this is a good time to do it!
        if (GPSECHO)
          if (c) Serial.write(c);
      }
    } 

    // if a sentence is received, we can check the checksum, parse it...
    if (GPS.newNMEAreceived()) {
      // a tricky thing here is if we print the NMEA sentence, or data
      // we end up not listening and catching other sentences!
      // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
      //        Serial.println(GPS.lastNMEA());   // this also sets the newNMEAreceived() flag to false

      if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
        return;  // we can fail to parse a sentence in which case we should just wait for another

      if (strstr(GPS.lastNMEA(), "RMC"))      //if we just got an RMC string
      {
        compass.read();
        FilterAD();
        AD2Degree(); 
        long xRaw = compass.a.x;
        long yRaw = compass.a.y;
        long zRaw = compass.a.z;
        int mx = compass.m.x;
        int my = compass.m.y;

        // Convert raw values to 'milli-Gs"
        long xScaled = map(xRaw, xRawMin, xRawMax, -1000, 1000);
        long yScaled = map(yRaw, yRawMin, yRawMax, -1000, 1000);
        long zScaled = map(zRaw, zRawMin, zRawMax, -1000, 1000);

        // re-scale to fractional Gs
        float xAccel = xScaled / 1000.0;
        float yAccel = yScaled / 1000.0;
        float zAccel = zScaled / 1000.0;

        float Pi = 3.14159;
        float heading = (atan2(my,mx) * 180) / Pi; 
        // Normalize to 0-360
        if (heading < 0)
        {
          heading = 360 + heading;
        }




        float totalG = (sqrt(sq(xAccel)+sq(yAccel)+sq(zAccel)));




        //       Serial.println(GPS.parse(GPS.lastNMEA())); //debug////////////
        if(zeroSixtyState == 0){//stop state
          //digitalWrite(3, HIGH);
          if((GPS.speed*1.15078) >= 1){
            timerBegin = millis();
            //delay(5000);
            zeroSixtyState = 2;
          }
        }
        else if(zeroSixtyState == 3){
          zeroSixtyState = 1;
        }

        else if(zeroSixtyState == 2){//wait for the speed to reach 60
          //digitalWrite(4, HIGH);
          if((GPS.speed*1.15078) >= 20){
            long timerEnd = millis();
            time = (timerEnd-timerBegin);
            zeroSixtyState = 3;
          }
        }

        //      // if millis() or timer wraps around, we'll just reset it
        //      if (timer > millis())  timer = millis();
        //
        //      // approximately every 2 seconds or so, print out the current stats
        //      if (millis() - timer > 5) {
        //        timer = millis(); // reset the timer
        //      }  
        // Rad. lets log it!
        //    Serial.println("Log");








        logfile.print(GPS.hour, DEC);
        logfile.print(':');
        logfile.print(GPS.minute, DEC);
        logfile.print(':');
        logfile.print(GPS.seconds, DEC);
        logfile.print('.');
        logfile.print(GPS.milliseconds);
        logfile.print(",");

        logfile.print(GPS.month, DEC); 
        logfile.print('/');
        logfile.print(GPS.day, DEC);
        logfile.print("/20");
        logfile.print(GPS.year, DEC);
        logfile.print(",");

        logfile.print(GPS.latitude, 4);
        logfile.print(GPS.lat);
        logfile.print(",");
        logfile.print(GPS.longitude, 4);
        logfile.print(GPS.lon);
        logfile.print(",");
        logfile.print(GPS.altitude);
        logfile.print(",");
        logfile.print(GPS.speed*1.15078);
        logfile.print(",");
        logfile.print(GPS.angle, 0);
        logfile.print(",");
        logfile.print((int)GPS.satellites);
        logfile.print(",");
        //        logfile.print(xAccel);
        //        logfile.print("G");
        //        logfile.print(",");
        //        logfile.print(yAccel);
        //        logfile.print("G");
        //        logfile.print(",");
        //        logfile.print(zAccel);
        //        logfile.print("G");
        //        logfile.print(",");
        //        logfile.print(totalG);
        //        logfile.print("G");
        //        logfile.print(",");
        //        logfile.print(int(Pitch*180/PI));
        //        logfile.print(",");
        //        logfile.print(int(Roll*180/PI));
        //        logfile.print(",");
        ////        logfile.print((int)heading);
        //        logfile.print(",");
        logfile.println( degToCardinal(GPS.angle) );
        logfile.flush();



        //            Serial.print(GPS.hour, DEC);
        //            Serial.print(':');
        //            Serial.print(GPS.minute, DEC);
        //            Serial.print(':');
        //            Serial.print(GPS.seconds, DEC);
        //            Serial.print('.');
        //            Serial.print(GPS.milliseconds);
        //            Serial.print(",");
        //        
        //            Serial.print(GPS.month, DEC); 
        //            Serial.print('/');
        //            Serial.print(GPS.day, DEC);
        //            Serial.print("/20");
        //            Serial.print(GPS.year, DEC);
        //            Serial.print(",");
        //        
        //            Serial.print(GPS.latitude, 4);
        //            Serial.print(GPS.lat);
        //            Serial.print(",");
        //            Serial.print(GPS.longitude, 4);
        //            Serial.print(GPS.lon);
        //            Serial.print(",");
        //            Serial.print(GPS.altitude);
        //            Serial.print(",");
        //            Serial.print(GPS.speed*1.15078);
        //            Serial.print(",");
        //            Serial.print(GPS.angle, 0);
        //            Serial.print(",");
        //            Serial.print(GPS.satellites);
        //            Serial.print(",");
        //            Serial.print(xAccel);
        //            Serial.print("G,");
        //            Serial.print(yAccel);
        //            Serial.print("G,");
        //            Serial.print(zAccel);
        //            Serial.print("G");
        //            Serial.print(",");
        //            Serial.print(totalG);
        //            Serial.print("G");
        //            Serial.print(",");
        //        //      Serial.print("Pitch: ");
        //          Serial.print(int(Pitch*180/PI));
        //              Serial.print(",");
        //        //  Serial.print(" Roll: ");
        //          Serial.print(int(Roll*180/PI));
        //              Serial.print(",");
        //            Serial.print((int)heading); 
        //            Serial.print(",");
        //            Serial.println( degToCardinal(heading) );



        maxspeedold = maxspeed;

        int flag;

        if (maxspeedold < maxspeed) {

          flag = '1';
        }
        else {
          flag = '0';
        }

        if (maxspeed < (GPS.speed*1.15078))
        {
          maxspeed = (GPS.speed*1.15078);
        }


        //FLOAT TO STRING CONVERTION

        //SPEED
        float spd = (GPS.speed*1.15078);
        char cspd[6];

        dtostrf(spd, 5, 1, cspd);

        String s;
        s = String(cspd);

        //MAX SPEED     
        // float mspd = (maxspeed);
        char cmspd[6];

        dtostrf(maxspeed, 5, 1, cmspd);

        String ms;
        ms = String(cmspd);

        // ALTITUDE       
        float alt = (GPS.altitude);
        char calt[6];

        dtostrf(alt, 5, 0, calt);

        String a;
        a = String(calt);

        //   PITCH

        float pitch = ((Pitch*180/PI));
        char cpitch[6];

        dtostrf(pitch, 3, 0, cpitch);

        String p;
        p = String(cpitch);

        //   HEADING

        float head = (GPS.angle);
        char chead[6];

        dtostrf(head, 5, 0, chead);

        String h;
        h = String(chead);

        //   ROLL

        float roll = ((Roll*180/PI));
        char croll[6];

        dtostrf(roll, 3, 0, croll);

        String r;
        r = String(croll);

        //   SAT #

        float sat = (GPS.satellites);
        char csat[6];

        dtostrf(sat, 5, 0, csat);

        String st;
        st = String(csat);

        //   TOTAL G #

        float tg = (totalG);
        char ctg[6];

        dtostrf(tg, 4, 1, ctg);

        String tgs;
        tgs = String(ctg);       



        //int c = (degToCardinal(heading))
        String comp;
        comp = String(degToCardinal(GPS.angle));

        while (comp.length() < 2){

          comp=" "+comp;
        }


        switch (lcdLine) {
        case 0:
          lcd.setCursor(1, 0);
          lcd.print(s);//Serial.println(GPS.speed*1.15078);

          if (flag = 1) {

            lcd.setCursor(7, 1);
            lcd.print(ms);



          }
          //          else {
          //            }
          lcd.setCursor(15, 0);
          lcd.print(comp);



          //LOCK ICON

          if (LOG_FIXONLY && !GPS.fix) {
            unsigned long blink_currentMillis = millis();
            if(blink_currentMillis - blink_previousMillis > blink_interval){
              blink_previousMillis = blink_currentMillis;  
              if (blink_state == 0){
                blink_state = 1; 
              }
              else{
                blink_state = 0;
              }

            }
            if (blink_state == 0){                                   
              lcd.setCursor(19, 0);
              lcd.write(2);
            }
            else{
              lcd.setCursor(19, 0);
              lcd.write(1);
            }
          }
          else {
            lcd.setCursor(19, 0);
            lcd.write(1);
          }
          break;

        case 1:
          lcd.setCursor(1, 1);
          lcd.print(a); //Serial.println(GPS.altitude);
          lcd.setCursor(14, 1);
          lcd.print(p);

          break;

        case 2:
          lcd.setCursor(1, 2);
          lcd.print(h); //Serial.println(GPS.angle, 0);
          lcd.setCursor(14, 2);
          lcd.print(r);

          if (zeroSixtyState == 3){
            lcd.setCursor(8, 2);
            lcd.print(time/1000);
          }

          break;

        case 3:
          lcd.setCursor(1, 3);
          lcd.print(st); //Serial.println((int)GPS.satellites);
          lcd.setCursor(15, 3);
          lcd.print(tgs);
          break;


        default:
          break;

        }
        lcdLine++;
        lcdLine &= 3;  //increment lcdLine, but keep in range 0..3

          Serial.println(millis()-test); 
      }
    }
  }
}

thegunman
 
Posts: 54
Joined: Mon Jun 09, 2014 7:43 pm

Re: Ultimate GPS shield can't run on more than 1hz with MEGA

Post by thegunman »

Hi Rick,

is works, it filters the RMC sentence and no dublicated lines but it still has some ~150 ms for writing while the normal times are ~30 and when this happens it drop a line in th logfile

what can be the reason for that spikes in the CPU time?

Does it matter how big the SD card is?

Code: Select all

23:20:33.703,
29
23:20:33.781,
26
23:20:33.906,
157
23:20:34.0,
35
23:20:34.203,
25
23:20:34.296,
29
23:20:34.406,
26
23:20:34.500,
28
23:20:34.578,
33
23:20:34.703,
36
23:20:34.796,
25
23:20:34.906,
29
23:20:35.0,
26

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Ultimate GPS shield can't run on more than 1hz with MEGA

Post by adafruit_support_mike »

thegunman wrote:Does it matter how big the SD card is?
You'll need one that fits in the connector. ;-)

Seriously, the upper limit is 32GB. Plain-vanilla SD cards use the FAT16 filesystem, which is limited to 4GB. SDHC cards use FAT32, which has a theoretical upper limit of 16TB, but the SDHC standard only supports sizes up to 32GB. Cards larger than that use the SDXC standard and the exFAT filesystem.

thegunman
 
Posts: 54
Joined: Mon Jun 09, 2014 7:43 pm

Re: Ultimate GPS shield can't run on more than 1hz with MEGA

Post by thegunman »

I was thinking if bigger card will make it slow since the one I use is 2gb

what can cause that spikes in CPU time??

I am lost and can't see why it is acting like this

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Ultimate GPS shield can't run on more than 1hz with MEGA

Post by adafruit_support_rick »

Size of the card shouldn't matter.

Try commenting out the logFile.flush()

thegunman
 
Posts: 54
Joined: Mon Jun 09, 2014 7:43 pm

Re: Ultimate GPS shield can't run on more than 1hz with MEGA

Post by thegunman »

This solves the problem timewise but does not write the log data

EDIT: I made it flush once every second and the problem is gone but have no idea how correctly is this

Code: Select all

if (test-testold > 1000) {
  logfile.flush();
  testold = test;
}

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Ultimate GPS shield can't run on more than 1hz with MEGA

Post by adafruit_support_mike »

That's fine. If there's no data in the buffer, flush() will return without doing anything.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Ultimate GPS shield can't run on more than 1hz with MEGA

Post by adafruit_support_rick »

thegunman wrote:This solves the problem timewise but does not write the log data
It will write the log data automatically when the internal buffer is full. When you're running at 10Hz, the buffer will fill up at least once every second.

thegunman
 
Posts: 54
Joined: Mon Jun 09, 2014 7:43 pm

Re: Ultimate GPS shield can't run on more than 1hz with MEGA

Post by thegunman »

this is to confirm that with no logfile.flush() no data i recorded to the SD card. I left it for over 2 minutes

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”