Use of GT-U7 GPS

I am using an Arduino Mega 2560 with GT-U7 to build a photo observation station - for rocket launches.. The launch site is fixed and known but the observation station may change with conditions. I need just location fix, not a continuous track of a moving station. I found an example code in these Fora and I want to be certain that I am applying them correctly. The names and commenting have been changed to aid my development. There are two basic questions:
1: The use of if (Serial1.available() > 0) vs. while (Serial1.available() > 0)
I assume that the "if" case applies to my fixed station and will return
1 result but the "while: case applies to a tracker and will return
repeated results as the tracker moves.

2: The use of if (gps.location.isValid()) vs. if(gps.location.isUpdated())
I assume that the "isValid" case applies to my fixed station and the
"isUpdated" case applies to a tracker.

Thanks, the two versions of the code around these options are posted below:

Amplifying my question, I simply want one location from the GT-U7. The code examples do not seem to work when placed in void setup(). The code does work when placed in void loop(). Since the operation of my project occurs 1 time, I want to have all of the code ( calculating positions, moving servos, etc, in void setup() with void loop() present but empty. In addition,, if I try to add any code around the GPS code, again, it does not work. I suspect this has something to do with the Serial1 port as the GPS unit functions.



// This code, for a fixed station displays information
// when a new sentence is correctly encoded.
// https://forum.arduino.cc/t/if-gps-encode-gpsserial-read/1023492/4


void some_function()
{ // begin some function
if (Serial1.available() > 0)
{ // begin there is a character available from the GPS at a fixed station
if (gps.encode(Serial1.read()))
{ // begin that available character completed a message
Serial.println(F(" display fixed station info "));
Z21_displayInfo();
} // end that available character completed a message

if (millis() > 5000 && gps.charsProcessed() < 10)
{  // begin nothing received
  Serial.println(F("No GPS detected"));
  while (true)
    ;
}  // end   nothing received

} // end there is a character available from the GPS at a fixed station

} // end some_function()

void Z21_displayInfo()
{ // begin Z21 display fixed info 1 time

Serial.println(F(" display fixed info here "));
if (gps.location.isValid())
{ // begin the completed message is valid ( fixed location ) / updated the location ( moving tracker )

double FixedLat = gps.location.lat();
double FixedLon = gps.location.lng();
double FixedAlt = gps.altitude.meters() / 1000;

Serial.println(F(" "));
Serial.print(F(" Fixed Lat       is ")), Serial.println(FixedLat);
Serial.print(F(" Fixed Lon       is ")), Serial.println(FixedLon);
Serial.print(F(" Fixed Alt in km is ")), Serial.println(FixedAlt);
Serial.println(F(" "));

} // end the completed message is valid ( fixed location )
else { Serial.print(F(" FIXED LOCATION INVALID")); }

Serial.println();
} // end Z21 display fixed info 1 time

// This code, for a moving tracker displays information
// when a new sentence is correctly encoded.
// https://forum.arduino.cc/t/if-gps-encode-gpsserial-read/1023492/4

void some_function()
{  // begin some function
  while (Serial1.available() > 0)
  {  // begin there is a character available from the GPS from a tracker
    if (gps.encode(Serial1.read()))
    {  // begin that available character completed a message
      Serial.println(F(" display tracker info "));
      Z21_displayInfo();
    }  // end   that available character completed a message

    if (millis() > 5000 && gps.charsProcessed() < 10)
    {  // begin nothing received
      Serial.println(F("No GPS detected"));
      while (true)
        ;
    }  // end   nothing received
  }    // end   there is a character available from the GPS from a tracker
}  // end   some_function()

void Z21_displayInfo()
{  // begin Z21 display tracker info repeatedly
  
  Serial.println(F(" display tracker info here "));
  if (gps.location.isUpdated())
  {  // begin the completed message is valid ( fixed location ) / updated the location ( moving tracker )

    double TrackLat = gps.location.lat();
    double TrackLon = gps.location.lng();
    double TrackAlt = gps.altitude.meters() / 1000

    Serial.println(F(" "));
    Serial.print(F(" Tracker Lat       is ")), Serial.println(TrackLat);
    Serial.print(F(" Tracker Lon       is ")), Serial.println(TrackLon);
    Serial.print(F(" Tracker Alt in km is ")), Serial.println(TrackAlt);
    Serial.println(F(" "));

  }  // end   the completed message updated the location ( moving tracker ) 
  else { Serial.print(F(" TRACKER LOCATION NOT UPDATED ")); }

  Serial.println();
}  // end   Z21 display tracker info repeatedly

Hi @rocketman75 ,

Welcome to the forum..

"if" happens once if the expression evaluates true.
"while" loops while true.
Serial.available() return how many bytes waiting to be read..
might not have all 10 yet..
looks like some function was written to be looped..
5 seconds after board boot it will lock..
in your setup try something like..

while (true){
some_function();
}

looks like it needs 10 bytes to get one location lock..
it will get one location and loop with a while (true)..

not sure about your gps questions..
need to see the lib source..

good luck.. ~q

need to alter this a bit..

 if (millis() > 5000 )
    {  
      while (true)
        ;
    }  

then it will only try to get one location..
spoke to soon on prior post..
might need more than 5 seconds..
~q

Well I think you might be using the TinyGPS++ library, and if you are your assertion about isValid() and isUpdated() is not correct at all.

Further comment is not possible, since the code you posted is not complete and impossible (for me) to follow.

See here for info on posting code so that it can be read by forum users;

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966

some_function is just a placeholder for the larger sketch that is served by the GPS unit ( with the TinyGPSPlus library ). The questions about "isValid" and is Updated are no longer relevant. The most helpful parts of you reply deal with timing. Thanks. I thouht maybe I needed to close the Serial1 port but I think your suggestions are more direct.

To give you more about my project. I have built a mechanism to carry a small "Go-Pro" camera and possibly an image recognition camera (Pixy2). My sketch shall be able to determine the location, pointing, and leveling of the mechanism using appropriate sensors. The sketch will be given knowledge of the Rocket's trajectory ( available by studying other missions ). The sketch shall calculate pointing servo angles, with corrections for tilt and frame centering from the Pixy2. The mechanism will not follow the rocket continuously, but will point to discrete waypoints in the sky during from launch to stage separation and then, back to landing ( before the rocket disappears behind hills. If it all works, the rocket will fly into the view of the camera at each waypoint.

These launches are Falcon 9 Rockets from Vandenberg Space Force Base.

ok, I see it now, looks like it's from the DeviceExample..
there it is called repeatedly in the loop..
the "if" gives the gps at least 5 seconds to start sending data..

and am I correct in thinking you only want to call this function inside of your setup to get a location 1 time??
if yes, maybe something like this..

bool some_function()
{ // begin some function
  bool timedOut = false;
  unsigned long startTime = millis();
  unsigned long intervalTimeout = 5000;//timeout after 5 seconds..
  while (!timedOut) {
    timedOut = (millis() - startTime) >= intervalTimeout;
    while (Serial1.available() > 0)
    { // begin there is a character available from the GPS from a tracker
      if (gps.encode(Serial1.read()))
      { // begin that available character completed a message
        Serial.println(F(" display tracker info "));
        Z21_displayInfo();
        return true;
      }  // end   that available character completed a message
    }    // end   there is a character available from the GPS from a tracker
  }
  Serial.println(F("No GPS detected"));
  return false;
}  // end   some_function()

sounds like an interesting project..

~q

There is a discussion of the issues there can be reading a GPS with TinyGPSplus here;

https://stuartsprojects.github.io/2024/09/21/How-not-to-read-a-GPS.html

Also example codes for reading GPSs in a reliable way.

1 Like

After reading the nice article posted by @srnet , thanks..
One small change is needed I think..

bool some_function()
{ // begin some function
  bool timedOut = false;
  unsigned long startTime = millis();
  unsigned long intervalTimeout = 5000;//timeout after 5 seconds..
  while (!timedOut) {
    timedOut = (millis() - startTime) >= intervalTimeout;
    while (Serial1.available() > 0)
    { // begin there is a character available from the GPS from a tracker
      if (gps.encode(Serial1.read()))
      { // begin that available character completed a message
       if (gps.speed.isUpdated() && gps.satellites.isUpdated()) //ensures that GGA and RMC sentences have been received
           {      
             Serial.println(F(" display tracker info "));
             Z21_displayInfo();
             return true;
           }
      }  // end   that available character completed a message
    }    // end   there is a character available from the GPS from a tracker
  }
  Serial.println(F("No GPS detected"));
  return false;
}  // end   some_function()

good luck.. ~q

1 Like

Thanks for the additional discusssion and the Stuarts reference. I suspected there was a timing issue.

Again, since my goal is to get 1 fix, and I will have some time to get it, the robustness of these approaches increases.

One thing about the display info function, I assume its not a part of the TinyGPS library. I just write my own to print lat, lon, and alt for diagostics.

Also, now I see the difference between isValid and isUpdated. For a fixed station, I could use either.
thanks again

Andy Gonzales

1 Like

I decided to go all in and use Robertson's Robust hardware Serial approach. I dropped his code into a stub of my project and it worked for awhile until I had a loose wire. Once I tightened the wire, it worked again. I do not have a "check wire" branch. The code prints several NMEA sentences along the way.

thanks again