1 /******************************************************************************
3 * Written by Durk Talsma, started August 1, 2010.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 **************************************************************************/
29 #include <Airports/dynamics.hxx>
30 #include <Airports/airport.hxx>
31 #include <Scenery/scenery.hxx>
32 #include "atc_mgr.hxx"
37 FGATCManager::FGATCManager() {
40 networkVisible = false;
41 initSucceeded = false;
44 FGATCManager::~FGATCManager() {
48 void FGATCManager::init() {
53 trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
55 // find a reasonable controller for our user's aircraft..
56 // Let's start by working out the following three scenarios:
57 // Starting on ground at a parking position
58 // Starting on ground at the runway.
59 // Starting in the Air
60 bool onGround = fgGetBool("/sim/presets/onground");
61 string runway = fgGetString("/sim/atc/runway");
62 string airport = fgGetString("/sim/presets/airport-id");
63 string parking = fgGetString("/sim/presets/parkpos");
66 // Create an (invisible) AIAircraft represenation of the current
67 // Users, aircraft, that mimicks the user aircraft's behavior.
68 string callsign= fgGetString("/sim/multiplay/callsign");
69 double longitude = fgGetDouble("/position/longitude-deg");
70 double latitude = fgGetDouble("/position/latitude-deg");
71 double altitude = fgGetDouble("/position/altitude-ft");
72 double heading = fgGetDouble("/orientation/heading-deg");
73 double speed = fgGetDouble("/velocities/groundspeed-kt");
74 double aircraftRadius = 40; // note that this is currently hardcoded to a one-size-fits all JumboJet value. Should change later;
77 ai_ac.setCallSign ( callsign );
78 ai_ac.setLongitude( longitude );
79 ai_ac.setLatitude ( latitude );
80 ai_ac.setAltitude ( altitude );
81 ai_ac.setPerformance("", "jet_transport");
83 // NEXT UP: Create a traffic Schedule and fill that with appropriate information. This we can use to flight planning.
84 // Note that these are currently only defaults.
85 FGAISchedule *trafficRef = new FGAISchedule;
86 trafficRef->setFlightType("gate");
88 FGScheduledFlight *flight = new FGScheduledFlight;
89 flight->setDepartureAirport(airport);
90 flight->setArrivalAirport(airport);
91 flight->initializeAirports();
92 flight->setFlightRules("IFR");
93 flight->setCallSign(callsign);
95 trafficRef->assign(flight);
96 FGAIFlightPlan *fp = 0;
97 ai_ac.setTrafficRef(trafficRef);
99 string flightPlanName = airport + "-" + airport + ".xml";
100 //double cruiseAlt = 100; // Doesn't really matter right now.
101 //double courseToDest = 180; // Just use something neutral; this value might affect the runway that is used though...
102 //time_t deptime = 0; // just make sure how flightplan processing is affected by this...
105 FGAirport *apt = FGAirport::findByIdent(airport);
106 if (apt && onGround) {// && !runway.empty()) {
107 FGAirportDynamics* dcs = apt->getDynamics();
108 ParkingAssignment pk(dcs->getParkingByName(parking));
110 // No valid parking location, so either at the runway or at a random location.
112 fp = new FGAIFlightPlan;
113 controller = apt->getDynamics()->getStartupController();
114 int stationFreq = apt->getDynamics()->getGroundFrequency(1);
117 //cerr << "Setting radio frequency to : " << stationFreq << endl;
118 fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
121 //double, lat, lon, head; // Unused variables;
122 //int getId = apt->getDynamics()->getParking(gateId, &lat, &lon, &head);
123 aircraftRadius = pk.parking()->getRadius();
124 string fltType = pk.parking()->getType(); // gate / ramp, ga, etc etc.
125 string aircraftType; // Unused.
126 string airline; // Currently used for gate selection, but a fallback mechanism will apply when not specified.
128 if (!(fp->createPushBack(&ai_ac,
141 } else if (!runway.empty()) {
142 controller = apt->getDynamics()->getTowerController();
143 int stationFreq = apt->getDynamics()->getTowerFrequency(2);
146 //cerr << "Setting radio frequency to in airfrequency: " << stationFreq << endl;
147 fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
149 fp = new FGAIFlightPlan;
151 string fltType = "ga";
152 fp->setRunway(runway);
153 fp->createTakeOff(&ai_ac, false, apt, 0, fltType);
154 ai_ac.setTakeOffStatus(2);
156 // We're on the ground somewhere. Handle this case later.
160 fp->getLastWaypoint()->setName( fp->getLastWaypoint()->getName() + string("legend"));
166 // Create an initial flightplan and assign it to the ai_ac. We won't use this flightplan, but it is necessary to
167 // keep the ATC code happy.
171 ai_ac.SetFlightPlan(fp);
174 controller->announcePosition(ai_ac.getID(), fp, fp->getCurrentWaypoint()->getRouteIndex(),
175 ai_ac._getLatitude(), ai_ac._getLongitude(), heading, speed, altitude,
176 aircraftRadius, leg, &ai_ac);
180 //osg::Node* node = apt->getDynamics()->getGroundNetwork()->getRenderNode();
181 //cerr << "Adding groundnetWork to the scenegraph::init" << endl;
182 //globals->get_scenery()->get_scene_graph()->addChild(node);
184 initSucceeded = true;
187 void FGATCManager::addController(FGATCController *controller) {
188 activeStations.push_back(controller);
191 void FGATCManager::update ( double time ) {
192 //cerr << "ATC update code is running at time: " << time << endl;
193 // Test code: let my virtual co-pilot handle ATC:
197 FGAIFlightPlan *fp = ai_ac.GetFlightPlan();
199 /* test code : find out how the routing develops */
201 int size = fp->getNrOfWayPoints();
202 //cerr << "Setting pos" << pos << " ";
203 //cerr << "setting intentions " ;
204 // This indicates that we have run out of waypoints: Im future versions, the
205 // user should be able to select a new route, but for now just shut down the
208 //cerr << "Shutting down the atc_mgr" << endl;
211 //cerr << "Size of waypoint cue " << size << " ";
212 //for (int i = 0; i < size; i++) {
213 // int val = fp->getRouteIndex(i);
214 //cerr << fp->getWayPoint(i)->getName() << " ";
215 //if ((val) && (val != pos)) {
216 //intentions.push_back(val);
217 //cerr << "[done ] " << endl;
220 //cerr << "[done ] " << endl;
223 //cerr << "Currently at leg : " << fp->getLeg() << endl;
225 double longitude = fgGetDouble("/position/longitude-deg");
226 double latitude = fgGetDouble("/position/latitude-deg");
227 double heading = fgGetDouble("/orientation/heading-deg");
228 double speed = fgGetDouble("/velocities/groundspeed-kt");
229 double altitude = fgGetDouble("/position/altitude-ft");
232 SGGeod me(SGGeod::fromDegM(longitude,
235 SGGeod wpt1(SGGeod::fromDegM(fp->getWayPoint(1)->getLongitude(),
236 fp->getWayPoint(1)->getLatitude(),
237 fp->getWayPoint(1)->getAltitude()));
238 SGGeod wpt2(SGGeod::fromDegM(fp->getWayPoint(1)->getLongitude(),
239 fp->getWayPoint(1)->getLatitude(),
240 fp->getWayPoint(1)->getAltitude()));
242 double course1, az1, dist1;
243 double course2, az2, dist2;
244 SGGeodesy::inverse(me, wpt1, course1, az1, dist1);
246 cerr << "Bearing to nearest waypoint : " << course1 << " " << dist1 << ". (course " << course2 << ")." << endl;
248 ai_ac.setLatitude(latitude);
249 ai_ac.setLongitude(longitude);
250 ai_ac.setAltitude(altitude);
251 ai_ac.setHeading(heading);
252 ai_ac.setSpeed(speed);
254 controller = ai_ac.getATCController();
255 FGATCDialogNew::instance()->update(time);
257 //cerr << "name of previous waypoint : " << fp->getPreviousWaypoint()->getName() << endl;
259 //cerr << "Running FGATCManager::update()" << endl;
260 //cerr << "Currently under control of " << controller->getName() << endl;
261 controller->updateAircraftInformation(ai_ac.getID(),
267 //string airport = fgGetString("/sim/presets/airport-id");
268 //FGAirport *apt = FGAirport::findByIdent(airport);
269 // AT this stage we should update the flightplan, so that waypoint incrementing is conducted as well as leg loading.
270 int n = trans_num->getIntValue();
272 //cerr << "Toggling ground network visibility " << networkVisible << endl;
273 networkVisible = !networkVisible;
274 trans_num->setIntValue(-1);
276 if ((controller != prevController) && (prevController)) {
277 prevController->render(false);
279 controller->render(networkVisible);
281 //cerr << "Adding groundnetWork to the scenegraph::update" << endl;
282 prevController = controller;
284 for (AtcVecIterator atc = activeStations.begin(); atc != activeStations.end(); atc++) {
285 (*atc)->update(time);