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
82 float etau, ptau, dummy;
83 _prop->calc(_rho, speed, _omega, &dummy, &ptau);
84 _eng->calc(_pressure, _temp, _omega, &etau, &dummy);
85 float tdiff = etau - ptau;
87 if(Math::abs(tdiff/_moment) < 0.1)
91 if(!goingUp) step *= 0.5;
93 if(!_variable) _omega += step;
94 else _prop->modPitch(1+(step*0.005));
96 if(goingUp) step *= 0.5;
98 if(!_variable) _omega -= step;
99 else _prop->modPitch(1-(step*0.005));
104 void PropEngine::integrate(float dt)
106 float speed = -Math::dot3(_wind, _dir);
108 float propTorque, engTorque, thrust;
110 _eng->setThrottle(_throttle);
111 _eng->setMixture(_mixture);
113 _prop->calc(_rho, speed, _omega,
114 &thrust, &propTorque);
115 _eng->calc(_pressure, _temp, _omega, &engTorque, &_fuelFlow);
117 // Turn the thrust into a vector and save it
118 Math::mul3(thrust, _dir, _thrust);
120 // Euler-integrate the RPM. This doesn't need the full-on
121 // Runge-Kutta stuff.
122 float rotacc = (engTorque-propTorque)/Math::abs(_moment);
123 _omega += dt * rotacc;
125 // Clamp to a 500 rpm idle. This should probably be settable, and
126 // needs to go away when the startup code gets written.
127 if(_omega < 52.3) _omega = 52.3;
129 // Store the total angular momentum into _gyro
130 Math::mul3(_omega*_moment, _dir, _gyro);
132 // Accumulate the engine torque, it acts on the body as a whole.
133 // (Note: engine torque, not propeller torque. They can be
134 // different, but the difference goes to accelerating the
135 // rotation. It is the engine torque that is felt at the shaft
136 // and works on the body.)
137 float tau = _moment < 0 ? engTorque : -engTorque;
138 Math::mul3(tau, _dir, _torque);
140 // Iterate the propeller governor, if we have one. Since engine
141 // torque is basically constant with RPM, we want to make the
142 // propeller torque at the target RPM equal to the engine by
143 // varying the pitch. Assume the the torque goes as the square of
144 // the RPM (roughly correct) and compute a "target" torque for the
145 // _current_ RPM. Seek to that. This is sort of a continuous
146 // Newton-Raphson, basically.
148 float targetOmega = _minOmega + _advance*(_maxOmega-_minOmega);
149 float ratio2 = (_omega*_omega)/(targetOmega*targetOmega);
150 float targetTorque = engTorque * ratio2;
152 float mod = propTorque < targetTorque ? 1.04 : (1/1.04);
154 // Convert to an acceleration here, so that big propellers
155 // don't seek faster than small ones.
156 float diff = Math::abs(propTorque - targetTorque) / _moment;
157 if(diff < 10) mod = 1 + (mod-1)*(0.1*diff);
159 _prop->modPitch(mod);
163 }; // namespace yasim