]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/FGFDM.cpp
abe8843513fc500565dcfff9f10bc1abba7ca33d
[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     char buf[256];
312
313     // The control axes
314     ControlMap* cm = _airplane.getControlMap();
315     cm->reset();
316     int i;
317     for(i=0; i<_axes.size(); i++) {
318         AxisRec* a = (AxisRec*)_axes.get(i);
319         float val = fgGetFloat(a->name, 0);
320         cm->setInput(a->handle, val);
321     }
322     cm->applyControls(dt);
323
324     // Weights
325     for(i=0; i<_weights.size(); i++) {
326         WeightRec* wr = (WeightRec*)_weights.get(i);
327         _airplane.setWeight(wr->handle, LBS2KG * fgGetFloat(wr->prop));
328     }
329
330     for(i=0; i<_thrusters.size(); i++) {
331         EngRec* er = (EngRec*)_thrusters.get(i);
332         Thruster* t = er->eng;
333
334         if(t->getPropEngine()) {
335             PropEngine* p = t->getPropEngine();
336             sprintf(buf, "%s/rpm", er->prefix);
337             p->setOmega(fgGetFloat(buf) * RPM2RAD);
338         }
339     }
340 }
341
342 void FGFDM::setOutputProperties()
343 {
344     char buf[256];
345     int i;
346
347     float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
348     fgSetFloat("/yasim/gross-weight-lbs", grossWgt);
349
350     ControlMap* cm = _airplane.getControlMap();
351     for(i=0; i<_controlProps.size(); i++) {
352         PropOut* p = (PropOut*)_controlProps.get(i);
353         float val = (p->left
354                      ? cm->getOutput(p->handle)
355                      : cm->getOutputR(p->handle));
356         float rmin = cm->rangeMin(p->type);
357         float rmax = cm->rangeMax(p->type);
358         float frac = (val - rmin) / (rmax - rmin);
359         val = frac*(p->max - p->min) + p->min;
360         p->prop->setFloatValue(val);
361     }
362
363     float totalFuel = 0, totalCap = 0;
364     float fuelDensity = 720; // in kg/m^3, default to gasoline: ~6 lb/gal
365     for(i=0; i<_airplane.numTanks(); i++) {
366         fuelDensity = _airplane.getFuelDensity(i);
367         sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
368         fgSetFloat(buf, CM2GALS*_airplane.getFuel(i)/fuelDensity);
369         sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
370         fgSetFloat(buf, KG2LBS*_airplane.getFuel(i));
371         totalFuel += _airplane.getFuel(i);
372         totalCap += _airplane.getTankCapacity(i);
373     }
374     if(totalCap != 0) {
375         fgSetFloat("/consumables/fuel/total-fuel-lbs", KG2LBS*totalFuel);
376         fgSetFloat("/consumables/fuel/total-fuel-gals",
377                    CM2GALS*totalFuel/fuelDensity);
378         fgSetFloat("/consumables/fuel/total-fuel-norm", totalFuel/totalCap);
379     }
380
381     for(i=0; i<_airplane.getNumRotors(); i++) {
382         Rotor*r=(Rotor*)_airplane.getRotor(i);
383         int j=0;
384         float f;
385         char b[256];
386         while(j=r->getValueforFGSet(j,b,&f))
387         {
388               if (b[0])
389               {
390                  fgSetFloat(b,f);
391               }
392         }
393         
394         for(j=0; j<r->numRotorparts(); j++) {
395             Rotorpart* s = (Rotorpart*)r->getRotorpart(j);
396             char *b;
397             int k;
398             for (k=0;k<2;k++)
399             {
400               b=s->getAlphaoutput(k);
401               if (b[0])
402               {
403                  fgSetFloat(b,s->getAlpha(k));
404                  //printf("setting [%s]\n",b);
405               }
406             }
407         }
408         for(j=0; j<r->numRotorblades(); j++) {
409             Rotorblade* s = (Rotorblade*)r->getRotorblade(j);
410             char *b;
411             int k;
412             for (k=0;k<2;k++)
413             {
414               b=s->getAlphaoutput(k);
415               if (b[0])
416               {
417                  fgSetFloat(b,s->getAlpha(k));
418               }
419             }
420         }
421      }
422
423
424     for(i=0; i<_thrusters.size(); i++) {
425         EngRec* er = (EngRec*)_thrusters.get(i);
426         Thruster* t = er->eng;
427
428         sprintf(buf, "%s/fuel-flow-gph", er->prefix);
429         fgSetFloat(buf, (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
430
431         if(t->getPropEngine()) {
432             PropEngine* p = t->getPropEngine();
433
434             sprintf(buf, "%s/rpm", er->prefix);
435             fgSetFloat(buf, p->getOmega() / RPM2RAD);
436         }
437
438         if(t->getPistonEngine()) {
439             PistonEngine* p = t->getPistonEngine();
440             
441             sprintf(buf, "%s/mp-osi", er->prefix);
442             fgSetFloat(buf, p->getMP() * (1/INHG2PA));
443
444             sprintf(buf, "%s/egt-degf", er->prefix);
445             fgSetFloat(buf, p->getEGT() * K2DEGF + K2DEGFOFFSET);
446         }
447
448         if(t->getJet()) {
449             Jet* j = t->getJet();
450
451             sprintf(buf, "%s/n1", er->prefix);
452             fgSetFloat(buf, j->getN1());
453
454             sprintf(buf, "%s/n2", er->prefix);
455             fgSetFloat(buf, j->getN2());
456
457             sprintf(buf, "%s/epr", er->prefix);
458             fgSetFloat(buf, j->getEPR());
459
460             sprintf(buf, "%s/egt-degf", er->prefix);
461             fgSetFloat(buf, j->getEGT() * K2DEGF + K2DEGFOFFSET);
462         }
463     }
464 }
465
466 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
467 {
468     Wing* w = new Wing();
469
470     float defDihed = 0;
471     if(eq(type, "vstab"))
472         defDihed = 90;
473     else
474         w->setMirror(true);
475
476     float pos[3];
477     pos[0] = attrf(a, "x");
478     pos[1] = attrf(a, "y");
479     pos[2] = attrf(a, "z");
480     w->setBase(pos);
481
482     w->setLength(attrf(a, "length"));
483     w->setChord(attrf(a, "chord"));
484     w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
485     w->setTaper(attrf(a, "taper", 1));
486     w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
487     w->setCamber(attrf(a, "camber", 0));
488     w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
489     w->setTwist(attrf(a, "twist", 0) * DEG2RAD);
490
491     // The 70% is a magic number that sorta kinda seems to match known
492     // throttle settings to approach speed.
493     w->setInducedDrag(0.7*attrf(a, "idrag", 1));
494
495     float effect = attrf(a, "effectiveness", 1);
496     w->setDragScale(w->getDragScale()*effect);
497
498     _currObj = w;
499     return w;
500 }
501 Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
502 {
503     Rotor* w = new Rotor();
504
505     float defDihed = 0;
506
507     float pos[3];
508     pos[0] = attrf(a, "x");
509     pos[1] = attrf(a, "y");
510     pos[2] = attrf(a, "z");
511     w->setBase(pos);
512
513     float normal[3];
514     normal[0] = attrf(a, "nx");
515     normal[1] = attrf(a, "ny");
516     normal[2] = attrf(a, "nz");
517     w->setNormal(normal);
518
519     float forward[3];
520     forward[0] = attrf(a, "fx");
521     forward[1] = attrf(a, "fy");
522     forward[2] = attrf(a, "fz");
523     w->setForward(forward);
524
525
526
527     w->setMaxCyclicail(attrf(a, "maxcyclicail", 7.6));
528     w->setMaxCyclicele(attrf(a, "maxcyclicele", 4.94));
529     w->setMinCyclicail(attrf(a, "mincyclicail", -7.6));
530     w->setMinCyclicele(attrf(a, "mincyclicele", -4.94));
531     w->setMaxCollective(attrf(a, "maxcollective", 15.8));
532     w->setMinCollective(attrf(a, "mincollective", -0.2));
533     w->setDiameter(attrf(a, "diameter", 10.2));
534     w->setWeightPerBlade(attrf(a, "weightperblade", 44));
535     w->setNumberOfBlades(attrf(a, "numblades", 4));
536     w->setRelBladeCenter(attrf(a, "relbladecenter", 0.7));
537     w->setDynamic(attrf(a, "dynamic", 0.7));
538     w->setDelta3(attrf(a, "delta3", 0));
539     w->setDelta(attrf(a, "delta", 0));
540     w->setTranslift(attrf(a, "translift", 0.05));
541     w->setC2(attrf(a, "dragfactor", 1));
542     w->setStepspersecond(attrf(a, "stepspersecond", 120));
543     w->setRPM(attrf(a, "rpm", 424));
544     w->setRelLenHinge(attrf(a, "rellenflaphinge", 0.07));
545     w->setAlpha0((attrf(a, "flap0", -5))*YASIM_PI/180);
546     w->setAlphamin((attrf(a, "flapmin", -15))/180*YASIM_PI);
547     w->setAlphamax((attrf(a, "flapmax",  15))*YASIM_PI/180);
548     w->setAlpha0factor(attrf(a, "flap0factor", 1));
549     w->setTeeterdamp(attrf(a,"teeterdamp",.0001));
550     w->setMaxteeterdamp(attrf(a,"maxteeterdamp",1000));
551     w->setRelLenTeeterHinge(attrf(a,"rellenteeterhinge",0.01));
552     void setAlphamin(float f);
553     void setAlphamax(float f);
554     void setAlpha0factor(float f);
555
556     if(attristrue(a,"ccw"))
557        w->setCcw(1); 
558     
559     if(a->hasAttribute("name"))
560        w->setName(a->getValue("name") );
561     if(a->hasAttribute("alphaout0"))
562        w->setAlphaoutput(0,a->getValue("alphaout0") );
563     if(a->hasAttribute("alphaout1"))  w->setAlphaoutput(1,a->getValue("alphaout1") );
564     if(a->hasAttribute("alphaout2"))  w->setAlphaoutput(2,a->getValue("alphaout2") );
565     if(a->hasAttribute("alphaout3"))  w->setAlphaoutput(3,a->getValue("alphaout3") );
566     if(a->hasAttribute("coneout"))  w->setAlphaoutput(4,a->getValue("coneout") );
567     if(a->hasAttribute("yawout"))   w->setAlphaoutput(5,a->getValue("yawout") );
568     if(a->hasAttribute("rollout"))  w->setAlphaoutput(6,a->getValue("rollout") );
569
570     w->setPitchA(attrf(a, "pitch_a", 10));
571     w->setPitchB(attrf(a, "pitch_b", 10));
572     w->setForceAtPitchA(attrf(a, "forceatpitch_a", 3000));
573     w->setPowerAtPitch0(attrf(a, "poweratpitch_0", 300));
574     w->setPowerAtPitchB(attrf(a, "poweratpitch_b", 3000));
575     if(attristrue(a,"notorque"))
576        w->setNotorque(1); 
577     if(attristrue(a,"simblades"))
578        w->setSimBlades(1); 
579
580
581
582
583
584     _currObj = w;
585     return w;
586 }
587
588 void FGFDM::parsePropeller(XMLAttributes* a)
589 {
590     float cg[3];
591     cg[0] = attrf(a, "x");
592     cg[1] = attrf(a, "y");
593     cg[2] = attrf(a, "z");
594     float mass = attrf(a, "mass") * LBS2KG;
595     float moment = attrf(a, "moment");
596     float radius = attrf(a, "radius");
597     float speed = attrf(a, "cruise-speed") * KTS2MPS;
598     float omega = attrf(a, "cruise-rpm") * RPM2RAD;
599     float power = attrf(a, "cruise-power") * HP2W;
600     float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
601
602     // Hack, fix this pronto:
603     float engP = attrf(a, "eng-power") * HP2W;
604     float engS = attrf(a, "eng-rpm") * RPM2RAD;
605
606     Propeller* prop = new Propeller(radius, speed, omega, rho, power);
607     PistonEngine* eng = new PistonEngine(engP, engS);
608     PropEngine* thruster = new PropEngine(prop, eng, moment);
609     _airplane.addThruster(thruster, mass, cg);
610
611     if(a->hasAttribute("displacement"))
612         eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
613
614     if(a->hasAttribute("compression"))
615         eng->setCompression(attrf(a, "compression"));        
616
617     if(a->hasAttribute("turbo-mul")) {
618         float mul = attrf(a, "turbo-mul");
619         float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
620         eng->setTurboParams(mul, mp);
621     }
622
623     if(a->hasAttribute("takeoff-power")) {
624         float power0 = attrf(a, "takeoff-power") * HP2W;
625         float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
626         prop->setTakeoff(omega0, power0);
627     }
628
629     if(a->hasAttribute("max-rpm")) {
630         float max = attrf(a, "max-rpm") * RPM2RAD;
631         float min = attrf(a, "min-rpm") * RPM2RAD;
632         thruster->setVariableProp(min, max);
633     }
634
635     if(a->hasAttribute("manual-pitch")) {
636         prop->setManualPitch();
637     }
638
639     char buf[64];
640     sprintf(buf, "/engines/engine[%d]", _nextEngine++);
641     EngRec* er = new EngRec();
642     er->eng = thruster;
643     er->prefix = dup(buf);
644     _thrusters.add(er);
645
646     _currObj = thruster;
647 }
648
649 // Turns a string axis name into an integer for use by the
650 // ControlMap.  Creates a new axis if this one hasn't been defined
651 // yet.
652 int FGFDM::parseAxis(const char* name)
653 {
654     int i;
655     for(i=0; i<_axes.size(); i++) {
656         AxisRec* a = (AxisRec*)_axes.get(i);
657         if(eq(a->name, name))
658             return a->handle;
659     }
660
661     // Not there, make a new one.
662     AxisRec* a = new AxisRec();
663     a->name = dup(name);
664     a->handle = _airplane.getControlMap()->newInput();
665     _axes.add(a);
666     return a->handle;
667 }
668
669 int FGFDM::parseOutput(const char* name)
670 {
671     if(eq(name, "THROTTLE"))  return ControlMap::THROTTLE;
672     if(eq(name, "MIXTURE"))   return ControlMap::MIXTURE;
673     if(eq(name, "STARTER"))   return ControlMap::STARTER;
674     if(eq(name, "MAGNETOS"))  return ControlMap::MAGNETOS;
675     if(eq(name, "ADVANCE"))   return ControlMap::ADVANCE;
676     if(eq(name, "REHEAT"))    return ControlMap::REHEAT;
677     if(eq(name, "BOOST"))     return ControlMap::BOOST;
678     if(eq(name, "VECTOR"))    return ControlMap::VECTOR;
679     if(eq(name, "PROP"))      return ControlMap::PROP;
680     if(eq(name, "BRAKE"))     return ControlMap::BRAKE;
681     if(eq(name, "STEER"))     return ControlMap::STEER;
682     if(eq(name, "EXTEND"))    return ControlMap::EXTEND;
683     if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
684     if(eq(name, "FLAP0"))     return ControlMap::FLAP0;
685     if(eq(name, "FLAP1"))     return ControlMap::FLAP1;
686     if(eq(name, "SLAT"))      return ControlMap::SLAT;
687     if(eq(name, "SPOILER"))   return ControlMap::SPOILER;
688     if(eq(name, "CASTERING")) return ControlMap::CASTERING;
689     if(eq(name, "PROPPITCH")) return ControlMap::PROPPITCH;
690     if(eq(name, "COLLECTIVE")) return ControlMap::COLLECTIVE;
691     if(eq(name, "CYCLICAIL")) return ControlMap::CYCLICAIL;
692     if(eq(name, "CYCLICELE")) return ControlMap::CYCLICELE;
693     if(eq(name, "ROTORENGINEON")) return ControlMap::ROTORENGINEON;
694     SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
695            << name << "' in YASim aircraft description.");
696     exit(1);
697
698 }
699
700 void FGFDM::parseWeight(XMLAttributes* a)
701 {
702     WeightRec* wr = new WeightRec();
703
704     float v[3];
705     v[0] = attrf(a, "x");
706     v[1] = attrf(a, "y");
707     v[2] = attrf(a, "z");
708
709     wr->prop = dup(a->getValue("mass-prop"));
710     wr->size = attrf(a, "size", 0);
711     wr->handle = _airplane.addWeight(v, wr->size);
712
713     _weights.add(wr);
714 }
715
716 bool FGFDM::eq(const char* a, const char* b)
717 {
718     // Figure it out for yourself. :)
719     while(*a && *b && *a == *b) { a++; b++; }
720     return !(*a || *b);
721 }
722
723 char* FGFDM::dup(const char* s)
724 {
725     int len=0;
726     while(s[len++]);
727     char* s2 = new char[len+1];
728     char* p = s2;
729     while((*p++ = *s++));
730     s2[len] = 0;
731     return s2;
732 }
733
734 int FGFDM::attri(XMLAttributes* atts, char* attr)
735 {
736     if(!atts->hasAttribute(attr)) {
737         SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
738                "' in YASim aircraft description");
739         exit(1);
740     }
741     return attri(atts, attr, 0);
742 }
743
744 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
745 {
746     const char* val = atts->getValue(attr);
747     if(val == 0) return def;
748     else         return atol(val);
749 }
750
751 float FGFDM::attrf(XMLAttributes* atts, char* attr)
752 {
753     if(!atts->hasAttribute(attr)) {
754         SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
755                "' in YASim aircraft description");
756         exit(1);
757     }
758     return attrf(atts, attr, 0);
759 }
760
761 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
762 {
763     const char* val = atts->getValue(attr);
764     if(val == 0) return def;
765     else         return (float)atof(val);    
766 }
767
768 bool FGFDM::attristrue(XMLAttributes* atts, char* attr)
769 {
770     const char* val = atts->getValue(attr);
771     if(val == 0) return false;
772     else         return eq(val,"true");    
773 }
774
775 }; // namespace yasim