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