async def log_kml(fn='/sd/log.kml', interval=10): yellow.on() # Waiting for data uart = pyb.UART(4, 9600, read_buf_len=200) # Data on X2 sreader = asyncio.StreamReader(uart) gps = as_GPS.AS_GPS(sreader, fix_cb=toggle_led) await gps.data_received(True, True, True, True) yellow.off() with open(fn, 'w') as f: f.write(str_start) while not sw.value(): f.write(gps.longitude_string(as_GPS.KML)) f.write(',') f.write(gps.latitude_string(as_GPS.KML)) f.write(',') f.write(str(gps.altitude)) f.write('\r\n') blue.toggle() for _ in range(interval * 10): await asyncio.sleep_ms(100) if sw.value(): break f.write(str_end) red.off() green.on()
async def gps_test(): print('Initialising') # Adapt for other MicroPython hardware uart = pyb.UART(4, 9600, read_buf_len=200) # read_buf_len is precautionary: code runs reliably without it.) sreader = asyncio.StreamReader(uart) timer = aswitch.Delay_ms(timeout) sentence_count = 0 gps = as_GPS.AS_GPS(sreader, local_offset=1, fix_cb=callback, fix_cb_args=(timer,)) loop = asyncio.get_event_loop() print('awaiting first fix') loop.create_task(sat_test(gps)) loop.create_task(stats(gps)) loop.create_task(navigation(gps)) loop.create_task(course(gps)) loop.create_task(date(gps))
async def run(): sentence_count = 0 test_RMC = ['$GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62\n', '$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A\n', '$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68\n', '$GPRMC,180041.896,A,3749.1851,N,08338.7891,W,001.9,154.9,240911,,,A*7A\n', '$GPRMC,180049.896,A,3749.1808,N,08338.7869,W,001.8,156.3,240911,,,A*70\n', '$GPRMC,092751.000,A,5321.6802,N,00630.3371,W,0.06,31.66,280511,,,A*45\n'] test_VTG = ['$GPVTG,232.9,T,,M,002.3,N,004.3,K,A*01\n'] test_GGA = ['$GPGGA,180050.896,3749.1802,N,08338.7865,W,1,07,1.1,397.4,M,-32.5,M,,0000*6C\n'] test_GSA = ['$GPGSA,A,3,07,11,28,24,26,08,17,,,,,,2.0,1.1,1.7*37\n', '$GPGSA,A,3,07,02,26,27,09,04,15,,,,,,1.8,1.0,1.5*33\n'] test_GSV = ['$GPGSV,3,1,12,28,72,355,39,01,52,063,33,17,51,272,44,08,46,184,38*74\n', '$GPGSV,3,2,12,24,42,058,33,11,34,053,33,07,20,171,40,20,15,116,*71\n', '$GPGSV,3,3,12,04,12,204,34,27,11,324,35,32,11,089,,26,10,264,40*7B\n', '$GPGSV,3,1,11,03,03,111,00,04,15,270,00,06,01,010,00,13,06,292,00*74\n', '$GPGSV,3,2,11,14,25,170,00,16,57,208,39,18,67,296,40,19,40,246,00*74\n', '$GPGSV,3,3,11,22,42,067,42,24,14,311,43,27,05,244,00,,,,*4D\n', '$GPGSV,4,1,14,22,81,349,25,14,64,296,22,18,54,114,21,51,40,212,*7D\n', '$GPGSV,4,2,14,24,30,047,22,04,22,312,26,31,22,204,,12,19,088,23*72\n', '$GPGSV,4,3,14,25,17,127,18,21,16,175,,11,09,315,16,19,05,273,*72\n', '$GPGSV,4,4,14,32,05,303,,15,02,073,*7A\n'] test_GLL = ['$GPGLL,3711.0942,N,08671.4472,W,000812.000,A,A*46\n', '$GPGLL,4916.45,N,12311.12,W,225444,A,*1D\n', '$GPGLL,4250.5589,S,14718.5084,E,092204.999,A*2D\n', '$GPGLL,0000.0000,N,00000.0000,E,235947.000,V*2D\n'] my_gps = as_GPS.AS_GPS(None) sentence = '' for sentence in test_RMC: my_gps._valid = 0 sentence_count += 1 sentence = await my_gps._update(sentence) if sentence is None: print('RMC sentence is invalid.') else: print('Parsed a', sentence, 'Sentence') print('Longitude:', my_gps.longitude()) print('Latitude', my_gps.latitude()) print('UTC Timestamp:', my_gps.utc) print('Speed:', my_gps.speed()) print('Date Stamp:', my_gps.date) print('Course', my_gps.course) print('Data is Valid:', bool(my_gps._valid & 1)) print('Compass Direction:', my_gps.compass_direction()) print('') for sentence in test_GLL: my_gps._valid = 0 sentence_count += 1 sentence = await my_gps._update(sentence) if sentence is None: print('GLL sentence is invalid.') else: print('Parsed a', sentence, 'Sentence') print('Longitude:', my_gps.longitude()) print('Latitude', my_gps.latitude()) print('UTC Timestamp:', my_gps.utc) print('Data is Valid:', bool(my_gps._valid & 2)) print('') for sentence in test_VTG: my_gps._valid = 0 sentence_count += 1 sentence = await my_gps._update(sentence) if sentence is None: print('VTG sentence is invalid.') else: print('Parsed a', sentence, 'Sentence') print('Speed:', my_gps.speed()) print('Course', my_gps.course) print('Compass Direction:', my_gps.compass_direction()) print('Data is Valid:', bool(my_gps._valid & 4)) print('') for sentence in test_GGA: my_gps._valid = 0 sentence_count += 1 sentence = await my_gps._update(sentence) if sentence is None: print('GGA sentence is invalid.') else: print('Parsed a', sentence, 'Sentence') print('Longitude', my_gps.longitude()) print('Latitude', my_gps.latitude()) print('UTC Timestamp:', my_gps.utc) print('Altitude:', my_gps.altitude) print('Height Above Geoid:', my_gps.geoid_height) print('Horizontal Dilution of Precision:', my_gps.hdop) print('Satellites in Use by Receiver:', my_gps.satellites_in_use) print('Data is Valid:', bool(my_gps._valid & 8)) print('') for sentence in test_GSA: my_gps._valid = 0 sentence_count += 1 sentence = await my_gps._update(sentence) if sentence is None: print('GSA sentence is invalid.') else: print('Parsed a', sentence, 'Sentence') print('Satellites Used', my_gps.satellites_used) print('Horizontal Dilution of Precision:', my_gps.hdop) print('Vertical Dilution of Precision:', my_gps.vdop) print('Position Dilution of Precision:', my_gps.pdop) print('Data is Valid:', bool(my_gps._valid & 16)) print('') for sentence in test_GSV: my_gps._valid = 0 sentence_count += 1 sentence = await my_gps._update(sentence) if sentence is None: print('GSV sentence is invalid.') else: print('Parsed a', sentence, 'Sentence') print('SV Sentences Parsed', my_gps._last_sv_sentence) print('SV Sentences in Total', my_gps._total_sv_sentences) print('# of Satellites in View:', my_gps.satellites_in_view) print('Data is Valid:', bool(my_gps._valid & 32)) data_valid = my_gps._total_sv_sentences > 0 and my_gps._total_sv_sentences == my_gps._last_sv_sentence print('Is Satellite Data Valid?:', data_valid) if data_valid: print('Satellite Data:', my_gps._satellite_data) print('Satellites Visible:', list(my_gps._satellite_data.keys())) print('') print("Pretty Print Examples:") print('Latitude (degs):', my_gps.latitude_string(as_GPS.DD)) print('Longitude (degs):', my_gps.longitude_string(as_GPS.DD)) print('Latitude (dms):', my_gps.latitude_string(as_GPS.DMS)) print('Longitude (dms):', my_gps.longitude_string(as_GPS.DMS)) print('Latitude (kml):', my_gps.latitude_string(as_GPS.KML)) print('Longitude (kml):', my_gps.longitude_string(as_GPS.KML)) print('Latitude (degs, mins):', my_gps.latitude_string()) print('Longitude (degs, mins):', my_gps.longitude_string()) print('Speed:', my_gps.speed_string(as_GPS.KPH), 'or', my_gps.speed_string(as_GPS.MPH), 'or', my_gps.speed_string(as_GPS.KNOT)) print('Date (Long Format):', my_gps.date_string(as_GPS.LONG)) print('Date (Short D/M/Y Format):', my_gps.date_string(as_GPS.DMY)) print('Date (Short M/D/Y Format):', my_gps.date_string(as_GPS.MDY)) print('Time:', my_gps.time_string()) print() print('### Final Results ###') print('Sentences Attempted:', sentence_count) print('Sentences Found:', my_gps.clean_sentences) print('Sentences Parsed:', my_gps.parsed_sentences) print('Unsupported sentences:', my_gps.unsupported_sentences) print('CRC_Fails:', my_gps.crc_fails)
async def run_tests(): uart = UART(4, 9600, read_buf_len=200) swriter = asyncio.StreamWriter(uart, {}) sreader = asyncio.StreamReader(uart) sentence_count = 0 test_RMC = [ '$GPRMC,180041.896,A,3749.1851,N,08338.7891,W,001.9,154.9,240911,,,A*7A\n', '$GPRMC,180049.896,A,3749.1808,N,08338.7869,W,001.8,156.3,240911,,,A*70\n', '$GPRMC,092751.000,A,5321.6802,N,00630.3371,W,0.06,31.66,280511,,,A*45\n' ] test_VTG = ['$GPVTG,232.9,T,,M,002.3,N,004.3,K,A*01\n'] test_GGA = [ '$GPGGA,180050.896,3749.1802,N,08338.7865,W,1,07,1.1,397.4,M,-32.5,M,,0000*6C\n' ] test_GSA = [ '$GPGSA,A,3,07,11,28,24,26,08,17,,,,,,2.0,1.1,1.7*37\n', '$GPGSA,A,3,07,02,26,27,09,04,15,,,,,,1.8,1.0,1.5*33\n' ] test_GSV = [ '$GPGSV,3,1,12,28,72,355,39,01,52,063,33,17,51,272,44,08,46,184,38*74\n', '$GPGSV,3,2,12,24,42,058,33,11,34,053,33,07,20,171,40,20,15,116,*71\n', '$GPGSV,3,3,12,04,12,204,34,27,11,324,35,32,11,089,,26,10,264,40*7B\n', '$GPGSV,3,1,11,03,03,111,00,04,15,270,00,06,01,010,00,13,06,292,00*74\n', '$GPGSV,3,2,11,14,25,170,00,16,57,208,39,18,67,296,40,19,40,246,00*74\n', '$GPGSV,3,3,11,22,42,067,42,24,14,311,43,27,05,244,00,,,,*4D\n', '$GPGSV,4,1,14,22,81,349,25,14,64,296,22,18,54,114,21,51,40,212,*7D\n', '$GPGSV,4,2,14,24,30,047,22,04,22,312,26,31,22,204,,12,19,088,23*72\n', '$GPGSV,4,3,14,25,17,127,18,21,16,175,,11,09,315,16,19,05,273,*72\n', '$GPGSV,4,4,14,32,05,303,,15,02,073,*7A\n' ] test_GLL = [ '$GPGLL,3711.0942,N,08671.4472,W,000812.000,A,A*46\n', '$GPGLL,4916.45,N,12311.12,W,225444,A,*1D\n', '$GPGLL,4250.5589,S,14718.5084,E,092204.999,A*2D\n', '$GPGLL,4250.5589,S,14718.5084,E,092204.999,A*2D\n', ] # '$GPGLL,0000.0000,N,00000.0000,E,235947.000,V*2D\n', # Will ignore this one my_gps = as_GPS.AS_GPS(sreader, fix_cb=callback, fix_cb_args=(42, )) sentence = '' for sentence in test_RMC: sentence_count += 1 await swriter.awrite(sentence) await my_gps.data_received(date=True) print('Longitude:', my_gps.longitude()) print('Latitude', my_gps.latitude()) print('UTC Time:', my_gps.utc) print('Speed:', my_gps.speed()) print('Date Stamp:', my_gps.date) print('Course', my_gps.course) print('Data is Valid:', my_gps._valid) print('Compass Direction:', my_gps.compass_direction()) print('') for sentence in test_GLL: sentence_count += 1 await swriter.awrite(sentence) await my_gps.data_received(position=True) print('Longitude:', my_gps.longitude()) print('Latitude', my_gps.latitude()) print('UTC Time:', my_gps.utc) print('Data is Valid:', my_gps._valid) print('') for sentence in test_VTG: print('Test VTG', sentence) sentence_count += 1 await swriter.awrite(sentence) await asyncio.sleep_ms( 200) # Can't wait for course because of position check print('Speed:', my_gps.speed()) print('Course', my_gps.course) print('Compass Direction:', my_gps.compass_direction()) print('') for sentence in test_GGA: sentence_count += 1 await swriter.awrite(sentence) await my_gps.data_received(position=True) print('Longitude', my_gps.longitude()) print('Latitude', my_gps.latitude()) print('UTC Time:', my_gps.utc) # print('Fix Status:', my_gps.fix_stat) print('Altitude:', my_gps.altitude) print('Height Above Geoid:', my_gps.geoid_height) print('Horizontal Dilution of Precision:', my_gps.hdop) print('Satellites in Use by Receiver:', my_gps.satellites_in_use) print('') for sentence in test_GSA: sentence_count += 1 await swriter.awrite(sentence) await asyncio.sleep_ms(200) print('Satellites Used', my_gps.satellites_used) print('Horizontal Dilution of Precision:', my_gps.hdop) print('Vertical Dilution of Precision:', my_gps.vdop) print('Position Dilution of Precision:', my_gps.pdop) print('') for sentence in test_GSV: sentence_count += 1 await swriter.awrite(sentence) await asyncio.sleep_ms(200) print('SV Sentences Parsed', my_gps._last_sv_sentence) print('SV Sentences in Total', my_gps._total_sv_sentences) print('# of Satellites in View:', my_gps.satellites_in_view) data_valid = my_gps._total_sv_sentences > 0 and my_gps._total_sv_sentences == my_gps._last_sv_sentence print('Is Satellite Data Valid?:', data_valid) if data_valid: print('Satellite Data:', my_gps._satellite_data) print('Satellites Visible:', list(my_gps._satellite_data.keys())) print('') print("Pretty Print Examples:") print('Latitude (degs):', my_gps.latitude_string(as_GPS.DD)) print('Longitude (degs):', my_gps.longitude_string(as_GPS.DD)) print('Latitude (dms):', my_gps.latitude_string(as_GPS.DMS)) print('Longitude (dms):', my_gps.longitude_string(as_GPS.DMS)) print('Latitude (kml):', my_gps.latitude_string(as_GPS.KML)) print('Longitude (kml):', my_gps.longitude_string(as_GPS.KML)) print('Latitude (degs, mins):', my_gps.latitude_string()) print('Longitude (degs, mins):', my_gps.longitude_string()) print('Speed:', my_gps.speed_string(as_GPS.KPH), 'or', my_gps.speed_string(as_GPS.MPH), 'or', my_gps.speed_string(as_GPS.KNOT)) print('Date (Long Format):', my_gps.date_string(as_GPS.LONG)) print('Date (Short D/M/Y Format):', my_gps.date_string(as_GPS.DMY)) print('Date (Short M/D/Y Format):', my_gps.date_string(as_GPS.MDY)) print('Time:', my_gps.time_string()) print() print('### Final Results ###') print('Sentences Attempted:', sentence_count) print('Sentences Found:', my_gps.clean_sentences) print('Sentences Parsed:', my_gps.parsed_sentences) print('Unsupported sentences:', my_gps.unsupported_sentences) print('CRC_Fails:', my_gps.crc_fails)
print("connection lost - moved to fallback loop") # set colour pycom.heartbeat(False) pycom.rgbled(0xf200ea) try: gps_uart = UART(2, baudrate=9600, pins=('P9', 'P11')) except: print("no gps") if GPS: #set up GPS sreader = asyncio.StreamReader(gps_uart) # Create a StreamReader gps = as_GPS.AS_GPS(sreader) # Instantiate GPS if f[3]: # set up serial connection to arduino using custom pins conn = UART(1, baudrate=57600, pins=('P4', 'P10')) # initialise variables for clarity rc_write = [0, 0, 0, 0, 0, 0] #6 channels rc_read = [0, 0, 0, 0, 0, 0] # "" if f[0]: # set up link to ground station with datalink_setup() as link: link.file = normal_file pycom.heartbeat(False) pycom.rgbled(0x007f00) # green
from machine import UART def callback(gps, *_): # Runs for each valid fix print("GPS latitude: {}, GPS longitude: {}, altitude: {:f}".format( gps.latitude_string(), gps.longitude_string(), gps.altitude)) print("Date and time: ", gps.date_string(formatting=as_GPS.LONG), " ", gps.time_string(), " CET") print("Speed: ", gps.speed_string()) print("no of satellites in view: {:d}".format(gps.satellites_in_view)) uart = UART(2, baudrate=115200, rx=21, tx=22, timeout=10000) sreader = asyncio.StreamReader(uart) # Create a StreamReader gps = as_GPS.AS_GPS(sreader, fix_cb=callback, cb_mask=as_GPS.GLL | as_GPS.VTG | as_GPS.RMC, local_offset=2) # Instantiate GPS async def test(): print('waiting for GPS data') await gps.data_received(position=True, date=True, altitude=True) await asyncio.sleep(60) # Run for one minute print("Error statistics:") print("crc errors: {:d}".format(gps.crc_fails)) print("no of clean sentences: {:d}".format(gps.clean_sentences)) print("no of parsed sentences: {:d}".format(gps.parsed_sentences)) print("no of unsupported sentences: {:d}".format( gps.unsupported_sentences))
import uasyncio as asyncio import as_GPS from machine import UART def callback(gps, *_): # Runs for each valid fix print(gps.latitude(), gps.longitude(), gps.altitude) uart = UART(2, baudrate=9600, rx=16, tx=17, timeout=10) sreader = asyncio.StreamReader(uart) # Create a StreamReader gps = as_GPS.AS_GPS(sreader, fix_cb=callback) # Instantiate GPS async def test(): print('waiting for GPS data') await gps.data_received(position=True, altitude=True) await asyncio.sleep(60) # Run for one minute loop = asyncio.get_event_loop() loop.run_until_complete(test())