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 // FIXME: Integrate the propeller governor here, when that gets
132 // Store the total angular momentum into _gyro
133 Math::mul3(_omega*_moment, _dir, _gyro);
135 // Accumulate the engine torque, it acts on the body as a whole.
136 // (Note: engine torque, not propeller torque. They can be
137 // different, but the difference goes to accelerating the
138 // rotation. It is the engine torque that is felt at the shaft
139 // and works on the body.)
140 float tau = _moment < 0 ? engTorque : -engTorque;
141 Math::mul3(tau, _dir, _torque);
143 // Play with the variable propeller, but only if the propeller is
144 // vaguely stable alread (accelerating less than 100 rpm/s)
145 if(_variable && Math::abs(rotacc) < 20) {
146 float target = _minOmega + _advance*(_maxOmega-_minOmega);
148 if(target > _omega) mod = 1/mod;
149 float diff = Math::abs(target - _omega);
150 if(diff < 1) mod = 1 + (mod-1)*diff;
151 if(thrust < 0) mod = 1;
152 _prop->modPitch(mod);
156 }; // namespace yasim