]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/FGFDM.cpp
Support for a new "contra" attribute on propellers, which properly
[flightgear.git] / src / FDM / YASim / FGFDM.cpp
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include <Main/fg_props.hxx>
5
6 #include "Math.hpp"
7 #include "Jet.hpp"
8 #include "SimpleJet.hpp"
9 #include "Gear.hpp"
10 #include "Atmosphere.hpp"
11 #include "PropEngine.hpp"
12 #include "Propeller.hpp"
13 #include "PistonEngine.hpp"
14 #include "TurbineEngine.hpp"
15 #include "Rotor.hpp"
16 #include "Rotorpart.hpp"
17 #include "Rotorblade.hpp"
18
19 #include "FGFDM.hpp"
20
21 namespace yasim {
22
23 // Some conversion factors
24 static const float KTS2MPS = 0.514444444444;
25 static const float FT2M = 0.3048;
26 static const float DEG2RAD = 0.0174532925199;
27 static const float RPM2RAD = 0.10471975512;
28 static const float LBS2N = 4.44822;
29 static const float LBS2KG = 0.45359237;
30 static const float KG2LBS = 2.2046225;
31 static const float CM2GALS = 264.172037284;
32 static const float HP2W = 745.700;
33 static const float INHG2PA = 3386.389;
34 static const float K2DEGF = 1.8;
35 static const float K2DEGFOFFSET = -459.4;
36 static const float CIN2CM = 1.6387064e-5;
37 static const float YASIM_PI = 3.14159265358979323846;
38
39 static const float NM2FTLB = (1/(LBS2N*FT2M));
40
41 // Stubs, so that this can be compiled without the FlightGear
42 // binary.  What's the best way to handle this?
43
44 //     float fgGetFloat(char* name, float def) { return 0; }
45 //     void fgSetFloat(char* name, float val) {}
46
47 FGFDM::FGFDM()
48 {
49     _nextEngine = 0;
50
51     // Map /controls/flight/elevator to the approach elevator control.  This
52     // should probably be settable, but there are very few aircraft
53     // who trim their approaches using things other than elevator.
54     _airplane.setElevatorControl(parseAxis("/controls/flight/elevator-trim"));
55
56     // FIXME: read seed from somewhere?
57     int seed = 0;
58     _turb = new Turbulence(10, seed);
59 }
60
61 FGFDM::~FGFDM()
62 {
63     int i;
64     for(i=0; i<_axes.size(); i++) {
65         AxisRec* a = (AxisRec*)_axes.get(i);
66         delete[] a->name;
67         delete a;
68     }
69     for(i=0; i<_thrusters.size(); i++) {
70         EngRec* er = (EngRec*)_thrusters.get(i);
71         delete[] er->prefix;
72         delete er->eng;
73         delete er;
74     }
75     for(i=0; i<_weights.size(); i++) {
76         WeightRec* wr = (WeightRec*)_weights.get(i);
77         delete[] wr->prop;
78         delete wr;
79     }
80     for(i=0; i<_controlProps.size(); i++)
81         delete (PropOut*)_controlProps.get(i);
82 }
83
84 void FGFDM::iterate(float dt)
85 {
86     getExternalInput(dt);
87     _airplane.iterate(dt);
88
89     // Do fuel stuff (FIXME: should stash SGPropertyNode objects here)
90     char buf[256];
91     for(int i=0; i<_airplane.numThrusters(); i++) {
92         Thruster* t = _airplane.getThruster(i);
93
94         sprintf(buf, "/engines/engine[%d]/out-of-fuel", i);
95         t->setFuelState(!fgGetBool(buf));
96
97         sprintf(buf, "/engines/engine[%d]/fuel-consumed-lbs", i);
98         double consumed = fgGetDouble(buf) + dt * KG2LBS * t->getFuelFlow();
99         fgSetDouble(buf, consumed);
100     }
101     for(int i=0; i<_airplane.numTanks(); i++) {
102         sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
103         _airplane.setFuel(i, LBS2KG * fgGetFloat(buf));
104     } 
105     _airplane.calcFuelWeights();
106     
107     setOutputProperties(dt);
108 }
109
110 Airplane* FGFDM::getAirplane()
111 {
112     return &_airplane;
113 }
114
115 void FGFDM::init()
116 {
117     // Allows the user to start with something other than full fuel
118     _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
119
120     // Read out the resulting fuel state
121     char buf[256];
122     for(int i=0; i<_airplane.numTanks(); i++) {
123         sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
124         fgSetDouble(buf, _airplane.getFuel(i) * KG2LBS);
125
126         double density = _airplane.getFuelDensity(i);
127         sprintf(buf, "/consumables/fuel/tank[%d]/density-ppg", i);
128         fgSetDouble(buf, density * (KG2LBS/CM2GALS));
129
130         sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
131         fgSetDouble(buf, _airplane.getFuel(i) * CM2GALS / density);
132
133         sprintf(buf, "/consumables/fuel/tank[%d]/capacity-gal_us", i);
134         fgSetDouble(buf, CM2GALS * _airplane.getTankCapacity(i)/density);
135     }    
136
137     // This has a nasty habit of being false at startup.  That's not
138     // good.
139     fgSetBool("/controls/gear/gear-down", true);
140
141     _airplane.getModel()->setTurbulence(_turb);
142 }
143
144 // Not the worlds safest parser.  But it's short & sweet.
145 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
146 {
147     XMLAttributes* a = (XMLAttributes*)&atts;
148     float v[3];
149     char buf[64];
150
151     if(eq(name, "airplane")) {
152         _airplane.setWeight(attrf(a, "mass") * LBS2KG);
153     } else if(eq(name, "approach")) {
154         float spd = attrf(a, "speed") * KTS2MPS;
155         float alt = attrf(a, "alt", 0) * FT2M;
156         float aoa = attrf(a, "aoa", 0) * DEG2RAD;
157         _airplane.setApproach(spd, alt, aoa, attrf(a, "fuel", 0.2));
158         _cruiseCurr = false;
159     } else if(eq(name, "cruise")) {
160         float spd = attrf(a, "speed") * KTS2MPS;
161         float alt = attrf(a, "alt") * FT2M;
162         _airplane.setCruise(spd, alt, attrf(a, "fuel", 0.5));
163         _cruiseCurr = true;
164     } else if(eq(name, "solve-weight")) {
165         int idx = attri(a, "idx");
166         float wgt = attrf(a, "weight") * LBS2KG;
167         _airplane.addSolutionWeight(!_cruiseCurr, idx, wgt);
168     } else if(eq(name, "cockpit")) {
169         v[0] = attrf(a, "x");
170         v[1] = attrf(a, "y");
171         v[2] = attrf(a, "z");
172         _airplane.setPilotPos(v);
173     } else if(eq(name, "rotor")) {
174         _airplane.addRotor(parseRotor(a, name));
175     } else if(eq(name, "wing")) {
176         _airplane.setWing(parseWing(a, name));
177     } else if(eq(name, "hstab")) {
178         _airplane.setTail(parseWing(a, name));
179     } else if(eq(name, "vstab") || eq(name, "mstab")) {
180         _airplane.addVStab(parseWing(a, name));
181     } else if(eq(name, "piston-engine")) {
182         parsePistonEngine(a);
183     } else if(eq(name, "turbine-engine")) {
184         parseTurbineEngine(a);
185     } else if(eq(name, "propeller")) {
186         parsePropeller(a);
187     } else if(eq(name, "thruster")) {
188         SimpleJet* j = new SimpleJet();
189         _currObj = j;
190         v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
191         j->setPosition(v);
192         _airplane.addThruster(j, 0, v);
193         v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
194         j->setDirection(v);
195         j->setThrust(attrf(a, "thrust") * LBS2N);
196     } else if(eq(name, "jet")) {
197         Jet* j = new Jet();
198         _currObj = j;
199         v[0] = attrf(a, "x");
200         v[1] = attrf(a, "y");
201         v[2] = attrf(a, "z");
202         float mass = attrf(a, "mass") * LBS2KG;
203         j->setMaxThrust(attrf(a, "thrust") * LBS2N,
204                         attrf(a, "afterburner", 0) * LBS2N);
205         j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
206         j->setReverseThrust(attrf(a, "reverse", 0.2));
207
208         float n1min = attrf(a, "n1-idle", 55);
209         float n1max = attrf(a, "n1-max", 102);
210         float n2min = attrf(a, "n2-idle", 73);
211         float n2max = attrf(a, "n2-max", 103);
212         j->setRPMs(n1min, n1max, n2min, n2max);
213
214         j->setTSFC(attrf(a, "tsfc", 0.8));
215         if(a->hasAttribute("egt"))  j->setEGT(attrf(a, "egt"));
216         if(a->hasAttribute("epr"))  j->setEPR(attrf(a, "epr"));
217         if(a->hasAttribute("exhaust-speed"))
218             j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
219         
220         j->setPosition(v);
221         _airplane.addThruster(j, mass, v);
222         sprintf(buf, "/engines/engine[%d]", _nextEngine++);
223         EngRec* er = new EngRec();
224         er->eng = j;
225         er->prefix = dup(buf);
226         _thrusters.add(er);
227     } else if(eq(name, "gear")) {
228         Gear* g = new Gear();
229         _currObj = g;
230         v[0] = attrf(a, "x");
231         v[1] = attrf(a, "y");
232         v[2] = attrf(a, "z");
233         g->setPosition(v);
234         v[0] = 0;
235         v[1] = 0;
236         v[2] = attrf(a, "compression", 1);
237         g->setCompression(v);
238         g->setBrake(attrf(a, "skid", 0));
239         g->setStaticFriction(attrf(a, "sfric", 0.8));
240         g->setDynamicFriction(attrf(a, "dfric", 0.7));
241         g->setSpring(attrf(a, "spring", 1));
242         g->setDamping(attrf(a, "damp", 1));
243         _airplane.addGear(g);
244     } else if(eq(name, "fuselage")) {
245         float b[3];
246         v[0] = attrf(a, "ax");
247         v[1] = attrf(a, "ay");
248         v[2] = attrf(a, "az");
249         b[0] = attrf(a, "bx");
250         b[1] = attrf(a, "by");
251         b[2] = attrf(a, "bz");
252         float taper = attrf(a, "taper", 1);
253         float mid = attrf(a, "midpoint", 0.5);
254         _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid);
255     } else if(eq(name, "tank")) {
256         v[0] = attrf(a, "x");
257         v[1] = attrf(a, "y");
258         v[2] = attrf(a, "z");
259         float density = 6.0; // gasoline, in lbs/gal
260         if(a->hasAttribute("jet")) density = 6.72; 
261         density *= LBS2KG*CM2GALS;
262         _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
263     } else if(eq(name, "ballast")) {
264         v[0] = attrf(a, "x");
265         v[1] = attrf(a, "y");
266         v[2] = attrf(a, "z");
267         _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
268     } else if(eq(name, "weight")) {
269         parseWeight(a);
270     } else if(eq(name, "stall")) {
271         Wing* w = (Wing*)_currObj;
272         w->setStall(attrf(a, "aoa") * DEG2RAD);
273         w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
274         w->setStallPeak(attrf(a, "peak", 1.5));
275     } else if(eq(name, "flap0")) {
276         ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
277                                     attrf(a, "lift"), attrf(a, "drag"));
278     } else if(eq(name, "flap1")) {
279         ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
280                                     attrf(a, "lift"), attrf(a, "drag"));
281     } else if(eq(name, "slat")) {
282         ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
283                                    attrf(a, "aoa"), attrf(a, "drag"));
284     } else if(eq(name, "spoiler")) {
285         ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
286                                       attrf(a, "lift"), attrf(a, "drag"));
287     /* } else if(eq(name, "collective")) {
288         ((Rotor*)_currObj)->setcollective(attrf(a, "min"), attrf(a, "max"));
289     } else if(eq(name, "cyclic")) {
290         ((Rotor*)_currObj)->setcyclic(attrf(a, "ail"), attrf(a, "ele"));
291     */                               
292     } else if(eq(name, "actionpt")) {
293         v[0] = attrf(a, "x");
294         v[1] = attrf(a, "y");
295         v[2] = attrf(a, "z");
296         ((Thruster*)_currObj)->setPosition(v);
297     } else if(eq(name, "dir")) {
298         v[0] = attrf(a, "x");
299         v[1] = attrf(a, "y");
300         v[2] = attrf(a, "z");
301         ((Thruster*)_currObj)->setDirection(v);
302     } else if(eq(name, "control-setting")) {
303         // A cruise or approach control setting
304         const char* axis = a->getValue("axis");
305         float value = attrf(a, "value", 0);
306         if(_cruiseCurr)
307             _airplane.addCruiseControl(parseAxis(axis), value);
308         else
309             _airplane.addApproachControl(parseAxis(axis), value);
310     } else if(eq(name, "control-input")) {
311
312         // A mapping of input property to a control
313         int axis = parseAxis(a->getValue("axis"));
314         int control = parseOutput(a->getValue("control"));
315         int opt = 0;
316         opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
317         opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
318         opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
319         
320         ControlMap* cm = _airplane.getControlMap();
321         if(a->hasAttribute("src0")) {
322                            cm->addMapping(axis, control, _currObj, opt,
323                            attrf(a, "src0"), attrf(a, "src1"), 
324                            attrf(a, "dst0"), attrf(a, "dst1"));
325         } else {
326             cm->addMapping(axis, control, _currObj, opt);
327         }
328     } else if(eq(name, "control-output")) {
329         // A property output for a control on the current object
330         ControlMap* cm = _airplane.getControlMap();
331         int type = parseOutput(a->getValue("control"));
332         int handle = cm->getOutputHandle(_currObj, type);
333
334         PropOut* p = new PropOut();
335         p->prop = fgGetNode(a->getValue("prop"), true);
336         p->handle = handle;
337         p->type = type;
338         p->left = !(a->hasAttribute("side") &&
339                         eq("right", a->getValue("side")));
340         p->min = attrf(a, "min", cm->rangeMin(type));
341         p->max = attrf(a, "max", cm->rangeMax(type));
342         _controlProps.add(p);
343
344     } else if(eq(name, "control-speed")) {
345         ControlMap* cm = _airplane.getControlMap();
346         int type = parseOutput(a->getValue("control"));
347         int handle = cm->getOutputHandle(_currObj, type);
348         float time = attrf(a, "transition-time", 0);
349         
350         cm->setTransitionTime(handle, time);
351     } else {
352         SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '"
353                << name << "' found in YASim aircraft description");
354         exit(1);
355     }
356 }
357
358 void FGFDM::getExternalInput(float dt)
359 {
360     char buf[256];
361
362     _turb->setMagnitude(fgGetFloat("/environment/turbulence/magnitude-norm"));
363     _turb->update(dt, fgGetFloat("/environment/turbulence/rate-hz"));
364
365     // The control axes
366     ControlMap* cm = _airplane.getControlMap();
367     cm->reset();
368     int i;
369     for(i=0; i<_axes.size(); i++) {
370         AxisRec* a = (AxisRec*)_axes.get(i);
371         float val = fgGetFloat(a->name, 0);
372         cm->setInput(a->handle, val);
373     }
374     cm->applyControls(dt);
375
376     // Weights
377     for(i=0; i<_weights.size(); i++) {
378         WeightRec* wr = (WeightRec*)_weights.get(i);
379         _airplane.setWeight(wr->handle, LBS2KG * fgGetFloat(wr->prop));
380     }
381
382     for(i=0; i<_thrusters.size(); i++) {
383         EngRec* er = (EngRec*)_thrusters.get(i);
384         Thruster* t = er->eng;
385
386         if(t->getPropEngine()) {
387             PropEngine* p = t->getPropEngine();
388             sprintf(buf, "%s/rpm", er->prefix);
389             p->setOmega(fgGetFloat(buf, 500) * RPM2RAD);
390         }
391     }
392 }
393
394 // Linearly "seeks" a property by the specified fraction of the way to
395 // the target value.  Used to emulate "slowly changing" output values.
396 static void moveprop(SGPropertyNode* node, const char* prop,
397                     float target, float frac)
398 {
399     float val = node->getFloatValue(prop);
400     if(frac > 1) frac = 1;
401     if(frac < 0) frac = 0;
402     val += (target - val) * frac;
403     node->setFloatValue(prop, val);
404 }
405
406 void FGFDM::setOutputProperties(float dt)
407 {
408     // char buf[256];
409     int i;
410
411     float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
412     fgSetFloat("/yasim/gross-weight-lbs", grossWgt);
413
414     ControlMap* cm = _airplane.getControlMap();
415     for(i=0; i<_controlProps.size(); i++) {
416         PropOut* p = (PropOut*)_controlProps.get(i);
417         float val = (p->left
418                      ? cm->getOutput(p->handle)
419                      : cm->getOutputR(p->handle));
420         float rmin = cm->rangeMin(p->type);
421         float rmax = cm->rangeMax(p->type);
422         float frac = (val - rmin) / (rmax - rmin);
423         val = frac*(p->max - p->min) + p->min;
424         p->prop->setFloatValue(val);
425     }
426
427     for(i=0; i<_airplane.getNumRotors(); i++) {
428         Rotor*r=(Rotor*)_airplane.getRotor(i);
429         int j = 0;
430         float f;
431         char b[256];
432         while(j = r->getValueforFGSet(j, b, &f))
433             if(b[0]) fgSetFloat(b,f);
434         
435         for(j=0; j < r->numRotorparts(); j++) {
436             Rotorpart* s = (Rotorpart*)r->getRotorpart(j);
437             char *b;
438             int k;
439             for(k=0; k<2; k++) {
440                 b=s->getAlphaoutput(k);
441                 if(b[0]) fgSetFloat(b, s->getAlpha(k));
442             }
443         }
444         for(j=0; j < r->numRotorblades(); j++) {
445             Rotorblade* s = (Rotorblade*)r->getRotorblade(j);
446             char *b;
447             int k;
448             for (k=0; k<2; k++) {
449                 b = s->getAlphaoutput(k);
450                 if(b[0]) fgSetFloat(b, s->getAlpha(k));
451             }
452         }
453     }
454
455     float fuelDensity = _airplane.getFuelDensity(0); // HACK
456     for(i=0; i<_thrusters.size(); i++) {
457         EngRec* er = (EngRec*)_thrusters.get(i);
458         Thruster* t = er->eng;
459         SGPropertyNode * node = fgGetNode("engines/engine", i, true);
460
461         // Set: running, cranking, prop-thrust, max-hp, power-pct
462         node->setBoolValue("running", t->isRunning());
463         node->setBoolValue("cranking", t->isCranking());
464
465         float tmp[3];
466         t->getThrust(tmp);
467         float lbs = Math::mag3(tmp) * (KG2LBS/9.8);
468         node->setFloatValue("prop-thrust", lbs); // Deprecated name
469         node->setFloatValue("thrust-lbs", lbs);
470         node->setFloatValue("fuel-flow-gph",
471                             (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
472
473         if(t->getPropEngine()) {
474             PropEngine* p = t->getPropEngine();
475             node->setFloatValue("rpm", p->getOmega() * (1/RPM2RAD));
476             node->setFloatValue("torque-ftlb",
477                                 p->getEngine()->getTorque() * NM2FTLB);
478         
479             if(p->getEngine()->isPistonEngine()) {
480                 PistonEngine* pe = p->getEngine()->isPistonEngine();
481                 node->setFloatValue("mp-osi", pe->getMP() * (1/INHG2PA));
482                 node->setFloatValue("mp-inhg", pe->getMP() * (1/INHG2PA));
483                 node->setFloatValue("egt-degf",
484                                     pe->getEGT() * K2DEGF + K2DEGFOFFSET);
485             } else if(p->getEngine()->isTurbineEngine()) {
486                 TurbineEngine* te = p->getEngine()->isTurbineEngine();
487                 node->setFloatValue("n2", te->getN2());
488             }
489         }
490
491         if(t->getJet()) {
492             Jet* j = t->getJet();
493             node->setFloatValue("n1", j->getN1());
494             node->setFloatValue("n2", j->getN2());
495             node->setFloatValue("epr", j->getEPR());
496             node->setFloatValue("egr-degf",
497                                 j->getEGT() * K2DEGF + K2DEGFOFFSET);
498
499             // These are "unmodeled" values that are still needed for
500             // many cockpits.  Tie them all to the N1 speed, but
501             // normalize the numbers to the range [0:1] so the
502             // cockpit code can scale them to the right values.
503             float pnorm = j->getPerfNorm();
504             moveprop(node, "oilp-norm", pnorm, dt/3); // 3s seek time
505             moveprop(node, "oilt-norm", pnorm, dt/30); // 30s 
506             moveprop(node, "itt-norm", pnorm, dt/1); // 1s
507         }
508     }
509 }
510
511 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
512 {
513     Wing* w = new Wing();
514
515     float defDihed = 0;
516     if(eq(type, "vstab"))
517         defDihed = 90;
518     else
519         w->setMirror(true);
520
521     float pos[3];
522     pos[0] = attrf(a, "x");
523     pos[1] = attrf(a, "y");
524     pos[2] = attrf(a, "z");
525     w->setBase(pos);
526
527     w->setLength(attrf(a, "length"));
528     w->setChord(attrf(a, "chord"));
529     w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
530     w->setTaper(attrf(a, "taper", 1));
531     w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
532     w->setCamber(attrf(a, "camber", 0));
533     w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
534     w->setTwist(attrf(a, "twist", 0) * DEG2RAD);
535
536     // The 70% is a magic number that sorta kinda seems to match known
537     // throttle settings to approach speed.
538     w->setInducedDrag(0.7*attrf(a, "idrag", 1));
539
540     float effect = attrf(a, "effectiveness", 1);
541     w->setDragScale(w->getDragScale()*effect);
542
543     _currObj = w;
544     return w;
545 }
546
547 Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
548 {
549     Rotor* w = new Rotor();
550
551     // float defDihed = 0;
552
553     float pos[3];
554     pos[0] = attrf(a, "x");
555     pos[1] = attrf(a, "y");
556     pos[2] = attrf(a, "z");
557     w->setBase(pos);
558
559     float normal[3];
560     normal[0] = attrf(a, "nx");
561     normal[1] = attrf(a, "ny");
562     normal[2] = attrf(a, "nz");
563     w->setNormal(normal);
564
565     float forward[3];
566     forward[0] = attrf(a, "fx");
567     forward[1] = attrf(a, "fy");
568     forward[2] = attrf(a, "fz");
569     w->setForward(forward);
570
571     w->setMaxCyclicail(attrf(a, "maxcyclicail", 7.6));
572     w->setMaxCyclicele(attrf(a, "maxcyclicele", 4.94));
573     w->setMinCyclicail(attrf(a, "mincyclicail", -7.6));
574     w->setMinCyclicele(attrf(a, "mincyclicele", -4.94));
575     w->setMaxCollective(attrf(a, "maxcollective", 15.8));
576     w->setMinCollective(attrf(a, "mincollective", -0.2));
577     w->setDiameter(attrf(a, "diameter", 10.2));
578     w->setWeightPerBlade(attrf(a, "weightperblade", 44));
579     w->setNumberOfBlades(attrf(a, "numblades", 4));
580     w->setRelBladeCenter(attrf(a, "relbladecenter", 0.7));
581     w->setDynamic(attrf(a, "dynamic", 0.7));
582     w->setDelta3(attrf(a, "delta3", 0));
583     w->setDelta(attrf(a, "delta", 0));
584     w->setTranslift(attrf(a, "translift", 0.05));
585     w->setC2(attrf(a, "dragfactor", 1));
586     w->setStepspersecond(attrf(a, "stepspersecond", 120));
587     w->setRPM(attrf(a, "rpm", 424));
588     w->setRelLenHinge(attrf(a, "rellenflaphinge", 0.07));
589     w->setAlpha0((attrf(a, "flap0", -5))*YASIM_PI/180);
590     w->setAlphamin((attrf(a, "flapmin", -15))/180*YASIM_PI);
591     w->setAlphamax((attrf(a, "flapmax",  15))*YASIM_PI/180);
592     w->setAlpha0factor(attrf(a, "flap0factor", 1));
593     w->setTeeterdamp(attrf(a,"teeterdamp",.0001));
594     w->setMaxteeterdamp(attrf(a,"maxteeterdamp",1000));
595     w->setRelLenTeeterHinge(attrf(a,"rellenteeterhinge",0.01));
596     void setAlphamin(float f);
597     void setAlphamax(float f);
598     void setAlpha0factor(float f);
599
600     if(attrb(a,"ccw"))
601        w->setCcw(1); 
602     
603     if(a->hasAttribute("name"))
604        w->setName(a->getValue("name") );
605     if(a->hasAttribute("alphaout0"))
606        w->setAlphaoutput(0,a->getValue("alphaout0") );
607     if(a->hasAttribute("alphaout1"))  w->setAlphaoutput(1,a->getValue("alphaout1") );
608     if(a->hasAttribute("alphaout2"))  w->setAlphaoutput(2,a->getValue("alphaout2") );
609     if(a->hasAttribute("alphaout3"))  w->setAlphaoutput(3,a->getValue("alphaout3") );
610     if(a->hasAttribute("coneout"))  w->setAlphaoutput(4,a->getValue("coneout") );
611     if(a->hasAttribute("yawout"))   w->setAlphaoutput(5,a->getValue("yawout") );
612     if(a->hasAttribute("rollout"))  w->setAlphaoutput(6,a->getValue("rollout") );
613
614     w->setPitchA(attrf(a, "pitch_a", 10));
615     w->setPitchB(attrf(a, "pitch_b", 10));
616     w->setForceAtPitchA(attrf(a, "forceatpitch_a", 3000));
617     w->setPowerAtPitch0(attrf(a, "poweratpitch_0", 300));
618     w->setPowerAtPitchB(attrf(a, "poweratpitch_b", 3000));
619     if(attrb(a,"notorque"))
620        w->setNotorque(1); 
621     if(attrb(a,"simblades"))
622        w->setSimBlades(1); 
623
624     _currObj = w;
625     return w;
626 }
627
628 void FGFDM::parsePistonEngine(XMLAttributes* a)
629 {
630     float engP = attrf(a, "eng-power") * HP2W;
631     float engS = attrf(a, "eng-rpm") * RPM2RAD;
632
633     PistonEngine* eng = new PistonEngine(engP, engS);
634
635     if(a->hasAttribute("displacement"))
636         eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
637
638     if(a->hasAttribute("compression"))
639         eng->setCompression(attrf(a, "compression"));        
640
641     if(a->hasAttribute("turbo-mul")) {
642         float mul = attrf(a, "turbo-mul");
643         float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
644         eng->setTurboParams(mul, mp);
645     }
646
647     ((PropEngine*)_currObj)->setEngine(eng);
648 }
649
650 void FGFDM::parseTurbineEngine(XMLAttributes* a)
651 {
652     float power = attrf(a, "eng-power") * HP2W;
653     float omega = attrf(a, "eng-rpm") * RPM2RAD;
654     float alt = attrf(a, "alt") * FT2M;
655     float flatRating = attrf(a, "flat-rating") * HP2W;
656     TurbineEngine* eng = new TurbineEngine(power, omega, alt, flatRating);
657
658     if(a->hasAttribute("n2-low-idle"))
659         eng->setN2Range(attrf(a, "n2-low-idle"), attrf(a, "n2-high-idle"),
660                         attrf(a, "n2-max"));
661
662     // Nasty units conversion: lbs/hr per hp -> kg/s per watt
663     if(a->hasAttribute("bsfc"))
664         eng->setFuelConsumption(attrf(a, "bsfc") * (LBS2KG/(3600*HP2W)));
665
666     ((PropEngine*)_currObj)->setEngine(eng);
667 }
668
669 void FGFDM::parsePropeller(XMLAttributes* a)
670 {
671     // Legacy Handling for the old engines syntax:
672     PistonEngine* eng = 0;
673     if(a->hasAttribute("eng-power")) {
674         SG_LOG(SG_FLIGHT,SG_ALERT, "WARNING: "
675                << "Legacy engine definition in YASim configuration file.  "
676                << "Please fix.");
677         float engP = attrf(a, "eng-power") * HP2W;
678         float engS = attrf(a, "eng-rpm") * RPM2RAD;
679         eng = new PistonEngine(engP, engS);
680         if(a->hasAttribute("displacement"))
681             eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
682         if(a->hasAttribute("compression"))
683             eng->setCompression(attrf(a, "compression"));        
684         if(a->hasAttribute("turbo-mul")) {
685             float mul = attrf(a, "turbo-mul");
686             float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
687             eng->setTurboParams(mul, mp);
688         }
689     }
690
691     // Now parse the actual propeller definition:
692     float cg[3];
693     cg[0] = attrf(a, "x");
694     cg[1] = attrf(a, "y");
695     cg[2] = attrf(a, "z");
696     float mass = attrf(a, "mass") * LBS2KG;
697     float moment = attrf(a, "moment");
698     float radius = attrf(a, "radius");
699     float speed = attrf(a, "cruise-speed") * KTS2MPS;
700     float omega = attrf(a, "cruise-rpm") * RPM2RAD;
701     float power = attrf(a, "cruise-power") * HP2W;
702     float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
703
704     Propeller* prop = new Propeller(radius, speed, omega, rho, power);
705     PropEngine* thruster = new PropEngine(prop, eng, moment);
706     _airplane.addThruster(thruster, mass, cg);
707
708     if(a->hasAttribute("takeoff-power")) {
709         float power0 = attrf(a, "takeoff-power") * HP2W;
710         float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
711         prop->setTakeoff(omega0, power0);
712     }
713
714     if(a->hasAttribute("max-rpm")) {
715         float max = attrf(a, "max-rpm") * RPM2RAD;
716         float min = attrf(a, "min-rpm") * RPM2RAD;
717         thruster->setVariableProp(min, max);
718     }
719
720     if(attrb(a, "contra"))
721         thruster->setContraPair(true);
722
723     if(a->hasAttribute("manual-pitch")) {
724         prop->setManualPitch();
725     }
726
727     thruster->setGearRatio(attrf(a, "gear-ratio", 1));
728
729     char buf[64];
730     sprintf(buf, "/engines/engine[%d]", _nextEngine++);
731     EngRec* er = new EngRec();
732     er->eng = thruster;
733     er->prefix = dup(buf);
734     _thrusters.add(er);
735
736     _currObj = thruster;
737 }
738
739 // Turns a string axis name into an integer for use by the
740 // ControlMap.  Creates a new axis if this one hasn't been defined
741 // yet.
742 int FGFDM::parseAxis(const char* name)
743 {
744     int i;
745     for(i=0; i<_axes.size(); i++) {
746         AxisRec* a = (AxisRec*)_axes.get(i);
747         if(eq(a->name, name))
748             return a->handle;
749     }
750
751     // Not there, make a new one.
752     AxisRec* a = new AxisRec();
753     a->name = dup(name);
754     fgGetNode( a->name, true ); // make sure the property name exists
755     a->handle = _airplane.getControlMap()->newInput();
756     _axes.add(a);
757     return a->handle;
758 }
759
760 int FGFDM::parseOutput(const char* name)
761 {
762     if(eq(name, "THROTTLE"))  return ControlMap::THROTTLE;
763     if(eq(name, "MIXTURE"))   return ControlMap::MIXTURE;
764     if(eq(name, "CONDLEVER")) return ControlMap::CONDLEVER;
765     if(eq(name, "STARTER"))   return ControlMap::STARTER;
766     if(eq(name, "MAGNETOS"))  return ControlMap::MAGNETOS;
767     if(eq(name, "ADVANCE"))   return ControlMap::ADVANCE;
768     if(eq(name, "REHEAT"))    return ControlMap::REHEAT;
769     if(eq(name, "BOOST"))     return ControlMap::BOOST;
770     if(eq(name, "VECTOR"))    return ControlMap::VECTOR;
771     if(eq(name, "PROP"))      return ControlMap::PROP;
772     if(eq(name, "BRAKE"))     return ControlMap::BRAKE;
773     if(eq(name, "STEER"))     return ControlMap::STEER;
774     if(eq(name, "EXTEND"))    return ControlMap::EXTEND;
775     if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
776     if(eq(name, "FLAP0"))     return ControlMap::FLAP0;
777     if(eq(name, "FLAP1"))     return ControlMap::FLAP1;
778     if(eq(name, "SLAT"))      return ControlMap::SLAT;
779     if(eq(name, "SPOILER"))   return ControlMap::SPOILER;
780     if(eq(name, "CASTERING")) return ControlMap::CASTERING;
781     if(eq(name, "PROPPITCH")) return ControlMap::PROPPITCH;
782     if(eq(name, "PROPFEATHER")) return ControlMap::PROPFEATHER;
783     if(eq(name, "COLLECTIVE")) return ControlMap::COLLECTIVE;
784     if(eq(name, "CYCLICAIL")) return ControlMap::CYCLICAIL;
785     if(eq(name, "CYCLICELE")) return ControlMap::CYCLICELE;
786     if(eq(name, "ROTORENGINEON")) return ControlMap::ROTORENGINEON;
787     if(eq(name, "REVERSE_THRUST")) return ControlMap::REVERSE_THRUST;
788     SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
789            << name << "' in YASim aircraft description.");
790     exit(1);
791
792 }
793
794 void FGFDM::parseWeight(XMLAttributes* a)
795 {
796     WeightRec* wr = new WeightRec();
797
798     float v[3];
799     v[0] = attrf(a, "x");
800     v[1] = attrf(a, "y");
801     v[2] = attrf(a, "z");
802
803     wr->prop = dup(a->getValue("mass-prop"));
804     wr->size = attrf(a, "size", 0);
805     wr->handle = _airplane.addWeight(v, wr->size);
806
807     _weights.add(wr);
808 }
809
810 bool FGFDM::eq(const char* a, const char* b)
811 {
812     // Figure it out for yourself. :)
813     while(*a && *b && *a == *b) { a++; b++; }
814     return !(*a || *b);
815 }
816
817 char* FGFDM::dup(const char* s)
818 {
819     int len=0;
820     while(s[len++]);
821     char* s2 = new char[len+1];
822     char* p = s2;
823     while((*p++ = *s++));
824     s2[len] = 0;
825     return s2;
826 }
827
828 int FGFDM::attri(XMLAttributes* atts, char* attr)
829 {
830     if(!atts->hasAttribute(attr)) {
831         SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
832                "' in YASim aircraft description");
833         exit(1);
834     }
835     return attri(atts, attr, 0);
836 }
837
838 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
839 {
840     const char* val = atts->getValue(attr);
841     if(val == 0) return def;
842     else         return atol(val);
843 }
844
845 float FGFDM::attrf(XMLAttributes* atts, char* attr)
846 {
847     if(!atts->hasAttribute(attr)) {
848         SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
849                "' in YASim aircraft description");
850         exit(1);
851     }
852     return attrf(atts, attr, 0);
853 }
854
855 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
856 {
857     const char* val = atts->getValue(attr);
858     if(val == 0) return def;
859     else         return (float)atof(val);    
860 }
861
862 // ACK: the dreaded ambiguous string boolean.  Remind me to shoot Maik
863 // when I have a chance. :).  Unless you have a parser that can check
864 // symbol constants (we don't), this kind of coding is just a Bad
865 // Idea.  This implementation, for example, silently returns a boolean
866 // falsehood for values of "1", "yes", "True", and "TRUE".  Which is
867 // especially annoying preexisting boolean attributes in the same
868 // parser want to see "1" and will choke on a "true"...
869 //
870 // Unfortunately, this usage creeped into existing configuration files
871 // while I wasn't active, and it's going to be hard to remove.  Issue
872 // a warning to nag people into changing their ways for now...
873 bool FGFDM::attrb(XMLAttributes* atts, char* attr)
874 {
875     const char* val = atts->getValue(attr);
876     if(val == 0) return false;
877
878     if(eq(val,"true")) {
879         SG_LOG(SG_FLIGHT, SG_ALERT, "Warning: " <<
880                "deprecated 'true' boolean in YASim configuration file.  " <<
881                "Use numeric booleans (attribute=\"1\") instead");
882         return true;
883     }
884     return attri(atts, attr, 0) ? true : false;
885 }
886
887 }; // namespace yasim