PDA

View Full Version : How do I make a formula from gps information.



tasmod
- 11th March 2014, 08:49
Hi all,

Apologies in advance but I posted this in GPS part of forum but it seems everyone hangs out here.

I'm trying to achieve code that will take gps information and convert it to Maidenhead Locator. A location system used in ham radio. i.e. GJ34wg

I have some idea but maths is not my strong point. I have written what I considered to be the process but pseudo code. Am I on the right lines ?(See end) I don't know how to deal with FP and negative maths here.

This is the relevant information to achieve the locator.



Longitude is always the first, followed by latitude, for each pair. For simplicity, let's assume that West and South are negative lat/long, as is a common convention. For example purposes, I'm going to use 32.123 W, 14.321 N. The key thing is to do the following.:
Longitude
1. Add 180 to the longitude, and take the integer value /20, and add one. Then figure out which letter of the alphabet that corresponds to, usually written in upper case. The example will be 147.877/20=7. Adding one will give the 8th letter of the alphabet, or G. Note 7.877 is remaining.
2. Take the remainder of what is left, and divide by 2, rounding down. This is the number, no conversion required. The example will give a value of 3. Note 1.877 is remaining.
3. Take the remainder that is left, and multiply by 12, and add one. Round down to the nearest integer.. This is the letter of the alphabet, usually written in lower case. The example gives a value of 22+1=23. This will be the letter w.
Latitude
1. Add 90 to the latitude, and take the integer value /10, and add one. Then figure out which letter of the alphabet that corresponds to, usually written in upper case. The example will be 104.321/10=10. Adding one will give the 8th letter of the alphabet, or J. Note 4.321 is remaining.
2. Take the remainder of what is left, and round down. This is the number, no conversion required. The example will give a value of 4. Note 0.321 is remaining.
3. Take the remainder that is left, and multiply by 24, and add one. Round down to the nearest integer.. This is the letter of the alphabet, usually written in lower case. The example gives a value of 7+1=8. This will be the letter g.
Putting them together by pairs, and alternating first longitude then latitude, gives the grid square for 32.123 W, 14.321 N to be locator GJ34wg.


I have the information from the GPS RMC string as :-




latdeg VAR BYTE 'degrees latitude
latmin VAR BYTE 'minutes latitude
NS VAR BYTE 'north or south
londeg VAR BYTE 'degrees longitude
lonmin VAR BYTE 'minutes longitude
EO VAR BYTE 'east or west



My idea of the process is something like this :-




locator:
lonloc1=londeg + lonmin + 180
lonloc2=lonloc1/20
lonloc3=lonloc1//20
maid1=lonloc2+1
lookup maid1 ; first main letter - prob not need lookup

lonloc4=lonloc3//2
maid3=lonloc3/2 ;first number


maid5=lonloc4*12+1
lookup maid5 ; 1st 2nd lower case letter by convention



latloc1=latdeg + latmin + 90
latloc2=latloc1/10+1
latloc3=latloc1//10
maid1=latloc2+1
lookup maid2 ; second main letter


maid4=latloc3 ; second number


maid6=latloc3*24+1
lookup maid6 ; 2nd 2nd lower case letter by convention

maidenhead=maid1, maid2, maid3, maid4, maid5, maid6

Amoque
- 11th March 2014, 11:50
I did not check every step, but it certainly seems you are on track - the math seems simple enough when broken into steps and, presuming you follow syntax rules, it should work fine. A few notes:

As you are working with byte values, it will likely be easiest to manipulate maidenhead in an array - each character in its own element as: MH[0] .. MH[5]. In this way you may loop

EL = 0 to 5
MH[EL] 'Do something with MH[EL] - serial out...
Next EL

If you want to "Put them together in pairs", you can assign them to a word variable with .HIGHBYTE, .LOWBYTE. Perhaps this will work well, if Highbyte is always a letter (or rather the ASCII value associated) and Lowbyte is always a number. Take a look at ARRAYWRITE and ARRAYREAD also.

Couple of other notes:
Capital letters are related (value wise) to lowercase letters, one LOOKUP will do- Check an ASCII table, but I think adding 40 is lowercase.
You may assign results to a variable in the formula as MH[0] = MH[0] + 5. In this way (sometimes) the code is clearer.

All I got - nothing earthshaking, just some thoughts as I consider how I might do it. Oh, almost forgot... Good luck!