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