Calculate Waypoint

D396

Senior Member
This isn't necessarily picaxe related but anyway. I have a Latitude and Longitude. I also have a distance and a heading (0-360). I want to put those into a function and get another set of latitude and longitude that are the distance and heading from the beginning one. I just want a formula that I will adopt to picaxe or excel code because I will assume it uses trig. Has anybody done this before? What formula did you use. Thanks.
 

srnet

Senior Member
This isn't necessarily picaxe related but anyway. I have a Latitude and Longitude. I also have a distance and a heading (0-360). I want to put those into a function and get another set of latitude and longitude that are the distance and heading from the beginning one. I just want a formula that I will adopt to picaxe or excel code because I will assume it uses trig. Has anybody done this before? What formula did you use. Thanks.
I have been building some code to do just that, using the NMEA string from a GPS to read in a home position then calculating the distance and direction to the GPS current position.

I am however using a 18F25k22 and Microbasic Pro, the calculation needs trig and floating point numbers, which I suspected were beyond the scope of a PICAXE.

Anyway the code looks like this;

Code:
sub procedure calclocation()
'La1 and Lo1 are Lat & Long (in decimal degrees) of home position
'La2 and La1 are Lat & Long of current position
'First, convert to radians
La1=La1*pi/180
Lo1=Lo1*pi/180

La2=La2*pi/180
Lo2=Lo2*pi/180
'calculate distance using great circle formula
Dist=2*Asin(sqrt(sq(sin((La1-La2)/2))+cos(La1)*cos(La2)*sq(Sin((Lo1-Lo2)/2))))
'calculate direction
Direct=acos((sin(La2)-sin(La1)*cos(Dist))/(sin(Dist)*cos(La1)))
if sin(Lo2-Lo1)>0 then
Direct=2*pi-Direct
end if
'convert results
Direct=(180/pi)*Direct 'convert to degrees
Dist=3982*Dist ' convert to miles
Dist=dist*1.6 ' convert to KM
dist = dist * 1000 'convert to Metres
end sub
It seems to be accurate to a couple of metres in 500M.

I think I have some Excel code somewhere too, I was using it to check the accuracy of the PIC calculation.
 
Last edited:

srnet

Senior Member
Here we go, formula for Excel, assuming La1, Lo1, la2 and lo2 are in radians, the distance in kM between two points is;

=ACOS(SIN(lat1)*SIN(lat2)+COS(lat1)*COS(lat2)*COS(long2-long1))*6371

and the direction (in radians) is;

=ATAN2(COS(lat1)*SIN(lat2)-SIN(lat1)*COS(lat2)*COS(long2-long1);SIN(long2-long1)*COS(lat2))
 

papaof2

Senior Member
If you want floating point with a PICAXE, the uMFPU chip can do it.

The uMFPU talks to the PICAXE via I2C and it can receive and parse the NMEA sentences from a GPS - no problems with the PICAXE being tied up reading serial in. The uMFPU is fast, running at about 20MHz.

I've seen it running but have no code to offer (under an NDA).

John
 

D396

Senior Member
Sorry about the confusion. I already have the distance and heading between two points. What I want to do is have one point and then be able to calculate another using the first point and the distance and heading from that point. I have found online calculators but no formulas.
 

hippy

Technical Support
Staff member
I cannot follow exactly what it is you actually have and what you want to calculate - perhaps show us a picture; it will likely be worth a thousand words :)
 

Svejk

Senior Member
You may have to take into consideration the curvature of the Earth, depending how far the points are. Look for great circle distance calculation.

For short distances, ie considering the plane Mercator projection:

Destination Latitude = Distance * sin(heading) + Actual Latitude

That is true as the the distance between 2 points situated at same longitude does not change with longitude or latitude.

For longitude things are a little bit more complicated as the distance between 2 points situated at the same latitude changes with latitude. That is, at Equator 1 minute of longitude equals one nautical mile, while at 45º North 1 minute of longitude is smaller. To understand this, consider that the Greenwich meridian is perpendicular to Equator. So is the 90º E meridian, but those are meeting at North Pole.

Perhaps the best approach for Picaxe computing is to multiply the result with a value obtained from a lookup table depending on the actual latitude:

Destination Longitude = (Distance * cos(heading) + actual Longitude)*Latitude Ratio

Needles to say, care must be taken for crossing points (East to West, North to South and the other way around)
 

D396

Senior Member
Thanks. Now just have to figure out what it all means but that is what I wanted. It will only have to be for a max of 200M so I dont know if I will have to compensate for the earths curve. Where wold I find the Latitude Ratio for my location?
 

Svejk

Senior Member
Short distances is a few miles, about a cable is ok.

To do a lookup table for ratios go to one great circle distance calculators found all over Internet and calculate the distance in miles for one degree of longitude for each degree of latitude:

Something like this:

0° 60 miles 1
10º 58 miles 0.967


etc.

Edit 1:
Or for your location do one degree of longitude at your location compared with one degree of longitude at Equator.

If you do the first please post it here for further reference.

Edit 2:
It may be a pain: for short distances you may have to consider the slope of direction in Z plane. That is 100 m at 20° uphill will give slightly different results than 100 m straight.
 
Last edited:

Svejk

Senior Member
at "Calculate the distance between two addresses"
Enter for Location 1: "0, 0" and for Location 2: "0, 1". This is 1 degree at Equator. Record it.

Then enter "Your latitude, 0" for Location 1 and "Your latitude, 1" for Location 2. this is 1 degree at your latitude. Record it.

