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::setPropPitch(float proppitch)
39 // update Propeller property
40 _prop->setPropPitch(proppitch);
43 void PropEngine::setVariableProp(float min, float max)
50 bool PropEngine::isRunning()
52 return _eng->isRunning();
55 bool PropEngine::isCranking()
57 return _eng->isCranking();
60 float PropEngine::getOmega()
65 void PropEngine::getThrust(float* out)
68 for(i=0; i<3; i++) out[i] = _thrust[i];
71 void PropEngine::getTorque(float* out)
74 for(i=0; i<3; i++) out[i] = _torque[i];
77 void PropEngine::getGyro(float* out)
80 for(i=0; i<3; i++) out[i] = _gyro[i];
83 float PropEngine::getFuelFlow()
88 void PropEngine::stabilize()
90 float speed = -Math::dot3(_wind, _dir);
91 _eng->setThrottle(_throttle);
92 _eng->setMixture(_mixture);
95 _eng->setRunning(true);
98 _omega = _minOmega + _advance * (_maxOmega - _minOmega);
99 _prop->modPitch(1e6); // Start at maximum pitch and move down
104 bool goingUp = false;
108 _prop->calc(_rho, speed, _omega, &dummy, &ptau);
109 _eng->calc(_pressure, _temp, _omega);
110 float etau = _eng->getTorque();
111 float tdiff = etau - ptau;
113 if(Math::abs(tdiff/_moment) < 0.1)
117 if(!goingUp) step *= 0.5f;
119 if(!_variable) _omega += step;
120 else _prop->modPitch(1+(step*0.005f));
122 if(goingUp) step *= 0.5f;
124 if(!_variable) _omega -= step;
125 else _prop->modPitch(1-(step*0.005f));
130 _eng->setRunning(false);
133 void PropEngine::init()
136 _eng->setStarter(false);
137 _eng->setMagnetos(0);
140 void PropEngine::integrate(float dt)
142 float speed = -Math::dot3(_wind, _dir);
144 float propTorque, engTorque, thrust;
146 _eng->setThrottle(_throttle);
147 _eng->setStarter(_starter);
148 _eng->setMagnetos(_magnetos);
149 _eng->setMixture(_mixture);
150 _eng->setFuelState(_fuel);
152 _prop->calc(_rho, speed, _omega, &thrust, &propTorque);
153 _eng->calc(_pressure, _temp, _omega);
154 engTorque = _eng->getTorque();
155 _fuelFlow = _eng->getFuelFlow();
157 // Turn the thrust into a vector and save it
158 Math::mul3(thrust, _dir, _thrust);
160 // Euler-integrate the RPM. This doesn't need the full-on
161 // Runge-Kutta stuff.
162 float rotacc = (engTorque-propTorque)/Math::abs(_moment);
163 _omega += dt * rotacc;
165 // Store the total angular momentum into _gyro
166 Math::mul3(_omega*_moment, _dir, _gyro);
168 // Accumulate the engine torque, it acts on the body as a whole.
169 // (Note: engine torque, not propeller torque. They can be
170 // different, but the difference goes to accelerating the
171 // rotation. It is the engine torque that is felt at the shaft
172 // and works on the body.)
173 float tau = _moment < 0 ? engTorque : -engTorque;
174 Math::mul3(tau, _dir, _torque);
176 // Iterate the propeller governor, if we have one. Since engine
177 // torque is basically constant with RPM, we want to make the
178 // propeller torque at the target RPM equal to the engine by
179 // varying the pitch. Assume the the torque goes as the square of
180 // the RPM (roughly correct) and compute a "target" torque for the
181 // _current_ RPM. Seek to that. This is sort of a continuous
182 // Newton-Raphson, basically.
184 float targetOmega = _minOmega + _advance*(_maxOmega-_minOmega);
185 float ratio2 = (_omega*_omega)/(targetOmega*targetOmega);
186 float targetTorque = engTorque * ratio2;
188 float mod = propTorque < targetTorque ? 1.04f : (1.0f/1.04f);
190 // Convert to an acceleration here, so that big propellers
191 // don't seek faster than small ones.
192 float diff = Math::abs((propTorque - targetTorque) / _moment);
193 if(diff < 10) mod = 1 + (mod-1)*(0.1f*diff);
195 _prop->modPitch(mod);
199 }; // namespace yasim