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