1 // FGAIFlightPlan - class for loading and storing AI flight plans
2 // Written by David Culp, started May 2004
3 // - davidculp2@comcast.net
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.
25 #include <simgear/misc/sg_path.hxx>
26 #include <simgear/debug/logstream.hxx>
27 #include <simgear/route/waypoint.hxx>
28 #include <simgear/math/sg_geodesy.hxx>
29 #include <simgear/structure/exception.hxx>
30 #include <simgear/constants.h>
31 #include <simgear/props/props.hxx>
32 #include <simgear/props/props_io.hxx>
34 #include <Main/globals.hxx>
35 #include <Main/fg_props.hxx>
36 #include <Main/fg_init.hxx>
37 #include <Airports/simple.hxx>
38 #include <Airports/runways.hxx>
39 #include <Airports/groundnetwork.hxx>
41 #include <Environment/environment_mgr.hxx>
42 #include <Environment/environment.hxx>
44 #include "AIFlightPlan.hxx"
45 #include "AIAircraft.hxx"
49 FGAIFlightPlan::FGAIFlightPlan()
54 FGAIFlightPlan::FGAIFlightPlan(const string& filename)
62 SGPath path( globals->get_fg_root() );
63 path.append( ("/AI/FlightPlans/" + filename).c_str() );
68 readProperties(path.str(), &root);
69 } catch (const sg_exception &) {
70 SG_LOG(SG_GENERAL, SG_ALERT,
71 "Error reading AI flight plan: " << path.str());
72 // cout << path.str() << endl;
76 SGPropertyNode * node = root.getNode("flightplan");
77 for (i = 0; i < node->nChildren(); i++) {
78 //cout << "Reading waypoint " << i << endl;
79 waypoint* wpt = new waypoint;
80 SGPropertyNode * wpt_node = node->getChild(i);
81 wpt->name = wpt_node->getStringValue("name", "END");
82 wpt->latitude = wpt_node->getDoubleValue("lat", 0);
83 wpt->longitude = wpt_node->getDoubleValue("lon", 0);
84 wpt->altitude = wpt_node->getDoubleValue("alt", 0);
85 wpt->speed = wpt_node->getDoubleValue("ktas", 0);
86 wpt->crossat = wpt_node->getDoubleValue("crossat", -10000);
87 wpt->gear_down = wpt_node->getBoolValue("gear-down", false);
88 wpt->flaps_down= wpt_node->getBoolValue("flaps-down", false);
89 wpt->on_ground = wpt_node->getBoolValue("on-ground", false);
90 wpt->time_sec = wpt_node->getDoubleValue("time-sec", 0);
91 wpt->time = wpt_node->getStringValue("time", "");
93 if (wpt->name == "END") wpt->finished = true;
94 else wpt->finished = false;
96 waypoints.push_back( wpt );
99 wpt_iterator = waypoints.begin();
100 //cout << waypoints.size() << " waypoints read." << endl;
104 // This is a modified version of the constructor,
105 // Which not only reads the waypoints from a
106 // Flight plan file, but also adds the current
107 // Position computed by the traffic manager, as well
108 // as setting speeds and altitude computed by the
110 FGAIFlightPlan::FGAIFlightPlan(FGAIAircraft *ac,
111 const std::string& p,
122 const string& fltType,
123 const string& acType,
124 const string& airline)
132 bool useInitialWayPoint = true;
133 bool useCurrentWayPoint = false;
134 SGPath path( globals->get_fg_root() );
135 path.append( "/AI/FlightPlans" );
140 // This is a bit of a hack:
141 // Normally the value of course will be used to evaluate whether
142 // or not a waypoint will be used for midair initialization of
143 // an AI aircraft. However, if a course value of 999 will be passed
144 // when an update request is received, which will by definition always be
145 // on the ground and should include all waypoints.
148 useInitialWayPoint = false;
149 useCurrentWayPoint = true;
156 readProperties(path.str(), &root);
158 SGPropertyNode * node = root.getNode("flightplan");
160 //waypoints.push_back( init_waypoint );
161 for (int i = 0; i < node->nChildren(); i++) {
162 //cout << "Reading waypoint " << i << endl;
163 waypoint* wpt = new waypoint;
164 SGPropertyNode * wpt_node = node->getChild(i);
165 wpt->name = wpt_node->getStringValue("name", "END");
166 wpt->latitude = wpt_node->getDoubleValue("lat", 0);
167 wpt->longitude = wpt_node->getDoubleValue("lon", 0);
168 wpt->altitude = wpt_node->getDoubleValue("alt", 0);
169 wpt->speed = wpt_node->getDoubleValue("ktas", 0);
170 //wpt->speed = speed;
171 wpt->crossat = wpt_node->getDoubleValue("crossat", -10000);
172 wpt->gear_down = wpt_node->getBoolValue("gear-down", false);
173 wpt->flaps_down= wpt_node->getBoolValue("flaps-down", false);
175 if (wpt->name == "END") wpt->finished = true;
176 else wpt->finished = false;
177 waypoints.push_back(wpt);
179 } catch (const sg_exception &e) {
180 SG_LOG(SG_GENERAL, SG_WARN, "Error reading AI flight plan: " <<
181 e.getMessage() << " from " << e.getOrigin());
184 // cout << path.str() << endl;
185 // cout << "Trying to create this plan dynamically" << endl;
186 // cout << "Route from " << dep->id << " to " << arr->id << endl;
187 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
188 time_t timeDiff = now-start;
191 if ((timeDiff > 300) && (timeDiff < 1200))
193 else if ((timeDiff >= 1200) && (timeDiff < 1500))
195 else if ((timeDiff >= 1500) && (timeDiff < 2000))
197 else if (timeDiff >= 2000)
200 if (timeDiff >= 2000)
203 SG_LOG(SG_GENERAL, SG_INFO, "Route from " << dep->getId() << " to " << arr->getId() << ". Set leg to : " << leg << " " << ac->getTrafficRef()->getCallSign());
204 wpt_iterator = waypoints.begin();
205 create(ac, dep,arr, leg, alt, speed, lat, lon,
206 firstLeg, radius, fltType, acType, airline);
207 wpt_iterator = waypoints.begin();
208 //cerr << "after create: " << (*wpt_iterator)->name << endl;
210 // Now that we have dynamically created a flight plan,
211 // we need to add some code that pops any waypoints already past.
215 waypoint* init_waypoint = new waypoint;
216 init_waypoint->name = string("initial position");
217 init_waypoint->latitude = entity->latitude;
218 init_waypoint->longitude = entity->longitude;
219 init_waypoint->altitude = entity->altitude;
220 init_waypoint->speed = entity->speed;
221 init_waypoint->crossat = - 10000;
222 init_waypoint->gear_down = false;
223 init_waypoint->flaps_down = false;
224 init_waypoint->finished = false;
226 wpt_vector_iterator i = waypoints.begin();
227 while (i != waypoints.end())
229 //cerr << "Checking status of each waypoint: " << (*i)->name << endl;
230 SGWayPoint first(init_waypoint->longitude,
231 init_waypoint->latitude,
232 init_waypoint->altitude);
233 SGWayPoint curr ((*i)->longitude,
236 double crse, crsDiff;
238 curr.CourseAndDistance(first, &crse, &dist);
240 dist *= SG_METER_TO_NM;
242 // We're only interested in the absolute value of crsDiff
243 // wich should fall in the 0-180 deg range.
244 crsDiff = fabs(crse-course);
246 crsDiff = 360-crsDiff;
247 // These are the three conditions that we consider including
248 // in our flight plan:
249 // 1) current waypoint is less then 100 miles away OR
250 // 2) curren waypoint is ahead of us, at any distance
252 if ((dist > 20.0) && (crsDiff > 90.0) && ((*i)->name != string ("EOF")))
255 // Once we start including waypoints, we have to continue, even though
256 // one of the following way point would suffice.
257 // so once is the useWpt flag is set to true, we cannot reset it to false.
258 //cerr << "Discarding waypoint: " << (*i)->name
259 // << ": Course difference = " << crsDiff
260 // << "Course = " << course
261 // << "crse = " << crse << endl;
264 useCurrentWayPoint = true;
266 if (useCurrentWayPoint)
268 if ((dist > 100.0) && (useInitialWayPoint))
270 //waypoints.push_back(init_waypoint);;
271 waypoints.insert(i, init_waypoint);
272 //cerr << "Using waypoint : " << init_waypoint->name << endl;
274 //if (useInitialWayPoint)
276 // (*i)->speed = dist; // A hack
278 //waypoints.push_back( wpt );
279 //cerr << "Using waypoint : " << (*i)->name
280 // << ": course diff : " << crsDiff
281 // << "Course = " << course
282 // << "crse = " << crse << endl
283 // << "distance : " << dist << endl;
284 useInitialWayPoint = false;
291 i = waypoints.erase(i);
296 //for (i = waypoints.begin(); i != waypoints.end(); i++)
297 // cerr << "Using waypoint : " << (*i)->name << endl;
298 //wpt_iterator = waypoints.begin();
299 //cout << waypoints.size() << " waypoints read." << endl;
305 FGAIFlightPlan::~FGAIFlightPlan()
312 FGAIFlightPlan::waypoint* const
313 FGAIFlightPlan::getPreviousWaypoint( void ) const
315 if (wpt_iterator == waypoints.begin()) {
318 wpt_vector_iterator prev = wpt_iterator;
323 FGAIFlightPlan::waypoint* const
324 FGAIFlightPlan::getCurrentWaypoint( void ) const
326 return *wpt_iterator;
329 FGAIFlightPlan::waypoint* const
330 FGAIFlightPlan::getNextWaypoint( void ) const
332 wpt_vector_iterator i = waypoints.end();
333 i--; // end() points to one element after the last one.
334 if (wpt_iterator == i) {
337 wpt_vector_iterator next = wpt_iterator;
342 void FGAIFlightPlan::IncrementWaypoint(bool eraseWaypoints )
346 if (wpt_iterator == waypoints.begin())
350 delete *(waypoints.begin());
351 waypoints.erase(waypoints.begin());
352 wpt_iterator = waypoints.begin();
361 void FGAIFlightPlan::DecrementWaypoint(bool eraseWaypoints )
365 if (wpt_iterator == waypoints.end())
369 delete *(waypoints.end());
370 waypoints.erase(waypoints.end());
371 wpt_iterator = waypoints.end();
381 // gives distance in feet from a position to a waypoint
382 double FGAIFlightPlan::getDistanceToGo(double lat, double lon, waypoint* wp) const{
383 return SGGeodesy::distanceM(SGGeod::fromDeg(lon, lat),
384 SGGeod::fromDeg(wp->longitude, wp->latitude));
387 // sets distance in feet from a lead point to the current waypoint
388 void FGAIFlightPlan::setLeadDistance(double speed, double bearing,
389 waypoint* current, waypoint* next){
391 // Handle Ground steering
392 // At a turn rate of 30 degrees per second, it takes 12 seconds to do a full 360 degree turn
393 // So, to get an estimate of the turn radius, calculate the cicumference of the circle
394 // we travel on. Get the turn radius by dividing by PI (*2).
400 turn_radius = ((360/30)*15) / (2*M_PI);
402 turn_radius = 0.1911 * speed * speed; // an estimate for 25 degrees bank
404 double inbound = bearing;
405 double outbound = getBearing(current, next);
406 leadInAngle = fabs(inbound - outbound);
407 if (leadInAngle > 180.0)
408 leadInAngle = 360.0 - leadInAngle;
409 //if (leadInAngle < 30.0) // To prevent lead_dist from getting so small it is skipped
410 // leadInAngle = 30.0;
412 //lead_distance = turn_radius * sin(leadInAngle * SG_DEGREES_TO_RADIANS);
413 lead_distance = turn_radius * tan((leadInAngle * SG_DEGREES_TO_RADIANS)/2);
414 // if ((errno == EDOM) || (errno == ERANGE) || lead_distance < 1.0)
419 void FGAIFlightPlan::setLeadDistance(double distance_ft){
420 lead_distance = distance_ft;
424 double FGAIFlightPlan::getBearing(waypoint* first, waypoint* second) const{
425 return getBearing(first->latitude, first->longitude, second);
429 double FGAIFlightPlan::getBearing(double lat, double lon, waypoint* wp) const{
430 return SGGeodesy::courseDeg(SGGeod::fromDeg(lon, lat),
431 SGGeod::fromDeg(wp->longitude, wp->latitude));
434 void FGAIFlightPlan::deleteWaypoints()
436 for (wpt_vector_iterator i = waypoints.begin(); i != waypoints.end();i++)
441 // Delete all waypoints except the last,
442 // which we will recycle as the first waypoint in the next leg;
443 void FGAIFlightPlan::resetWaypoints()
445 if (waypoints.begin() == waypoints.end())
449 waypoint *wpt = new waypoint;
450 wpt_vector_iterator i = waypoints.end();
452 wpt->name = (*i)->name;
453 wpt->latitude = (*i)->latitude;
454 wpt->longitude = (*i)->longitude;
455 wpt->altitude = (*i)->altitude;
456 wpt->speed = (*i)->speed;
457 wpt->crossat = (*i)->crossat;
458 wpt->gear_down = (*i)->gear_down;
459 wpt->flaps_down= (*i)->flaps_down;
460 wpt->finished = false;
461 wpt->on_ground = (*i)->on_ground;
462 //cerr << "Recycling waypoint " << wpt->name << endl;
464 waypoints.push_back(wpt);
468 // Start flightplan over from the beginning
469 void FGAIFlightPlan::restart()
471 wpt_iterator = waypoints.begin();
475 void FGAIFlightPlan::deleteTaxiRoute()
482 int FGAIFlightPlan::getRouteIndex(int i) {
483 if ((i > 0) && (i < (int)waypoints.size())) {
484 return waypoints[i]->routeIndex;