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