1 // ATISmgr.cxx - Implementation of FGATISMgr - a global Flightgear ATIS manager.
3 // Written by David Luff, started February 2002.
5 // Copyright (C) 2002 David C Luff - david.luff@nottingham.ac.uk
6 // Copyright (C) 2012 Thorsten Brehm
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include <simgear/misc/sg_path.hxx>
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/structure/exception.hxx>
30 #include <Airports/simple.hxx>
31 #include <ATC/CommStation.hxx>
32 #include <Main/fg_props.hxx>
34 #include "ATISmgr.hxx"
35 #include "ATCutils.hxx"
38 using flightgear::CommStation;
40 FGATISMgr::FGATISMgr() :
43 #ifdef ENABLE_AUDIO_SUPPORT
50 globals->set_ATIS_mgr(this);
53 FGATISMgr::~FGATISMgr()
55 globals->set_ATIS_mgr(NULL);
57 for (unsigned int unit = 0;unit < _maxCommRadios; ++unit)
59 delete radios[unit].station;
60 radios[unit].station = NULL;
63 #ifdef ENABLE_AUDIO_SUPPORT
68 void FGATISMgr::bind()
72 void FGATISMgr::unbind()
76 void FGATISMgr::init()
78 lon_node = fgGetNode("/position/longitude-deg", true);
79 lat_node = fgGetNode("/position/latitude-deg", true);
80 elev_node = fgGetNode("/position/altitude-ft", true);
82 for (unsigned int unit = 0;unit < _maxCommRadios; ++unit)
86 if (unit < _maxCommRadios/2)
87 ncunit = "comm[" + decimalNumeral(unit) + "]";
89 ncunit = "nav[" + decimalNumeral(unit - _maxCommRadios/2) + "]";
90 string commbase = "/instrumentation/" + ncunit;
91 string commfreq = commbase + "/frequencies/selected-mhz";
93 data.freq = fgGetNode(commfreq.c_str(), true);
94 data.station = new FGATIS(commbase);
95 radios.push_back(data);
99 void FGATISMgr::update(double dt)
101 // update only runs every now and then (1-2 per second)
102 if (++_currentUnit >= _maxCommRadios)
105 FGATC* pStation = radios[_currentUnit].station;
107 pStation->Update(dt * _maxCommRadios);
109 // Search the tuned frequencies
110 FreqSearch(_currentUnit);
113 // Return a pointer to an appropriate voice for a given type of ATC
114 // creating the voice if necessary - i.e. make sure exactly one copy
115 // of every voice in use exists in memory.
117 // TODO - in the future this will get more complex and dole out country/airport
118 // specific voices, and possible make sure that the same voice doesn't get used
119 // at different airports in quick succession if a large enough selection are available.
120 FGATCVoice* FGATISMgr::GetVoicePointer(const atc_type& type)
122 // TODO - implement me better - maintain a list of loaded voices and other voices!!
127 case ATIS: case AWOS:
128 #ifdef ENABLE_AUDIO_SUPPORT
129 // Delayed loading for all available voices, needed because the
130 // sound manager might not be initialized (at all) at this point.
131 // For now we'll do one hard-wired one
133 /* I've loaded the voice even if /sim/sound/pause is true
134 * since I know no way of forcing load of the voice if the user
135 * subsequently switches /sim/sound/audible to true.
136 * (which is the right thing to do -- CLO) :-)
138 if (!voice1 && fgGetBool("/sim/sound/working")) {
139 voice1 = new FGATCVoice;
141 voice = voice1->LoadVoice("default");
142 } catch ( sg_io_exception & e) {
143 SG_LOG(SG_ATC, SG_ALERT, "Unable to load default voice : "
144 << e.getFormattedMessage().c_str());
168 class RangeFilter : public CommStation::Filter
171 RangeFilter( const SGGeod & pos ) :
172 CommStation::Filter(),
173 _cart(SGVec3d::fromGeod(pos)),
178 virtual bool pass(FGPositioned* aPos) const
180 flightgear::CommStation * stn = dynamic_cast<flightgear::CommStation*>(aPos);
184 // do the range check in cartesian space, since the distances are potentially
185 // large enough that the geodetic functions become unstable
186 // (eg, station on opposite side of the planet)
187 double rangeM = SGMiscd::max( stn->rangeNm(), 10.0 ) * SG_NM_TO_METER;
188 double d2 = distSqr( aPos->cart(), _cart);
190 return d2 <= (rangeM * rangeM);
197 // Search for ATC stations by frequency
198 void FGATISMgr::FreqSearch(const unsigned int unit)
200 double frequency = radios[unit].freq->getDoubleValue();
202 // Note: 122.375 must be rounded DOWN to 12237
203 // in order to be consistent with apt.dat et cetera.
204 int freqKhz = static_cast<int>(frequency * 100.0 + 0.25);
206 _aircraftPos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
207 lat_node->getDoubleValue(), elev_node->getDoubleValue());
209 RangeFilter rangeFilter(_aircraftPos );
210 CommStation* sta = CommStation::findByFreq(freqKhz, _aircraftPos, &rangeFilter );
211 radios[unit].station->SetStation(sta);