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();
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, "fuselage")) {
246 v[0] = attrf(a, "ax");
247 v[1] = attrf(a, "ay");
248 v[2] = attrf(a, "az");
249 b[0] = attrf(a, "bx");
250 b[1] = attrf(a, "by");
251 b[2] = attrf(a, "bz");
252 float taper = attrf(a, "taper", 1);
253 float mid = attrf(a, "midpoint", 0.5);
254 _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid);
255 } else if(eq(name, "tank")) {
256 v[0] = attrf(a, "x");
257 v[1] = attrf(a, "y");
258 v[2] = attrf(a, "z");
259 float density = 6.0; // gasoline, in lbs/gal
260 if(a->hasAttribute("jet")) density = 6.72;
261 density *= LBS2KG*CM2GALS;
262 _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
263 } else if(eq(name, "ballast")) {
264 v[0] = attrf(a, "x");
265 v[1] = attrf(a, "y");
266 v[2] = attrf(a, "z");
267 _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
268 } else if(eq(name, "weight")) {
270 } else if(eq(name, "stall")) {
271 Wing* w = (Wing*)_currObj;
272 w->setStall(attrf(a, "aoa") * DEG2RAD);
273 w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
274 w->setStallPeak(attrf(a, "peak", 1.5));
275 } else if(eq(name, "flap0")) {
276 ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
277 attrf(a, "lift"), attrf(a, "drag"));
278 } else if(eq(name, "flap1")) {
279 ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
280 attrf(a, "lift"), attrf(a, "drag"));
281 } else if(eq(name, "slat")) {
282 ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
283 attrf(a, "aoa"), attrf(a, "drag"));
284 } else if(eq(name, "spoiler")) {
285 ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
286 attrf(a, "lift"), attrf(a, "drag"));
287 /* } else if(eq(name, "collective")) {
288 ((Rotor*)_currObj)->setcollective(attrf(a, "min"), attrf(a, "max"));
289 } else if(eq(name, "cyclic")) {
290 ((Rotor*)_currObj)->setcyclic(attrf(a, "ail"), attrf(a, "ele"));
292 } else if(eq(name, "actionpt")) {
293 v[0] = attrf(a, "x");
294 v[1] = attrf(a, "y");
295 v[2] = attrf(a, "z");
296 ((Thruster*)_currObj)->setPosition(v);
297 } else if(eq(name, "dir")) {
298 v[0] = attrf(a, "x");
299 v[1] = attrf(a, "y");
300 v[2] = attrf(a, "z");
301 ((Thruster*)_currObj)->setDirection(v);
302 } else if(eq(name, "control-setting")) {
303 // A cruise or approach control setting
304 const char* axis = a->getValue("axis");
305 float value = attrf(a, "value", 0);
307 _airplane.addCruiseControl(parseAxis(axis), value);
309 _airplane.addApproachControl(parseAxis(axis), value);
310 } else if(eq(name, "control-input")) {
312 // A mapping of input property to a control
313 int axis = parseAxis(a->getValue("axis"));
314 int control = parseOutput(a->getValue("control"));
316 opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
317 opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
318 opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
320 ControlMap* cm = _airplane.getControlMap();
321 if(a->hasAttribute("src0")) {
322 cm->addMapping(axis, control, _currObj, opt,
323 attrf(a, "src0"), attrf(a, "src1"),
324 attrf(a, "dst0"), attrf(a, "dst1"));
326 cm->addMapping(axis, control, _currObj, opt);
328 } else if(eq(name, "control-output")) {
329 // A property output for a control on the current object
330 ControlMap* cm = _airplane.getControlMap();
331 int type = parseOutput(a->getValue("control"));
332 int handle = cm->getOutputHandle(_currObj, type);
334 PropOut* p = new PropOut();
335 p->prop = fgGetNode(a->getValue("prop"), true);
338 p->left = !(a->hasAttribute("side") &&
339 eq("right", a->getValue("side")));
340 p->min = attrf(a, "min", cm->rangeMin(type));
341 p->max = attrf(a, "max", cm->rangeMax(type));
342 _controlProps.add(p);
344 } else if(eq(name, "control-speed")) {
345 ControlMap* cm = _airplane.getControlMap();
346 int type = parseOutput(a->getValue("control"));
347 int handle = cm->getOutputHandle(_currObj, type);
348 float time = attrf(a, "transition-time", 0);
350 cm->setTransitionTime(handle, time);
352 SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '"
353 << name << "' found in YASim aircraft description");
358 void FGFDM::getExternalInput(float dt)
362 _turb->setMagnitude(fgGetFloat("/environment/turbulence/magnitude-norm"));
363 _turb->update(dt, fgGetFloat("/environment/turbulence/rate-hz"));
366 ControlMap* cm = _airplane.getControlMap();
369 for(i=0; i<_axes.size(); i++) {
370 AxisRec* a = (AxisRec*)_axes.get(i);
371 float val = fgGetFloat(a->name, 0);
372 cm->setInput(a->handle, val);
374 cm->applyControls(dt);
377 for(i=0; i<_weights.size(); i++) {
378 WeightRec* wr = (WeightRec*)_weights.get(i);
379 _airplane.setWeight(wr->handle, LBS2KG * fgGetFloat(wr->prop));
382 for(i=0; i<_thrusters.size(); i++) {
383 EngRec* er = (EngRec*)_thrusters.get(i);
384 Thruster* t = er->eng;
386 if(t->getPropEngine()) {
387 PropEngine* p = t->getPropEngine();
388 sprintf(buf, "%s/rpm", er->prefix);
389 p->setOmega(fgGetFloat(buf, 500) * RPM2RAD);
394 void FGFDM::setOutputProperties()
399 float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
400 fgSetFloat("/yasim/gross-weight-lbs", grossWgt);
402 ControlMap* cm = _airplane.getControlMap();
403 for(i=0; i<_controlProps.size(); i++) {
404 PropOut* p = (PropOut*)_controlProps.get(i);
406 ? cm->getOutput(p->handle)
407 : cm->getOutputR(p->handle));
408 float rmin = cm->rangeMin(p->type);
409 float rmax = cm->rangeMax(p->type);
410 float frac = (val - rmin) / (rmax - rmin);
411 val = frac*(p->max - p->min) + p->min;
412 p->prop->setFloatValue(val);
415 for(i=0; i<_airplane.getNumRotors(); i++) {
416 Rotor*r=(Rotor*)_airplane.getRotor(i);
420 while(j = r->getValueforFGSet(j, b, &f))
421 if(b[0]) fgSetFloat(b,f);
423 for(j=0; j < r->numRotorparts(); j++) {
424 Rotorpart* s = (Rotorpart*)r->getRotorpart(j);
428 b=s->getAlphaoutput(k);
429 if(b[0]) fgSetFloat(b, s->getAlpha(k));
432 for(j=0; j < r->numRotorblades(); j++) {
433 Rotorblade* s = (Rotorblade*)r->getRotorblade(j);
436 for (k=0; k<2; k++) {
437 b = s->getAlphaoutput(k);
438 if(b[0]) fgSetFloat(b, s->getAlpha(k));
443 float fuelDensity = _airplane.getFuelDensity(0); // HACK
444 for(i=0; i<_thrusters.size(); i++) {
445 EngRec* er = (EngRec*)_thrusters.get(i);
446 Thruster* t = er->eng;
447 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
449 // Set: running, cranking, prop-thrust, max-hp, power-pct
450 node->setBoolValue("running", t->isRunning());
451 node->setBoolValue("cranking", t->isCranking());
455 float lbs = Math::mag3(tmp) * (KG2LBS/9.8);
456 node->setFloatValue("prop-thrust", lbs); // Deprecated name
457 node->setFloatValue("thrust-lbs", lbs);
458 node->setFloatValue("fuel-flow-gph",
459 (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
461 if(t->getPropEngine()) {
462 PropEngine* p = t->getPropEngine();
463 node->setFloatValue("rpm", p->getOmega() * (1/RPM2RAD));
464 node->setFloatValue("torque-ftlb",
465 p->getEngine()->getTorque() * NM2FTLB);
467 if(p->getEngine()->isPistonEngine()) {
468 PistonEngine* pe = p->getEngine()->isPistonEngine();
469 node->setFloatValue("mp-osi", pe->getMP() * (1/INHG2PA));
470 node->setFloatValue("mp-inhg", pe->getMP() * (1/INHG2PA));
471 node->setFloatValue("egt-degf",
472 pe->getEGT() * K2DEGF + K2DEGFOFFSET);
473 } else if(p->getEngine()->isTurbineEngine()) {
474 TurbineEngine* te = p->getEngine()->isTurbineEngine();
475 node->setFloatValue("n2", te->getN2());
480 Jet* j = t->getJet();
481 node->setFloatValue("n1", j->getN1());
482 node->setFloatValue("n2", j->getN2());
483 node->setFloatValue("epr", j->getEPR());
484 node->setFloatValue("egr-degf",
485 j->getEGT() * K2DEGF + K2DEGFOFFSET);
490 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
492 Wing* w = new Wing();
495 if(eq(type, "vstab"))
501 pos[0] = attrf(a, "x");
502 pos[1] = attrf(a, "y");
503 pos[2] = attrf(a, "z");
506 w->setLength(attrf(a, "length"));
507 w->setChord(attrf(a, "chord"));
508 w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
509 w->setTaper(attrf(a, "taper", 1));
510 w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
511 w->setCamber(attrf(a, "camber", 0));
512 w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
513 w->setTwist(attrf(a, "twist", 0) * DEG2RAD);
515 // The 70% is a magic number that sorta kinda seems to match known
516 // throttle settings to approach speed.
517 w->setInducedDrag(0.7*attrf(a, "idrag", 1));
519 float effect = attrf(a, "effectiveness", 1);
520 w->setDragScale(w->getDragScale()*effect);
526 Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
528 Rotor* w = new Rotor();
530 // float defDihed = 0;
533 pos[0] = attrf(a, "x");
534 pos[1] = attrf(a, "y");
535 pos[2] = attrf(a, "z");
539 normal[0] = attrf(a, "nx");
540 normal[1] = attrf(a, "ny");
541 normal[2] = attrf(a, "nz");
542 w->setNormal(normal);
545 forward[0] = attrf(a, "fx");
546 forward[1] = attrf(a, "fy");
547 forward[2] = attrf(a, "fz");
548 w->setForward(forward);
550 w->setMaxCyclicail(attrf(a, "maxcyclicail", 7.6));
551 w->setMaxCyclicele(attrf(a, "maxcyclicele", 4.94));
552 w->setMinCyclicail(attrf(a, "mincyclicail", -7.6));
553 w->setMinCyclicele(attrf(a, "mincyclicele", -4.94));
554 w->setMaxCollective(attrf(a, "maxcollective", 15.8));
555 w->setMinCollective(attrf(a, "mincollective", -0.2));
556 w->setDiameter(attrf(a, "diameter", 10.2));
557 w->setWeightPerBlade(attrf(a, "weightperblade", 44));
558 w->setNumberOfBlades(attrf(a, "numblades", 4));
559 w->setRelBladeCenter(attrf(a, "relbladecenter", 0.7));
560 w->setDynamic(attrf(a, "dynamic", 0.7));
561 w->setDelta3(attrf(a, "delta3", 0));
562 w->setDelta(attrf(a, "delta", 0));
563 w->setTranslift(attrf(a, "translift", 0.05));
564 w->setC2(attrf(a, "dragfactor", 1));
565 w->setStepspersecond(attrf(a, "stepspersecond", 120));
566 w->setRPM(attrf(a, "rpm", 424));
567 w->setRelLenHinge(attrf(a, "rellenflaphinge", 0.07));
568 w->setAlpha0((attrf(a, "flap0", -5))*YASIM_PI/180);
569 w->setAlphamin((attrf(a, "flapmin", -15))/180*YASIM_PI);
570 w->setAlphamax((attrf(a, "flapmax", 15))*YASIM_PI/180);
571 w->setAlpha0factor(attrf(a, "flap0factor", 1));
572 w->setTeeterdamp(attrf(a,"teeterdamp",.0001));
573 w->setMaxteeterdamp(attrf(a,"maxteeterdamp",1000));
574 w->setRelLenTeeterHinge(attrf(a,"rellenteeterhinge",0.01));
575 void setAlphamin(float f);
576 void setAlphamax(float f);
577 void setAlpha0factor(float f);
582 if(a->hasAttribute("name"))
583 w->setName(a->getValue("name") );
584 if(a->hasAttribute("alphaout0"))
585 w->setAlphaoutput(0,a->getValue("alphaout0") );
586 if(a->hasAttribute("alphaout1")) w->setAlphaoutput(1,a->getValue("alphaout1") );
587 if(a->hasAttribute("alphaout2")) w->setAlphaoutput(2,a->getValue("alphaout2") );
588 if(a->hasAttribute("alphaout3")) w->setAlphaoutput(3,a->getValue("alphaout3") );
589 if(a->hasAttribute("coneout")) w->setAlphaoutput(4,a->getValue("coneout") );
590 if(a->hasAttribute("yawout")) w->setAlphaoutput(5,a->getValue("yawout") );
591 if(a->hasAttribute("rollout")) w->setAlphaoutput(6,a->getValue("rollout") );
593 w->setPitchA(attrf(a, "pitch_a", 10));
594 w->setPitchB(attrf(a, "pitch_b", 10));
595 w->setForceAtPitchA(attrf(a, "forceatpitch_a", 3000));
596 w->setPowerAtPitch0(attrf(a, "poweratpitch_0", 300));
597 w->setPowerAtPitchB(attrf(a, "poweratpitch_b", 3000));
598 if(attrb(a,"notorque"))
600 if(attrb(a,"simblades"))
607 void FGFDM::parsePistonEngine(XMLAttributes* a)
609 float engP = attrf(a, "eng-power") * HP2W;
610 float engS = attrf(a, "eng-rpm") * RPM2RAD;
612 PistonEngine* eng = new PistonEngine(engP, engS);
614 if(a->hasAttribute("displacement"))
615 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
617 if(a->hasAttribute("compression"))
618 eng->setCompression(attrf(a, "compression"));
620 if(a->hasAttribute("turbo-mul")) {
621 float mul = attrf(a, "turbo-mul");
622 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
623 eng->setTurboParams(mul, mp);
626 ((PropEngine*)_currObj)->setEngine(eng);
629 void FGFDM::parseTurbineEngine(XMLAttributes* a)
631 float power = attrf(a, "eng-power") * HP2W;
632 float omega = attrf(a, "eng-rpm") * RPM2RAD;
633 float alt = attrf(a, "alt") * FT2M;
634 float flatRating = attrf(a, "flat-rating") * HP2W;
635 TurbineEngine* eng = new TurbineEngine(power, omega, alt, flatRating);
637 if(a->hasAttribute("min-n2"))
638 eng->setN2Range(attrf(a, "min-n2"), attrf(a, "max-n2"));
640 // Nasty units conversion: lbs/hr per hp -> kg/s per watt
641 if(a->hasAttribute("bsfc"))
642 eng->setFuelConsumption(attrf(a, "bsfc") * (LBS2KG/(3600*HP2W)));
644 ((PropEngine*)_currObj)->setEngine(eng);
647 void FGFDM::parsePropeller(XMLAttributes* a)
649 // Legacy Handling for the old engines syntax:
650 PistonEngine* eng = 0;
651 if(a->hasAttribute("eng-power")) {
652 SG_LOG(SG_FLIGHT,SG_ALERT, "WARNING: "
653 << "Legacy engine definition in YASim configuration file. "
655 float engP = attrf(a, "eng-power") * HP2W;
656 float engS = attrf(a, "eng-rpm") * RPM2RAD;
657 eng = new PistonEngine(engP, engS);
658 if(a->hasAttribute("displacement"))
659 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
660 if(a->hasAttribute("compression"))
661 eng->setCompression(attrf(a, "compression"));
662 if(a->hasAttribute("turbo-mul")) {
663 float mul = attrf(a, "turbo-mul");
664 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
665 eng->setTurboParams(mul, mp);
669 // Now parse the actual propeller definition:
671 cg[0] = attrf(a, "x");
672 cg[1] = attrf(a, "y");
673 cg[2] = attrf(a, "z");
674 float mass = attrf(a, "mass") * LBS2KG;
675 float moment = attrf(a, "moment");
676 float radius = attrf(a, "radius");
677 float speed = attrf(a, "cruise-speed") * KTS2MPS;
678 float omega = attrf(a, "cruise-rpm") * RPM2RAD;
679 float power = attrf(a, "cruise-power") * HP2W;
680 float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
682 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
683 PropEngine* thruster = new PropEngine(prop, eng, moment);
684 _airplane.addThruster(thruster, mass, cg);
686 if(a->hasAttribute("takeoff-power")) {
687 float power0 = attrf(a, "takeoff-power") * HP2W;
688 float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
689 prop->setTakeoff(omega0, power0);
692 if(a->hasAttribute("max-rpm")) {
693 float max = attrf(a, "max-rpm") * RPM2RAD;
694 float min = attrf(a, "min-rpm") * RPM2RAD;
695 thruster->setVariableProp(min, max);
698 if(a->hasAttribute("manual-pitch")) {
699 prop->setManualPitch();
702 thruster->setGearRatio(attrf(a, "gear-ratio", 1));
705 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
706 EngRec* er = new EngRec();
708 er->prefix = dup(buf);
714 // Turns a string axis name into an integer for use by the
715 // ControlMap. Creates a new axis if this one hasn't been defined
717 int FGFDM::parseAxis(const char* name)
720 for(i=0; i<_axes.size(); i++) {
721 AxisRec* a = (AxisRec*)_axes.get(i);
722 if(eq(a->name, name))
726 // Not there, make a new one.
727 AxisRec* a = new AxisRec();
729 a->handle = _airplane.getControlMap()->newInput();
734 int FGFDM::parseOutput(const char* name)
736 if(eq(name, "THROTTLE")) return ControlMap::THROTTLE;
737 if(eq(name, "MIXTURE")) return ControlMap::MIXTURE;
738 if(eq(name, "STARTER")) return ControlMap::STARTER;
739 if(eq(name, "MAGNETOS")) return ControlMap::MAGNETOS;
740 if(eq(name, "ADVANCE")) return ControlMap::ADVANCE;
741 if(eq(name, "REHEAT")) return ControlMap::REHEAT;
742 if(eq(name, "BOOST")) return ControlMap::BOOST;
743 if(eq(name, "VECTOR")) return ControlMap::VECTOR;
744 if(eq(name, "PROP")) return ControlMap::PROP;
745 if(eq(name, "BRAKE")) return ControlMap::BRAKE;
746 if(eq(name, "STEER")) return ControlMap::STEER;
747 if(eq(name, "EXTEND")) return ControlMap::EXTEND;
748 if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
749 if(eq(name, "FLAP0")) return ControlMap::FLAP0;
750 if(eq(name, "FLAP1")) return ControlMap::FLAP1;
751 if(eq(name, "SLAT")) return ControlMap::SLAT;
752 if(eq(name, "SPOILER")) return ControlMap::SPOILER;
753 if(eq(name, "CASTERING")) return ControlMap::CASTERING;
754 if(eq(name, "PROPPITCH")) return ControlMap::PROPPITCH;
755 if(eq(name, "COLLECTIVE")) return ControlMap::COLLECTIVE;
756 if(eq(name, "CYCLICAIL")) return ControlMap::CYCLICAIL;
757 if(eq(name, "CYCLICELE")) return ControlMap::CYCLICELE;
758 if(eq(name, "ROTORENGINEON")) return ControlMap::ROTORENGINEON;
759 if(eq(name, "REVERSE_THRUST")) return ControlMap::REVERSE_THRUST;
760 SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
761 << name << "' in YASim aircraft description.");
766 void FGFDM::parseWeight(XMLAttributes* a)
768 WeightRec* wr = new WeightRec();
771 v[0] = attrf(a, "x");
772 v[1] = attrf(a, "y");
773 v[2] = attrf(a, "z");
775 wr->prop = dup(a->getValue("mass-prop"));
776 wr->size = attrf(a, "size", 0);
777 wr->handle = _airplane.addWeight(v, wr->size);
782 bool FGFDM::eq(const char* a, const char* b)
784 // Figure it out for yourself. :)
785 while(*a && *b && *a == *b) { a++; b++; }
789 char* FGFDM::dup(const char* s)
793 char* s2 = new char[len+1];
795 while((*p++ = *s++));
800 int FGFDM::attri(XMLAttributes* atts, char* attr)
802 if(!atts->hasAttribute(attr)) {
803 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
804 "' in YASim aircraft description");
807 return attri(atts, attr, 0);
810 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
812 const char* val = atts->getValue(attr);
813 if(val == 0) return def;
814 else return atol(val);
817 float FGFDM::attrf(XMLAttributes* atts, char* attr)
819 if(!atts->hasAttribute(attr)) {
820 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
821 "' in YASim aircraft description");
824 return attrf(atts, attr, 0);
827 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
829 const char* val = atts->getValue(attr);
830 if(val == 0) return def;
831 else return (float)atof(val);
834 // ACK: the dreaded ambiguous string boolean. Remind me to shoot Maik
835 // when I have a chance. :). Unless you have a parser that can check
836 // symbol constants (we don't), this kind of coding is just a Bad
837 // Idea. This implementation, for example, silently returns a boolean
838 // falsehood for values of "1", "yes", "True", and "TRUE". Which is
839 // especially annoying preexisting boolean attributes in the same
840 // parser want to see "1" and will choke on a "true"...
842 // Unfortunately, this usage creeped into existing configuration files
843 // while I wasn't active, and it's going to be hard to remove. Issue
844 // a warning to nag people into changing their ways for now...
845 bool FGFDM::attrb(XMLAttributes* atts, char* attr)
847 const char* val = atts->getValue(attr);
848 if(val == 0) return false;
851 SG_LOG(SG_FLIGHT, SG_ALERT, "Warning: " <<
852 "deprecated 'true' boolean in YASim configuration file. " <<
853 "Use numeric booleans (attribute=\"1\") instead");
856 return attri(atts, attr, 0) ? true : false;
859 }; // namespace yasim