|
8 | 8 | import adafruit_gps
|
9 | 9 |
|
10 | 10 |
|
| 11 | +# Define RX and TX pins for the board's serial port connected to the GPS. |
| 12 | +# These are the defaults you should use for the GPS FeatherWing. |
| 13 | +# For other boards set RX = GPS module TX, and TX = GPS module RX pins. |
11 | 14 | RX = board.RX
|
12 | 15 | TX = board.TX
|
13 | 16 |
|
| 17 | +# Create a serial connection for the GPS connection using default speed and |
| 18 | +# a slightly higher timeout (GPS modules typically update once a second). |
14 | 19 | uart = busio.UART(TX, RX, baudrate=9600, timeout=30)
|
15 | 20 |
|
| 21 | +# for a computer, use the pyserial library for uart access |
| 22 | +#import serial |
| 23 | +#uart = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=3000) |
| 24 | + |
| 25 | +# Create a GPS module instance. |
16 | 26 | gps = adafruit_gps.GPS(uart, debug=False)
|
17 | 27 |
|
| 28 | +# Initialize the GPS module by changing what data it sends and at what rate. |
| 29 | +# These are NMEA extensions for PMTK_314_SET_NMEA_OUTPUT and |
| 30 | +# PMTK_220_SET_NMEA_UPDATERATE but you can send anything from here to adjust |
| 31 | +# the GPS module behavior: |
| 32 | +# https://cdn-shop.adafruit.com/datasheets/PMTK_A11.pdf |
| 33 | + |
| 34 | +# Turn on the basic GGA and RMC info (what you typically want) |
18 | 35 | gps.send_command(b'PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
|
19 | 36 | # Turn on just minimum info (RMC only, location):
|
20 | 37 | #gps.send_command(b'PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
|
|
23 | 40 | # Tuen on everything (not all of it is parsed!)
|
24 | 41 | #gps.send_command(b'PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0')
|
25 | 42 |
|
| 43 | +# Set update rate to once a second (1hz) which is what you typically want. |
26 | 44 | gps.send_command(b'PMTK220,1000')
|
| 45 | +# Or decrease to once every two seconds by doubling the millisecond value. |
| 46 | +# Be sure to also increase your UART timeout above! |
| 47 | +#gps.send_command(b'PMTK220,2000') |
| 48 | +# You can also speed up the rate, but don't go too fast or else you can lose |
| 49 | +# data during parsing. This would be twice a second (2hz, 500ms delay): |
| 50 | +#gps.send_command(b'PMTK220,500') |
27 | 51 |
|
| 52 | +# Main loop runs forever printing the location, etc. every second. |
28 | 53 | last_print = time.monotonic()
|
29 | 54 | while True:
|
30 |
| - a = gps.update() |
| 55 | + # Make sure to call gps.update() every loop iteration and at least twice |
| 56 | + # as fast as data comes from the GPS unit (usually every second). |
| 57 | + # This returns a bool that's true if it parsed new data (you can ignore it |
| 58 | + # though if you don't care and instead look at the has_fix property). |
| 59 | + gps.update() |
| 60 | + # Every second print out current location details if there's a fix. |
31 | 61 | current = time.monotonic()
|
32 | 62 | if current - last_print >= 1.0:
|
33 | 63 | last_print = current
|
34 |
| - print(a) |
| 64 | + if not gps.has_fix: |
| 65 | + # Try again if we don't have a fix yet. |
| 66 | + print('Waiting for fix...') |
| 67 | + continue |
| 68 | + # We have a fix! (gps.has_fix is true) |
| 69 | + # Print out details about the fix like location, date, etc. |
| 70 | + print('=' * 40) # Print a separator line. |
| 71 | + print('Fix timestamp: {}/{}/{} {:02}:{:02}:{:02}'.format( |
| 72 | + gps.timestamp_utc.tm_mon, # Grab parts of the time from the |
| 73 | + gps.timestamp_utc.tm_mday, # struct_time object that holds |
| 74 | + gps.timestamp_utc.tm_year, # the fix time. Note you might |
| 75 | + gps.timestamp_utc.tm_hour, # not get all data like year, day, |
| 76 | + gps.timestamp_utc.tm_min, # month! |
| 77 | + gps.timestamp_utc.tm_sec)) |
| 78 | + print('Latitude: {0:.6f} degrees'.format(gps.latitude)) |
| 79 | + print('Longitude: {0:.6f} degrees'.format(gps.longitude)) |
| 80 | + print('Fix quality: {}'.format(gps.fix_quality)) |
| 81 | + # Some attributes beyond latitude, longitude and timestamp are optional |
| 82 | + # and might not be present. Check if they're None before trying to use! |
| 83 | + if gps.satellites is not None: |
| 84 | + print('# satellites: {}'.format(gps.satellites)) |
| 85 | + if gps.altitude_m is not None: |
| 86 | + print('Altitude: {} meters'.format(gps.altitude_m)) |
| 87 | + if gps.speed_knots is not None: |
| 88 | + print('Speed: {} knots'.format(gps.speed_knots)) |
| 89 | + if gps.track_angle_deg is not None: |
| 90 | + print('Track angle: {} degrees'.format(gps.track_angle_deg)) |
| 91 | + if gps.horizontal_dilution is not None: |
| 92 | + print('Horizontal dilution: {}'.format(gps.horizontal_dilution)) |
| 93 | + if gps.height_geoid is not None: |
| 94 | + print('Height geo ID: {} meters'.format(gps.height_geoid)) |
0 commit comments