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