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/simple.hxx>
31 #include <Scenery/scenery.hxx>
32 #include "atc_mgr.hxx"
35 FGATCManager::FGATCManager() {
38 networkVisible = false;
39 initSucceeded = false;
42 FGATCManager::~FGATCManager() {
46 void FGATCManager::init() {
51 trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
53 // find a reasonable controller for our user's aircraft..
54 // Let's start by working out the following three scenarios:
55 // Starting on ground at a parking position
56 // Starting on ground at the runway.
57 // Starting in the Air
58 bool onGround = fgGetBool("/sim/presets/onground");
59 string runway = fgGetString("/sim/atc/runway");
60 string airport = fgGetString("/sim/presets/airport-id");
61 string parking = fgGetString("/sim/presets/parkpos");
64 // Create an (invisible) AIAircraft represenation of the current
65 // Users, aircraft, that mimicks the user aircraft's behavior.
66 string callsign= fgGetString("/sim/multiplay/callsign");
67 double longitude = fgGetDouble("/position/longitude-deg");
68 double latitude = fgGetDouble("/position/latitude-deg");
69 double altitude = fgGetDouble("/position/altitude-ft");
70 double heading = fgGetDouble("/orientation/heading-deg");
71 double speed = fgGetDouble("/velocities/groundspeed-kt");
72 double aircraftRadius = 40; // note that this is currently hardcoded to a one-size-fits all JumboJet value. Should change later;
75 ai_ac.setCallSign ( callsign );
76 ai_ac.setLongitude( longitude );
77 ai_ac.setLatitude ( latitude );
78 ai_ac.setAltitude ( altitude );
79 ai_ac.setPerformance("", "jet_transport");
81 // NEXT UP: Create a traffic Schedule and fill that with appropriate information. This we can use to flight planning.
82 // Note that these are currently only defaults.
83 FGAISchedule *trafficRef = new FGAISchedule;
84 trafficRef->setFlightType("gate");
86 FGScheduledFlight *flight = new FGScheduledFlight;
87 flight->setDepartureAirport(airport);
88 flight->setArrivalAirport(airport);
89 flight->initializeAirports();
90 flight->setFlightRules("IFR");
91 flight->setCallSign(callsign);
93 trafficRef->assign(flight);
94 FGAIFlightPlan *fp = 0;
95 ai_ac.setTrafficRef(trafficRef);
97 string flightPlanName = airport + "-" + airport + ".xml";
98 //double cruiseAlt = 100; // Doesn't really matter right now.
99 //double courseToDest = 180; // Just use something neutral; this value might affect the runway that is used though...
100 //time_t deptime = 0; // just make sure how flightplan processing is affected by this...
103 FGAirport *apt = FGAirport::findByIdent(airport);
104 if (apt && onGround) {// && !runway.empty()) {
105 FGAirportDynamics* dcs = apt->getDynamics();
106 fp = new FGAIFlightPlan;
107 ParkingAssignment pk(dcs->getParkingByName(parking));
109 // No valid parking location, so either at the runway or at a random location.
111 if (!runway.empty()) {
112 controller = apt->getDynamics()->getTowerController();
113 int stationFreq = apt->getDynamics()->getTowerFrequency(2);
116 //cerr << "Setting radio frequency to in airfrequency: " << stationFreq << endl;
117 fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
120 string fltType = "ga";
121 fp->setRunway(runway);
122 fp->createTakeOff(&ai_ac, false, apt, 0, fltType);
123 ai_ac.setTakeOffStatus(2);
125 // We're on the ground somewhere. Handle this case later.
128 controller = apt->getDynamics()->getStartupController();
129 int stationFreq = apt->getDynamics()->getGroundFrequency(1);
132 //cerr << "Setting radio frequency to : " << stationFreq << endl;
133 fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
136 //double, lat, lon, head; // Unused variables;
137 //int getId = apt->getDynamics()->getParking(gateId, &lat, &lon, &head);
138 aircraftRadius = pk.parking()->getRadius();
139 string fltType = pk.parking()->getType(); // gate / ramp, ga, etc etc.
140 string aircraftType; // Unused.
141 string airline; // Currently used for gate selection, but a fallback mechanism will apply when not specified.
143 if (!(fp->createPushBack(&ai_ac,
155 fp->getLastWaypoint()->setName( fp->getLastWaypoint()->getName() + string("legend"));
160 // Create an initial flightplan and assign it to the ai_ac. We won't use this flightplan, but it is necessary to
161 // keep the ATC code happy.
165 ai_ac.SetFlightPlan(fp);
168 controller->announcePosition(ai_ac.getID(), fp, fp->getCurrentWaypoint()->getRouteIndex(),
169 ai_ac._getLatitude(), ai_ac._getLongitude(), heading, speed, altitude,
170 aircraftRadius, leg, &ai_ac);
174 //osg::Node* node = apt->getDynamics()->getGroundNetwork()->getRenderNode();
175 //cerr << "Adding groundnetWork to the scenegraph::init" << endl;
176 //globals->get_scenery()->get_scene_graph()->addChild(node);
178 initSucceeded = true;
181 void FGATCManager::addController(FGATCController *controller) {
182 activeStations.push_back(controller);
185 void FGATCManager::update ( double time ) {
186 //cerr << "ATC update code is running at time: " << time << endl;
187 // Test code: let my virtual co-pilot handle ATC:
191 FGAIFlightPlan *fp = ai_ac.GetFlightPlan();
193 /* test code : find out how the routing develops */
195 int size = fp->getNrOfWayPoints();
196 //cerr << "Setting pos" << pos << " ";
197 //cerr << "setting intentions " ;
198 // This indicates that we have run out of waypoints: Im future versions, the
199 // user should be able to select a new route, but for now just shut down the
202 //cerr << "Shutting down the atc_mgr" << endl;
205 //cerr << "Size of waypoint cue " << size << " ";
206 //for (int i = 0; i < size; i++) {
207 // int val = fp->getRouteIndex(i);
208 //cerr << fp->getWayPoint(i)->getName() << " ";
209 //if ((val) && (val != pos)) {
210 //intentions.push_back(val);
211 //cerr << "[done ] " << endl;
214 //cerr << "[done ] " << endl;
217 //cerr << "Currently at leg : " << fp->getLeg() << endl;
219 double longitude = fgGetDouble("/position/longitude-deg");
220 double latitude = fgGetDouble("/position/latitude-deg");
221 double heading = fgGetDouble("/orientation/heading-deg");
222 double speed = fgGetDouble("/velocities/groundspeed-kt");
223 double altitude = fgGetDouble("/position/altitude-ft");
226 SGGeod me(SGGeod::fromDegM(longitude,
229 SGGeod wpt1(SGGeod::fromDegM(fp->getWayPoint(1)->getLongitude(),
230 fp->getWayPoint(1)->getLatitude(),
231 fp->getWayPoint(1)->getAltitude()));
232 SGGeod wpt2(SGGeod::fromDegM(fp->getWayPoint(1)->getLongitude(),
233 fp->getWayPoint(1)->getLatitude(),
234 fp->getWayPoint(1)->getAltitude()));
236 double course1, az1, dist1;
237 double course2, az2, dist2;
238 SGGeodesy::inverse(me, wpt1, course1, az1, dist1);
240 cerr << "Bearing to nearest waypoint : " << course1 << " " << dist1 << ". (course " << course2 << ")." << endl;
242 ai_ac.setLatitude(latitude);
243 ai_ac.setLongitude(longitude);
244 ai_ac.setAltitude(altitude);
245 ai_ac.setHeading(heading);
246 ai_ac.setSpeed(speed);
248 controller = ai_ac.getATCController();
249 FGATCDialogNew::instance()->update(time);
251 //cerr << "name of previous waypoint : " << fp->getPreviousWaypoint()->getName() << endl;
253 //cerr << "Running FGATCManager::update()" << endl;
254 //cerr << "Currently under control of " << controller->getName() << endl;
255 controller->updateAircraftInformation(ai_ac.getID(),
261 //string airport = fgGetString("/sim/presets/airport-id");
262 //FGAirport *apt = FGAirport::findByIdent(airport);
263 // AT this stage we should update the flightplan, so that waypoint incrementing is conducted as well as leg loading.
264 int n = trans_num->getIntValue();
266 //cerr << "Toggling ground network visibility " << networkVisible << endl;
267 networkVisible = !networkVisible;
268 trans_num->setIntValue(-1);
270 if ((controller != prevController) && (prevController)) {
271 prevController->render(false);
273 controller->render(networkVisible);
275 //cerr << "Adding groundnetWork to the scenegraph::update" << endl;
276 prevController = controller;
278 for (AtcVecIterator atc = activeStations.begin(); atc != activeStations.end(); atc++) {
279 (*atc)->update(time);