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