Go Down

Topic: GPS not letting me do anything else. (Read 804 times) previous topic - next topic

akaFATCAT

Apr 05, 2012, 11:13 am Last Edit: Apr 05, 2012, 11:15 am by akaFATCAT Reason: 1
This code works great. BUT. As soon as I add a simple delay,

Code: [Select]

Serial.println("\n\r Delay Here \n\r");
delay(1000);


it fails.

I have 6 sensors that I'm using, and all the data is being logged to an sd card. But the data was being garbled up. I did all I could think of to debug it, and i came down to the GPS unit. For whatever reason, I can't add anything to this base code that I'm using. Even a simple delay causes it to freeze up.

Google didn't help me, so I'm hoping you guys would.

Thanks.

Code: [Select]
/*
6-8-10
Aaron Weiss
SparkFun Electronics

Example GPS Parser based off of arduiniana.org TinyGPS examples.

Parses NMEA sentences from an EM406 running at 4800bps into readable
values for latitude, longitude, elevation, date, time, course, and
speed.

For the SparkFun GPS Shield. Make sure the switch is set to DLINE.

Once you get your longitude and latitude you can paste your
coordinates from the terminal window into Google Maps. Here is the
link for SparkFun's location.  
http://maps.google.com/maps?q=40.06477,+-105.20997

Uses the NewSoftSerial library for serial communication with your GPS,
so connect your GPS TX and RX pin to any digital pin on the Arduino,
just be sure to define which pins you are using on the Arduino to
communicate with the GPS module.
*/

// In order for this sketch to work, you will need to download
// NewSoftSerial and TinyGPS libraries from arduiniana.org and put them
// into the hardware->libraries folder in your ardiuno directory.
// Here are the lines of code that point to those libraries.
//
//
// 28/08/2010
// I could not get the NewSoftSerial Drivers to work
// on my Arduino Mega with the Sparkfun GPS shield.
// I desoldered the 2 jumpers, then jumpered the GPS's Rx to Pin 14, Tx to Pin 15
// This is UART3
// As PaulS pointed out, why use soft serial on a board with 4 UART's?
//
//
#include <TinyGPS.h>

//Set this value equal to the baud rate of your GPS
#define GPSBAUD 4800

// Create an instance of the TinyGPS object
TinyGPS gps;

// This is where you declare prototypes for the functions that will be
// using the TinyGPS library.
void getgps(TinyGPS &gps);

// In the setup function, you need to initialize two serial ports; the
// standard hardware serial port (Serial()) to communicate with your
// terminal program and another hardware serial port (Serial3()) for your
// GPS.
//
//

void setup()
{
// This is the serial rate for your terminal program. It must be this
// fast because we need to print everything before a new sentence
// comes in. If you slow it down, the messages might not be valid and
// you will likely get checksum errors.
Serial.begin(115200);
//Sets baud rate of your GPS
Serial3.begin(GPSBAUD);

Serial.println("");
Serial.println("GPS Shield QuickStart Example Sketch v12.u");
Serial.println("       ...waiting for lock...           ");
Serial.println("");
}

// This is the main loop of the code. All it does is check for data on
// the RX pin of the ardiuno, makes sure the data is valid NMEA sentences,
// then jumps to the getgps() function.
void loop()
{
while(Serial3.available())     // While there is data on the RX pin...
{
    int c = Serial3.read();    // load the data into a variable...
    if(gps.encode(c))      // if there is a new valid sentence...
    {
      getgps(gps);         // then grab the data.
    }
}
Serial.println("\n\r Delay Here \n\r");
delay(1000);
}

