U-blox NEO-6M GPS timing issue

My recently acquired GPS unit worked fine… initially, but after a while, it started failing.
I’ve confirmed it is NOT cabling etc… the issue is more fundamental.

I was using the basic test sketch as shown in the getting-started guide. Read once per second, print the results. Basic UART test - checked. GPS test..hmmm…

The symptoms observed… for around 2 minutes - it all works perfectly. Then - things start to go bad. The output shows loss of fix… then goes into an odd state where every alternate read is bad. This is repeatable. It is not intermittent - it’s a systemic problem. Wait a little longer… and it gets back ‘in sync’.

My tests have confirmed the issue is about timing… and I suspect, UART buffer size. If I slow down the read rate to 1300 ms, not 1000 … it continues to work. Apparently, given enough time…AND enough found satellites… it seems the data returned eventually can’t be transmitted within the allocated window. Slowing the rate down resolves the issue. OK…so…

My app needs to get GPS data every second (at least). My question to the forum is: Has anyone hit this issue.. and has anyone overcome it via a better gps parser? What I have in mind is the following solution:.. Start an asyncio stream reader, which only returns after a complete block of NMEA data has been received, thus avoiding the possibility that the UART buffer and time window end up in conflict.

Increasing baud rate would help… but is more like a band-aid than a robust algorithm.

Maybe there are better solutions?

Cheers, T.

Hey @Trevor277988, welcome to the forum. Glad to have you here.

For anyone else reading, Trevor is using the NEO-6M module. On that unit, alternating good and bad reads can be caused by UART buffering. Your timing tests fit that possibility, though they don’t confirm buffer overflow on their own.

Increasing the receive buffer could help absorb bursts, but it won’t solve a reader that consistently falls behind. Likewise, a 1300 ms interval may only change where reads fall relative to the output rather than resolve the underlying problem.

Could you share the host board, the example code you’re using, and a few raw NMEA lines around a failed read? I’d check whether the receiver reports “no fix” or the parser is rejecting incomplete data before choosing a replacement parser.

@Jane, I’m still investigating this, but yes I can see incomplete NMEA blocks when fix is lost. I’ve tried a few variations on timing, but I suspect I’ve found the root cause, but… I don’t have a complete NMEA reference at hand, these are just my initial observations and analysis:

What I can see is GPSReader._parse_gps_data ONLY seems to understand $GPRMC, $GPGGA and $GPGSA. Other sentences ($GPGSV, $GPVTG, $GPGGA…) are not parsed at all. Which may be why _process_buffer ends up trying to decode a buffer that starts mid-way through a sentence, as is shown in my debug trace.

I’m not an NMEA guru - never played with this before, but I don’t think gps_parser’s logic adequately deals with the buffer properly. Even ignoring the missing sentence-parsing routines (which might be for a good reason), I don’t believe the basic buffer-processing logic handles syncing with a sliding window (of varying length, depending on how many satellites are involved), I believe things are inevitably going to get out of sync unless some more sophisticated processing is involved. Just appending new data to the buffer is NOT enough! Something needs to recognise an end-of-block/start-of-new-block… I just don’t see this in the existing code. At present, the processing tries to begin in the middle of a sentence…which might be in the middle of a block.

My next test will be to modify the driver’s code to AT LEAST skip to the beginning of the next sentence if it is asked to _process_buffer at some random point mid-sentence.

Well, that’s two-cents worth… hope it helps

PS… Yes I modified the GPSReader code to allow for different timeouts…which I found did not solve the fundamental issue.. and I believe that makes sense in view of the above comments.
PPS: Running on a Pico 2W mounted on a PiicoDev LiPo platform,

My test script:

from machine import Pin, UART
import time
from PiicoDev_Unified import sleep_ms
import gps_parser

uart = UART(0, baudrate=9600, tx=Pin(16), rx=Pin(17))
print('UART 0...Pin 16/17')
sms = int(input("Enter sleep_ms: ")) or 1000
to  = int(input("Enter timeout ms: ")) or 600
gps = gps_parser.GPSReader(uart, timeout=to)
i=0
while True:
    gps_data = gps.get_data()
    print(f'{i:3} {gps_data.has_fix:2}  {gps_data.satellites:2}  {gps_data.latitude:<10.6f}  {gps_data.longitude:<10.5f}')
    if not gps_data.has_fix:
        print(gps.message_buffer)
    i += 1
    sleep_ms(sms)

log6NMEA.zip (2.8 KB)

I use assembler on a PIC processor to decode GPS messages. So posting the code would not be useful. The logic deals with each character as it is received in an interrupt routine. The data is stored in two 80 character buffers (receiving one and processing the other).

