]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/PropEngine.cpp
Updated to YASim-0.1.2
[flightgear.git] / src / FDM / YASim / PropEngine.cpp
1 #include "Math.hpp"
2 #include "Propeller.hpp"
3 #include "PistonEngine.hpp"
4 #include "PropEngine.hpp"
5 namespace yasim {
6
7 PropEngine::PropEngine(Propeller* prop, PistonEngine* eng, float moment)
8 {
9     // Start off at 500rpm, because the start code doesn't exist yet
10     _omega = 52.3;
11     _dir[0] = 1; _dir[1] = 0; _dir[2] = 0;
12
13     _variable = false;
14
15     _prop = prop;
16     _eng = eng;
17     _moment = moment;
18 }
19
20 PropEngine::~PropEngine()
21 {
22     delete _prop;
23     delete _eng;
24 }
25
26 void PropEngine::setAdvance(float advance)
27 {
28     _advance = Math::clamp(advance, 0, 1);
29 }
30
31 void PropEngine::setVariableProp(float min, float max)
32 {
33     _variable = true;
34     _minOmega = min;
35     _maxOmega = max;
36 }
37
38 float PropEngine::getOmega()
39 {
40     return _omega;
41 }
42
43 void PropEngine::getThrust(float* out)
44 {
45     int i;
46     for(i=0; i<3; i++) out[i] = _thrust[i];    
47 }
48
49 void PropEngine::getTorque(float* out)
50 {
51     int i;
52     for(i=0; i<3; i++) out[i] = _torque[i];
53 }
54
55 void PropEngine::getGyro(float* out)
56 {
57     int i;
58     for(i=0; i<3; i++) out[i] = _gyro[i];
59 }
60
61 float PropEngine::getFuelFlow()
62 {
63     return _fuelFlow;
64 }
65
66 void PropEngine::stabilize()
67 {
68     float speed = -Math::dot3(_wind, _dir);
69     _eng->setThrottle(_throttle);
70     _eng->setMixture(_mixture);
71
72     if(_variable) {
73         _omega = _minOmega + _advance * (_maxOmega - _minOmega);
74         _prop->modPitch(1e6); // Start at maximum pitch and move down
75     } else {
76         _omega = 52;
77     }
78
79     bool goingUp = false;
80     float step = 10;
81     while(true) {
82         float etau, ptau, dummy;
83         _prop->calc(_rho, speed, _omega, &dummy, &ptau);
84         _eng->calc(_pressure, _temp, _omega, &etau, &dummy);
85         float tdiff = etau - ptau;
86
87         if(Math::abs(tdiff/_moment) < 0.1)
88             break;
89
90         if(tdiff > 0) {
91             if(!goingUp) step *= 0.5;
92             goingUp = true;
93             if(!_variable)  _omega += step;
94             else            _prop->modPitch(1+(step*0.005));
95         } else {
96             if(goingUp) step *= 0.5;
97             goingUp = false;
98             if(!_variable)  _omega -= step;
99             else            _prop->modPitch(1-(step*0.005));
100         }
101     }
102 }
103
104 void PropEngine::integrate(float dt)
105 {
106     float speed = -Math::dot3(_wind, _dir);
107
108     float propTorque, engTorque, thrust;
109
110     _eng->setThrottle(_throttle);
111     _eng->setMixture(_mixture);
112     
113     _prop->calc(_rho, speed, _omega,
114                 &thrust, &propTorque);
115     _eng->calc(_pressure, _temp, _omega, &engTorque, &_fuelFlow);
116
117     // Turn the thrust into a vector and save it
118     Math::mul3(thrust, _dir, _thrust);
119
120     // Euler-integrate the RPM.  This doesn't need the full-on
121     // Runge-Kutta stuff.
122     float rotacc = (engTorque-propTorque)/Math::abs(_moment);
123     _omega += dt * rotacc;
124
125     // Clamp to a 500 rpm idle.  This should probably be settable, and
126     // needs to go away when the startup code gets written.
127     if(_omega < 52.3) _omega = 52.3;
128
129     // FIXME: Integrate the propeller governor here, when that gets
130     // implemented...
131
132     // Store the total angular momentum into _gyro
133     Math::mul3(_omega*_moment, _dir, _gyro);
134
135     // Accumulate the engine torque, it acts on the body as a whole.
136     // (Note: engine torque, not propeller torque.  They can be
137     // different, but the difference goes to accelerating the
138     // rotation.  It is the engine torque that is felt at the shaft
139     // and works on the body.)
140     float tau = _moment < 0 ? engTorque : -engTorque;
141     Math::mul3(tau, _dir, _torque);
142
143     // Play with the variable propeller, but only if the propeller is
144     // vaguely stable alread (accelerating less than 100 rpm/s)
145     if(_variable && Math::abs(rotacc) < 20) {
146         float target = _minOmega + _advance*(_maxOmega-_minOmega);
147         float mod = 1.04;
148         if(target > _omega) mod = 1/mod;
149         float diff = Math::abs(target - _omega);
150         if(diff < 1) mod = 1 + (mod-1)*diff;
151         if(thrust < 0) mod = 1;
152         _prop->modPitch(mod);
153     }
154 }
155
156 }; // namespace yasim