]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navrecord.cxx
041c12f1a663fc84aef85f6b021682eedb01c089
[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/misc/sg_path.hxx>
31 #include <simgear/structure/exception.hxx>
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/sg_inlines.h>
34 #include <simgear/props/props.hxx>
35 #include <simgear/props/props_io.hxx>
36
37 #include <Navaids/navrecord.hxx>
38 #include <Navaids/navdb.hxx>
39 #include <Airports/runways.hxx>
40 #include <Airports/simple.hxx>
41 #include <Airports/xmlloader.hxx>
42
43 #include <Main/fg_props.hxx>
44
45 FGNavRecord::FGNavRecord(Type aTy, const std::string& aIdent, 
46   const std::string& aName, const SGGeod& aPos,
47   int aFreq, int aRange, double aMultiuse) :
48   FGPositioned(aTy, aIdent, aPos),
49   freq(aFreq),
50   range(aRange),
51   multiuse(aMultiuse),
52   _name(aName),
53   mRunway(NULL),
54   serviceable(true),
55   trans_ident(aIdent)
56
57   initAirportRelation();
58
59   // Ranges are included with the latest data format, no need to
60   // assign our own defaults, unless the range is not set for some
61   // reason.
62   if (range < 0.1) {
63     SG_LOG(SG_GENERAL, SG_WARN, "navaid " << ident() << " has no range set, using defaults");
64     switch (type()) {
65     case NDB:
66     case VOR:
67       range = FG_NAV_DEFAULT_RANGE;
68       break;
69     
70     case LOC:
71     case ILS:
72     case GS:
73       range = FG_LOC_DEFAULT_RANGE;
74       break;
75       
76     case DME:
77       range = FG_DME_DEFAULT_RANGE;
78       break;
79     
80     default:
81       range = FG_LOC_DEFAULT_RANGE;
82     }
83   }
84   
85 }
86
87 void FGNavRecord::initAirportRelation()
88 {
89   if ((type() < ILS) || (type() > GS)) {
90     return; // not airport-located
91   }
92   
93   mRunway = getRunwayFromName(_name);  
94   if (!mRunway) {
95     return;
96   }
97   
98   if (type() != GS) {
99     readAirportSceneryData();
100   }
101         
102   // fudge elevation to the runway elevation if it's not specified
103   if (fabs(elevation()) < 0.01) {
104     mPosition.setElevationFt(mRunway->elevation());
105   }
106   
107   if (type() == ILS || type() == LOC) {
108     mRunway->setILS(this);
109   }
110   
111   // align localizers with their runway
112   if ((type() == ILS) || (type() == LOC)) {
113     if (!fgGetBool("/sim/navdb/localizers/auto-align", true)) {
114       return;
115     }
116     
117    double threshold 
118      = fgGetDouble( "/sim/navdb/localizers/auto-align-threshold-deg", 5.0 );
119     alignLocaliserWithRunway(threshold);
120   }
121 }
122
123 void FGNavRecord::readAirportSceneryData()
124 {
125   // allow users to disable the scenery data in the short-term
126   // longer term, this option can probably disappear
127   if (!fgGetBool("/sim/use-scenery-airport-data")) {
128     return; 
129   }
130   
131   SGPath path;
132   SGPropertyNode_ptr rootNode = new SGPropertyNode;
133   if (!XMLLoader::findAirportData(mRunway->airport()->ident(), "ils", path)) {
134     return;
135   }
136   
137   readProperties(path.str(), rootNode);
138   SGPropertyNode* runwayNode, *ilsNode;
139   for (int i=0; (runwayNode = rootNode->getChild("runway", i)) != NULL; ++i) {
140     for (int j=0; (ilsNode = runwayNode->getChild("ils", j)) != NULL; ++j) {
141       // must match on both nav-ident and runway ident, to support the following:
142       // - runways with multiple distinct ILS installations (KEWD, for example)
143       // - runways where both ends share the same nav ident (LFAT, for example)
144       if ((ilsNode->getStringValue("nav-id") == ident()) &&
145           (ilsNode->getStringValue("rwy") == mRunway->ident())) {
146         processSceneryILS(ilsNode);
147         return;
148       }
149     } // of ILS iteration
150   } // of runway iteration
151 }
152
153 void FGNavRecord::processSceneryILS(SGPropertyNode* aILSNode)
154 {
155   double hdgDeg = aILSNode->getDoubleValue("hdg-deg"),
156     lon = aILSNode->getDoubleValue("lon"),
157     lat = aILSNode->getDoubleValue("lat"),
158     elevM = aILSNode->getDoubleValue("elev-m");
159     
160   mPosition = SGGeod::fromDegM(lon, lat, elevM);
161   multiuse = hdgDeg;
162 }
163
164 void FGNavRecord::alignLocaliserWithRunway(double aThreshold)
165 {
166 // find the distance from the threshold to the localizer
167   double dist = SGGeodesy::distanceM(mPosition, mRunway->threshold());
168
169 // back project that distance along the runway center line
170   SGGeod newPos = mRunway->pointOnCenterline(dist);
171
172   double hdg_diff = get_multiuse() - mRunway->headingDeg();
173   SG_NORMALIZE_RANGE(hdg_diff, -180.0, 180.0);
174
175   if ( fabs(hdg_diff) <= aThreshold ) {
176     mPosition = SGGeod::fromGeodFt(newPos, mPosition.getElevationFt());
177     set_multiuse( mRunway->headingDeg() );
178   } else {
179     SG_LOG(SG_GENERAL, SG_DEBUG, "localizer:" << ident() << ", aligning with runway " 
180       << mRunway->ident() << " exceeded heading threshold");
181   }
182 }
183
184 double FGNavRecord::localizerWidth() const
185 {
186   if (!mRunway) {
187     return 6.0;
188   }
189   
190   SGVec3d thresholdCart(SGVec3d::fromGeod(mRunway->threshold()));
191   double axisLength = dist(cart(), thresholdCart);
192   double landingLength = dist(thresholdCart, SGVec3d::fromGeod(mRunway->end()));
193   
194 // Reference: http://dcaa.slv.dk:8000/icaodocs/
195 // ICAO standard width at threshold is 210 m = 689 feet = approx 700 feet.
196 // ICAO 3.1.1 half course = DDM = 0.0775
197 // ICAO 3.1.3.7.1 Sensitivity 0.00145 DDM/m at threshold
198 //  implies peg-to-peg of 214 m ... we will stick with 210.
199 // ICAO 3.1.3.7.1 "Course sector angle shall not exceed 6 degrees."
200               
201 // Very short runway:  less than 1200 m (4000 ft) landing length:
202   if (landingLength < 1200.0) {
203 // ICAO fudges localizer sensitivity for very short runways.
204 // This produces a non-monotonic sensitivity-versus length relation.
205     axisLength += 1050.0;
206   }
207
208 // Example: very short: San Diego   KMYF (Montgomery Field) ILS RWY 28R
209 // Example: short:      Tom's River KMJX (Robert J. Miller) ILS RWY 6
210 // Example: very long:  Denver      KDEN (Denver)           ILS RWY 16R
211   double raw_width = 210.0 / axisLength * SGD_RADIANS_TO_DEGREES;
212   return raw_width < 6.0? raw_width : 6.0;
213
214 }
215
216 FGTACANRecord::FGTACANRecord(void) :
217     channel(""),
218     freq(0)
219     
220 {
221 }
222
223 std::istream&
224 operator >> ( std::istream& in, FGTACANRecord& n )
225 {
226     in >> n.channel >> n.freq ;
227     //getline( in, n.name );
228
229     return in;
230 }