Skip to content

Commit d184e6e

Browse files
committed
Initial GPS changes.
1 parent 9770d62 commit d184e6e

File tree

8 files changed

+59
-44
lines changed

8 files changed

+59
-44
lines changed

gps/README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,22 @@ the Pyboard is run from a voltage >5V the Pyboard 3V3 pin should be used.
6464
| Vin | V+ or 3V3 | |
6565
| Gnd | Gnd | |
6666
| PPS | X3 | Y |
67-
| Tx | X2 (U4 rx) | Y |
68-
| Rx | X1 (U4 tx) | |
67+
| Tx | X2 (U4 rx) | |
68+
| Rx | X1 (U4 tx) | Y |
6969

7070
This is based on UART 4 as used in the test programs; any UART may be used. The
7171
UART Tx-GPS Rx connection is only necessary if using the read/write driver. The
7272
PPS connection is required only if using the timing driver `as_tGPS.py`. Any
7373
pin may be used.
7474

75+
On the Pyboard D the 3.3V output is switched. Enable it with the following
76+
(typically in `main.py`):
77+
```python
78+
import time
79+
machine.Pin.board.EN_3V3.value(1)
80+
time.sleep(1)
81+
```
82+
7583
## 1.2 Basic Usage
7684

7785
If running on a [MicroPython] target the `uasyncio` library must be installed.
@@ -906,4 +914,4 @@ duration over which it is measured.
906914
[pyboard]:http://docs.micropython.org/en/latest/pyboard/pyboard/quickref.html
907915
[MTK_command]:https://github.com/inmcm/MTK_commands
908916
[Ultimate GPS Breakout]:http://www.adafruit.com/product/746
909-
[micropyGPS]:https://github.com/inmcm/micropyGPS.git
917+
[micropyGPS]:https://github.com/inmcm/micropyGPS.git

gps/ast_pbrw.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
# Avoid multiple baudrates. Tests use 9600 or 19200 only.
2222
BAUDRATE = 19200
23-
red, green, yellow, blue = pyb.LED(1), pyb.LED(2), pyb.LED(3), pyb.LED(4)
23+
red, green, yellow = pyb.LED(1), pyb.LED(2), pyb.LED(3)
2424
ntimeouts = 0
2525

2626
def callback(gps, _, timer):
@@ -89,7 +89,6 @@ async def date(gps):
8989
while True:
9090
await asyncio.sleep(4)
9191
await gps.data_received(date=True)
92-
blue.toggle()
9392
print('***** DATE AND TIME *****')
9493
print('Data is Valid:', hex(gps._valid))
9594
print('UTC Time:', gps.utc)

gps/log_kml.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# log_kml.py Log GPS data to a kml file for display on Google Earth
22

3-
# Copyright (c) Peter Hinch 2018
3+
# Copyright (c) Peter Hinch 2018-2020
44
# MIT License (MIT) - see LICENSE file
55
# Test program for asynchronous GPS device driver as_pyGPS
66
# KML file format: https://developers.google.com/kml/documentation/kml_tut
77
# http://www.toptechboy.com/arduino/lesson-25-display-your-gps-data-as-track-on-google-earth/
88

9+
# Remove blue LED for Pyboard D
10+
911
# Logging stops and the file is closed when the user switch is pressed.
1012

