]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/PropEngine.cpp
Mac OS X fixes and MSVC warning fixes from Jonathan Polley.
[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 }
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->setMixture(_mixture);
86
87     _eng->setMagnetos(3);
88     _eng->setRunning(true);
89
90     if(_variable) {
91         _omega = _minOmega + _advance * (_maxOmega - _minOmega);
92         _prop->modPitch(1e6); // Start at maximum pitch and move down
93     } else {
94         _omega = 52;
95     }
96
97     bool goingUp = false;
98     float step = 10;
99     while(true) {
100         float ptau, dummy;
101         _prop->calc(_rho, speed, _omega, &dummy, &ptau);
102         _eng->calc(_pressure, _temp, _omega);
103         float etau = _eng->getTorque();
104         float tdiff = etau - ptau;
105
106         if(Math::abs(tdiff/_moment) < 0.1)
107             break;
108
109         if(tdiff > 0) {
110             if(!goingUp) step *= 0.5f;
111             goingUp = true;
112             if(!_variable)  _omega += step;
113             else            _prop->modPitch(1+(step*0.005f));
114         } else {
115             if(goingUp) step *= 0.5f;
116             goingUp = false;
117             if(!_variable)  _omega -= step;
118             else            _prop->modPitch(1-(step*0.005f));
119         }
120     }
121
122     // ...and back off
123     _eng->setRunning(false);
124 }
125
126 void PropEngine::init()
127 {
128     _omega = 0.01f;
129     _eng->setStarter(false);
130     _eng->setMagnetos(0);
131 }
132
133 void PropEngine::integrate(float dt)
134 {
135     float speed = -Math::dot3(_wind, _dir);
136
137     float propTorque, engTorque, thrust;
138
139     _eng->setThrottle(_throttle);
140     _eng->setStarter(_starter);
141     _eng->setMagnetos(_magnetos);
142     _eng->setMixture(_mixture);
143     
144     _prop->calc(_rho, speed, _omega, &thrust, &propTorque);
145     _eng->calc(_pressure, _temp, _omega);
146     engTorque = _eng->getTorque();
147     _fuelFlow = _eng->getFuelFlow();
148
149     // Turn the thrust into a vector and save it
150     Math::mul3(thrust, _dir, _thrust);
151
152     // Euler-integrate the RPM.  This doesn't need the full-on
153     // Runge-Kutta stuff.
154     float rotacc = (engTorque-propTorque)/Math::abs(_moment);
155     _omega += dt * rotacc;
156
157     // Store the total angular momentum into _gyro
158     Math::mul3(_omega*_moment, _dir, _gyro);
159
160     // Accumulate the engine torque, it acts on the body as a whole.
161     // (Note: engine torque, not propeller torque.  They can be
162     // different, but the difference goes to accelerating the
163     // rotation.  It is the engine torque that is felt at the shaft
164     // and works on the body.)
165     float tau = _moment < 0 ? engTorque : -engTorque;
166     Math::mul3(tau, _dir, _torque);
167
168     // Iterate the propeller governor, if we have one.  Since engine
169     // torque is basically constant with RPM, we want to make the
170     // propeller torque at the target RPM equal to the engine by
171     // varying the pitch.  Assume the the torque goes as the square of
172     // the RPM (roughly correct) and compute a "target" torque for the
173     // _current_ RPM.  Seek to that.  This is sort of a continuous
174     // Newton-Raphson, basically.
175     if(_variable) {
176         float targetOmega = _minOmega + _advance*(_maxOmega-_minOmega);
177         float ratio2 = (_omega*_omega)/(targetOmega*targetOmega);
178         float targetTorque = engTorque * ratio2;
179
180         float mod = propTorque < targetTorque ? 1.04f : (1.0f/1.04f);
181
182         // Convert to an acceleration here, so that big propellers
183         // don't seek faster than small ones.
184         float diff = Math::abs((propTorque - targetTorque) / _moment);
185         if(diff < 10) mod = 1 + (mod-1)*(0.1f*diff);
186
187         _prop->modPitch(mod);
188     }
189 }
190
191 }; // namespace yasim