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;
33 void Surface::setPosition(float* p)
36 for(i=0; i<3; i++) _pos[i] = p[i];
39 void Surface::getPosition(float* out)
42 for(i=0; i<3; i++) out[i] = _pos[i];
45 void Surface::setChord(float chord)
50 void Surface::setTotalDrag(float c0)
55 float Surface::getTotalDrag()
60 void Surface::setXDrag(float cx)
65 void Surface::setYDrag(float cy)
70 void Surface::setZDrag(float cz)
75 void Surface::setBaseZDrag(float cz0)
80 void Surface::setStallPeak(int i, float peak)
85 void Surface::setStall(int i, float alpha)
90 void Surface::setStallWidth(int i, float width)
95 void Surface::setOrientation(float* o)
102 void Surface::setIncidence(float angle)
107 void Surface::setTwist(float angle)
112 void Surface::setSlatParams(float stallDelta, float dragPenalty)
114 _slatAlpha = stallDelta;
115 _slatDrag = dragPenalty;
118 void Surface::setFlapParams(float liftAdd, float dragPenalty)
121 _flapDrag = dragPenalty;
124 void Surface::setSpoilerParams(float liftPenalty, float dragPenalty)
126 _spoilerLift = liftPenalty;
127 _spoilerDrag = dragPenalty;
130 void Surface::setFlap(float pos)
135 void Surface::setSlat(float pos)
140 void Surface::setSpoiler(float pos)
145 // Calculate the aerodynamic force given a wind vector v (in the
146 // aircraft's "local" coordinates) and an air density rho. Returns a
147 // torque about the Y axis, too.
148 void Surface::calcForce(float* v, float rho, float* out, float* torque)
150 // Split v into magnitude and direction:
151 float vel = Math::mag3(v);
153 // Handle the blowup condition. Zero velocity means zero force by
157 for(i=0; i<3; i++) out[i] = torque[i] = 0;
161 // special case this so the logic below doesn't produce a non-zero
162 // force; should probably have a "no force" flag instead...
163 if(_cx == 0. && _cy == 0. && _cz == 0.) {
164 for(int i=0; i<3; i++) out[i] = torque[i] = 0.;
168 Math::mul3(1/vel, v, out);
170 // Convert to the surface's coordinates
171 Math::vmul33(_orient, out, out);
173 // "Rotate" by the incidence angle. Assume small angles, so we
174 // need to diddle only the Z component, X is relatively unchanged
175 // by small rotations.
176 float incidence = _incidence + _twist;
177 out[2] += incidence * out[0]; // z' = z + incidence * x
179 // Hold onto the local wind vector so we can multiply the induced
182 Math::set3(out, lwind);
184 // Diddle the Z force according to our configuration
185 float stallMul = stallFunc(out);
186 stallMul *= 1 + _spoilerPos * (_spoilerLift - 1);
187 float stallLift = (stallMul - 1) * _cz * out[2];
188 float flaplift = flapLift(out[2]);
190 out[2] *= _cz; // scaling factor
191 out[2] += _cz*_cz0; // zero-alpha lift
195 // Airfoil lift (pre-stall and zero-alpha) torques "up" (negative
196 // torque) around the Y axis, while flap lift pushes down. Both
197 // forces are considered to act at one third chord from the
198 // edge. Convert to local (i.e. airplane) coordiantes and store
201 torque[1] = 0.1667f * _chord * (flaplift - (_cz*_cz0 + stallLift));
203 Math::tmul33(_orient, torque, torque);
205 // The X (drag) force gets diddled for control deflection
206 out[0] = controlDrag(out[2], _cx * out[0]);
208 // Add in any specific Y (side force) coefficient.
211 // Diddle the induced drag
212 Math::mul3(-1*_inducedDrag*out[2]*lwind[2], lwind, lwind);
213 Math::add3(lwind, out, out);
215 // Reverse the incidence rotation to get back to surface
217 out[2] -= incidence * out[0];
219 // Convert back to external coordinates
220 Math::tmul33(_orient, out, out);
222 // Add in the units to make a real force:
223 float scale = 0.5f*rho*vel*vel*_c0;
224 Math::mul3(scale, out, out);
225 Math::mul3(scale, torque, torque);
231 static const float DEG2RAD = 0.0174532925199;
232 float v[3], force[3], torque[3];
233 float rho = Atmosphere::getStdDensity(0);
240 for(float angle = -90; angle<90; angle += 0.01) {
241 float rad = angle * DEG2RAD;
242 v[0] = spd * -Math::cos(rad);
244 v[2] = spd * Math::sin(rad);
245 calcForce(v, rho, force, torque);
246 float lift = force[2] * Math::cos(rad) + force[0] * Math::sin(rad);
247 //__builtin_printf("%f %f\n", angle, lift);
248 __builtin_printf("%f %f\n", angle, torque[2]);
253 // Returns a multiplier for the "plain" force equations that
254 // approximates an airfoil's lift/stall curve.
255 float Surface::stallFunc(float* v)
257 // Sanity check to treat FPU psychopathology
258 if(v[0] == 0) return 1;
260 float alpha = Math::abs(v[2]/v[0]);
262 // Wacky use of indexing, see setStall*() methods.
263 int fwdBak = v[0] > 0; // set if this is "backward motion"
264 int posNeg = v[2] < 0; // set if the airflow is toward -z
265 int i = (fwdBak<<1) | posNeg;
267 float stallAlpha = _stalls[i];
272 stallAlpha += _slatAlpha;
275 if(alpha > stallAlpha+_widths[i])
278 // (note mask: we want to use the "positive" stall angle here)
279 float scale = 0.5f*_peaks[fwdBak]/_stalls[i&2];
282 if(alpha <= stallAlpha)
285 // Inside the stall. Compute a cubic interpolation between the
286 // pre-stall "scale" value and the post-stall unity.
287 float frac = (alpha - stallAlpha) / _widths[i];
288 frac = frac*frac*(3-2*frac);
290 return scale*(1-frac) + frac;
293 // Similar to the above -- interpolates out the flap lift past the
295 float Surface::flapLift(float alpha)
297 float flapLift = _cz * _flapPos * (_flapLift-1);
299 if(alpha < 0) alpha = -alpha;
300 if(alpha < _stalls[0])
302 else if(alpha > _stalls[0] + _widths[0])
305 float frac = (alpha - _stalls[0]) / _widths[0];
306 frac = frac*frac*(3-2*frac);
307 return flapLift * (1-frac) + frac;
310 float Surface::controlDrag(float lift, float drag)
312 // Negative flap deflections don't affect drag until their lift
313 // multiplier exceeds the "camber" (cz0) of the surface. Use a
314 // synthesized "fp" number instead of the actual flap position.
318 fp -= _cz0/(_flapLift-1);
322 // Calculate an "effective" drag -- this is the drag that would
323 // have been produced by an unflapped surface at the same lift.
324 float flapDragAoA = (_flapLift - 1 - _cz0) * _stalls[0];
325 float fd = Math::abs(lift * flapDragAoA * fp);
326 if(drag < 0) fd = -fd;
329 // Now multiply by the various control factors
330 drag *= 1 + fp * (_flapDrag - 1);
331 drag *= 1 + _spoilerPos * (_spoilerDrag - 1);
332 drag *= 1 + _slatPos * (_slatDrag - 1);
337 }; // namespace yasim