Operating a commercially available GPS module attached to a PyCubed mainboard.

⚙ Hardware Needed


📚 External Libraries Needed


pycubed.py

adafruit_gps.py

<aside> ⚠️ ALWAYS make sure you have antennas connected to the GPS connectors BEFORE powering the PyCubed board and especially before enabling power to the GPS.

</aside>

📑 Code


"""
Simple GPS example adapted from Adafruit gps_simpletest.py

"""

from pycubed import cubesat
import adafruit_gps, time
import time,board, digitalio, busio

print('Power to GPS on')
cubesat.en_gps.value = True
time.sleep(2)
print('Booting GPS')

# Create a GPS module instance.
gps = adafruit_gps.GPS(cubesat.uart, debug=False) # Enable debugging to see raw GPS output

# Main loop runs forever printing the location, etc. every second.
last_print = time.monotonic()
while True:
    gps.update()

    current = time.monotonic()
    if current - last_print >= 1.0:
        last_print = current
        if not gps.has_fix:
            # Try again if we don't have a fix yet.
            print('Waiting for fix...be patient!')
            continue
        # We have a fix! (gps.has_fix is true)
        # Print out details about the fix like location, date, etc.
        print('=' * 40)  # Print a separator line.
        print('Fix timestamp: {}/{}/{} {:02}:{:02}:{:02}'.format(
            gps.timestamp_utc.tm_mon,   # Grab parts of the time from the
            gps.timestamp_utc.tm_mday,  # struct_time object that holds
            gps.timestamp_utc.tm_year,  # the fix time.  Note you might
            gps.timestamp_utc.tm_hour,  # not get all data like year, day,
            gps.timestamp_utc.tm_min,   # month!
            gps.timestamp_utc.tm_sec))
        print('Latitude: {0:.6f} degrees'.format(gps.latitude))
        print('Longitude: {0:.6f} degrees'.format(gps.longitude))
        print('Fix quality: {}'.format(gps.fix_quality))
        # Some attributes beyond latitude, longitude and timestamp are optional
        # and might not be present.  Check if they're None before trying to use!
        if gps.satellites is not None:
            print('# satellites: {}'.format(gps.satellites))
        if gps.altitude_m is not None:
            print('Altitude: {} meters'.format(gps.altitude_m))
        if gps.speed_knots is not None:
            print('Speed: {} knots'.format(gps.speed_knots))
        if gps.track_angle_deg is not None:
            print('Track angle: {} degrees'.format(gps.track_angle_deg))
        if gps.horizontal_dilution is not None:
            print('Horizontal dilution: {}'.format(gps.horizontal_dilution))
        if gps.height_geoid is not None:
            print('Height geo ID: {} meters'.format(gps.height_geoid))

Details


With gps debugging enabled, you should see the something similar to the code below each second while waiting for a GPS fix:

Waiting for fix...
('GNGGA', '115950.000,0000.0000,N,00000.0000,E,0,00,0.0,0.0,M,0.0,M,,0000')
('GNGLL', '0000.0000,N,00000.0000,E,115950.000,V,N')
('GNGSA', 'A,1,,,,,,,,,,,,,0.0,0.0,0.0')
('GNRMC', '115950.000,V,0000.0000,N,00000.0000,E,000.0,000.0,280606,,,N')
('GNVTG', '000.0,T,,M,000.0,N,000.0,K,N')
('GNZDA', '115950.000,28,06,2006,00,00')

it can take a while to get a GPS fix, especially if you're inside. Once the GPS has a fix, the output will then change to something like this (location redacted):

========================================
Fix timestamp: 1/13/2020 18:05:04
Latitude: 11.111111 degrees
Longitude: -111.111111 degrees
Fix quality: 1
# satellites: 4
Altitude: -44.7 meters
Speed: 3.3 knots
Track angle: 10.4 degrees
Horizontal dilution: 4.4
Height geo ID: -32.2 meters
('GNGGA', '111111.000,1111.1111,N,11111.1111,W,1,01,1.1,-11.1,M,-32.2,M,,0000')
('GNGLL', '1111.1111,N,11111.1111,W,111111.000,A,A')
('GPGSA', 'A,1,01,11,11,11,,,,,,,,,11.1,1.1,1.1')
('GNRMC', '111111.000,A,1111.1111,N,11111.1111,W,001.1,010.1,111110,,,A')
('GNVTG', '010.8,T,,M,003.3,N,006.2,K,A')
('GNZDA', '111111.000,13,01,2020,00,00')
========================================

↩ Return Home

↩ Return to Resources

↩ Return to Code Examples