]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AICarrier.cxx
Merge branch 'next' of gitorious.org:fg/flightgear into next
[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 <simgear/sg_inlines.h>
29 #include <simgear/math/SGMath.hxx>
30 #include <simgear/math/sg_geodesy.hxx>
31
32 #include <math.h>
33 #include <Main/util.hxx>
34 #include <Main/viewer.hxx>
35
36 #include "AICarrier.hxx"
37
38 FGAICarrier::FGAICarrier() : FGAIShip(otCarrier) {
39 }
40
41 FGAICarrier::~FGAICarrier() {
42 }
43
44 void FGAICarrier::readFromScenario(SGPropertyNode* scFileNode) {
45   if (!scFileNode)
46     return;
47
48   FGAIShip::readFromScenario(scFileNode);
49
50   setRadius(scFileNode->getDoubleValue("turn-radius-ft", 2000));
51   setSign(scFileNode->getStringValue("pennant-number"));
52   setWind_from_east(scFileNode->getDoubleValue("wind_from_east", 0));
53   setWind_from_north(scFileNode->getDoubleValue("wind_from_north", 0));
54   setTACANChannelID(scFileNode->getStringValue("TACAN-channel-ID", "029Y"));
55   setMaxLat(scFileNode->getDoubleValue("max-lat", 0));
56   setMinLat(scFileNode->getDoubleValue("min-lat", 0));
57   setMaxLong(scFileNode->getDoubleValue("max-long", 0));
58   setMinLong(scFileNode->getDoubleValue("min-long", 0));
59   setMPControl(scFileNode->getBoolValue("mp-control", false));
60   setAIControl(scFileNode->getBoolValue("ai-control", false));
61
62   SGPropertyNode* flols = scFileNode->getChild("flols-pos");
63   if (flols) {
64     // Transform to the right coordinate frame, configuration is done in
65     // the usual x-back, y-right, z-up coordinates, computations
66     // in the simulation usual body x-forward, y-right, z-down coordinates
67     flols_off(0) = - flols->getDoubleValue("x-offset-m", 0);
68     flols_off(1) = flols->getDoubleValue("y-offset-m", 0);
69     flols_off(2) = - flols->getDoubleValue("z-offset-m", 0);
70   } else
71     flols_off = SGVec3d::zeros();
72
73   std::vector<SGPropertyNode_ptr> props = scFileNode->getChildren("parking-pos");
74   std::vector<SGPropertyNode_ptr>::const_iterator it;
75   for (it = props.begin(); it != props.end(); ++it) {
76     string name = (*it)->getStringValue("name", "unnamed");
77     // Transform to the right coordinate frame, configuration is done in
78     // the usual x-back, y-right, z-up coordinates, computations
79     // in the simulation usual body x-forward, y-right, z-down coordinates
80     double offset_x = -(*it)->getDoubleValue("x-offset-m", 0);
81     double offset_y = (*it)->getDoubleValue("y-offset-m", 0);
82     double offset_z = -(*it)->getDoubleValue("z-offset-m", 0);
83     double hd = (*it)->getDoubleValue("heading-offset-deg", 0);
84     ParkPosition pp(name, SGVec3d(offset_x, offset_y, offset_z), hd);
85     ppositions.push_back(pp);
86   }
87 }
88
89 void FGAICarrier::setWind_from_east(double fps) {
90     wind_from_east = fps;
91 }
92
93 void FGAICarrier::setWind_from_north(double fps) {
94     wind_from_north = fps;
95 }
96
97 void FGAICarrier::setMaxLat(double deg) {
98     max_lat = fabs(deg);
99 }
100
101 void FGAICarrier::setMinLat(double deg) {
102     min_lat = fabs(deg);
103 }
104
105 void FGAICarrier::setMaxLong(double deg) {
106     max_long = fabs(deg);
107 }
108
109 void FGAICarrier::setMinLong(double deg) {
110     min_long = fabs(deg);
111 }
112
113 void FGAICarrier::setSign(const string& s) {
114     sign = s;
115 }
116
117 void FGAICarrier::setTACANChannelID(const string& id) {
118     TACAN_channel_id = id;
119 }
120
121 void FGAICarrier::setMPControl(bool c) {
122     MPControl = c;
123 }
124
125 void FGAICarrier::setAIControl(bool c) {
126     AIControl = c;
127 }
128
129 void FGAICarrier::update(double dt) {
130     // Now update the position and heading. This will compute new hdg and
131     // roll values required for the rotation speed computation.
132     FGAIShip::update(dt);
133
134     //automatic turn into wind with a target wind of 25 kts otd
135     //SG_LOG(SG_GENERAL, SG_ALERT, "AICarrier: MPControl " << MPControl << " AIControl " << AIControl);
136     if (!MPControl && AIControl){
137
138         if(turn_to_launch_hdg){
139             TurnToLaunch();
140         } else if(turn_to_recovery_hdg ){
141             TurnToRecover();
142         } else if(OutsideBox() || returning ) {// check that the carrier is inside
143             ReturnToBox();                     // the operating box,
144         } else {
145             TurnToBase();
146         }
147
148     } else {
149         FGAIShip::TurnTo(tgt_heading);
150         FGAIShip::AccelTo(tgt_speed);
151     }
152
153     UpdateWind(dt);
154     UpdateElevator(dt, transition_time);
155     UpdateJBD(dt, jbd_transition_time);
156
157     // Transform that one to the horizontal local coordinate system.
158     SGQuatd ec2hl = SGQuatd::fromLonLat(pos);
159     // The orientation of the carrier wrt the horizontal local frame
160     SGQuatd hl2body = SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
161     // and postrotate the orientation of the AIModel wrt the horizontal
162     // local frame
163     SGQuatd ec2body = ec2hl*hl2body;
164     // The cartesian position of the carrier in the wgs84 world
165     SGVec3d cartPos = SGVec3d::fromGeod(pos);
166
167     // The position of the eyepoint - at least near that ...
168     SGVec3d eyePos(globals->get_current_view()->get_view_pos());
169     // Add the position offset of the AIModel to gain the earth
170     // centered position
171     SGVec3d eyeWrtCarrier = eyePos - cartPos;
172     // rotate the eyepoint wrt carrier vector into the carriers frame
173     eyeWrtCarrier = ec2body.transform(eyeWrtCarrier);
174     // the eyepoints vector wrt the flols position
175     SGVec3d eyeWrtFlols = eyeWrtCarrier - flols_off;
176
177     // the distance from the eyepoint to the flols
178     dist = norm(eyeWrtFlols);
179
180     // now the angle, positive angles are upwards
181     if (fabs(dist) < SGLimits<float>::min()) {
182       angle = 0;
183     } else {
184       double sAngle = -eyeWrtFlols(2)/dist;
185       sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
186       angle = SGMiscd::rad2deg(asin(sAngle));
187     }
188
189     // set the value of source
190     if ( angle <= 4.35 && angle > 4.01 )
191       source = 1;
192     else if ( angle <= 4.01 && angle > 3.670 )
193       source = 2;
194     else if ( angle <= 3.670 && angle > 3.330 )
195       source = 3;
196     else if ( angle <= 3.330 && angle > 2.990 )
197       source = 4;
198     else if ( angle <= 2.990 && angle > 2.650 )
199       source = 5;
200     else if ( angle <= 2.650 )
201       source = 6;
202     else
203       source = 0;
204 } //end update
205
206 bool FGAICarrier::init(bool search_in_AI_path) {
207     if (!FGAIShip::init(search_in_AI_path))
208         return false;
209
210     _longitude_node = fgGetNode("/position/longitude-deg", true);
211     _latitude_node = fgGetNode("/position/latitude-deg", true);
212     _altitude_node = fgGetNode("/position/altitude-ft", true);
213
214     _launchbar_state_node = fgGetNode("/gear/launchbar/state", true);
215
216     _surface_wind_from_deg_node =
217             fgGetNode("/environment/config/boundary/entry[0]/wind-from-heading-deg", true);
218     _surface_wind_speed_node =
219             fgGetNode("/environment/config/boundary/entry[0]/wind-speed-kt", true);
220
221
222     turn_to_launch_hdg = false;
223     turn_to_recovery_hdg = false;
224     turn_to_base_course = true;
225     returning = false;
226     in_to_wind = false;
227
228     mOpBoxPos = pos;
229     base_course = hdg;
230     base_speed = speed;
231
232     pos_norm = raw_pos_norm = 0;
233     elevators = false;
234     transition_time = 150;
235     time_constant = 0.005;
236     jbd_pos_norm = raw_jbd_pos_norm = 0;
237     jbd = false ;
238     jbd_transition_time = 3;
239     jbd_time_constant = 0.1;
240     return true;
241 }
242
243 void FGAICarrier::bind() {
244     FGAIShip::bind();
245
246     props->untie("velocities/true-airspeed-kt");
247
248     props->tie("controls/flols/source-lights",
249                 SGRawValuePointer<int>(&source));
250     props->tie("controls/flols/distance-m",
251                 SGRawValuePointer<double>(&dist));
252     props->tie("controls/flols/angle-degs",
253                 SGRawValuePointer<double>(&angle));
254     props->tie("controls/turn-to-launch-hdg",
255                 SGRawValuePointer<bool>(&turn_to_launch_hdg));
256     props->tie("controls/in-to-wind",
257                 SGRawValuePointer<bool>(&turn_to_launch_hdg));
258     props->tie("controls/base-course-deg",
259                 SGRawValuePointer<double>(&base_course));
260     props->tie("controls/base-speed-kts",
261                 SGRawValuePointer<double>(&base_speed));
262     props->tie("controls/start-pos-lat-deg",
263                SGRawValueMethods<SGGeod,double>(pos, &SGGeod::getLatitudeDeg));
264     props->tie("controls/start-pos-long-deg",
265                SGRawValueMethods<SGGeod,double>(pos, &SGGeod::getLongitudeDeg));
266     props->tie("controls/mp-control",
267                 SGRawValuePointer<bool>(&MPControl));
268     props->tie("controls/ai-control",
269                 SGRawValuePointer<bool>(&AIControl));
270     props->tie("environment/surface-wind-speed-true-kts",
271                 SGRawValuePointer<double>(&wind_speed_kts));
272     props->tie("environment/surface-wind-from-true-degs",
273                 SGRawValuePointer<double>(&wind_from_deg));
274     props->tie("environment/rel-wind-from-degs",
275                 SGRawValuePointer<double>(&rel_wind_from_deg));
276     props->tie("environment/rel-wind-from-carrier-hdg-degs",
277                 SGRawValuePointer<double>(&rel_wind));
278     props->tie("environment/rel-wind-speed-kts",
279                 SGRawValuePointer<double>(&rel_wind_speed_kts));
280     props->tie("environment/in-to-wind",
281         SGRawValuePointer<bool>(&in_to_wind));
282     //props->tie("controls/flols/wave-off-lights",
283     //            SGRawValuePointer<bool>(&wave_off_lights));
284     props->tie("controls/elevators",
285                 SGRawValuePointer<bool>(&elevators));
286     props->tie("surface-positions/elevators-pos-norm",
287                 SGRawValuePointer<double>(&pos_norm));
288     props->tie("controls/constants/elevators/trans-time-s",
289                 SGRawValuePointer<double>(&transition_time));
290     props->tie("controls/constants/elevators/time-constant",
291                 SGRawValuePointer<double>(&time_constant));
292     props->tie("controls/jbd",
293         SGRawValuePointer<bool>(&jbd));
294     props->tie("surface-positions/jbd-pos-norm",
295         SGRawValuePointer<double>(&jbd_pos_norm));
296     props->tie("controls/constants/jbd/trans-time-s",
297         SGRawValuePointer<double>(&jbd_transition_time));
298     props->tie("controls/constants/jbd/time-constant",
299         SGRawValuePointer<double>(&jbd_time_constant));
300     props->tie("controls/turn-to-recovery-hdg",
301         SGRawValuePointer<bool>(&turn_to_recovery_hdg));
302     props->tie("controls/turn-to-base-course",
303         SGRawValuePointer<bool>(&turn_to_base_course));
304
305
306     props->setBoolValue("controls/flols/cut-lights", false);
307     props->setBoolValue("controls/flols/wave-off-lights", false);
308     props->setBoolValue("controls/flols/cond-datum-lights", true);
309     props->setBoolValue("controls/crew", false);
310     props->setStringValue("navaids/tacan/channel-ID", TACAN_channel_id.c_str());
311     props->setStringValue("sign", sign.c_str());
312     props->setBoolValue("controls/lighting/deck-lights", false);
313     props->setDoubleValue("controls/lighting/flood-lights-red-norm", 0);
314 }
315
316
317 void FGAICarrier::unbind() {
318     FGAIShip::unbind();
319
320     props->untie("velocities/true-airspeed-kt");
321     props->untie("controls/flols/source-lights");
322     props->untie("controls/flols/distance-m");
323     props->untie("controls/flols/angle-degs");
324     props->untie("controls/turn-to-launch-hdg");
325     props->untie("environment/wind-speed-true-kts");
326     props->untie("environment/wind-from-true-degs");
327     props->untie("environment/rel-wind-from-degs");
328     props->untie("environment/rel-wind-speed-kts");
329     props->untie("environment/in-to-wind");
330     //props->untie("controls/flols/wave-off-lights");
331     props->untie("controls/elevators");
332     props->untie("surface-positions/elevators-pos-norm");
333     props->untie("controls/constants/elevators/trans-time-secs");
334     props->untie("controls/constants/elevators/time-constant");
335     props->untie("controls/jbd");
336     props->untie("surface-positions/jbd/pos-norm");
337     props->untie("controls/constants/jbd/trans-time-s");
338     props->untie("controls/jbd-time-constant");
339     props->untie("controls/mp-control");
340     props->untie("controls/ai-control");
341     props->untie("controls/turn-to-recovery-hdg");
342     props->untie("controls/turn-to-base-course");
343 }
344
345
346 bool FGAICarrier::getParkPosition(const string& id, SGGeod& geodPos,
347                                   double& hdng, SGVec3d& uvw)
348 {
349
350     // FIXME: does not yet cover rotation speeds.
351     list<ParkPosition>::iterator it = ppositions.begin();
352     while (it != ppositions.end()) {
353         // Take either the specified one or the first one ...
354         if ((*it).name == id || id.empty()) {
355             ParkPosition ppos = *it;
356             SGVec3d cartPos = getCartPosAt(ppos.offset);
357             geodPos = SGGeod::fromCart(cartPos);
358             hdng = hdg + ppos.heading_deg;
359             double shdng = sin(ppos.heading_deg * SGD_DEGREES_TO_RADIANS);
360             double chdng = cos(ppos.heading_deg * SGD_DEGREES_TO_RADIANS);
361             double speed_fps = speed*1.6878099;
362             uvw = SGVec3d(chdng*speed_fps, shdng*speed_fps, 0);
363             return true;
364         }
365         ++it;
366     }
367
368     return false;
369 }
370
371 // find relative wind
372 void FGAICarrier::UpdateWind( double dt) {
373
374     //get the surface wind speed and direction
375     wind_from_deg = _surface_wind_from_deg_node->getDoubleValue();
376     wind_speed_kts  = _surface_wind_speed_node->getDoubleValue();
377
378     //calculate the surface wind speed north and east in kts
379     double wind_speed_from_north_kts = cos( wind_from_deg / SGD_RADIANS_TO_DEGREES )* wind_speed_kts ;
380     double wind_speed_from_east_kts  = sin( wind_from_deg / SGD_RADIANS_TO_DEGREES )* wind_speed_kts ;
381
382     //calculate the carrier speed north and east in kts
383     double speed_north_kts = cos( hdg / SGD_RADIANS_TO_DEGREES )* speed ;
384     double speed_east_kts  = sin( hdg / SGD_RADIANS_TO_DEGREES )* speed ;
385
386     //calculate the relative wind speed north and east in kts
387     double rel_wind_speed_from_east_kts = wind_speed_from_east_kts + speed_east_kts;
388     double rel_wind_speed_from_north_kts = wind_speed_from_north_kts + speed_north_kts;
389
390     //combine relative speeds north and east to get relative windspeed in kts
391     rel_wind_speed_kts = sqrt((rel_wind_speed_from_east_kts * rel_wind_speed_from_east_kts)
392     + (rel_wind_speed_from_north_kts * rel_wind_speed_from_north_kts));
393
394     //calculate the relative wind direction
395     rel_wind_from_deg = atan2(rel_wind_speed_from_east_kts, rel_wind_speed_from_north_kts)
396                             * SG_RADIANS_TO_DEGREES;
397
398     //calculate rel wind
399     rel_wind = rel_wind_from_deg - hdg;
400     SG_NORMALIZE_RANGE(rel_wind, -180.0, 180.0);
401
402     //set in to wind property
403     InToWind();
404
405     //switch the wave-off lights
406     //if (InToWind())
407     //   wave_off_lights = false;
408     //else
409     //   wave_off_lights = true;
410
411     // cout << "rel wind: " << rel_wind << endl;
412
413 }// end update wind
414
415
416 void FGAICarrier::TurnToLaunch(){
417
418     // calculate tgt heading
419     if (wind_speed_kts < 3){
420         tgt_heading = base_course;
421     } else {
422         tgt_heading = wind_from_deg;
423     }
424
425     //calculate tgt speed
426     double tgt_speed = 25 - wind_speed_kts;
427     if (tgt_speed < 10)
428         tgt_speed = 10;
429
430     //turn the carrier
431     FGAIShip::TurnTo(tgt_heading);
432     FGAIShip::AccelTo(tgt_speed);
433
434 }
435
436 void FGAICarrier::TurnToRecover(){
437
438     //these are the rules for adjusting heading to provide a relative wind
439     //down the angled flightdeck
440
441     if (wind_speed_kts < 3){
442         tgt_heading = base_course + 60;
443     } else if (rel_wind < -9 && rel_wind >= -180){
444         tgt_heading = wind_from_deg;
445     } else if (rel_wind > -7 && rel_wind < 45){
446         tgt_heading = wind_from_deg + 60;
447     } else if (rel_wind >=45 && rel_wind < 180){
448         tgt_heading = wind_from_deg + 45;
449     } else
450         tgt_heading = hdg;
451
452     SG_NORMALIZE_RANGE(tgt_heading, 0.0, 360.0);
453
454     //calculate tgt speed
455     double tgt_speed = 26 - wind_speed_kts;
456     if (tgt_speed < 10)
457         tgt_speed = 10;
458
459     //turn the carrier
460     FGAIShip::TurnTo(tgt_heading);
461     FGAIShip::AccelTo(tgt_speed);
462
463 }
464 void FGAICarrier::TurnToBase(){
465
466     //turn the carrier
467     FGAIShip::TurnTo(base_course);
468     FGAIShip::AccelTo(base_speed);
469
470 }
471
472
473 void FGAICarrier::ReturnToBox(){
474     double course, distance, az2;
475
476     //calculate the bearing and range of the initial position from the carrier
477     geo_inverse_wgs_84(pos, mOpBoxPos, &course, &az2, &distance);
478
479     distance *= SG_METER_TO_NM;
480
481     //cout << "return course: " << course << " distance: " << distance << endl;
482     //turn the carrier
483     FGAIShip::TurnTo(course);
484     FGAIShip::AccelTo(base_speed);
485
486     if (distance >= 1)
487         returning = true;
488     else
489         returning = false;
490
491 } //  end turn to base
492
493
494 bool FGAICarrier::OutsideBox() { //returns true if the carrier is outside operating box
495
496     if ( max_lat == 0 && min_lat == 0 && max_long == 0 && min_long == 0) {
497         SG_LOG(SG_GENERAL, SG_DEBUG, "AICarrier: No Operating Box defined" );
498         return false;
499     }
500
501     if (mOpBoxPos.getLatitudeDeg() >= 0) { //northern hemisphere
502         if (pos.getLatitudeDeg() >= mOpBoxPos.getLatitudeDeg() + max_lat)
503             return true;
504
505         if (pos.getLatitudeDeg() <= mOpBoxPos.getLatitudeDeg() - min_lat)
506             return true;
507
508     } else {                  //southern hemisphere
509         if (pos.getLatitudeDeg() <= mOpBoxPos.getLatitudeDeg() - max_lat)
510             return true;
511
512         if (pos.getLatitudeDeg() >= mOpBoxPos.getLatitudeDeg() + min_lat)
513             return true;
514     }
515
516     if (mOpBoxPos.getLongitudeDeg() >=0) { //eastern hemisphere
517         if (pos.getLongitudeDeg() >= mOpBoxPos.getLongitudeDeg() + max_long)
518             return true;
519
520         if (pos.getLongitudeDeg() <= mOpBoxPos.getLongitudeDeg() - min_long)
521             return true;
522
523     } else {                 //western hemisphere
524         if (pos.getLongitudeDeg() <= mOpBoxPos.getLongitudeDeg() - max_long)
525             return true;
526
527         if (pos.getLongitudeDeg() >= mOpBoxPos.getLongitudeDeg() + min_long)
528             return true;
529     }
530
531     SG_LOG(SG_GENERAL, SG_DEBUG, "AICarrier: Inside Operating Box" );
532     return false;
533
534 } // end OutsideBox
535
536
537 bool FGAICarrier::InToWind() {
538     in_to_wind = false;
539
540     if ( fabs(rel_wind) < 10 ){
541         in_to_wind = true;
542         return true;
543     }
544     return false;
545 }
546
547
548 void FGAICarrier::UpdateElevator(double dt, double transition_time) {
549
550     double step = 0;
551
552     if ((elevators && pos_norm >= 1 ) || (!elevators && pos_norm <= 0 ))
553         return;
554
555     // move the elevators
556     if ( elevators ) {
557         step = dt/transition_time;
558         if ( step > 1 )
559             step = 1;
560     } else {
561         step = -dt/transition_time;
562         if ( step < -1 )
563             step = -1;
564     }
565     // assume a linear relationship
566     raw_pos_norm += step;
567
568     //low pass filter
569     pos_norm = (raw_pos_norm * time_constant) + (pos_norm * (1 - time_constant));
570
571     //sanitise the output
572     if (raw_pos_norm >= 1) {
573         raw_pos_norm = 1;
574     } else if (raw_pos_norm <= 0) {
575         raw_pos_norm = 0;
576     }
577     return;
578
579 } // end UpdateElevator
580
581 void FGAICarrier::UpdateJBD(double dt, double jbd_transition_time) {
582
583     string launchbar_state = _launchbar_state_node->getStringValue();
584     double step = 0;
585
586     if (launchbar_state == "Engaged"){
587         jbd = true;
588     } else {
589         jbd = false;
590     }
591
592     if (( jbd && jbd_pos_norm >= 1 ) || ( !jbd && jbd_pos_norm <= 0 )){
593         return;
594     }
595
596     // move the jbds
597     if ( jbd ) {
598         step = dt/jbd_transition_time;
599         if ( step > 1 )
600             step = 1;
601     } else {
602         step = -dt/jbd_transition_time;
603         if ( step < -1 )
604             step = -1;
605     }
606
607     // assume a linear relationship
608     raw_jbd_pos_norm += step;
609
610     //low pass filter
611     jbd_pos_norm = (raw_jbd_pos_norm * jbd_time_constant) + (jbd_pos_norm * (1 - jbd_time_constant));
612
613     //sanitise the output
614     if (jbd_pos_norm >= 1) {
615         jbd_pos_norm = 1;
616     } else if (jbd_pos_norm <= 0) {
617         jbd_pos_norm = 0;
618     }
619
620     return;
621
622 } // end UpdateJBD