]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/performancedata.cxx
d3c32dd607aa55eeaa8e21cd21b095d7a87b2912
[flightgear.git] / src / AIModel / performancedata.cxx
1
2 #ifdef HAVE_CONFIG_H
3 #  include "config.h"
4 #endif
5
6 #include "performancedata.hxx"
7 #include "AIAircraft.hxx"
8
9 PerformanceData::PerformanceData(double acceleration,
10                                  double deceleration,
11                                  double climbRate,
12                                  double descentRate,
13                                  double vRotate,
14                                  double vTakeOff,
15                                  double vClimb,
16                                  double vCruise,
17                                  double vDescent,
18                                  double vApproach,
19                                  double vTouchdown,
20                                  double vTaxi) :
21     _acceleration(acceleration),
22     _deceleration(deceleration),
23     _climbRate(climbRate),
24     _descentRate(descentRate),
25     _vRotate(vRotate),
26     _vTakeOff(vTakeOff),
27     _vClimb(vClimb),
28     _vCruise(vCruise),
29     _vDescent(vDescent),
30     _vApproach(vApproach),
31     _vTouchdown(vTouchdown),
32     _vTaxi(vTaxi)
33 {
34     _rollrate = 9.0; // degrees per second
35     _maxbank = 30.0; // passenger friendly bank angle
36 }
37
38 // read perf data from file
39 PerformanceData::PerformanceData( const std::string& filename)
40 {}
41
42 PerformanceData::~PerformanceData()
43 {}
44
45 double PerformanceData::actualSpeed(FGAIAircraft* ac, double tgt_speed, double dt) {
46     // if (tgt_speed > _vTaxi & ac->onGround()) // maximum taxi speed on ground
47     //    tgt_speed = _vTaxi;
48     // bad idea for a take off roll :-)
49     
50     double speed = ac->getSpeed();
51     double speed_diff = tgt_speed - speed;
52
53     if (speed_diff > 0.0)        // need to accelerate
54     {
55         speed += _acceleration * dt;
56         if ( speed > tgt_speed )
57             speed = tgt_speed;
58
59     } else if (speed_diff < 0.0) { // decelerate
60         if (ac->onGround()) {
61             // deceleration performance is better due to wheel brakes.
62             speed -= 3 * _deceleration * dt;
63         } else {
64             speed -= _deceleration * dt;
65         }
66
67         if ( speed < tgt_speed )
68             speed = tgt_speed;
69
70     }
71
72     return speed;
73 }
74
75 double PerformanceData::actualBankAngle(FGAIAircraft* ac, double tgt_roll, double dt) {
76     // check maximum bank angle
77     if (fabs(tgt_roll) > _maxbank)
78         tgt_roll = _maxbank * tgt_roll/fabs(tgt_roll);
79     
80     double roll = ac->getRoll();
81     double bank_diff = tgt_roll - roll;
82     
83     if (fabs(bank_diff) > 0.2) {
84         if (bank_diff > 0.0) {
85             roll += _rollrate * dt;
86             if (roll > tgt_roll)
87                 roll = tgt_roll;
88         }
89         else if (bank_diff < 0.0) {
90             roll -= _rollrate * dt;
91
92             if (roll < tgt_roll)
93                 roll = tgt_roll;
94         }
95         //while (roll > 180) roll -= 360;
96         //while (roll < 180) roll += 360;
97     }
98
99     return roll;
100 }
101
102 double PerformanceData::actualPitch(FGAIAircraft* ac, double tgt_pitch, double dt) {
103     double pitch = ac->getPitch();
104     double pdiff = tgt_pitch - pitch;
105
106     if (pdiff > 0.0) { // nose up
107         pitch += 0.005*_climbRate * dt / 3.0; //TODO avoid hardcoded 3 secs
108
109         if (pitch > tgt_pitch)
110             pitch = tgt_pitch;
111             
112     } else if (pdiff < 0.0) { // nose down
113         pitch -= 0.002*_descentRate * dt / 3.0;
114
115         if (pitch < tgt_pitch)
116             pitch = tgt_pitch;
117     }
118
119     return pitch;
120 }
121
122 double PerformanceData::actualAltitude(FGAIAircraft* ac, double tgt_altitude, double dt) {
123     if (ac->onGround()) {
124         //FIXME: return a value here
125     } else
126         return ac->getAltitude() + ac->getVerticalSpeed()*dt/60.0;
127 }
128
129 double PerformanceData::actualVerticalSpeed(FGAIAircraft* ac, double tgt_vs, double dt) {
130     double vs = ac->getVerticalSpeed();
131     double vs_diff = tgt_vs - vs;
132     
133     if (fabs(vs_diff) > 10.0) {
134         if (vs_diff > 0.0) {
135             vs += _climbRate * dt / 3.0; //TODO avoid hardcoded 3 secs to attain climb rate from level flight
136
137             if (vs > tgt_vs)
138                 vs = tgt_vs;
139             
140         } else if (vs_diff < 0.0) {
141             vs -= _descentRate * dt / 3.0;
142
143             if (vs < tgt_vs)
144                 vs = tgt_vs;
145         }
146     }
147
148     return vs;
149 }
150
151 bool PerformanceData::gearExtensible(const FGAIAircraft* ac) {
152     return (ac->altitudeAGL() < 900.0)
153             && (ac->airspeed() < _vTouchdown * 1.25);
154 }