]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/FGFDM.cpp
Make yasim accept the launchbar and hook properties. They are not tied to anything...
[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 "Math.hpp"
7 #include "Jet.hpp"
8 #include "SimpleJet.hpp"
9 #include "Gear.hpp"
10 #include "Atmosphere.hpp"
11 #include "PropEngine.hpp"
12 #include "Propeller.hpp"
13 #include "PistonEngine.hpp"
14 #include "TurbineEngine.hpp"
15 #include "Rotor.hpp"
16 #include "Rotorpart.hpp"
17 #include "Rotorblade.hpp"
18
19 #include "FGFDM.hpp"
20
21 namespace yasim {
22
23 // Some conversion factors
24 static const float KTS2MPS = 0.514444444444;
25 static const float FT2M = 0.3048;
26 static const float DEG2RAD = 0.0174532925199;
27 static const float RPM2RAD = 0.10471975512;
28 static const float LBS2N = 4.44822;
29 static const float LBS2KG = 0.45359237;
30 static const float KG2LBS = 2.2046225;
31 static const float CM2GALS = 264.172037284;
32 static const float HP2W = 745.700;
33 static const float INHG2PA = 3386.389;
34 static const float K2DEGF = 1.8;
35 static const float K2DEGFOFFSET = -459.4;
36 static const float CIN2CM = 1.6387064e-5;
37 static const float YASIM_PI = 3.14159265358979323846;
38
39 static const float NM2FTLB = (1/(LBS2N*FT2M));
40
41 // Stubs, so that this can be compiled without the FlightGear
42 // binary.  What's the best way to handle this?
43
44 //     float fgGetFloat(char* name, float def) { return 0; }
45 //     void fgSetFloat(char* name, float val) {}
46
47 FGFDM::FGFDM()
48 {
49     _nextEngine = 0;
50
51     // Map /controls/flight/elevator to the approach elevator control.  This
52     // should probably be settable, but there are very few aircraft
53     // who trim their approaches using things other than elevator.
54     _airplane.setElevatorControl(parseAxis("/controls/flight/elevator-trim"));
55
56     // FIXME: read seed from somewhere?
57     int seed = 0;
58     _turb = new Turbulence(10, seed);
59 }
60
61 FGFDM::~FGFDM()
62 {
63     int i;
64     for(i=0; i<_axes.size(); i++) {
65         AxisRec* a = (AxisRec*)_axes.get(i);
66         delete[] a->name;
67         delete a;
68     }
69     for(i=0; i<_thrusters.size(); i++) {
70         EngRec* er = (EngRec*)_thrusters.get(i);
71         delete[] er->prefix;
72         delete er->eng;
73         delete er;
74     }
75     for(i=0; i<_weights.size(); i++) {
76         WeightRec* wr = (WeightRec*)_weights.get(i);
77         delete[] wr->prop;
78         delete wr;
79     }
80     for(i=0; i<_controlProps.size(); i++)
81         delete (PropOut*)_controlProps.get(i);
82 }
83
84 void FGFDM::iterate(float dt)
85 {
86     getExternalInput(dt);
87     _airplane.iterate(dt);
88
89     // Do fuel stuff (FIXME: should stash SGPropertyNode objects here)
90     char buf[256];
91     for(int i=0; i<_airplane.numThrusters(); i++) {
92         Thruster* t = _airplane.getThruster(i);
93
94         sprintf(buf, "/engines/engine[%d]/out-of-fuel", i);
95         t->setFuelState(!fgGetBool(buf));
96
97         sprintf(buf, "/engines/engine[%d]/fuel-consumed-lbs", i);
98         double consumed = fgGetDouble(buf) + dt * KG2LBS * t->getFuelFlow();
99         fgSetDouble(buf, consumed);
100     }
101     for(int i=0; i<_airplane.numTanks(); i++) {
102         sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
103         _airplane.setFuel(i, LBS2KG * fgGetFloat(buf));
104     } 
105     _airplane.calcFuelWeights();
106     
107     setOutputProperties(dt);
108 }
109
110 Airplane* FGFDM::getAirplane()
111 {
112     return &_airplane;
113 }
114
115 void FGFDM::init()
116 {
117     // Allows the user to start with something other than full fuel
118     _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
119
120     // Read out the resulting fuel state
121     char buf[256];
122     for(int i=0; i<_airplane.numTanks(); i++) {
123         sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
124         fgSetDouble(buf, _airplane.getFuel(i) * KG2LBS);
125
126         double density = _airplane.getFuelDensity(i);
127         sprintf(buf, "/consumables/fuel/tank[%d]/density-ppg", i);
128         fgSetDouble(buf, density * (KG2LBS/CM2GALS));
129
130         sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
131         fgSetDouble(buf, _airplane.getFuel(i) * CM2GALS / density);
132
133         sprintf(buf, "/consumables/fuel/tank[%d]/capacity-gal_us", i);
134         fgSetDouble(buf, CM2GALS * _airplane.getTankCapacity(i)/density);
135     }    
136
137     // This has a nasty habit of being false at startup.  That's not
138     // good.
139     fgSetBool("/controls/gear/gear-down", true);
140
141     _airplane.getModel()->setTurbulence(_turb);
142 }
143
144 // Not the worlds safest parser.  But it's short & sweet.
145 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
146 {
147     XMLAttributes* a = (XMLAttributes*)&atts;
148     float v[3];
149     char buf[64];
150
151     if(eq(name, "airplane")) {
152         _airplane.setWeight(attrf(a, "mass") * LBS2KG);
153     } else if(eq(name, "approach")) {
154         float spd = attrf(a, "speed") * KTS2MPS;
155         float alt = attrf(a, "alt", 0) * FT2M;
156         float aoa = attrf(a, "aoa", 0) * DEG2RAD;
157         _airplane.setApproach(spd, alt, aoa, attrf(a, "fuel", 0.2));
158         _cruiseCurr = false;
159     } else if(eq(name, "cruise")) {
160         float spd = attrf(a, "speed") * KTS2MPS;
161         float alt = attrf(a, "alt") * FT2M;
162         _airplane.setCruise(spd, alt, attrf(a, "fuel", 0.5));
163         _cruiseCurr = true;
164     } else if(eq(name, "solve-weight")) {
165         int idx = attri(a, "idx");
166         float wgt = attrf(a, "weight") * LBS2KG;
167         _airplane.addSolutionWeight(!_cruiseCurr, idx, wgt);
168     } else if(eq(name, "cockpit")) {
169         v[0] = attrf(a, "x");
170         v[1] = attrf(a, "y");
171         v[2] = attrf(a, "z");
172         _airplane.setPilotPos(v);
173     } else if(eq(name, "rotor")) {
174         _airplane.addRotor(parseRotor(a, name));
175     } else if(eq(name, "wing")) {
176         _airplane.setWing(parseWing(a, name));
177     } else if(eq(name, "hstab")) {
178         _airplane.setTail(parseWing(a, name));
179     } else if(eq(name, "vstab") || eq(name, "mstab")) {
180         _airplane.addVStab(parseWing(a, name));
181     } else if(eq(name, "piston-engine")) {
182         parsePistonEngine(a);
183     } else if(eq(name, "turbine-engine")) {
184         parseTurbineEngine(a);
185     } else if(eq(name, "propeller")) {
186         parsePropeller(a);
187     } else if(eq(name, "thruster")) {
188         SimpleJet* j = new SimpleJet();
189         _currObj = j;
190         v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
191         j->setPosition(v);
192         _airplane.addThruster(j, 0, v);
193         v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
194         j->setDirection(v);
195         j->setThrust(attrf(a, "thrust") * LBS2N);
196     } else if(eq(name, "jet")) {
197         Jet* j = new Jet();
198         _currObj = j;
199         v[0] = attrf(a, "x");
200         v[1] = attrf(a, "y");
201         v[2] = attrf(a, "z");
202         float mass = attrf(a, "mass") * LBS2KG;
203         j->setMaxThrust(attrf(a, "thrust") * LBS2N,
204                         attrf(a, "afterburner", 0) * LBS2N);
205         j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
206         j->setReverseThrust(attrf(a, "reverse", 0.2));
207
208         float n1min = attrf(a, "n1-idle", 55);
209         float n1max = attrf(a, "n1-max", 102);
210         float n2min = attrf(a, "n2-idle", 73);
211         float n2max = attrf(a, "n2-max", 103);
212         j->setRPMs(n1min, n1max, n2min, n2max);
213
214         j->setTSFC(attrf(a, "tsfc", 0.8));
215         if(a->hasAttribute("egt"))  j->setEGT(attrf(a, "egt"));
216         if(a->hasAttribute("epr"))  j->setEPR(attrf(a, "epr"));
217         if(a->hasAttribute("exhaust-speed"))
218             j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
219         
220         j->setPosition(v);
221         _airplane.addThruster(j, mass, v);
222         sprintf(buf, "/engines/engine[%d]", _nextEngine++);
223         EngRec* er = new EngRec();
224         er->eng = j;
225         er->prefix = dup(buf);
226         _thrusters.add(er);
227     } else if(eq(name, "gear")) {
228         Gear* g = new Gear();
229         _currObj = g;
230         v[0] = attrf(a, "x");
231         v[1] = attrf(a, "y");
232         v[2] = attrf(a, "z");
233         g->setPosition(v);
234         v[0] = 0;
235         v[1] = 0;
236         v[2] = attrf(a, "compression", 1);
237         g->setCompression(v);
238         g->setBrake(attrf(a, "skid", 0));
239         g->setStaticFriction(attrf(a, "sfric", 0.8));
240         g->setDynamicFriction(attrf(a, "dfric", 0.7));
241         g->setSpring(attrf(a, "spring", 1));
242         g->setDamping(attrf(a, "damp", 1));
243         _airplane.addGear(g);
244     } else if(eq(name, "hook")) {
245     } else if(eq(name, "launchbar")) {
246     } else if(eq(name, "fuselage")) {
247         float b[3];
248         v[0] = attrf(a, "ax");
249         v[1] = attrf(a, "ay");
250         v[2] = attrf(a, "az");
251         b[0] = attrf(a, "bx");
252         b[1] = attrf(a, "by");
253         b[2] = attrf(a, "bz");
254         float taper = attrf(a, "taper", 1);
255         float mid = attrf(a, "midpoint", 0.5);
256         _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid);
257     } else if(eq(name, "tank")) {
258         v[0] = attrf(a, "x");
259         v[1] = attrf(a, "y");
260         v[2] = attrf(a, "z");
261         float density = 6.0; // gasoline, in lbs/gal
262         if(a->hasAttribute("jet")) density = 6.72; 
263         density *= LBS2KG*CM2GALS;
264         _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
265     } else if(eq(name, "ballast")) {
266         v[0] = attrf(a, "x");
267         v[1] = attrf(a, "y");
268         v[2] = attrf(a, "z");
269         _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
270     } else if(eq(name, "weight")) {
271         parseWeight(a);
272     } else if(eq(name, "stall")) {
273         Wing* w = (Wing*)_currObj;
274         w->setStall(attrf(a, "aoa") * DEG2RAD);
275         w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
276         w->setStallPeak(attrf(a, "peak", 1.5));
277     } else if(eq(name, "flap0")) {
278         ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
279                                     attrf(a, "lift"), attrf(a, "drag"));
280     } else if(eq(name, "flap1")) {
281         ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
282                                     attrf(a, "lift"), attrf(a, "drag"));
283     } else if(eq(name, "slat")) {
284         ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
285                                    attrf(a, "aoa"), attrf(a, "drag"));
286     } else if(eq(name, "spoiler")) {
287         ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
288                                       attrf(a, "lift"), attrf(a, "drag"));
289     /* } else if(eq(name, "collective")) {
290         ((Rotor*)_currObj)->setcollective(attrf(a, "min"), attrf(a, "max"));
291     } else if(eq(name, "cyclic")) {
292         ((Rotor*)_currObj)->setcyclic(attrf(a, "ail"), attrf(a, "ele"));
293     */                               
294     } else if(eq(name, "actionpt")) {
295         v[0] = attrf(a, "x");
296         v[1] = attrf(a, "y");
297         v[2] = attrf(a, "z");
298         ((Thruster*)_currObj)->setPosition(v);
299     } else if(eq(name, "dir")) {
300         v[0] = attrf(a, "x");
301         v[1] = attrf(a, "y");
302         v[2] = attrf(a, "z");
303         ((Thruster*)_currObj)->setDirection(v);
304     } else if(eq(name, "control-setting")) {
305         // A cruise or approach control setting
306         const char* axis = a->getValue("axis");
307         float value = attrf(a, "value", 0);
308         if(_cruiseCurr)
309             _airplane.addCruiseControl(parseAxis(axis), value);
310         else
311             _airplane.addApproachControl(parseAxis(axis), value);
312     } else if(eq(name, "control-input")) {
313
314         // A mapping of input property to a control
315         int axis = parseAxis(a->getValue("axis"));
316         int control = parseOutput(a->getValue("control"));
317         int opt = 0;
318         opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
319         opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
320         opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
321         
322         ControlMap* cm = _airplane.getControlMap();
323         if(a->hasAttribute("src0")) {
324                            cm->addMapping(axis, control, _currObj, opt,
325                            attrf(a, "src0"), attrf(a, "src1"), 
326                            attrf(a, "dst0"), attrf(a, "dst1"));
327         } else {
328             cm->addMapping(axis, control, _currObj, opt);
329         }
330     } else if(eq(name, "control-output")) {
331         // A property output for a control on the current object
332         ControlMap* cm = _airplane.getControlMap();
333         int type = parseOutput(a->getValue("control"));
334         int handle = cm->getOutputHandle(_currObj, type);
335
336         PropOut* p = new PropOut();
337         p->prop = fgGetNode(a->getValue("prop"), true);
338         p->handle = handle;
339         p->type = type;
340         p->left = !(a->hasAttribute("side") &&
341                         eq("right", a->getValue("side")));
342         p->min = attrf(a, "min", cm->rangeMin(type));
343         p->max = attrf(a, "max", cm->rangeMax(type));
344         _controlProps.add(p);
345
346     } else if(eq(name, "control-speed")) {
347         ControlMap* cm = _airplane.getControlMap();
348         int type = parseOutput(a->getValue("control"));
349         int handle = cm->getOutputHandle(_currObj, type);
350         float time = attrf(a, "transition-time", 0);
351         
352         cm->setTransitionTime(handle, time);
353     } else {
354         SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '"
355                << name << "' found in YASim aircraft description");
356         exit(1);
357     }
358 }
359
360 void FGFDM::getExternalInput(float dt)
361 {
362     char buf[256];
363
364     _turb->setMagnitude(fgGetFloat("/environment/turbulence/magnitude-norm"));
365     _turb->update(dt, fgGetFloat("/environment/turbulence/rate-hz"));
366
367     // The control axes
368     ControlMap* cm = _airplane.getControlMap();
369     cm->reset();
370     int i;
371     for(i=0; i<_axes.size(); i++) {
372         AxisRec* a = (AxisRec*)_axes.get(i);
373         float val = fgGetFloat(a->name, 0);
374         cm->setInput(a->handle, val);
375     }
376     cm->applyControls(dt);
377
378     // Weights
379     for(i=0; i<_weights.size(); i++) {
380         WeightRec* wr = (WeightRec*)_weights.get(i);
381         _airplane.setWeight(wr->handle, LBS2KG * fgGetFloat(wr->prop));
382     }
383
384     for(i=0; i<_thrusters.size(); i++) {
385         EngRec* er = (EngRec*)_thrusters.get(i);
386         Thruster* t = er->eng;
387
388         if(t->getPropEngine()) {
389             PropEngine* p = t->getPropEngine();
390             sprintf(buf, "%s/rpm", er->prefix);
391             p->setOmega(fgGetFloat(buf, 500) * RPM2RAD);
392         }
393     }
394 }
395
396 // Linearly "seeks" a property by the specified fraction of the way to
397 // the target value.  Used to emulate "slowly changing" output values.
398 static void moveprop(SGPropertyNode* node, const char* prop,
399                     float target, float frac)
400 {
401     float val = node->getFloatValue(prop);
402     if(frac > 1) frac = 1;
403     if(frac < 0) frac = 0;
404     val += (target - val) * frac;
405     node->setFloatValue(prop, val);
406 }
407
408 void FGFDM::setOutputProperties(float dt)
409 {
410     // char buf[256];
411     int i;
412
413     float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
414     fgSetFloat("/yasim/gross-weight-lbs", grossWgt);
415
416     ControlMap* cm = _airplane.getControlMap();
417     for(i=0; i<_controlProps.size(); i++) {
418         PropOut* p = (PropOut*)_controlProps.get(i);
419         float val = (p->left
420                      ? cm->getOutput(p->handle)
421                      : cm->getOutputR(p->handle));
422         float rmin = cm->rangeMin(p->type);
423         float rmax = cm->rangeMax(p->type);
424         float frac = (val - rmin) / (rmax - rmin);
425         val = frac*(p->max - p->min) + p->min;
426         p->prop->setFloatValue(val);
427     }
428
429     for(i=0; i<_airplane.getNumRotors(); i++) {
430         Rotor*r=(Rotor*)_airplane.getRotor(i);
431         int j = 0;
432         float f;
433         char b[256];
434         while(j = r->getValueforFGSet(j, b, &f))
435             if(b[0]) fgSetFloat(b,f);
436         
437         for(j=0; j < r->numRotorparts(); j++) {
438             Rotorpart* s = (Rotorpart*)r->getRotorpart(j);
439             char *b;
440             int k;
441             for(k=0; k<2; k++) {
442                 b=s->getAlphaoutput(k);
443                 if(b[0]) fgSetFloat(b, s->getAlpha(k));
444             }
445         }
446         for(j=0; j < r->numRotorblades(); j++) {
447             Rotorblade* s = (Rotorblade*)r->getRotorblade(j);
448             char *b;
449             int k;
450             for (k=0; k<2; k++) {
451                 b = s->getAlphaoutput(k);
452                 if(b[0]) fgSetFloat(b, s->getAlpha(k));
453             }
454         }
455     }
456
457     float fuelDensity = _airplane.getFuelDensity(0); // HACK
458     for(i=0; i<_thrusters.size(); i++) {
459         EngRec* er = (EngRec*)_thrusters.get(i);
460         Thruster* t = er->eng;
461         SGPropertyNode * node = fgGetNode("engines/engine", i, true);
462
463         // Set: running, cranking, prop-thrust, max-hp, power-pct
464         node->setBoolValue("running", t->isRunning());
465         node->setBoolValue("cranking", t->isCranking());
466
467         float tmp[3];
468         t->getThrust(tmp);
469         float lbs = Math::mag3(tmp) * (KG2LBS/9.8);
470         node->setFloatValue("prop-thrust", lbs); // Deprecated name
471         node->setFloatValue("thrust-lbs", lbs);
472         node->setFloatValue("fuel-flow-gph",
473                             (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
474
475         if(t->getPropEngine()) {
476             PropEngine* p = t->getPropEngine();
477             node->setFloatValue("rpm", p->getOmega() * (1/RPM2RAD));
478             node->setFloatValue("torque-ftlb",
479                                 p->getEngine()->getTorque() * NM2FTLB);
480         
481             if(p->getEngine()->isPistonEngine()) {
482                 PistonEngine* pe = p->getEngine()->isPistonEngine();
483                 node->setFloatValue("mp-osi", pe->getMP() * (1/INHG2PA));
484                 node->setFloatValue("mp-inhg", pe->getMP() * (1/INHG2PA));
485                 node->setFloatValue("egt-degf",
486                                     pe->getEGT() * K2DEGF + K2DEGFOFFSET);
487             } else if(p->getEngine()->isTurbineEngine()) {
488                 TurbineEngine* te = p->getEngine()->isTurbineEngine();
489                 node->setFloatValue("n2", te->getN2());
490             }
491         }
492
493         if(t->getJet()) {
494             Jet* j = t->getJet();
495             node->setFloatValue("n1", j->getN1());
496             node->setFloatValue("n2", j->getN2());
497             node->setFloatValue("epr", j->getEPR());
498             node->setFloatValue("egr-degf",
499                                 j->getEGT() * K2DEGF + K2DEGFOFFSET);
500
501             // These are "unmodeled" values that are still needed for
502             // many cockpits.  Tie them all to the N1 speed, but
503             // normalize the numbers to the range [0:1] so the
504             // cockpit code can scale them to the right values.
505             float pnorm = j->getPerfNorm();
506             moveprop(node, "oilp-norm", pnorm, dt/3); // 3s seek time
507             moveprop(node, "oilt-norm", pnorm, dt/30); // 30s 
508             moveprop(node, "itt-norm", pnorm, dt/1); // 1s
509         }
510     }
511 }
512
513 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
514 {
515     Wing* w = new Wing();
516
517     float defDihed = 0;
518     if(eq(type, "vstab"))
519         defDihed = 90;
520     else
521         w->setMirror(true);
522
523     float pos[3];
524     pos[0] = attrf(a, "x");
525     pos[1] = attrf(a, "y");
526     pos[2] = attrf(a, "z");
527     w->setBase(pos);
528
529     w->setLength(attrf(a, "length"));
530     w->setChord(attrf(a, "chord"));
531     w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
532     w->setTaper(attrf(a, "taper", 1));
533     w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
534     w->setCamber(attrf(a, "camber", 0));
535     w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
536     w->setTwist(attrf(a, "twist", 0) * DEG2RAD);
537
538     // The 70% is a magic number that sorta kinda seems to match known
539     // throttle settings to approach speed.
540     w->setInducedDrag(0.7*attrf(a, "idrag", 1));
541
542     float effect = attrf(a, "effectiveness", 1);
543     w->setDragScale(w->getDragScale()*effect);
544
545     _currObj = w;
546     return w;
547 }
548
549 Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
550 {
551     Rotor* w = new Rotor();
552
553     // float defDihed = 0;
554
555     float pos[3];
556     pos[0] = attrf(a, "x");
557     pos[1] = attrf(a, "y");
558     pos[2] = attrf(a, "z");
559     w->setBase(pos);
560
561     float normal[3];
562     normal[0] = attrf(a, "nx");
563     normal[1] = attrf(a, "ny");
564     normal[2] = attrf(a, "nz");
565     w->setNormal(normal);
566
567     float forward[3];
568     forward[0] = attrf(a, "fx");
569     forward[1] = attrf(a, "fy");
570     forward[2] = attrf(a, "fz");
571     w->setForward(forward);
572
573     w->setMaxCyclicail(attrf(a, "maxcyclicail", 7.6));
574     w->setMaxCyclicele(attrf(a, "maxcyclicele", 4.94));
575     w->setMinCyclicail(attrf(a, "mincyclicail", -7.6));
576     w->setMinCyclicele(attrf(a, "mincyclicele", -4.94));
577     w->setMaxCollective(attrf(a, "maxcollective", 15.8));
578     w->setMinCollective(attrf(a, "mincollective", -0.2));
579     w->setDiameter(attrf(a, "diameter", 10.2));
580     w->setWeightPerBlade(attrf(a, "weightperblade", 44));
581     w->setNumberOfBlades(attrf(a, "numblades", 4));
582     w->setRelBladeCenter(attrf(a, "relbladecenter", 0.7));
583     w->setDynamic(attrf(a, "dynamic", 0.7));
584     w->setDelta3(attrf(a, "delta3", 0));
585     w->setDelta(attrf(a, "delta", 0));
586     w->setTranslift(attrf(a, "translift", 0.05));
587     w->setC2(attrf(a, "dragfactor", 1));
588     w->setStepspersecond(attrf(a, "stepspersecond", 120));
589     w->setRPM(attrf(a, "rpm", 424));
590     w->setRelLenHinge(attrf(a, "rellenflaphinge", 0.07));
591     w->setAlpha0((attrf(a, "flap0", -5))*YASIM_PI/180);
592     w->setAlphamin((attrf(a, "flapmin", -15))/180*YASIM_PI);
593     w->setAlphamax((attrf(a, "flapmax",  15))*YASIM_PI/180);
594     w->setAlpha0factor(attrf(a, "flap0factor", 1));
595     w->setTeeterdamp(attrf(a,"teeterdamp",.0001));
596     w->setMaxteeterdamp(attrf(a,"maxteeterdamp",1000));
597     w->setRelLenTeeterHinge(attrf(a,"rellenteeterhinge",0.01));
598     void setAlphamin(float f);
599     void setAlphamax(float f);
600     void setAlpha0factor(float f);
601
602     if(attrb(a,"ccw"))
603        w->setCcw(1); 
604     
605     if(a->hasAttribute("name"))
606        w->setName(a->getValue("name") );
607     if(a->hasAttribute("alphaout0"))
608        w->setAlphaoutput(0,a->getValue("alphaout0") );
609     if(a->hasAttribute("alphaout1"))  w->setAlphaoutput(1,a->getValue("alphaout1") );
610     if(a->hasAttribute("alphaout2"))  w->setAlphaoutput(2,a->getValue("alphaout2") );
611     if(a->hasAttribute("alphaout3"))  w->setAlphaoutput(3,a->getValue("alphaout3") );
612     if(a->hasAttribute("coneout"))  w->setAlphaoutput(4,a->getValue("coneout") );
613     if(a->hasAttribute("yawout"))   w->setAlphaoutput(5,a->getValue("yawout") );
614     if(a->hasAttribute("rollout"))  w->setAlphaoutput(6,a->getValue("rollout") );
615
616     w->setPitchA(attrf(a, "pitch_a", 10));
617     w->setPitchB(attrf(a, "pitch_b", 10));
618     w->setForceAtPitchA(attrf(a, "forceatpitch_a", 3000));
619     w->setPowerAtPitch0(attrf(a, "poweratpitch_0", 300));
620     w->setPowerAtPitchB(attrf(a, "poweratpitch_b", 3000));
621     if(attrb(a,"notorque"))
622        w->setNotorque(1); 
623     if(attrb(a,"simblades"))
624        w->setSimBlades(1); 
625
626     _currObj = w;
627     return w;
628 }
629
630 void FGFDM::parsePistonEngine(XMLAttributes* a)
631 {
632     float engP = attrf(a, "eng-power") * HP2W;
633     float engS = attrf(a, "eng-rpm") * RPM2RAD;
634
635     PistonEngine* eng = new PistonEngine(engP, engS);
636
637     if(a->hasAttribute("displacement"))
638         eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
639
640     if(a->hasAttribute("compression"))
641         eng->setCompression(attrf(a, "compression"));        
642
643     if(a->hasAttribute("turbo-mul")) {
644         float mul = attrf(a, "turbo-mul");
645         float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
646         eng->setTurboParams(mul, mp);
647     }
648
649     ((PropEngine*)_currObj)->setEngine(eng);
650 }
651
652 void FGFDM::parseTurbineEngine(XMLAttributes* a)
653 {
654     float power = attrf(a, "eng-power") * HP2W;
655     float omega = attrf(a, "eng-rpm") * RPM2RAD;
656     float alt = attrf(a, "alt") * FT2M;
657     float flatRating = attrf(a, "flat-rating") * HP2W;
658     TurbineEngine* eng = new TurbineEngine(power, omega, alt, flatRating);
659
660     if(a->hasAttribute("n2-low-idle"))
661         eng->setN2Range(attrf(a, "n2-low-idle"), attrf(a, "n2-high-idle"),
662                         attrf(a, "n2-max"));
663
664     // Nasty units conversion: lbs/hr per hp -> kg/s per watt
665     if(a->hasAttribute("bsfc"))
666         eng->setFuelConsumption(attrf(a, "bsfc") * (LBS2KG/(3600*HP2W)));
667
668     ((PropEngine*)_currObj)->setEngine(eng);
669 }
670
671 void FGFDM::parsePropeller(XMLAttributes* a)
672 {
673     // Legacy Handling for the old engines syntax:
674     PistonEngine* eng = 0;
675     if(a->hasAttribute("eng-power")) {
676         SG_LOG(SG_FLIGHT,SG_ALERT, "WARNING: "
677                << "Legacy engine definition in YASim configuration file.  "
678                << "Please fix.");
679         float engP = attrf(a, "eng-power") * HP2W;
680         float engS = attrf(a, "eng-rpm") * RPM2RAD;
681         eng = new PistonEngine(engP, engS);
682         if(a->hasAttribute("displacement"))
683             eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
684         if(a->hasAttribute("compression"))
685             eng->setCompression(attrf(a, "compression"));        
686         if(a->hasAttribute("turbo-mul")) {
687             float mul = attrf(a, "turbo-mul");
688             float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
689             eng->setTurboParams(mul, mp);
690         }
691     }
692
693     // Now parse the actual propeller definition:
694     float cg[3];
695     cg[0] = attrf(a, "x");
696     cg[1] = attrf(a, "y");
697     cg[2] = attrf(a, "z");
698     float mass = attrf(a, "mass") * LBS2KG;
699     float moment = attrf(a, "moment");
700     float radius = attrf(a, "radius");
701     float speed = attrf(a, "cruise-speed") * KTS2MPS;
702     float omega = attrf(a, "cruise-rpm") * RPM2RAD;
703     float power = attrf(a, "cruise-power") * HP2W;
704     float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
705
706     Propeller* prop = new Propeller(radius, speed, omega, rho, power);
707     PropEngine* thruster = new PropEngine(prop, eng, moment);
708     _airplane.addThruster(thruster, mass, cg);
709
710     if(a->hasAttribute("takeoff-power")) {
711         float power0 = attrf(a, "takeoff-power") * HP2W;
712         float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
713         prop->setTakeoff(omega0, power0);
714     }
715
716     if(a->hasAttribute("max-rpm")) {
717         float max = attrf(a, "max-rpm") * RPM2RAD;
718         float min = attrf(a, "min-rpm") * RPM2RAD;
719         thruster->setVariableProp(min, max);
720     }
721
722     if(attrb(a, "contra"))
723         thruster->setContraPair(true);
724
725     if(a->hasAttribute("manual-pitch")) {
726         prop->setManualPitch();
727     }
728
729     thruster->setGearRatio(attrf(a, "gear-ratio", 1));
730
731     char buf[64];
732     sprintf(buf, "/engines/engine[%d]", _nextEngine++);
733     EngRec* er = new EngRec();
734     er->eng = thruster;
735     er->prefix = dup(buf);
736     _thrusters.add(er);
737
738     _currObj = thruster;
739 }
740
741 // Turns a string axis name into an integer for use by the
742 // ControlMap.  Creates a new axis if this one hasn't been defined
743 // yet.
744 int FGFDM::parseAxis(const char* name)
745 {
746     int i;
747     for(i=0; i<_axes.size(); i++) {
748         AxisRec* a = (AxisRec*)_axes.get(i);
749         if(eq(a->name, name))
750             return a->handle;
751     }
752
753     // Not there, make a new one.
754     AxisRec* a = new AxisRec();
755     a->name = dup(name);
756     fgGetNode( a->name, true ); // make sure the property name exists
757     a->handle = _airplane.getControlMap()->newInput();
758     _axes.add(a);
759     return a->handle;
760 }
761
762 int FGFDM::parseOutput(const char* name)
763 {
764     if(eq(name, "THROTTLE"))  return ControlMap::THROTTLE;
765     if(eq(name, "MIXTURE"))   return ControlMap::MIXTURE;
766     if(eq(name, "CONDLEVER")) return ControlMap::CONDLEVER;
767     if(eq(name, "STARTER"))   return ControlMap::STARTER;
768     if(eq(name, "MAGNETOS"))  return ControlMap::MAGNETOS;
769     if(eq(name, "ADVANCE"))   return ControlMap::ADVANCE;
770     if(eq(name, "REHEAT"))    return ControlMap::REHEAT;
771     if(eq(name, "BOOST"))     return ControlMap::BOOST;
772     if(eq(name, "VECTOR"))    return ControlMap::VECTOR;
773     if(eq(name, "PROP"))      return ControlMap::PROP;
774     if(eq(name, "BRAKE"))     return ControlMap::BRAKE;
775     if(eq(name, "STEER"))     return ControlMap::STEER;
776     if(eq(name, "EXTEND"))    return ControlMap::EXTEND;
777     if(eq(name, "HEXTEND"))   return ControlMap::HEXTEND;
778     if(eq(name, "LEXTEND"))   return ControlMap::LEXTEND;
779     if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
780     if(eq(name, "FLAP0"))     return ControlMap::FLAP0;
781     if(eq(name, "FLAP1"))     return ControlMap::FLAP1;
782     if(eq(name, "SLAT"))      return ControlMap::SLAT;
783     if(eq(name, "SPOILER"))   return ControlMap::SPOILER;
784     if(eq(name, "CASTERING")) return ControlMap::CASTERING;
785     if(eq(name, "PROPPITCH")) return ControlMap::PROPPITCH;
786     if(eq(name, "PROPFEATHER")) return ControlMap::PROPFEATHER;
787     if(eq(name, "COLLECTIVE")) return ControlMap::COLLECTIVE;
788     if(eq(name, "CYCLICAIL")) return ControlMap::CYCLICAIL;
789     if(eq(name, "CYCLICELE")) return ControlMap::CYCLICELE;
790     if(eq(name, "ROTORENGINEON")) return ControlMap::ROTORENGINEON;
791     if(eq(name, "REVERSE_THRUST")) return ControlMap::REVERSE_THRUST;
792     SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
793            << name << "' in YASim aircraft description.");
794     exit(1);
795
796 }
797
798 void FGFDM::parseWeight(XMLAttributes* a)
799 {
800     WeightRec* wr = new WeightRec();
801
802     float v[3];
803     v[0] = attrf(a, "x");
804     v[1] = attrf(a, "y");
805     v[2] = attrf(a, "z");
806
807     wr->prop = dup(a->getValue("mass-prop"));
808     wr->size = attrf(a, "size", 0);
809     wr->handle = _airplane.addWeight(v, wr->size);
810
811     _weights.add(wr);
812 }
813
814 bool FGFDM::eq(const char* a, const char* b)
815 {
816     // Figure it out for yourself. :)
817     while(*a && *b && *a == *b) { a++; b++; }
818     return !(*a || *b);
819 }
820
821 char* FGFDM::dup(const char* s)
822 {
823     int len=0;
824     while(s[len++]);
825     char* s2 = new char[len+1];
826     char* p = s2;
827     while((*p++ = *s++));
828     s2[len] = 0;
829     return s2;
830 }
831
832 int FGFDM::attri(XMLAttributes* atts, char* attr)
833 {
834     if(!atts->hasAttribute(attr)) {
835         SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
836                "' in YASim aircraft description");
837         exit(1);
838     }
839     return attri(atts, attr, 0);
840 }
841
842 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
843 {
844     const char* val = atts->getValue(attr);
845     if(val == 0) return def;
846     else         return atol(val);
847 }
848
849 float FGFDM::attrf(XMLAttributes* atts, char* attr)
850 {
851     if(!atts->hasAttribute(attr)) {
852         SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
853                "' in YASim aircraft description");
854         exit(1);
855     }
856     return attrf(atts, attr, 0);
857 }
858
859 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
860 {
861     const char* val = atts->getValue(attr);
862     if(val == 0) return def;
863     else         return (float)atof(val);    
864 }
865
866 // ACK: the dreaded ambiguous string boolean.  Remind me to shoot Maik
867 // when I have a chance. :).  Unless you have a parser that can check
868 // symbol constants (we don't), this kind of coding is just a Bad
869 // Idea.  This implementation, for example, silently returns a boolean
870 // falsehood for values of "1", "yes", "True", and "TRUE".  Which is
871 // especially annoying preexisting boolean attributes in the same
872 // parser want to see "1" and will choke on a "true"...
873 //
874 // Unfortunately, this usage creeped into existing configuration files
875 // while I wasn't active, and it's going to be hard to remove.  Issue
876 // a warning to nag people into changing their ways for now...
877 bool FGFDM::attrb(XMLAttributes* atts, char* attr)
878 {
879     const char* val = atts->getValue(attr);
880     if(val == 0) return false;
881
882     if(eq(val,"true")) {
883         SG_LOG(SG_FLIGHT, SG_ALERT, "Warning: " <<
884                "deprecated 'true' boolean in YASim configuration file.  " <<
885                "Use numeric booleans (attribute=\"1\") instead");
886         return true;
887     }
888     return attri(atts, attr, 0) ? true : false;
889 }
890
891 }; // namespace yasim