1 // FGAIWingman - FGAIBllistic-derived class creates an AI Wingman
3 // Written by Vivian Meazza, started February 2008.
4 // - vivian.meazza at lineone.net
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.
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.
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.
24 #include <simgear/sg_inlines.h>
25 #include <simgear/math/SGMath.hxx>
28 #include "AIWingman.hxx"
30 FGAIWingman::FGAIWingman() : FGAIBallistic(otWingman),
47 FGAIWingman::~FGAIWingman() {}
49 void FGAIWingman::readFromScenario(SGPropertyNode* scFileNode) {
53 FGAIBase::readFromScenario(scFileNode);
55 setAzimuth(scFileNode->getDoubleValue("azimuth", 0.0));
56 setElevation(scFileNode->getDoubleValue("elevation", 0.0));
57 setLife(scFileNode->getDoubleValue("life", -1));
58 setNoRoll(scFileNode->getBoolValue("no-roll", false));
59 setName(scFileNode->getStringValue("name", "Wingman"));
60 setParentName(scFileNode->getStringValue("parent", ""));
61 setSubID(scFileNode->getIntValue("SubID", 0));
62 setXoffset(scFileNode->getDoubleValue("x-offset", 0.0));
63 setYoffset(scFileNode->getDoubleValue("y-offset", 0.0));
64 setZoffset(scFileNode->getDoubleValue("z-offset", 0.0));
65 setPitchoffset(scFileNode->getDoubleValue("pitch-offset", 0.0));
66 setRolloffset(scFileNode->getDoubleValue("roll-offset", 0.0));
67 setYawoffset(scFileNode->getDoubleValue("yaw-offset", 0.0));
68 setGroundOffset(scFileNode->getDoubleValue("ground-offset", 0.0));
69 setFormate(scFileNode->getBoolValue("formate", true));
70 setMaxSpeed(scFileNode->getDoubleValue("max-speed-kts", 300.0));
71 setCoeffHdg(scFileNode->getDoubleValue("coefficients/heading", 5.0));
72 setCoeffPch(scFileNode->getDoubleValue("coefficients/pitch", 5.0));
73 setCoeffBnk(scFileNode->getDoubleValue("coefficients/bank", 4.0));
74 setCoeffSpd(scFileNode->getDoubleValue("coefficients/speed", 2.0));
79 void FGAIWingman::bind() {
80 FGAIBallistic::bind();
82 props->untie("controls/slave-to-ac");
84 props->tie("id", SGRawValueMethods<FGAIBase,int>(*this,
86 props->tie("subID", SGRawValueMethods<FGAIBase,int>(*this,
87 &FGAIBase::_getSubID));
88 props->tie("position/altitude-ft",
89 SGRawValueMethods<FGAIBase,double>(*this,
90 &FGAIBase::_getElevationFt,
91 &FGAIBase::_setAltitude));
92 props->tie("position/latitude-deg",
93 SGRawValueMethods<FGAIBase,double>(*this,
94 &FGAIBase::_getLatitude,
95 &FGAIBase::_setLatitude));
96 props->tie("position/longitude-deg",
97 SGRawValueMethods<FGAIBase,double>(*this,
98 &FGAIBase::_getLongitude,
99 &FGAIBase::_setLongitude));
101 props->tie("controls/break", SGRawValuePointer<bool>(&_break));
102 props->tie("controls/join", SGRawValuePointer<bool>(&_join));
104 props->tie("controls/formate-to-ac",
105 SGRawValueMethods<FGAIWingman,bool>
106 (*this, &FGAIWingman::getFormate, &FGAIWingman::setFormate));
107 props->tie("controls/tgt-heading-deg",
108 SGRawValueMethods<FGAIWingman,double>
109 (*this, &FGAIWingman::getTgtHdg, &FGAIWingman::setTgtHdg));
110 props->tie("controls/tgt-speed-kt",
111 SGRawValueMethods<FGAIWingman,double>
112 (*this, &FGAIWingman::getTgtSpd, &FGAIWingman::setTgtSpd));
113 props->tie("controls/break-deg-rel",
114 SGRawValueMethods<FGAIWingman,double>
115 (*this, &FGAIWingman::getBrkAng, &FGAIWingman::setBrkAng));
116 props->tie("controls/coefficients/heading",
117 SGRawValuePointer<double>(&_coeff_hdg));
118 props->tie("controls/coefficients/pitch",
119 SGRawValuePointer<double>(&_coeff_pch));
120 props->tie("controls/coefficients/bank",
121 SGRawValuePointer<double>(&_coeff_bnk));
122 props->tie("controls/coefficients/speed",
123 SGRawValuePointer<double>(&_coeff_spd));
125 props->tie("orientation/pitch-deg", SGRawValuePointer<double>(&pitch));
126 props->tie("orientation/roll-deg", SGRawValuePointer<double>(&roll));
127 props->tie("orientation/true-heading-deg", SGRawValuePointer<double>(&hdg));
129 props->tie("submodels/serviceable", SGRawValuePointer<bool>(&serviceable));
131 props->tie("load/rel-brg-to-user-deg",
132 SGRawValueMethods<FGAIBallistic,double>
133 (*this, &FGAIBallistic::getRelBrgHitchToUser));
134 props->tie("load/elev-to-user-deg",
135 SGRawValueMethods<FGAIBallistic,double>
136 (*this, &FGAIBallistic::getElevHitchToUser));
138 props->tie("velocities/vertical-speed-fps",
139 SGRawValuePointer<double>(&vs));
140 props->tie("velocities/true-airspeed-kt",
141 SGRawValuePointer<double>(&speed));
142 props->tie("velocities/speed-east-fps",
143 SGRawValuePointer<double>(&_speed_east_fps));
144 props->tie("velocities/speed-north-fps",
145 SGRawValuePointer<double>(&_speed_north_fps));
147 props->tie("position/x-offset",
148 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getXOffset, &FGAIBase::setXoffset));
149 props->tie("position/y-offset",
150 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getYOffset, &FGAIBase::setYoffset));
151 props->tie("position/z-offset",
152 SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getZOffset, &FGAIBase::setZoffset));
153 props->tie("position/tgt-x-offset",
154 SGRawValueMethods<FGAIBallistic,double>(*this, &FGAIBallistic::getTgtXOffset, &FGAIBallistic::setTgtXOffset));
155 props->tie("position/tgt-y-offset",
156 SGRawValueMethods<FGAIBallistic,double>(*this, &FGAIBallistic::getTgtYOffset, &FGAIBallistic::setTgtYOffset));
157 props->tie("position/tgt-z-offset",
158 SGRawValueMethods<FGAIBallistic,double>(*this, &FGAIBallistic::getTgtZOffset, &FGAIBallistic::setTgtZOffset));
161 void FGAIWingman::unbind() {
162 FGAIBallistic::unbind();
165 props->untie("SubID");
167 props->untie("orientation/pitch-deg");
168 props->untie("orientation/roll-deg");
169 props->untie("orientation/true-heading-deg");
171 props->untie("controls/formate-to-ac");
172 props->untie("controls/break");
173 props->untie("controls/join");
174 props->untie("controls/tgt-heading-deg");
175 props->untie("controls/tgt-speed-kt");
176 props->untie("controls/break-deg-rel");
177 props->untie("controls/coefficients/heading");
178 props->untie("controls/coefficients/pitch");
179 props->untie("controls/coefficients/bank");
180 props->untie("controls/coefficients/speed");
182 props->untie("submodels/serviceable");
184 props->untie("velocities/true-airspeed-kt");
185 props->untie("velocities/vertical-speed-fps");
186 props->untie("velocities/speed_east_fps");
187 props->untie("velocities/speed_north_fps");
189 props->untie("load/rel-brg-to-user-deg");
190 props->untie("load/elev-to-user-deg");
192 props->untie("position/altitude-ft");
193 props->untie("position/latitude-deg");
194 props->untie("position/longitude-deg");
195 props->untie("position/x-offset");
196 props->untie("position/y-offset");
197 props->untie("position/z-offset");
198 props->untie("position/tgt-x-offset");
199 props->untie("position/tgt-y-offset");
200 props->untie("position/tgt-z-offset");
203 bool FGAIWingman::init(bool search_in_AI_path) {
204 if (!FGAIBallistic::init(search_in_AI_path))
209 _tgt_x_offset = _x_offset;
210 _tgt_y_offset = _y_offset;
211 _tgt_z_offset = _z_offset;
222 setParentNodes(_selected_ac);
224 props->setStringValue("submodels/path", _path.c_str());
225 user_WoW_node = fgGetNode("gear/gear[1]/wow", true);
229 void FGAIWingman::update(double dt) {
231 // FGAIBallistic::update(dt);
236 setBrkHdg(_break_angle);
238 FGAIBase::update(dt);
239 tgt_altitude_ft = altitude_ft;
252 double FGAIWingman::calcDistanceM(SGGeod pos1, SGGeod pos2) const {
253 //calculate the distance load to hitch
254 SGVec3d cartPos1 = SGVec3d::fromGeod(pos1);
255 SGVec3d cartPos2 = SGVec3d::fromGeod(pos2);
257 SGVec3d diff = cartPos1 - cartPos2;
258 double distance = norm(diff);
262 double FGAIWingman::calcAngle(double range, SGGeod pos1, SGGeod pos2){
265 double distance = calcDistanceM(pos1, pos2);
266 double daltM = pos1.getElevationM() - pos2.getElevationM();
268 if (fabs(distance) < SGLimits<float>::min()) {
271 double sAngle = daltM/range;
272 sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
273 angle = SGMiscd::rad2deg(asin(sAngle));
279 void FGAIWingman::formateToAC(double dt){
281 double p_hdg, p_pch, p_rll, p_agl, p_ht, p_wow = 0;
283 setTgtOffsets(dt, 25);
287 p_hdg = _p_hdg_node->getDoubleValue();
288 p_pch = _p_pch_node->getDoubleValue();
289 p_rll = _p_rll_node->getDoubleValue();
290 p_ht = _p_alt_node->getDoubleValue();
291 setOffsetPos(_parentpos, p_hdg, p_pch, p_rll);
292 setSpeed(_p_spd_node->getDoubleValue());
295 p_hdg = manager->get_user_heading();
296 p_pch = manager->get_user_pitch();
297 p_rll = manager->get_user_roll();
298 p_ht = manager->get_user_altitude();
299 setOffsetPos(userpos, p_hdg,p_pch, p_rll);
300 setSpeed(manager->get_user_speed());
303 // elapsed time has a random initialisation so that each
304 // wingman moves differently
307 // we derive a sine based factor to give us smoothly
308 // varying error between -1 and 1
309 double factor = sin(SGMiscd::deg2rad(_elapsed_time * 10));
310 double r_angle = 5 * factor;
311 double p_angle = 2.5 * factor;
312 double h_angle = 5 * factor;
313 double h_feet = 3 * factor;
315 p_agl = manager->get_user_agl();
316 p_wow = user_WoW_node->getDoubleValue();
318 if(p_agl <= 10 || p_wow == 1) {
320 //cout << "ht case1 " ;
321 } else if (p_agl > 10 && p_agl <= 150 ) {
322 setHt(p_ht, dt, 1.0);
323 //cout << "ht case2 " ;
324 } else if (p_agl > 150 && p_agl <= 250) {
325 setHt(_offsetpos.getElevationFt()+ h_feet, dt, 0.75);
326 //cout << "ht case3 " ;
328 setHt(_offsetpos.getElevationFt()+ h_feet, dt, 0.5);
329 //cout << "ht case4 " ;
332 pos.setElevationFt(_height);
333 pos.setLatitudeDeg(_offsetpos.getLatitudeDeg());
334 pos.setLongitudeDeg(_offsetpos.getLongitudeDeg());
336 // these calculations are unreliable at slow speeds
337 // and we don't want random movement on the ground
338 if(speed >= 10 && p_wow != 1) {
339 setHdg(p_hdg + h_angle, dt, 0.9);
340 setPch(p_pch + p_angle + _pitch_offset, dt, 0.9);
342 if (roll <= 115 && roll >= -115)
343 setBnk(p_rll + r_angle + _roll_offset, dt, 0.5);
345 roll = p_rll + r_angle + _roll_offset;
348 setHdg(p_hdg, dt, 0.9);
349 setPch(p_pch + _pitch_offset, dt, 0.9);
350 setBnk(p_rll + _roll_offset, dt, 0.9);
353 setOffsetVelocity(dt, pos);
356 void FGAIWingman::Break(double dt) {
360 //calculate the turn direction: 1 = right, -1 = left
361 double rel_brg = calcRelBearingDeg(tgt_heading, hdg);
362 int turn = SGMiscd::sign(rel_brg);
364 // set heading and pitch
365 setHdg(tgt_heading, dt, _coeff_hdg);
366 setPch(0, dt, _coeff_pch);
368 if (fabs(tgt_heading - hdg) >= 10)
369 setBnk(45 * turn , dt, _coeff_bnk);
371 setBnk(0, dt, _coeff_bnk);
375 void FGAIWingman::Join(double dt) {
377 double range, bearing, az2;
378 double parent_hdg, parent_spd, parent_ht= 0;
379 double p_hdg, p_pch, p_rll = 0;
381 setTgtOffsets(dt, 25);
385 p_hdg = _p_hdg_node->getDoubleValue();
386 p_pch = _p_pch_node->getDoubleValue();
387 p_rll = _p_rll_node->getDoubleValue();
388 setOffsetPos(_parentpos, p_hdg, p_pch, p_rll);
389 parent_hdg = _p_hdg_node->getDoubleValue();
390 parent_spd = _p_spd_node->getDoubleValue();
393 p_hdg = manager->get_user_heading();
394 p_pch = manager->get_user_pitch();
395 p_rll = manager->get_user_roll();
396 setOffsetPos(userpos, p_hdg, p_pch, p_rll);
397 parent_hdg = manager->get_user_heading();
398 parent_spd = manager->get_user_speed();
401 setSpeed(parent_spd);
403 double distance = calcDistanceM(pos, _offsetpos);
404 double daltM = _offsetpos.getElevationM() - pos.getElevationM();
406 double hdg_l_lim = parent_hdg - limit;
407 SG_NORMALIZE_RANGE(hdg_l_lim, 0.0, 360.0);
408 double hdg_r_lim = parent_hdg + limit;
409 SG_NORMALIZE_RANGE(hdg_r_lim, 0.0, 360.0);
411 if (distance <= 2 && fabs(daltM) <= 2 &&
412 (hdg >= hdg_l_lim || hdg <= hdg_r_lim)){
413 _height = _offsetpos.getElevationFt();
414 _formate_to_ac = true;
417 SG_LOG(SG_GENERAL, SG_ALERT, _name << " joined " << " RANGE " << distance
418 << " SPEED " << speed );
423 geo_inverse_wgs_84(pos, _offsetpos, &bearing, &az2, &range);
425 double rel_brg = calcRelBearingDeg(bearing, hdg);
426 double recip_brg = calcRecipBearingDeg(bearing);
427 double angle = calcAngle(distance,_offsetpos, pos);
428 double approx_angle = atan2(daltM, range);
429 double frm_spd = 50; // formation speed
430 double join_rnge = 1000.0;
431 double recip_parent_hdg = calcRecipBearingDeg(parent_hdg);
432 int turn = SGMiscd::sign(rel_brg);// turn direction: 1 = right, -1 = left
434 if (range <= join_rnge && (hdg >= hdg_l_lim || hdg <= hdg_r_lim)){
436 //these are the rules governing joining
438 if ((rel_brg <= -175 || rel_brg >= 175) && range <=10 ){
439 // station is behind us - back up a bit
440 setSpeed(parent_spd - ((frm_spd/join_rnge) * range));
441 setHdg(recip_brg, dt, _coeff_hdg);
442 setPch(angle, dt, _coeff_pch);
443 //cout << _name << " backing up HEADING " << hdg
444 // << " RANGE " << range;
445 } else if (rel_brg >= -5 || rel_brg <= 5) {
446 // station is in front of us - slow down
447 setSpeed(parent_spd + ((frm_spd/100) * range));
449 setHdg(bearing, dt, 1.5);
450 setPch(angle, dt, _coeff_pch);
451 //cout << _name << " slowing HEADING " << hdg
452 // << " RANGE " << range <<endl;
453 } else if ( range <=10 ){
454 // station is to one side - equal speed and turn towards
455 setSpd(parent_spd , dt, 2.0);
457 setHdg(parent_hdg + (5 * turn), dt, _coeff_hdg);
458 //cout << _name << " equal speed HEADING " << hdg
459 // << " RANGE " << range<< endl;
461 // we missed it - equal speed and turn to recip
462 setSpd(parent_spd , dt, 2.0);
464 setHdg(recip_brg, dt, _coeff_hdg);
465 //cout << _name << " WHOOPS!! missed join HEADING " << hdg
466 // << " RANGE " << range<< endl;
469 } else if (range <= join_rnge) {
470 // we missed it - equal speed and turn to recip
471 setSpd(parent_spd , dt, 2.0);
473 setHdg(recip_brg , dt, _coeff_hdg);
474 //cout << _name << " WHOOPS!! missed approach HEADING " << hdg
475 // << " " << recip_brg
476 // /*<< " " << recip_parent_hdg*/
477 // << " RANGE " << range<< endl;
478 } else if (range > join_rnge && range <= 2000 ){
480 //cout << _name << " approach HEADING " << hdg
481 // << " RANGE " << range<< endl;
482 setSpd(parent_spd + frm_spd, dt, 2.0);
484 setHdg(bearing, dt, _coeff_hdg);
485 setPch(angle, dt, _coeff_pch);
488 //cout << _name << " hurry up HEADING " << hdg
489 // << " RANGE " << range<< endl;
490 setSpd(_max_speed -10, dt, 2.0);
492 setHdg(bearing, dt, _coeff_hdg);
493 setPch(angle, dt, _coeff_pch);
500 if (fabs(bearing - hdg) >= 10)
501 setBnk(45 * turn , dt, _coeff_bnk);
503 setBnk(0, dt, _coeff_bnk);
507 void FGAIWingman::Run(double dt) {
509 // don't let speed become negative
510 SG_CLAMP_RANGE(speed, 100.0, _max_speed);
512 double speed_fps = speed * SG_KT_TO_FPS;
514 // calculate vertical and horizontal speed components
518 vs = sin( pitch * SG_DEGREES_TO_RADIANS ) * speed_fps;
519 hs = cos( pitch * SG_DEGREES_TO_RADIANS ) * speed_fps;
522 //cout << "vs hs " << vs << " " << hs << endl;
524 //resolve horizontal speed into north and east components:
525 double speed_north_fps = cos(hdg / SG_RADIANS_TO_DEGREES) * hs;
526 double speed_east_fps = sin(hdg / SG_RADIANS_TO_DEGREES) * hs;
528 // convert horizontal speed (fps) to degrees per second
529 double speed_north_deg_sec = speed_north_fps / ft_per_deg_lat;
530 double speed_east_deg_sec = speed_east_fps / ft_per_deg_lon;
532 //get wind components
533 _wind_from_north = manager->get_wind_from_north();
534 _wind_from_east = manager->get_wind_from_east();
536 // convert wind speed (fps) to degrees lat/lon per second
537 double wind_speed_from_north_deg_sec = _wind_from_north / ft_per_deg_lat;
538 double wind_speed_from_east_deg_sec = _wind_from_east / ft_per_deg_lon;
540 //recombine the horizontal velocity components
541 hs = sqrt(((speed_north_fps) * (speed_north_fps))
542 + ((speed_east_fps)* (speed_east_fps )));
547 if (vs <= 0.00001 && vs >= -0.00001)
550 //cout << "lat " << pos.getLatitudeDeg()<< endl;
552 pos.setLatitudeDeg( pos.getLatitudeDeg()
553 + (speed_north_deg_sec - wind_speed_from_north_deg_sec) * dt );
554 pos.setLongitudeDeg( pos.getLongitudeDeg()
555 + (speed_east_deg_sec - wind_speed_from_east_deg_sec ) * dt );
556 pos.setElevationFt(pos.getElevationFt() + vs * dt);
558 //cout << _name << " run hs " << hs << " vs " << vs << endl;
560 // recalculate total speed
561 if ( vs == 0 && hs == 0)
564 speed = sqrt( vs * vs + hs * hs) / SG_KT_TO_FPS;
566 // recalculate elevation and azimuth (velocity vectors)
567 pitch = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
568 hdg = atan2((speed_east_fps),(speed_north_fps))* SG_RADIANS_TO_DEGREES;
570 // rationalise heading
571 SG_NORMALIZE_RANGE(hdg, 0.0, 360.0);