]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/performancedata.cxx
Fix a Clang warning, checking signed char as if it was unsigned.
[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, bool maxBrakes) {
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             double brakePower = 0;
70             if (maxBrakes) {
71                 brakePower = 3;
72             } else {
73                 brakePower = BRAKE_SETTING;
74             }
75             speed -= brakePower * _deceleration * dt;
76         } else {
77             speed -= _deceleration * dt;
78         }
79
80         if ( speed < tgt_speed )
81             speed = tgt_speed;
82
83     }
84
85     return speed;
86 }
87
88 double PerformanceData::actualBankAngle(FGAIAircraft* ac, double tgt_roll, double dt) {
89     // check maximum bank angle
90     if (fabs(tgt_roll) > _maxbank)
91         tgt_roll = _maxbank * tgt_roll/fabs(tgt_roll);
92
93     double roll = ac->getRoll();
94     double bank_diff = tgt_roll - roll;
95
96     if (fabs(bank_diff) > 0.2) {
97         if (bank_diff > 0.0) {
98             roll += _rollrate * dt;
99             if (roll > tgt_roll)
100                 roll = tgt_roll;
101         }
102         else if (bank_diff < 0.0) {
103             roll -= _rollrate * dt;
104
105             if (roll < tgt_roll)
106                 roll = tgt_roll;
107         }
108         //while (roll > 180) roll -= 360;
109         //while (roll < 180) roll += 360;
110     }
111
112     return roll;
113 }
114
115 double PerformanceData::actualPitch(FGAIAircraft* ac, double tgt_pitch, double dt) {
116     double pitch = ac->getPitch();
117     double pdiff = tgt_pitch - pitch;
118
119     if (pdiff > 0.0) { // nose up
120         pitch += 0.005*_climbRate * dt / 3.0; //TODO avoid hardcoded 3 secs
121
122         if (pitch > tgt_pitch)
123             pitch = tgt_pitch;
124     } else if (pdiff < 0.0) { // nose down
125         pitch -= 0.002*_descentRate * dt / 3.0;
126
127         if (pitch < tgt_pitch)
128             pitch = tgt_pitch;
129     }
130
131     return pitch;
132 }
133
134 double PerformanceData::actualAltitude(FGAIAircraft* ac, double tgt_altitude, double dt) {
135     if (ac->onGround()) {
136         //FIXME: a return sensible value here
137         return 0.0; // 0 for now to avoid compiler errors
138     } else
139         return ac->getAltitude() + ac->getVerticalSpeed()*dt/60.0;
140 }
141
142 double PerformanceData::actualVerticalSpeed(FGAIAircraft* ac, double tgt_vs, double dt) {
143     double vs = ac->getVerticalSpeed();
144     double vs_diff = tgt_vs - vs;
145
146     if (fabs(vs_diff) > .001) {
147         if (vs_diff > 0.0) {
148             vs += _climbRate * dt / 3.0; //TODO avoid hardcoded 3 secs to attain climb rate from level flight
149
150             if (vs > tgt_vs)
151                 vs = tgt_vs;
152         } else if (vs_diff < 0.0) {
153             vs -= _descentRate * dt / 3.0;
154
155             if (vs < tgt_vs)
156                 vs = tgt_vs;
157         }
158     }
159
160     return vs;
161 }
162
163 bool PerformanceData::gearExtensible(const FGAIAircraft* ac) {
164     return (ac->altitudeAGL() < 900.0)
165             && (ac->airspeed() < _vTouchdown * 1.25);
166 }