FlightGear next
AICarrier.cxx
Go to the documentation of this file.
1/*
2 * SPDX-FileName: AICarrier.cxx
3 * SPDX-FileComment: AIShip-derived class creates an AI aircraft carrier
4 * SPDX-FileCopyrightText: Written by David Culp, started October 2004 - davidculp2@comcast.net
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8#include <config.h>
9
10#include <algorithm>
11#include <string>
12#include <vector>
13
14#include <simgear/sg_inlines.h>
15#include <simgear/math/sg_geodesy.hxx>
16
17#include <cmath>
18#include <Main/util.hxx>
19#include <Main/globals.hxx>
20#include <Main/fg_props.hxx>
21#include <Main/globals.hxx>
22#include <Main/util.hxx>
23
24
25#include "AICarrier.hxx"
26#include "AINotifications.hxx"
27
29{
30 simgear::Emesary::GlobalTransmitter::instance()->Register(this);
31}
32
34{
35 simgear::Emesary::GlobalTransmitter::instance()->DeRegister(this);
36}
37
38void FGAICarrier::readFromScenario(SGPropertyNode* scFileNode) {
39 if (!scFileNode)
40 return;
41
43
44 setRadius(scFileNode->getDoubleValue("turn-radius-ft", 2000));
45 setSign(scFileNode->getStringValue("pennant-number"));
46 setDeckAltitudeFt(scFileNode->getDoubleValue("deck-altitude"));
47 setWind_from_east(scFileNode->getDoubleValue("wind_from_east", 0));
48 setWind_from_north(scFileNode->getDoubleValue("wind_from_north", 0));
49 setTACANChannelID(scFileNode->getStringValue("TACAN-channel-ID", "029Y"));
50 setMaxLat(scFileNode->getDoubleValue("max-lat", 0));
51 setMinLat(scFileNode->getDoubleValue("min-lat", 0));
52 setMaxLong(scFileNode->getDoubleValue("max-long", 0));
53 setMinLong(scFileNode->getDoubleValue("min-long", 0));
54 setMPControl(scFileNode->getBoolValue("mp-control", false));
55 setAIControl(scFileNode->getBoolValue("ai-control", false));
56 setCallSign(scFileNode->getStringValue("callsign", ""));
57
58 _angled_deck_degrees = scFileNode->getDoubleValue("angled-deck-degrees", -8.5);
59
60 SGPropertyNode* flolsNode = getPositionFromNode(scFileNode, "flols-pos", _flolsPosOffset);
61 if (flolsNode) {
62 _flolsHeadingOffsetDeg = flolsNode->getDoubleValue("heading-offset-deg", 0.0);
63 _flolsApproachAngle = flolsNode->getDoubleValue("glidepath-angle-deg", 3.5);
64 }
65 else {
66 _flolsPosOffset(2) = -(_deck_altitude_ft * SG_FEET_TO_METER + 10);
67 }
68
72
73 _flolsTouchdownPosition = _flolsPosOffset; // default to the flolsPosition
74 getPositionFromNode(scFileNode, "flols-touchdown-position", _flolsTouchdownPosition);
75
76 if (!getPositionFromNode(scFileNode, "tower-position", _towerPosition)) {
77 _towerPosition(2) = -(_deck_altitude_ft * SG_FEET_TO_METER + 10);
78 SG_LOG(SG_AI, SG_INFO, "AICarrier: tower-position not defined - using default");
79 }
80
81 if (!getPositionFromNode(scFileNode, "lso-position", _lsoPosition)){
82 _lsoPosition(2) = -(_deck_altitude_ft * SG_FEET_TO_METER + 10);
83 SG_LOG(SG_AI, SG_INFO, "AICarrier: lso-position not defined - using default");
84 }
85
86
87 std::vector<SGPropertyNode_ptr> props = scFileNode->getChildren("parking-pos");
88 std::vector<SGPropertyNode_ptr>::const_iterator it;
89 for (it = props.begin(); it != props.end(); ++it) {
90 const std::string name = (*it)->getStringValue("name", "unnamed");
91 // Transform to the right coordinate frame, configuration is done in
92 // the usual x-back, y-right, z-up coordinates, computations
93 // in the simulation usual body x-forward, y-right, z-down coordinates
94 double offset_x = -(*it)->getDoubleValue("x-offset-m", 0);
95 double offset_y = (*it)->getDoubleValue("y-offset-m", 0);
96 double offset_z = -(*it)->getDoubleValue("z-offset-m", 0);
97 double hd = (*it)->getDoubleValue("heading-offset-deg", 0);
98 ParkPosition pp(name, SGVec3d(offset_x, offset_y, offset_z), hd);
99 _ppositions.push_back(pp);
100 }
101}
102
104 _wind_from_east = fps;
105}
106
108 _wind_from_north = fps;
109}
110
111void FGAICarrier::setMaxLat(double deg) {
112 _max_lat = fabs(deg);
113}
114
115void FGAICarrier::setMinLat(double deg) {
116 _min_lat = fabs(deg);
117}
118
119void FGAICarrier::setMaxLong(double deg) {
120 _max_lon = fabs(deg);
121}
122
123void FGAICarrier::setMinLong(double deg) {
124 _min_lon = fabs(deg);
125}
126
127
128void FGAICarrier::setDeckAltitudeFt(const double altitude_feet) {
129 _deck_altitude_ft = altitude_feet;
130}
131
132void FGAICarrier::setSign(const std::string& s) {
133 _sign = s;
134}
135
136void FGAICarrier::setTACANChannelID(const std::string& id) {
137 _TACAN_channel_id = id;
138}
139
141 _MPControl = c;
142}
143
145 _AIControl = c;
146}
147
148void FGAICarrier::update(double dt) {
149 // Now update the position and heading. This will compute new hdg and
150 // roll values required for the rotation speed computation.
152
153 if (_is_user_craft->getBoolValue()) {
154 _latitude_node->setDoubleValue(pos.getLatitudeDeg());
155 _longitude_node->setDoubleValue(pos.getLongitudeDeg());
156 _altitude_node->setDoubleValue(pos.getElevationFt());
157 _heading_node->setDoubleValue(hdg);
158 _pitch_node->setDoubleValue(pitch);
159 _roll_node->setDoubleValue(roll);
160 }
161
162 //automatic turn into wind with a target wind of 25 kts otd
163 //SG_LOG(SG_AI, SG_ALERT, "AICarrier: MPControl " << MPControl << " AIControl " << AIControl);
164 if (_ai_latch_node->getStringValue() != "") {
165 SG_LOG(SG_AI, SG_DEBUG, "FGAICarrier::update(): not updating because ai-latch=" << _ai_latch_node->getStringValue());
166 }
167 else if (!_MPControl && _AIControl){
168
169 if(_turn_to_launch_hdg){
170 TurnToLaunch();
171 } else if(_turn_to_recovery_hdg ){
173 } else if(OutsideBox() || _returning ) {// check that the carrier is inside
174 ReturnToBox(); // the operating box,
175 } else {
176 TurnToBase();
177 }
178
179 } else {
182 }
183
184 UpdateWind(dt);
185 UpdateElevator(dt);
186 UpdateJBD(dt);
187
188 // Transform that one to the horizontal local coordinate system.
189 SGQuatd ec2hl = SGQuatd::fromLonLat(pos);
190 // The orientation of the carrier wrt the horizontal local frame
191 SGQuatd hl2body = SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
192 // and postrotate the orientation of the AIModel wrt the horizontal
193 // local frame
194 SGQuatd ec2body = ec2hl * hl2body;
195 // The cartesian position of the carrier in the wgs84 world
196 SGVec3d cartPos = SGVec3d::fromGeod(pos);
197
198 // The position of the eyepoint - at least near that ...
200 // Add the position offset of the AIModel to gain the earth
201 // centered position
202 SGVec3d eyeWrtCarrier = eyePos - cartPos;
203 // rotate the eyepoint wrt carrier vector into the carriers frame
204 eyeWrtCarrier = ec2body.transform(eyeWrtCarrier);
205 // the eyepoints vector wrt the flols position
206 SGVec3d eyeWrtFlols = eyeWrtCarrier - _flolsPosOffset;
207
208 SGVec3d flols_location = getCartPosAt(_flolsPosOffset);
209
210 // the distance from the eyepoint to the flols
211 _flols_dist = norm(eyeWrtFlols);
212
213 // lineup (left/right) - stern lights and Carrier landing system (Aircraft/Generic/an_spn_46.nas)
214 double lineup_hdg, lineup_az2, lineup_s;
215
216 SGGeod g_eyePos = SGGeod::fromCart(eyePos);
217 SGGeod g_carrier = SGGeod::fromCart(cartPos);
218
219 //
220 // set the view as requested by control/view-index.
221 SGGeod viewPosition;
222 switch (_view_index) {
223 default:
224 case 0:
225 viewPosition = SGGeod::fromCart(getCartPosAt(_towerPosition));
226 break;
227
228 case 1:
229 viewPosition = SGGeod::fromCart(getCartPosAt(_flolsTouchdownPosition));
230 break;
231
232 case 2:
233 viewPosition = SGGeod::fromCart(getCartPosAt(_lsoPosition));
234 break;
235 }
236 _view_position_lat_deg_node->setDoubleValue(viewPosition.getLatitudeDeg());
237 _view_position_lon_deg_node->setDoubleValue(viewPosition.getLongitudeDeg());
238 _view_position_alt_ft_node->setDoubleValue(viewPosition.getElevationFt());
239
240 SGGeodesy::inverse(g_carrier, g_eyePos, lineup_hdg, lineup_az2, lineup_s);
241
242 double target_lineup = _getHeading() + _angled_deck_degrees + 180.0;
243 SG_NORMALIZE_RANGE(target_lineup, 0.0, 360.0);
244
245 _lineup = lineup_hdg - target_lineup;
246 // now the angle, positive angles are upwards
247 if (fabs(_flols_dist) < SGLimits<double>::min()) {
248 _flols_angle = 0;
249 } else {
250 double sAngle = -eyeWrtFlols(2) / _flols_dist;
251 sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
252 _flols_angle = SGMiscd::rad2deg(asin(sAngle));
253 }
254 if (_flols_dist < 8000){
255 SGVec3d eyeWrtFlols_tdp = eyeWrtCarrier - _flolsTouchdownPosition;
256
257 // the distance from the eyepoint to the flols
258 double dist_tdp = norm(eyeWrtFlols_tdp);
259 //double angle_tdp = 0;
260
261 // now the angle, positive angles are upwards
262 if (fabs(dist_tdp) < SGLimits<double>::min()) {
263 //angle_tdp = 0;
264 }
265 else {
266 double sAngle = -eyeWrtFlols_tdp(2) / dist_tdp;
267 sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
268 //angle_tdp = SGMiscd::rad2deg(asin(sAngle));
269 }
270// printf("angle %5.2f td angle %5.2f \n", _flols_angle, angle_tdp);
271 //angle += 1.481; // adjust for FLOLS offset (measured on Nimitz class)
272 }
273
274 // set the value of _flols_visible_light
275 if ( _flols_angle <= 4.35 && _flols_angle > 4.01 )
276 _flols_visible_light = 1;
277 else if ( _flols_angle <= 4.01 && _flols_angle > 3.670 )
278 _flols_visible_light = 2;
279 else if ( _flols_angle <= 3.670 && _flols_angle > 3.330 )
280 _flols_visible_light = 3;
281 else if ( _flols_angle <= 3.330 && _flols_angle > 2.990 )
282 _flols_visible_light = 4;
283 else if ( _flols_angle <= 2.990 && _flols_angle > 2.650 )
284 _flols_visible_light = 5;
285 else if ( _flols_angle <= 2.650 )
286 _flols_visible_light = 6;
287 else
288 _flols_visible_light = 0;
289
290 // only bother with waveoff FLOLS when ownship within a reasonable range.
291 // red ball is <= 3.075 to 2.65, below this is off. above this is orange.
292 // only do this when within ~1.8nm
293 if (_flols_dist < 3200) {
294 if (_flols_dist > 100) {
295 bool new_wave_off_lights_demand = (_flols_angle <= 3.0);
296
297 if (new_wave_off_lights_demand != _wave_off_lights_demand) {
298 // start timing when the lights come up.
299 _wave_off_lights_demand = new_wave_off_lights_demand;
300 }
301
303 if (_flols_angle < 2 && _flols_dist < 800) {
304 _wave_off_lights_demand = true;
305 }
306 }
307 }
308 else {
309 _wave_off_lights_demand = true; // sensible default when very far away.
310 }
311} //end update
312
314 if (!FGAIShip::init(searchOrder))
315 return false;
316
317 _longitude_node = fgGetNode("/position/longitude-deg", true);
318 _latitude_node = fgGetNode("/position/latitude-deg", true);
319 _altitude_node = fgGetNode("/position/altitude-ft", true);
320 _heading_node = fgGetNode("/orientation/true-heading-deg", true);
321 _pitch_node = fgGetNode("/orientation/pitch-deg", true);
322 _roll_node = fgGetNode("/orientation/roll-deg", true);
323
324 _launchbar_state_node = fgGetNode("/gear/launchbar/state", true);
325
326 _surface_wind_from_deg_node = fgGetNode("/environment/config/boundary/entry[0]/wind-from-heading-deg", true);
327 _surface_wind_speed_node = fgGetNode("/environment/config/boundary/entry[0]/wind-speed-kt", true);
328
329
330 int dmd_course = fgGetInt("/sim/presets/carrier-course");
331 if (dmd_course == 2) {
332 // launch
333 _turn_to_launch_hdg = true;
334 _turn_to_recovery_hdg = false;
335 _turn_to_base_course = false;
336 } else if (dmd_course == 3) {
337 // recovery
338 _turn_to_launch_hdg = false;
339 _turn_to_recovery_hdg = true;
340 _turn_to_base_course = false;
341 } else {
342 // default to base
343 _turn_to_launch_hdg = false;
344 _turn_to_recovery_hdg = false;
345 _turn_to_base_course = true;
346 }
347
348 _returning = false;
349 _in_to_wind = false;
350
351 _mOpBoxPos = pos;
352 _base_course = hdg;
353 _base_speed = speed;
354
355 _elevator_pos_norm = 0;
356 _elevator_pos_norm_raw = 0;
357 _elevators = false;
358 _elevator_transition_time = 150;
359 _elevator_time_constant = 0.005;
360 _jbd_elevator_pos_norm = 0;
361 _jbd_elevator_pos_norm_raw = 0;
362 _jbd = false ;
363 _jbd_transition_time = 3;
364 _jbd_time_constant = 0.1;
365 return true;
366}
367
370 _is_user_craft = props->getNode("is-user-craft", true /*create*/);
371 _ai_latch_node = props->getNode("ai-latch", true /*create*/);
372
373 props->untie("velocities/true-airspeed-kt");
374
375 props->getNode("position/deck-altitude-feet", true)->setDoubleValue(_deck_altitude_ft);
376
377 tie("controls/flols/source-lights",
378 SGRawValuePointer<int>(&_flols_visible_light));
379 tie("controls/flols/distance-m",
380 SGRawValuePointer<double>(&_flols_dist));
381 tie("controls/flols/angle-degs",
382 SGRawValuePointer<double>(&_flols_angle));
383 tie("controls/flols/lineup-degs",
384 SGRawValuePointer<double>(&_lineup));
385 tie("controls/turn-to-launch-hdg",
386 SGRawValuePointer<bool>(&_turn_to_launch_hdg));
387 tie("controls/in-to-wind",
388 SGRawValuePointer<bool>(&_turn_to_launch_hdg));
389 tie("controls/base-course-deg",
390 SGRawValuePointer<double>(&_base_course));
391 tie("controls/base-speed-kts",
392 SGRawValuePointer<double>(&_base_speed));
393 tie("controls/start-pos-lat-deg",
394 SGRawValueMethods<SGGeod,double>(pos, &SGGeod::getLatitudeDeg));
395 tie("controls/start-pos-long-deg",
396 SGRawValueMethods<SGGeod,double>(pos, &SGGeod::getLongitudeDeg));
397 tie("controls/mp-control",
398 SGRawValuePointer<bool>(&_MPControl));
399 tie("controls/ai-control",
400 SGRawValuePointer<bool>(&_AIControl));
401 tie("environment/surface-wind-speed-true-kts",
402 SGRawValuePointer<double>(&_wind_speed_kts));
403 tie("environment/surface-wind-from-true-degs",
404 SGRawValuePointer<double>(&_wind_from_deg));
405 tie("environment/rel-wind-from-degs",
406 SGRawValuePointer<double>(&_rel_wind_from_deg));
407 tie("environment/rel-wind-from-carrier-hdg-degs",
408 SGRawValuePointer<double>(&_rel_wind));
409 tie("environment/rel-wind-speed-kts",
410 SGRawValuePointer<double>(&_rel_wind_speed_kts));
411 tie("environment/in-to-wind",
412 SGRawValuePointer<bool>(&_in_to_wind));
413 tie("controls/flols/wave-off-lights-demand",
414 SGRawValuePointer<bool>(&_wave_off_lights_demand));
415 tie("controls/elevators",
416 SGRawValuePointer<bool>(&_elevators));
417 tie("surface-positions/elevators-pos-norm",
418 SGRawValuePointer<double>(&_elevator_pos_norm));
419 tie("controls/constants/elevators/trans-time-s",
420 SGRawValuePointer<double>(&_elevator_transition_time));
421 tie("controls/constants/elevators/time-constant",
422 SGRawValuePointer<double>(&_elevator_time_constant));
423 tie("controls/jbd",
424 SGRawValuePointer<bool>(&_jbd));
425 tie("surface-positions/jbd-pos-norm",
426 SGRawValuePointer<double>(&_jbd_elevator_pos_norm));
427 tie("controls/constants/jbd/trans-time-s",
428 SGRawValuePointer<double>(&_jbd_transition_time));
429 tie("controls/constants/jbd/time-constant",
430 SGRawValuePointer<double>(&_jbd_time_constant));
431 tie("controls/turn-to-recovery-hdg",
432 SGRawValuePointer<bool>(&_turn_to_recovery_hdg));
433 tie("controls/turn-to-base-course",
434 SGRawValuePointer<bool>(&_turn_to_base_course));
435
436 tie("controls/view-index", SGRawValuePointer<int>(&_view_index));
437
438 props->setBoolValue("controls/flols/cut-lights", false);
439 props->setBoolValue("controls/flols/wave-off-lights", false);
440 props->setBoolValue("controls/flols/wave-off-lights-emergency", false);
441 props->setBoolValue("controls/flols/cond-datum-lights", true);
442 props->setBoolValue("controls/crew", false);
443 props->setStringValue("navaids/tacan/channel-ID", _TACAN_channel_id.c_str());
444 props->setStringValue("sign", _sign.c_str());
445 std::string island_texture = "island_" + _sign + ".jpg";
446 props->setStringValue("island_texture", island_texture.c_str());
447 props->setBoolValue("controls/lighting/deck-lights", false);
448 props->setDoubleValue("controls/lighting/flood-lights-red-norm", 0);
449
450 _flols_x_node = props->getNode("position/flols-x", true);
451 _flols_y_node = props->getNode("position/flols-y", true);
452 _flols_z_node = props->getNode("position/flols-z", true);
453
454 _view_position_lat_deg_node = props->getNode("position/view-position-lat", true);
455 _view_position_lon_deg_node = props->getNode("position/view-position-lon", true);
456 _view_position_alt_ft_node = props->getNode("position/view-position-alt", true);
457
458 // Write out a list of the parking positions - useful for the UI to select
459 // from
460 for (const auto& ppos : _ppositions) {
461 if (ppos.name != "") props->addChild("parking-pos")->setStringValue("name", ppos.name);
462 }
463}
464
465bool FGAICarrier::getParkPosition(const std::string& id, SGGeod& geodPos,
466 double& hdng, SGVec3d& uvw)
467{
468
469 // FIXME: does not yet cover rotation speeds.
470 for (const auto& ppos : _ppositions) {
471 // Take either the specified one or the first one ...
472 if (ppos.name == id || id.empty()) {
473 SGVec3d cartPos = getCartPosAt(ppos.offset);
474 geodPos = SGGeod::fromCart(cartPos);
475 hdng = hdg + ppos.heading_deg;
476 double shdng = sin(ppos.heading_deg * SGD_DEGREES_TO_RADIANS);
477 double chdng = cos(ppos.heading_deg * SGD_DEGREES_TO_RADIANS);
478 double speed_fps = speed*1.6878099;
479 uvw = SGVec3d(chdng*speed_fps, shdng*speed_fps, 0);
480 return true;
481 }
482 }
483
484 return false;
485}
486
487bool FGAICarrier::getFLOLSPositionHeading(SGGeod& geodPos, double& heading) const
488{
489 SGVec3d cartPos = getCartPosAt(_flolsPosOffset);
490 geodPos = SGGeod::fromCart(cartPos);
491
492 // at present we don't support a heading offset for the FLOLS, so
493 // heading is just the carrier heading
494 heading = hdg + _flolsHeadingOffsetDeg;
495
496 return true;
497}
498
500{
501 return _flolsApproachAngle;
502}
503
504// find relative wind
505void FGAICarrier::UpdateWind( double dt) {
506
507 //get the surface wind speed and direction
508 _wind_from_deg = _surface_wind_from_deg_node->getDoubleValue();
509 _wind_speed_kts = _surface_wind_speed_node->getDoubleValue();
510
511 //calculate the surface wind speed north and east in kts
512 double wind_speed_from_north_kts = cos( _wind_from_deg / SGD_RADIANS_TO_DEGREES )* _wind_speed_kts ;
513 double wind_speed_from_east_kts = sin( _wind_from_deg / SGD_RADIANS_TO_DEGREES )* _wind_speed_kts ;
514
515 //calculate the carrier speed north and east in kts
516 double speed_north_kts = cos( hdg / SGD_RADIANS_TO_DEGREES )* speed ;
517 double speed_east_kts = sin( hdg / SGD_RADIANS_TO_DEGREES )* speed ;
518
519 //calculate the relative wind speed north and east in kts
520 double rel_wind_speed_from_east_kts = wind_speed_from_east_kts + speed_east_kts;
521 double rel_wind_speed_from_north_kts = wind_speed_from_north_kts + speed_north_kts;
522
523 //combine relative speeds north and east to get relative windspeed in kts
524 _rel_wind_speed_kts = sqrt((rel_wind_speed_from_east_kts * rel_wind_speed_from_east_kts)
525 + (rel_wind_speed_from_north_kts * rel_wind_speed_from_north_kts));
526
527 //calculate the relative wind direction
528 _rel_wind_from_deg = SGMiscd::rad2deg(atan2(rel_wind_speed_from_east_kts, rel_wind_speed_from_north_kts));
529
530 //calculate rel wind
531 _rel_wind = _rel_wind_from_deg - hdg;
532 SG_NORMALIZE_RANGE(_rel_wind, -180.0, 180.0);
533
534 //set in to wind property
535 InToWind();
536
537 //switch the wave-off lights
538 //if (InToWind())
539 // wave_off_lights = false;
540 //else
541 // wave_off_lights = true;
542
543 // cout << "rel wind: " << rel_wind << endl;
544
545}// end update wind
546
547
549
550 // calculate tgt heading
551 if (_wind_speed_kts < 3){
552 tgt_heading = _base_course;
553 } else {
554 tgt_heading = _wind_from_deg;
555 }
556
557 //calculate tgt speed
558 double tgt_speed = 25 - _wind_speed_kts;
559 if (tgt_speed < 10)
560 tgt_speed = 10;
561
562 //turn the carrier
565
566}
567
569
570 //these are the rules for adjusting heading to provide a relative wind
571 //down the angled flightdeck
572
573 if (_wind_speed_kts < 3){
574 tgt_heading = _base_course + 60;
575 } else if (_rel_wind < -9 && _rel_wind >= -180){
576 tgt_heading = _wind_from_deg;
577 } else if (_rel_wind > -7 && _rel_wind < 45){
578 tgt_heading = _wind_from_deg + 60;
579 } else if (_rel_wind >=45 && _rel_wind < 180){
580 tgt_heading = _wind_from_deg + 45;
581 } else
583
584 SG_NORMALIZE_RANGE(tgt_heading, 0.0, 360.0);
585
586 //calculate tgt speed
587 double tgt_speed = 26 - _wind_speed_kts;
588 if (tgt_speed < 10)
589 tgt_speed = 10;
590
591 //turn the carrier
594
595}
597
598 //turn the carrier
599 FGAIShip::TurnTo(_base_course);
600 FGAIShip::AccelTo(_base_speed);
601
602}
603
604
606 double course, distance, az2;
607
608 //calculate the bearing and range of the initial position from the carrier
609 geo_inverse_wgs_84(pos, _mOpBoxPos, &course, &az2, &distance);
610
611 distance *= SG_METER_TO_NM;
612
613 //cout << "return course: " << course << " distance: " << distance << endl;
614 //turn the carrier
615 FGAIShip::TurnTo(course);
616 FGAIShip::AccelTo(_base_speed);
617
618 if (distance >= 1)
619 _returning = true;
620 else
621 _returning = false;
622
623} // end turn to base
624
625
626bool FGAICarrier::OutsideBox() { //returns true if the carrier is outside operating box
627
628 if ( _max_lat == 0 && _min_lat == 0 && _max_lon == 0 && _min_lon == 0) {
629 SG_LOG(SG_AI, SG_DEBUG, "AICarrier: No Operating Box defined" );
630 return false;
631 }
632
633 if (_mOpBoxPos.getLatitudeDeg() >= 0) { //northern hemisphere
634 if (pos.getLatitudeDeg() >= _mOpBoxPos.getLatitudeDeg() + _max_lat)
635 return true;
636
637 if (pos.getLatitudeDeg() <= _mOpBoxPos.getLatitudeDeg() - _min_lat)
638 return true;
639
640 } else { //southern hemisphere
641 if (pos.getLatitudeDeg() <= _mOpBoxPos.getLatitudeDeg() - _max_lat)
642 return true;
643
644 if (pos.getLatitudeDeg() >= _mOpBoxPos.getLatitudeDeg() + _min_lat)
645 return true;
646 }
647
648 if (_mOpBoxPos.getLongitudeDeg() >=0) { //eastern hemisphere
649 if (pos.getLongitudeDeg() >= _mOpBoxPos.getLongitudeDeg() + _max_lon)
650 return true;
651
652 if (pos.getLongitudeDeg() <= _mOpBoxPos.getLongitudeDeg() - _min_lon)
653 return true;
654
655 } else { //western hemisphere
656 if (pos.getLongitudeDeg() <= _mOpBoxPos.getLongitudeDeg() - _max_lon)
657 return true;
658
659 if (pos.getLongitudeDeg() >= _mOpBoxPos.getLongitudeDeg() + _min_lon)
660 return true;
661 }
662
663 return false;
664
665} // end OutsideBox
666
667
668bool FGAICarrier::InToWind() {
669 _in_to_wind = false;
670
671 if ( fabs(_rel_wind) < 10 ){
672 _in_to_wind = true;
673 return true;
674 }
675 return false;
676}
677
678
679void FGAICarrier::UpdateElevator(double dt) {
680
681 double step = 0;
682
683 if ((_elevators && _elevator_pos_norm >= 1 ) || (!_elevators && _elevator_pos_norm <= 0 ))
684 return;
685
686 // move the elevators
687 if (_elevators ) {
688 step = dt / _elevator_transition_time;
689 if ( step > 1 )
690 step = 1;
691 } else {
692 step = -dt / _elevator_transition_time;
693 if ( step < -1 )
694 step = -1;
695 }
696 // assume a linear relationship
697 _elevator_pos_norm_raw += step;
698
699 //low pass filter
700 _elevator_pos_norm = (_elevator_pos_norm_raw * _elevator_time_constant) + (_elevator_pos_norm * (1 - _elevator_time_constant));
701
702 //sanitize the output
703 if (_elevator_pos_norm_raw >= 1.0) {
704 _elevator_pos_norm_raw = 1.0;
705 } else if (_elevator_pos_norm_raw <= 0.0) {
706 _elevator_pos_norm_raw = 0.0;
707 }
708
709 return;
710
711} // end UpdateElevator
712
713void FGAICarrier::UpdateJBD(double dt) {
714
715 const std::string launchbar_state = _launchbar_state_node->getStringValue();
716 double step = 0;
717
718 if (launchbar_state == "Engaged"){
719 _jbd = true;
720 } else {
721 _jbd = false;
722 }
723
724 if ((_jbd && _jbd_elevator_pos_norm >= 1 ) || ( !_jbd && _jbd_elevator_pos_norm <= 0 )){
725 return;
726 }
727
728 // move the jbds
729 if ( _jbd ) {
730 step = dt / _jbd_transition_time;
731 if ( step > 1 )
732 step = 1;
733 } else {
734 step = -dt / _jbd_transition_time;
735 if ( step < -1 )
736 step = -1;
737 }
738
739 // assume a linear relationship
740 _jbd_elevator_pos_norm_raw += step;
741
742 //low pass filter
743 _jbd_elevator_pos_norm = (_jbd_elevator_pos_norm_raw * _jbd_time_constant) + (_jbd_elevator_pos_norm * (1 - _jbd_time_constant));
744
745 //sanitize the output
746 if (_jbd_elevator_pos_norm >= 1.0) {
747 _jbd_elevator_pos_norm = 1.0;
748 } else if (_jbd_elevator_pos_norm <= 0.0) {
749 _jbd_elevator_pos_norm = 0.0;
750 }
751
752 return;
753
754} // end UpdateJBD
755
756std::pair<bool, SGGeod> FGAICarrier::initialPositionForCarrier(const std::string& namePennant)
757{
759 // this is actually a three-layer search (we want the scenario with the
760 // carrier with the correct penanant or name. Sometimes an XPath for
761 // properties would be quite handy :)
762
763 for (auto s : fgGetNode("/sim/ai/scenarios")->getChildren("scenario")) {
764 auto carriers = s->getChildren("carrier");
765 auto it = std::find_if(carriers.begin(), carriers.end(),
766 [namePennant] (const SGPropertyNode* n)
767 {
768 // don't want to use a recursive lambda here, so inner search is a flat loop
769 for (auto nameChild : n->getChildren("name")) {
770 if (nameChild->getStringValue() == namePennant) return true;
771 }
772 return false;
773 });
774 if (it == carriers.end()) {
775 continue;
776 }
777
778 // mark the scenario for loading (which will happen in post-init of the AIManager)
779 fgGetNode("/sim/ai/")->addChild("scenario")->setStringValue(s->getStringValue("id"));
780
781 // read out the initial-position
782 SGGeod geod = SGGeod::fromDeg((*it)->getDoubleValue("longitude"),
783 (*it)->getDoubleValue("latitude"));
784 return std::make_pair(true, geod);
785 } // of scenarios iteration
786
787 return std::make_pair(false, SGGeod());
788}
789
790SGSharedPtr<FGAICarrier> FGAICarrier::findCarrierByNameOrPennant(const std::string& namePennant)
791{
792 const auto aiManager = globals->get_subsystem<FGAIManager>();
793 if (!aiManager) {
794 return {};
795 }
796
797 for (const auto& aiObject : aiManager->get_ai_list()) {
798 if (aiObject->isa(object_type::otCarrier)) {
799 SGSharedPtr<FGAICarrier> c = static_cast<FGAICarrier*>(aiObject.get());
800 if ((c->_sign == namePennant) || (c->_getName() == namePennant)) {
801 return c;
802 }
803 }
804 } // of all objects iteration
805
806 return {};
807}
808
809void FGAICarrier::extractCarriersFromScenario(SGPropertyNode_ptr xmlNode, SGPropertyNode_ptr scenario)
810{
811 for (auto c : xmlNode->getChildren("entry")) {
812 if (c->getStringValue("type") != std::string("carrier"))
813 continue;
814
815 const std::string name = c->getStringValue("name");
816 const std::string pennant = c->getStringValue("pennant-number");
817 if (name.empty() && pennant.empty()) {
818 continue;
819 }
820
821 SGPropertyNode_ptr carrierNode = scenario->addChild("carrier");
822
823 // extract the initial position from the scenario
824 carrierNode->setDoubleValue("longitude", c->getDoubleValue("longitude"));
825 carrierNode->setDoubleValue("latitude", c->getDoubleValue("latitude"));
826
827 // A description of the carrier is also available from the entry. Primarily for use by the launcher
828 carrierNode->setStringValue("description", c->getStringValue("description"));
829
830 // the find code above just looks for anything called a name (so alias
831 // are possible, for example)
832 if (!name.empty()) carrierNode->addChild("name")->setStringValue(name);
833 if (!pennant.empty()) {
834 carrierNode->addChild("name")->setStringValue(pennant);
835 carrierNode->addChild("pennant-number")->setStringValue(pennant);
836 }
837
838 // extract parkings
839 for (auto p : c->getChildren("parking-pos")) {
840 carrierNode->addChild("parking-pos")->setStringValue(p->getStringValue("name"));
841 }
842 }
843}
844
845simgear::Emesary::ReceiptStatus FGAICarrier::Receive(simgear::Emesary::INotificationPtr n)
846{
847 auto nctn = dynamic_pointer_cast<NearestCarrierToNotification>(n);
848
849 if (nctn) {
850 if (!nctn->GetCarrier() || nctn->GetDistanceMeters() > nctn->GetDistanceToMeters(pos)) {
851 nctn->SetCarrier(this, &pos);
852 nctn->SetViewPositionLatNode(_view_position_lat_deg_node);
853 nctn->SetViewPositionLonNode(_view_position_lon_deg_node);
854 nctn->SetViewPositionAltNode(_view_position_alt_ft_node);
855 nctn->SetDeckheight(_deck_altitude_ft);
856 nctn->SetHeading(hdg);
857 nctn->SetVckts(speed);
858 nctn->SetCarrierIdent(this->_getName());
859 }
860 return simgear::Emesary::ReceiptStatus::OK;
861 }
862 return simgear::Emesary::ReceiptStatus::NotProcessed;
863}
#define p(x)
SGPropertyNode * getPositionFromNode(SGPropertyNode *scFileNode, const std::string &key, SGVec3d &position)
Definition AIBase.cxx:910
double speed_fps
Definition AIBase.hxx:217
double tgt_heading
Definition AIBase.hxx:229
SGGeod pos
Definition AIBase.hxx:212
void setCallSign(const std::string &)
Definition AIBase.hxx:451
SGVec3d getCartPosAt(const SGVec3d &off) const
Definition AIBase.cxx:881
double _getHeading() const
Definition AIBase.cxx:1112
void setRadius(double radius)
Definition AIBase.hxx:409
double speed
Definition AIBase.hxx:216
const char * _getName() const
Definition AIBase.cxx:1136
double hdg
Definition AIBase.hxx:213
double tgt_speed
Definition AIBase.hxx:231
double pitch
Definition AIBase.hxx:215
ModelSearchOrder
Definition AIBase.hxx:63
double roll
Definition AIBase.hxx:214
void tie(const char *aRelPath, const SGRawValue< T > &aRawValue)
Tied-properties helper, record nodes which are tied for easy un-tie-ing.
Definition AIBase.hxx:198
SGPropertyNode_ptr props
Definition AIBase.hxx:205
void setMaxLat(double deg)
void TurnToBase()
static std::pair< bool, SGGeod > initialPositionForCarrier(const std::string &namePennant)
void setAIControl(bool c)
static SGSharedPtr< FGAICarrier > findCarrierByNameOrPennant(const std::string &namePennant)
type-safe wrapper around AIManager::getObjectFromProperty
void setTACANChannelID(const std::string &)
bool OutsideBox()
void TurnToRecover()
void setMinLong(double deg)
void ReturnToBox()
void setWind_from_east(double fps)
virtual ~FGAICarrier()
Definition AICarrier.cxx:33
bool getParkPosition(const std::string &id, SGGeod &geodPos, double &hdng, SGVec3d &uvw)
void setMinLat(double deg)
bool init(ModelSearchOrder searchOrder) override
void setSign(const std::string &)
double getFLOLFSGlidepathAngleDeg() const
void setMaxLong(double deg)
void setDeckAltitudeFt(const double altitude_feet)
void setMPControl(bool c)
void setWind_from_north(double fps)
void TurnToLaunch()
void UpdateWind(double dt)
bool getFLOLSPositionHeading(SGGeod &pos, double &heading) const
virtual simgear::Emesary::ReceiptStatus Receive(simgear::Emesary::INotificationPtr n) override
void bind() override
void readFromScenario(SGPropertyNode *scFileNode) override
Definition AICarrier.cxx:38
static void extractCarriersFromScenario(SGPropertyNode_ptr xmlNode, SGPropertyNode_ptr scenario)
for a given scenario node, check for carriers within, and write nodes with names, pennants and initia...
static void registerScenarios(SGPropertyNode_ptr root={})
Static helper to register scenarios.
void readFromScenario(SGPropertyNode *scFileNode) override
Definition AIShip.cxx:63
void update(double dt) override
Definition AIShip.cxx:199
void bind() override
Definition AIShip.cxx:124
void TurnTo(double heading)
Definition AIShip.cxx:457
void AccelTo(double speed)
Definition AIShip.cxx:431
FGAIShip(object_type ot=object_type::otShip)
Definition AIShip.cxx:30
bool init(ModelSearchOrder searchOrder) override
Definition AIShip.cxx:92
SGVec3d get_ownship_reference_position_cart() const
Definition globals.cxx:644
const char * name
int fgGetInt(const char *name, int defaultValue)
Get an int value for a property.
Definition fg_props.cxx:532
FGGlobals * globals
Definition globals.cxx:142
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.
Definition proptest.cpp:27