]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIAircraft.cxx
ignore resets for now because every z/Z key press would trigger a call to NOAA. We...
[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() {
55    _self = this;
56
57    // set heading and altitude locks
58    hdg_lock = false;
59    alt_lock = false;
60    _type_str = "aircraft";
61 }
62
63
64 FGAIAircraft::~FGAIAircraft() {
65     _self = NULL;
66 }
67
68
69 bool FGAIAircraft::init() {
70    return FGAIBase::init();
71 }
72
73 void FGAIAircraft::bind() {
74     FGAIBase::bind();
75
76     props->tie("controls/gear/gear-down",
77                SGRawValueFunctions<bool>(FGAIAircraft::_getGearDown));
78
79 /*
80     props->getNode("controls/lighting/landing-lights", true)
81            ->alias("controls/gear/gear-down");
82 */
83 }
84
85 void FGAIAircraft::unbind() {
86     FGAIBase::unbind();
87
88     props->untie("controls/gear/gear-down");
89 //    props->getNode("controls/lighting/landing-lights")->unalias();
90 }
91
92
93 void FGAIAircraft::update(double dt) {
94
95    Run(dt);
96    Transform();
97    FGAIBase::update(dt);
98 }
99
100 void FGAIAircraft::SetPerformance(const PERF_STRUCT *ps) {
101    
102    performance = ps;
103
104
105
106 void FGAIAircraft::Run(double dt) {
107
108    FGAIAircraft::dt = dt;
109         
110    double turn_radius_ft;
111    double turn_circum_ft;
112    double speed_north_deg_sec;
113    double speed_east_deg_sec;
114    double ft_per_deg_lon;
115    double ft_per_deg_lat;
116    double dist_covered_ft;
117    double alpha;
118
119    // get size of a degree at this latitude
120    ft_per_deg_lat = 366468.96 - 3717.12 * cos(pos.lat()/SG_RADIANS_TO_DEGREES);
121    ft_per_deg_lon = 365228.16 * cos(pos.lat() / SG_RADIANS_TO_DEGREES);
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) speed -= performance->decel * dt;
128    } 
129    
130    // convert speed to degrees per second
131    speed_north_deg_sec = cos( hdg / SG_RADIANS_TO_DEGREES )
132                           * speed * 1.686 / ft_per_deg_lat;
133    speed_east_deg_sec  = sin( hdg / SG_RADIANS_TO_DEGREES )
134                           * speed * 1.686 / ft_per_deg_lon;
135
136    // set new position
137    pos.setlat( pos.lat() + speed_north_deg_sec * dt);
138    pos.setlon( pos.lon() + speed_east_deg_sec * dt); 
139
140    // adjust heading based on current bank angle
141    if (roll != 0.0) {
142      turn_radius_ft = 0.088362 * speed * speed
143                        / tan( fabs(roll) / SG_RADIANS_TO_DEGREES );
144      turn_circum_ft = SGD_2PI * turn_radius_ft;
145      dist_covered_ft = speed * 1.686 * dt; 
146      alpha = dist_covered_ft / turn_circum_ft * 360.0;
147      hdg += alpha * sign( roll );
148      if ( hdg > 360.0 ) hdg -= 360.0;
149      if ( hdg < 0.0) hdg += 360.0;
150    }
151
152    // adjust target bank angle if heading lock engaged
153    if (hdg_lock) {
154      double bank_sense = 0.0;
155      double diff = fabs(hdg - tgt_heading);
156      if (diff > 180) diff = fabs(diff - 360);
157      double sum = hdg + diff;
158      if (sum > 360.0) sum -= 360.0;
159      if (fabs(sum - tgt_heading) < 1.0) { 
160        bank_sense = 1.0;
161      } else {
162        bank_sense = -1.0;
163      } 
164      if (diff < 30) tgt_roll = diff * bank_sense; 
165    }
166
167    // adjust bank angle
168    double bank_diff = tgt_roll - roll;
169    if (fabs(bank_diff) > 0.2) {
170      if (bank_diff > 0.0) roll += 5.0 * dt;
171      if (bank_diff < 0.0) roll -= 5.0 * dt;
172    }
173
174    // adjust altitude (meters) based on current vertical speed (fpm)
175    altitude += vs * 0.0166667 * dt * SG_FEET_TO_METER;  
176
177    // find target vertical speed if altitude lock engaged
178    if (alt_lock) {
179      double altitude_ft = altitude * SG_METER_TO_FEET;
180      if (altitude_ft < tgt_altitude) {
181        tgt_vs = tgt_altitude - altitude_ft;
182        if (tgt_vs > performance->climb_rate)
183          tgt_vs = performance->climb_rate;
184      } else {
185        tgt_vs = tgt_altitude - altitude_ft;
186        if (tgt_vs  < (-performance->descent_rate))
187          tgt_vs = -performance->descent_rate;
188      }
189    }
190
191    // adjust vertical speed
192    double vs_diff = tgt_vs - vs;
193    if (fabs(vs_diff) > 1.0) {
194      if (vs_diff > 0.0) {
195        vs += 400.0 * dt;
196        if (vs > tgt_vs) vs = tgt_vs;
197      } else {
198        vs -= 300.0 * dt;
199        if (vs < tgt_vs) vs = tgt_vs;
200      }
201    }   
202    
203    // match pitch angle to vertical speed
204    pitch = vs * 0.005;
205
206 }
207
208
209 void FGAIAircraft::AccelTo(double speed) {
210    tgt_speed = speed;
211 }
212
213
214 void FGAIAircraft::PitchTo(double angle) {
215    tgt_pitch = angle;
216    alt_lock = false;
217 }
218
219
220 void FGAIAircraft::RollTo(double angle) {
221    tgt_roll = angle;
222    hdg_lock = false; 
223 }
224
225
226 void FGAIAircraft::YawTo(double angle) {
227    tgt_yaw = angle;
228 }
229
230
231 void FGAIAircraft::ClimbTo(double altitude) {
232    tgt_altitude = altitude;
233    alt_lock = true;
234 }
235
236
237 void FGAIAircraft::TurnTo(double heading) {
238    tgt_heading = heading;
239    hdg_lock = true;
240 }
241
242
243 double FGAIAircraft::sign(double x) {
244
245   if ( x < 0.0 ) { return -1.0; }
246   else { return 1.0; }
247 }