]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIWingman.cxx
Interim windows build fix
[flightgear.git] / src / AIModel / AIWingman.cxx
1 // FGAIWingman - FGAIBllistic-derived class creates an AI Wingman
2 //
3 // Written by Vivian Meazza, started February 2008.
4 // - vivian.meazza at lineone.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 <simgear/sg_inlines.h>
25
26 #include <Main/fg_props.hxx>
27
28 #include "AIWingman.hxx"
29
30 FGAIWingman::FGAIWingman() : FGAIBallistic(otWingman),
31 _formate_to_ac(true),
32 _break(false),
33 _join(false),
34 _break_angle(-90),
35 _coeff_hdg(5.0),
36 _coeff_pch(5.0),
37 _coeff_bnk(5.0),
38 _coeff_spd(2.0)
39
40 {
41     invisible = false;
42     _parent="";
43     tgt_heading = 250;
44
45 }
46
47 FGAIWingman::~FGAIWingman() {}
48
49 void FGAIWingman::readFromScenario(SGPropertyNode* scFileNode) {
50     if (!scFileNode)
51         return;
52
53     FGAIBase::readFromScenario(scFileNode);
54
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));
75
76
77 }
78
79 void FGAIWingman::bind() {
80     FGAIBallistic::bind();
81
82     props->untie("controls/slave-to-ac");
83
84     tie("id", SGRawValueMethods<FGAIBase,int>(*this,
85         &FGAIBase::getID));
86     tie("subID", SGRawValueMethods<FGAIBase,int>(*this,
87         &FGAIBase::_getSubID));
88     tie("position/altitude-ft",
89         SGRawValueMethods<FGAIBase,double>(*this,
90         &FGAIBase::_getElevationFt,
91         &FGAIBase::_setAltitude));
92     tie("position/latitude-deg",
93         SGRawValueMethods<FGAIBase,double>(*this,
94         &FGAIBase::_getLatitude,
95         &FGAIBase::_setLatitude));
96     tie("position/longitude-deg",
97         SGRawValueMethods<FGAIBase,double>(*this,
98         &FGAIBase::_getLongitude,
99         &FGAIBase::_setLongitude));
100
101     tie("controls/break", SGRawValuePointer<bool>(&_break));
102     tie("controls/join", SGRawValuePointer<bool>(&_join));
103
104     tie("controls/formate-to-ac",
105         SGRawValueMethods<FGAIWingman,bool>
106         (*this, &FGAIWingman::getFormate, &FGAIWingman::setFormate));
107     tie("controls/tgt-heading-deg",
108         SGRawValueMethods<FGAIWingman,double>
109         (*this, &FGAIWingman::getTgtHdg, &FGAIWingman::setTgtHdg));
110     tie("controls/tgt-speed-kt",
111         SGRawValueMethods<FGAIWingman,double>
112         (*this, &FGAIWingman::getTgtSpd, &FGAIWingman::setTgtSpd));
113     tie("controls/break-deg-rel",
114         SGRawValueMethods<FGAIWingman,double>
115         (*this, &FGAIWingman::getBrkAng, &FGAIWingman::setBrkAng));
116     tie("controls/coefficients/heading",
117         SGRawValuePointer<double>(&_coeff_hdg));
118     tie("controls/coefficients/pitch",
119         SGRawValuePointer<double>(&_coeff_pch));
120     tie("controls/coefficients/bank",
121         SGRawValuePointer<double>(&_coeff_bnk));
122     tie("controls/coefficients/speed",
123         SGRawValuePointer<double>(&_coeff_spd));
124
125     tie("orientation/pitch-deg",   SGRawValuePointer<double>(&pitch));
126     tie("orientation/roll-deg",    SGRawValuePointer<double>(&roll));
127     tie("orientation/true-heading-deg", SGRawValuePointer<double>(&hdg));
128
129     tie("submodels/serviceable", SGRawValuePointer<bool>(&serviceable));
130
131     tie("load/rel-brg-to-user-deg",
132         SGRawValueMethods<FGAIBallistic,double>
133         (*this, &FGAIBallistic::getRelBrgHitchToUser));
134     tie("load/elev-to-user-deg",
135         SGRawValueMethods<FGAIBallistic,double>
136         (*this, &FGAIBallistic::getElevHitchToUser));
137
138     tie("velocities/vertical-speed-fps",
139         SGRawValuePointer<double>(&vs));
140     tie("velocities/true-airspeed-kt",
141         SGRawValuePointer<double>(&speed));
142     tie("velocities/speed-east-fps",
143         SGRawValuePointer<double>(&_speed_east_fps));
144     tie("velocities/speed-north-fps",
145         SGRawValuePointer<double>(&_speed_north_fps));
146
147     tie("position/x-offset",
148         SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getXOffset, &FGAIBase::setXoffset));
149     tie("position/y-offset",
150         SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getYOffset, &FGAIBase::setYoffset));
151     tie("position/z-offset",
152         SGRawValueMethods<FGAIBase,double>(*this, &FGAIBase::_getZOffset, &FGAIBase::setZoffset));
153     tie("position/tgt-x-offset",
154         SGRawValueMethods<FGAIBallistic,double>(*this, &FGAIBallistic::getTgtXOffset, &FGAIBallistic::setTgtXOffset));
155     tie("position/tgt-y-offset",
156         SGRawValueMethods<FGAIBallistic,double>(*this, &FGAIBallistic::getTgtYOffset, &FGAIBallistic::setTgtYOffset));
157     tie("position/tgt-z-offset",
158         SGRawValueMethods<FGAIBallistic,double>(*this, &FGAIBallistic::getTgtZOffset, &FGAIBallistic::setTgtZOffset));
159 }
160
161 bool FGAIWingman::init(bool search_in_AI_path) {
162     if (!FGAIBallistic::init(search_in_AI_path))
163         return false;
164     reinit();
165     return true;
166 }
167
168 void FGAIWingman::reinit() {
169     invisible = false;
170
171     _tgt_x_offset = _x_offset;
172     _tgt_y_offset = _y_offset;
173     _tgt_z_offset = _z_offset;
174
175     hdg = _azimuth;
176     pitch = _elevation;
177     roll = _rotation;
178     _ht_agl_ft = 1e10;
179
180     if(_parent != ""){
181         setParentNode();
182     }
183
184     setParentNodes(_selected_ac);
185
186     props->setStringValue("submodels/path", _path.c_str());
187     user_WoW_node      = fgGetNode("gear/gear[1]/wow", true);
188
189     FGAIBallistic::reinit();
190 }
191
192 void FGAIWingman::update(double dt) {
193
194 //    FGAIBallistic::update(dt);
195
196     if (_formate_to_ac){
197         formateToAC(dt);
198         Transform();
199         setBrkHdg(_break_angle);
200     }else if (_break) {
201         FGAIBase::update(dt);
202         tgt_altitude_ft = altitude_ft;
203         tgt_speed = speed;
204         tgt_roll = roll;
205         tgt_pitch = pitch;
206         Break(dt);
207         Transform();
208     } else {
209         Join(dt);
210         Transform();
211     }
212
213 }
214
215 double FGAIWingman::calcDistanceM(SGGeod pos1, SGGeod pos2) const {
216     //calculate the distance load to hitch
217     SGVec3d cartPos1 = SGVec3d::fromGeod(pos1);
218     SGVec3d cartPos2 = SGVec3d::fromGeod(pos2);
219
220     SGVec3d diff = cartPos1 - cartPos2;
221     double distance = norm(diff);
222     return distance;
223 }
224
225 double FGAIWingman::calcAngle(double range, SGGeod pos1, SGGeod pos2){
226
227     double angle = 0;
228     double distance = calcDistanceM(pos1, pos2);
229     double daltM = pos1.getElevationM() - pos2.getElevationM();
230
231     if (fabs(distance) < SGLimits<float>::min()) {
232         angle = 0;
233     } else {
234         double sAngle = daltM/range;
235         sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
236         angle = SGMiscd::rad2deg(asin(sAngle));
237     }
238
239     return angle;
240 }
241
242 void FGAIWingman::formateToAC(double dt){
243
244     double p_hdg, p_pch, p_rll, p_agl, p_ht, p_wow = 0;
245
246     setTgtOffsets(dt, 25);
247
248     if (_pnode != 0) {
249         setParentPos();
250         p_hdg = _p_hdg_node->getDoubleValue();
251         p_pch = _p_pch_node->getDoubleValue();
252         p_rll = _p_rll_node->getDoubleValue();
253         p_ht  = _p_alt_node->getDoubleValue();
254         setOffsetPos(_parentpos, p_hdg, p_pch, p_rll);
255         setSpeed(_p_spd_node->getDoubleValue());
256     }else {
257         p_hdg = manager->get_user_heading();
258         p_pch = manager->get_user_pitch();
259         p_rll = manager->get_user_roll();
260         p_ht  = globals->get_aircraft_position().getElevationFt();
261         setOffsetPos(globals->get_aircraft_position(), p_hdg,p_pch, p_rll);
262         setSpeed(manager->get_user_speed());
263     }
264
265     // elapsed time has a random initialisation so that each
266     // wingman moves differently
267     _elapsed_time += dt;
268
269     // we derive a sine based factor to give us smoothly
270     // varying error between -1 and 1
271     double factor  = sin(SGMiscd::deg2rad(_elapsed_time * 10));
272     double r_angle = 5 * factor;
273     double p_angle = 2.5 * factor;
274     double h_angle = 5 * factor;
275     double h_feet  = 3 * factor;
276
277     p_agl = manager->get_user_agl();
278     p_wow = user_WoW_node->getDoubleValue();
279
280     if(p_agl <= 10 || p_wow == 1) {
281         _height = p_ht;
282         //cout << "ht case1 " ;
283     } else if (p_agl > 10 && p_agl <= 150 ) {
284         setHt(p_ht, dt, 1.0);
285         //cout << "ht case2 " ;
286     } else if (p_agl > 150 && p_agl <= 250) {
287         setHt(_offsetpos.getElevationFt()+ h_feet, dt, 0.75);
288         //cout << "ht case3 " ;
289     } else{
290         setHt(_offsetpos.getElevationFt()+ h_feet, dt, 0.5);
291         //cout << "ht case4 " ;
292     }
293
294     pos.setElevationFt(_height);
295     pos.setLatitudeDeg(_offsetpos.getLatitudeDeg());
296     pos.setLongitudeDeg(_offsetpos.getLongitudeDeg());
297
298     // these calculations are unreliable at slow speeds
299     // and we don't want random movement on the ground
300     if(speed >= 10 && p_wow != 1) {
301         setHdg(p_hdg + h_angle, dt, 0.9);
302         setPch(p_pch + p_angle + _pitch_offset, dt, 0.9);
303
304         if (roll <= 115 && roll >= -115)
305             setBnk(p_rll + r_angle + _roll_offset, dt, 0.5);
306         else
307             roll = p_rll + r_angle + _roll_offset;
308
309     } else {
310         setHdg(p_hdg, dt, 0.9);
311         setPch(p_pch + _pitch_offset, dt, 0.9);
312         setBnk(p_rll + _roll_offset, dt, 0.9);
313     }
314
315         setOffsetVelocity(dt, pos);
316 }// end formateToAC
317
318 void FGAIWingman::Break(double dt) {
319
320     Run(dt);
321
322     //calculate the turn direction: 1 = right, -1 = left
323     double rel_brg = calcRelBearingDeg(tgt_heading, hdg);
324     int turn = SGMiscd::sign(rel_brg);
325
326     // set heading and pitch
327     setHdg(tgt_heading, dt, _coeff_hdg);
328     setPch(0, dt, _coeff_pch);
329
330     if (fabs(tgt_heading - hdg) >= 10)
331         setBnk(45 * turn , dt, _coeff_bnk);
332     else
333         setBnk(0, dt, _coeff_bnk);
334
335 }  // end Break
336
337 void FGAIWingman::Join(double dt) {
338
339     double range, bearing, az2;
340     double parent_hdg, parent_spd = 0;
341     double p_hdg, p_pch, p_rll = 0;
342
343     setTgtOffsets(dt, 25);
344
345     if (_pnode != 0) {
346         setParentPos();
347         p_hdg = _p_hdg_node->getDoubleValue();
348         p_pch = _p_pch_node->getDoubleValue();
349         p_rll = _p_rll_node->getDoubleValue();
350         setOffsetPos(_parentpos, p_hdg, p_pch, p_rll);
351         parent_hdg = _p_hdg_node->getDoubleValue();
352         parent_spd = _p_spd_node->getDoubleValue();
353     }else {
354         p_hdg = manager->get_user_heading();
355         p_pch = manager->get_user_pitch();
356         p_rll = manager->get_user_roll();
357         setOffsetPos(globals->get_aircraft_position(), p_hdg, p_pch, p_rll);
358         parent_hdg = manager->get_user_heading();
359         parent_spd = manager->get_user_speed();
360     }
361
362     setSpeed(parent_spd);
363
364     double distance = calcDistanceM(pos, _offsetpos);
365     double daltM = _offsetpos.getElevationM() - pos.getElevationM();
366     double limit = 10;
367     double hdg_l_lim = parent_hdg - limit;
368     SG_NORMALIZE_RANGE(hdg_l_lim, 0.0, 360.0);
369     double hdg_r_lim = parent_hdg + limit;
370     SG_NORMALIZE_RANGE(hdg_r_lim, 0.0, 360.0);
371
372     if (distance <= 2 && fabs(daltM) <= 2 &&
373         (hdg >= hdg_l_lim || hdg <= hdg_r_lim)){
374             _height = _offsetpos.getElevationFt();
375             _formate_to_ac = true;
376             _join = false;
377
378             SG_LOG(SG_AI, SG_ALERT, _name << " joined " << " RANGE " << distance
379             << " SPEED " << speed );
380
381             return;
382     }
383
384     geo_inverse_wgs_84(pos, _offsetpos, &bearing, &az2, &range);
385
386     double rel_brg   = calcRelBearingDeg(bearing, hdg);
387     double recip_brg = calcRecipBearingDeg(bearing);
388     double angle = calcAngle(distance,_offsetpos, pos);
389     //double approx_angle = atan2(daltM, range);
390     double frm_spd = 50; // formation speed
391     double join_rnge = 1000.0;
392 //    double recip_parent_hdg = calcRecipBearingDeg(parent_hdg);
393     int turn = SGMiscd::sign(rel_brg);// turn direction: 1 = right, -1 = left
394
395     if (range <= join_rnge && (hdg >= hdg_l_lim || hdg <= hdg_r_lim)){
396
397         //these are the rules governing joining
398
399         if ((rel_brg <= -175 || rel_brg >= 175) && range <=10 ){
400             // station is behind us - back up a bit
401             setSpeed(parent_spd - ((frm_spd/join_rnge) * range));
402             setHdg(recip_brg, dt, _coeff_hdg);
403             setPch(angle, dt, _coeff_pch);
404             //cout << _name << " backing up HEADING " << hdg
405             //    << " RANGE " << range;
406         } else if (rel_brg >= -5 || rel_brg <= 5) {
407             // station is in front of us - slow down
408             setSpeed(parent_spd + ((frm_spd/100) * range));
409             //SGMiscd::clip
410             setHdg(bearing, dt, 1.5);
411             setPch(angle, dt, _coeff_pch);
412             //cout << _name << " slowing HEADING " << hdg
413             //    << " RANGE " << range <<endl;
414         } else if ( range <=10 ){
415             // station is to one side - equal speed and turn towards
416             setSpd(parent_spd , dt, 2.0);
417             setSpeed(_speed);
418             setHdg(parent_hdg + (5 * turn), dt, _coeff_hdg);
419             //cout << _name << " equal speed HEADING " << hdg
420             //    << " RANGE " << range<< endl;
421         } else {
422             // we missed it - equal speed and turn to recip
423             setSpd(parent_spd , dt, 2.0);
424             setSpeed(_speed);
425             setHdg(recip_brg, dt, _coeff_hdg);
426             //cout << _name << " WHOOPS!! missed join HEADING " << hdg
427             //    << " RANGE " << range<< endl;
428         }
429
430     } else if (range <= join_rnge) {
431         // we missed it - equal speed and turn to recip
432         setSpd(parent_spd , dt, 2.0);
433         setSpeed(_speed);
434         setHdg(recip_brg , dt, _coeff_hdg);
435         //cout << _name << " WHOOPS!! missed approach HEADING " << hdg
436         //    << " " << recip_brg
437         //    /*<< " " << recip_parent_hdg*/
438         //    << " RANGE " << range<< endl;
439     } else if (range > join_rnge && range <= 2000 ){
440         //approach phase
441         //cout << _name << " approach HEADING " << hdg
442         //        << " RANGE " << range<< endl;
443         setSpd(parent_spd + frm_spd, dt, 2.0);
444         setSpeed(_speed);
445         setHdg(bearing, dt, _coeff_hdg);
446         setPch(angle, dt, _coeff_pch);
447     } else {
448         //hurry up
449         //cout << _name << " hurry up HEADING " << hdg
450         //        << " RANGE " << range<< endl;
451         setSpd(_max_speed -10, dt, 2.0);
452         setSpeed(_speed);
453         setHdg(bearing, dt, _coeff_hdg);
454         setPch(angle, dt, _coeff_pch);
455     }
456
457     Run(dt);
458
459     // set roll
460
461     if (fabs(bearing - hdg) >= 10)
462         setBnk(45 * turn , dt, _coeff_bnk);
463     else
464         setBnk(0, dt, _coeff_bnk);
465
466 }  // end Join
467
468 void FGAIWingman::Run(double dt) {
469
470     // don't let speed become negative
471     SG_CLAMP_RANGE(speed, 100.0, _max_speed);
472
473     double speed_fps = speed * SG_KT_TO_FPS;
474
475     // calculate vertical and horizontal speed components
476     if (speed == 0.0) {
477         hs = vs = 0.0;
478     } else {
479         vs = sin( pitch * SG_DEGREES_TO_RADIANS ) * speed_fps;
480         hs = cos( pitch * SG_DEGREES_TO_RADIANS ) * speed_fps;
481     }
482
483     //cout << "vs hs " << vs << " " << hs << endl;
484
485     //resolve horizontal speed into north and east components:
486     double speed_north_fps = cos(hdg / SG_RADIANS_TO_DEGREES) * hs;
487     double speed_east_fps = sin(hdg / SG_RADIANS_TO_DEGREES) * hs;
488
489     // convert horizontal speed (fps) to degrees per second
490     double speed_north_deg_sec = speed_north_fps / ft_per_deg_lat;
491     double speed_east_deg_sec  = speed_east_fps / ft_per_deg_lon;
492
493     //get wind components
494     _wind_from_north = manager->get_wind_from_north();
495     _wind_from_east = manager->get_wind_from_east();
496
497     // convert wind speed (fps) to degrees lat/lon per second
498     double wind_speed_from_north_deg_sec = _wind_from_north / ft_per_deg_lat;
499     double wind_speed_from_east_deg_sec  = _wind_from_east / ft_per_deg_lon;
500
501     //recombine the horizontal velocity components
502     hs = sqrt(((speed_north_fps) * (speed_north_fps))
503         + ((speed_east_fps)* (speed_east_fps )));
504
505     if (hs <= 0.00001)
506         hs = 0;
507
508     if (vs <= 0.00001 && vs >= -0.00001)
509         vs = 0;
510
511     //cout << "lat " << pos.getLatitudeDeg()<< endl;
512     // set new position
513     pos.setLatitudeDeg( pos.getLatitudeDeg()
514             + (speed_north_deg_sec - wind_speed_from_north_deg_sec) * dt );
515     pos.setLongitudeDeg( pos.getLongitudeDeg()
516             + (speed_east_deg_sec - wind_speed_from_east_deg_sec ) * dt );
517     pos.setElevationFt(pos.getElevationFt() + vs * dt);
518
519     //cout << _name << " run hs " << hs << " vs " << vs << endl;
520
521     // recalculate total speed
522     if ( vs == 0 && hs == 0)
523         speed = 0;
524     else
525         speed = sqrt( vs * vs + hs * hs) / SG_KT_TO_FPS;
526
527     // recalculate elevation and azimuth (velocity vectors)
528     pitch = atan2( vs, hs ) * SG_RADIANS_TO_DEGREES;
529     hdg   = atan2((speed_east_fps),(speed_north_fps))* SG_RADIANS_TO_DEGREES;
530
531     // rationalise heading
532     SG_NORMALIZE_RANGE(hdg, 0.0, 360.0);
533
534 }// end Run
535
536 // end AIWingman