]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AICarrier.cxx
182bcad874f3a34cc63bfde25474f401507c7697
[flightgear.git] / src / AIModel / AICarrier.cxx
1 // FGAICarrier - FGAIShip-derived class creates an AI aircraft carrier
2 //
3 // Written by David Culp, started October 2004.
4 // - davidculp2@comcast.net
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include <algorithm>
25 #include <string>
26 #include <vector>
27
28 #include <osg/NodeVisitor>
29
30 #include <simgear/sg_inlines.h>
31 #include <simgear/math/SGMath.hxx>
32 #include <simgear/math/sg_geodesy.hxx>
33 #include <simgear/scene/util/SGNodeMasks.hxx>
34
35 #include <math.h>
36 #include <Main/util.hxx>
37 #include <Main/viewer.hxx>
38
39 #include "AICarrier.hxx"
40
41 class FGCarrierVisitor : public osg::NodeVisitor {
42 public:
43   FGCarrierVisitor(FGAICarrier* carrier,
44                    const std::list<std::string>& wireObjects,
45                    const std::list<std::string>& catapultObjects,
46                    const std::list<std::string>& solidObjects) :
47     osg::NodeVisitor(osg::NodeVisitor::NODE_VISITOR,
48                      osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
49     mWireObjects(wireObjects),
50     mCatapultObjects(catapultObjects),
51     mSolidObjects(solidObjects),
52     mFoundHot(false),
53     mCarrier(carrier)
54   { }
55   virtual void apply(osg::Node& node)
56   {
57     osg::ref_ptr<osg::Referenced> oldUserData = mUserData;
58     bool oldFoundHot = mFoundHot;
59     mFoundHot = false;
60
61     if (std::find(mWireObjects.begin(), mWireObjects.end(), node.getName())
62         != mWireObjects.end()) {
63       mFoundHot = true;
64       mUserData = FGAICarrierHardware::newWire(mCarrier);
65     }
66     if (std::find(mCatapultObjects.begin(), mCatapultObjects.end(), node.getName())
67         != mCatapultObjects.end()) {
68       mFoundHot = true;
69       mUserData = FGAICarrierHardware::newCatapult(mCarrier);
70     }
71     if (std::find(mSolidObjects.begin(), mSolidObjects.end(), node.getName())
72         != mSolidObjects.end()) {
73       mFoundHot = true;
74       mUserData = FGAICarrierHardware::newSolid(mCarrier);
75       //SG_LOG(SG_GENERAL, SG_ALERT, "AICarrierVisitor::apply() solidObject" );
76     }
77     node.setUserData(mUserData.get());
78
79     traverse(node);
80
81     mFoundHot = oldFoundHot || mFoundHot;
82
83     if (mFoundHot) {
84       node.setNodeMask(node.getNodeMask() | SG_NODEMASK_TERRAIN_BIT);
85     } else
86       node.setNodeMask(node.getNodeMask() & ~SG_NODEMASK_TERRAIN_BIT);
87
88     mUserData = oldUserData;
89   }
90   
91 private:
92   std::list<std::string> mWireObjects;
93   std::list<std::string> mCatapultObjects;
94   std::list<std::string> mSolidObjects;
95   bool mFoundHot;
96   FGAICarrier* mCarrier;
97   osg::ref_ptr<osg::Referenced> mUserData;
98 };
99
100 FGAICarrier::FGAICarrier() : FGAIShip(otCarrier) {
101 }
102
103 FGAICarrier::~FGAICarrier() {
104 }
105
106 void FGAICarrier::readFromScenario(SGPropertyNode* scFileNode) {
107   if (!scFileNode)
108     return;
109
110   FGAIShip::readFromScenario(scFileNode);
111
112   setRadius(scFileNode->getDoubleValue("turn-radius-ft", 2000));
113   setSign(scFileNode->getStringValue("pennant-number"));
114   setWind_from_east(scFileNode->getDoubleValue("wind_from_east", 0));
115   setWind_from_north(scFileNode->getDoubleValue("wind_from_north", 0));
116   setTACANChannelID(scFileNode->getStringValue("TACAN-channel-ID", "029Y"));
117   setMaxLat(scFileNode->getDoubleValue("max-lat", 0));
118   setMinLat(scFileNode->getDoubleValue("min-lat", 0));
119   setMaxLong(scFileNode->getDoubleValue("max-long", 0));
120   setMinLong(scFileNode->getDoubleValue("min-long", 0));
121
122   SGPropertyNode* flols = scFileNode->getChild("flols-pos");
123   if (flols) {
124     // Transform to the right coordinate frame, configuration is done in
125     // the usual x-back, y-right, z-up coordinates, computations
126     // in the simulation usual body x-forward, y-right, z-down coordinates
127     flols_off(0) = - flols->getDoubleValue("x-offset-m", 0);
128     flols_off(1) = flols->getDoubleValue("y-offset-m", 0);
129     flols_off(2) = - flols->getDoubleValue("z-offset-m", 0);
130   } else
131     flols_off = SGVec3d::zeros();
132
133   std::vector<SGPropertyNode_ptr> props = scFileNode->getChildren("wire");
134   std::vector<SGPropertyNode_ptr>::const_iterator it;
135   for (it = props.begin(); it != props.end(); ++it) {
136     std::string s = (*it)->getStringValue();
137     if (!s.empty())
138       wire_objects.push_back(s);
139   }
140
141   props = scFileNode->getChildren("catapult");
142   for (it = props.begin(); it != props.end(); ++it) {
143     std::string s = (*it)->getStringValue();
144     if (!s.empty())
145       catapult_objects.push_back(s);
146   }
147
148   props = scFileNode->getChildren("solid");
149   for (it = props.begin(); it != props.end(); ++it) {
150     std::string s = (*it)->getStringValue();
151     if (!s.empty())
152       solid_objects.push_back(s);
153   }
154
155   props = scFileNode->getChildren("parking-pos");
156   for (it = props.begin(); it != props.end(); ++it) {
157     string name = (*it)->getStringValue("name", "unnamed");
158     // Transform to the right coordinate frame, configuration is done in
159     // the usual x-back, y-right, z-up coordinates, computations
160     // in the simulation usual body x-forward, y-right, z-down coordinates
161     double offset_x = -(*it)->getDoubleValue("x-offset-m", 0);
162     double offset_y = (*it)->getDoubleValue("y-offset-m", 0);
163     double offset_z = -(*it)->getDoubleValue("z-offset-m", 0);
164     double hd = (*it)->getDoubleValue("heading-offset-deg", 0);
165     ParkPosition pp(name, SGVec3d(offset_x, offset_y, offset_z), hd);
166     ppositions.push_back(pp);
167   }
168 }
169
170 void FGAICarrier::setWind_from_east(double fps) {
171     wind_from_east = fps;
172 }
173
174 void FGAICarrier::setWind_from_north(double fps) {
175     wind_from_north = fps;
176 }
177
178 void FGAICarrier::setMaxLat(double deg) {
179     max_lat = fabs(deg);
180 }
181
182 void FGAICarrier::setMinLat(double deg) {
183     min_lat = fabs(deg);
184 }
185
186 void FGAICarrier::setMaxLong(double deg) {
187     max_long = fabs(deg);
188 }
189
190 void FGAICarrier::setMinLong(double deg) {
191     min_long = fabs(deg);
192 }
193
194 void FGAICarrier::setSign(const string& s) {
195     sign = s;
196 }
197
198 void FGAICarrier::setTACANChannelID(const string& id) {
199     TACAN_channel_id = id;
200 }
201
202 void FGAICarrier::getVelocityWrtEarth(SGVec3d& v, SGVec3d& omega, SGVec3d& pivot) {
203     v = vel_wrt_earth;
204     omega = rot_wrt_earth;
205     pivot = rot_pivot_wrt_earth;
206 }
207
208 void FGAICarrier::update(double dt) {
209     // For computation of rotation speeds we just use finite differences here.
210     // That is perfectly valid since this thing is not driven by accelerations
211     // but by just apply discrete changes at its velocity variables.
212     // Update the velocity information stored in those nodes.
213     // Transform that one to the horizontal local coordinate system.
214     SGQuatd ec2hl = SGQuatd::fromLonLat(pos);
215     // The orientation of the carrier wrt the horizontal local frame
216     SGQuatd hl2body = SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
217     // and postrotate the orientation of the AIModel wrt the horizontal
218     // local frame
219     SGQuatd ec2body = ec2hl*hl2body;
220     // The cartesian position of the carrier in the wgs84 world
221     SGVec3d cartPos = SGVec3d::fromGeod(pos);
222     // Store for later use by the groundcache
223     rot_pivot_wrt_earth = cartPos;
224
225     // Compute the velocity in m/s in the earth centered coordinate system axis
226     double v_north = 0.51444444*speed*cos(hdg * SGD_DEGREES_TO_RADIANS);
227     double v_east  = 0.51444444*speed*sin(hdg * SGD_DEGREES_TO_RADIANS);
228     vel_wrt_earth = ec2hl.backTransform(SGVec3d(v_north, v_east, 0));
229
230     // Now update the position and heading. This will compute new hdg and
231     // roll values required for the rotation speed computation.
232     FGAIShip::update(dt);
233
234
235     //automatic turn into wind with a target wind of 25 kts otd
236     if(turn_to_launch_hdg){
237         TurnToLaunch();
238     } else if(OutsideBox() || returning) {// check that the carrier is inside the operating box
239         ReturnToBox();
240     } else {
241         TurnToBase();
242     }
243
244     // Only change these values if we are able to compute them safely
245     if (dt < DBL_MIN)
246       rot_wrt_earth = SGVec3d::zeros();
247     else {
248       // Now here is the finite difference ...
249
250       // Transform that one to the horizontal local coordinate system.
251       SGQuatd ec2hlNew = SGQuatd::fromLonLat(pos);
252       // compute the new orientation
253       SGQuatd hl2bodyNew = SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
254       // The rotation difference
255       SGQuatd dOr = inverse(ec2body)*ec2hlNew*hl2bodyNew;
256       SGVec3d dOrAngleAxis;
257       dOr.getAngleAxis(dOrAngleAxis);
258       // divided by the time difference provides a rotation speed vector
259       dOrAngleAxis /= dt;
260
261       // now rotate the rotation speed vector back into the
262       // earth centered frames coordinates
263       dOrAngleAxis = ec2body.backTransform(dOrAngleAxis);
264 //       dOrAngleAxis = hl2body.backTransform(dOrAngleAxis);
265 //       dOrAngleAxis(1) = 0;
266 //       dOrAngleAxis = ec2hl.backTransform(dOrAngleAxis);
267       rot_wrt_earth = dOrAngleAxis;
268     }
269
270     UpdateWind(dt);
271     UpdateElevator(dt, transition_time);
272     UpdateJBD(dt, jbd_transition_time);
273     // For the flols reuse some computations done above ...
274
275     // The position of the eyepoint - at least near that ...
276     SGVec3d eyePos(globals->get_current_view()->get_view_pos());
277     // Add the position offset of the AIModel to gain the earth
278     // centered position
279     SGVec3d eyeWrtCarrier = eyePos - cartPos;
280     // rotate the eyepoint wrt carrier vector into the carriers frame
281     eyeWrtCarrier = ec2body.transform(eyeWrtCarrier);
282     // the eyepoints vector wrt the flols position
283     SGVec3d eyeWrtFlols = eyeWrtCarrier - flols_off;
284     
285     // the distance from the eyepoint to the flols
286     dist = norm(eyeWrtFlols);
287     
288     // now the angle, positive angles are upwards
289     if (fabs(dist) < SGLimits<float>::min()) {
290       angle = 0;
291     } else {
292       double sAngle = -eyeWrtFlols(2)/dist;
293       sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
294       angle = SGMiscd::rad2deg(asin(sAngle));
295     }
296     
297     // set the value of source
298     if ( angle <= 4.35 && angle > 4.01 )
299       source = 1;
300     else if ( angle <= 4.01 && angle > 3.670 )
301       source = 2;
302     else if ( angle <= 3.670 && angle > 3.330 )
303       source = 3;
304     else if ( angle <= 3.330 && angle > 2.990 )
305       source = 4;
306     else if ( angle <= 2.990 && angle > 2.650 )
307       source = 5;
308     else if ( angle <= 2.650 )
309       source = 6;
310     else
311       source = 0;
312 } //end update
313
314 bool FGAICarrier::init(bool search_in_AI_path) {
315     if (!FGAIShip::init(search_in_AI_path))
316         return false;
317
318     _longitude_node = fgGetNode("/position/longitude-deg", true);
319     _latitude_node = fgGetNode("/position/latitude-deg", true);
320     _altitude_node = fgGetNode("/position/altitude-ft", true);
321
322     _launchbar_state_node = fgGetNode("/gear/launchbar/state", true);
323
324     _surface_wind_from_deg_node =
325             fgGetNode("/environment/config/boundary/entry[0]/wind-from-heading-deg", true);
326     _surface_wind_speed_node =
327             fgGetNode("/environment/config/boundary/entry[0]/wind-speed-kt", true);
328
329
330     turn_to_launch_hdg = false;
331     returning = false;
332
333     mOpBoxPos = pos;
334     base_course = hdg;
335     base_speed = speed;
336
337     pos_norm = 0;
338     elevators = false;
339     transition_time = 150;
340     time_constant = 0.005;
341     jbd_pos_norm = raw_jbd_pos_norm = 0;
342     jbd = false ;
343     jbd_transition_time = 3;
344     jbd_time_constant = 0.1;
345     return true;
346 }
347
348 void FGAICarrier::initModel(osg::Node *node)
349 {
350     // SG_LOG(SG_GENERAL, SG_BULK, "AICarrier::initModel()" );
351     FGAIShip::initModel(node);
352     // process the 3d model here
353     // mark some objects solid, mark the wires ...
354
355     // The model should be used for altitude computations.
356     // To avoid that every detail in a carrier 3D model will end into
357     // the aircraft local cache, only set the HOT traversal bit on
358     // selected objects.
359
360     // Clear the HOT traversal flag
361     // Selectively set that flag again for wires/cats/solid objects.
362     // Attach a pointer to this carrier class to those objects.
363     // SG_LOG(SG_GENERAL, SG_BULK, "AICarrier::initModel() visit" );
364     FGCarrierVisitor carrierVisitor(this, wire_objects, catapult_objects, solid_objects);
365     model->accept(carrierVisitor);
366 //    model->setNodeMask(node->getNodeMask() & SG_NODEMASK_TERRAIN_BIT | model->getNodeMask());
367 }
368
369 void FGAICarrier::bind() {
370     FGAIShip::bind();
371
372     props->untie("velocities/true-airspeed-kt");
373
374     props->tie("controls/flols/source-lights",
375                 SGRawValuePointer<int>(&source));
376     props->tie("controls/flols/distance-m",
377                 SGRawValuePointer<double>(&dist));
378     props->tie("controls/flols/angle-degs",
379                 SGRawValuePointer<double>(&angle));
380     props->tie("controls/turn-to-launch-hdg",
381                 SGRawValuePointer<bool>(&turn_to_launch_hdg));
382     props->tie("controls/in-to-wind",
383                 SGRawValuePointer<bool>(&turn_to_launch_hdg));
384     props->tie("controls/base-course-deg",
385                 SGRawValuePointer<double>(&base_course));
386     props->tie("controls/base-speed-kts",
387                 SGRawValuePointer<double>(&base_speed));
388     props->tie("controls/start-pos-lat-deg",
389                SGRawValueMethods<SGGeod,double>(pos, &SGGeod::getLatitudeDeg));
390     props->tie("controls/start-pos-long-deg",
391                SGRawValueMethods<SGGeod,double>(pos, &SGGeod::getLongitudeDeg));
392     props->tie("velocities/speed-kts",
393                 SGRawValuePointer<double>(&speed));
394     props->tie("environment/surface-wind-speed-true-kts",
395                 SGRawValuePointer<double>(&wind_speed_kts));
396     props->tie("environment/surface-wind-from-true-degs",
397                 SGRawValuePointer<double>(&wind_from_deg));
398     props->tie("environment/rel-wind-from-degs",
399                 SGRawValuePointer<double>(&rel_wind_from_deg));
400     props->tie("environment/rel-wind-from-carrier-hdg-degs",
401                 SGRawValuePointer<double>(&rel_wind));
402     props->tie("environment/rel-wind-speed-kts",
403                 SGRawValuePointer<double>(&rel_wind_speed_kts));
404     props->tie("controls/flols/wave-off-lights",
405                 SGRawValuePointer<bool>(&wave_off_lights));
406     props->tie("controls/elevators",
407                 SGRawValuePointer<bool>(&elevators));
408     props->tie("surface-positions/elevators-pos-norm",
409                 SGRawValuePointer<double>(&pos_norm));
410     props->tie("controls/elevators-trans-time-s",
411                 SGRawValuePointer<double>(&transition_time));
412     props->tie("controls/elevators-time-constant",
413                 SGRawValuePointer<double>(&time_constant));
414     props->tie("controls/jbd",
415         SGRawValuePointer<bool>(&jbd));
416     props->tie("surface-positions/jbd-pos-norm",
417         SGRawValuePointer<double>(&jbd_pos_norm));
418     props->tie("controls/jbd-trans-time-s",
419         SGRawValuePointer<double>(&jbd_transition_time));
420     props->tie("controls/jbd-time-constant",
421         SGRawValuePointer<double>(&jbd_time_constant));
422
423     props->setBoolValue("controls/flols/cut-lights", false);
424     props->setBoolValue("controls/flols/wave-off-lights", false);
425     props->setBoolValue("controls/flols/cond-datum-lights", true);
426     props->setBoolValue("controls/crew", false);
427     props->setStringValue("navaids/tacan/channel-ID", TACAN_channel_id.c_str());
428     props->setStringValue("sign", sign.c_str());
429     props->setBoolValue("controls/lighting/deck-lights", false);
430     props->setDoubleValue("controls/lighting/flood-lights-red-norm", 0);
431 }
432
433
434 void FGAICarrier::unbind() {
435     FGAIShip::unbind();
436
437     props->untie("velocities/true-airspeed-kt");
438     props->untie("controls/flols/source-lights");
439     props->untie("controls/flols/distance-m");
440     props->untie("controls/flols/angle-degs");
441     props->untie("controls/turn-to-launch-hdg");
442     props->untie("velocities/speed-kts");
443     props->untie("environment/wind-speed-true-kts");
444     props->untie("environment/wind-from-true-degs");
445     props->untie("environment/rel-wind-from-degs");
446     props->untie("environment/rel-wind-speed-kts");
447     props->untie("controls/flols/wave-off-lights");
448     props->untie("controls/elevators");
449     props->untie("surface-positions/elevators-pos-norm");
450     props->untie("controls/elevators-trans-time-secs");
451     props->untie("controls/elevators-time-constant");
452     props->untie("controls/jbd");
453     props->untie("surface-positions/jbd-pos-norm");
454     props->untie("controls/jbd-trans-time-s");
455     props->untie("controls/jbd-time-constant");
456
457 }
458
459
460 bool FGAICarrier::getParkPosition(const string& id, SGGeod& geodPos,
461                                   double& hdng, SGVec3d& uvw)
462 {
463
464     // FIXME: does not yet cover rotation speeds.
465     list<ParkPosition>::iterator it = ppositions.begin();
466     while (it != ppositions.end()) {
467         // Take either the specified one or the first one ...
468         if ((*it).name == id || id.empty()) {
469             ParkPosition ppos = *it;
470             SGVec3d cartPos = getCartPosAt(ppos.offset);
471             geodPos = SGGeod::fromCart(cartPos);
472             hdng = hdg + ppos.heading_deg;
473             double shdng = sin(ppos.heading_deg * SGD_DEGREES_TO_RADIANS);
474             double chdng = cos(ppos.heading_deg * SGD_DEGREES_TO_RADIANS);
475             double speed_fps = speed*1.6878099;
476             uvw = SGVec3d(chdng*speed_fps, shdng*speed_fps, 0);
477             return true;
478         }
479         ++it;
480     }
481
482     return false;
483 }
484
485 // find relative wind
486 void FGAICarrier::UpdateWind( double dt) {
487
488     //get the surface wind speed and direction
489     wind_from_deg = _surface_wind_from_deg_node->getDoubleValue();
490     wind_speed_kts  = _surface_wind_speed_node->getDoubleValue();
491
492     //calculate the surface wind speed north and east in kts
493     double wind_speed_from_north_kts = cos( wind_from_deg / SGD_RADIANS_TO_DEGREES )* wind_speed_kts ;
494     double wind_speed_from_east_kts  = sin( wind_from_deg / SGD_RADIANS_TO_DEGREES )* wind_speed_kts ;
495
496     //calculate the carrier speed north and east in kts
497     double speed_north_kts = cos( hdg / SGD_RADIANS_TO_DEGREES )* speed ;
498     double speed_east_kts  = sin( hdg / SGD_RADIANS_TO_DEGREES )* speed ;
499
500     //calculate the relative wind speed north and east in kts
501     double rel_wind_speed_from_east_kts = wind_speed_from_east_kts + speed_east_kts;
502     double rel_wind_speed_from_north_kts = wind_speed_from_north_kts + speed_north_kts;
503
504     //combine relative speeds north and east to get relative windspeed in kts
505     rel_wind_speed_kts = sqrt((rel_wind_speed_from_east_kts * rel_wind_speed_from_east_kts)
506     + (rel_wind_speed_from_north_kts * rel_wind_speed_from_north_kts));
507
508     //calculate the relative wind direction
509     rel_wind_from_deg = atan2(rel_wind_speed_from_east_kts, rel_wind_speed_from_north_kts)
510                             * SG_RADIANS_TO_DEGREES;
511
512     //calculate rel wind
513     rel_wind = rel_wind_from_deg - hdg;
514     SG_NORMALIZE_RANGE(rel_wind, -180.0, 180.0);
515
516     //switch the wave-off lights
517     if (InToWind())
518        wave_off_lights = false;
519     else
520        wave_off_lights = true;
521
522     // cout << "rel wind: " << rel_wind << endl;
523
524 }// end update wind
525
526
527 void FGAICarrier::TurnToLaunch(){
528
529     //calculate tgt speed
530     double tgt_speed = 25 - wind_speed_kts;
531     if (tgt_speed < 10)
532         tgt_speed = 10;
533
534     //turn the carrier
535     FGAIShip::TurnTo(wind_from_deg);
536     FGAIShip::AccelTo(tgt_speed);
537
538 }
539
540
541 void FGAICarrier::TurnToBase(){
542
543     //turn the carrier
544     FGAIShip::TurnTo(base_course);
545     FGAIShip::AccelTo(base_speed);
546
547 }
548
549
550 void FGAICarrier::ReturnToBox(){
551     double course, distance, az2;
552
553     //calculate the bearing and range of the initial position from the carrier
554     geo_inverse_wgs_84(pos, mOpBoxPos, &course, &az2, &distance);
555
556     distance *= SG_METER_TO_NM;
557
558     //cout << "return course: " << course << " distance: " << distance << endl;
559     //turn the carrier
560     FGAIShip::TurnTo(course);
561     FGAIShip::AccelTo(base_speed);
562
563     if (distance >= 1)
564         returning = true;
565     else
566         returning = false;
567
568 } //  end turn to base
569
570
571 bool FGAICarrier::OutsideBox() { //returns true if the carrier is outside operating box
572
573     if ( max_lat == 0 && min_lat == 0 && max_long == 0 && min_long == 0) {
574         SG_LOG(SG_GENERAL, SG_DEBUG, "AICarrier: No Operating Box defined" );
575         return false;
576     }
577
578     if (mOpBoxPos.getLatitudeDeg() >= 0) { //northern hemisphere
579         if (pos.getLatitudeDeg() >= mOpBoxPos.getLatitudeDeg() + max_lat)
580             return true;
581
582         if (pos.getLatitudeDeg() <= mOpBoxPos.getLatitudeDeg() - min_lat)
583             return true;
584
585     } else {                  //southern hemisphere
586         if (pos.getLatitudeDeg() <= mOpBoxPos.getLatitudeDeg() - max_lat)
587             return true;
588
589         if (pos.getLatitudeDeg() >= mOpBoxPos.getLatitudeDeg() + min_lat)
590             return true;
591     }
592
593     if (mOpBoxPos.getLongitudeDeg() >=0) { //eastern hemisphere
594         if (pos.getLongitudeDeg() >= mOpBoxPos.getLongitudeDeg() + max_long)
595             return true;
596
597         if (pos.getLongitudeDeg() <= mOpBoxPos.getLongitudeDeg() - min_long)
598             return true;
599
600     } else {                 //western hemisphere
601         if (pos.getLongitudeDeg() <= mOpBoxPos.getLongitudeDeg() - max_long)
602             return true;
603
604         if (pos.getLongitudeDeg() >= mOpBoxPos.getLongitudeDeg() + min_long)
605             return true;
606     }
607
608     SG_LOG(SG_GENERAL, SG_DEBUG, "AICarrier: Inside Operating Box" );
609     return false;
610
611 } // end OutsideBox
612
613
614 bool FGAICarrier::InToWind() {
615     if ( fabs(rel_wind) < 5 )
616         return true;
617
618     return false;
619 }
620
621
622 void FGAICarrier::UpdateElevator(double dt, double transition_time) {
623
624     double step = 0;
625
626     if ((elevators && pos_norm >= 1 ) || (!elevators && pos_norm <= 0 ))
627         return;
628
629     // move the elevators
630     if ( elevators ) {
631         step = dt/transition_time;
632         if ( step > 1 )
633             step = 1;
634     } else {
635         step = -dt/transition_time;
636         if ( step < -1 )
637             step = -1;
638     }
639     // assume a linear relationship
640     raw_pos_norm += step;
641
642     //low pass filter
643     pos_norm = (raw_pos_norm * time_constant) + (pos_norm * (1 - time_constant));
644
645     //sanitise the output
646     if (raw_pos_norm >= 1) {
647         raw_pos_norm = 1;
648     } else if (raw_pos_norm <= 0) {
649         raw_pos_norm = 0;
650     }
651     return;
652
653 } // end UpdateElevator
654
655 void FGAICarrier::UpdateJBD(double dt, double jbd_transition_time) {
656
657     string launchbar_state = _launchbar_state_node->getStringValue();
658     double step = 0;
659
660     if (launchbar_state == "Engaged"){
661         jbd = true;
662     } else {
663         jbd = false;
664     }
665
666     if (( jbd && jbd_pos_norm >= 1 ) || ( !jbd && jbd_pos_norm <= 0 )){
667         return;
668     }
669
670     // move the jbds
671     if ( jbd ) {
672         step = dt/jbd_transition_time;
673         if ( step > 1 )
674             step = 1;
675     } else {
676         step = -dt/jbd_transition_time;
677         if ( step < -1 )
678             step = -1;
679     }
680
681     // assume a linear relationship
682     raw_jbd_pos_norm += step;
683
684     //low pass filter
685     jbd_pos_norm = (raw_jbd_pos_norm * jbd_time_constant) + (jbd_pos_norm * (1 - jbd_time_constant));
686
687     //sanitise the output
688     if (jbd_pos_norm >= 1) {
689         jbd_pos_norm = 1;
690     } else if (jbd_pos_norm <= 0) {
691         jbd_pos_norm = 0;
692     }
693
694     return;
695
696 } // end UpdateJBD
697
698
699 int FGAICarrierHardware::unique_id = 1;
700