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