| 1 | // Extracted from trafficrecord.cxx - Implementation of AIModels ATC code. |
| 2 | // |
| 3 | // Written by Durk Talsma, started September 2006. |
| 4 | // |
| 5 | // Copyright (C) 2006 Durk Talsma. |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | // |
| 21 | // $Id$ |
| 22 | |
| 23 | #include <config.h> |
| 24 | |
| 25 | #include <algorithm> |
| 26 | #include <cstdio> |
| 27 | #include <random> |
| 28 | |
| 29 | #include <osg/Geode> |
| 30 | #include <osg/Geometry> |
| 31 | #include <osg/MatrixTransform> |
| 32 | #include <osg/Shape> |
| 33 | |
| 34 | #include <simgear/scene/material/EffectGeode.hxx> |
| 35 | #include <simgear/scene/material/matlib.hxx> |
| 36 | #include <simgear/scene/material/mat.hxx> |
| 37 | #include <simgear/scene/util/OsgMath.hxx> |
| 38 | #include <simgear/timing/sg_time.hxx> |
| 39 | |
| 40 | #include <Scenery/scenery.hxx> |
| 41 | |
| 42 | #include "trafficcontrol.hxx" |
| 43 | #include "atc_mgr.hxx" |
| 44 | #include <AIModel/AIAircraft.hxx> |
| 45 | #include <AIModel/AIFlightPlan.hxx> |
| 46 | #include <AIModel/performancedata.hxx> |
| 47 | #include <Traffic/TrafficMgr.hxx> |
| 48 | #include <Airports/groundnetwork.hxx> |
| 49 | #include <Airports/dynamics.hxx> |
| 50 | #include <Airports/airport.hxx> |
| 51 | #include <Radio/radio.hxx> |
| 52 | #include <signal.h> |
| 53 | |
| 54 | #include <ATC/atc_mgr.hxx> |
| 55 | #include <ATC/trafficcontrol.hxx> |
| 56 | #include <ATC/ATCController.hxx> |
| 57 | #include <ATC/StartupController.hxx> |
| 58 | |
| 59 | using std::sort; |
| 60 | using std::string; |
| 61 | |
| 62 | /*************************************************************************** |
| 63 | * class FGStartupController |
| 64 | * subclass of FGATCController |
| 65 | **************************************************************************/ |
| 66 | FGStartupController::FGStartupController(FGAirportDynamics *par): |
| 67 | FGATCController() |
| 68 | { |
| 69 | parent = par; |
| 70 | } |
| 71 | |
| 72 | FGStartupController::~FGStartupController() |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | void FGStartupController::announcePosition(int id, |
| 77 | FGAIFlightPlan * intendedRoute, |
| 78 | int currentPosition, double lat, |
| 79 | double lon, double heading, |
| 80 | double speed, double alt, |
| 81 | double radius, int leg, |
| 82 | FGAIAircraft * ref) |
| 83 | { |
| 84 | init(); |
| 85 | // Search activeTraffic for a record matching our id |
| 86 | TrafficVectorIterator i = FGATCController::searchActiveTraffic(id); |
| 87 | |
| 88 | // Add a new TrafficRecord if no one exists for this aircraft. |
| 89 | if (i == activeTraffic.end() || activeTraffic.empty()) { |
| 90 | FGTrafficRecord rec; |
| 91 | rec.setId(id); |
| 92 | |
| 93 | rec.setPositionAndHeading(lat, lon, heading, speed, alt); |
| 94 | rec.setRunway(intendedRoute->getRunway()); |
| 95 | rec.setLeg(leg); |
| 96 | rec.setPositionAndIntentions(currentPosition, intendedRoute); |
| 97 | rec.setCallsign(ref->getCallSign()); |
| 98 | rec.setAircraft(ref); |
| 99 | rec.setHoldPosition(true); |
| 100 | activeTraffic.push_back(rec); |
| 101 | } else { |
| 102 | i->setPositionAndIntentions(currentPosition, intendedRoute); |
| 103 | i->setPositionAndHeading(lat, lon, heading, speed, alt); |
| 104 | |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void FGStartupController::updateAircraftInformation(int id, SGGeod geod, double heading, double speed, double alt, |
| 109 | double dt) |
| 110 | { |
| 111 | // Search activeTraffic for a record matching our id |
| 112 | TrafficVectorIterator i = FGATCController::searchActiveTraffic(id); |
| 113 | TrafficVectorIterator current; |
| 114 | |
| 115 | if (i == activeTraffic.end() || (activeTraffic.size() == 0)) { |
| 116 | SG_LOG(SG_ATC, SG_ALERT, |
| 117 | "AI error: updating aircraft without traffic record at " << SG_ORIGIN); |
| 118 | return; |
| 119 | } else { |
| 120 | i->setPositionAndHeading(geod.getLatitudeDeg(), geod.getLongitudeDeg(), heading, speed, alt); |
| 121 | current = i; |
| 122 | } |
| 123 | setDt(getDt() + dt); |
| 124 | |
| 125 | int state = i->getState(); |
| 126 | |
| 127 | // Sentry FLIGHTGEAR-2Q : don't crash on null TrafficRef |
| 128 | // Sentry FLIGHTGEAR-129: don't crash on null aircraft |
| 129 | if (!i->getAircraft() || !i->getAircraft()->getTrafficRef()) { |
| 130 | SG_LOG(SG_ATC, SG_ALERT, "AI traffic: updating aircraft without traffic ref"); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | // The user controlled aircraft should have crashed here, because it doesn't have a traffic reference. |
| 135 | // NOTE: if we create a traffic schedule for the user aircraft, we can use this to plan a flight. |
| 136 | time_t startTime = i->getAircraft()->getTrafficRef()->getDepartureTime(); |
| 137 | time_t now = globals->get_time_params()->get_cur_time(); |
| 138 | |
| 139 | |
| 140 | if (((startTime - now) > 60 && (startTime - now)%60 == 0) || |
| 141 | ((startTime - now) < 60 && (startTime - now) > 0)) { |
| 142 | SG_LOG(SG_ATC, SG_BULK, i->getAircraft()->getTrafficRef()->getCallSign() << " is scheduled to depart in " << startTime - now << " seconds. Available = " << available << " at parking " << getGateName(i->getAircraft())); |
| 143 | } |
| 144 | |
| 145 | if ((now - lastTransmission) > 3 + (rand() % 15)) { |
| 146 | available = true; |
| 147 | } |
| 148 | |
| 149 | if (now >(startTime + 0)) { |
| 150 | checkTransmissionState(ATCMessageState::NORMAL, ATCMessageState::NORMAL, i, now, MSG_ANNOUNCE_ENGINE_START, ATC_AIR_TO_GROUND); |
| 151 | } |
| 152 | if (now >(startTime + 60)) { |
| 153 | checkTransmissionState(ATCMessageState::ACK_HOLD, ATCMessageState::ACK_HOLD, i, now, MSG_REQUEST_ENGINE_START, ATC_AIR_TO_GROUND); |
| 154 | } |
| 155 | if (now >(startTime + 80)) { |
| 156 | checkTransmissionState(ATCMessageState::ACK_RESUME_TAXI, ATCMessageState::ACK_RESUME_TAXI, i, now, MSG_PERMIT_ENGINE_START, ATC_GROUND_TO_AIR); |
| 157 | } |
| 158 | if (now >(startTime + 100)) { |
| 159 | checkTransmissionState(ATCMessageState::TAXI_CLEARED, ATCMessageState::TAXI_CLEARED, i, now, MSG_ACKNOWLEDGE_ENGINE_START, ATC_AIR_TO_GROUND); |
| 160 | } |
| 161 | if (now >(startTime + 130)) { |
| 162 | checkTransmissionState(ATCMessageState::ACK_TAXI_CLEARED, ATCMessageState::ACK_TAXI_CLEARED, i, now, MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY, ATC_AIR_TO_GROUND); |
| 163 | } |
| 164 | if (now >(startTime + 140)) { |
| 165 | checkTransmissionState(ATCMessageState::START_TAXI, ATCMessageState::START_TAXI, i, now, MSG_INITIATE_CONTACT, ATC_AIR_TO_GROUND); |
| 166 | } |
| 167 | if (now >(startTime + 150)) { |
| 168 | checkTransmissionState(ATCMessageState::REPORT_RUNWAY, ATCMessageState::REPORT_RUNWAY, i, now, MSG_ACKNOWLEDGE_INITIATE_CONTACT, ATC_GROUND_TO_AIR); |
| 169 | } |
| 170 | if (now >(startTime + 180)) { |
| 171 | checkTransmissionState(ATCMessageState::ACK_REPORT_RUNWAY, ATCMessageState::ACK_REPORT_RUNWAY, i, now, MSG_REQUEST_PUSHBACK_CLEARANCE, ATC_AIR_TO_GROUND); |
| 172 | } |
| 173 | if ((state == ATCMessageState::SWITCH_GROUND_TOWER) && available) { |
| 174 | if (now > startTime + 200) { |
| 175 | if (i->pushBackAllowed()) { |
| 176 | i->allowRepeatedTransmissions(); |
| 177 | transmit(&(*i), &(*parent), MSG_PERMIT_PUSHBACK_CLEARANCE, |
| 178 | ATC_GROUND_TO_AIR, true); |
| 179 | i->updateState(); |
| 180 | } else { |
| 181 | transmit(&(*i), &(*parent), MSG_HOLD_PUSHBACK_CLEARANCE, |
| 182 | ATC_GROUND_TO_AIR, true); |
| 183 | i->suppressRepeatedTransmissions(); |
| 184 | } |
| 185 | lastTransmission = now; |
| 186 | available = false; |
| 187 | } |
| 188 | } |
| 189 | if ((state == ATCMessageState::ACK_SWITCH_GROUND_TOWER) && available) { |
| 190 | i->setHoldPosition(false); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // Note that this function is copied from simgear. for maintenance purposes, it's probably better to make a general function out of that. |
| 195 | static void WorldCoordinate(osg::Matrix& obj_pos, double lat, |
| 196 | double lon, double elev, double hdg, double slope) |
| 197 | { |
| 198 | SGGeod geod = SGGeod::fromDegM(lon, lat, elev); |
| 199 | obj_pos = makeZUpFrame(geod); |
| 200 | // hdg is not a compass heading, but a counter-clockwise rotation |
| 201 | // around the Z axis |
| 202 | obj_pos.preMult(osg::Matrix::rotate(hdg * SGD_DEGREES_TO_RADIANS, |
| 203 | 0.0, 0.0, 1.0)); |
| 204 | obj_pos.preMult(osg::Matrix::rotate(slope * SGD_DEGREES_TO_RADIANS, |
| 205 | 0.0, 1.0, 0.0)); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | void FGStartupController::render(bool visible) |
| 210 | { |
| 211 | SGMaterialLib *matlib = globals->get_matlib(); |
| 212 | if (group) { |
| 213 | //int nr = ; |
| 214 | globals->get_scenery()->get_scene_graph()->removeChild(group); |
| 215 | //while (group->getNumChildren()) { |
| 216 | // SG_LOG(SG_ATC, SG_BULK, "Number of children: " << group->getNumChildren()); |
| 217 | //simgear::EffectGeode* geode = (simgear::EffectGeode*) group->getChild(0); |
| 218 | //osg::MatrixTransform *obj_trans = (osg::MatrixTransform*) group->getChild(0); |
| 219 | //geode->releaseGLObjects(); |
| 220 | //group->removeChild(geode); |
| 221 | //delete geode; |
| 222 | group = 0; |
| 223 | } |
| 224 | if (visible) { |
| 225 | SG_LOG(SG_ATC, SG_BULK, "Rendering startup controller"); |
| 226 | group = new osg::Group; |
| 227 | FGScenery * local_scenery = globals->get_scenery(); |
| 228 | //double elevation_meters = 0.0; |
| 229 | //double elevation_feet = 0.0; |
| 230 | |
| 231 | FGGroundNetwork* groundNet = parent->parent()->groundNetwork(); |
| 232 | |
| 233 | //for ( FGTaxiSegmentVectorIterator i = segments.begin(); i != segments.end(); i++) { |
| 234 | double dx = 0; |
| 235 | time_t now = globals->get_time_params()->get_cur_time(); |
| 236 | |
| 237 | for (TrafficVectorIterator i = activeTraffic.begin(); i != activeTraffic.end(); ++i) { |
| 238 | if (i->isActive(300)) { |
| 239 | // Handle start point |
| 240 | int pos = i->getCurrentPosition(); |
| 241 | SG_LOG(SG_ATC, SG_BULK, "rendering for " << i->getAircraft()->getCallSign() << "pos = " << pos); |
| 242 | if (pos > 0) { |
| 243 | FGTaxiSegment *segment = groundNet->findSegment(pos); |
| 244 | SGGeod start = i->getPos(); |
| 245 | SGGeod end (segment->getEnd()->geod()); |
| 246 | |
| 247 | double length = SGGeodesy::distanceM(start, end); |
| 248 | //heading = SGGeodesy::headingDeg(start->geod(), end->geod()); |
| 249 | |
| 250 | double az2, heading; //, distanceM; |
| 251 | SGGeodesy::inverse(start, end, heading, az2, length); |
| 252 | double coveredDistance = length * 0.5; |
| 253 | SGGeod center; |
| 254 | SGGeodesy::direct(start, heading, coveredDistance, center, az2); |
| 255 | SG_LOG(SG_ATC, SG_BULK, "Active Aircraft : Centerpoint = (" << center.getLatitudeDeg() << ", " << center.getLongitudeDeg() << "). Heading = " << heading); |
| 256 | /////////////////////////////////////////////////////////////////////////////// |
| 257 | // Make a helper function out of this |
| 258 | osg::Matrix obj_pos; |
| 259 | osg::MatrixTransform *obj_trans = new osg::MatrixTransform; |
| 260 | obj_trans->setDataVariance(osg::Object::STATIC); |
| 261 | // Experimental: Calculate slope here, based on length, and the individual elevations |
| 262 | double elevationStart; |
| 263 | if (isUserAircraft((i)->getAircraft())) { |
| 264 | elevationStart = fgGetDouble("/position/ground-elev-m"); |
| 265 | } else { |
| 266 | elevationStart = ((i)->getAircraft()->_getAltitude() * SG_FEET_TO_METER); |
| 267 | } |
| 268 | double elevationEnd = segment->getEnd()->getElevationM(); |
| 269 | if ((elevationEnd == 0) || (elevationEnd == parent->getElevation())) { |
| 270 | SGGeod center2 = end; |
| 271 | center2.setElevationM(SG_MAX_ELEVATION_M); |
| 272 | if (local_scenery->get_elevation_m( center2, elevationEnd, NULL )) { |
| 273 | //elevation_feet = elevationEnd * SG_METER_TO_FEET + 0.5; |
| 274 | //elevation_meters += 0.5; |
| 275 | } |
| 276 | else { |
| 277 | elevationEnd = parent->getElevation(); |
| 278 | } |
| 279 | segment->getEnd()->setElevation(elevationEnd); |
| 280 | } |
| 281 | |
| 282 | double elevationMean = (elevationStart + elevationEnd) / 2.0; |
| 283 | double elevDiff = elevationEnd - elevationStart; |
| 284 | |
| 285 | double slope = atan2(elevDiff, length) * SGD_RADIANS_TO_DEGREES; |
| 286 | |
| 287 | SG_LOG(SG_ATC, SG_BULK, "1. Using mean elevation : " << elevationMean << " and " << slope); |
| 288 | |
| 289 | WorldCoordinate( obj_pos, center.getLatitudeDeg(), center.getLongitudeDeg(), elevationMean + 0.5 + dx, -(heading), slope ); |
| 290 | ; |
| 291 | |
| 292 | obj_trans->setMatrix( obj_pos ); |
| 293 | //osg::Vec3 center(0, 0, 0) |
| 294 | |
| 295 | float width = length /2.0; |
| 296 | osg::Vec3 corner(-width, 0, 0.25f); |
| 297 | osg::Vec3 widthVec(2*width + 1, 0, 0); |
| 298 | osg::Vec3 heightVec(0, 1, 0); |
| 299 | osg::Geometry* geometry; |
| 300 | geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec); |
| 301 | simgear::EffectGeode* geode = new simgear::EffectGeode; |
| 302 | geode->setName("test"); |
| 303 | geode->addDrawable(geometry); |
| 304 | //osg::Node *custom_obj; |
| 305 | SGMaterial *mat; |
| 306 | if (segment->hasBlock(now)) { |
| 307 | mat = matlib->find("UnidirectionalTaperRed", center); |
| 308 | } else { |
| 309 | mat = matlib->find("UnidirectionalTaperGreen", center); |
| 310 | } |
| 311 | if (mat) |
| 312 | geode->setEffect(mat->get_effect()); |
| 313 | obj_trans->addChild(geode); |
| 314 | // wire as much of the scene graph together as we can |
| 315 | //->addChild( obj_trans ); |
| 316 | group->addChild( obj_trans ); |
| 317 | ///////////////////////////////////////////////////////////////////// |
| 318 | } else { |
| 319 | SG_LOG(SG_ATC, SG_DEBUG, "BIG FAT WARNING: current position is here : " << pos); |
| 320 | } |
| 321 | for (intVecIterator j = (i)->getIntentions().begin(); j != (i)->getIntentions().end(); ++j) { |
| 322 | osg::Matrix obj_pos; |
| 323 | int k = (*j); |
| 324 | if (k > 0) { |
| 325 | SG_LOG(SG_ATC, SG_BULK, "rendering for " << i->getAircraft()->getCallSign() << "intention = " << k); |
| 326 | osg::MatrixTransform *obj_trans = new osg::MatrixTransform; |
| 327 | obj_trans->setDataVariance(osg::Object::STATIC); |
| 328 | FGTaxiSegment *segment = groundNet->findSegment(k); |
| 329 | |
| 330 | double elevationStart = segment->getStart()->getElevationM(); |
| 331 | double elevationEnd = segment->getEnd ()->getElevationM(); |
| 332 | if ((elevationStart == 0) || (elevationStart == parent->getElevation())) { |
| 333 | SGGeod center2 = segment->getStart()->geod(); |
| 334 | center2.setElevationM(SG_MAX_ELEVATION_M); |
| 335 | if (local_scenery->get_elevation_m( center2, elevationStart, NULL )) { |
| 336 | //elevation_feet = elevationStart * SG_METER_TO_FEET + 0.5; |
| 337 | //elevation_meters += 0.5; |
| 338 | } |
| 339 | else { |
| 340 | elevationStart = parent->getElevation(); |
| 341 | } |
| 342 | segment->getStart()->setElevation(elevationStart); |
| 343 | } |
| 344 | if ((elevationEnd == 0) || (elevationEnd == parent->getElevation())) { |
| 345 | SGGeod center2 = segment->getEnd()->geod(); |
| 346 | center2.setElevationM(SG_MAX_ELEVATION_M); |
| 347 | if (local_scenery->get_elevation_m( center2, elevationEnd, NULL )) { |
| 348 | //elevation_feet = elevationEnd * SG_METER_TO_FEET + 0.5; |
| 349 | //elevation_meters += 0.5; |
| 350 | } |
| 351 | else { |
| 352 | elevationEnd = parent->getElevation(); |
| 353 | } |
| 354 | segment->getEnd()->setElevation(elevationEnd); |
| 355 | } |
| 356 | |
| 357 | double elevationMean = (elevationStart + elevationEnd) / 2.0; |
| 358 | double elevDiff = elevationEnd - elevationStart; |
| 359 | double length = segment->getLength(); |
| 360 | double slope = atan2(elevDiff, length) * SGD_RADIANS_TO_DEGREES; |
| 361 | |
| 362 | SG_LOG(SG_ATC, SG_BULK, "2. Using mean elevation : " << elevationMean << " and " << slope); |
| 363 | |
| 364 | SGGeod segCenter(segment->getCenter()); |
| 365 | WorldCoordinate( obj_pos, segCenter.getLatitudeDeg(), |
| 366 | segCenter.getLongitudeDeg(), elevationMean + 0.5 + dx, -(segment->getHeading()), slope ); |
| 367 | |
| 368 | //WorldCoordinate( obj_pos, segment->getLatitude(), segment->getLongitude(), parent->getElevation()+8+dx, -(segment->getHeading()) ); |
| 369 | |
| 370 | obj_trans->setMatrix( obj_pos ); |
| 371 | //osg::Vec3 center(0, 0, 0) |
| 372 | |
| 373 | float width = segment->getLength() /2.0; |
| 374 | osg::Vec3 corner(-width, 0, 0.25f); |
| 375 | osg::Vec3 widthVec(2*width + 1, 0, 0); |
| 376 | osg::Vec3 heightVec(0, 1, 0); |
| 377 | osg::Geometry* geometry; |
| 378 | geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec); |
| 379 | simgear::EffectGeode* geode = new simgear::EffectGeode; |
| 380 | geode->setName("test"); |
| 381 | geode->addDrawable(geometry); |
| 382 | //osg::Node *custom_obj; |
| 383 | SGMaterial *mat; |
| 384 | if (segment->hasBlock(now)) { |
| 385 | mat = matlib->find("UnidirectionalTaperRed", segCenter); |
| 386 | } else { |
| 387 | mat = matlib->find("UnidirectionalTaperGreen", segCenter); |
| 388 | } |
| 389 | if (mat) |
| 390 | geode->setEffect(mat->get_effect()); |
| 391 | obj_trans->addChild(geode); |
| 392 | // wire as much of the scene graph together as we can |
| 393 | //->addChild( obj_trans ); |
| 394 | group->addChild( obj_trans ); |
| 395 | } else { |
| 396 | SG_LOG(SG_ATC, SG_DEBUG, "BIG FAT WARNING: k is here : " << pos); |
| 397 | } |
| 398 | } |
| 399 | dx += 0.2; |
| 400 | } |
| 401 | } |
| 402 | globals->get_scenery()->get_scene_graph()->addChild(group); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | string FGStartupController::getName() { |
| 407 | return string(parent->parent()->getName() + "-Startup"); |
| 408 | } |
| 409 | |
| 410 | void FGStartupController::update(double dt) |
| 411 | { |
| 412 | FGATCController::eraseDeadTraffic(); |
| 413 | } |
| 414 | |
| 415 | int FGStartupController::getFrequency() { |
| 416 | int groundFreq = parent->getGroundFrequency(2); |
| 417 | int towerFreq = parent->getTowerFrequency(2); |
| 418 | return groundFreq>0?groundFreq:towerFreq; |
| 419 | } |
| 420 |