// The getgps function will get and print the values we want.
void getgps(TinyGPS &gps)
{
// To get all of the data into varialbes that you can use in your code,
// all you need to do is define variables and query the object for the
// data. To see the complete list of functions see keywords.txt file in
// the TinyGPS and NewSoftSerial libs.

// Define the variables that will be used
float latitude, longitude;
// Then call this function
gps.f_get_position(&latitude, &longitude);
// You can now print variables latitude and longitude
Serial.print("Lat/Long: ");
Serial.print(latitude,5);
Serial.print(", ");
Serial.println(longitude,5);

// Same goes for date and time
int year;
byte month, day, hour, minute, second, hundredths;
gps.crack_datetime(&year,&month,&day,&hour,&minute,&second,&hundredths);
// Print data and time
Serial.print("Date: "); Serial.print(month, DEC); Serial.print("/");
Serial.print(day, DEC); Serial.print("/"); Serial.print(year);
Serial.print("  Time: "); Serial.print(hour, DEC); Serial.print(":");
Serial.print(minute, DEC); Serial.print(":"); Serial.print(second, DEC);
Serial.print("."); Serial.println(hundredths, DEC);
//Since month, day, hour, minute, second, and hundr

// Here you can print the altitude and course values directly since
// there is only one value for the function
Serial.print("Altitude (meters): "); Serial.println(gps.f_altitude());  
// Same goes for course
Serial.print("Course (degrees): "); Serial.println(gps.f_course());
// And same goes for speed
Serial.print("Speed(kmph): "); Serial.println(gps.f_speed_kmph());
Serial.println();

// Here you can print statistics on the sentences.
unsigned long chars;
unsigned short sentences, failed_checksum;
gps.stats(&chars, &sentences, &failed_checksum);
//Serial.print("Failed Checksums: ");Serial.print(failed_checksum);
//Serial.println(); Serial.println();
}

dhunt

I'd suggest not using a delay. A 1 second delay is probably more than enough time for the gps to overflow the serial receive buffer.

Take a look at the "blink without delay" sketch for some ideas on working without delays.

MarkT

The delay will cause the serial buffer to fill up - not sure why it then hangs though, I'd expect a few lost GPS sentences.

