]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIAircraft.cxx
Roy Vegard Ovesen:
[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 };
50
51
52 FGAIAircraft *FGAIAircraft::_self = NULL;
53
54 FGAIAircraft::FGAIAircraft(FGAIManager* mgr) {
55    manager = mgr;   
56    _self = this;
57    _type_str = "aircraft";
58    _otype = otAircraft;
59
60    // set heading and altitude locks
61    hdg_lock = false;
62    alt_lock = false;
63 }
64
65
66 FGAIAircraft::~FGAIAircraft() {
67     _self = NULL;
68 }
69
70
71 bool FGAIAircraft::init() {
72    return FGAIBase::init();
73 }
74
75 void FGAIAircraft::bind() {
76     FGAIBase::bind();
77
78     props->tie("controls/gear/gear-down",
79                SGRawValueFunctions<bool>(FGAIAircraft::_getGearDown));
80
81 /*
82     props->getNode("controls/lighting/landing-lights", true)
83            ->alias("controls/gear/gear-down");
84 */
85 }
86
87 void FGAIAircraft::unbind() {
88     FGAIBase::unbind();
89
90     props->untie("controls/gear/gear-down");
91 //    props->getNode("controls/lighting/landing-lights")->unalias();
92 }
93
94
95 void FGAIAircraft::update(double dt) {
96
97    Run(dt);
98    Transform();
99    FGAIBase::update(dt);
100 }
101
102 void FGAIAircraft::SetPerformance(const PERF_STRUCT *ps) {
103    
104    performance = ps;
105
106
107
108 void FGAIAircraft::Run(double dt) {
109
110    FGAIAircraft::dt = dt;
111         
112    double turn_radius_ft;
113    double turn_circum_ft;
114    double speed_north_deg_sec;
115    double speed_east_deg_sec;
116    double ft_per_deg_lon;
117    double ft_per_deg_lat;
118    double dist_covered_ft;
119    double alpha;
120
121    // get size of a degree at this latitude
122    ft_per_deg_lat = 366468.96 - 3717.12 * cos(pos.lat()/SG_RADIANS_TO_DEGREES);
123    ft_per_deg_lon = 365228.16 * cos(pos.lat() / SG_RADIANS_TO_DEGREES);
124
125    // adjust speed
126    double speed_diff = tgt_speed - speed;
127    if (fabs(speed_diff) > 0.2) {
128      if (speed_diff > 0.0) speed += performance->accel * dt;
129      if (speed_diff < 0.0) speed -= performance->decel * dt;
130    } 
131    
132    // convert speed to degrees per second
133    speed_north_deg_sec = cos( hdg / SG_RADIANS_TO_DEGREES )
134                           * speed * 1.686 / ft_per_deg_lat;
135    speed_east_deg_sec  = sin( hdg / SG_RADIANS_TO_DEGREES )
136                           * speed * 1.686 / ft_per_deg_lon;
137
138    // set new position
139    pos.setlat( pos.lat() + speed_north_deg_sec * dt);
140    pos.setlon( pos.lon() + speed_east_deg_sec * dt); 
141
142    // adjust heading based on current bank angle
143    if (roll != 0.0) {
144      turn_radius_ft = 0.088362 * speed * speed
145                        / tan( fabs(roll) / SG_RADIANS_TO_DEGREES );
146      turn_circum_ft = SGD_2PI * turn_radius_ft;
147      dist_covered_ft = speed * 1.686 * dt; 
148      alpha = dist_covered_ft / turn_circum_ft * 360.0;
149      hdg += alpha * sign( roll );
150      if ( hdg > 360.0 ) hdg -= 360.0;
151      if ( hdg < 0.0) hdg += 360.0;
152    }
153
154    // adjust target bank angle if heading lock engaged
155    if (hdg_lock) {
156      double bank_sense = 0.0;
157      double diff = fabs(hdg - tgt_heading);
158      if (diff > 180) diff = fabs(diff - 360);
159      double sum = hdg + diff;
160      if (sum > 360.0) sum -= 360.0;
161      if (fabs(sum - tgt_heading) < 1.0) { 
162        bank_sense = 1.0;
163      } else {
164        bank_sense = -1.0;
165      } 
166      if (diff < 30) tgt_roll = diff * bank_sense; 
167    }
168
169    // adjust bank angle
170    double bank_diff = tgt_roll - roll;
171    if (fabs(bank_diff) > 0.2) {
172      if (bank_diff > 0.0) roll += 5.0 * dt;
173      if (bank_diff < 0.0) roll -= 5.0 * dt;
174    }
175
176    // adjust altitude (meters) based on current vertical speed (fpm)
177    altitude += vs * 0.0166667 * dt * SG_FEET_TO_METER;  
178    double altitude_ft = altitude * SG_METER_TO_FEET;
179
180    // find target vertical speed if altitude lock engaged
181    if (alt_lock) {
182      if (altitude_ft < tgt_altitude) {
183        tgt_vs = tgt_altitude - altitude_ft;
184        if (tgt_vs > performance->climb_rate)
185          tgt_vs = performance->climb_rate;
186      } else {
187        tgt_vs = tgt_altitude - altitude_ft;
188        if (tgt_vs  < (-performance->descent_rate))
189          tgt_vs = -performance->descent_rate;
190      }
191    }
192
193    // adjust vertical speed
194    double vs_diff = tgt_vs - vs;
195    if (fabs(vs_diff) > 1.0) {
196      if (vs_diff > 0.0) {
197        vs += 400.0 * dt;
198        if (vs > tgt_vs) vs = tgt_vs;
199      } else {
200        vs -= 300.0 * dt;
201        if (vs < tgt_vs) vs = tgt_vs;
202      }
203    }   
204    
205    // match pitch angle to vertical speed
206    pitch = vs * 0.005;
207
208    //###########################//
209    // do calculations for radar //
210    //###########################//
211
212    // copy values from the AIManager
213    double user_latitude  = manager->get_user_latitude();
214    double user_longitude = manager->get_user_longitude();
215    double user_altitude  = manager->get_user_altitude();
216    double user_heading   = manager->get_user_heading();
217    double user_pitch     = manager->get_user_pitch();
218    double user_yaw       = manager->get_user_yaw();
219    double user_speed     = manager->get_user_speed();
220
221    // calculate range to target in feet and nautical miles
222    double lat_range = fabs(pos.lat() - user_latitude) * ft_per_deg_lat;
223    double lon_range = fabs(pos.lon() - user_longitude) * ft_per_deg_lon;
224    double range_ft = sqrt( lat_range*lat_range + lon_range*lon_range );
225    range = range_ft / 6076.11549;
226
227    // calculate bearing to target
228    if (pos.lat() >= user_latitude) {   
229       bearing = atan2(lat_range, lon_range) * SG_RADIANS_TO_DEGREES;
230         if (pos.lon() >= user_longitude) {
231            bearing = 90.0 - bearing;
232         } else {
233            bearing = 270.0 + bearing;
234         }
235    } else {
236       bearing = atan2(lon_range, lat_range) * SG_RADIANS_TO_DEGREES;
237         if (pos.lon() >= user_longitude) {
238            bearing = 180.0 - bearing;
239         } else {
240            bearing = 180.0 + bearing;
241         }
242    }
243
244    // calculate look left/right to target, without yaw correction
245    horiz_offset = bearing - user_heading;
246    if (horiz_offset > 180.0) horiz_offset -= 360.0;
247    if (horiz_offset < -180.0) horiz_offset += 360.0;
248
249    // calculate elevation to target
250    elevation = atan2( altitude_ft - user_altitude, range_ft )
251                       * SG_RADIANS_TO_DEGREES;
252    
253    // calculate look up/down to target
254    vert_offset = elevation + user_pitch;
255
256 /* this calculation needs to be fixed
257    // calculate range rate
258    double recip_bearing = bearing + 180.0;
259    if (recip_bearing > 360.0) recip_bearing -= 360.0;
260    double my_horiz_offset = recip_bearing - hdg;
261    if (my_horiz_offset > 180.0) my_horiz_offset -= 360.0;
262    if (my_horiz_offset < -180.0) my_horiz_offset += 360.0;
263    rdot = (-user_speed * cos( horiz_offset * SG_DEGREES_TO_RADIANS ))
264                + (-speed * 1.686 * cos( my_horiz_offset * SG_DEGREES_TO_RADIANS ));
265 */
266    
267    // now correct look left/right for yaw
268    horiz_offset += user_yaw;
269
270    // calculate values for radar display
271    y_shift = range * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
272    x_shift = range * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
273    rotation = hdg - user_heading;
274    if (rotation < 0.0) rotation += 360.0; 
275
276 }
277
278
279 void FGAIAircraft::AccelTo(double speed) {
280    tgt_speed = speed;
281 }
282
283
284 void FGAIAircraft::PitchTo(double angle) {
285    tgt_pitch = angle;
286    alt_lock = false;
287 }
288
289
290 void FGAIAircraft::RollTo(double angle) {
291    tgt_roll = angle;
292    hdg_lock = false; 
293 }
294
295
296 void FGAIAircraft::YawTo(double angle) {
297    tgt_yaw = angle;
298 }
299
300
301 void FGAIAircraft::ClimbTo(double altitude) {
302    tgt_altitude = altitude;
303    alt_lock = true;
304 }
305
306
307 void FGAIAircraft::TurnTo(double heading) {
308    tgt_heading = heading;
309    hdg_lock = true;
310 }
311
312
313 double FGAIAircraft::sign(double x) {
314
315   if ( x < 0.0 ) { return -1.0; }
316   else { return 1.0; }
317 }