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::setAdvance(float advance)
28 _advance = Math::clamp(advance, 0, 1);
31 void PropEngine::setVariableProp(float min, float max)
38 float PropEngine::getOmega()
43 void PropEngine::getThrust(float* out)
46 for(i=0; i<3; i++) out[i] = _thrust[i];
49 void PropEngine::getTorque(float* out)
52 for(i=0; i<3; i++) out[i] = _torque[i];
55 void PropEngine::getGyro(float* out)
58 for(i=0; i<3; i++) out[i] = _gyro[i];
61 float PropEngine::getFuelFlow()
66 void PropEngine::stabilize()
68 float speed = -Math::dot3(_wind, _dir);
69 _eng->setThrottle(_throttle);
70 _eng->setMixture(_mixture);
73 _omega = _minOmega + _advance * (_maxOmega - _minOmega);
74 _prop->modPitch(1e6); // Start at maximum pitch and move down
83 _prop->calc(_rho, speed, _omega, &dummy, &ptau);
84 _eng->calc(_pressure, _temp, _omega);
85 float etau = _eng->getTorque();
86 float tdiff = etau - ptau;
88 if(Math::abs(tdiff/_moment) < 0.1)
92 if(!goingUp) step *= 0.5;
94 if(!_variable) _omega += step;
95 else _prop->modPitch(1+(step*0.005));
97 if(goingUp) step *= 0.5;
99 if(!_variable) _omega -= step;
100 else _prop->modPitch(1-(step*0.005));
105 void PropEngine::integrate(float dt)
107 float speed = -Math::dot3(_wind, _dir);
109 float propTorque, engTorque, thrust;
111 _eng->setThrottle(_throttle);
112 _eng->setMixture(_mixture);
114 _prop->calc(_rho, speed, _omega, &thrust, &propTorque);
115 _eng->calc(_pressure, _temp, _omega);
116 engTorque = _eng->getTorque();
117 _fuelFlow = _eng->getFuelFlow();
119 // Turn the thrust into a vector and save it
120 Math::mul3(thrust, _dir, _thrust);
122 // Euler-integrate the RPM. This doesn't need the full-on
123 // Runge-Kutta stuff.
124 float rotacc = (engTorque-propTorque)/Math::abs(_moment);
125 _omega += dt * rotacc;
127 // Clamp to a 500 rpm idle. This should probably be settable, and
128 // needs to go away when the startup code gets written.
129 if(_omega < 52.3) _omega = 52.3;
131 // Store the total angular momentum into _gyro
132 Math::mul3(_omega*_moment, _dir, _gyro);
134 // Accumulate the engine torque, it acts on the body as a whole.
135 // (Note: engine torque, not propeller torque. They can be
136 // different, but the difference goes to accelerating the
137 // rotation. It is the engine torque that is felt at the shaft
138 // and works on the body.)
139 float tau = _moment < 0 ? engTorque : -engTorque;
140 Math::mul3(tau, _dir, _torque);
142 // Iterate the propeller governor, if we have one. Since engine
143 // torque is basically constant with RPM, we want to make the
144 // propeller torque at the target RPM equal to the engine by
145 // varying the pitch. Assume the the torque goes as the square of
146 // the RPM (roughly correct) and compute a "target" torque for the
147 // _current_ RPM. Seek to that. This is sort of a continuous
148 // Newton-Raphson, basically.
150 float targetOmega = _minOmega + _advance*(_maxOmega-_minOmega);
151 float ratio2 = (_omega*_omega)/(targetOmega*targetOmega);
152 float targetTorque = engTorque * ratio2;
154 float mod = propTorque < targetTorque ? 1.04 : (1/1.04);
156 // Convert to an acceleration here, so that big propellers
157 // don't seek faster than small ones.
158 float diff = Math::abs((propTorque - targetTorque) / _moment);
159 if(diff < 10) mod = 1 + (mod-1)*(0.1*diff);
161 _prop->modPitch(mod);
165 }; // namespace yasim