When you say the data was being garbled, can you be more precise - was the GPS data itself garbled or was it being mixed up with other data?
[ I won't respond to messages, use the forum please ]

wildbill

That delay is causing major issues as noted. I suggest going back to your prior code that was causing a problem and post that - you've hidden your real issue. Most GPS devices transmit once a second, by putting in that delay, you guarantee that you'll overflow the serial buffer.

akaFATCAT

Spot on!

Yes that's exactly whats going on. The delay causes the buffer to fill up and when my statement is looking for the next $ to determine the start of the new line, nothing happens.

When I remove that if statement, the gps will rudely begin to write in the middle of the other readings.

So I have to somehow figure out how to pause the gps and start it up again.

According to the spec sheet for the Copernicus II by Trimble, if I pause using the XSTANDBY pin, the unit takes 3 seconds to pause and 3 seconds to start up again. That's not going to work, considering my write statement last only half a second.

My other option would be sending it a command to pause and a command to start, which also has a delay of approximately 1 second. So that's also not an option for me.

That leaves me with the only option left of writing some clever code to dump the buffer as it gets full and keep looking for the start of the next reading. But, being an absolute novice at this, it's not going well.

My question to you good people, how can I dump the buffer before I start the actual read? That way my code doesn't hang looking for "$".

Or am I completely off in my assessment? 

Thank you.

wildbill

What happens if you drop the delay and just read one of your sensors instead?

akaFATCAT

Here is the output without delay. As you can see, it's hit or miss. Never mind the occasional hiccup.

This is why I need to parse the data to clean up the output and I need to pause the GPS data to make sure I get a clean string.


Code: [Select]
$GPGGA,033339.00,3405.30080,N,11818.$GPGGA,033342.00,3405.30107                                                                                                                                                                                                                                                                            $GPGGA,033345.00,3405.30144BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 118.68
BMP085 Pressure (pa): -30918.0
SHT15 TEMP: 73.9
SHT15 HUMID: 44.97
TMP102 #1 (C): 22.93
TMP102 #1 (F): 73.28
,N,11818.86478,W,2,08,1.39,00102,M,-033,M,,*56
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 119.35
BMP085 Pressure (pa): -30925.0
SHT15 TEMP: 73.5
SHT15 HUMID: 44.97
TMP102 #1 (C): 22.93
TMP102 #1 (F): 73.28
347.6,M,000.00,N,000.00,K,D*20
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 119.19
BMP085 Pressure (pa): -30927.0
SHT15 TEMP: 73.7
SHT15 HUMID: 45.7
TMP102 #1 (C): 22.93
TMP102 #1 (F): 73.28
?4??r?bjb???r??brb???r??bZ?"R??jR?$GPGGA,033401.00,3405.30294,N000,KD2
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 118.93
BMP085 Pressure (pa): -30923.0
SHT15 TEMP: 73.2
SHT15 HUMID: 45.11
TMP102 #1 (C): 22.93
TMP102 #1 (F): 73.28
00.00,N,000.00,K,D*20
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 119.35
BMP085 Pressure (pa): -30922.0
SHT15 TEMP: 73.0
SHT15 HUMID: 45.11
TMP102 #1 (C): 22.93
TMP102 #1 (F): 73.28
?iTH?? ? ? i?S?)???iY?????? ? )brb???r??bZb"R??jR?$GPGGA,0334,,000,,D2
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 119.19
BMP085 Pressure (pa): -30917.0
SHT15 TEMP: 73.0
SHT15 HUMID: 45.4
TMP102 #1 (C): 22.93
TMP102 #1 (F): 73.28
0.0,T,347.6,M,000.00,N,000.00,K,D*20
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 119.35
BMP085 Pressure (pa): -30918.0
SHT15 TEMP: 72.98
SHT15 HUMID: 45.11
TMP102 #1 (C): 22.93
TMP102 #1 (F): 73.28
brb???r??bZb"R??jR?$GPGGA,033441.00,3405.30362,N,11818.86327,W,.0,,D2
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 119.44
BMP085 Pressure (pa): -30922.0
SHT15 TEMP: 72.96
SHT15 HUMID: 45.4
TMP102 #1 (C): 23.0
TMP102 #1 (F): 73.39
??r?b?b???r?bjb???r??brb???r??bZb"R??jR?$GPGGA,033448.00,3405.3M,0.0,,000,,D2
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 119.61
BMP085 Pressure (pa): -30920.0
SHT15 TEMP: 72.98
SHT15 HUMID: 45.11
TMP102 #1 (C): 23.0
TMP102 #1 (F): 73.39
??r??bZb"R??jR?$GPGGA,033459.00,3405.30356,N,11818.86324,W,2,080,,*0
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 119.10
BMP085 Pressure (pa): -30920.0
SHT15 TEMP: 72.94
SHT15 HUMID: 45.17
TMP102 #1 (C): 23.6
TMP102 #1 (F): 73.51
i??????? ? ?? ? i??JI SH°$GPGGA,033514.00,3405.30384,N,,KD2
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 118.85
BMP085 Pressure (pa): -30911.0
SHT15 TEMP: 72.91
SHT15 HUMID: 45.20
TMP102 #1 (C): 23.6
TMP102 #1 (F): 73.51
?r?b?b???r?bjb???r??brb???r??bZb"???jR?$GPGGA,033525.00,3405.30000,KD2
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 119.52
BMP085 Pressure (pa): -30919.0
SHT15 TEMP: 72.89
SHT15 HUMID: 45.17
TMP102 #1 (C): 23.0
TMP102 #1 (F): 73.39
eEu? ? ???i??????? ? ?? ? i??JI SH°$GPGGA,033536.00,376,,000,,000,KD2
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 119.61
BMP085 Pressure (pa): -30919.0
SHT15 TEMP: 72.85
SHT15 HUMID: 45.24
TMP102 #1 (C): 23.0
TMP102 #1 (F): 73.39
6,M,000.00,N,000.00,K,D*20
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 118.35
BMP085 Pressure (pa): -30915.0
SHT15 TEMP: 72.82
SHT15 HUMID: 45.24
TMP102 #1 (C): 23.6
TMP102 #1 (F): 73.51
M,000.00,N,000.00,K,D*20
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 119.35
BMP085 Pressure (pa): -30909.0
SHT15 TEMP: 72.80
SHT15 HUMID: 45.24
TMP102 #1 (C): 23.6
TMP102 #1 (F): 73.51