1113
import as_GPS
@@ -39,11 +41,11 @@
3941
</Document></kml>
4042
'''
4143

42-
red, green, yellow, blue = pyb.LED(1), pyb.LED(2), pyb.LED(3), pyb.LED(4)
44+
red, green, yellow = pyb.LED(1), pyb.LED(2), pyb.LED(3)
4345
sw = pyb.Switch()
4446

4547
# Toggle the red LED
46-
def toggle_led(gps):
48+
def toggle_led(*_):
4749
red.toggle()
4850

4951
async def log_kml(fn='/sd/log.kml', interval=10):
@@ -62,7 +64,6 @@ async def log_kml(fn='/sd/log.kml', interval=10):
6264
f.write(',')
6365
f.write(str(gps.altitude))
6466
f.write('\r\n')
65-
blue.toggle()
6667
for _ in range(interval * 10):
6768
await asyncio.sleep_ms(100)
6869
if sw.value():

v3/demos/gps/README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ based on this excellent library [micropyGPS].
77
The code in this V3 repo has been ported to uasyncio V3. Some modules can run
88
under CPython: doing so will require Python V3.8 or later.
99

10+
**UNDER REVIEW: API MAY CHANGE**
11+
1012
## 1.1 Driver characteristics
1113

1214
* Asynchronous: UART messaging is handled as a background task allowing the
@@ -67,14 +69,22 @@ the Pyboard is run from a voltage >5V the Pyboard 3V3 pin should be used.
6769
| Vin | V+ or 3V3 | |
6870
| Gnd | Gnd | |
6971
| PPS | X3 | Y |
70-
| Tx | X2 (U4 rx) | Y |
71-
| Rx | X1 (U4 tx) | |
72+
| Tx | X2 (U4 rx) | |
73+
| Rx | X1 (U4 tx) | Y |
7274

7375
This is based on UART 4 as used in the test programs; any UART may be used. The
7476
UART Tx-GPS Rx connection is only necessary if using the read/write driver. The
7577
PPS connection is required only if using the timing driver `as_tGPS.py`. Any
7678
pin may be used.
7779

80+
On the Pyboard D the 3.3V output is switched. Enable it with the following
81+
(typically in `main.py`):
82+
```python
83+
import time
84+
machine.Pin.board.EN_3V3.value(1)
85+
time.sleep(1)
86+
```
87+
7888
## 1.2 Basic Usage
7989

8090
If running on a [MicroPython] target the `uasyncio` library must be installed.
@@ -160,7 +170,7 @@ optionally be installed; this requires `aswitch.py` from the root of this
160170
repository.
161171
For the [timing driver](./README.md#4-using-gps-for-accurate-timing)
162172
`as_tGPS.py` should also be copied across. The optional test program
163-
`as_GPS_time.py` requires `asyn.py` from the root of this repository.
173+
`as_GPS_time.py` requires the `primitives` package.
164174

165175
### 1.4.2 Python 3.8 or later
166176

@@ -436,7 +446,6 @@ LED's:
436446
* Red: Toggles each time a GPS update occurs.
437447
* Green: ON if GPS data is being received, OFF if no data received for >10s.
438448
* Yellow: Toggles each 4s if navigation updates are being received.
439-
* Blue: Toggles each 4s if time updates are being received.
440449

441450
### 3.1.1 Usage example
442451

@@ -635,13 +644,13 @@ import as_tGPS
635644
async def test():
636645
fstr = '{}ms Time: {:02d}:{:02d}:{:02d}:{:06d}'
637646
red = pyb.LED(1)
638-
blue = pyb.LED(4)
647+
green = pyb.LED(2)
639648
uart = pyb.UART(4, 9600, read_buf_len=200)
640649
sreader = asyncio.StreamReader(uart)
641650
pps_pin = pyb.Pin('X3', pyb.Pin.IN)
642651
gps_tim = as_tGPS.GPS_Timer(sreader, pps_pin, local_offset=1,
643652
fix_cb=lambda *_: red.toggle(),
644-
pps_cb=lambda *_: blue.toggle())
653+
pps_cb=lambda *_: green.toggle())
645654
print('Waiting for signal.')
646655
await gps_tim.ready() # Wait for GPS to get a signal
647656
await gps_tim.set_rtc() # Set RTC from GPS

v3/demos/gps/as_GPS_time.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
# Using GPS for precision timing and for calibrating Pyboard RTC
33

44
# This is STM-specific: requires pyb module.
5-
# Requires asyn.py from this repo.
65

7-
# Copyright (c) 2018 Peter Hinch
6+
# Copyright (c) 2018-2020 Peter Hinch
87
# Released under the MIT License (MIT) - see LICENSE file
98

109
import uasyncio as asyncio
1110
import pyb
1211
import utime
1312
import math
14-
import asyn
1513
import as_tGPS
14+
from primitives.message import Message
1615

1716
# Hardware assumptions. Change as required.
1817
PPS_PIN = pyb.Pin.board.X3
@@ -25,16 +24,16 @@
2524
print('usec(minutes=1) Measure accuracy of usec timer.')
2625
print('Press ctrl-d to reboot after each test.')
2726

28-
# Setup for tests. Red LED toggles on fix, blue on PPS interrupt.
27+
# Setup for tests. Red LED toggles on fix, green on PPS interrupt.
2928
async def setup():
3029
red = pyb.LED(1)
31-
blue = pyb.LED(4)
30+
green = pyb.LED(2)
3231
uart = pyb.UART(UART_ID, 9600, read_buf_len=200)
3332
sreader = asyncio.StreamReader(uart)
3433
pps_pin = pyb.Pin(PPS_PIN, pyb.Pin.IN)
3534
return as_tGPS.GPS_Timer(sreader, pps_pin, local_offset=1,
3635
fix_cb=lambda *_: red.toggle(),
37-
pps_cb=lambda *_: blue.toggle())
36+
pps_cb=lambda *_: green.toggle())
3837

3938
# Test terminator: task sets the passed event after the passed time.
4039
async def killer(end_event, minutes):
@@ -66,7 +65,7 @@ async def do_drift(minutes):
6665
gps = await setup()
6766
print('Waiting for time data.')
6867
await gps.ready()
69-
terminate = asyn.Event()
68+
terminate = asyncio.Event()
7069
asyncio.create_task(killer(terminate, minutes))
7170
print('Setting RTC.')
7271
await gps.set_rtc()
@@ -90,7 +89,7 @@ async def do_time(minutes):
9089
await gps.ready()
9190
print('Setting RTC.')
9291
await gps.set_rtc()
93-
terminate = asyn.Event()
92+
terminate = asyncio.Event()
9493
asyncio.create_task(killer(terminate, minutes))
9594
while not terminate.is_set():
9695
await asyncio.sleep(1)
@@ -115,24 +114,24 @@ def time(minutes=1):
115114
def us_cb(my_gps, tick, led):
116115
global us_acquired # Time of previous PPS edge in ticks_us()
117116
if us_acquired is not None:
118-
# Trigger event. Pass time between PPS measured by utime.ticks_us()
117+
# Trigger Message. Pass time between PPS measured by utime.ticks_us()
119118
tick.set(utime.ticks_diff(my_gps.acquired, us_acquired))
120119
us_acquired = my_gps.acquired
121120
led.toggle()
122121

123122
# Setup initialises with above callback
124123
async def us_setup(tick):
125124
red = pyb.LED(1)
126-
blue = pyb.LED(4)
125+
yellow = pyb.LED(3)
127126
uart = pyb.UART(UART_ID, 9600, read_buf_len=200)
128127
sreader = asyncio.StreamReader(uart)
129128
pps_pin = pyb.Pin(PPS_PIN, pyb.Pin.IN)
130129
return as_tGPS.GPS_Timer(sreader, pps_pin, local_offset=1,
131130
fix_cb=lambda *_: red.toggle(),
132-
pps_cb=us_cb, pps_cb_args=(tick, blue))
131+
pps_cb=us_cb, pps_cb_args=(tick, yellow))
133132

134133
async def do_usec(minutes):
135-
tick = asyn.Event()
134+
tick = Message()
136135
print('Setting up GPS.')
137136
gps = await us_setup(tick)
138137
print('Waiting for time data.')
@@ -142,10 +141,10 @@ async def do_usec(minutes):
142141
sd = 0
143142
nsamples = 0
144143
count = 0
145-
terminate = asyn.Event()
144+
terminate = asyncio.Event()
146145
asyncio.create_task(killer(terminate, minutes))
147146
while not terminate.is_set():
148-
await tick
147+
await tick.wait()
149148
usecs = tick.value()
150149
tick.clear()
151150
err = 1000000 - usecs

v3/demos/gps/ast_pb.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import pyb
99
import uasyncio as asyncio
10-
import aswitch
10+
from primitives.delay_ms import Delay_ms
1111
import as_GPS
1212

1313
red = pyb.LED(1)
@@ -84,15 +84,15 @@ async def gps_test():
8484
uart = pyb.UART(4, 9600, read_buf_len=200)
8585
# read_buf_len is precautionary: code runs reliably without it.)
8686
sreader = asyncio.StreamReader(uart)
87-
timer = aswitch.Delay_ms(timeout)
87+
timer = Delay_ms(timeout)
8888
sentence_count = 0
8989
gps = as_GPS.AS_GPS(sreader, local_offset=1, fix_cb=callback, fix_cb_args=(timer,))
9090
print('awaiting first fix')
9191
asyncio.create_task(sat_test(gps))
9292
asyncio.create_task(stats(gps))
9393
asyncio.create_task(navigation(gps))
9494
asyncio.create_task(course(gps))
95-
asyncio.create_task(date(gps))
95+
await date(gps)
9696

9797

9898
asyncio.run(gps_test())

v3/demos/gps/ast_pbrw.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
# ast_pb.py
22
# Basic test/demo of AS_GPS class (asynchronous GPS device driver)
33
# Runs on a Pyboard with GPS data on pin X2.
4-
# Copyright (c) Peter Hinch 2018
4+
# Copyright (c) Peter Hinch 2018-2020
55
# Released under the MIT License (MIT) - see LICENSE file
66
# Test asynchronous GPS device driver as_rwGPS
77

88
# LED's:
99
# Green indicates data is being received.
1010
# Red toggles on RMC message received.
11-
# Yellow and blue: coroutines have 4s loop delay.
11+
# Yellow: coroutine has 4s loop delay.
1212
# Yellow toggles on position reading.
13-
# Blue toggles on date valid.
1413

1514
import pyb
1615
import uasyncio as asyncio
17-
import aswitch
16+
from primitives.delay_ms import Delay_ms
1817
import as_GPS
1918
import as_rwGPS
2019

2120
# Avoid multiple baudrates. Tests use 9600 or 19200 only.
2221
BAUDRATE = 19200
23-
red, green, yellow, blue = pyb.LED(1), pyb.LED(2), pyb.LED(3), pyb.LED(4)
22+
red, green, yellow = pyb.LED(1), pyb.LED(2), pyb.LED(3)
2423
ntimeouts = 0
2524

2625
def callback(gps, _, timer):
@@ -58,7 +57,7 @@ async def stats(gps):
5857
print('Sentences Parsed:', gps.parsed_sentences)
5958
print('CRC_Fails:', gps.crc_fails)
6059
print('Antenna status:', gps.antenna)
61-
print('Firmware vesrion:', gps.version)
60+
print('Firmware version:', gps.version)
6261
print('Enabled sentences:', gps.enabled)
6362
print()
6463

@@ -89,7 +88,6 @@ async def date(gps):
8988
while True:
9089
await asyncio.sleep(4)
9190
await gps.data_received(date=True)
92-
blue.toggle()
9391
print('***** DATE AND TIME *****')
9492
print('Data is Valid:', hex(gps._valid))
9593
print('UTC Time:', gps.utc)
@@ -136,7 +134,7 @@ async def gps_test():
136134
# read_buf_len is precautionary: code runs reliably without it.
137135
sreader = asyncio.StreamReader(uart)
138136
swriter = asyncio.StreamWriter(uart, {})
139-
timer = aswitch.Delay_ms(cb_timeout)
137+
timer = Delay_ms(cb_timeout)
140138
sentence_count = 0
141139
gps = as_rwGPS.GPS(sreader, swriter, local_offset=1, fix_cb=callback,
142140
fix_cb_args=(timer,), msg_cb = message_cb)
@@ -152,7 +150,7 @@ async def gps_test():
152150
asyncio.create_task(course(gps))
153151
asyncio.create_task(date(gps))
154152
await gps.data_received(True, True, True, True) # all messages
155-
asyncio.create_task(change_status(gps, uart))
153+
await change_status(gps, uart)
156154

157155
async def shutdown():
158156
# Normally UART is already at BAUDRATE. But if last session didn't restore
@@ -167,5 +165,7 @@ async def shutdown():
167165

168166
try:
169167
asyncio.run(gps_test())
168+
except KeyboardInterrupt:
169+
print('Interrupted')
170170
finally:
171171
asyncio.run(shutdown())

v3/demos/gps/log_kml.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939
</Document></kml>
4040
'''
4141

42-
red, green, yellow, blue = pyb.LED(1), pyb.LED(2), pyb.LED(3), pyb.LED(4)
42+
red, green, yellow = pyb.LED(1), pyb.LED(2), pyb.LED(3)
4343
sw = pyb.Switch()
4444

4545
# Toggle the red LED
46-
def toggle_led(gps):
46+
def toggle_led(*_):
4747
red.toggle()
4848

4949
async def log_kml(fn='/sd/log.kml', interval=10):
@@ -62,7 +62,6 @@ async def log_kml(fn='/sd/log.kml', interval=10):
6262
f.write(',')
6363
f.write(str(gps.altitude))
6464
f.write('\r\n')
65-
blue.toggle()
6665
for _ in range(interval * 10):
6766
await asyncio.sleep_ms(100)
6867
if sw.value():

0 commit comments

Comments
 (0)