Hi All,
I've been playing around with 2 GPS receivers on my Mega 2560. I wanted to get 1 Hz NMEA data from both units using the UARTS (serial 1 and 2) and then display it. So, based on the examples in the Tiny GPS library, I have come up with this:
#include <TinyGPS.h>
/*
For Mega 2560
9600 baud serial GPS device hooked up on Serial 1.1Hz NMEA data.
9600 baud serial GPS device hooked up on Serial 2.1Hz NMEA data.
*/
TinyGPS gps;
void setup()
{
Serial.begin(115200); //set serial monitor Port Baud rate
Serial1.begin(9600); //set GPS Port One Baud rate.
Serial2.begin(9600); //set GPS Port Two Baud rate.
Serial.print("GPS Test: Two Port Data");
Serial.println();
}
void loop()
{
bool newDataOne = false;
bool newDataTwo = false;
unsigned long chars;
// For 1000ms we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (Serial1.available()) // Port One Data available
{
char c = Serial1.read();
if (gps.encode(c)) // Did a new valid sentence come in?
newDataOne = true;
}
}
if (newDataOne) // If Port One Data available
{
float flata, flona;
gps.f_get_position(&flata, &flona); // Get LAT, LON data
Serial.print(" GPS 1 LAT="); Serial.print(flata, 8);
Serial.print(" LON="); Serial.print(flona, 8);
Serial.print(" SAT="); Serial.print(gps.satellites());
Serial.print(" HDOP="); Serial.print(gps.hdop());
Serial.print(" ALT="); Serial.print(gps.f_altitude());
Serial.print(" COG="); Serial.print(gps.f_course());
Serial.print(" Speed kmph="); Serial.print(gps.f_speed_kmph());
Serial.println();
}
// For 1000ms we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (Serial2.available()) // Port Two Data available
{
char d = Serial2.read();
if (gps.encode(d)) // Did a new valid sentence come in?
newDataTwo = true;
}
}
if (newDataTwo) // If Port Two Data available
{
float flatb, flonb;
gps.f_get_position(&flatb, &flonb); // Get LAT, LON data
Serial.print(" GPS 2 LAT="); Serial.print(flatb, 8);
Serial.print(" LON="); Serial.print(flonb, 8);
Serial.print(" SAT="); Serial.print(gps.satellites());
Serial.print(" HDOP="); Serial.print(gps.hdop());
Serial.print(" ALT="); Serial.print(gps.f_altitude());
Serial.print(" COG="); Serial.print(gps.f_course());
Serial.print(" Speed kmph="); Serial.print(gps.f_speed_kmph());
Serial.println();
Serial.print(" LAT Diff="); Serial.print(flatb-51, 8); //Perform a calculation on the second set of LAT data
Serial.print(" LON Diff="); Serial.print(flonb-1, 8); //Perform a calculation on the second set of LON data
Serial.println();
}
}
So, whilst this works, its a bit clunky in the sense that it is synchronous and the loop has a 1 second timer on it. If I up the data rate from the GPS engine (UBlox - can go to 4Hz), how can I speed up the routine to handle the faster data rate?
Secondly, I wanted to perform a calculation between the first and second data, for example the distance between the LAT of data A and data B, but because the data is contained within their own loops, it doesn't work. In my example above I have performed data within the second loop (B) which does work just as a check. So, how can I perform the calculation?
Any help much appreciated ! Thanks.