4 #include <Main/fg_props.hxx>
8 #include "SimpleJet.hpp"
10 #include "Atmosphere.hpp"
11 #include "PropEngine.hpp"
12 #include "Propeller.hpp"
13 #include "PistonEngine.hpp"
14 #include "TurbineEngine.hpp"
16 #include "Rotorpart.hpp"
17 #include "Rotorblade.hpp"
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;
39 static const float NM2FTLB = (1/(LBS2N*FT2M));
41 // Stubs, so that this can be compiled without the FlightGear
42 // binary. What's the best way to handle this?
44 // float fgGetFloat(char* name, float def) { return 0; }
45 // void fgSetFloat(char* name, float val) {}
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"));
56 // FIXME: read seed from somewhere?
58 _turb = new Turbulence(10, seed);
64 for(i=0; i<_axes.size(); i++) {
65 AxisRec* a = (AxisRec*)_axes.get(i);
69 for(i=0; i<_thrusters.size(); i++) {
70 EngRec* er = (EngRec*)_thrusters.get(i);
75 for(i=0; i<_weights.size(); i++) {
76 WeightRec* wr = (WeightRec*)_weights.get(i);
80 for(i=0; i<_controlProps.size(); i++)
81 delete (PropOut*)_controlProps.get(i);
84 void FGFDM::iterate(float dt)
87 _airplane.iterate(dt);
89 // Do fuel stuff (FIXME: should stash SGPropertyNode objects here)
91 for(int i=0; i<_airplane.numThrusters(); i++) {
92 Thruster* t = _airplane.getThruster(i);
94 sprintf(buf, "/engines/engine[%d]/out-of-fuel", i);
95 t->setFuelState(!fgGetBool(buf));
97 sprintf(buf, "/engines/engine[%d]/fuel-consumed-lbs", i);
98 double consumed = fgGetDouble(buf) + dt * KG2LBS * t->getFuelFlow();
99 fgSetDouble(buf, consumed);
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));
105 _airplane.calcFuelWeights();
107 setOutputProperties(dt);
110 Airplane* FGFDM::getAirplane()
117 // Allows the user to start with something other than full fuel
118 _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
120 // Read out the resulting fuel state
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);
126 double density = _airplane.getFuelDensity(i);
127 sprintf(buf, "/consumables/fuel/tank[%d]/density-ppg", i);
128 fgSetDouble(buf, density * (KG2LBS/CM2GALS));
130 sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
131 fgSetDouble(buf, _airplane.getFuel(i) * CM2GALS / density);
133 sprintf(buf, "/consumables/fuel/tank[%d]/capacity-gal_us", i);
134 fgSetDouble(buf, CM2GALS * _airplane.getTankCapacity(i)/density);
137 // This has a nasty habit of being false at startup. That's not
139 fgSetBool("/controls/gear/gear-down", true);
141 _airplane.getModel()->setTurbulence(_turb);
144 // Not the worlds safest parser. But it's short & sweet.
145 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
147 XMLAttributes* a = (XMLAttributes*)&atts;
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));
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));
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")) {
187 } else if(eq(name, "thruster")) {
188 SimpleJet* j = new SimpleJet();
190 v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
192 _airplane.addThruster(j, 0, v);
193 v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
195 j->setThrust(attrf(a, "thrust") * LBS2N);
196 } else if(eq(name, "jet")) {
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));
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);
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);
221 _airplane.addThruster(j, mass, v);
222 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
223 EngRec* er = new EngRec();
225 er->prefix = dup(buf);
227 } else if(eq(name, "gear")) {
228 Gear* g = new Gear();
230 v[0] = attrf(a, "x");
231 v[1] = attrf(a, "y");
232 v[2] = attrf(a, "z");
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")) {
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")) {
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"));
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);
309 _airplane.addCruiseControl(parseAxis(axis), value);
311 _airplane.addApproachControl(parseAxis(axis), value);
312 } else if(eq(name, "control-input")) {
314 // A mapping of input property to a control
315 int axis = parseAxis(a->getValue("axis"));
316 int control = parseOutput(a->getValue("control"));
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;
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"));
328 cm->addMapping(axis, control, _currObj, opt);
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);
336 PropOut* p = new PropOut();
337 p->prop = fgGetNode(a->getValue("prop"), true);
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);
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);
352 cm->setTransitionTime(handle, time);
354 SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '"
355 << name << "' found in YASim aircraft description");
360 void FGFDM::getExternalInput(float dt)
364 _turb->setMagnitude(fgGetFloat("/environment/turbulence/magnitude-norm"));
365 _turb->update(dt, fgGetFloat("/environment/turbulence/rate-hz"));
368 ControlMap* cm = _airplane.getControlMap();
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);
376 cm->applyControls(dt);
379 for(i=0; i<_weights.size(); i++) {
380 WeightRec* wr = (WeightRec*)_weights.get(i);
381 _airplane.setWeight(wr->handle, LBS2KG * fgGetFloat(wr->prop));
384 for(i=0; i<_thrusters.size(); i++) {
385 EngRec* er = (EngRec*)_thrusters.get(i);
386 Thruster* t = er->eng;
388 if(t->getPropEngine()) {
389 PropEngine* p = t->getPropEngine();
390 sprintf(buf, "%s/rpm", er->prefix);
391 p->setOmega(fgGetFloat(buf, 500) * RPM2RAD);
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)
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);
408 void FGFDM::setOutputProperties(float dt)
413 float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
414 fgSetFloat("/yasim/gross-weight-lbs", grossWgt);
416 ControlMap* cm = _airplane.getControlMap();
417 for(i=0; i<_controlProps.size(); i++) {
418 PropOut* p = (PropOut*)_controlProps.get(i);
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);
429 for(i=0; i<_airplane.getNumRotors(); i++) {
430 Rotor*r=(Rotor*)_airplane.getRotor(i);
434 while(j = r->getValueforFGSet(j, b, &f))
435 if(b[0]) fgSetFloat(b,f);
437 for(j=0; j < r->numRotorparts(); j++) {
438 Rotorpart* s = (Rotorpart*)r->getRotorpart(j);
442 b=s->getAlphaoutput(k);
443 if(b[0]) fgSetFloat(b, s->getAlpha(k));
446 for(j=0; j < r->numRotorblades(); j++) {
447 Rotorblade* s = (Rotorblade*)r->getRotorblade(j);
450 for (k=0; k<2; k++) {
451 b = s->getAlphaoutput(k);
452 if(b[0]) fgSetFloat(b, s->getAlpha(k));
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);
463 // Set: running, cranking, prop-thrust, max-hp, power-pct
464 node->setBoolValue("running", t->isRunning());
465 node->setBoolValue("cranking", t->isCranking());
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);
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);
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());
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);
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
513 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
515 Wing* w = new Wing();
518 if(eq(type, "vstab"))
524 pos[0] = attrf(a, "x");
525 pos[1] = attrf(a, "y");
526 pos[2] = attrf(a, "z");
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);
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));
542 float effect = attrf(a, "effectiveness", 1);
543 w->setDragScale(w->getDragScale()*effect);
549 Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
551 Rotor* w = new Rotor();
553 // float defDihed = 0;
556 pos[0] = attrf(a, "x");
557 pos[1] = attrf(a, "y");
558 pos[2] = attrf(a, "z");
562 normal[0] = attrf(a, "nx");
563 normal[1] = attrf(a, "ny");
564 normal[2] = attrf(a, "nz");
565 w->setNormal(normal);
568 forward[0] = attrf(a, "fx");
569 forward[1] = attrf(a, "fy");
570 forward[2] = attrf(a, "fz");
571 w->setForward(forward);
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);
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") );
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"))
623 if(attrb(a,"simblades"))
630 void FGFDM::parsePistonEngine(XMLAttributes* a)
632 float engP = attrf(a, "eng-power") * HP2W;
633 float engS = attrf(a, "eng-rpm") * RPM2RAD;
635 PistonEngine* eng = new PistonEngine(engP, engS);
637 if(a->hasAttribute("displacement"))
638 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
640 if(a->hasAttribute("compression"))
641 eng->setCompression(attrf(a, "compression"));
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);
649 ((PropEngine*)_currObj)->setEngine(eng);
652 void FGFDM::parseTurbineEngine(XMLAttributes* a)
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);
660 if(a->hasAttribute("n2-low-idle"))
661 eng->setN2Range(attrf(a, "n2-low-idle"), attrf(a, "n2-high-idle"),
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)));
668 ((PropEngine*)_currObj)->setEngine(eng);
671 void FGFDM::parsePropeller(XMLAttributes* a)
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. "
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);
693 // Now parse the actual propeller definition:
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);
706 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
707 PropEngine* thruster = new PropEngine(prop, eng, moment);
708 _airplane.addThruster(thruster, mass, cg);
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);
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);
722 if(attrb(a, "contra"))
723 thruster->setContraPair(true);
725 if(a->hasAttribute("manual-pitch")) {
726 prop->setManualPitch();
729 thruster->setGearRatio(attrf(a, "gear-ratio", 1));
732 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
733 EngRec* er = new EngRec();
735 er->prefix = dup(buf);
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
744 int FGFDM::parseAxis(const char* name)
747 for(i=0; i<_axes.size(); i++) {
748 AxisRec* a = (AxisRec*)_axes.get(i);
749 if(eq(a->name, name))
753 // Not there, make a new one.
754 AxisRec* a = new AxisRec();
756 fgGetNode( a->name, true ); // make sure the property name exists
757 a->handle = _airplane.getControlMap()->newInput();
762 int FGFDM::parseOutput(const char* name)
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.");
798 void FGFDM::parseWeight(XMLAttributes* a)
800 WeightRec* wr = new WeightRec();
803 v[0] = attrf(a, "x");
804 v[1] = attrf(a, "y");
805 v[2] = attrf(a, "z");
807 wr->prop = dup(a->getValue("mass-prop"));
808 wr->size = attrf(a, "size", 0);
809 wr->handle = _airplane.addWeight(v, wr->size);
814 bool FGFDM::eq(const char* a, const char* b)
816 // Figure it out for yourself. :)
817 while(*a && *b && *a == *b) { a++; b++; }
821 char* FGFDM::dup(const char* s)
825 char* s2 = new char[len+1];
827 while((*p++ = *s++));
832 int FGFDM::attri(XMLAttributes* atts, char* attr)
834 if(!atts->hasAttribute(attr)) {
835 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
836 "' in YASim aircraft description");
839 return attri(atts, attr, 0);
842 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
844 const char* val = atts->getValue(attr);
845 if(val == 0) return def;
846 else return atol(val);
849 float FGFDM::attrf(XMLAttributes* atts, char* attr)
851 if(!atts->hasAttribute(attr)) {
852 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
853 "' in YASim aircraft description");
856 return attrf(atts, attr, 0);
859 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
861 const char* val = atts->getValue(attr);
862 if(val == 0) return def;
863 else return (float)atof(val);
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"...
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)
879 const char* val = atts->getValue(attr);
880 if(val == 0) return false;
883 SG_LOG(SG_FLIGHT, SG_ALERT, "Warning: " <<
884 "deprecated 'true' boolean in YASim configuration file. " <<
885 "Use numeric booleans (attribute=\"1\") instead");
888 return attri(atts, attr, 0) ? true : false;
891 }; // namespace yasim