Ultimate GPS with odometer data

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
ormapper
 
Posts: 12
Joined: Thu Mar 28, 2013 3:41 pm

Ultimate GPS with odometer data

Post by ormapper »

I'm trying to write output from my Ultimate GPS and an odometer to the console and can't quite get it working right. I need the GPS output to be at least 5hz and my odometer rate will be variable from 0-100hz.

In order to combine the outputs I moved the sensor read code into a Loop as per a post I read here in the Forum. This works swell as long as my odometer is working at a really low rate. If my odometer rate is fast then the GPS output on the console gets broken into multiple lines like this:

11
12
13
$PGTOP,11,2*6E
$GPRMC14
,195357.600,A,4440.5762,N,12315.5101,W,15
0.00,207.01,280313,,,D*7F
16
17
18

I'm pretty sure that it has to do with how the GPS data is getting read/written to Serial. But I don't have a clue as to what to do about it. Here's my loop code:

Code: Select all

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

   // read the value from the odometer sensor:
  sensorValueA0 = analogRead(sensorPinA0); 
  //Pullup resister SHOULD cause values to be closer to 1023 but there may be errors. Using 500 allows a wide error margin
  if (sensorValueA0 > 500){
      //Serial.println("ContactA0");
      currValue = 1000;
        }
  else {
      //Serial.println("********");
      currValue = 0;
  }
  //We're looking for the edge from LOW to HIGH
  if (currValue != lastValue){
    if (currValue == 1000){  
          Serial.println(pulseCounter);      
          pulseCounter++;  //Increment pulse count by one   
          if (pulseCounter > 24){ 
            pulseCounter = 0;
          }
    }
  }  
  lastValue = currValue;
  
  //Read data from GPS
   while (Serial1.available()) {                 //while data available from GPS
      Serial.print((char)Serial1.read());    //copy data to console      
  } 

}

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

Re: Ultimate GPS with odometer data

Post by adafruit_support_mike »

It looks like a buffering issue.

The 'Serial.available()' function tells you how many bytes are currently in the read buffer. If something delays the next byte, the code you listed will drop out of the read/write loop and start the loop() function all over again. You don't notice it when the odometer is slow because nothing intrudes between the end of one read/write loop and the beninning of the next. As the odometer gets faster, the chance of getting output during the half-steps increases.

I'd suggest you use a second buffer for the GPS data:

Code: Select all


char GPSString[128];
int idx = 0;
char c = 0;

while (Serial1.available()) {
    c = Serial1.read();                 //  get the next character
    
    if (("\n" == c) || ("\r" == c)) {
        GPSString[ idx ] = 0;           //  terminate the string
        Serial.println( GPSString );    //  print the string
        idx = 0;                        //  reset the buffer
    } else {
        GPSString[ idx++ ] = c;         //  append the character to the string
    }
}

ormapper
 
Posts: 12
Joined: Thu Mar 28, 2013 3:41 pm

Re: Ultimate GPS with odometer data

Post by ormapper »

Thanks for the reply. This seems to have done the trick!

I changed the double quotes

Code: Select all

("\n" == c) || ("\r" == c)
to single

Code: Select all

('\n' == c) || ('\r' == c)
otherwise I got a comparison between pointer and integer error.

Cheers!

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

Return to “Other Products from Adafruit”