]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/PropEngine.cpp
YASim-0.1.3 updates.
[flightgear.git] / src / FDM / YASim / PropEngine.cpp
1 #include "Math.hpp"
2 #include "Propeller.hpp"
3 #include "PistonEngine.hpp"
4 #include "PropEngine.hpp"
5 namespace yasim {
6
7 PropEngine::PropEngine(Propeller* prop, PistonEngine* eng, float moment)
8 {
9     // Start off at 500rpm, because the start code doesn't exist yet
10     _omega = 52.3;
11     _dir[0] = 1; _dir[1] = 0; _dir[2] = 0;
12
13     _variable = false;
14
15     _prop = prop;
16     _eng = eng;
17     _moment = moment;
18 }
19
20 PropEngine::~PropEngine()
21 {
22     delete _prop;
23     delete _eng;
24 }
25
26 void PropEngine::setAdvance(float advance)
27 {
28     _advance = Math::clamp(advance, 0, 1);
29 }
30
31 void PropEngine::setVariableProp(float min, float max)
32 {
33     _variable = true;
34     _minOmega = min;
35     _maxOmega = max;
36 }
37
38 float PropEngine::getOmega()
39 {
40     return _omega;
41 }
42
43 void PropEngine::getThrust(float* out)
44 {
45     int i;
46     for(i=0; i<3; i++) out[i] = _thrust[i];    
47 }
48
49 void PropEngine::getTorque(float* out)
50 {
51     int i;
52     for(i=0; i<3; i++) out[i] = _torque[i];
53 }
54
55 void PropEngine::getGyro(float* out)
56 {
57     int i;
58     for(i=0; i<3; i++) out[i] = _gyro[i];
59 }
60
61 float PropEngine::getFuelFlow()
62 {
63     return _fuelFlow;
64 }
65
66 void PropEngine::stabilize()
67 {
68     float speed = -Math::dot3(_wind, _dir);
69     _eng->setThrottle(_throttle);
70     _eng->setMixture(_mixture);
71
72     if(_variable) {
73         _omega = _minOmega + _advance * (_maxOmega - _minOmega);
74         _prop->modPitch(1e6); // Start at maximum pitch and move down
75     } else {
76         _omega = 52;
77     }
78
79     bool goingUp = false;
80     float step = 10;
81     while(true) {
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;
86
87         if(Math::abs(tdiff/_moment) < 0.1)
88             break;
89
90         if(tdiff > 0) {
91             if(!goingUp) step *= 0.5;
92             goingUp = true;
93             if(!_variable)  _omega += step;
94             else            _prop->modPitch(1+(step*0.005));
95         } else {
96             if(goingUp) step *= 0.5;
97             goingUp = false;
98             if(!_variable)  _omega -= step;
99             else            _prop->modPitch(1-(step*0.005));
100         }
101     }
102 }
103
104 void PropEngine::integrate(float dt)
105 {
106     float speed = -Math::dot3(_wind, _dir);
107
108     float propTorque, engTorque, thrust;
109
110     _eng->setThrottle(_throttle);
111     _eng->setMixture(_mixture);
112     
113     _prop->calc(_rho, speed, _omega,
114                 &thrust, &propTorque);
115     _eng->calc(_pressure, _temp, _omega, &engTorque, &_fuelFlow);
116
117     // Turn the thrust into a vector and save it
118     Math::mul3(thrust, _dir, _thrust);
119
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;
124
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;
128
129     // Store the total angular momentum into _gyro
130     Math::mul3(_omega*_moment, _dir, _gyro);
131
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);
139
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.
147     if(_variable) {
148         float targetOmega = _minOmega + _advance*(_maxOmega-_minOmega);
149         float ratio2 = (_omega*_omega)/(targetOmega*targetOmega);
150         float targetTorque = engTorque * ratio2;
151
152         float mod = propTorque < targetTorque ? 1.04 : (1/1.04);
153
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);
158
159         _prop->modPitch(mod);
160     }
161 }
162
163 }; // namespace yasim