The ratio is second over first. EG

at Equator: 69.171 mi
at 45°: 48.993 mi

Ratio is 48.993/69.171 = 0.708

Edit: for results as described in post #9 all distances have to be converted in nautical miles. 1 NM = 1 minute latitude. 60 minutes = 1 degree.
 
Last edited:

D396

Senior Member
Ratio at my latitude (38) is .7783. So according to the formula you provided
destination latitude = distance * sin(heading) + start Latitude
destination longitude = (Distance * cos(heading) + start Longitude)*.7783
Will this work for distances in cm?
Does all of this sound right? I just want to make sure I am getting it.
 
Last edited:

Svejk

Senior Member
Oopsy, I've made a mistake*: If you travel 18.52 km NE (45º) from 38º N, 0°E then you should end with about 38º 7.07'N and 0° 8.9'E

The correct ratio is the other way around 1/0.7783=1.2849


-----------------------------------
* I'm a bit rusty, I haven't played with those in the last 15 years.
 
Last edited:

D396

Senior Member
I found some code (arduino) that uses these formulas.
Code:
if (com >= 0 && com <=90) {Q=1;} // IDENTIFY THE QUADRANTS
if (com > 90 && com <=180) {Q=2;}
if (com > 180 && com <=270) {Q=3;}
if (com > 270 && com <=360) {Q=4;}


if (Q==1) {dx=(dis*(cos((com)*3.14159/180)));} // CALCULATE THE X & Y OFFSET FROM HOME
if (Q==1) {dy=(dis*(sin((com)*3.14159/180)));} // Degrees must be converted to radians for sin & cos function


if (Q==2) {dx=(dis*(sin((com-90)*3.14159/180)));}
if (Q==2) {dy=-(dis*(cos((com-90)*3.14159/180)));}


if (Q==3) {dx=-(dis*(sin((270-com)*3.14159/180)));}
if (Q==3) {dy=-(dis*(cos((270-com)*3.14159/180)));}


if (Q==4) {dx=-(dis*(cos((360-com)*3.14159/180)));}
if (Q==4) {dy=(dis*(sin((360-com)*3.14159/180)));}
{Serial.print("Compas: ");Serial.println(com);}
{Serial.print("dx= ");Serial.println(dx);}
{Serial.print("dy= ");Serial.println(dy);}

{dxlat=(dx*dxf)/1000; //START OF (APPLY SCALING FACTOR TO X&Y OFFSET) LOOP
dylon=(dy*dyf)/1000;}
{Serial.print("dxlat= ");Serial.println(dxlat,7);}
{Serial.print("dylon= ");Serial.println(dylon,7);} //END OF (APPLY SCALING FACTOR TO X&Y OFFSET) LOOP
It reads the compass heading and then changes the heading based on the reading??? why does it subtracted the reading from 360 or 270. this is not the hole code by the way the rest of the formula is later.
Thanks for your help and time.
 

Pauldesign

Senior Member
why does it subtracted the reading from 360 or 270. this is not the hole code by the way the rest of the formula is later.
I'll suggest you do a bit of research on Spherical laws of Cosine and it's application; as doing that might help u a lot esp if u're not mathematically inclined.
 

D396

Senior Member
OK. Thanks. but that code should work correct. All I need to do is provide it with a compass heading.
 

hippy

Technical Support
Staff member
I'd say forget the complexities of curvature and slope for now and understand the basics first. Also, what distances are we talking here, a few metres or many kilometres; I'm not sure what "200M" means, metres or miles.

Having the coordinates of one point, a bearing and distance to another, the coordinates of that other point can be determined by simple sine and cosine maths.

It may help if you describe your application. If firing a projectile a distance in a direction then you may need to take into account curvature and so on to know where it will land exactly. For travelling from point A to B that's usually the inverse, creating a direction and distance from where you are to where you want to be. Curvature, slope, wind and sea current can all be ignored so long as you don't blindly follow the first heading calculated but keep reassessing the heading needed. As you approach the target the error will reduce.

Above all else, you need to understand the maths before worrying over the code implementation of that.
 

D396

Senior Member
200 Meters. The application is a robot where it figures out the destination and goes there. I will put it on the ground and have a potentiometer and a button. I will point it the direction I want it to go and use the potentiometer to get distance (already done). My maths are basic simply because I am still in algebra 2 (8th grade) so I am going off what my dad says and internet tutorials.
 

D396

Senior Member
Almost got it. When I use this code (arduino again simply because it can do sin and cos without a separate chip). I get a destination the right distance but the wrong heading from my starting point.
Serial.println( 38.982623 + ((1*.000539956803) * (cos(0) * PI/180)) , 7);
Serial.println( -77.307674 + ((1*.000539956803) * (sin(0) * PI/180))* 1.2849, 7);
Any ideas. I did not really see how the spherical law of cosines has anything today with this.
 

MartinM57

Moderator
I've no idea what formula you are trying to model.
I've no idea where the values that you have put into the equations come from.
...so I have no ideas ;)
 

D396

Senior Member
Sorry first number (38.982623) is the latitude second number is (1*.000539956803) the distance in meters (1) converted to Nautical miles for the formula. The next set is the heading in Radians (0).
Same for the next equation except also use latitude offset.
 

D396

Senior Member
Turns out I don't need that formula the one provided previosly works. For some reason outputting it over serial screwed up the value so when I plugged it back into a online calculator the heading was off. I will get back to you guys on any progress made.
 
Top