]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/FGFDM.cpp
Add support for a turbo prop condition lever.
[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();
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 void FGFDM::setOutputProperties()
395 {
396     // char buf[256];
397     int i;
398
399     float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
400     fgSetFloat("/yasim/gross-weight-lbs", grossWgt);
401
402     ControlMap* cm = _airplane.getControlMap();
403     for(i=0; i<_controlProps.size(); i++) {
404         PropOut* p = (PropOut*)_controlProps.get(i);
405         float val = (p->left
406                      ? cm->getOutput(p->handle)
407                      : cm->getOutputR(p->handle));
408         float rmin = cm->rangeMin(p->type);
409         float rmax = cm->rangeMax(p->type);
410         float frac = (val - rmin) / (rmax - rmin);
411         val = frac*(p->max - p->min) + p->min;
412         p->prop->setFloatValue(val);
413     }
414
415     for(i=0; i<_airplane.getNumRotors(); i++) {
416         Rotor*r=(Rotor*)_airplane.getRotor(i);
417         int j = 0;
418         float f;
419         char b[256];
420         while(j = r->getValueforFGSet(j, b, &f))
421             if(b[0]) fgSetFloat(b,f);
422         
423         for(j=0; j < r->numRotorparts(); j++) {
424             Rotorpart* s = (Rotorpart*)r->getRotorpart(j);
425             char *b;
426             int k;
427             for(k=0; k<2; k++) {
428                 b=s->getAlphaoutput(k);
429                 if(b[0]) fgSetFloat(b, s->getAlpha(k));
430             }
431         }
432         for(j=0; j < r->numRotorblades(); j++) {
433             Rotorblade* s = (Rotorblade*)r->getRotorblade(j);
434             char *b;
435             int k;
436             for (k=0; k<2; k++) {
437                 b = s->getAlphaoutput(k);
438                 if(b[0]) fgSetFloat(b, s->getAlpha(k));
439             }
440         }
441     }
442
443     float fuelDensity = _airplane.getFuelDensity(0); // HACK
444     for(i=0; i<_thrusters.size(); i++) {
445         EngRec* er = (EngRec*)_thrusters.get(i);
446         Thruster* t = er->eng;
447         SGPropertyNode * node = fgGetNode("engines/engine", i, true);
448
449         // Set: running, cranking, prop-thrust, max-hp, power-pct
450         node->setBoolValue("running", t->isRunning());
451         node->setBoolValue("cranking", t->isCranking());
452
453         float tmp[3];
454         t->getThrust(tmp);
455         float lbs = Math::mag3(tmp) * (KG2LBS/9.8);
456         node->setFloatValue("prop-thrust", lbs); // Deprecated name
457         node->setFloatValue("thrust-lbs", lbs);
458         node->setFloatValue("fuel-flow-gph",
459                             (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
460
461         if(t->getPropEngine()) {
462             PropEngine* p = t->getPropEngine();
463             node->setFloatValue("rpm", p->getOmega() * (1/RPM2RAD));
464             node->setFloatValue("torque-ftlb",
465                                 p->getEngine()->getTorque() * NM2FTLB);
466         
467             if(p->getEngine()->isPistonEngine()) {
468                 PistonEngine* pe = p->getEngine()->isPistonEngine();
469                 node->setFloatValue("mp-osi", pe->getMP() * (1/INHG2PA));
470                 node->setFloatValue("mp-inhg", pe->getMP() * (1/INHG2PA));
471                 node->setFloatValue("egt-degf",
472                                     pe->getEGT() * K2DEGF + K2DEGFOFFSET);
473             } else if(p->getEngine()->isTurbineEngine()) {
474                 TurbineEngine* te = p->getEngine()->isTurbineEngine();
475                 node->setFloatValue("n2", te->getN2());
476             }
477         }
478
479         if(t->getJet()) {
480             Jet* j = t->getJet();
481             node->setFloatValue("n1", j->getN1());
482             node->setFloatValue("n2", j->getN2());
483             node->setFloatValue("epr", j->getEPR());
484             node->setFloatValue("egr-degf",
485                                 j->getEGT() * K2DEGF + K2DEGFOFFSET);
486         }
487     }
488 }
489
490 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
491 {
492     Wing* w = new Wing();
493
494     float defDihed = 0;
495     if(eq(type, "vstab"))
496         defDihed = 90;
497     else
498         w->setMirror(true);
499
500     float pos[3];
501     pos[0] = attrf(a, "x");
502     pos[1] = attrf(a, "y");
503     pos[2] = attrf(a, "z");
504     w->setBase(pos);
505
506     w->setLength(attrf(a, "length"));
507     w->setChord(attrf(a, "chord"));
508     w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
509     w->setTaper(attrf(a, "taper", 1));
510     w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
511     w->setCamber(attrf(a, "camber", 0));
512     w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
513     w->setTwist(attrf(a, "twist", 0) * DEG2RAD);
514
515     // The 70% is a magic number that sorta kinda seems to match known
516     // throttle settings to approach speed.
517     w->setInducedDrag(0.7*attrf(a, "idrag", 1));
518
519     float effect = attrf(a, "effectiveness", 1);
520     w->setDragScale(w->getDragScale()*effect);
521
522     _currObj = w;
523     return w;
524 }
525
526 Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
527 {
528     Rotor* w = new Rotor();
529
530     // float defDihed = 0;
531
532     float pos[3];
533     pos[0] = attrf(a, "x");
534     pos[1] = attrf(a, "y");
535     pos[2] = attrf(a, "z");
536     w->setBase(pos);
537
538     float normal[3];
539     normal[0] = attrf(a, "nx");
540     normal[1] = attrf(a, "ny");
541     normal[2] = attrf(a, "nz");
542     w->setNormal(normal);
543
544     float forward[3];
545     forward[0] = attrf(a, "fx");
546     forward[1] = attrf(a, "fy");
547     forward[2] = attrf(a, "fz");
548     w->setForward(forward);
549
550     w->setMaxCyclicail(attrf(a, "maxcyclicail", 7.6));
551     w->setMaxCyclicele(attrf(a, "maxcyclicele", 4.94));
552     w->setMinCyclicail(attrf(a, "mincyclicail", -7.6));
553     w->setMinCyclicele(attrf(a, "mincyclicele", -4.94));
554     w->setMaxCollective(attrf(a, "maxcollective", 15.8));
555     w->setMinCollective(attrf(a, "mincollective", -0.2));
556     w->setDiameter(attrf(a, "diameter", 10.2));
557     w->setWeightPerBlade(attrf(a, "weightperblade", 44));
558     w->setNumberOfBlades(attrf(a, "numblades", 4));
559     w->setRelBladeCenter(attrf(a, "relbladecenter", 0.7));
560     w->setDynamic(attrf(a, "dynamic", 0.7));
561     w->setDelta3(attrf(a, "delta3", 0));
562     w->setDelta(attrf(a, "delta", 0));
563     w->setTranslift(attrf(a, "translift", 0.05));
564     w->setC2(attrf(a, "dragfactor", 1));
565     w->setStepspersecond(attrf(a, "stepspersecond", 120));
566     w->setRPM(attrf(a, "rpm", 424));
567     w->setRelLenHinge(attrf(a, "rellenflaphinge", 0.07));
568     w->setAlpha0((attrf(a, "flap0", -5))*YASIM_PI/180);
569     w->setAlphamin((attrf(a, "flapmin", -15))/180*YASIM_PI);
570     w->setAlphamax((attrf(a, "flapmax",  15))*YASIM_PI/180);
571     w->setAlpha0factor(attrf(a, "flap0factor", 1));
572     w->setTeeterdamp(attrf(a,"teeterdamp",.0001));
573     w->setMaxteeterdamp(attrf(a,"maxteeterdamp",1000));
574     w->setRelLenTeeterHinge(attrf(a,"rellenteeterhinge",0.01));
575     void setAlphamin(float f);
576     void setAlphamax(float f);
577     void setAlpha0factor(float f);
578
579     if(attrb(a,"ccw"))
580        w->setCcw(1); 
581     
582     if(a->hasAttribute("name"))
583        w->setName(a->getValue("name") );
584     if(a->hasAttribute("alphaout0"))
585        w->setAlphaoutput(0,a->getValue("alphaout0") );
586     if(a->hasAttribute("alphaout1"))  w->setAlphaoutput(1,a->getValue("alphaout1") );
587     if(a->hasAttribute("alphaout2"))  w->setAlphaoutput(2,a->getValue("alphaout2") );
588     if(a->hasAttribute("alphaout3"))  w->setAlphaoutput(3,a->getValue("alphaout3") );
589     if(a->hasAttribute("coneout"))  w->setAlphaoutput(4,a->getValue("coneout") );
590     if(a->hasAttribute("yawout"))   w->setAlphaoutput(5,a->getValue("yawout") );
591     if(a->hasAttribute("rollout"))  w->setAlphaoutput(6,a->getValue("rollout") );
592
593     w->setPitchA(attrf(a, "pitch_a", 10));
594     w->setPitchB(attrf(a, "pitch_b", 10));
595     w->setForceAtPitchA(attrf(a, "forceatpitch_a", 3000));
596     w->setPowerAtPitch0(attrf(a, "poweratpitch_0", 300));
597     w->setPowerAtPitchB(attrf(a, "poweratpitch_b", 3000));
598     if(attrb(a,"notorque"))
599        w->setNotorque(1); 
600     if(attrb(a,"simblades"))
601        w->setSimBlades(1); 
602
603     _currObj = w;
604     return w;
605 }
606
607 void FGFDM::parsePistonEngine(XMLAttributes* a)
608 {
609     float engP = attrf(a, "eng-power") * HP2W;
610     float engS = attrf(a, "eng-rpm") * RPM2RAD;
611
612     PistonEngine* eng = new PistonEngine(engP, engS);
613
614     if(a->hasAttribute("displacement"))
615         eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
616
617     if(a->hasAttribute("compression"))
618         eng->setCompression(attrf(a, "compression"));        
619
620     if(a->hasAttribute("turbo-mul")) {
621         float mul = attrf(a, "turbo-mul");
622         float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
623         eng->setTurboParams(mul, mp);
624     }
625
626     ((PropEngine*)_currObj)->setEngine(eng);
627 }
628
629 void FGFDM::parseTurbineEngine(XMLAttributes* a)
630 {
631     float power = attrf(a, "eng-power") * HP2W;
632     float omega = attrf(a, "eng-rpm") * RPM2RAD;
633     float alt = attrf(a, "alt") * FT2M;
634     float flatRating = attrf(a, "flat-rating") * HP2W;
635     TurbineEngine* eng = new TurbineEngine(power, omega, alt, flatRating);
636
637     if(a->hasAttribute("n2-low-idle"))
638         eng->setN2Range(attrf(a, "n2-low-idle"), attrf(a, "n2-high-idle"),
639                         attrf(a, "n2-max"));
640
641     // Nasty units conversion: lbs/hr per hp -> kg/s per watt
642     if(a->hasAttribute("bsfc"))
643         eng->setFuelConsumption(attrf(a, "bsfc") * (LBS2KG/(3600*HP2W)));
644
645     ((PropEngine*)_currObj)->setEngine(eng);
646 }
647
648 void FGFDM::parsePropeller(XMLAttributes* a)
649 {
650     // Legacy Handling for the old engines syntax:
651     PistonEngine* eng = 0;
652     if(a->hasAttribute("eng-power")) {
653         SG_LOG(SG_FLIGHT,SG_ALERT, "WARNING: "
654                << "Legacy engine definition in YASim configuration file.  "
655                << "Please fix.");
656         float engP = attrf(a, "eng-power") * HP2W;
657         float engS = attrf(a, "eng-rpm") * RPM2RAD;
658         eng = new PistonEngine(engP, engS);
659         if(a->hasAttribute("displacement"))
660             eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
661         if(a->hasAttribute("compression"))
662             eng->setCompression(attrf(a, "compression"));        
663         if(a->hasAttribute("turbo-mul")) {
664             float mul = attrf(a, "turbo-mul");
665             float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
666             eng->setTurboParams(mul, mp);
667         }
668     }
669
670     // Now parse the actual propeller definition:
671     float cg[3];
672     cg[0] = attrf(a, "x");
673     cg[1] = attrf(a, "y");
674     cg[2] = attrf(a, "z");
675     float mass = attrf(a, "mass") * LBS2KG;
676     float moment = attrf(a, "moment");
677     float radius = attrf(a, "radius");
678     float speed = attrf(a, "cruise-speed") * KTS2MPS;
679     float omega = attrf(a, "cruise-rpm") * RPM2RAD;
680     float power = attrf(a, "cruise-power") * HP2W;
681     float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
682
683     Propeller* prop = new Propeller(radius, speed, omega, rho, power);
684     PropEngine* thruster = new PropEngine(prop, eng, moment);
685     _airplane.addThruster(thruster, mass, cg);
686
687     if(a->hasAttribute("takeoff-power")) {
688         float power0 = attrf(a, "takeoff-power") * HP2W;
689         float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
690         prop->setTakeoff(omega0, power0);
691     }
692
693     if(a->hasAttribute("max-rpm")) {
694         float max = attrf(a, "max-rpm") * RPM2RAD;
695         float min = attrf(a, "min-rpm") * RPM2RAD;
696         thruster->setVariableProp(min, max);
697     }
698
699     if(a->hasAttribute("manual-pitch")) {
700         prop->setManualPitch();
701     }
702
703     thruster->setGearRatio(attrf(a, "gear-ratio", 1));
704
705     char buf[64];
706     sprintf(buf, "/engines/engine[%d]", _nextEngine++);
707     EngRec* er = new EngRec();
708     er->eng = thruster;
709     er->prefix = dup(buf);
710     _thrusters.add(er);
711
712     _currObj = thruster;
713 }
714
715 // Turns a string axis name into an integer for use by the
716 // ControlMap.  Creates a new axis if this one hasn't been defined
717 // yet.
718 int FGFDM::parseAxis(const char* name)
719 {
720     int i;
721     for(i=0; i<_axes.size(); i++) {
722         AxisRec* a = (AxisRec*)_axes.get(i);
723         if(eq(a->name, name))
724             return a->handle;
725     }
726
727     // Not there, make a new one.
728     AxisRec* a = new AxisRec();
729     a->name = dup(name);
730     fgGetNode( a->name, true ); // make sure the property name exists
731     a->handle = _airplane.getControlMap()->newInput();
732     _axes.add(a);
733     return a->handle;
734 }
735
736 int FGFDM::parseOutput(const char* name)
737 {
738     if(eq(name, "THROTTLE"))  return ControlMap::THROTTLE;
739     if(eq(name, "MIXTURE"))   return ControlMap::MIXTURE;
740     if(eq(name, "CONDLEVER")) return ControlMap::CONDLEVER;
741     if(eq(name, "STARTER"))   return ControlMap::STARTER;
742     if(eq(name, "MAGNETOS"))  return ControlMap::MAGNETOS;
743     if(eq(name, "ADVANCE"))   return ControlMap::ADVANCE;
744     if(eq(name, "REHEAT"))    return ControlMap::REHEAT;
745     if(eq(name, "BOOST"))     return ControlMap::BOOST;
746     if(eq(name, "VECTOR"))    return ControlMap::VECTOR;
747     if(eq(name, "PROP"))      return ControlMap::PROP;
748     if(eq(name, "BRAKE"))     return ControlMap::BRAKE;
749     if(eq(name, "STEER"))     return ControlMap::STEER;
750     if(eq(name, "EXTEND"))    return ControlMap::EXTEND;
751     if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
752     if(eq(name, "FLAP0"))     return ControlMap::FLAP0;
753     if(eq(name, "FLAP1"))     return ControlMap::FLAP1;
754     if(eq(name, "SLAT"))      return ControlMap::SLAT;
755     if(eq(name, "SPOILER"))   return ControlMap::SPOILER;
756     if(eq(name, "CASTERING")) return ControlMap::CASTERING;
757     if(eq(name, "PROPPITCH")) return ControlMap::PROPPITCH;
758     if(eq(name, "PROPFEATHER")) return ControlMap::PROPFEATHER;
759     if(eq(name, "COLLECTIVE")) return ControlMap::COLLECTIVE;
760     if(eq(name, "CYCLICAIL")) return ControlMap::CYCLICAIL;
761     if(eq(name, "CYCLICELE")) return ControlMap::CYCLICELE;
762     if(eq(name, "ROTORENGINEON")) return ControlMap::ROTORENGINEON;
763     if(eq(name, "REVERSE_THRUST")) return ControlMap::REVERSE_THRUST;
764     SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
765            << name << "' in YASim aircraft description.");
766     exit(1);
767
768 }
769
770 void FGFDM::parseWeight(XMLAttributes* a)
771 {
772     WeightRec* wr = new WeightRec();
773
774     float v[3];
775     v[0] = attrf(a, "x");
776     v[1] = attrf(a, "y");
777     v[2] = attrf(a, "z");
778
779     wr->prop = dup(a->getValue("mass-prop"));
780     wr->size = attrf(a, "size", 0);
781     wr->handle = _airplane.addWeight(v, wr->size);
782
783     _weights.add(wr);
784 }
785
786 bool FGFDM::eq(const char* a, const char* b)
787 {
788     // Figure it out for yourself. :)
789     while(*a && *b && *a == *b) { a++; b++; }
790     return !(*a || *b);
791 }
792
793 char* FGFDM::dup(const char* s)
794 {
795     int len=0;
796     while(s[len++]);
797     char* s2 = new char[len+1];
798     char* p = s2;
799     while((*p++ = *s++));
800     s2[len] = 0;
801     return s2;
802 }
803
804 int FGFDM::attri(XMLAttributes* atts, char* attr)
805 {
806     if(!atts->hasAttribute(attr)) {
807         SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
808                "' in YASim aircraft description");
809         exit(1);
810     }
811     return attri(atts, attr, 0);
812 }
813
814 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
815 {
816     const char* val = atts->getValue(attr);
817     if(val == 0) return def;
818     else         return atol(val);
819 }
820
821 float FGFDM::attrf(XMLAttributes* atts, char* attr)
822 {
823     if(!atts->hasAttribute(attr)) {
824         SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
825                "' in YASim aircraft description");
826         exit(1);
827     }
828     return attrf(atts, attr, 0);
829 }
830
831 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
832 {
833     const char* val = atts->getValue(attr);
834     if(val == 0) return def;
835     else         return (float)atof(val);    
836 }
837
838 // ACK: the dreaded ambiguous string boolean.  Remind me to shoot Maik
839 // when I have a chance. :).  Unless you have a parser that can check
840 // symbol constants (we don't), this kind of coding is just a Bad
841 // Idea.  This implementation, for example, silently returns a boolean
842 // falsehood for values of "1", "yes", "True", and "TRUE".  Which is
843 // especially annoying preexisting boolean attributes in the same
844 // parser want to see "1" and will choke on a "true"...
845 //
846 // Unfortunately, this usage creeped into existing configuration files
847 // while I wasn't active, and it's going to be hard to remove.  Issue
848 // a warning to nag people into changing their ways for now...
849 bool FGFDM::attrb(XMLAttributes* atts, char* attr)
850 {
851     const char* val = atts->getValue(attr);
852     if(val == 0) return false;
853
854     if(eq(val,"true")) {
855         SG_LOG(SG_FLIGHT, SG_ALERT, "Warning: " <<
856                "deprecated 'true' boolean in YASim configuration file.  " <<
857                "Use numeric booleans (attribute=\"1\") instead");
858         return true;
859     }
860     return attri(atts, attr, 0) ? true : false;
861 }
862
863 }; // namespace yasim