The ISR logic has a variable which determines what happens to each received character.
0 - look for a $ (start of message). If received, variable = 1, clear the checksum.
1 - check for $ (invalid, discard the buffer, variable = 0). Check for asterisk - end of data, variable - 2. If neither, it is data so store in the current buffer (I check not more than 79 characters in buffer first)
2 - the asterisk is followed by the checksum as 2 hexadecimal characters. Process the next 2 characters as checksum, test checksum for zero (not zero, invalid, discard)

The checksum is an XOR of all the characters, the hex after the asterisk should XOR the result to zero. If it does, the buffer contains valid data to be processed stripped of the $ and checksum. Flip buffers and set the variable to zero.

Once a buffer is valid, a process is initiated to examine the data and extract whatever is needed (for my needs, that is just date, time, and if the GPS data is valid - A in the xxRMC message).

I hope this is useful.

Thanks @Alan73922 , your character-at-a-time processing would avoid the issue I see altogether, and I suspect is quite robust.

It’s always a bit dodgy trying to fully understand someone else’s logic, but I still think that the fundamental issue with Core’s gps_reader buffer handling is that it fails to address the inevitable scenario of being presented with a buffer that starts smack bang in the middle of an NMEA sentence. If I have not understood the writer’s cunning algo - I apologise! Why inevitable? I’ll try to explain.. a picture might help.

The NEO-6M is sending a continuous stream of NMEA chunks/blocks, each containing multiple sentences, delimited as you said by $ and * characters. So - it looks like this (apologies for the simplistic rendering, and I’m leaving the checksum out for simplicity), I’ve inserted ‘B’ meaning Block Boundary… new chunk starting.

.$xxx*..$xxx*..$xxx* B $xxx*..$xxx*..$xxx* B $xxx*..$xxx*..$xxx* B $xxx*..$xxx*..$xxx*

                ^

Now, the gps_reader comes along, and at regular, fixed intervals, reads the above stream via UART i/f, starting from some point.which I represent by the ^ pointer. Given the fixed timing of the reads… and the overall (variable) length of the NMEA data, it seems inevitable, as my debug prints show… that after a while, these two independent processes get out of sync… but, given say another 30 seconds - the window slides along enough, and they get back in sync. This is exactly what my output demonstrates, and I’ve repeated it many times, with different timings.

I’d be happy if someone (the original author?) could explain how the gps_reader’s logic manages this … I can’t see it. I do see the symptoms of NOT recognising the issue. My gut feeling is that what is required is another layer - which either

  • no look-back… (minimally) skips to the start of the next NMEA sentence (ie, looks for a “$”) or possibly, skips to the start of the next chunk, or
  • with look-back builds a complete NMEA chunk out of the previous buffer plus contents of the current buffer… and hands that to the sentence-parser

@Jane … any chance of getting original author to comment on this?

Cheers, T.

I wonder if the problem is the sleep(sms). If you process data really quickly (a few ms) then sleep for 1000ms your actual time taken is slightly more than 1000ms, meaning you would come back later and later until eventually slipping one second. The usual way to process intermittent data is to spin waiting for the data capture to complete (which assumes the process has a flag to indicate it is complete). My implementation has a number of background processes, they each have their own flag and there is a common flag which is set by any completing process. So the main program just spins testing that flag if it has nothing else to do.

I can’t think how an NMEA handler can determine when a block is finished unless it is looking for just one type of message like xxRMC, which comes out once a second. I suppose it could assume when the data stream from the GPS unit pauses for a time (e.g. 10ms), that’s the end, but that’s an assumption not a good practice.

Out of interest, I looked at the NEO-7 I use, the last block each second is xxGLL. The GPS module hasn’t been reconfigured, using the default when it was purchased. I suppose that could be used to indicate input is complete.

The way my program works is to extract data from each message as soon as it is in a buffer. Memory is limited so there’s no way to store all the messages received in a second. The trigger to the main program is receiving the 1PPS (one pulse per second) from the GPS unit (the NEO-6 has this). There’s enough time to read the extracted data before it is overwritten by the next block.

And yes, this has proven to be surprisingly robust. More surprising because it receives the data using bit bashing, the UART has a different purpose. And there’s a constraint on how long is spent in an interrupt routine, there’s one that must be serviced every 25µs. One reason for using assembler.

Hi Trevor,

I’ve used gps_parser.py with RP2040-Zero and RP2350-Zero boards without experiencing that problem, so the parser does work. Those boards do not have WIFI and you mention later that you’re using Pico 2W.

