]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/FGFDM.cpp
Pull Sound-manager out of FGGlobals
[flightgear.git] / src / FDM / YASim / FGFDM.cpp
1 #ifdef HAVE_CONFIG_H
2 #  include "config.h"
3 #endif
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include <Main/fg_props.hxx>
9
10 #include "Math.hpp"
11 #include "Jet.hpp"
12 #include "SimpleJet.hpp"
13 #include "Gear.hpp"
14 #include "Hook.hpp"
15 #include "Launchbar.hpp"
16 #include "Atmosphere.hpp"
17 #include "PropEngine.hpp"
18 #include "Propeller.hpp"
19 #include "PistonEngine.hpp"
20 #include "TurbineEngine.hpp"
21 #include "Rotor.hpp"
22 #include "Rotorpart.hpp"
23 #include "Hitch.hpp"
24
25 #include "FGFDM.hpp"
26
27 namespace yasim {
28
29 // Some conversion factors
30 static const float KTS2MPS = 0.514444444444;
31 static const float FT2M = 0.3048;
32 static const float DEG2RAD = 0.0174532925199;
33 static const float RPM2RAD = 0.10471975512;
34 static const float LBS2N = 4.44822;
35 static const float LBS2KG = 0.45359237;
36 static const float KG2LBS = 2.2046225;
37 static const float CM2GALS = 264.172037284;
38 static const float HP2W = 745.700;
39 static const float INHG2PA = 3386.389;
40 static const float K2DEGF = 1.8;
41 static const float K2DEGFOFFSET = -459.4;
42 static const float CIN2CM = 1.6387064e-5;
43 static const float YASIM_PI = 3.14159265358979323846;
44
45 static const float NM2FTLB = (1/(LBS2N*FT2M));
46
47 // Stubs, so that this can be compiled without the FlightGear
48 // binary.  What's the best way to handle this?
49
50 //     float fgGetFloat(char* name, float def) { return 0; }
51 //     void fgSetFloat(char* name, float val) {}
52
53 FGFDM::FGFDM()
54 {
55     _vehicle_radius = 0.0f;
56
57     _nextEngine = 0;
58
59     // Map /controls/flight/elevator to the approach elevator control.  This
60     // should probably be settable, but there are very few aircraft
61     // who trim their approaches using things other than elevator.
62     _airplane.setElevatorControl(parseAxis("/controls/flight/elevator-trim"));
63
64     // FIXME: read seed from somewhere?
65     int seed = 0;
66     _turb = new Turbulence(10, seed);
67 }
68
69 FGFDM::~FGFDM()
70 {
71     for(int i=0; i<_axes.size(); i++) {
72         AxisRec* a = (AxisRec*)_axes.get(i);
73         delete[] a->name;
74         delete a;
75     }
76
77     for(int i=0; i<_thrusters.size(); i++) {
78         EngRec* er = (EngRec*)_thrusters.get(i);
79         delete[] er->prefix;
80         delete er->eng;
81         delete er;
82     }
83
84     for(int i=0; i<_weights.size(); i++) {
85         WeightRec* wr = (WeightRec*)_weights.get(i);
86         delete[] wr->prop;
87         delete wr;
88     }
89
90     for(int i=0; i<_controlProps.size(); i++)
91         delete (PropOut*)_controlProps.get(i);
92
93     delete _turb;
94 }
95
96 void FGFDM::iterate(float dt)
97 {
98     getExternalInput(dt);
99     _airplane.iterate(dt);
100
101     // Do fuel stuff
102     for(int i=0; i<_airplane.numThrusters(); i++) {
103         Thruster* t = _airplane.getThruster(i);
104
105         bool out_of_fuel = _fuel_props[i]._out_of_fuel->getBoolValue();
106         t->setFuelState(!out_of_fuel);
107
108         double consumed = _fuel_props[i]._fuel_consumed_lbs->getDoubleValue();
109         _fuel_props[i]._fuel_consumed_lbs->setDoubleValue(
110                 consumed + dt * KG2LBS * t->getFuelFlow());
111     }
112     for(int i=0; i<_airplane.numTanks(); i++) {
113         _airplane.setFuel(i, LBS2KG * _tank_level_lbs[i]->getFloatValue());
114     }
115     _airplane.calcFuelWeights();
116     
117     setOutputProperties(dt);
118 }
119
120 Airplane* FGFDM::getAirplane()
121 {
122     return &_airplane;
123 }
124
125 void FGFDM::init()
126 {
127     _turb_magnitude_norm = fgGetNode("/environment/turbulence/magnitude-norm", true);
128     _turb_rate_hz        = fgGetNode("/environment/turbulence/rate-hz", true);
129     _gross_weight_lbs    = fgGetNode("/yasim/gross-weight-lbs", true);
130
131     // Allows the user to start with something other than full fuel
132     _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
133
134     // stash engine/thruster properties
135     _thrust_props.clear();
136     for (int i=0; i<_thrusters.size(); i++) {
137         SGPropertyNode_ptr node = fgGetNode("engines/engine", i, true);
138         Thruster* t = ((EngRec*)_thrusters.get(i))->eng;
139
140         ThrusterProps tp;
141         tp._running =       node->getChild("running", 0, true);
142         tp._cranking =      node->getChild("cranking", 0, true);
143         tp._prop_thrust =   node->getChild("prop-thrust", 0, true); // Deprecated name
144         tp._thrust_lbs =    node->getChild("thrust-lbs", 0, true);
145         tp._fuel_flow_gph = node->getChild("fuel-flow-gph", 0, true);
146
147         if(t->getPropEngine())
148         {
149             tp._rpm = node->getChild("rpm", 0, true);
150             tp._torque_ftlb = node->getChild("torque-ftlb", 0, true);
151
152             PropEngine* p = t->getPropEngine();
153             if(p->getEngine()->isPistonEngine())
154             {
155                 tp._mp_osi =   node->getChild("mp-osi",   0, true);
156                 tp._mp_inhg =  node->getChild("mp-inhg",  0, true);
157                 tp._egt_degf = node->getChild("egt-degf", 0, true);
158
159                 tp._oil_temperature_degf = node->getChild("oil-temperature-degf", 0, true);
160                 tp._boost_gauge_inhg =     node->getChild("boost-gauge-inhg", 0, true);
161             } else if(p->getEngine()->isTurbineEngine()) {
162                 tp._n2 = node->getChild("n2", 0, true);
163             }
164         }
165
166         if(t->getJet())
167         {
168             tp._n1 =       node->getChild("n1",       0, true);
169             tp._n2 =       node->getChild("n2",       0, true);
170             tp._epr =      node->getChild("epr",      0, true);
171             tp._egt_degf = node->getChild("egt-degf", 0, true);
172         }
173         _thrust_props.push_back(tp);
174     }
175
176     // stash properties for fuel state
177     _fuel_props.clear();
178     for(int i=0; i<_airplane.numThrusters(); i++) {
179         SGPropertyNode_ptr e = fgGetNode("engines/engine", i, true);
180         FuelProps f;
181         f._out_of_fuel       = e->getChild("out-of-fuel", 0, true);
182         f._fuel_consumed_lbs = e->getChild("fuel-consumed-lbs", 0, true);
183         _fuel_props.push_back(f);
184     }
185
186     // initialize tanks and stash properties for tank level
187     _tank_level_lbs.clear();
188     for(int i=0; i<_airplane.numTanks(); i++) {
189         char buf[256];
190         sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
191         fgSetDouble(buf, _airplane.getFuel(i) * KG2LBS);
192         _tank_level_lbs.push_back(fgGetNode(buf, true));
193
194         double density = _airplane.getFuelDensity(i);
195         sprintf(buf, "/consumables/fuel/tank[%d]/density-ppg", i);
196         fgSetDouble(buf, density * (KG2LBS/CM2GALS));
197
198 // set in TankProperties class
199 //        sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
200 //        fgSetDouble(buf, _airplane.getFuel(i) * CM2GALS / density);
201
202         sprintf(buf, "/consumables/fuel/tank[%d]/capacity-gal_us", i);
203         fgSetDouble(buf, CM2GALS * _airplane.getTankCapacity(i)/density);
204     }
205
206     // This has a nasty habit of being false at startup.  That's not
207     // good.
208     fgSetBool("/controls/gear/gear-down", true);
209
210     _airplane.getModel()->setTurbulence(_turb);
211 }
212
213 // Not the worlds safest parser.  But it's short & sweet.
214 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
215 {
216     XMLAttributes* a = (XMLAttributes*)&atts;
217     float v[3];
218     char buf[64];
219
220     if(eq(name, "airplane")) {
221         _airplane.setWeight(attrf(a, "mass") * LBS2KG);
222         if(a->hasAttribute("version")) {
223           _airplane.setVersion( a->getValue("version") );
224         }
225 #if defined(ENABLE_DEV_WARNINGS)
226         if( !_airplane.isVersionOrNewer( Version::YASIM_VERSION_CURRENT ) ) {
227           SG_LOG(SG_FLIGHT,SG_ALERT, "This aircraft does not use the latest yasim configuration version.");
228         }
229 #endif
230     } else if(eq(name, "approach")) {
231         float spd = attrf(a, "speed") * KTS2MPS;
232         float alt = attrf(a, "alt", 0) * FT2M;
233         float aoa = attrf(a, "aoa", 0) * DEG2RAD;
234         float gla = attrf(a, "glide-angle", 0) * DEG2RAD;
235         _airplane.setApproach(spd, alt, aoa, attrf(a, "fuel", 0.2),gla);
236         _cruiseCurr = false;
237     } else if(eq(name, "cruise")) {
238         float spd = attrf(a, "speed") * KTS2MPS;
239         float alt = attrf(a, "alt") * FT2M;
240         float gla = attrf(a, "glide-angle", 0) * DEG2RAD;
241         _airplane.setCruise(spd, alt, attrf(a, "fuel", 0.5),gla);
242         _cruiseCurr = true;
243     } else if(eq(name, "solve-weight")) {
244         int idx = attri(a, "idx");
245         float wgt = attrf(a, "weight") * LBS2KG;
246         _airplane.addSolutionWeight(!_cruiseCurr, idx, wgt);
247     } else if(eq(name, "cockpit")) {
248         v[0] = attrf(a, "x");
249         v[1] = attrf(a, "y");
250         v[2] = attrf(a, "z");
251         _airplane.setPilotPos(v);
252     } else if(eq(name, "rotor")) {
253         _airplane.getModel()->getRotorgear()->addRotor(parseRotor(a, name));
254     } else if(eq(name, "rotorgear")) {
255         Rotorgear* r = _airplane.getModel()->getRotorgear();
256         _currObj = r;
257         #define p(x) if (a->hasAttribute(#x)) r->setParameter((char *)#x,attrf(a,#x) );
258         #define p2(x,y) if (a->hasAttribute(y)) r->setParameter((char *)#x,attrf(a,y) );
259         p2(max_power_engine,"max-power-engine")
260         p2(engine_prop_factor,"engine-prop-factor")
261         p(yasimdragfactor)
262         p(yasimliftfactor)
263         p2(max_power_rotor_brake,"max-power-rotor-brake")
264         p2(rotorgear_friction,"rotorgear-friction")
265         p2(engine_accel_limit,"engine-accel-limit")
266         #undef p
267         #undef p2
268         r->setInUse();
269     } else if(eq(name, "wing")) {
270         _airplane.setWing(parseWing(a, name, &_airplane));
271     } else if(eq(name, "hstab")) {
272         _airplane.setTail(parseWing(a, name, &_airplane));
273     } else if(eq(name, "vstab") || eq(name, "mstab")) {
274         _airplane.addVStab(parseWing(a, name, &_airplane));
275     } else if(eq(name, "piston-engine")) {
276         parsePistonEngine(a);
277     } else if(eq(name, "turbine-engine")) {
278         parseTurbineEngine(a);
279     } else if(eq(name, "propeller")) {
280         parsePropeller(a);
281     } else if(eq(name, "thruster")) {
282         SimpleJet* j = new SimpleJet();
283         _currObj = j;
284         v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
285         j->setPosition(v);
286         _airplane.addThruster(j, 0, v);
287         v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
288         j->setDirection(v);
289         j->setThrust(attrf(a, "thrust") * LBS2N);
290     } else if(eq(name, "jet")) {
291         Jet* j = new Jet();
292         _currObj = j;
293         v[0] = attrf(a, "x");
294         v[1] = attrf(a, "y");
295         v[2] = attrf(a, "z");
296         float mass = attrf(a, "mass") * LBS2KG;
297         j->setMaxThrust(attrf(a, "thrust") * LBS2N,
298                         attrf(a, "afterburner", 0) * LBS2N);
299         j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
300         j->setReverseThrust(attrf(a, "reverse", 0.2));
301
302         float n1min = attrf(a, "n1-idle", 55);
303         float n1max = attrf(a, "n1-max", 102);
304         float n2min = attrf(a, "n2-idle", 73);
305         float n2max = attrf(a, "n2-max", 103);
306         j->setRPMs(n1min, n1max, n2min, n2max);
307
308         j->setTSFC(attrf(a, "tsfc", 0.8));
309         if(a->hasAttribute("egt"))  j->setEGT(attrf(a, "egt"));
310         if(a->hasAttribute("epr"))  j->setEPR(attrf(a, "epr"));
311         if(a->hasAttribute("exhaust-speed"))
312             j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
313         if(a->hasAttribute("spool-time"))
314             j->setSpooling(attrf(a, "spool-time"));
315         
316         j->setPosition(v);
317         _airplane.addThruster(j, mass, v);
318         sprintf(buf, "/engines/engine[%d]", _nextEngine++);
319         EngRec* er = new EngRec();
320         er->eng = j;
321         er->prefix = dup(buf);
322         _thrusters.add(er);
323     } else if(eq(name, "hitch")) {
324         Hitch* h = new Hitch(a->getValue("name"));
325         _currObj = h;
326         v[0] = attrf(a, "x");
327         v[1] = attrf(a, "y");
328         v[2] = attrf(a, "z");
329         h->setPosition(v);
330         if(a->hasAttribute("force-is-calculated-by-other")) h->setForceIsCalculatedByOther(attrb(a,"force-is-calculated-by-other"));
331         _airplane.addHitch(h);
332     } else if(eq(name, "tow")) {
333         Hitch* h = (Hitch*)_currObj;
334         if(a->hasAttribute("length"))
335             h->setTowLength(attrf(a, "length"));
336         if(a->hasAttribute("elastic-constant"))
337             h->setTowElasticConstant(attrf(a, "elastic-constant"));
338         if(a->hasAttribute("break-force"))
339             h->setTowBreakForce(attrf(a, "break-force"));
340         if(a->hasAttribute("weight-per-meter"))
341             h->setTowWeightPerM(attrf(a, "weight-per-meter"));
342         if(a->hasAttribute("mp-auto-connect-period"))
343             h->setMpAutoConnectPeriod(attrf(a, "mp-auto-connect-period"));
344     } else if(eq(name, "winch")) {
345         Hitch* h = (Hitch*)_currObj;
346         double pos[3];
347         pos[0] = attrd(a, "x",0);
348         pos[1] = attrd(a, "y",0);
349         pos[2] = attrd(a, "z",0);
350         h->setWinchPosition(pos);
351         if(a->hasAttribute("max-speed"))
352             h->setWinchMaxSpeed(attrf(a, "max-speed"));
353         if(a->hasAttribute("power"))
354             h->setWinchPower(attrf(a, "power") * 1000);
355         if(a->hasAttribute("max-force"))
356             h->setWinchMaxForce(attrf(a, "max-force"));
357         if(a->hasAttribute("initial-tow-length"))
358             h->setWinchInitialTowLength(attrf(a, "initial-tow-length"));
359         if(a->hasAttribute("max-tow-length"))
360             h->setWinchMaxTowLength(attrf(a, "max-tow-length"));
361         if(a->hasAttribute("min-tow-length"))
362             h->setWinchMinTowLength(attrf(a, "min-tow-length"));
363     } else if(eq(name, "gear")) {
364         Gear* g = new Gear();
365         _currObj = g;
366         v[0] = attrf(a, "x");
367         v[1] = attrf(a, "y");
368         v[2] = attrf(a, "z");
369         g->setPosition(v);
370         float nrm = Math::mag3(v);
371         if (_vehicle_radius < nrm)
372             _vehicle_radius = nrm;
373         if(a->hasAttribute("upx")) {
374             v[0] = attrf(a, "upx");
375             v[1] = attrf(a, "upy");
376             v[2] = attrf(a, "upz");
377             Math::unit3(v, v);
378         } else {
379             v[0] = 0;
380             v[1] = 0;
381             v[2] = 1;
382         }
383         for(int i=0; i<3; i++)
384             v[i] *= attrf(a, "compression", 1);
385         g->setCompression(v);
386         g->setBrake(attrf(a, "skid", 0));
387         g->setInitialLoad(attrf(a, "initial-load", 0));
388         g->setStaticFriction(attrf(a, "sfric", 0.8));
389         g->setDynamicFriction(attrf(a, "dfric", 0.7));
390         g->setSpring(attrf(a, "spring", 1));
391         g->setDamping(attrf(a, "damp", 1));
392         if(a->hasAttribute("on-water")) g->setOnWater(attrb(a,"on-water"));
393         if(a->hasAttribute("on-solid")) g->setOnSolid(attrb(a,"on-solid"));
394         if(a->hasAttribute("ignored-by-solver")) g->setIgnoreWhileSolving(attrb(a,"ignored-by-solver"));
395         g->setSpringFactorNotPlaning(attrf(a, "spring-factor-not-planing", 1));
396         g->setSpeedPlaning(attrf(a, "speed-planing", 0) * KTS2MPS);
397         g->setReduceFrictionByExtension(attrf(a, "reduce-friction-by-extension", 0));
398         _airplane.addGear(g);
399     } else if(eq(name, "hook")) {
400         Hook* h = new Hook();
401         _currObj = h;
402         v[0] = attrf(a, "x");
403         v[1] = attrf(a, "y");
404         v[2] = attrf(a, "z");
405         h->setPosition(v);
406         float length = attrf(a, "length", 1.0);
407         h->setLength(length);
408         float nrm = length+Math::mag3(v);
409         if (_vehicle_radius < nrm)
410             _vehicle_radius = nrm;
411         h->setDownAngle(attrf(a, "down-angle", 70) * DEG2RAD);
412         h->setUpAngle(attrf(a, "up-angle", 0) * DEG2RAD);
413         _airplane.addHook(h);
414     } else if(eq(name, "launchbar")) {
415         Launchbar* l = new Launchbar();
416         _currObj = l;
417         v[0] = attrf(a, "x");
418         v[1] = attrf(a, "y");
419         v[2] = attrf(a, "z");
420         l->setLaunchbarMount(v);
421         v[0] = attrf(a, "holdback-x", v[0]);
422         v[1] = attrf(a, "holdback-y", v[1]);
423         v[2] = attrf(a, "holdback-z", v[2]);
424         l->setHoldbackMount(v);
425         float length = attrf(a, "length", 1.0);
426         l->setLength(length);
427         l->setDownAngle(attrf(a, "down-angle", 45) * DEG2RAD);
428         l->setUpAngle(attrf(a, "up-angle", -45) * DEG2RAD);
429         l->setHoldbackLength(attrf(a, "holdback-length", 2.0));
430         _airplane.addLaunchbar(l);
431     } else if(eq(name, "fuselage")) {
432         float b[3];
433         v[0] = attrf(a, "ax");
434         v[1] = attrf(a, "ay");
435         v[2] = attrf(a, "az");
436         b[0] = attrf(a, "bx");
437         b[1] = attrf(a, "by");
438         b[2] = attrf(a, "bz");
439         float taper = attrf(a, "taper", 1);
440         float mid = attrf(a, "midpoint", 0.5);
441         if (_airplane.isVersionOrNewer(Version::YASIM_VERSION_32)) {
442             // A fuselage's "midpoint" XML attribute is defined from the
443             // fuselage's front end, but the Fuselage object's internal
444             // "mid" attribute is actually defined from the rear end.
445             // Thus YASim's original interpretation of "midpoint" was wrong.
446             // Complement the "midpoint" value to ensure the fuselage
447             // points the right way.
448             mid = 1 - mid;
449         }
450         float cx = attrf(a, "cx", 1);
451         float cy = attrf(a, "cy", 1);
452         float cz = attrf(a, "cz", 1);
453         float idrag = attrf(a, "idrag", 1);
454         _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid, 
455             cx, cy, cz, idrag);
456     } else if(eq(name, "tank")) {
457         v[0] = attrf(a, "x");
458         v[1] = attrf(a, "y");
459         v[2] = attrf(a, "z");
460         float density = 6.0; // gasoline, in lbs/gal
461         if(a->hasAttribute("jet")) density = 6.72; 
462         density *= LBS2KG*CM2GALS;
463         _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
464     } else if(eq(name, "ballast")) {
465         v[0] = attrf(a, "x");
466         v[1] = attrf(a, "y");
467         v[2] = attrf(a, "z");
468         _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
469     } else if(eq(name, "weight")) {
470         parseWeight(a);
471     } else if(eq(name, "stall")) {
472         Wing* w = (Wing*)_currObj;
473         w->setStall(attrf(a, "aoa") * DEG2RAD);
474         w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
475         w->setStallPeak(attrf(a, "peak", 1.5));
476     } else if(eq(name, "flap0")) {
477         ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
478                                     attrf(a, "lift"), attrf(a, "drag"));
479     } else if(eq(name, "flap1")) {
480         ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
481                                     attrf(a, "lift"), attrf(a, "drag"));
482     } else if(eq(name, "slat")) {
483         ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
484                                    attrf(a, "aoa"), attrf(a, "drag"));
485     } else if(eq(name, "spoiler")) {
486         ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
487                                       attrf(a, "lift"), attrf(a, "drag"));
488     /* } else if(eq(name, "collective")) {
489         ((Rotor*)_currObj)->setcollective(attrf(a, "min"), attrf(a, "max"));
490     } else if(eq(name, "cyclic")) {
491         ((Rotor*)_currObj)->setcyclic(attrf(a, "ail"), attrf(a, "ele"));
492     */                               
493     } else if(eq(name, "actionpt")) {
494         v[0] = attrf(a, "x");
495         v[1] = attrf(a, "y");
496         v[2] = attrf(a, "z");
497         ((Thruster*)_currObj)->setPosition(v);
498     } else if(eq(name, "dir")) {
499         v[0] = attrf(a, "x");
500         v[1] = attrf(a, "y");
501         v[2] = attrf(a, "z");
502         ((Thruster*)_currObj)->setDirection(v);
503     } else if(eq(name, "control-setting")) {
504         // A cruise or approach control setting
505         const char* axis = a->getValue("axis");
506         float value = attrf(a, "value", 0);
507         if(_cruiseCurr)
508             _airplane.addCruiseControl(parseAxis(axis), value);
509         else
510             _airplane.addApproachControl(parseAxis(axis), value);
511     } else if(eq(name, "control-input")) {
512
513         // A mapping of input property to a control
514         int axis = parseAxis(a->getValue("axis"));
515         int control = parseOutput(a->getValue("control"));
516         int opt = 0;
517         opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
518         opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
519         opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
520         
521         ControlMap* cm = _airplane.getControlMap();
522         if(a->hasAttribute("src0")) {
523                            cm->addMapping(axis, control, _currObj, opt,
524                            attrf(a, "src0"), attrf(a, "src1"), 
525                            attrf(a, "dst0"), attrf(a, "dst1"));
526         } else {
527             cm->addMapping(axis, control, _currObj, opt);
528         }
529     } else if(eq(name, "control-output")) {
530         // A property output for a control on the current object
531         ControlMap* cm = _airplane.getControlMap();
532         int type = parseOutput(a->getValue("control"));
533         int handle = cm->getOutputHandle(_currObj, type);
534
535         PropOut* p = new PropOut();
536         p->prop = fgGetNode(a->getValue("prop"), true);
537         p->handle = handle;
538         p->type = type;
539         p->left = !(a->hasAttribute("side") &&
540                         eq("right", a->getValue("side")));
541         p->min = attrf(a, "min", cm->rangeMin(type));
542         p->max = attrf(a, "max", cm->rangeMax(type));
543         _controlProps.add(p);
544
545     } else if(eq(name, "control-speed")) {
546         ControlMap* cm = _airplane.getControlMap();
547         int type = parseOutput(a->getValue("control"));
548         int handle = cm->getOutputHandle(_currObj, type);
549         float time = attrf(a, "transition-time", 0);
550         
551         cm->setTransitionTime(handle, time);
552     } else {
553         SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '"
554                << name << "' found in YASim aircraft description");
555         exit(1);
556     }
557 }
558
559 void FGFDM::getExternalInput(float dt)
560 {
561     char buf[256];
562
563     _turb->setMagnitude(_turb_magnitude_norm->getFloatValue());
564     _turb->update(dt, _turb_rate_hz->getFloatValue());
565
566     // The control axes
567     ControlMap* cm = _airplane.getControlMap();
568     cm->reset();
569
570     for(int i=0; i<_axes.size(); i++) {
571         AxisRec* a = (AxisRec*)_axes.get(i);
572         float val = fgGetFloat(a->name, 0);
573         cm->setInput(a->handle, val);
574     }
575     cm->applyControls(dt);
576
577     // Weights
578     for(int i=0; i<_weights.size(); i++) {
579         WeightRec* wr = (WeightRec*)_weights.get(i);
580         _airplane.setWeight(wr->handle, LBS2KG * fgGetFloat(wr->prop));
581     }
582
583     for(int i=0; i<_thrusters.size(); i++) {
584         EngRec* er = (EngRec*)_thrusters.get(i);
585         Thruster* t = er->eng;
586
587         if(t->getPropEngine()) {
588             PropEngine* p = t->getPropEngine();
589             sprintf(buf, "%s/rpm", er->prefix);
590             p->setOmega(fgGetFloat(buf, 500) * RPM2RAD);
591         }
592     }
593 }
594
595 // Linearly "seeks" a property by the specified fraction of the way to
596 // the target value.  Used to emulate "slowly changing" output values.
597 static void moveprop(SGPropertyNode* node, const char* prop,
598                     float target, float frac)
599 {
600     float val = node->getFloatValue(prop);
601     if(frac > 1) frac = 1;
602     if(frac < 0) frac = 0;
603     val += (target - val) * frac;
604     node->setFloatValue(prop, val);
605 }
606
607 void FGFDM::setOutputProperties(float dt)
608 {
609     float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
610     _gross_weight_lbs->setFloatValue(grossWgt);
611
612     ControlMap* cm = _airplane.getControlMap();
613     for(int i=0; i<_controlProps.size(); i++) {
614         PropOut* p = (PropOut*)_controlProps.get(i);
615         float val = (p->left
616                      ? cm->getOutput(p->handle)
617                      : cm->getOutputR(p->handle));
618         float rmin = cm->rangeMin(p->type);
619         float rmax = cm->rangeMax(p->type);
620         float frac = (val - rmin) / (rmax - rmin);
621         val = frac*(p->max - p->min) + p->min;
622         p->prop->setFloatValue(val);
623     }
624
625     for(int i=0; i<_airplane.getRotorgear()->getNumRotors(); i++) {
626         Rotor*r=(Rotor*)_airplane.getRotorgear()->getRotor(i);
627         int j = 0;
628         float f;
629         char b[256];
630         while((j = r->getValueforFGSet(j, b, &f)))
631             if(b[0]) fgSetFloat(b,f);
632         j=0;
633         while((j = _airplane.getRotorgear()->getValueforFGSet(j, b, &f)))
634             if(b[0]) fgSetFloat(b,f);
635         for(j=0; j < r->numRotorparts(); j+=r->numRotorparts()>>2) {
636             Rotorpart* s = (Rotorpart*)r->getRotorpart(j);
637             char *b;
638             int k;
639             for(k=0; k<2; k++) {
640                 b=s->getAlphaoutput(k);
641                 if(b[0]) fgSetFloat(b, s->getAlpha(k));
642             }
643         }
644     }
645
646     // Use the density of the first tank, or a dummy value if no tanks
647     float fuelDensity = 1.0;
648     if(_airplane.numTanks())
649         fuelDensity = _airplane.getFuelDensity(0);
650     for(int i=0; i<_thrusters.size(); i++) {
651         EngRec* er = (EngRec*)_thrusters.get(i);
652         Thruster* t = er->eng;
653         SGPropertyNode * node = fgGetNode("engines/engine", i, true);
654
655         ThrusterProps& tp = _thrust_props[i];
656
657         // Set: running, cranking, prop-thrust, max-hp, power-pct
658         tp._running->setBoolValue(t->isRunning());
659         tp._cranking->setBoolValue(t->isCranking());
660
661         float tmp[3];
662         t->getThrust(tmp);
663         float lbs = Math::mag3(tmp) * (KG2LBS/9.8);
664         tp._prop_thrust->setFloatValue(lbs); // Deprecated name
665         tp._thrust_lbs->setFloatValue(lbs);
666         tp._fuel_flow_gph->setFloatValue(
667                 (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
668
669         if(t->getPropEngine()) {
670             PropEngine* p = t->getPropEngine();
671             tp._rpm->setFloatValue(p->getOmega() * (1/RPM2RAD));
672             tp._torque_ftlb->setFloatValue(
673                     p->getEngine()->getTorque() * NM2FTLB);
674
675             if(p->getEngine()->isPistonEngine()) {
676                 PistonEngine* pe = p->getEngine()->isPistonEngine();
677                 tp._mp_osi->setFloatValue(pe->getMP() * (1/INHG2PA));
678                 tp._mp_inhg->setFloatValue(pe->getMP() * (1/INHG2PA));
679                 tp._egt_degf->setFloatValue(
680                         pe->getEGT() * K2DEGF + K2DEGFOFFSET);
681                 tp._oil_temperature_degf->setFloatValue(
682                         pe->getOilTemp() * K2DEGF + K2DEGFOFFSET);
683                 tp._boost_gauge_inhg->setFloatValue(
684                         pe->getBoost() * (1/INHG2PA));
685             } else if(p->getEngine()->isTurbineEngine()) {
686                 TurbineEngine* te = p->getEngine()->isTurbineEngine();
687                 tp._n2->setFloatValue(te->getN2());
688             }
689         }
690
691         if(t->getJet()) {
692             Jet* j = t->getJet();
693             tp._n1->setFloatValue(j->getN1());
694             tp._n2->setFloatValue(j->getN2());
695             tp._epr->setFloatValue(j->getEPR());
696             tp._egt_degf->setFloatValue(
697                     j->getEGT() * K2DEGF + K2DEGFOFFSET);
698
699             // These are "unmodeled" values that are still needed for
700             // many cockpits.  Tie them all to the N1 speed, but
701             // normalize the numbers to the range [0:1] so the
702             // cockpit code can scale them to the right values.
703             float pnorm = j->getPerfNorm();
704             moveprop(node, "oilp-norm", pnorm, dt/3); // 3s seek time
705             moveprop(node, "oilt-norm", pnorm, dt/30); // 30s 
706             moveprop(node, "itt-norm", pnorm, dt/1); // 1s
707         }
708     }
709 }
710
711 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type, Version * version)
712 {
713     Wing* w = new Wing(version);
714
715     float defDihed = 0;
716     if(eq(type, "vstab"))
717         defDihed = 90;
718     else
719         w->setMirror(true);
720
721     float pos[3];
722     pos[0] = attrf(a, "x");
723     pos[1] = attrf(a, "y");
724     pos[2] = attrf(a, "z");
725     w->setBase(pos);
726
727     w->setLength(attrf(a, "length"));
728     w->setChord(attrf(a, "chord"));
729     w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
730     w->setTaper(attrf(a, "taper", 1));
731     w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
732     w->setCamber(attrf(a, "camber", 0));
733
734     // These come in with positive indicating positive AoA, but the
735     // internals expect a rotation about the left-pointing Y axis, so
736     // invert the sign.
737     w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD * -1);
738     w->setTwist(attrf(a, "twist", 0) * DEG2RAD * -1);
739
740     // The 70% is a magic number that sorta kinda seems to match known
741     // throttle settings to approach speed.
742     w->setInducedDrag(0.7*attrf(a, "idrag", 1));
743
744     float effect = attrf(a, "effectiveness", 1);
745     w->setDragScale(w->getDragScale()*effect);
746
747     _currObj = w;
748     return w;
749 }
750
751 Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
752 {
753     Rotor* w = new Rotor();
754
755     // float defDihed = 0;
756
757     float pos[3];
758     pos[0] = attrf(a, "x");
759     pos[1] = attrf(a, "y");
760     pos[2] = attrf(a, "z");
761     w->setBase(pos);
762
763     float normal[3];
764     normal[0] = attrf(a, "nx");
765     normal[1] = attrf(a, "ny");
766     normal[2] = attrf(a, "nz");
767     w->setNormal(normal);
768
769     float forward[3];
770     forward[0] = attrf(a, "fx");
771     forward[1] = attrf(a, "fy");
772     forward[2] = attrf(a, "fz");
773     w->setForward(forward);
774
775     w->setMaxCyclicail(attrf(a, "maxcyclicail", 7.6));
776     w->setMaxCyclicele(attrf(a, "maxcyclicele", 4.94));
777     w->setMinCyclicail(attrf(a, "mincyclicail", -7.6));
778     w->setMinCyclicele(attrf(a, "mincyclicele", -4.94));
779     w->setMaxCollective(attrf(a, "maxcollective", 15.8));
780     w->setMinCollective(attrf(a, "mincollective", -0.2));
781     w->setDiameter(attrf(a, "diameter", 10.2));
782     w->setWeightPerBlade(attrf(a, "weightperblade", 44));
783     w->setNumberOfBlades(attrf(a, "numblades", 4));
784     w->setRelBladeCenter(attrf(a, "relbladecenter", 0.7));
785     w->setDynamic(attrf(a, "dynamic", 0.7));
786     w->setDelta3(attrf(a, "delta3", 0));
787     w->setDelta(attrf(a, "delta", 0));
788     w->setTranslift(attrf(a, "translift", 0.05));
789     w->setC2(attrf(a, "dragfactor", 1));
790     w->setStepspersecond(attrf(a, "stepspersecond", 120));
791     w->setPhiNull((attrf(a, "phi0", 0))*YASIM_PI/180);
792     w->setRPM(attrf(a, "rpm", 424));
793     w->setRelLenHinge(attrf(a, "rellenflaphinge", 0.07));
794     w->setAlpha0((attrf(a, "flap0", -5))*YASIM_PI/180);
795     w->setAlphamin((attrf(a, "flapmin", -15))/180*YASIM_PI);
796     w->setAlphamax((attrf(a, "flapmax",  15))*YASIM_PI/180);
797     w->setAlpha0factor(attrf(a, "flap0factor", 1));
798     w->setTeeterdamp(attrf(a,"teeterdamp",.0001));
799     w->setMaxteeterdamp(attrf(a,"maxteeterdamp",1000));
800     w->setRelLenTeeterHinge(attrf(a,"rellenteeterhinge",0.01));
801     w->setBalance(attrf(a,"balance",1.0));
802     w->setMinTiltYaw(attrf(a,"mintiltyaw",0.0));
803     w->setMinTiltPitch(attrf(a,"mintiltpitch",0.0));
804     w->setMinTiltRoll(attrf(a,"mintiltroll",0.0));
805     w->setMaxTiltYaw(attrf(a,"maxtiltyaw",0.0));
806     w->setMaxTiltPitch(attrf(a,"maxtiltpitch",0.0));
807     w->setMaxTiltRoll(attrf(a,"maxtiltroll",0.0));
808     w->setTiltCenterX(attrf(a,"tiltcenterx",0.0));
809     w->setTiltCenterY(attrf(a,"tiltcentery",0.0));
810     w->setTiltCenterZ(attrf(a,"tiltcenterz",0.0));
811     w->setDownwashFactor(attrf(a, "downwashfactor", 1));
812     if(attrb(a,"ccw"))
813        w->setCcw(1); 
814     if(attrb(a,"sharedflaphinge"))
815        w->setSharedFlapHinge(true); 
816
817     if(a->hasAttribute("name"))
818        w->setName(a->getValue("name") );
819     if(a->hasAttribute("alphaout0"))
820        w->setAlphaoutput(0,a->getValue("alphaout0") );
821     if(a->hasAttribute("alphaout1"))  w->setAlphaoutput(1,a->getValue("alphaout1") );
822     if(a->hasAttribute("alphaout2"))  w->setAlphaoutput(2,a->getValue("alphaout2") );
823     if(a->hasAttribute("alphaout3"))  w->setAlphaoutput(3,a->getValue("alphaout3") );
824     if(a->hasAttribute("coneout"))  w->setAlphaoutput(4,a->getValue("coneout") );
825     if(a->hasAttribute("yawout"))   w->setAlphaoutput(5,a->getValue("yawout") );
826     if(a->hasAttribute("rollout"))  w->setAlphaoutput(6,a->getValue("rollout") );
827
828     w->setPitchA(attrf(a, "pitch-a", 10));
829     w->setPitchB(attrf(a, "pitch-b", 10));
830     w->setForceAtPitchA(attrf(a, "forceatpitch-a", 3000));
831     w->setPowerAtPitch0(attrf(a, "poweratpitch-0", 300));
832     w->setPowerAtPitchB(attrf(a, "poweratpitch-b", 3000));
833     if(attrb(a,"notorque"))
834        w->setNotorque(1); 
835
836 #define p(x) if (a->hasAttribute(#x)) w->setParameter((char *)#x,attrf(a,#x) );
837 #define p2(x,y) if (a->hasAttribute(y)) w->setParameter((char *)#x,attrf(a,y) );
838     p2(translift_ve,"translift-ve")
839     p2(translift_maxfactor,"translift-maxfactor")
840     p2(ground_effect_constant,"ground-effect-constant")
841     p2(vortex_state_lift_factor,"vortex-state-lift-factor")
842     p2(vortex_state_c1,"vortex-state-c1")
843     p2(vortex_state_c2,"vortex-state-c2")
844     p2(vortex_state_c3,"vortex-state_c3")
845     p2(vortex_state_e1,"vortex-state-e1")
846     p2(vortex_state_e2,"vortex-state-e2")
847     p(twist)
848     p2(number_of_segments,"number-of-segments")
849     p2(number_of_parts,"number-of-parts")
850     p2(rel_len_where_incidence_is_measured,"rel-len-where-incidence-is-measured")
851     p(chord)
852     p(taper)
853     p2(airfoil_incidence_no_lift,"airfoil-incidence-no-lift")
854     p2(rel_len_blade_start,"rel-len-blade-start")
855     p2(incidence_stall_zero_speed,"incidence-stall-zero-speed")
856     p2(incidence_stall_half_sonic_speed,"incidence-stall-half-sonic-speed")
857     p2(lift_factor_stall,"lift-factor-stall")
858     p2(stall_change_over,"stall-change-over")
859     p2(drag_factor_stall,"drag-factor-stall")
860     p2(airfoil_lift_coefficient,"airfoil-lift-coefficient")
861     p2(airfoil_drag_coefficient0,"airfoil-drag-coefficient0")
862     p2(airfoil_drag_coefficient1,"airfoil-drag-coefficient1")
863     p2(cyclic_factor,"cyclic-factor")
864     p2(rotor_correction_factor,"rotor-correction-factor")
865 #undef p
866 #undef p2
867     _currObj = w;
868     return w;
869 }
870
871 void FGFDM::parsePistonEngine(XMLAttributes* a)
872 {
873     float engP = attrf(a, "eng-power") * HP2W;
874     float engS = attrf(a, "eng-rpm") * RPM2RAD;
875
876     PistonEngine* eng = new PistonEngine(engP, engS);
877
878     if(a->hasAttribute("displacement"))
879         eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
880
881     if(a->hasAttribute("compression"))
882         eng->setCompression(attrf(a, "compression"));        
883
884     if(a->hasAttribute("min-throttle"))
885         eng->setMinThrottle(attrf(a, "min-throttle"));
886
887     if(a->hasAttribute("turbo-mul")) {
888         float mul = attrf(a, "turbo-mul");
889         float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
890         eng->setTurboParams(mul, mp);
891         eng->setTurboLag(attrf(a, "turbo-lag", 2));
892     }
893
894     if(a->hasAttribute("supercharger"))
895         eng->setSupercharger(attrb(a, "supercharger"));
896
897     ((PropEngine*)_currObj)->setEngine(eng);
898 }
899
900 void FGFDM::parseTurbineEngine(XMLAttributes* a)
901 {
902     float power = attrf(a, "eng-power") * HP2W;
903     float omega = attrf(a, "eng-rpm") * RPM2RAD;
904     float alt = attrf(a, "alt") * FT2M;
905     float flatRating = attrf(a, "flat-rating") * HP2W;
906     TurbineEngine* eng = new TurbineEngine(power, omega, alt, flatRating);
907
908     if(a->hasAttribute("n2-low-idle"))
909         eng->setN2Range(attrf(a, "n2-low-idle"), attrf(a, "n2-high-idle"),
910                         attrf(a, "n2-max"));
911
912     // Nasty units conversion: lbs/hr per hp -> kg/s per watt
913     if(a->hasAttribute("bsfc"))
914         eng->setFuelConsumption(attrf(a, "bsfc") * (LBS2KG/(3600*HP2W)));
915
916     ((PropEngine*)_currObj)->setEngine(eng);
917 }
918
919 void FGFDM::parsePropeller(XMLAttributes* a)
920 {
921     // Legacy Handling for the old engines syntax:
922     PistonEngine* eng = 0;
923     if(a->hasAttribute("eng-power")) {
924         SG_LOG(SG_FLIGHT,SG_ALERT, "WARNING: "
925                << "Legacy engine definition in YASim configuration file.  "
926                << "Please fix.");
927         float engP = attrf(a, "eng-power") * HP2W;
928         float engS = attrf(a, "eng-rpm") * RPM2RAD;
929         eng = new PistonEngine(engP, engS);
930         if(a->hasAttribute("displacement"))
931             eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
932         if(a->hasAttribute("compression"))
933             eng->setCompression(attrf(a, "compression"));        
934         if(a->hasAttribute("turbo-mul")) {
935             float mul = attrf(a, "turbo-mul");
936             float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
937             eng->setTurboParams(mul, mp);
938         }
939     }
940
941     // Now parse the actual propeller definition:
942     float cg[3];
943     cg[0] = attrf(a, "x");
944     cg[1] = attrf(a, "y");
945     cg[2] = attrf(a, "z");
946     float mass = attrf(a, "mass") * LBS2KG;
947     float moment = attrf(a, "moment");
948     float radius = attrf(a, "radius");
949     float speed = attrf(a, "cruise-speed") * KTS2MPS;
950     float omega = attrf(a, "cruise-rpm") * RPM2RAD;
951     float power = attrf(a, "cruise-power") * HP2W;
952     float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
953
954     Propeller* prop = new Propeller(radius, speed, omega, rho, power);
955     PropEngine* thruster = new PropEngine(prop, eng, moment);
956     _airplane.addThruster(thruster, mass, cg);
957
958     // Set the stops (fine = minimum pitch, coarse = maximum pitch)
959     float fine_stop = attrf(a, "fine-stop", 0.25f);
960     float coarse_stop = attrf(a, "coarse-stop", 4.0f);
961     prop->setStops(fine_stop, coarse_stop);
962
963     if(a->hasAttribute("takeoff-power")) {
964         float power0 = attrf(a, "takeoff-power") * HP2W;
965         float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
966         prop->setTakeoff(omega0, power0);
967     }
968
969     if(a->hasAttribute("max-rpm")) {
970         float max = attrf(a, "max-rpm") * RPM2RAD;
971         float min = attrf(a, "min-rpm") * RPM2RAD;
972         thruster->setVariableProp(min, max);
973     }
974
975     if(attrb(a, "contra"))
976         thruster->setContraPair(true);
977
978     if(a->hasAttribute("manual-pitch")) {
979         prop->setManualPitch();
980     }
981
982     thruster->setGearRatio(attrf(a, "gear-ratio", 1));
983
984     char buf[64];
985     sprintf(buf, "/engines/engine[%d]", _nextEngine++);
986     EngRec* er = new EngRec();
987     er->eng = thruster;
988     er->prefix = dup(buf);
989     _thrusters.add(er);
990
991     _currObj = thruster;
992 }
993
994 // Turns a string axis name into an integer for use by the
995 // ControlMap.  Creates a new axis if this one hasn't been defined
996 // yet.
997 int FGFDM::parseAxis(const char* name)
998 {
999     for(int i=0; i<_axes.size(); i++) {
1000         AxisRec* a = (AxisRec*)_axes.get(i);
1001         if(eq(a->name, name))
1002             return a->handle;
1003     }
1004
1005     // Not there, make a new one.
1006     AxisRec* a = new AxisRec();
1007     a->name = dup(name);
1008     fgGetNode( a->name, true ); // make sure the property name exists
1009     a->handle = _airplane.getControlMap()->newInput();
1010     _axes.add(a);
1011     return a->handle;
1012 }
1013
1014 int FGFDM::parseOutput(const char* name)
1015 {
1016     if(eq(name, "THROTTLE"))  return ControlMap::THROTTLE;
1017     if(eq(name, "MIXTURE"))   return ControlMap::MIXTURE;
1018     if(eq(name, "CONDLEVER")) return ControlMap::CONDLEVER;
1019     if(eq(name, "STARTER"))   return ControlMap::STARTER;
1020     if(eq(name, "MAGNETOS"))  return ControlMap::MAGNETOS;
1021     if(eq(name, "ADVANCE"))   return ControlMap::ADVANCE;
1022     if(eq(name, "REHEAT"))    return ControlMap::REHEAT;
1023     if(eq(name, "BOOST"))     return ControlMap::BOOST;
1024     if(eq(name, "VECTOR"))    return ControlMap::VECTOR;
1025     if(eq(name, "PROP"))      return ControlMap::PROP;
1026     if(eq(name, "BRAKE"))     return ControlMap::BRAKE;
1027     if(eq(name, "STEER"))     return ControlMap::STEER;
1028     if(eq(name, "EXTEND"))    return ControlMap::EXTEND;
1029     if(eq(name, "HEXTEND"))   return ControlMap::HEXTEND;
1030     if(eq(name, "LEXTEND"))   return ControlMap::LEXTEND;
1031         if(eq(name, "LACCEL"))    return ControlMap::LACCEL;
1032     if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
1033     if(eq(name, "FLAP0"))     return ControlMap::FLAP0;
1034     if(eq(name, "FLAP0EFFECTIVENESS"))   return ControlMap::FLAP0EFFECTIVENESS;
1035     if(eq(name, "FLAP1"))     return ControlMap::FLAP1;
1036     if(eq(name, "FLAP1EFFECTIVENESS"))   return ControlMap::FLAP1EFFECTIVENESS;
1037     if(eq(name, "SLAT"))      return ControlMap::SLAT;
1038     if(eq(name, "SPOILER"))   return ControlMap::SPOILER;
1039     if(eq(name, "CASTERING")) return ControlMap::CASTERING;
1040     if(eq(name, "PROPPITCH")) return ControlMap::PROPPITCH;
1041     if(eq(name, "PROPFEATHER")) return ControlMap::PROPFEATHER;
1042     if(eq(name, "COLLECTIVE")) return ControlMap::COLLECTIVE;
1043     if(eq(name, "CYCLICAIL")) return ControlMap::CYCLICAIL;
1044     if(eq(name, "CYCLICELE")) return ControlMap::CYCLICELE;
1045     if(eq(name, "TILTROLL")) return ControlMap::TILTROLL;
1046     if(eq(name, "TILTPITCH")) return ControlMap::TILTPITCH;
1047     if(eq(name, "TILTYAW")) return ControlMap::TILTYAW;
1048     if(eq(name, "ROTORGEARENGINEON")) return ControlMap::ROTORENGINEON;
1049     if(eq(name, "ROTORBRAKE")) return ControlMap::ROTORBRAKE;
1050     if(eq(name, "ROTORENGINEMAXRELTORQUE")) 
1051         return ControlMap::ROTORENGINEMAXRELTORQUE;
1052     if(eq(name, "ROTORRELTARGET")) return ControlMap::ROTORRELTARGET;
1053     if(eq(name, "ROTORBALANCE")) return ControlMap::ROTORBALANCE;
1054     if(eq(name, "REVERSE_THRUST")) return ControlMap::REVERSE_THRUST;
1055     if(eq(name, "WASTEGATE")) return ControlMap::WASTEGATE;
1056     if(eq(name, "WINCHRELSPEED")) return ControlMap::WINCHRELSPEED;
1057     if(eq(name, "HITCHOPEN")) return ControlMap::HITCHOPEN;
1058     if(eq(name, "PLACEWINCH")) return ControlMap::PLACEWINCH;
1059     if(eq(name, "FINDAITOW")) return ControlMap::FINDAITOW;
1060
1061     SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
1062            << name << "' in YASim aircraft description.");
1063     exit(1);
1064
1065 }
1066
1067 void FGFDM::parseWeight(XMLAttributes* a)
1068 {
1069     WeightRec* wr = new WeightRec();
1070
1071     float v[3];
1072     v[0] = attrf(a, "x");
1073     v[1] = attrf(a, "y");
1074     v[2] = attrf(a, "z");
1075
1076     wr->prop = dup(a->getValue("mass-prop"));
1077     wr->size = attrf(a, "size", 0);
1078     wr->handle = _airplane.addWeight(v, wr->size);
1079
1080     _weights.add(wr);
1081 }
1082
1083 bool FGFDM::eq(const char* a, const char* b)
1084 {
1085     // Figure it out for yourself. :)
1086     while(*a && *b && *a == *b) { a++; b++; }
1087     return !(*a || *b);
1088 }
1089
1090 char* FGFDM::dup(const char* s)
1091 {
1092     int len=0;
1093     while(s[len++]);
1094     char* s2 = new char[len+1];
1095     char* p = s2;
1096     while((*p++ = *s++));
1097     s2[len] = 0;
1098     return s2;
1099 }
1100
1101 int FGFDM::attri(XMLAttributes* atts, const char* attr)
1102 {
1103     if(!atts->hasAttribute(attr)) {
1104         SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1105                "' in YASim aircraft description");
1106         exit(1);
1107     }
1108     return attri(atts, attr, 0);
1109 }
1110
1111 int FGFDM::attri(XMLAttributes* atts, const char* attr, int def)
1112 {
1113     const char* val = atts->getValue(attr);
1114     if(val == 0) return def;
1115     else         return atol(val);
1116 }
1117
1118 float FGFDM::attrf(XMLAttributes* atts, const char* attr)
1119 {
1120     if(!atts->hasAttribute(attr)) {
1121         SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1122                "' in YASim aircraft description");
1123         exit(1);
1124     }
1125     return attrf(atts, attr, 0);
1126 }
1127
1128 float FGFDM::attrf(XMLAttributes* atts, const char* attr, float def)
1129 {
1130     const char* val = atts->getValue(attr);
1131     if(val == 0) return def;
1132     else         return (float)atof(val);    
1133 }
1134
1135 double FGFDM::attrd(XMLAttributes* atts, const char* attr)
1136 {
1137     if(!atts->hasAttribute(attr)) {
1138         SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1139                "' in YASim aircraft description");
1140         exit(1);
1141     }
1142     return attrd(atts, attr, 0);
1143 }
1144
1145 double FGFDM::attrd(XMLAttributes* atts, const char* attr, double def)
1146 {
1147     const char* val = atts->getValue(attr);
1148     if(val == 0) return def;
1149     else         return atof(val);
1150 }
1151
1152 // ACK: the dreaded ambiguous string boolean.  Remind me to shoot Maik
1153 // when I have a chance. :).  Unless you have a parser that can check
1154 // symbol constants (we don't), this kind of coding is just a Bad
1155 // Idea.  This implementation, for example, silently returns a boolean
1156 // falsehood for values of "1", "yes", "True", and "TRUE".  Which is
1157 // especially annoying preexisting boolean attributes in the same
1158 // parser want to see "1" and will choke on a "true"...
1159 //
1160 // Unfortunately, this usage creeped into existing configuration files
1161 // while I wasn't active, and it's going to be hard to remove.  Issue
1162 // a warning to nag people into changing their ways for now...
1163 bool FGFDM::attrb(XMLAttributes* atts, const char* attr)
1164 {
1165     const char* val = atts->getValue(attr);
1166     if(val == 0) return false;
1167
1168     if(eq(val,"true")) {
1169         SG_LOG(SG_FLIGHT, SG_ALERT, "Warning: " <<
1170                "deprecated 'true' boolean in YASim configuration file.  " <<
1171                "Use numeric booleans (attribute=\"1\") instead");
1172         return true;
1173     }
1174     return attri(atts, attr, 0) ? true : false;
1175 }
1176
1177 }; // namespace yasim