asynycio as_GPS

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.

Online
User avatar
Roberthh
Posts: 2054
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: asynycio as_GPS

Post by Roberthh » Thu Jan 09, 2020 1:05 pm

Maybe it makes sence to create a german sub forum here? Ich fänd das toll
I do not agree - Ich stimme dem nicht zu.
A fragmentation of the forum is of no benefit for anyone. Information will get lost.

User avatar
MostlyHarmless
Posts: 147
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: asynycio as_GPS

Post by MostlyHarmless » Thu Jan 09, 2020 1:52 pm

Roberthh wrote:
Thu Jan 09, 2020 1:05 pm
Maybe it makes sence to create a german sub forum here? Ich fänd das toll
I do not agree - Ich stimme dem nicht zu.
A fragmentation of the forum is of no benefit for anyone. Information will get lost.
I second that. There will always be the an exception made when someone needs help but cannot express in English what their problem is. Helping them with translation is the polite thing to do. But the default language should remain English.


Mit freundlichen Grüßen, Jan

User avatar
romeotango
Posts: 27
Joined: Tue Jun 16, 2015 10:52 am
Location: Germany

Re: asynycio as_GPS

Post by romeotango » Fri Jan 10, 2020 12:00 pm

A language fragmentation ist for certain not necessary. As long as deepL is available for longer text passages. ;)
@MostlyHarmless As expected, one problem leads to another, now I can even work with Docker, which I have always avoided. Thanks for the tip.
@PeterHinch I read your tutorial with pleasure and am beginning to understand asyncio slowly. Thanks for the chapter for beginners.

My Beginners-Project will be a small tracker for motorcycling. But with a planned frequency of 10!Hz.
I'm still thinking about the storage format to allow the evaluation and storage on SD-Card on the LoPy or similar.
Timestamp as seconds
For Position, I prefer integers of the seconds of the position, + North, - South, + East, - West.
Or simply the decimal degree multiplied by 10^5.
Speed and the course as integer values.

You can see, lots of thoughts wait for me.

I drink english breakfast tea, smoke pipe tobacco named Virginia Mixture (Neighbour-State to Pennsylvania) and drink wine from Rhineland tonight . I wish you all a nice weekend!

User avatar
pythoncoder
Posts: 4347
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: asynycio as_GPS

Post by pythoncoder » Fri Jan 10, 2020 6:38 pm

romeotango wrote:
Fri Jan 10, 2020 12:00 pm
...My Beginners-Project will be a small tracker for motorcycling. But with a planned frequency of 10!Hz.
I'm still thinking about the storage format to allow the evaluation and storage on SD-Card on the LoPy or similar...
I assume you've seen log_kml.py which logs GPS coordinates in a format which enables the route to be plotted on Google Earth. By default it only logs a point every 10s: you must be one fast rider (or a fighter pilot) to need to log every 10ms ;)
Peter Hinch

User avatar
MostlyHarmless
Posts: 147
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: asynycio as_GPS

Post by MostlyHarmless » Fri Jan 10, 2020 7:32 pm

10Hz is 100ms ... but anyhow, I agree, that is a heck of a log frequency for GPS tracking. And definitely yes on the KML format. It is not only used by Google Earth, but also supported by many other track/route editors. Then again, since it is based on XML it is prone to corruption when writing continuous and then experiencing a power loss or other glitch. So maybe an intermediate format that has "easy conversion to KML" in mind is better. Start a new file whenever the logger starts and discard incomplete records at EOF when converting.

In my experience (mostly using Garmin handhelds for like 15 years) a high log frequency is actually conterproductive. The signal always bounces a little. The resulting zig-zag travel can increase the claimed distance by quite a bit. It is in the lower percent when riding a motorcycle, but quite substantial when hiking. Although it is not legal on the roads where I live, I do know for some reason that my C650GT can reach the maximum speed of 110 MPH (175 km/h) that the manual claims. They must have restricted that in the ECU because it can do that uphill too. At that speed a track point every second would be 50m apart. Are you sure you need more accuracy than that?

I only played so far with a NEO-6M that is connecting via UART and a PPS pin. That thing doesn't send data more frequent than 1Hz. There is also a Beitian BN-880 in my box labeled "Sensors of Interest." But that one probably needs to wait until next winter.


Regards, Jan

(always keep the shiny side up and the little rubber things on the road)

User avatar
romeotango
Posts: 27
Joined: Tue Jun 16, 2015 10:52 am
Location: Germany

Re: asynycio as_GPS

Post by romeotango » Tue Jan 14, 2020 5:39 pm

OK, here we go...
the version2 asyncio library gets into OSError: [Errno 19] ENODEV
with fast_io and uasyncio from DPG no error messages
But there is only one run-through.
>>> Running log_kml-lopy.py

