]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIAircraft.cxx
Move the radar update code to the AIBase class. There seems to be a problem where...
[flightgear.git] / src / AIModel / AIAircraft.cxx
1 // FGAIAircraft - FGAIBase-derived class creates an AI airplane
2 //
3 // Written by David Culp, started October 2003.
4 //
5 // Copyright (C) 2003  David P. Culp - davidculp2@comcast.net
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <simgear/math/point3d.hxx>
26 #include <Main/fg_props.hxx>
27 #include <Main/globals.hxx>
28 #include <Scenery/scenery.hxx>
29 #include <string>
30 #include <math.h>
31
32 SG_USING_STD(string);
33
34 #include "AIAircraft.hxx"
35
36 //
37 // accel, decel, climb_rate, descent_rate, takeoff_speed, climb_speed,
38 // cruise_speed, descent_speed, land_speed
39 //
40 const FGAIAircraft::PERF_STRUCT FGAIAircraft::settings[] = {
41     // light aircraft
42     {2.0, 2.0,  450.0, 1000.0,  70.0,  80.0, 100.0,  80.0,  60.0},
43     // ww2_fighter
44     {4.0, 2.0, 3000.0, 1500.0, 110.0, 180.0, 250.0, 200.0, 100.0},
45     // jet_transport
46     {5.0, 2.0, 3000.0, 1500.0, 140.0, 300.0, 430.0, 300.0, 130.0},
47     // jet_fighter
48     {7.0, 3.0, 4000.0, 2000.0, 150.0, 350.0, 500.0, 350.0, 150.0},
49     // tanker
50     {5.0, 2.0, 3000.0, 1500.0, 140.0, 300.0, 430.0, 300.0, 130.0}
51 };
52
53
54 FGAIAircraft::FGAIAircraft(FGAIManager* mgr) {
55    manager = mgr;   
56    _type_str = "aircraft";
57    _otype = otAircraft;
58    fp = 0;
59    dt_count = 0;
60    use_perf_vs = true;
61    isTanker = false;
62
63    // set heading and altitude locks
64    hdg_lock = false;
65    alt_lock = false;
66 }
67
68
69 FGAIAircraft::~FGAIAircraft() {
70 }
71
72
73 bool FGAIAircraft::init() {
74    refuel_node = fgGetNode("systems/refuel/contact", true);
75    return FGAIBase::init();
76 }
77
78 void FGAIAircraft::bind() {
79     FGAIBase::bind();
80
81 /*
82     props->tie("controls/gear/gear-down",
83                SGRawValueFunctions<bool>(FGAIAircraft::_getGearDown));
84
85     props->getNode("controls/lighting/landing-lights", true)
86            ->alias("controls/gear/gear-down");
87 */
88 }
89
90 void FGAIAircraft::unbind() {
91     FGAIBase::unbind();
92
93 //    props->untie("controls/gear/gear-down");
94 //    props->getNode("controls/lighting/landing-lights")->unalias();
95 }
96
97
98 void FGAIAircraft::update(double dt) {
99
100    FGAIBase::update(dt);
101    Run(dt);
102    Transform();
103 }
104
105 void FGAIAircraft::SetPerformance(const PERF_STRUCT *ps) {
106    
107    performance = ps;
108
109
110
111 void FGAIAircraft::Run(double dt) {
112
113    FGAIAircraft::dt = dt;
114
115    if (fp) ProcessFlightPlan(dt);
116         
117    double turn_radius_ft;
118    double turn_circum_ft;
119    double speed_north_deg_sec;
120    double speed_east_deg_sec;
121    double dist_covered_ft;
122    double alpha;
123
124    // adjust speed
125    double speed_diff = tgt_speed - speed;
126    if (fabs(speed_diff) > 0.2) {
127      if (speed_diff > 0.0) speed += performance->accel * dt;
128      if (speed_diff < 0.0) {
129         if (!no_roll) {
130            speed -= performance->decel * dt * 3;
131         } else {
132            speed -= performance->decel * dt;
133         }
134      }
135    } 
136    
137    // convert speed to degrees per second
138    speed_north_deg_sec = cos( hdg / SG_RADIANS_TO_DEGREES )
139                           * speed * 1.686 / ft_per_deg_lat;
140    speed_east_deg_sec  = sin( hdg / SG_RADIANS_TO_DEGREES )
141                           * speed * 1.686 / ft_per_deg_lon;
142
143    // set new position
144    pos.setlat( pos.lat() + speed_north_deg_sec * dt);
145    pos.setlon( pos.lon() + speed_east_deg_sec * dt); 
146
147    // adjust heading based on current bank angle
148    if (roll != 0.0) {
149      turn_radius_ft = 0.088362 * speed * speed
150                        / tan( fabs(roll) / SG_RADIANS_TO_DEGREES );
151      turn_circum_ft = SGD_2PI * turn_radius_ft;
152      dist_covered_ft = speed * 1.686 * dt; 
153      alpha = dist_covered_ft / turn_circum_ft * 360.0;
154      hdg += alpha * sign( roll );
155      if ( hdg > 360.0 ) hdg -= 360.0;
156      if ( hdg < 0.0) hdg += 360.0;
157    }
158
159    // adjust target bank angle if heading lock engaged
160    if (hdg_lock) {
161      double bank_sense = 0.0;
162      double diff = fabs(hdg - tgt_heading);
163      if (diff > 180) diff = fabs(diff - 360);
164      double sum = hdg + diff;
165      if (sum > 360.0) sum -= 360.0;
166      if (fabs(sum - tgt_heading) < 1.0) { 
167        bank_sense = 1.0;   // right turn
168      } else {
169        bank_sense = -1.0;  // left turn
170      } 
171      if (diff < 30) {
172          tgt_roll = diff * bank_sense; 
173      } else {
174          tgt_roll = 30.0 * bank_sense;
175      }
176    }
177
178    // adjust bank angle, use 9 degrees per second
179    double bank_diff = tgt_roll - roll;
180    if (fabs(bank_diff) > 0.2) {
181      if (bank_diff > 0.0) roll += 9.0 * dt;
182      if (bank_diff < 0.0) roll -= 9.0 * dt;
183    }
184
185    // adjust altitude (meters) based on current vertical speed (fpm)
186    altitude += vs / 60.0 * dt;
187    pos.setelev(altitude * SG_FEET_TO_METER);  
188    double altitude_ft = altitude;
189
190    // find target vertical speed if altitude lock engaged
191    if (alt_lock && use_perf_vs) {
192      if (altitude_ft < tgt_altitude) {
193        tgt_vs = tgt_altitude - altitude_ft;
194        if (tgt_vs > performance->climb_rate)
195          tgt_vs = performance->climb_rate;
196      } else {
197        tgt_vs = tgt_altitude - altitude_ft;
198        if (tgt_vs  < (-performance->descent_rate))
199          tgt_vs = -performance->descent_rate;
200      }
201    }
202
203    if (alt_lock && !use_perf_vs) {
204      double max_vs = 4*(tgt_altitude - altitude);
205      double min_vs = 100;
206      if (tgt_altitude < altitude) min_vs = -100.0;
207      if ((fabs(tgt_altitude - altitude) < 1500.0) && 
208          (fabs(max_vs) < fabs(tgt_vs))) tgt_vs = max_vs;
209      if (fabs(tgt_vs) < fabs(min_vs)) tgt_vs = min_vs;
210    }   
211
212    // adjust vertical speed
213    double vs_diff = tgt_vs - vs;
214    if (fabs(vs_diff) > 10.0) {
215      if (vs_diff > 0.0) {
216        vs += 900.0 * dt;
217        if (vs > tgt_vs) vs = tgt_vs;
218      } else {
219        vs -= 400.0 * dt;
220        if (vs < tgt_vs) vs = tgt_vs;
221      }
222    }   
223    
224    // match pitch angle to vertical speed
225    if (vs > 0){
226      pitch = vs * 0.005;
227    } else {
228      pitch = vs * 0.002;
229    }
230
231    //###########################//
232    // do calculations for radar //
233    //###########################//
234    double range_ft2 = UpdateRadar(manager);
235
236    //************************************//
237    // Tanker code                        //
238    //************************************//
239
240    if ( isTanker) {
241      if ( (range_ft2 < 250.0 * 250.0) &&
242           (y_shift > 0.0)    &&
243           (elevation > 0.0) ) {
244        refuel_node->setBoolValue(true);
245      } else {
246        refuel_node->setBoolValue(false);
247      } 
248    }
249 }
250
251
252 void FGAIAircraft::AccelTo(double speed) {
253    tgt_speed = speed;
254 }
255
256
257 void FGAIAircraft::PitchTo(double angle) {
258    tgt_pitch = angle;
259    alt_lock = false;
260 }
261
262
263 void FGAIAircraft::RollTo(double angle) {
264    tgt_roll = angle;
265    hdg_lock = false; 
266 }
267
268
269 void FGAIAircraft::YawTo(double angle) {
270    tgt_yaw = angle;
271 }
272
273
274 void FGAIAircraft::ClimbTo(double altitude) {
275    tgt_altitude = altitude;
276    alt_lock = true;
277 }
278
279
280 void FGAIAircraft::TurnTo(double heading) {
281    tgt_heading = heading;
282    hdg_lock = true;
283 }
284
285
286 double FGAIAircraft::sign(double x) {
287   if ( x < 0.0 ) { return -1.0; }
288   else { return 1.0; }
289 }
290
291 void FGAIAircraft::SetFlightPlan(FGAIFlightPlan *f) {
292   fp = f;
293 }
294
295 void FGAIAircraft::ProcessFlightPlan( double dt ) {
296   FGAIFlightPlan::waypoint* prev = 0; // the one behind you
297   FGAIFlightPlan::waypoint* curr = 0; // the one ahead
298   FGAIFlightPlan::waypoint* next = 0; // the next plus 1
299   prev = fp->getPreviousWaypoint();
300   curr = fp->getCurrentWaypoint();
301   next = fp->getNextWaypoint();
302   dt_count += dt;
303
304   if (!prev) {  //beginning of flightplan, do this initialization once
305     fp->IncrementWaypoint();
306     prev = fp->getPreviousWaypoint(); //first waypoint
307     curr = fp->getCurrentWaypoint();  //second waypoint
308     next = fp->getNextWaypoint();     //third waypoint (might not exist!) 
309     setLatitude(prev->latitude);
310     setLongitude(prev->longitude);
311     setSpeed(prev->speed);
312     setAltitude(prev->altitude);
313     setHeading(fp->getBearing(prev->latitude, prev->longitude, curr));
314     if (next) fp->setLeadDistance(speed, hdg, curr, next);
315     
316     if (curr->crossat > -1000.0) { //use a calculated descent/climb rate
317         use_perf_vs = false;
318         tgt_vs = (curr->crossat - prev->altitude)/
319                  (fp->getDistanceToGo(pos.lat(), pos.lon(), curr)/
320                                       6076.0/prev->speed*60.0);
321         tgt_altitude = curr->crossat;
322     } else {
323         use_perf_vs = true;
324         tgt_altitude = prev->altitude;
325     }
326     alt_lock = hdg_lock = true;
327     no_roll = prev->on_ground;
328     //cout << "First waypoint:  " << prev->name << endl;
329     //cout << "  Target speed:    " << tgt_speed << endl;
330     //cout << "  Target altitude: " << tgt_altitude << endl;
331     //cout << "  Target heading:  " << tgt_heading << endl << endl;       
332     return;  
333   } // end of initialization
334
335   // let's only process the flight plan every 100 ms.
336   if (dt_count < 0.1) {
337     return;
338   } else {
339     dt_count = 0;
340
341     // check to see if we've reached the lead point for our next turn
342     double dist_to_go = fp->getDistanceToGo(pos.lat(), pos.lon(), curr); 
343     double lead_dist = fp->getLeadDistance();
344     if (lead_dist < (2*speed)) lead_dist = 2*speed;  //don't skip over the waypoint
345     //cout << "dist_to_go: " << dist_to_go << ",  lead_dist: " << lead_dist << endl;
346
347     if ( dist_to_go < lead_dist ) {
348       if (curr->finished) {  //end of the flight plan, so terminate
349         setDie(true);
350         return;
351       }
352       // we've reached the lead-point for the waypoint ahead 
353       if (next) tgt_heading = fp->getBearing(curr, next);  
354       fp->IncrementWaypoint();
355       prev = fp->getPreviousWaypoint();
356       curr = fp->getCurrentWaypoint();
357       next = fp->getNextWaypoint();
358       if (next) fp->setLeadDistance(speed, tgt_heading, curr, next);
359       if (curr->crossat > -1000.0) {
360         use_perf_vs = false;
361         tgt_vs = (curr->crossat - altitude)/
362                  (fp->getDistanceToGo(pos.lat(), pos.lon(), curr)/6076.0/speed*60.0);
363         tgt_altitude = curr->crossat;
364       } else {
365         use_perf_vs = true;
366         tgt_altitude = prev->altitude;
367       }
368       tgt_speed = prev->speed;
369       hdg_lock = alt_lock = true;
370       no_roll = prev->on_ground;
371       //cout << "Crossing waypoint: " << prev->name << endl;
372       //cout << "  Target speed:    " << tgt_speed << endl;
373       //cout << "  Target altitude: " << tgt_altitude << endl;
374       //cout << "  Target heading:  " << tgt_heading << endl << endl;       
375     } else {
376         double calc_bearing = fp->getBearing(pos.lat(), pos.lon(), curr);
377         double hdg_error = calc_bearing - tgt_heading;
378         if (fabs(hdg_error) > 1.0) {
379           TurnTo( calc_bearing ); 
380         }
381     }
382      
383   }
384
385 }
386
387