]> git.mxchange.org Git - flightgear.git/blob - src/ATC/atis.cxx
Changes to support voice rendering of ATC
[flightgear.git] / src / ATC / atis.cxx
1 // atis.cxx - routines to generate the ATIS info string
2 // This is the implementation of the FGATIS class
3 //
4 // Written by David Luff, started October 2001.
5 //
6 // Copyright (C) 2001  David C Luff - david.luff@nottingham.ac.uk
7 //
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.
12 //
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.
17 //
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., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <simgear/compiler.h>
28
29 #include <stdlib.h>     // atoi()
30 #include <stdio.h>      // sprintf
31 #include <string>
32 SG_USING_STD(string);
33
34 #include STL_IOSTREAM
35 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
36 SG_USING_STD(cout);
37 #endif
38
39 //#include <simgear/debug/logstream.hxx>
40 //#include <simgear/misc/sgstream.hxx>
41 #include <simgear/misc/sg_path.hxx>
42
43 #ifdef FG_WEATHERCM
44 # include <WeatherCM/FGLocalWeatherDatabase.h>
45 #else
46 # include <Environment/environment_mgr.hxx>
47 # include <Environment/environment.hxx>
48 #endif
49
50 #include <Main/fg_props.hxx>
51 #include <Main/globals.hxx>
52 #include <Airports/runways.hxx>
53
54 #include "atis.hxx"
55 #include "atislist.hxx"
56 #include "ATCdisplay.hxx"
57 #include "ATCutils.hxx"
58 #include "ATCmgr.hxx"
59
60 // Constructor
61 FGATIS::FGATIS()
62 : type(0),
63 lon(0.0), lat(0.0),
64 elev(0.0),
65 x(0.0), y(0.0), z(0.0),
66 freq(0),
67 range(0),
68 display(false),
69 displaying(false),
70 ident(""),
71 name(""),
72 transmission(""),
73 trans_ident(""),
74 atis_failed(false)
75 {
76 }
77
78 // Destructor
79 FGATIS::~FGATIS() {
80 }
81
82 // Main update function - checks whether we are displaying or not the correct message.
83 void FGATIS::Update() {
84         if(display) {
85                 if(displaying) {
86                         // Check if we need to update the message
87                         // - basically every hour and if the weather changes significantly at the station
88                         //globals->get_ATC_display()->ChangeRepeatingMessage(transmission);
89                 } else {
90                         // We need to get and display the message
91                         UpdateTransmission();
92                         //cout << "ATIS.CXX - calling ATCMgr to render transmission..." << endl;
93                         globals->get_ATC_mgr()->Render(transmission, true);
94                         displaying = true;
95                 }
96         } else {
97                 // We shouldn't be displaying
98                 if(displaying) {
99                         //cout << "ATIS.CXX - calling NoRender()..." << endl;
100                         globals->get_ATC_mgr()->NoRender();
101                         displaying = false;
102                 }
103         }
104 }
105
106 // Sets the actual broadcast ATIS transmission.
107 void FGATIS::UpdateTransmission() {
108         double visibility;
109         char buf[10];
110         int phonetic_id;
111         string phonetic_id_string;
112         string time_str = fgGetString("sim/time/gmt-string");
113         int hours;
114         // int minutes;
115         
116         #ifdef FG_WEATHERCM
117         sgVec3 position = { lat, lon, elev };
118         FGPhysicalProperty stationweather = WeatherDatabase->get(position);
119         #else
120         FGEnvironment stationweather =
121         globals->get_environment_mgr()->getEnvironment(lat, lon, elev);
122         #endif
123         
124         transmission = "";
125         
126         // UK CAA radiotelephony manual indicated ATIS transmissions start with "This is"
127         // Not sure about rest of the world though.
128         transmission += "This_is ";
129         // transmitted station name.
130         transmission += name;
131         // Add "Information"
132         transmission += " information";
133         
134         //cout << "In atis.cxx, time_str = " << time_str << '\n';
135         // Add the recording identifier
136         // For now we will assume we only transmit every hour
137         hours = atoi((time_str.substr(1,2)).c_str());   //Warning - this is fragile if the 
138         //time string format changes
139         //cout << "In atis.cxx, hours = " << hours << endl;
140         phonetic_id = current_atislist->GetCallSign(ident, hours, 0);
141         phonetic_id_string = GetPhoneticIdent(phonetic_id);
142         transmission += " ";
143         transmission += phonetic_id_string;
144         
145         // Output the recording time. - we'll just output the last whole hour for now.
146         // FIXME - this only gets GMT time but that appears to be all the clock outputs for now
147         //cout << "in atis.cxx, time = " << time_str << endl;
148         transmission = transmission + " / Weather " + ConvertNumToSpokenDigits((time_str.substr(0,3) + "00")) + " hours zulu";
149         
150         // Get the temperature
151         // (Hardwire it for now since the global property returns the temperature at the current altitude
152         //temperature = fgGetDouble("/environment/weather/temperature-K");
153         #ifdef FG_WEATHERCM
154         sprintf(buf, "%i", int(stationweather.Temperature - 273.15));
155         #else
156         // HACK ALERT - at the moment the new environment subsystem returns bogus temperatures
157         // FIXME - take out this hack when this gets fixed upstream
158         int temp = (int)stationweather.get_temperature_degc();
159         if((temp < -50) || (temp > 60)) {
160                 temp = 15;
161         }
162         // original:
163         //sprintf(buf, "%d", int(stationweather.get_temperature_degc()));
164         // hack:
165         sprintf(buf, "%d", temp);
166         #endif
167         transmission += " / Temperature ";
168         if(temp < 0) {
169                 transmission += "minus ";
170         }
171         string tempstr1 = buf;
172         string tempstr2;
173         transmission += ConvertNumToSpokenDigits(tempstr1);
174         transmission += " degrees_Celsius";
175         
176         // Get the visibility
177         #ifdef FG_WEATHERCM
178         visibility = fgGetDouble("/environment/visibility-m");
179         #else
180         visibility = stationweather.get_visibility_m();
181         #endif
182         sprintf(buf, "%i", int(visibility/1600));
183         transmission += " / Visibility ";
184         tempstr1 = buf;
185         transmission += ConvertNumToSpokenDigits(tempstr1);
186         transmission += " miles";
187         
188         // Get the cloudbase
189         // FIXME: kludge for now
190         if (strcmp(fgGetString("/environment/clouds/layer[0]/type"), "clear")) {
191                 double cloudbase =
192                 fgGetDouble("/environment/clouds/layer[0]/elevation-ft");
193                 // For some reason the altitude returned doesn't seem to correspond to the actual cloud altitude.
194                 char buf3[10];
195                 char buf4[10];
196                 // cout << "cloudbase = " << cloudbase << endl;
197                 sprintf(buf3, "%i", int(cloudbase)/1000);
198                 sprintf(buf4, "%i", ((int)cloudbase % 1000)/100);
199                 transmission += " / Cloudbase";
200                 if(int(cloudbase)/1000) {
201                         tempstr1 = buf3;
202                         transmission = transmission + " " + ConvertNumToSpokenDigits(tempstr1) + " thousand";
203                 }
204                 if(((int)cloudbase % 1000)/100) {
205                         tempstr1 = buf4;
206                         transmission = transmission + " " + ConvertNumToSpokenDigits(tempstr1) + " hundred";
207                 }                       
208                 transmission += " feet";
209         }
210         
211         // Get the pressure / altimeter
212         
213         #ifndef FG_WEATHERCM
214         double altimeter = stationweather.get_pressure_sea_level_inhg();
215         sprintf(buf, "%.2f", altimeter);
216         transmission += " / Altimeter ";
217         tempstr1 = buf;
218         transmission += ConvertNumToSpokenDigits(tempstr1);
219         #endif
220         
221         // Based on the airport-id and wind get the active runway
222         //FGRunway *r;
223         SGPath path( globals->get_fg_root() );
224         path.append( "Airports" );
225         path.append( "runways.mk4" );
226         FGRunways runways( path.c_str() );
227         
228         #ifdef FG_WEATHERCM
229         //Set the heading to into the wind
230         double wind_x = stationweather.Wind[0];
231         double wind_y = stationweather.Wind[1];
232         
233         double speed = sqrt( wind_x*wind_x + wind_y*wind_y ) * SG_METER_TO_NM / (60.0*60.0);
234         double hdg;
235         
236         //If no wind use 270degrees
237         if(speed == 0) {
238                 hdg = 270;
239                 transmission += " / Winds_light_and_variable";
240         } else {
241                 // //normalize the wind to get the direction
242                 //wind_x /= speed; wind_y /= speed;
243                 
244                 hdg = - atan2 ( wind_x, wind_y ) * SG_RADIANS_TO_DEGREES ;
245                 if (hdg < 0.0)
246                         hdg += 360.0;
247                 
248                 //add a description of the wind to the transmission
249                 char buf5[10];
250                 char buf6[10];
251                 sprintf(buf5, "%i", int(speed));
252                 sprintf(buf6, "%i", int(hdg));
253                 tempstr1 = buf5;
254                 tempstr2 = buf6;
255                 transmission = transmission + " / Winds " + ConvertNumToSpokenDigits(tempstr1) + " knots from "
256                                             + ConvertNumToSpokenDigits(tempstr2) + " degrees";
257         }
258         #else
259         double speed = stationweather.get_wind_speed_kt();
260         double hdg = stationweather.get_wind_from_heading_deg();
261         if (speed == 0) {
262                 transmission += " / Winds_light_and_variable";
263         } else {
264                 // FIXME: get gust factor in somehow
265                 char buf5[10];
266                 char buf6[10];
267                 sprintf(buf5, "%i", int(speed));
268                 sprintf(buf6, "%i", int(hdg));
269                 tempstr1 = buf5;
270                 tempstr2 = buf6;
271                 transmission = transmission + " / Winds " + ConvertNumToSpokenDigits(tempstr1) + " knots from "
272                                             + ConvertNumToSpokenDigits(tempstr2) + " degrees";
273         }
274         #endif
275         
276         string rwy_no = runways.search(ident, int(hdg));
277         if(rwy_no != (string)"NN") {
278                 transmission += " / Landing_and_departing_runway ";
279                 transmission += ConvertRwyNumToSpokenString(atoi(rwy_no.c_str()));
280                 //cout << "in atis.cxx, r.rwy_no = " << rwy_no << " r.id = " << r->id << " r.heading = " << r->heading << endl;
281         }
282         
283         // Anything else?
284         
285         transmission += " / Advise_controller_on_initial_contact_you_have ";
286         transmission += phonetic_id_string;
287         transmission += " /// ";
288 }