]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIAircraft.cxx
Set the format default to float instead of int.
[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     props->tie("controls/gear/gear-down",
82                SGRawValueMethods<FGAIAircraft,bool>(*this,
83                                               &FGAIAircraft::_getGearDown));
84
85     props->getNode("controls/lighting/landing-lights", true)
86            ->alias("controls/gear/gear-down");
87 }
88
89 void FGAIAircraft::unbind() {
90     FGAIBase::unbind();
91
92     props->untie("controls/gear/gear-down");
93     props->getNode("controls/lighting/landing-lights")->unalias();
94 }
95
96
97 void FGAIAircraft::update(double dt) {
98
99    FGAIBase::update(dt);
100    Run(dt);
101    Transform();
102 }
103
104 void FGAIAircraft::SetPerformance(const PERF_STRUCT *ps) {
105    
106    performance = ps;
107
108
109
110 void FGAIAircraft::Run(double dt) {
111
112    FGAIAircraft::dt = dt;
113
114    if (fp) ProcessFlightPlan(dt);
115         
116    double turn_radius_ft;
117    double turn_circum_ft;
118    double speed_north_deg_sec;
119    double speed_east_deg_sec;
120    double dist_covered_ft;
121    double alpha;
122
123    // adjust speed
124    double speed_diff = tgt_speed - speed;
125    if (fabs(speed_diff) > 0.2) {
126      if (speed_diff > 0.0) speed += performance->accel * dt;
127      if (speed_diff < 0.0) {
128         if (!no_roll) {
129            speed -= performance->decel * dt * 3;
130         } else {
131            speed -= performance->decel * dt;
132         }
133      }
134    } 
135    
136    // convert speed to degrees per second
137    speed_north_deg_sec = cos( hdg / SG_RADIANS_TO_DEGREES )
138                           * speed * 1.686 / ft_per_deg_lat;
139    speed_east_deg_sec  = sin( hdg / SG_RADIANS_TO_DEGREES )
140                           * speed * 1.686 / ft_per_deg_lon;
141
142    // set new position
143    pos.setlat( pos.lat() + speed_north_deg_sec * dt);
144    pos.setlon( pos.lon() + speed_east_deg_sec * dt); 
145
146    // adjust heading based on current bank angle
147    if (roll != 0.0) {
148      turn_radius_ft = 0.088362 * speed * speed
149                        / tan( fabs(roll) / SG_RADIANS_TO_DEGREES );
150      turn_circum_ft = SGD_2PI * turn_radius_ft;
151      dist_covered_ft = speed * 1.686 * dt; 
152      alpha = dist_covered_ft / turn_circum_ft * 360.0;
153      hdg += alpha * sign( roll );
154      if ( hdg > 360.0 ) hdg -= 360.0;
155      if ( hdg < 0.0) hdg += 360.0;
156    }
157
158    // adjust target bank angle if heading lock engaged
159    if (hdg_lock) {
160      double bank_sense = 0.0;
161      double diff = fabs(hdg - tgt_heading);
162      if (diff > 180) diff = fabs(diff - 360);
163      double sum = hdg + diff;
164      if (sum > 360.0) sum -= 360.0;
165      if (fabs(sum - tgt_heading) < 1.0) { 
166        bank_sense = 1.0;   // right turn
167      } else {
168        bank_sense = -1.0;  // left turn
169      } 
170      if (diff < 30) {
171          tgt_roll = diff * bank_sense; 
172      } else {
173          tgt_roll = 30.0 * bank_sense;
174      }
175    }
176
177    // adjust bank angle, use 9 degrees per second
178    double bank_diff = tgt_roll - roll;
179    if (fabs(bank_diff) > 0.2) {
180      if (bank_diff > 0.0) roll += 9.0 * dt;
181      if (bank_diff < 0.0) roll -= 9.0 * dt;
182    }
183
184    // adjust altitude (meters) based on current vertical speed (fpm)
185    altitude += vs / 60.0 * dt;
186    pos.setelev(altitude * SG_FEET_TO_METER);  
187    double altitude_ft = altitude;
188
189    // find target vertical speed if altitude lock engaged
190    if (alt_lock && use_perf_vs) {
191      if (altitude_ft < tgt_altitude) {
192        tgt_vs = tgt_altitude - altitude_ft;
193        if (tgt_vs > performance->climb_rate)
194          tgt_vs = performance->climb_rate;
195      } else {
196        tgt_vs = tgt_altitude - altitude_ft;
197        if (tgt_vs  < (-performance->descent_rate))
198          tgt_vs = -performance->descent_rate;
199      }
200    }
201
202    if (alt_lock && !use_perf_vs) {
203      double max_vs = 4*(tgt_altitude - altitude);
204      double min_vs = 100;
205      if (tgt_altitude < altitude) min_vs = -100.0;
206      if ((fabs(tgt_altitude - altitude) < 1500.0) && 
207          (fabs(max_vs) < fabs(tgt_vs))) tgt_vs = max_vs;
208      if (fabs(tgt_vs) < fabs(min_vs)) tgt_vs = min_vs;
209    }   
210
211    // adjust vertical speed
212    double vs_diff = tgt_vs - vs;
213    if (fabs(vs_diff) > 10.0) {
214      if (vs_diff > 0.0) {
215        vs += 900.0 * dt;
216        if (vs > tgt_vs) vs = tgt_vs;
217      } else {
218        vs -= 400.0 * dt;
219        if (vs < tgt_vs) vs = tgt_vs;
220      }
221    }   
222    
223    // match pitch angle to vertical speed
224    if (vs > 0){
225      pitch = vs * 0.005;
226    } else {
227      pitch = vs * 0.002;
228    }
229
230    //###########################//
231    // do calculations for radar //
232    //###########################//
233    double range_ft2 = UpdateRadar(manager);
234
235    //************************************//
236    // Tanker code                        //
237    //************************************//
238
239    if ( isTanker) {
240      if ( (range_ft2 < 250.0 * 250.0) &&
241           (y_shift > 0.0)    &&
242           (elevation > 0.0) ) {
243        refuel_node->setBoolValue(true);
244      } else {
245        refuel_node->setBoolValue(false);
246      } 
247    }
248 }
249
250
251 void FGAIAircraft::AccelTo(double speed) {
252    tgt_speed = speed;
253 }
254
255
256 void FGAIAircraft::PitchTo(double angle) {
257    tgt_pitch = angle;
258    alt_lock = false;
259 }
260
261
262 void FGAIAircraft::RollTo(double angle) {
263    tgt_roll = angle;
264    hdg_lock = false; 
265 }
266
267
268 void FGAIAircraft::YawTo(double angle) {
269    tgt_yaw = angle;
270 }
271
272
273 void FGAIAircraft::ClimbTo(double altitude) {
274    tgt_altitude = altitude;
275    alt_lock = true;
276 }
277
278
279 void FGAIAircraft::TurnTo(double heading) {
280    tgt_heading = heading;
281    hdg_lock = true;
282 }
283
284
285 double FGAIAircraft::sign(double x) {
286   if ( x < 0.0 ) { return -1.0; }
287   else { return 1.0; }
288 }
289
290 void FGAIAircraft::SetFlightPlan(FGAIFlightPlan *f) {
291   fp = f;
292 }
293
294 void FGAIAircraft::ProcessFlightPlan( double dt ) {
295   FGAIFlightPlan::waypoint* prev = 0; // the one behind you
296   FGAIFlightPlan::waypoint* curr = 0; // the one ahead
297   FGAIFlightPlan::waypoint* next = 0; // the next plus 1
298   prev = fp->getPreviousWaypoint();
299   curr = fp->getCurrentWaypoint();
300   next = fp->getNextWaypoint();
301   dt_count += dt;
302
303   if (!prev) {  //beginning of flightplan, do this initialization once
304     fp->IncrementWaypoint();
305     prev = fp->getPreviousWaypoint(); //first waypoint
306     curr = fp->getCurrentWaypoint();  //second waypoint
307     next = fp->getNextWaypoint();     //third waypoint (might not exist!) 
308     setLatitude(prev->latitude);
309     setLongitude(prev->longitude);
310     setSpeed(prev->speed);
311     setAltitude(prev->altitude);
312     setHeading(fp->getBearing(prev->latitude, prev->longitude, curr));
313     if (next) fp->setLeadDistance(speed, hdg, curr, next);
314     
315     if (curr->crossat > -1000.0) { //use a calculated descent/climb rate
316         use_perf_vs = false;
317         tgt_vs = (curr->crossat - prev->altitude)/
318                  (fp->getDistanceToGo(pos.lat(), pos.lon(), curr)/
319                                       6076.0/prev->speed*60.0);
320         tgt_altitude = curr->crossat;
321     } else {
322         use_perf_vs = true;
323         tgt_altitude = prev->altitude;
324     }
325     alt_lock = hdg_lock = true;
326     no_roll = prev->on_ground;
327     //cout << "First waypoint:  " << prev->name << endl;
328     //cout << "  Target speed:    " << tgt_speed << endl;
329     //cout << "  Target altitude: " << tgt_altitude << endl;
330     //cout << "  Target heading:  " << tgt_heading << endl << endl;       
331     return;  
332   } // end of initialization
333
334   // let's only process the flight plan every 100 ms.
335   if (dt_count < 0.1) {
336     return;
337   } else {
338     dt_count = 0;
339
340     // check to see if we've reached the lead point for our next turn
341     double dist_to_go = fp->getDistanceToGo(pos.lat(), pos.lon(), curr); 
342     double lead_dist = fp->getLeadDistance();
343     if (lead_dist < (2*speed)) lead_dist = 2*speed;  //don't skip over the waypoint
344     //cout << "dist_to_go: " << dist_to_go << ",  lead_dist: " << lead_dist << endl;
345
346     if ( dist_to_go < lead_dist ) {
347       if (curr->finished) {  //end of the flight plan, so terminate
348         setDie(true);
349         return;
350       }
351       // we've reached the lead-point for the waypoint ahead 
352       if (next) tgt_heading = fp->getBearing(curr, next);  
353       fp->IncrementWaypoint();
354       prev = fp->getPreviousWaypoint();
355       curr = fp->getCurrentWaypoint();
356       next = fp->getNextWaypoint();
357       if (next) fp->setLeadDistance(speed, tgt_heading, curr, next);
358       if (curr->crossat > -1000.0) {
359         use_perf_vs = false;
360         tgt_vs = (curr->crossat - altitude)/
361                  (fp->getDistanceToGo(pos.lat(), pos.lon(), curr)/6076.0/speed*60.0);
362         tgt_altitude = curr->crossat;
363       } else {
364         use_perf_vs = true;
365         tgt_altitude = prev->altitude;
366       }
367       tgt_speed = prev->speed;
368       hdg_lock = alt_lock = true;
369       no_roll = prev->on_ground;
370       //cout << "Crossing waypoint: " << prev->name << endl;
371       //cout << "  Target speed:    " << tgt_speed << endl;
372       //cout << "  Target altitude: " << tgt_altitude << endl;
373       //cout << "  Target heading:  " << tgt_heading << endl << endl;       
374     } else {
375         double calc_bearing = fp->getBearing(pos.lat(), pos.lon(), curr);
376         double hdg_error = calc_bearing - tgt_heading;
377         if (fabs(hdg_error) > 1.0) {
378           TurnTo( calc_bearing ); 
379         }
380     }
381      
382   }
383
384 }
385
386 bool FGAIAircraft::_getGearDown() const {
387    return ((props->getFloatValue("position/altitude-agl-ft") < 900.0)
388             && (props->getFloatValue("velocities/airspeed-kt")
389                  < performance->land_speed*1.25));
390 }