Converting GPS Latitude (String to Floating Point calculations)
Chuck Rice
Posts: 210
.
This works, but is there a better way to do the same thing?
Cane it be made cleaner and/or more efficient?
-Chuck-
.
Post Edited (Chuck Rice) : 4/9/2008 2:58:37 PM GMT
This works, but is there a better way to do the same thing?
pub getLatInDecDegrees | i,latPtr,fractPartPtr,c,dd,mm,mmmm,rads,degs latPtr := GPS.latitude 'get Latitude in dddmm.mmmm format bytemove(@buf,latPtr,strsize(latPtr)) 'Make a copy to mess with i := 0 repeat 'Split at the decimal point c:=buf[noparse][[/noparse] i++ ] until c == "." buf[noparse][[/noparse] --i ] := 0 'null terminate the degrees string fractPartPtr := @buf[noparse][[/noparse] i+1 ] 'Fractional part is what is after the '.' i-=2 'back up 2 bytes to get the minutes bytemove(@minPart[noparse][[/noparse]0],@buf[noparse][[/noparse] i ],2) 'copy the mm part minPart[noparse][[/noparse] 2 ] := 0 'null terminate the mm part dd := Format.atoi(@buf)/100 'Convert to numbers mm := Format.atoi(@minPart) mmmm := Format.atoi(fractPartPtr) mm := fp.ffloat(mm) 'Convert to float numbers dd := fp.ffloat(dd) mmmm := fp.fdiv(fp.ffloat(mmmm),10000.0) degs := fp.fadd(fp.fdiv(fp.fadd(mm, mmmm),60.0),dd) 'Calc decimal degrees return degs
Cane it be made cleaner and/or more efficient?
-Chuck-
.
Post Edited (Chuck Rice) : 4/9/2008 2:58:37 PM GMT
Comments
Hope this is not too far off thread but,
have you checked the output of your GPS to see if you can parse the NMEA sentence to recover the information that you are looking for. It might save you a bit of work.
I presume you are using a Parallax GPS, so I am not sure about NMEA 0183 (2.2) but a NMEA 0183 GPS system if 2.3 compliant will always transmit GPRMC, GPGGA, GPGL, GPBWC, GPVTG, GPXTE and GPRMB. Some of these sentences are not always documented (I.E GPGSA, GPGSV, GPWPL, GPRTE), because they are not decoded unless specially selected.
There just might be undocumented instructions for that chip, to select these additional sentences.
Ron
The Parallax GPS only provides 4 of the NMEA sentences.
I am getting the information I need from the raw NMEA sentence, but the latitude/longitude
gets delivered in DDMM.MMMM format. I need to convert it to radians to do great circle
calculations. Part of the conversion is to change it into decimal degrees, then radians.
This will then be used to calculate bearing and distance for my robot's navigation.
I have been following the math in this Circuit Cellar article:
www.circuitcellar.com/library/print/1000/Stefan123/4.htm
The conversion from the NMEA strings to floating point is what has me confused. I think
the example I posted works, but I thought that there might be a better/cleaner way to
code it. -Chuck-
.
I was going to send you the cals, but they are mostly covered in the circuitcellur link.
I am surprised the GPS does not transmit Bearing to Waypoint, Cross Track Error and VMG.
Even the simplist handheld GPS's send this basic data now a days and providing the GGA,GSV GSA and
RMC are disabled still conform to the max 2 secound update requirement.
One of the more interesting challanges for your Robot navigation will be when you have to dodge an object. Implementing
the methods to get back on track will require heeps of arithmatic with the limited sentences available to you.
Good luck.
Ron
Post Edited (Ron Sutcliffe) : 4/9/2008 2:24:44 PM GMT
I do not use any floating point at all. There is an internal format that keeps locations in meters(yards) relative to the starting point.
of course you will run into problems if you go over 65000 meters from the starting point, and I never bothered to calculate the reverse heading(just display it)
Perry
P.S.
The circuit cellar examples seem to ignore the concept of the ellipsoid, the earth is not a sphere.
here is another link that discusses various geographic algorithms
www.mentorsoftwareinc.com/cc/gistips/TIPSarch.HTM
Post Edited (Perry) : 4/10/2008 6:43:10 PM GMT