]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/PropEngine.cpp
Don't fiddle with control positions at startup -- we can do that in
[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.3f;
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     _fuel = true;
19 }
20
21 PropEngine::~PropEngine()
22 {
23     delete _prop;
24     delete _eng;
25 }
26
27 void PropEngine::setMagnetos(int pos)
28 {
29     _magnetos = pos;
30 }
31
32 void PropEngine::setAdvance(float advance)
33 {
34     _advance = Math::clamp(advance, 0, 1);
35 }
36
37 void PropEngine::setVariableProp(float min, float max)
38 {
39     _variable = true;
40     _minOmega = min;
41     _maxOmega = max;
42 }
43
44 bool PropEngine::isRunning()
45 {
46     return _eng->isRunning();
47 }
48
49 bool PropEngine::isCranking()
50 {
51     return _eng->isCranking();
52 }
53
54 float PropEngine::getOmega()
55 {
56     return _omega;
57 }
58
59 void PropEngine::getThrust(float* out)
60 {
61     int i;
62     for(i=0; i<3; i++) out[i] = _thrust[i];    
63 }
64
65 void PropEngine::getTorque(float* out)
66 {
67     int i;
68     for(i=0; i<3; i++) out[i] = _torque[i];
69 }
70
71 void PropEngine::getGyro(float* out)
72 {
73     int i;
74     for(i=0; i<3; i++) out[i] = _gyro[i];
75 }
76
77 float PropEngine::getFuelFlow()
78 {
79     return _fuelFlow;
80 }
81
82 void PropEngine::stabilize()
83 {
84     float speed = -Math::dot3(_wind, _dir);
85     _eng->setThrottle(_throttle);
86     _eng->setMixture(_mixture);
87
88     _eng->setMagnetos(3);
89     _eng->setRunning(true);
90
91     if(_variable) {
92         _omega = _minOmega + _advance * (_maxOmega - _minOmega);
93         _prop->modPitch(1e6); // Start at maximum pitch and move down
94     } else {
95         _omega = 52;
96     }
97
98     bool goingUp = false;
99     float step = 10;
100     while(true) {
101         float ptau, dummy;
102         _prop->calc(_rho, speed, _omega, &dummy, &ptau);
103         _eng->calc(_pressure, _temp, _omega);
104         float etau = _eng->getTorque();
105         float tdiff = etau - ptau;
106
107         if(Math::abs(tdiff/_moment) < 0.1)
108             break;
109
110         if(tdiff > 0) {
111             if(!goingUp) step *= 0.5f;
112             goingUp = true;
113             if(!_variable)  _omega += step;
114             else            _prop->modPitch(1+(step*0.005f));
115         } else {
116             if(goingUp) step *= 0.5f;
117             goingUp = false;
118             if(!_variable)  _omega -= step;
119             else            _prop->modPitch(1-(step*0.005f));
120         }
121     }
122
123     // ...and back off
124     _eng->setRunning(false);
125 }
126
127 void PropEngine::init()
128 {
129     _omega = 0.01f;
130     _eng->setStarter(false);
131     _eng->setMagnetos(0);
132 }
133
134 void PropEngine::integrate(float dt)
135 {
136     float speed = -Math::dot3(_wind, _dir);
137
138     float propTorque, engTorque, thrust;
139
140     _eng->setThrottle(_throttle);
141     _eng->setStarter(_starter);
142     _eng->setMagnetos(_magnetos);
143     _eng->setMixture(_mixture);
144     _eng->setFuelState(_fuel);
145     
146     _prop->calc(_rho, speed, _omega, &thrust, &propTorque);
147     _eng->calc(_pressure, _temp, _omega);
148     engTorque = _eng->getTorque();
149     _fuelFlow = _eng->getFuelFlow();
150
151     // Turn the thrust into a vector and save it
152     Math::mul3(thrust, _dir, _thrust);
153
154     // Euler-integrate the RPM.  This doesn't need the full-on
155     // Runge-Kutta stuff.
156     float rotacc = (engTorque-propTorque)/Math::abs(_moment);
157     _omega += dt * rotacc;
158
159     // Store the total angular momentum into _gyro
160     Math::mul3(_omega*_moment, _dir, _gyro);
161
162     // Accumulate the engine torque, it acts on the body as a whole.
163     // (Note: engine torque, not propeller torque.  They can be
164     // different, but the difference goes to accelerating the
165     // rotation.  It is the engine torque that is felt at the shaft
166     // and works on the body.)
167     float tau = _moment < 0 ? engTorque : -engTorque;
168     Math::mul3(tau, _dir, _torque);
169
170     // Iterate the propeller governor, if we have one.  Since engine
171     // torque is basically constant with RPM, we want to make the
172     // propeller torque at the target RPM equal to the engine by
173     // varying the pitch.  Assume the the torque goes as the square of
174     // the RPM (roughly correct) and compute a "target" torque for the
175     // _current_ RPM.  Seek to that.  This is sort of a continuous
176     // Newton-Raphson, basically.
177     if(_variable) {
178         float targetOmega = _minOmega + _advance*(_maxOmega-_minOmega);
179         float ratio2 = (_omega*_omega)/(targetOmega*targetOmega);
180         float targetTorque = engTorque * ratio2;
181
182         float mod = propTorque < targetTorque ? 1.04f : (1.0f/1.04f);
183
184         // Convert to an acceleration here, so that big propellers
185         // don't seek faster than small ones.
186         float diff = Math::abs((propTorque - targetTorque) / _moment);
187         if(diff < 10) mod = 1 + (mod-1)*(0.1f*diff);
188
189         _prop->modPitch(mod);
190     }
191 }
192
193 }; // namespace yasim