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