For LORA communication operating at 915 MHz, Core Electronics includes this warning “We strongly advise against using this module with a Pico W (at least without separation), as the WiFi interferes with LoRa reception.” GPS operates at 1575 MHz, so it may be possible that there’s some interference from Pico WIFI.

Increasing the GPS/antenna separation from the Pico, or testing on a non-WIFI Pico could be worth a try.

JohnH

@Alan73922 You raise an interesting question… namely is the phenomenon I’m seeing actually an artefact of the test script timing. That said, I did mention in my first post I wondered if using asyncio might be better… Further, I would be surprised if lots of users are NOT using a similar simple 1 second loop in their projects. Who knows… maybe they grab data less frequently.

My upgraded test script - I can specify sleep in ms, that was just so I could run the same script with 1 second… or slightly longer sleep periods. The output I attached last message, where it is clear that things start in-sync … go out of sync… and eventually come back in-sync, used 1000 ms sleep and 500 ms timeout on the UART - the default parameters.

In the absence of other assistance, I will probably resort to writing my own cobbled-together driver, with hopefully guaranteed correct buffer management.

Cheers, T.

@John265780 Hey John… I’m not using the WiFi on my Pico, nor LoRa modules. just a plain old Pico on a platform, with NOTHING else connected. Running it outside with no chance of stray radiation from “stuff” on my desk!

I started with the bare-bones script reading data at 1 Hz… and when I got the bad reads after a couple of minutes… started making my script a bit fancier to diagnose what was happening.

I’m seeing 10 or so satellites… the total length of the NMEA data coming down the pipe is like 540 bytes give or take per chunk… which at 9600 baud will take over 560 ms to transmit. ( I had high hopes that increasing the timeout value in the UART might be the fix… it wasn’t)

I still maintain that as my log shows… the gps_reader ends up trying to decode a block starting mid-sentence… does that for a while, and then things return to sensible-mode.. and then go bad again. This surely is a smoking gun that the buffer management is the root cause.

Thanks for your interest! Cheers, T.

PS John you said you had a project in which this device worked fine… how often were you reading data in the project?

Hi Trevor,
I was using 1 second intervals and 9600 baud. Any interval below 600 ms was too short to receive a frame of data.

(The project was a basic GPS receiver, showing position, direction (when moving), altitude and speed. I’ve tested it in the car and it works quite well. I also tried using it to record static locations and relatively short distances between points but it lacks accuracy. The main problem I had with UBlox-6M was time to initially connect to satellites, and then long delays with re-connection. Once connected, it worked fine.)

I’ll be interested to see how your problem is resolved.
JohnH

Makes sense… if like me you get 10 or 11 satellites you will need around 600 ms @ 9600 baud for each chunk of NMEA data.

I’ve taken a closer look at gps_reader’s code… I see my earlier comments did not properly note the action of the .split(‘$’)… Am still somewhat baffled trying to reconcile my debug output. Might do another test with a different Pico and therefore UART…

Cheers, T.

While a little late here…
For testing, if you can, connect the GPS to a USB-UART adaptor.
Then using a computer, connect a terminal to the USB Serial at the correct baud rate and log the GPS packets for review.

This removed any processing overhead and data corruption due to that processing.

The GPS as such will just send its data every X amount of time. e.g. Send its defined packet set ever 1 second (would be a common default); some GPS unitis allow faster updates, but will need faster baud rates to keep up.

So looping back, if you can do just a GPS to terminal on a computer, you can confirm if the issue is the data from the GPS or if its due to gps processing challenges.

Have have been using these for years all with my own parsers and never had any issues.

Hey @Michael99645 , always glad to get your input! Unfortunately, my debugging time is split between this new project and my main (more pressing) one… the one with the 1m long I2C cable we discussed ages ago!

I’ll try the direct USB connection ASAP. I have now tested with a second Pico, so ruled out anything weird on the Pico itself (or associated UART), and also a different (new) NEO-6M Got the same result, and even a quick look at the debug prints seems to confirm there’s some weird stuff happening with the buffer, but I’m flat out on my other project (debugging about 5,000 lines of rather complex Python)… and can’t immediately copy that info into this post - sorry. Will revert and show that when I get a bit of spare time (Hah!)

Interesting that you mention no problems using your own parsers. Core’s parsers decoding of sentences is fine… I’m just suspicious of the buffer management. Do you have an alternative you are able to share?

PS… I should have said… I have also be running a simple script that just dumps data read from the UART - no NMEA processing, just raw data. Looks just fine.

Thanks for your comments,
Cheers, T.