Jump to content
Sign in to follow this  
dragonflightdesign

Great Circle Calcbearing - What Am I Doing Wrong?

Recommended Posts

I knew I'd be back... I've tried a number of different formulae and methods for calculating the bearing required for a great circle route and all of them return the same answer, but NONE of them agree with FS. The code below is just the last iteration in a long line of frustration - like all the others I've tried, it gives a bearing of 318 degrees from KORD to 3CK but FS' flightplanner insists that it's actually 310 degrees. Why...? What am I not taking into account?

 

Ah - the reason for the six digit lon and five digit lat is that this is for an older INS.

 

-Dai

//---------------------------------------------------------------------------
// Bearing to next waypoint
//---------------------------------------------------------------------------
static float calcBearing(double ilat1,double ilon1,double ilat2,double ilon2,double dist)
{
// ilat1 = N415920  KORD
// ilon1 = W0875580 KORD
// ilat2 = N421220  3CK
// ilon2 = W0881920 3CK
// dist = 43km

    double tc,d;
    double lat1,lon1,lat2,lon2,x,y;

// East is negative in this iteration
  if(ilat1<0)ilat1=abs(ilat1);
  else ilat1=ilat1-(ilat1*2);
  if(ilat2<0)ilat2=abs(ilat2);
  else ilat2=ilat2-(ilat2*2);

// Convert to radians
    lat1=ilat1*PI/180;
    lat2=ilat2*PI/180;
    lon1=ilon1*PI/180;
    lon2=ilon2*PI/180;

    d=dist/EARTH_RADIUS;           // ER = 6372722.000
    x=sin(lat2)-sin(lat1)*cos(d);
    y=sin(d)*cos(lat1);
    if (fabs(y)>0.00000000001)
    {
        x=x/y;
        if (x<-1) x=-1;
        if (x>1) x=1;
        tc=acos(x);
        if (sin(lon2-lon1)>=0)tc=2*PI-tc;
    }
    else
    {
        if (lat1>=0)tc=PI;
        else tc=0;
    }
    tc*=180/PI;
    while (tc>=360) tc-=360;
    while (tc<0) tc+=360;
    return (float)(tc);  // Gives 318 - FS says it should be 310
}

Share this post


Link to post
Share on other sites

Dai,

 

This is what I used.  I get a bearing of 310.1° from Gate B16 at KORD to 3CK.

What immediately  strikes me is that you are not adjusting for magnetic variation.

BOOL FSAPI RangeAndBearing(double fLat1, double fLon1, double fLat2, double fLon2, double *fRng, double *fBrg)
{
	double lat1 = fLat1 * PI / 180;
	double lon1 = fLon1 * PI / 180;
	double lat2 = fLat2 * PI / 180;
	double lon2 = fLon2 * PI / 180;
	double bearing;

	if ( NULL != fRng )
		{
		double cos_aob = cos(lat1)*cos(lat2)*cos(lon2-lon1)+ sin(lat1)*sin(lat2);
		double aob = acos(cos_aob);
		*fRng = METER_TO_NAUTIC_MILE(aob * 6371)*1000;
		}
	else return NULL;

	if ( NULL != fBrg )
		{
		lookup_var (&MagVar);
		bearing = fmod(atan2(sin(lon2-lon1)*cos(lat2),cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1)),2*PI);
		bearing = bearing - (MagVar.var_value.n * PI / 180.);
		bearing = bearing *180 / PI;
		if ( bearing < 0 ) bearing = bearing + 360.;
		if ( bearing < 0 ) bearing = bearing + 360.;
		*fBrg = bearing;
		}
	else return NULL;

	return TRUE;
}

Share this post


Link to post
Share on other sites

I'm not sure what you are trying to do as I don't know that code, but 2 things come to mind as a pilot:

 

1: a great circle is not a constant track or bearing. If you fly a constant heading you are flying a small circle.

 

2: as the previous poster suggested, did you think about magnetic variation? Great and small circle tracks are calculated in true, so you need to convert to magnetic once you pc want to fly it.

Share this post


Link to post
Share on other sites

Mag variation... where's the head and wall smiley when you need it??? Two head and wall smileys. How did I miss that?? :blush:

 

I'm not sure what you are trying to do as I don't know that code, but 2 things come to mind as a pilot:

1: a great circle is not a constant track or bearing. If you fly a constant heading you are flying a small circle.

 

Effectively I'm calculating forward azimuth as the waypoints are (or should be) reasonably close together. Not so much a great circle heading as a series of short legs. This INS isn't capable of auto-updating the heading, unlike modern ones.

 

-Dai

Share this post


Link to post
Share on other sites

Doug -

 

Typo?

 

        if ( bearing < 0 ) bearing = bearing + 360;
        if ( bearing < 0 ) bearing = bearing + 360;

 

Should be

 

        if ( bearing < 0 ) bearing = bearing + 360;
        if ( bearing > 360 ) bearing = bearing - 360;

 

-Dai

Share this post


Link to post
Share on other sites

Hi Doug

 

Sorry to bother you again. I converted your pointer arithmetic to straight variables:

 

    lookup_var(&magvar);
    bearing=fmod(atan2(sin(lon2-lon1)*cos(lat2),cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1)),2*PI);
    bearing=bearing-(magvar.var_value.n*PI/180);
    bearing=bearing*180/PI;
    if(bearing<0)bearing=bearing+360;
    if(bearing>360)bearing=bearing-360;
    return bearing;

 

