]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navrecord.cxx
TACAN/mobile navaid cleanup and improvements.
[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
36
37 #include <Navaids/navrecord.hxx>
38 #include <Navaids/navdb.hxx>
39 #include <Airports/runways.hxx>
40 #include <Airports/airport.hxx>
41 #include <Airports/xmlloader.hxx>
42 #include <Main/fg_props.hxx>
43
44 FGNavRecord::FGNavRecord(PositionedID aGuid, Type aTy, const std::string& aIdent,
45   const std::string& aName, const SGGeod& aPos,
46   int aFreq, int aRange, double aMultiuse, PositionedID aRunway) :
47   FGPositioned(aGuid, aTy, aIdent, aPos),
48   freq(aFreq),
49   range(aRange),
50   multiuse(aMultiuse),
51   mName(aName),
52   mRunway(aRunway),
53   mColocated(0),
54   serviceable(true)
55
56 }
57
58 FGRunwayRef FGNavRecord::runway() const
59 {
60   return loadById<FGRunway>(mRunway);
61 }
62
63 double FGNavRecord::localizerWidth() const
64 {
65   if (!mRunway) {
66     return 6.0;
67   }
68   
69   FGRunway* rwy = runway();
70   SGVec3d thresholdCart(SGVec3d::fromGeod(rwy->threshold()));
71   double axisLength = dist(cart(), thresholdCart);
72   double landingLength = dist(thresholdCart, SGVec3d::fromGeod(rwy->end()));
73   
74 // Reference: http://dcaa.slv.dk:8000/icaodocs/
75 // ICAO standard width at threshold is 210 m = 689 feet = approx 700 feet.
76 // ICAO 3.1.1 half course = DDM = 0.0775
77 // ICAO 3.1.3.7.1 Sensitivity 0.00145 DDM/m at threshold
78 //  implies peg-to-peg of 214 m ... we will stick with 210.
79 // ICAO 3.1.3.7.1 "Course sector angle shall not exceed 6 degrees."
80               
81 // Very short runway:  less than 1200 m (4000 ft) landing length:
82   if (landingLength < 1200.0) {
83 // ICAO fudges localizer sensitivity for very short runways.
84 // This produces a non-monotonic sensitivity-versus length relation.
85     axisLength += 1050.0;
86   }
87
88 // Example: very short: San Diego   KMYF (Montgomery Field) ILS RWY 28R
89 // Example: short:      Tom's River KMJX (Robert J. Miller) ILS RWY 6
90 // Example: very long:  Denver      KDEN (Denver)           ILS RWY 16R
91   double raw_width = 210.0 / axisLength * SGD_RADIANS_TO_DEGREES;
92   return raw_width < 6.0? raw_width : 6.0;
93
94 }
95
96 bool FGNavRecord::hasDME()
97 {
98   return (mColocated > 0);
99 }
100
101 void FGNavRecord::setColocatedDME(PositionedID other)
102 {
103   mColocated = other;
104 }
105
106 void FGNavRecord::updateFromXML(const SGGeod& geod, double heading)
107 {
108     modifyPosition(geod);
109     multiuse = heading;
110 }
111
112 //------------------------------------------------------------------------------
113 FGMobileNavRecord::FGMobileNavRecord( PositionedID aGuid,
114                                       Type type,
115                                       const std::string& ident,
116                                       const std::string& name,
117                                       const SGGeod& aPos,
118                                       int freq,
119                                       int range,
120                                       double multiuse,
121                                       PositionedID aRunway ):
122   FGNavRecord(aGuid, type, ident, name, aPos, freq, range, multiuse, aRunway)
123 {
124
125 }
126
127 //------------------------------------------------------------------------------
128 const SGGeod& FGMobileNavRecord::geod() const
129 {
130   const_cast<FGMobileNavRecord*>(this)->updatePos();
131   return FGNavRecord::geod();
132 }
133
134 //------------------------------------------------------------------------------
135 const SGVec3d& FGMobileNavRecord::cart() const
136 {
137   const_cast<FGMobileNavRecord*>(this)->updatePos();
138   return FGNavRecord::cart();
139 }
140
141 //------------------------------------------------------------------------------
142 void FGMobileNavRecord::updatePos()
143 {
144   SGTimeStamp now = SGTimeStamp::now();
145   if( (now - _last_position_update).toSecs() < 1 )
146     return;
147   _last_position_update = now;
148
149   SGPropertyNode* ai_branch = fgGetNode("ai/models");
150   if( !ai_branch )
151   {
152     SG_LOG( SG_NAVAID,
153             SG_INFO,
154             "Can not update mobile navaid position (no ai/models branch)" );
155     return;
156   }
157
158   serviceable = true;
159   const std::string& nav_name = name();
160
161   // Try any aircraft carriers first
162   simgear::PropertyList carrier = ai_branch->getChildren("carrier");
163   for(size_t i = 0; i < carrier.size(); ++i)
164   {
165     const std::string carrier_name = carrier[i]->getStringValue("name");
166
167     if(    carrier_name.empty()
168         || nav_name.find(carrier_name) == std::string::npos )
169       continue;
170
171     modifyPosition(SGGeod::fromDegFt(
172       carrier[i]->getDoubleValue("position/longitude-deg"),
173       carrier[i]->getDoubleValue("position/latitude-deg"),
174       get_elev_ft()
175     ));
176     return;
177   }
178
179   // Now the tankers
180   const std::string tanker_branches[] = {
181     // AI tankers
182     "tanker",
183     // And finally mp tankers
184     "multiplayer"
185   };
186
187   for(size_t i = 0; i < sizeof(tanker_branches)/sizeof(tanker_branches[0]); ++i)
188   {
189     simgear::PropertyList tanker = ai_branch->getChildren(tanker_branches[i]);
190     for(size_t j = 0; j < tanker.size(); ++j)
191     {
192       const std::string callsign = tanker[j]->getStringValue("callsign");
193
194       if(    callsign.empty()
195           || nav_name.find(callsign) == std::string::npos )
196         continue;
197
198       modifyPosition(SGGeod::fromDegFt(
199         tanker[j]->getDoubleValue("position/longitude-deg"),
200         tanker[j]->getDoubleValue("position/latitude-deg"),
201         tanker[j]->getDoubleValue("position/altitude-ft")
202       ));
203       return;
204     }
205   }
206
207   // If no match was found set 'invalid' position (lat = lon = alt = 0)
208   modifyPosition(SGGeod());
209
210   // It's mobile but we do not know where it is...
211   serviceable = false;
212 }
213
214 FGTACANRecord::FGTACANRecord(void) :
215     channel(""),
216     freq(0)
217     
218 {
219 }
220
221 std::istream&
222 operator >> ( std::istream& in, FGTACANRecord& n )
223 {
224     in >> n.channel >> n.freq ;
225     //getline( in, n.name );
226
227     return in;
228 }