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