]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navrecord.cxx
Directly associate runways objects with their ILS navrecord (if one exists)
[flightgear.git] / src / Navaids / navrecord.cxx
1 // navrecord.cxx -- generic vor/dme/ndb class
2 //
3 // Written by Curtis Olson, started May 2004.
4 //
5 // Copyright (C) 2004  Curtis L. Olson - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24   #include "config.h"
25 #endif
26
27 #include <istream>
28
29 #include <simgear/misc/sgstream.hxx>
30 #include <simgear/structure/exception.hxx>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/sg_inlines.h>
33
34 #include <Navaids/navrecord.hxx>
35 #include <Navaids/navdb.hxx>
36 #include <Airports/runways.hxx>
37 #include <Main/fg_props.hxx>
38
39 FGNavRecord::FGNavRecord(Type aTy, const std::string& aIdent, 
40   const std::string& aName, const SGGeod& aPos,
41   int aFreq, int aRange, double aMultiuse) :
42   FGPositioned(aTy, aIdent, aPos),
43   freq(aFreq),
44   range(aRange),
45   multiuse(aMultiuse),
46   _name(aName),
47   mRunway(NULL),
48   serviceable(true),
49   trans_ident(aIdent)
50
51   initAirportRelation();
52
53   // Ranges are included with the latest data format, no need to
54   // assign our own defaults, unless the range is not set for some
55   // reason.
56   if (range < 0.1) {
57     SG_LOG(SG_GENERAL, SG_WARN, "navaid " << ident() << " has no range set, using defaults");
58     switch (type()) {
59     case NDB:
60     case VOR:
61       range = FG_NAV_DEFAULT_RANGE;
62       break;
63     
64     case LOC:
65     case ILS:
66     case GS:
67       range = FG_LOC_DEFAULT_RANGE;
68       break;
69       
70     case DME:
71       range = FG_DME_DEFAULT_RANGE;
72       break;
73     
74     default:
75       range = FG_LOC_DEFAULT_RANGE;
76     }
77   }
78   
79 }
80
81 void FGNavRecord::initAirportRelation()
82 {
83   if ((type() < ILS) || (type() > GS)) {
84     return; // not airport-located
85   }
86   
87   mRunway = getRunwayFromName(_name);    
88   // fudge elevation to the runway elevation if it's not specified
89   if (fabs(elevation()) < 0.01) {
90     mPosition.setElevationFt(mRunway->elevation());
91   }
92   
93   if (type() == ILS) {
94     mRunway->setILS(this);
95   }
96   
97   // align localizers with their runway
98   if ((type() == ILS) || (type() == LOC)) {
99     if (!fgGetBool("/sim/navdb/localizers/auto-align", true)) {
100       return;
101     }
102     
103    double threshold 
104      = fgGetDouble( "/sim/navdb/localizers/auto-align-threshold-deg", 5.0 );
105     alignLocaliserWithRunway(threshold);
106   }
107 }
108
109 void FGNavRecord::alignLocaliserWithRunway(double aThreshold)
110 {
111 // find the distance from the threshold to the localizer
112   SGGeod runwayThreshold(mRunway->threshold());
113   double dist, az1, az2;
114   SGGeodesy::inverse(mPosition, runwayThreshold, az1, az2, dist);
115
116 // back project that distance along the runway center line
117   SGGeod newPos = mRunway->pointOnCenterline(dist);
118
119   double hdg_diff = get_multiuse() - mRunway->headingDeg();
120   SG_NORMALIZE_RANGE(hdg_diff, -180.0, 180.0);
121
122   if ( fabs(hdg_diff) <= aThreshold ) {
123     mPosition = newPos;
124     set_multiuse( mRunway->headingDeg() );
125   } else {
126     SG_LOG(SG_GENERAL, SG_WARN, "localizer:" << ident() << ", aligning with runway " 
127       << mRunway->ident() << " exceeded heading threshold");
128   }
129 }
130
131 FGTACANRecord::FGTACANRecord(void) :
132     channel(""),
133     freq(0)
134     
135 {
136 }
137
138 std::istream&
139 operator >> ( std::istream& in, FGTACANRecord& n )
140 {
141     in >> n.channel >> n.freq ;
142     //getline( in, n.name );
143
144     return in;
145 }