1 // navrecord.cxx -- generic vor/dme/ndb class
3 // Written by Curtis Olson, started May 2004.
5 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
29 #include <simgear/misc/sgstream.hxx>
30 #include <simgear/structure/exception.hxx>
31 #include <simgear/misc/strutils.hxx>
32 #include <simgear/debug/logstream.hxx>
34 #include "Navaids/navrecord.hxx"
35 #include "Airports/simple.hxx"
36 #include "Airports/runways.hxx"
37 #include "Main/fg_props.hxx"
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),
50 initAirportRelation();
52 // Ranges are included with the latest data format, no need to
53 // assign our own defaults, unless the range is not set for some
56 SG_LOG(SG_GENERAL, SG_WARN, "navaid " << ident() << " has no range set, using defaults");
60 range = FG_NAV_DEFAULT_RANGE;
66 range = FG_LOC_DEFAULT_RANGE;
70 range = FG_DME_DEFAULT_RANGE;
74 range = FG_LOC_DEFAULT_RANGE;
80 SGVec3d FGNavRecord::get_cart() const
82 return SGVec3d::fromGeod(geod());
86 static FGPositioned::Type
87 mapRobinTypeToFGPType(int aTy)
91 case 2: return FGPositioned::NDB;
92 case 3: return FGPositioned::VOR;
93 case 4: return FGPositioned::LOC;
94 case 5: return FGPositioned::ILS;
95 case 6: return FGPositioned::GS;
96 case 7: return FGPositioned::OM;
97 case 8: return FGPositioned::MM;
98 case 9: return FGPositioned::IM;
100 case 13: return FGPositioned::DME;
101 case 99: return FGPositioned::INVALID; // end-of-file code
103 throw sg_range_exception("Got a nav.dat type we don't recgonize", "FGNavRecord::createFromStream");
107 FGNavRecord* FGNavRecord::createFromStream(std::istream& aStream)
112 return NULL; // happens with, eg, carrier_nav.dat
115 Type type = mapRobinTypeToFGPType(rawType);
116 if (type == INVALID) return NULL;
118 double lat, lon, elev_ft, multiuse;
120 std::string apt_id, name, ident;
121 aStream >> lat >> lon >> elev_ft >> freq >> range >> multiuse >> ident;
122 getline(aStream, name);
124 // silently multiply adf frequencies by 100 so that adf
125 // vs. nav/loc frequency lookups can use the same code.
130 FGNavRecord* result = new FGNavRecord(type, ident,
131 simgear::strutils::strip(name), SGGeod::fromDegFt(lon, lat, elev_ft),
132 freq, range, multiuse);
137 void FGNavRecord::initAirportRelation()
139 if (type() < ILS || type() > IM) {
140 return; // not airport-located
143 vector<string> parts = simgear::strutils::split(name);
145 const FGAirport* apt = fgFindAirportID(apt_id);
148 SG_LOG(SG_GENERAL, SG_WARN, "navaid " << ident() << " associated with bogus airport ID:" << apt_id);
152 // fudge elevation to the field elevation if it's not specified
153 if (fabs(elevation()) < 0.01) {
154 mPosition.setElevationFt(apt->getElevation());
157 // align localizers with their runway
158 if ((type() == ILS) || (type() == LOC)) {
159 if (!fgGetBool("/sim/navdb/localizers/auto-align", true)) {
163 if (parts.size() < 2) {
164 SG_LOG(SG_GENERAL, SG_ALERT, "can't parse navaid " << ident()
165 << " name for airport/runway:" << name);
170 = fgGetDouble( "/sim/navdb/localizers/auto-align-threshold-deg",
172 FGRunway* runway = apt->getRunwayByIdent(parts[1]);
173 alignLocaliserWithRunway(runway, threshold);
177 void FGNavRecord::alignLocaliserWithRunway(FGRunway* aRunway, double aThreshold)
179 // find the distance from the threshold to the localizer
180 SGGeod runwayThreshold(aRunway->threshold());
181 double dist, az1, az2;
182 SGGeodesy::inverse(mPosition, runwayThreshold, az1, az2, dist);
184 // back project that distance along the runway center line
185 SGGeod newPos = aRunway->pointOnCenterline(dist);
187 double hdg_diff = get_multiuse() - aRunway->headingDeg();
189 // clamp to [-180.0 ... 180.0]
190 if ( hdg_diff < -180.0 ) {
192 } else if ( hdg_diff > 180.0 ) {
196 if ( fabs(hdg_diff) <= aThreshold ) {
198 set_multiuse( aRunway->headingDeg() );
200 SG_LOG(SG_GENERAL, SG_WARN, "localizer:" << ident() << ", aligning with runway "
201 << aRunway->ident() << " exceeded heading threshold");
205 FGTACANRecord::FGTACANRecord(void) :
213 operator >> ( std::istream& in, FGTACANRecord& n )
215 in >> n.channel >> n.freq ;
216 //getline( in, n.name );