2 #include "Propeller.hpp"
3 #include "PistonEngine.hpp"
4 #include "PropEngine.hpp"
7 PropEngine::PropEngine(Propeller* prop, PistonEngine* eng, float moment)
9 // Start off at 500rpm, because the start code doesn't exist yet
11 _dir[0] = 1; _dir[1] = 0; _dir[2] = 0;
21 PropEngine::~PropEngine()
27 void PropEngine::setMagnetos(int pos)
32 void PropEngine::setAdvance(float advance)
34 _advance = Math::clamp(advance, 0, 1);
37 void PropEngine::setVariableProp(float min, float max)
44 bool PropEngine::isRunning()
46 return _eng->isRunning();
49 bool PropEngine::isCranking()
51 return _eng->isCranking();
54 float PropEngine::getOmega()
59 void PropEngine::getThrust(float* out)
62 for(i=0; i<3; i++) out[i] = _thrust[i];
65 void PropEngine::getTorque(float* out)
68 for(i=0; i<3; i++) out[i] = _torque[i];
71 void PropEngine::getGyro(float* out)
74 for(i=0; i<3; i++) out[i] = _gyro[i];
77 float PropEngine::getFuelFlow()
82 void PropEngine::stabilize()
84 float speed = -Math::dot3(_wind, _dir);
85 _eng->setThrottle(_throttle);
86 _eng->setMixture(_mixture);
89 _eng->setRunning(true);
92 _omega = _minOmega + _advance * (_maxOmega - _minOmega);
93 _prop->modPitch(1e6); // Start at maximum pitch and move down
102 _prop->calc(_rho, speed, _omega, &dummy, &ptau);
103 _eng->calc(_pressure, _temp, _omega);
104 float etau = _eng->getTorque();
105 float tdiff = etau - ptau;
107 if(Math::abs(tdiff/_moment) < 0.1)
111 if(!goingUp) step *= 0.5f;
113 if(!_variable) _omega += step;
114 else _prop->modPitch(1+(step*0.005f));
116 if(goingUp) step *= 0.5f;
118 if(!_variable) _omega -= step;
119 else _prop->modPitch(1-(step*0.005f));
124 _eng->setRunning(false);
127 void PropEngine::init()
130 _eng->setStarter(false);
131 _eng->setMagnetos(0);
134 void PropEngine::integrate(float dt)
136 float speed = -Math::dot3(_wind, _dir);
138 float propTorque, engTorque, thrust;
140 _eng->setThrottle(_throttle);
141 _eng->setStarter(_starter);
142 _eng->setMagnetos(_magnetos);
143 _eng->setMixture(_mixture);
144 _eng->setFuelState(_fuel);
146 _prop->calc(_rho, speed, _omega, &thrust, &propTorque);
147 _eng->calc(_pressure, _temp, _omega);
148 engTorque = _eng->getTorque();
149 _fuelFlow = _eng->getFuelFlow();
151 // Turn the thrust into a vector and save it
152 Math::mul3(thrust, _dir, _thrust);
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;
159 // Store the total angular momentum into _gyro
160 Math::mul3(_omega*_moment, _dir, _gyro);
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);
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.
178 float targetOmega = _minOmega + _advance*(_maxOmega-_minOmega);
179 float ratio2 = (_omega*_omega)/(targetOmega*targetOmega);
180 float targetTorque = engTorque * ratio2;
182 float mod = propTorque < targetTorque ? 1.04f : (1.0f/1.04f);
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);
189 _prop->modPitch(mod);
193 }; // namespace yasim