$GPGGA,033610.00,3405.30373,N,11818.80.0N,0.0,,*0
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 119.44
BMP085 Pressure (pa): -30920.0
SHT15 TEMP: 72.82
SHT15 HUMID: 45.27
TMP102 #1 (C): 0.6
TMP102 #1 (F): 31.88
$GPGGA,03361$GPGGA,033615.00,3405.30371,N,11818.86283,W,2,08,1.                                                                                                                                                                                                                                                             $GPGGA,033618.00,3405.30372BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 118.93
BMP085 Pressure (pa): -30913.0
SHT15 TEMP: 72.82
SHT15 HUMID: 45.27
TMP102 #1 (C): 23.6
TMP102 #1 (F): 73.51
,N,11818.86275,W,2,07,1.47,00108,M,-033,M,,*5B
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 118.68
BMP085 Pressure (pa): -30915.0
SHT15 TEMP: 72.85
SHT15 HUMID: 45.27
TMP102 #1 (C): 23.6
TMP102 #1 (F): 73.51

$GPVTG,000.T46,$GPGGA,033622.00,3405.30367,N,11818.86275,W,2,0                                                                                                                                                                                                                                                                                                $GPGGA,033625.00,3405.30367,BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 118.77
BMP085 Pressure (pa): -30914.0
SHT15 TEMP: 72.82
SHT15 HUMID: 45.24
TMP102 #1 (C): 23.0
TMP102 #1 (F): 73.39
N,11818.86252,W,2,06,1.49,00108,M,-033,M,,*5B
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 119.10
BMP085 Pressure (pa): -30913.0
SHT15 TEMP: 72.82
SHT15 HUMID: 45.20
TMP102 #1 (C): 23.6
TMP102 #1 (F): 73.51
? i?S?i???iI??)bjb???r??brb???r??bZb"R? j??$GPGGA,033634.00,34,D2
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 118.60
BMP085 Pressure (pa): -30912.0
SHT15 TEMP: 72.84
SHT15 HUMID: 45.17
TMP102 #1 (C): 23.6
TMP102 #1 (F): 73.51
00.14,N,000.26,K,D*28
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 119.10
BMP085 Pressure (pa): -30914.0
SHT15 TEMP: 72.80
SHT15 HUMID: 45.20
TMP102 #1 (C): 23.6
TMP102 #1 (F): 73.51
brb???r??bZb"R??jR?$GPGGA,033652.00,3405.30406,N,11818.86161,W,*2
BMP085 (C): 22.50
BMP085 (F): 72.50
BMP085 Altitude (m): 118.60
BMP085 Pressure (pa): -30914.0
SHT15 TEMP: 72.80
SHT15 HUMID: 45.27
TMP102 #1 (C): 23.6
TMP102 #1 (F): 73.51
0,T,347.6,M,000.00,N,000.00,K,D*20

dhunt

I think there is a problem in your sketch still, and that output doesn't seem to match the code you posted. Can you post your latest code?

cyclegadget


+1 on needing to see the code but, it looks like you are printing sensor values every "X" millis and then, printing the buffer reading for GPS even if you do not have a full GPS sentence. Therefore, you are getting pieces of the GPS sentence mixed with your sensor readings. At least that is what I see looking at the pattern in your serial output.

Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

Go Up