7 // Start in a "sane" mode, so unset stuff doesn't freak us out
11 _peaks[0] = _peaks[1] = 1;
15 _widths[i] = 0.01; // half a degree
17 _orient[0] = 1; _orient[1] = 0; _orient[2] = 0;
18 _orient[3] = 0; _orient[4] = 1; _orient[5] = 0;
19 _orient[6] = 0; _orient[7] = 0; _orient[8] = 1;
24 _slatPos = _spoilerPos = _flapPos = 0;
25 _slatDrag = _spoilerDrag = _flapDrag = 1;
28 _flapEffectiveness = 1;
34 void Surface::setPosition(float* p)
37 for(i=0; i<3; i++) _pos[i] = p[i];
40 void Surface::getPosition(float* out)
43 for(i=0; i<3; i++) out[i] = _pos[i];
46 void Surface::setChord(float chord)
51 void Surface::setTotalDrag(float c0)
56 float Surface::getTotalDrag()
61 void Surface::setXDrag(float cx)
66 void Surface::setYDrag(float cy)
71 void Surface::setZDrag(float cz)
76 void Surface::setBaseZDrag(float cz0)
81 void Surface::setStallPeak(int i, float peak)
86 void Surface::setStall(int i, float alpha)
91 void Surface::setStallWidth(int i, float width)
96 void Surface::setOrientation(float* o)
103 void Surface::setIncidence(float angle)
108 void Surface::setTwist(float angle)
113 void Surface::setSlatParams(float stallDelta, float dragPenalty)
115 _slatAlpha = stallDelta;
116 _slatDrag = dragPenalty;
119 void Surface::setFlapParams(float liftAdd, float dragPenalty)
122 _flapDrag = dragPenalty;
125 void Surface::setSpoilerParams(float liftPenalty, float dragPenalty)
127 _spoilerLift = liftPenalty;
128 _spoilerDrag = dragPenalty;
131 void Surface::setFlap(float pos)
136 void Surface::setFlapEffectiveness(float effectiveness)
138 _flapEffectiveness = effectiveness;
141 double Surface::getFlapEffectiveness()
143 return _flapEffectiveness;
147 void Surface::setSlat(float pos)
152 void Surface::setSpoiler(float pos)
157 // Calculate the aerodynamic force given a wind vector v (in the
158 // aircraft's "local" coordinates) and an air density rho. Returns a
159 // torque about the Y axis, too.
160 void Surface::calcForce(float* v, float rho, float* out, float* torque)
162 // Split v into magnitude and direction:
163 float vel = Math::mag3(v);
165 // Handle the blowup condition. Zero velocity means zero force by
169 for(i=0; i<3; i++) out[i] = torque[i] = 0;
173 // special case this so the logic below doesn't produce a non-zero
174 // force; should probably have a "no force" flag instead...
175 if(_cx == 0. && _cy == 0. && _cz == 0.) {
176 for(int i=0; i<3; i++) out[i] = torque[i] = 0.;
180 Math::mul3(1/vel, v, out);
182 // Convert to the surface's coordinates
183 Math::vmul33(_orient, out, out);
185 // "Rotate" by the incidence angle. Assume small angles, so we
186 // need to diddle only the Z component, X is relatively unchanged
187 // by small rotations.
188 float incidence = _incidence + _twist;
189 out[2] += incidence * out[0]; // z' = z + incidence * x
191 // Hold onto the local wind vector so we can multiply the induced
194 Math::set3(out, lwind);
196 // Diddle the Z force according to our configuration
197 float stallMul = stallFunc(out);
198 stallMul *= 1 + _spoilerPos * (_spoilerLift - 1);
199 float stallLift = (stallMul - 1) * _cz * out[2];
200 float flaplift = flapLift(out[2]);
202 out[2] *= _cz; // scaling factor
203 out[2] += _cz*_cz0; // zero-alpha lift
207 // Airfoil lift (pre-stall and zero-alpha) torques "up" (negative
208 // torque) around the Y axis, while flap lift pushes down. Both
209 // forces are considered to act at one third chord from the
210 // edge. Convert to local (i.e. airplane) coordiantes and store
213 torque[1] = 0.1667f * _chord * (flaplift - (_cz*_cz0 + stallLift));
215 Math::tmul33(_orient, torque, torque);
217 // The X (drag) force gets diddled for control deflection
218 out[0] = controlDrag(out[2], _cx * out[0]);
220 // Add in any specific Y (side force) coefficient.
223 // Diddle the induced drag
224 Math::mul3(-1*_inducedDrag*out[2]*lwind[2], lwind, lwind);
225 Math::add3(lwind, out, out);
227 // Reverse the incidence rotation to get back to surface
229 out[2] -= incidence * out[0];
231 // Convert back to external coordinates
232 Math::tmul33(_orient, out, out);
234 // Add in the units to make a real force:
235 float scale = 0.5f*rho*vel*vel*_c0;
236 Math::mul3(scale, out, out);
237 Math::mul3(scale, torque, torque);
243 static const float DEG2RAD = 0.0174532925199;
244 float v[3], force[3], torque[3];
245 float rho = Atmosphere::getStdDensity(0);
252 for(float angle = -90; angle<90; angle += 0.01) {
253 float rad = angle * DEG2RAD;
254 v[0] = spd * -Math::cos(rad);
256 v[2] = spd * Math::sin(rad);
257 calcForce(v, rho, force, torque);
258 float lift = force[2] * Math::cos(rad) + force[0] * Math::sin(rad);
259 //__builtin_printf("%f %f\n", angle, lift);
260 __builtin_printf("%f %f\n", angle, torque[2]);
265 // Returns a multiplier for the "plain" force equations that
266 // approximates an airfoil's lift/stall curve.
267 float Surface::stallFunc(float* v)
269 // Sanity check to treat FPU psychopathology
270 if(v[0] == 0) return 1;
272 float alpha = Math::abs(v[2]/v[0]);
274 // Wacky use of indexing, see setStall*() methods.
275 int fwdBak = v[0] > 0; // set if this is "backward motion"
276 int posNeg = v[2] < 0; // set if the airflow is toward -z
277 int i = (fwdBak<<1) | posNeg;
279 float stallAlpha = _stalls[i];
284 stallAlpha += _slatAlpha;
287 if(alpha > stallAlpha+_widths[i])
290 // (note mask: we want to use the "positive" stall angle here)
291 float scale = 0.5f*_peaks[fwdBak]/_stalls[i&2];
294 if(alpha <= stallAlpha)
297 // Inside the stall. Compute a cubic interpolation between the
298 // pre-stall "scale" value and the post-stall unity.
299 float frac = (alpha - stallAlpha) / _widths[i];
300 frac = frac*frac*(3-2*frac);
302 return scale*(1-frac) + frac;
305 // Similar to the above -- interpolates out the flap lift past the
307 float Surface::flapLift(float alpha)
309 float flapLift = _cz * _flapPos * (_flapLift-1) * _flapEffectiveness;
314 if(alpha < 0) alpha = -alpha;
315 if(alpha < _stalls[0])
317 else if(alpha > _stalls[0] + _widths[0])
320 float frac = (alpha - _stalls[0]) / _widths[0];
321 frac = frac*frac*(3-2*frac);
322 return flapLift * (1-frac);
325 float Surface::controlDrag(float lift, float drag)
327 // Negative flap deflections don't affect drag until their lift
328 // multiplier exceeds the "camber" (cz0) of the surface. Use a
329 // synthesized "fp" number instead of the actual flap position.
333 fp -= _cz0/(_flapLift-1);
337 // Calculate an "effective" drag -- this is the drag that would
338 // have been produced by an unflapped surface at the same lift.
339 float flapDragAoA = (_flapLift - 1 - _cz0) * _stalls[0];
340 float fd = Math::abs(lift * flapDragAoA * fp);
341 if(drag < 0) fd = -fd;
344 // Now multiply by the various control factors
345 drag *= 1 + fp * (_flapDrag - 1);
346 drag *= 1 + _spoilerPos * (_spoilerDrag - 1);
347 drag *= 1 + _slatPos * (_slatDrag - 1);
352 }; // namespace yasim