Pynmea2 output Decimal Degrees. (Solved)
Posted: Thu Sep 19, 2019 10:34 am
Hi,
I've been working on a project for over 3 months at this stage - most of that time has been spent waiting for parts.
The rest, learning that I know very little about python.
I've bolded the important parts for easy reading.
The Project: A raspberry-pi based Geiger counter with GPS logging.
The problem: Waypoints "clustering" (despite moving several kilometers, coord's change very little)
Where I went wrong:
This bit takes a few more words to explain.
However, to sum it up; I believe I'm only logging the first half of the coords.
I'm running one python script in the background to count radioactive particles via a 1 microsecond pulse, and write to a file every 5 sec
This bit works fine. (radcount.py) Not relevant to the problem, but included for context.
I'm running another python in the foreground that's much more verbose - that does most of the work.
This one does a few things:
Reads NMEA data using Pynmea2
Checks for gps fix
if there is:
opens a new file with the date in the name
creates the headers if the file didn't already exist
adds all the string values etc to a new line in said file
If not
skips over the rest, prints a message, and loops back to start
I'm having issues working out how to call the minutes and seconds portions of the coords and joining the strings to achieve decimal degrees.
After inspecting the output the first few times, and loading it into google earth, it seemed that the output of "msg.latitude and "msg.longitude" were indeed the full decimal degree output (based on length of value)
I now realize that I need to truncate the values to a number of digits and join on the minutes and seconds.
I have tried several methods to do this and all have failed, and I have sought out the documentation for pynmea2, but to a hobbyist such as myself who has not had years of intensive learning on the topic, it's a little confusing.
My current code is below:
I've been working on a project for over 3 months at this stage - most of that time has been spent waiting for parts.
The rest, learning that I know very little about python.
I've bolded the important parts for easy reading.
The Project: A raspberry-pi based Geiger counter with GPS logging.
The problem: Waypoints "clustering" (despite moving several kilometers, coord's change very little)
Where I went wrong:
This bit takes a few more words to explain.
However, to sum it up; I believe I'm only logging the first half of the coords.
I'm running one python script in the background to count radioactive particles via a 1 microsecond pulse, and write to a file every 5 sec
This bit works fine. (radcount.py) Not relevant to the problem, but included for context.
I'm running another python in the foreground that's much more verbose - that does most of the work.
This one does a few things:
Reads NMEA data using Pynmea2
Checks for gps fix
if there is:
opens a new file with the date in the name
creates the headers if the file didn't already exist
adds all the string values etc to a new line in said file
If not
skips over the rest, prints a message, and loops back to start
I'm having issues working out how to call the minutes and seconds portions of the coords and joining the strings to achieve decimal degrees.
After inspecting the output the first few times, and loading it into google earth, it seemed that the output of "msg.latitude and "msg.longitude" were indeed the full decimal degree output (based on length of value)
I now realize that I need to truncate the values to a number of digits and join on the minutes and seconds.
I have tried several methods to do this and all have failed, and I have sought out the documentation for pynmea2, but to a hobbyist such as myself who has not had years of intensive learning on the topic, it's a little confusing.
My current code is below:
Code: Select all
Alt = "None"
count = str(0)
uSvHr = str(0)
import os
import time
import serial
import pynmea2
from datetime import datetime
now = datetime.now()
def parseGPS(str):
if str.find('GGA') > 0:
msg = pynmea2.parse(str)
Alt = (msg.altitude_units)
time.sleep(15)
if Alt == 'M':
print ("GPS OK!- %s SATELITES" % (msg.num_sats))
with open("countlog.txt", "r") as radcount:
count = contents =radcount.readline()
print ("COUNTS PER MIN: " + count)
print ("uSv/Hr " + uSvHr)
with open("radlog" + now.strftime("%d-%m-%y") + ".csv", "a") as gps:
if (os.stat("radlog" + now.strftime("%d-%m-%y") + ".csv").st_size == 0): #check if the file i$
print "CSV FILE EMPTY!"
print "ADDING COLUMN NAMES"
gps.write("Time,Lat,Long,Alt,Sats,CPM,Quality" + '\n')
gps.write("%s,$s,$s,%s%s,%s,%s" % (msg.timestamp,msg.latitude,msg.longitude,msg.altitude,msg.a$
gps.write(",%s" % (count) + '\n')
else:
print ("NO GPS FIX! - LOGGING PAUSED. %s SATELITES" % msg.num_sats)
serialPort = serial.Serial("/dev/ttyAMA0", 9600, timeout=0.5)
while True:
# time.sleep(0.5)
str = serialPort.readline()
parseGPS(str)