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 // find a reasonable controller for our user's aircraft..
52 // Let's start by working out the following three scenarios:
53 // Starting on ground at a parking position
54 // Starting on ground at the runway.
55 // Starting in the Air
56 bool onGround = fgGetBool("/sim/presets/onground");
57 string runway = fgGetString("/sim/atc/runway");
58 string airport = fgGetString("/sim/presets/airport-id");
59 string parking = fgGetString("/sim/presets/parkpos");
62 // Create an (invisible) AIAircraft represenation of the current
63 // Users, aircraft, that mimicks the user aircraft's behavior.
64 string callsign= fgGetString("/sim/multiplay/callsign");
65 double longitude = fgGetDouble("/position/longitude-deg");
66 double latitude = fgGetDouble("/position/latitude-deg");
67 double altitude = fgGetDouble("/position/altitude-ft");
68 double heading = fgGetDouble("/orientation/heading-deg");
69 double speed = fgGetDouble("/velocities/groundspeed-kt");
70 double aircraftRadius = 40; // note that this is currently hardcoded to a one-size-fits all JumboJet value. Should change later;
73 ai_ac.setCallSign ( callsign );
74 ai_ac.setLongitude( longitude );
75 ai_ac.setLatitude ( latitude );
76 ai_ac.setAltitude ( altitude );
77 ai_ac.setPerformance("", "jet_transport");
79 // NEXT UP: Create a traffic Schedule and fill that with appropriate information. This we can use to flight planning.
80 // Note that these are currently only defaults.
81 FGAISchedule *trafficRef = new FGAISchedule;
82 trafficRef->setFlightType("gate");
84 FGScheduledFlight *flight = new FGScheduledFlight;
85 flight->setDepartureAirport(airport);
86 flight->setArrivalAirport(airport);
87 flight->initializeAirports();
88 flight->setFlightRules("IFR");
89 flight->setCallSign(callsign);
91 trafficRef->assign(flight);
92 FGAIFlightPlan *fp = 0;
93 ai_ac.setTrafficRef(trafficRef);
95 string flightPlanName = airport + "-" + airport + ".xml";
96 //double cruiseAlt = 100; // Doesn't really matter right now.
97 //double courseToDest = 180; // Just use something neutral; this value might affect the runway that is used though...
98 //time_t deptime = 0; // just make sure how flightplan processing is affected by this...
101 FGAirport *apt = FGAirport::findByIdent(airport);
102 if (apt && onGround) {// && !runway.empty()) {
103 FGAirportDynamics* dcs = apt->getDynamics();
104 fp = new FGAIFlightPlan;
105 ParkingAssignment pk(dcs->getParkingByName(parking));
107 // No valid parking location, so either at the runway or at a random location.
109 if (!runway.empty()) {
110 controller = apt->getDynamics()->getTowerController();
111 int stationFreq = apt->getDynamics()->getTowerFrequency(2);
114 //cerr << "Setting radio frequency to in airfrequency: " << stationFreq << endl;
115 fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
118 string fltType = "ga";
119 fp->setRunway(runway);
120 fp->createTakeOff(&ai_ac, false, apt, 0, fltType);
121 ai_ac.setTakeOffStatus(2);
123 // We're on the ground somewhere. Handle this case later.
126 controller = apt->getDynamics()->getStartupController();
127 int stationFreq = apt->getDynamics()->getGroundFrequency(1);
130 //cerr << "Setting radio frequency to : " << stationFreq << endl;
131 fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
134 //double, lat, lon, head; // Unused variables;
135 //int getId = apt->getDynamics()->getParking(gateId, &lat, &lon, &head);
136 aircraftRadius = pk.parking()->getRadius();
137 string fltType = pk.parking()->getType(); // gate / ramp, ga, etc etc.
138 string aircraftType; // Unused.
139 string airline; // Currently used for gate selection, but a fallback mechanism will apply when not specified.
141 if (!(fp->createPushBack(&ai_ac,
153 fp->getLastWaypoint()->setName( fp->getLastWaypoint()->getName() + string("legend"));
158 // Create an initial flightplan and assign it to the ai_ac. We won't use this flightplan, but it is necessary to
159 // keep the ATC code happy.
163 ai_ac.SetFlightPlan(fp);
166 controller->announcePosition(ai_ac.getID(), fp, fp->getCurrentWaypoint()->getRouteIndex(),
167 ai_ac._getLatitude(), ai_ac._getLongitude(), heading, speed, altitude,
168 aircraftRadius, leg, &ai_ac);
172 //osg::Node* node = apt->getDynamics()->getGroundNetwork()->getRenderNode();
173 //cerr << "Adding groundnetWork to the scenegraph::init" << endl;
174 //globals->get_scenery()->get_scene_graph()->addChild(node);
176 initSucceeded = true;
179 void FGATCManager::addController(FGATCController *controller) {
180 activeStations.push_back(controller);
183 void FGATCManager::update ( double time ) {
184 //cerr << "ATC update code is running at time: " << time << endl;
185 // Test code: let my virtual co-pilot handle ATC:
189 FGAIFlightPlan *fp = ai_ac.GetFlightPlan();
191 /* test code : find out how the routing develops */
193 int size = fp->getNrOfWayPoints();
194 //cerr << "Setting pos" << pos << " ";
195 //cerr << "setting intentions " ;
196 // This indicates that we have run out of waypoints: Im future versions, the
197 // user should be able to select a new route, but for now just shut down the
200 //cerr << "Shutting down the atc_mgr" << endl;
203 //cerr << "Size of waypoint cue " << size << " ";
204 //for (int i = 0; i < size; i++) {
205 // int val = fp->getRouteIndex(i);
206 //cerr << fp->getWayPoint(i)->getName() << " ";
207 //if ((val) && (val != pos)) {
208 //intentions.push_back(val);
209 //cerr << "[done ] " << endl;
212 //cerr << "[done ] " << endl;
215 //cerr << "Currently at leg : " << fp->getLeg() << endl;
217 double longitude = fgGetDouble("/position/longitude-deg");
218 double latitude = fgGetDouble("/position/latitude-deg");
219 double heading = fgGetDouble("/orientation/heading-deg");
220 double speed = fgGetDouble("/velocities/groundspeed-kt");
221 double altitude = fgGetDouble("/position/altitude-ft");
224 SGGeod me(SGGeod::fromDegM(longitude,
227 SGGeod wpt1(SGGeod::fromDegM(fp->getWayPoint(1)->getLongitude(),
228 fp->getWayPoint(1)->getLatitude(),
229 fp->getWayPoint(1)->getAltitude()));
230 SGGeod wpt2(SGGeod::fromDegM(fp->getWayPoint(1)->getLongitude(),
231 fp->getWayPoint(1)->getLatitude(),
232 fp->getWayPoint(1)->getAltitude()));
234 double course1, az1, dist1;
235 double course2, az2, dist2;
236 SGGeodesy::inverse(me, wpt1, course1, az1, dist1);
238 cerr << "Bearing to nearest waypoint : " << course1 << " " << dist1 << ". (course " << course2 << ")." << endl;
240 ai_ac.setLatitude(latitude);
241 ai_ac.setLongitude(longitude);
242 ai_ac.setAltitude(altitude);
243 ai_ac.setHeading(heading);
244 ai_ac.setSpeed(speed);
246 controller = ai_ac.getATCController();
247 FGATCDialogNew::instance()->update(time);
249 //cerr << "name of previous waypoint : " << fp->getPreviousWaypoint()->getName() << endl;
251 //cerr << "Running FGATCManager::update()" << endl;
252 //cerr << "Currently under control of " << controller->getName() << endl;
253 controller->updateAircraftInformation(ai_ac.getID(),
259 //string airport = fgGetString("/sim/presets/airport-id");
260 //FGAirport *apt = FGAirport::findByIdent(airport);
261 // AT this stage we should update the flightplan, so that waypoint incrementing is conducted as well as leg loading.
262 static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
263 int n = trans_num->getIntValue();
265 //cerr << "Toggling ground network visibility " << networkVisible << endl;
266 networkVisible = !networkVisible;
267 trans_num->setIntValue(-1);
269 if ((controller != prevController) && (prevController)) {
270 prevController->render(false);
272 controller->render(networkVisible);
274 //cerr << "Adding groundnetWork to the scenegraph::update" << endl;
275 prevController = controller;
277 for (AtcVecIterator atc = activeStations.begin(); atc != activeStations.end(); atc++) {
278 (*atc)->update(time);