]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/performancedata.cxx
f07ad6132d2724fd7a86e4a343c6a1d7976327b8
[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     } else if (pdiff < 0.0) { // nose down
112         pitch -= 0.002*_descentRate * dt / 3.0;
113
114         if (pitch < tgt_pitch)
115             pitch = tgt_pitch;
116     }
117
118     return pitch;
119 }
120
121 double PerformanceData::actualAltitude(FGAIAircraft* ac, double tgt_altitude, double dt) {
122     if (ac->onGround()) {
123         //FIXME: return a value here
124     } else
125         return ac->getAltitude() + ac->getVerticalSpeed()*dt/60.0;
126 }
127
128 double PerformanceData::actualVerticalSpeed(FGAIAircraft* ac, double tgt_vs, double dt) {
129     double vs = ac->getVerticalSpeed();
130     double vs_diff = tgt_vs - vs;
131
132     if (fabs(vs_diff) > 10.0) {
133         if (vs_diff > 0.0) {
134             vs += _climbRate * dt / 3.0; //TODO avoid hardcoded 3 secs to attain climb rate from level flight
135
136             if (vs > tgt_vs)
137                 vs = tgt_vs;
138         } else if (vs_diff < 0.0) {
139             vs -= _descentRate * dt / 3.0;
140
141             if (vs < tgt_vs)
142                 vs = tgt_vs;
143         }
144     }
145
146     return vs;
147 }
148
149 bool PerformanceData::gearExtensible(const FGAIAircraft* ac) {
150     return (ac->altitudeAGL() < 900.0)
151             && (ac->airspeed() < _vTouchdown * 1.25);
152 }