and the answer is 322 degrees. I have to have done something else really stupid but I can no longer see the wood for the trees.

 

-Dai

Share this post


Link to post
Share on other sites

Dai,

 

The two points you are using are so close together that slight discrepancies make a huge difference to the final result.

I had done my checking using FSX's origin point for 3CK, which is 42º 12.41'; -88º 19.38'.  I was using as the first point, various starting positions at KORD.

 

If I slew to 41º 59.2' -87º 55.8' and check the bearing to 42º 12.2' -88º 19.2', I get a value of 324.1º.

 

I also checked these airports, from runway 14R at KORD (41º 59.4', -87º 55.95')

CYYJ (48º 38.82', -123º 25.55') bearing is 301.1º

EGLL (51º 28.65', -0º 27.68') bearing is 51.7º

FAJS (-26º 8.02', 28º 14.54') bearing is 98.2º

All of these locations are retrieved through the FS GPS interface.

I'm guessing that with distances like those, your results will be pretty darned close to mine.

 

Doug

Share this post


Link to post
Share on other sites

Hi Doug

 

That looks like it - waypoints too close together.  Thank you. I rebuilt the INS flightplan with greater distances and over 153nm (shortest new leg) the error narrows to 3 degrees. At this point I'm sure that if the INS would take six digit lat and seven digit lon then the calculations would agree with the FSX flightplan. The reason for the original short legs was simply to test the timing on the INS flags - I had no idea that it would lead me to doubt much of what I'd coded!

 

The upside of all of this is that some of the notes regarding position updates in the INS manual now make far more sense than they did previously.

 

-Dai

Share this post


Link to post
Share on other sites

During the navigation following the great circle the bearing always changes To have an idea during an atlantic crossing from Europe to America at 50°N of latitude from a waypoint to the next one

(10° of longitude) the plane HDG changes against the precalculated Hdg 4°more at leg starting is coincident at middle leg and is 4° smaller at the lag end.  

Share this post


Link to post
Share on other sites

You give these coordinates in Post #1
 

// ilat1 = N415920  KORD
// ilon1 = W0875580 KORD

 

Airnav gives these:
 

41-58-45.6000N
41-58.760000N
41.97933339

087-54-26.6000W
087-54.443333W
-87.9073889

 

Are you using decimal deg or deg-min-sec or deg-min?

 

Also according to Google - For modern 64-bit floating-point numbers, the spherical law of cosines formula, given above, does not have serious rounding errors for distances larger than a few meters on the surface of the Earth


 

Share this post


Link to post
Share on other sites

have a look at :

http://williams.best.vwh.net

Anyway an INS to follow a great circle leg calculates every 24sec the initial True Course.

The initial True Course is the Tan of the arc  from the plane position to the arrival waypoint in the plane position.

You can understand that the plane Hdg commanded by the INS is changing every 24sec.

Share this post


Link to post
Share on other sites

deg-min-sec - read straight out of an FSX flightplan and rounded off to match the INS input.

 

But the sin/cos functions need decimal degs as their arguments.

decimal deg = deg + min/60 + sec/3600

So

// ilat1 = N415920  KORD

 gives

41 deg 59 min 20 sec = 41.98889 decimal deg

 

Airnav gives

41 deg 58 min + 45.6 sec = 41.97933339 decimal deg

 

Or am I missing something?

Share this post


Link to post
Share on other sites

have a look at :

http://williams.best.vwh.net

Anyway an INS to follow a great circle leg calculates every 24sec the initial True Course.

The initial True Course is the Tan of the arc  from the plane position to the arrival waypoint in the plane position.

You can understand that the plane Hdg commanded by the INS is changing every 24sec.

 

Ed Williams is already proven to be not accurate in his GC calculations (see post above). Now, I know my final calculations aren't going to be 100% accurate either (because of the age of the INS I'm trying to model) but I think that starting with a known inaccuracy to end with an inaccuracy is not a good idea. It will just make things worse.

 

 

But the sin/cos functions need decimal degs as their arguments.

decimal deg = deg + min/60 + sec/3600

So

 gives

41 deg 59 min 20 sec = 41.98889 decimal deg

 

Airnav gives

41 deg 58 min + 45.6 sec = 41.97933339 decimal deg

 

Or am I missing something?

 

Converted to radians first?

// Convert to radians
    lat1=ilat1*PI/180;
    lat2=ilat2*PI/180;
    lon1=ilon1*PI/180;
    lon2=ilon2*PI/180;

I'm on vacation for a week but I'll pick this up when I get back. Thanks to everyone for their input.

Share this post


Link to post
Share on other sites

Converted to radians first?

 

Yes, but that doesn't  account need to convert into decimal degs from degminsec units in the first case.

 

/ /Convert to radians

    lat1=ilat1*PI/180;

 

What numerical value are using for ilat1?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Tom Allensworth,
    Founder of AVSIM Online


  • Flight Simulation's Premier Resource!

    AVSIM is a free service to the flight simulation community. AVSIM is staffed completely by volunteers and all funds donated to AVSIM go directly back to supporting the community. Your donation here helps to pay our bandwidth costs, emergency funding, and other general costs that crop up from time to time. Thank you for your support!

    Click here for more information and to see all donations year to date.
×
×
  • Create New...