>>>
>>>
None
52.124739
8.638227
>
Pycom MicroPython 1.20.1.r2 [v1.11-06dfad0] on 2019-11-30; LoPy with ESP32
Pybytes Version: 1.3.0
The kml file looks like this
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Style id="yellowPoly">
<LineStyle>
<color>7f00ffff</color>
<width>4</width>
</LineStyle>
<PolyStyle>
<color>7f00ff00</color>
</PolyStyle>
</Style>
<Placemark><styleUrl>#yellowPoly</styleUrl>
<LineString>
<extrude>1</extrude>
<tesselate>1</tesselate>
<altitudeMode>absolute</altitudeMode>
<coordinates>

</coordinates>
</LineString></Placemark>

</Document></kml>
What is my error?
the coordinates are printed for test purpose

Data from gps-receiver are correct, when collected directly via uart.readline without asyncio.
But course and speed are not really valid due to no moving.

awaiting your hints :-)


Code: Select all

# log_kml.py Log GPS data to a kml file for display on Google Earth

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

# Logging stops and the file is closed when the keyboard interrupt given.
# Copyright (c) Rainer Treichel
# forked from Peter Hinch log_kml.py

import gc
gc.collect()
import as_GPS
gc.collect()
import uasyncio as asyncio
from machine import UART
gc.collect()
gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
import pycom
from micropython import const


str_start = '''<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Style id="yellowPoly">
<LineStyle>
<color>7f00ffff</color>
<width>4</width>
</LineStyle>
<PolyStyle>
<color>7f00ff00</color>
</PolyStyle>
</Style>
<Placemark><styleUrl>#yellowPoly</styleUrl>
<LineString>
<extrude>1</extrude>
<tesselate>1</tesselate>
<altitudeMode>absolute</altitudeMode>
<coordinates>
'''

str_end = '''
</coordinates>
</LineString></Placemark>
 
</Document></kml>
'''

green = const (0x007f00) # green
yellow = const (0x7f7f00) # yellow
red = const (0x7f0000) # red
blue = const (0x0000ff)
off = const (0x000000)

def led2me():
    pycom.heartbeat(False)

def led2pycom():
    pycom.heartbeat (True)

def led(colour):
    pycom.rgbled (colour)


# Toggle the LED green/blue
def toggle_led():
    led(green)
    await asyncio.sleep (1)
    led(blue)

async def garbage():
    gc.collect()
    print ("garbage collect")
    gc.threshold(gc.mem_free() // 4 + gc.mem_alloc()) 
    await asyncio.sleep(5)

async def log_kml(fn='/sd/log.kml', interval=1):
    led(yellow)  # Waiting for data
    uart = UART(1, baudrate=9600, pins=('P20','P21'))
    print(uart.readline())
    sreader = asyncio.StreamReader(uart)
    gps = as_GPS.AS_GPS(sreader, fix_cb=toggle_led())
    await gps.data_received()# True, True, True, False)
    print(gps.latitude_string(as_GPS.KML))
    print(gps.longitude_string(as_GPS.KML))
    led(blue)
    with open(fn, 'w') as f:
        f.write(str_start)
        while not KeyboardInterrupt:
            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')
            led(off)
            # for _ in range(interval * 10):
            await asyncio.sleep_ms(100)

        f.write(str_end)
    led(off)
    


led2me()
loop = asyncio.get_event_loop()
loop.run_until_complete(log_kml())
led2pycom()

User avatar
pythoncoder
Posts: 4347
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: asynycio as_GPS

Post by pythoncoder » Tue Jan 14, 2020 6:16 pm

It's hard for me to debug code when I don't have the relevant hardware and have no experience with the Pycom fork of MicroPython. This line looks odd to me

Code: Select all

while not KeyboardInterrupt:
as the symbol appears to be undefined, but you aren't getting an error message. Is this some kind of Pycom magic? In my book this should throw an exception.
Peter Hinch

User avatar
romeotango
Posts: 27
Joined: Tue Jun 16, 2015 10:52 am
Location: Germany

Re: asynycio as_GPS

Post by romeotango » Tue Jan 14, 2020 6:29 pm

Thank you for this fast reply.
If i use True instead, it is no difference.
I thought it has any silly error from me inside, that inhibits the running as expected.
Rainer

User avatar
MostlyHarmless
Posts: 147
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: asynycio as_GPS

Post by MostlyHarmless » Tue Jan 14, 2020 9:40 pm

KeyboardInterrupt is a standard Python exception class.

In your case a construct like this should work:

Code: Select all

while True:
    try:
        do_your_gps_stuff_here
    except KeyboardInterrupt:
        break

Post Reply