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