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;
20 PropEngine::~PropEngine()
26 void PropEngine::setMagnetos(int pos)
31 void PropEngine::setAdvance(float advance)
33 _advance = Math::clamp(advance, 0, 1);
36 void PropEngine::setVariableProp(float min, float max)
43 bool PropEngine::isRunning()
45 return _eng->isRunning();
48 bool PropEngine::isCranking()
50 return _eng->isCranking();
53 float PropEngine::getOmega()
58 void PropEngine::getThrust(float* out)
61 for(i=0; i<3; i++) out[i] = _thrust[i];
64 void PropEngine::getTorque(float* out)
67 for(i=0; i<3; i++) out[i] = _torque[i];
70 void PropEngine::getGyro(float* out)
73 for(i=0; i<3; i++) out[i] = _gyro[i];
76 float PropEngine::getFuelFlow()
81 void PropEngine::stabilize()
83 float speed = -Math::dot3(_wind, _dir);
84 _eng->setThrottle(_throttle);
85 _eng->setMixture(_mixture);
88 _eng->setRunning(true);
91 _omega = _minOmega + _advance * (_maxOmega - _minOmega);
92 _prop->modPitch(1e6); // Start at maximum pitch and move down
101 _prop->calc(_rho, speed, _omega, &dummy, &ptau);
102 _eng->calc(_pressure, _temp, _omega);
103 float etau = _eng->getTorque();
104 float tdiff = etau - ptau;
106 if(Math::abs(tdiff/_moment) < 0.1)
110 if(!goingUp) step *= 0.5;
112 if(!_variable) _omega += step;
113 else _prop->modPitch(1+(step*0.005));
115 if(goingUp) step *= 0.5;
117 if(!_variable) _omega -= step;
118 else _prop->modPitch(1-(step*0.005));
123 _eng->setRunning(false);
126 void PropEngine::init()
129 _eng->setStarter(false);
130 _eng->setMagnetos(0);
133 void PropEngine::integrate(float dt)
135 float speed = -Math::dot3(_wind, _dir);
137 float propTorque, engTorque, thrust;
139 _eng->setThrottle(_throttle);
140 _eng->setStarter(_starter);
141 _eng->setMagnetos(_magnetos);
142 _eng->setMixture(_mixture);
144 _prop->calc(_rho, speed, _omega, &thrust, &propTorque);
145 _eng->calc(_pressure, _temp, _omega);
146 engTorque = _eng->getTorque();
147 _fuelFlow = _eng->getFuelFlow();
149 // Turn the thrust into a vector and save it
150 Math::mul3(thrust, _dir, _thrust);
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;
157 // Store the total angular momentum into _gyro
158 Math::mul3(_omega*_moment, _dir, _gyro);
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);
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.
176 float targetOmega = _minOmega + _advance*(_maxOmega-_minOmega);
177 float ratio2 = (_omega*_omega)/(targetOmega*targetOmega);
178 float targetTorque = engTorque * ratio2;
180 float mod = propTorque < targetTorque ? 1.04 : (1/1.04);
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.1*diff);
187 _prop->modPitch(mod);
191 }; // namespace yasim