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