adafruit / Adafruit_CircuitPython_GPS

GPS parsing module for CircuitPython. Meant to parse NMEA data from serial GPS modules.
MIT License
75 stars 58 forks source link

Update adafruit_gps.py #17

Closed jomogalla closed 5 years ago

jomogalla commented 5 years ago

I was trying to call update() in the adafruit_gps.py module and it would take quite a while, blocking my program from continuing until _uart.readline() returned.

By adding a check to make sure the number of bytes in the input buffer was at least 64, the module doesn't seem to block while waiting for bytes in the buffer.

evaherrada commented 5 years ago

This is WAY faster. I was able to get the example file to run once every 0.01 seconds (I didn't try any faster than that) and without the code you suggested, it ran at a maximum of once every 0.08 seconds.

evaherrada commented 5 years ago

The only thing I'm wondering about is the GPS chip having a max refresh rate of 10Hz. Anything faster than that could produce duplicate data.

MSube commented 5 years ago

_uart.readline() blocks because it waits for the \r that terminates the NMEA sentence. Check for in_waiting >= 64 means that you wait for the buffer to fill up and the next byte received would overflow the internal buffer of the UART (which is 64 bytes. Correct?). That requires that you check it at least once per millisecond if running with 9600 because that's the time needed to receive one byte. I assume that it's also possible that the end of a sentence is in the uart buffer and you won't read it until the next sentence starts (happens if sentences are longer than 64 bytes).

A better approach is to use read(nbytes=in_waiting) if in_waiting > 0 and collect the incoming bytes in your own buffer. This way you only need to check every 64 ms to avoid an UART overflow.

evaherrada commented 5 years ago

By read() are you referring to self._uart.read() or something else?

MSube commented 5 years ago

yes, _uart.read()

MSube commented 5 years ago

Feel free to look at MSube/CircuitPython_GPS_NMEA, the update() function in msube_gps.py is doing this.

ladyada commented 5 years ago

ok cool - yeah we didnt have in_waiting before!