4 #include <Main/fg_props.hxx>
8 #include "SimpleJet.hpp"
11 #include "Launchbar.hpp"
12 #include "Atmosphere.hpp"
13 #include "PropEngine.hpp"
14 #include "Propeller.hpp"
15 #include "PistonEngine.hpp"
16 #include "TurbineEngine.hpp"
18 #include "Rotorpart.hpp"
19 #include "Rotorblade.hpp"
25 // Some conversion factors
26 static const float KTS2MPS = 0.514444444444;
27 static const float FT2M = 0.3048;
28 static const float DEG2RAD = 0.0174532925199;
29 static const float RPM2RAD = 0.10471975512;
30 static const float LBS2N = 4.44822;
31 static const float LBS2KG = 0.45359237;
32 static const float KG2LBS = 2.2046225;
33 static const float CM2GALS = 264.172037284;
34 static const float HP2W = 745.700;
35 static const float INHG2PA = 3386.389;
36 static const float K2DEGF = 1.8;
37 static const float K2DEGFOFFSET = -459.4;
38 static const float CIN2CM = 1.6387064e-5;
39 static const float YASIM_PI = 3.14159265358979323846;
41 static const float NM2FTLB = (1/(LBS2N*FT2M));
43 // Stubs, so that this can be compiled without the FlightGear
44 // binary. What's the best way to handle this?
46 // float fgGetFloat(char* name, float def) { return 0; }
47 // void fgSetFloat(char* name, float val) {}
51 _vehicle_radius = 0.0f;
55 // Map /controls/flight/elevator to the approach elevator control. This
56 // should probably be settable, but there are very few aircraft
57 // who trim their approaches using things other than elevator.
58 _airplane.setElevatorControl(parseAxis("/controls/flight/elevator-trim"));
60 // FIXME: read seed from somewhere?
62 _turb = new Turbulence(10, seed);
68 for(i=0; i<_axes.size(); i++) {
69 AxisRec* a = (AxisRec*)_axes.get(i);
73 for(i=0; i<_thrusters.size(); i++) {
74 EngRec* er = (EngRec*)_thrusters.get(i);
79 for(i=0; i<_weights.size(); i++) {
80 WeightRec* wr = (WeightRec*)_weights.get(i);
84 for(i=0; i<_controlProps.size(); i++)
85 delete (PropOut*)_controlProps.get(i);
88 void FGFDM::iterate(float dt)
91 _airplane.iterate(dt);
93 // Do fuel stuff (FIXME: should stash SGPropertyNode objects here)
95 for(int i=0; i<_airplane.numThrusters(); i++) {
96 Thruster* t = _airplane.getThruster(i);
98 sprintf(buf, "/engines/engine[%d]/out-of-fuel", i);
99 t->setFuelState(!fgGetBool(buf));
101 sprintf(buf, "/engines/engine[%d]/fuel-consumed-lbs", i);
102 double consumed = fgGetDouble(buf) + dt * KG2LBS * t->getFuelFlow();
103 fgSetDouble(buf, consumed);
105 for(int i=0; i<_airplane.numTanks(); i++) {
106 sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
107 _airplane.setFuel(i, LBS2KG * fgGetFloat(buf));
109 _airplane.calcFuelWeights();
111 setOutputProperties(dt);
114 Airplane* FGFDM::getAirplane()
121 // Allows the user to start with something other than full fuel
122 _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
124 // Read out the resulting fuel state
126 for(int i=0; i<_airplane.numTanks(); i++) {
127 sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
128 fgSetDouble(buf, _airplane.getFuel(i) * KG2LBS);
130 double density = _airplane.getFuelDensity(i);
131 sprintf(buf, "/consumables/fuel/tank[%d]/density-ppg", i);
132 fgSetDouble(buf, density * (KG2LBS/CM2GALS));
134 sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
135 fgSetDouble(buf, _airplane.getFuel(i) * CM2GALS / density);
137 sprintf(buf, "/consumables/fuel/tank[%d]/capacity-gal_us", i);
138 fgSetDouble(buf, CM2GALS * _airplane.getTankCapacity(i)/density);
141 // This has a nasty habit of being false at startup. That's not
143 fgSetBool("/controls/gear/gear-down", true);
145 _airplane.getModel()->setTurbulence(_turb);
148 // Not the worlds safest parser. But it's short & sweet.
149 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
151 XMLAttributes* a = (XMLAttributes*)&atts;
155 if(eq(name, "airplane")) {
156 _airplane.setWeight(attrf(a, "mass") * LBS2KG);
157 } else if(eq(name, "approach")) {
158 float spd = attrf(a, "speed") * KTS2MPS;
159 float alt = attrf(a, "alt", 0) * FT2M;
160 float aoa = attrf(a, "aoa", 0) * DEG2RAD;
161 _airplane.setApproach(spd, alt, aoa, attrf(a, "fuel", 0.2));
163 } else if(eq(name, "cruise")) {
164 float spd = attrf(a, "speed") * KTS2MPS;
165 float alt = attrf(a, "alt") * FT2M;
166 _airplane.setCruise(spd, alt, attrf(a, "fuel", 0.5));
168 } else if(eq(name, "solve-weight")) {
169 int idx = attri(a, "idx");
170 float wgt = attrf(a, "weight") * LBS2KG;
171 _airplane.addSolutionWeight(!_cruiseCurr, idx, wgt);
172 } else if(eq(name, "cockpit")) {
173 v[0] = attrf(a, "x");
174 v[1] = attrf(a, "y");
175 v[2] = attrf(a, "z");
176 _airplane.setPilotPos(v);
177 } else if(eq(name, "rotor")) {
178 _airplane.addRotor(parseRotor(a, name));
179 } else if(eq(name, "wing")) {
180 _airplane.setWing(parseWing(a, name));
181 } else if(eq(name, "hstab")) {
182 _airplane.setTail(parseWing(a, name));
183 } else if(eq(name, "vstab") || eq(name, "mstab")) {
184 _airplane.addVStab(parseWing(a, name));
185 } else if(eq(name, "piston-engine")) {
186 parsePistonEngine(a);
187 } else if(eq(name, "turbine-engine")) {
188 parseTurbineEngine(a);
189 } else if(eq(name, "propeller")) {
191 } else if(eq(name, "thruster")) {
192 SimpleJet* j = new SimpleJet();
194 v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
196 _airplane.addThruster(j, 0, v);
197 v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
199 j->setThrust(attrf(a, "thrust") * LBS2N);
200 } else if(eq(name, "jet")) {
203 v[0] = attrf(a, "x");
204 v[1] = attrf(a, "y");
205 v[2] = attrf(a, "z");
206 float mass = attrf(a, "mass") * LBS2KG;
207 j->setMaxThrust(attrf(a, "thrust") * LBS2N,
208 attrf(a, "afterburner", 0) * LBS2N);
209 j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
210 j->setReverseThrust(attrf(a, "reverse", 0.2));
212 float n1min = attrf(a, "n1-idle", 55);
213 float n1max = attrf(a, "n1-max", 102);
214 float n2min = attrf(a, "n2-idle", 73);
215 float n2max = attrf(a, "n2-max", 103);
216 j->setRPMs(n1min, n1max, n2min, n2max);
218 j->setTSFC(attrf(a, "tsfc", 0.8));
219 if(a->hasAttribute("egt")) j->setEGT(attrf(a, "egt"));
220 if(a->hasAttribute("epr")) j->setEPR(attrf(a, "epr"));
221 if(a->hasAttribute("exhaust-speed"))
222 j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
223 if(a->hasAttribute("spool-time"))
224 j->setSpooling(attrf(a, "spool-time"));
227 _airplane.addThruster(j, mass, v);
228 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
229 EngRec* er = new EngRec();
231 er->prefix = dup(buf);
233 } else if(eq(name, "gear")) {
234 Gear* g = new Gear();
236 v[0] = attrf(a, "x");
237 v[1] = attrf(a, "y");
238 v[2] = attrf(a, "z");
240 float nrm = Math::mag3(v);
241 if (_vehicle_radius < nrm)
242 _vehicle_radius = nrm;
243 if(a->hasAttribute("upx")) {
244 v[0] = attrf(a, "upx");
245 v[1] = attrf(a, "upy");
246 v[2] = attrf(a, "upz");
253 for(int i=0; i<3; i++)
254 v[i] *= attrf(a, "compression", 1);
255 g->setCompression(v);
256 g->setBrake(attrf(a, "skid", 0));
257 g->setStaticFriction(attrf(a, "sfric", 0.8));
258 g->setDynamicFriction(attrf(a, "dfric", 0.7));
259 g->setSpring(attrf(a, "spring", 1));
260 g->setDamping(attrf(a, "damp", 1));
261 _airplane.addGear(g);
262 } else if(eq(name, "hook")) {
263 Hook* h = new Hook();
265 v[0] = attrf(a, "x");
266 v[1] = attrf(a, "y");
267 v[2] = attrf(a, "z");
269 float length = attrf(a, "length", 1.0);
270 h->setLength(length);
271 float nrm = length+Math::mag3(v);
272 if (_vehicle_radius < nrm)
273 _vehicle_radius = nrm;
274 h->setDownAngle(attrf(a, "down-angle", 70) * DEG2RAD);
275 h->setUpAngle(attrf(a, "up-angle", 0) * DEG2RAD);
276 _airplane.addHook(h);
277 } else if(eq(name, "launchbar")) {
278 Launchbar* l = new Launchbar();
280 v[0] = attrf(a, "x");
281 v[1] = attrf(a, "y");
282 v[2] = attrf(a, "z");
283 l->setLaunchbarMount(v);
284 v[0] = attrf(a, "holdback-x", v[0]);
285 v[1] = attrf(a, "holdback-y", v[1]);
286 v[2] = attrf(a, "holdback-z", v[2]);
287 l->setHoldbackMount(v);
288 float length = attrf(a, "length", 1.0);
289 l->setLength(length);
290 l->setDownAngle(attrf(a, "down-angle", 45) * DEG2RAD);
291 l->setUpAngle(attrf(a, "up-angle", -45) * DEG2RAD);
292 l->setHoldbackLength(attrf(a, "holdback-length", 2.0));
293 _airplane.addLaunchbar(l);
294 } else if(eq(name, "fuselage")) {
296 v[0] = attrf(a, "ax");
297 v[1] = attrf(a, "ay");
298 v[2] = attrf(a, "az");
299 b[0] = attrf(a, "bx");
300 b[1] = attrf(a, "by");
301 b[2] = attrf(a, "bz");
302 float taper = attrf(a, "taper", 1);
303 float mid = attrf(a, "midpoint", 0.5);
304 _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid);
305 } else if(eq(name, "tank")) {
306 v[0] = attrf(a, "x");
307 v[1] = attrf(a, "y");
308 v[2] = attrf(a, "z");
309 float density = 6.0; // gasoline, in lbs/gal
310 if(a->hasAttribute("jet")) density = 6.72;
311 density *= LBS2KG*CM2GALS;
312 _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
313 } else if(eq(name, "ballast")) {
314 v[0] = attrf(a, "x");
315 v[1] = attrf(a, "y");
316 v[2] = attrf(a, "z");
317 _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
318 } else if(eq(name, "weight")) {
320 } else if(eq(name, "stall")) {
321 Wing* w = (Wing*)_currObj;
322 w->setStall(attrf(a, "aoa") * DEG2RAD);
323 w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
324 w->setStallPeak(attrf(a, "peak", 1.5));
325 } else if(eq(name, "flap0")) {
326 ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
327 attrf(a, "lift"), attrf(a, "drag"));
328 } else if(eq(name, "flap1")) {
329 ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
330 attrf(a, "lift"), attrf(a, "drag"));
331 } else if(eq(name, "slat")) {
332 ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
333 attrf(a, "aoa"), attrf(a, "drag"));
334 } else if(eq(name, "spoiler")) {
335 ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
336 attrf(a, "lift"), attrf(a, "drag"));
337 /* } else if(eq(name, "collective")) {
338 ((Rotor*)_currObj)->setcollective(attrf(a, "min"), attrf(a, "max"));
339 } else if(eq(name, "cyclic")) {
340 ((Rotor*)_currObj)->setcyclic(attrf(a, "ail"), attrf(a, "ele"));
342 } else if(eq(name, "actionpt")) {
343 v[0] = attrf(a, "x");
344 v[1] = attrf(a, "y");
345 v[2] = attrf(a, "z");
346 ((Thruster*)_currObj)->setPosition(v);
347 } else if(eq(name, "dir")) {
348 v[0] = attrf(a, "x");
349 v[1] = attrf(a, "y");
350 v[2] = attrf(a, "z");
351 ((Thruster*)_currObj)->setDirection(v);
352 } else if(eq(name, "control-setting")) {
353 // A cruise or approach control setting
354 const char* axis = a->getValue("axis");
355 float value = attrf(a, "value", 0);
357 _airplane.addCruiseControl(parseAxis(axis), value);
359 _airplane.addApproachControl(parseAxis(axis), value);
360 } else if(eq(name, "control-input")) {
362 // A mapping of input property to a control
363 int axis = parseAxis(a->getValue("axis"));
364 int control = parseOutput(a->getValue("control"));
366 opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
367 opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
368 opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
370 ControlMap* cm = _airplane.getControlMap();
371 if(a->hasAttribute("src0")) {
372 cm->addMapping(axis, control, _currObj, opt,
373 attrf(a, "src0"), attrf(a, "src1"),
374 attrf(a, "dst0"), attrf(a, "dst1"));
376 cm->addMapping(axis, control, _currObj, opt);
378 } else if(eq(name, "control-output")) {
379 // A property output for a control on the current object
380 ControlMap* cm = _airplane.getControlMap();
381 int type = parseOutput(a->getValue("control"));
382 int handle = cm->getOutputHandle(_currObj, type);
384 PropOut* p = new PropOut();
385 p->prop = fgGetNode(a->getValue("prop"), true);
388 p->left = !(a->hasAttribute("side") &&
389 eq("right", a->getValue("side")));
390 p->min = attrf(a, "min", cm->rangeMin(type));
391 p->max = attrf(a, "max", cm->rangeMax(type));
392 _controlProps.add(p);
394 } else if(eq(name, "control-speed")) {
395 ControlMap* cm = _airplane.getControlMap();
396 int type = parseOutput(a->getValue("control"));
397 int handle = cm->getOutputHandle(_currObj, type);
398 float time = attrf(a, "transition-time", 0);
400 cm->setTransitionTime(handle, time);
402 SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '"
403 << name << "' found in YASim aircraft description");
408 void FGFDM::getExternalInput(float dt)
412 _turb->setMagnitude(fgGetFloat("/environment/turbulence/magnitude-norm"));
413 _turb->update(dt, fgGetFloat("/environment/turbulence/rate-hz"));
416 ControlMap* cm = _airplane.getControlMap();
419 for(i=0; i<_axes.size(); i++) {
420 AxisRec* a = (AxisRec*)_axes.get(i);
421 float val = fgGetFloat(a->name, 0);
422 cm->setInput(a->handle, val);
424 cm->applyControls(dt);
427 for(i=0; i<_weights.size(); i++) {
428 WeightRec* wr = (WeightRec*)_weights.get(i);
429 _airplane.setWeight(wr->handle, LBS2KG * fgGetFloat(wr->prop));
432 for(i=0; i<_thrusters.size(); i++) {
433 EngRec* er = (EngRec*)_thrusters.get(i);
434 Thruster* t = er->eng;
436 if(t->getPropEngine()) {
437 PropEngine* p = t->getPropEngine();
438 sprintf(buf, "%s/rpm", er->prefix);
439 p->setOmega(fgGetFloat(buf, 500) * RPM2RAD);
444 // Linearly "seeks" a property by the specified fraction of the way to
445 // the target value. Used to emulate "slowly changing" output values.
446 static void moveprop(SGPropertyNode* node, const char* prop,
447 float target, float frac)
449 float val = node->getFloatValue(prop);
450 if(frac > 1) frac = 1;
451 if(frac < 0) frac = 0;
452 val += (target - val) * frac;
453 node->setFloatValue(prop, val);
456 void FGFDM::setOutputProperties(float dt)
461 float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
462 fgSetFloat("/yasim/gross-weight-lbs", grossWgt);
464 ControlMap* cm = _airplane.getControlMap();
465 for(i=0; i<_controlProps.size(); i++) {
466 PropOut* p = (PropOut*)_controlProps.get(i);
468 ? cm->getOutput(p->handle)
469 : cm->getOutputR(p->handle));
470 float rmin = cm->rangeMin(p->type);
471 float rmax = cm->rangeMax(p->type);
472 float frac = (val - rmin) / (rmax - rmin);
473 val = frac*(p->max - p->min) + p->min;
474 p->prop->setFloatValue(val);
477 for(i=0; i<_airplane.getNumRotors(); i++) {
478 Rotor*r=(Rotor*)_airplane.getRotor(i);
482 while(j = r->getValueforFGSet(j, b, &f))
483 if(b[0]) fgSetFloat(b,f);
485 for(j=0; j < r->numRotorparts(); j++) {
486 Rotorpart* s = (Rotorpart*)r->getRotorpart(j);
490 b=s->getAlphaoutput(k);
491 if(b[0]) fgSetFloat(b, s->getAlpha(k));
494 for(j=0; j < r->numRotorblades(); j++) {
495 Rotorblade* s = (Rotorblade*)r->getRotorblade(j);
498 for (k=0; k<2; k++) {
499 b = s->getAlphaoutput(k);
500 if(b[0]) fgSetFloat(b, s->getAlpha(k));
505 float fuelDensity = _airplane.getFuelDensity(0); // HACK
506 for(i=0; i<_thrusters.size(); i++) {
507 EngRec* er = (EngRec*)_thrusters.get(i);
508 Thruster* t = er->eng;
509 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
511 // Set: running, cranking, prop-thrust, max-hp, power-pct
512 node->setBoolValue("running", t->isRunning());
513 node->setBoolValue("cranking", t->isCranking());
517 float lbs = Math::mag3(tmp) * (KG2LBS/9.8);
518 node->setFloatValue("prop-thrust", lbs); // Deprecated name
519 node->setFloatValue("thrust-lbs", lbs);
520 node->setFloatValue("fuel-flow-gph",
521 (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
523 if(t->getPropEngine()) {
524 PropEngine* p = t->getPropEngine();
525 node->setFloatValue("rpm", p->getOmega() * (1/RPM2RAD));
526 node->setFloatValue("torque-ftlb",
527 p->getEngine()->getTorque() * NM2FTLB);
529 if(p->getEngine()->isPistonEngine()) {
530 PistonEngine* pe = p->getEngine()->isPistonEngine();
531 node->setFloatValue("mp-osi", pe->getMP() * (1/INHG2PA));
532 node->setFloatValue("mp-inhg", pe->getMP() * (1/INHG2PA));
533 node->setFloatValue("egt-degf",
534 pe->getEGT() * K2DEGF + K2DEGFOFFSET);
535 node->setFloatValue("oil-temperature-degf",
536 pe->getOilTemp() * K2DEGF + K2DEGFOFFSET);
537 node->setFloatValue("boost-gauge-inhg",
538 pe->getBoost() * (1/INHG2PA));
539 } else if(p->getEngine()->isTurbineEngine()) {
540 TurbineEngine* te = p->getEngine()->isTurbineEngine();
541 node->setFloatValue("n2", te->getN2());
546 Jet* j = t->getJet();
547 node->setFloatValue("n1", j->getN1());
548 node->setFloatValue("n2", j->getN2());
549 node->setFloatValue("epr", j->getEPR());
550 node->setFloatValue("egt-degf",
551 j->getEGT() * K2DEGF + K2DEGFOFFSET);
553 // These are "unmodeled" values that are still needed for
554 // many cockpits. Tie them all to the N1 speed, but
555 // normalize the numbers to the range [0:1] so the
556 // cockpit code can scale them to the right values.
557 float pnorm = j->getPerfNorm();
558 moveprop(node, "oilp-norm", pnorm, dt/3); // 3s seek time
559 moveprop(node, "oilt-norm", pnorm, dt/30); // 30s
560 moveprop(node, "itt-norm", pnorm, dt/1); // 1s
565 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
567 Wing* w = new Wing();
570 if(eq(type, "vstab"))
576 pos[0] = attrf(a, "x");
577 pos[1] = attrf(a, "y");
578 pos[2] = attrf(a, "z");
581 w->setLength(attrf(a, "length"));
582 w->setChord(attrf(a, "chord"));
583 w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
584 w->setTaper(attrf(a, "taper", 1));
585 w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
586 w->setCamber(attrf(a, "camber", 0));
588 // These come in with positive indicating positive AoA, but the
589 // internals expect a rotation about the left-pointing Y axis, so
591 w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD * -1);
592 w->setTwist(attrf(a, "twist", 0) * DEG2RAD * -1);
594 // The 70% is a magic number that sorta kinda seems to match known
595 // throttle settings to approach speed.
596 w->setInducedDrag(0.7*attrf(a, "idrag", 1));
598 float effect = attrf(a, "effectiveness", 1);
599 w->setDragScale(w->getDragScale()*effect);
605 Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
607 Rotor* w = new Rotor();
609 // float defDihed = 0;
612 pos[0] = attrf(a, "x");
613 pos[1] = attrf(a, "y");
614 pos[2] = attrf(a, "z");
618 normal[0] = attrf(a, "nx");
619 normal[1] = attrf(a, "ny");
620 normal[2] = attrf(a, "nz");
621 w->setNormal(normal);
624 forward[0] = attrf(a, "fx");
625 forward[1] = attrf(a, "fy");
626 forward[2] = attrf(a, "fz");
627 w->setForward(forward);
629 w->setMaxCyclicail(attrf(a, "maxcyclicail", 7.6));
630 w->setMaxCyclicele(attrf(a, "maxcyclicele", 4.94));
631 w->setMinCyclicail(attrf(a, "mincyclicail", -7.6));
632 w->setMinCyclicele(attrf(a, "mincyclicele", -4.94));
633 w->setMaxCollective(attrf(a, "maxcollective", 15.8));
634 w->setMinCollective(attrf(a, "mincollective", -0.2));
635 w->setDiameter(attrf(a, "diameter", 10.2));
636 w->setWeightPerBlade(attrf(a, "weightperblade", 44));
637 w->setNumberOfBlades(attrf(a, "numblades", 4));
638 w->setRelBladeCenter(attrf(a, "relbladecenter", 0.7));
639 w->setDynamic(attrf(a, "dynamic", 0.7));
640 w->setDelta3(attrf(a, "delta3", 0));
641 w->setDelta(attrf(a, "delta", 0));
642 w->setTranslift(attrf(a, "translift", 0.05));
643 w->setC2(attrf(a, "dragfactor", 1));
644 w->setStepspersecond(attrf(a, "stepspersecond", 120));
645 w->setRPM(attrf(a, "rpm", 424));
646 w->setRelLenHinge(attrf(a, "rellenflaphinge", 0.07));
647 w->setAlpha0((attrf(a, "flap0", -5))*YASIM_PI/180);
648 w->setAlphamin((attrf(a, "flapmin", -15))/180*YASIM_PI);
649 w->setAlphamax((attrf(a, "flapmax", 15))*YASIM_PI/180);
650 w->setAlpha0factor(attrf(a, "flap0factor", 1));
651 w->setTeeterdamp(attrf(a,"teeterdamp",.0001));
652 w->setMaxteeterdamp(attrf(a,"maxteeterdamp",1000));
653 w->setRelLenTeeterHinge(attrf(a,"rellenteeterhinge",0.01));
654 void setAlphamin(float f);
655 void setAlphamax(float f);
656 void setAlpha0factor(float f);
661 if(a->hasAttribute("name"))
662 w->setName(a->getValue("name") );
663 if(a->hasAttribute("alphaout0"))
664 w->setAlphaoutput(0,a->getValue("alphaout0") );
665 if(a->hasAttribute("alphaout1")) w->setAlphaoutput(1,a->getValue("alphaout1") );
666 if(a->hasAttribute("alphaout2")) w->setAlphaoutput(2,a->getValue("alphaout2") );
667 if(a->hasAttribute("alphaout3")) w->setAlphaoutput(3,a->getValue("alphaout3") );
668 if(a->hasAttribute("coneout")) w->setAlphaoutput(4,a->getValue("coneout") );
669 if(a->hasAttribute("yawout")) w->setAlphaoutput(5,a->getValue("yawout") );
670 if(a->hasAttribute("rollout")) w->setAlphaoutput(6,a->getValue("rollout") );
672 w->setPitchA(attrf(a, "pitch_a", 10));
673 w->setPitchB(attrf(a, "pitch_b", 10));
674 w->setForceAtPitchA(attrf(a, "forceatpitch_a", 3000));
675 w->setPowerAtPitch0(attrf(a, "poweratpitch_0", 300));
676 w->setPowerAtPitchB(attrf(a, "poweratpitch_b", 3000));
677 if(attrb(a,"notorque"))
679 if(attrb(a,"simblades"))
686 void FGFDM::parsePistonEngine(XMLAttributes* a)
688 float engP = attrf(a, "eng-power") * HP2W;
689 float engS = attrf(a, "eng-rpm") * RPM2RAD;
691 PistonEngine* eng = new PistonEngine(engP, engS);
693 if(a->hasAttribute("displacement"))
694 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
696 if(a->hasAttribute("compression"))
697 eng->setCompression(attrf(a, "compression"));
699 if(a->hasAttribute("turbo-mul")) {
700 float mul = attrf(a, "turbo-mul");
701 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
702 eng->setTurboParams(mul, mp);
703 eng->setTurboLag(attrf(a, "turbo-lag", 2));
706 if(a->hasAttribute("supercharger"))
707 eng->setSupercharger(attrb(a, "supercharger"));
709 ((PropEngine*)_currObj)->setEngine(eng);
712 void FGFDM::parseTurbineEngine(XMLAttributes* a)
714 float power = attrf(a, "eng-power") * HP2W;
715 float omega = attrf(a, "eng-rpm") * RPM2RAD;
716 float alt = attrf(a, "alt") * FT2M;
717 float flatRating = attrf(a, "flat-rating") * HP2W;
718 TurbineEngine* eng = new TurbineEngine(power, omega, alt, flatRating);
720 if(a->hasAttribute("n2-low-idle"))
721 eng->setN2Range(attrf(a, "n2-low-idle"), attrf(a, "n2-high-idle"),
724 // Nasty units conversion: lbs/hr per hp -> kg/s per watt
725 if(a->hasAttribute("bsfc"))
726 eng->setFuelConsumption(attrf(a, "bsfc") * (LBS2KG/(3600*HP2W)));
728 ((PropEngine*)_currObj)->setEngine(eng);
731 void FGFDM::parsePropeller(XMLAttributes* a)
733 // Legacy Handling for the old engines syntax:
734 PistonEngine* eng = 0;
735 if(a->hasAttribute("eng-power")) {
736 SG_LOG(SG_FLIGHT,SG_ALERT, "WARNING: "
737 << "Legacy engine definition in YASim configuration file. "
739 float engP = attrf(a, "eng-power") * HP2W;
740 float engS = attrf(a, "eng-rpm") * RPM2RAD;
741 eng = new PistonEngine(engP, engS);
742 if(a->hasAttribute("displacement"))
743 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
744 if(a->hasAttribute("compression"))
745 eng->setCompression(attrf(a, "compression"));
746 if(a->hasAttribute("turbo-mul")) {
747 float mul = attrf(a, "turbo-mul");
748 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
749 eng->setTurboParams(mul, mp);
753 // Now parse the actual propeller definition:
755 cg[0] = attrf(a, "x");
756 cg[1] = attrf(a, "y");
757 cg[2] = attrf(a, "z");
758 float mass = attrf(a, "mass") * LBS2KG;
759 float moment = attrf(a, "moment");
760 float radius = attrf(a, "radius");
761 float speed = attrf(a, "cruise-speed") * KTS2MPS;
762 float omega = attrf(a, "cruise-rpm") * RPM2RAD;
763 float power = attrf(a, "cruise-power") * HP2W;
764 float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
766 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
767 PropEngine* thruster = new PropEngine(prop, eng, moment);
768 _airplane.addThruster(thruster, mass, cg);
770 // Set the stops (fine = minimum pitch, coarse = maximum pitch)
771 float fine_stop = attrf(a, "fine-stop", 0.25f);
772 float coarse_stop = attrf(a, "coarse-stop", 4.0f);
773 prop->setStops(fine_stop, coarse_stop);
775 if(a->hasAttribute("takeoff-power")) {
776 float power0 = attrf(a, "takeoff-power") * HP2W;
777 float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
778 prop->setTakeoff(omega0, power0);
781 if(a->hasAttribute("max-rpm")) {
782 float max = attrf(a, "max-rpm") * RPM2RAD;
783 float min = attrf(a, "min-rpm") * RPM2RAD;
784 thruster->setVariableProp(min, max);
787 if(attrb(a, "contra"))
788 thruster->setContraPair(true);
790 if(a->hasAttribute("manual-pitch")) {
791 prop->setManualPitch();
794 thruster->setGearRatio(attrf(a, "gear-ratio", 1));
797 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
798 EngRec* er = new EngRec();
800 er->prefix = dup(buf);
806 // Turns a string axis name into an integer for use by the
807 // ControlMap. Creates a new axis if this one hasn't been defined
809 int FGFDM::parseAxis(const char* name)
812 for(i=0; i<_axes.size(); i++) {
813 AxisRec* a = (AxisRec*)_axes.get(i);
814 if(eq(a->name, name))
818 // Not there, make a new one.
819 AxisRec* a = new AxisRec();
821 fgGetNode( a->name, true ); // make sure the property name exists
822 a->handle = _airplane.getControlMap()->newInput();
827 int FGFDM::parseOutput(const char* name)
829 if(eq(name, "THROTTLE")) return ControlMap::THROTTLE;
830 if(eq(name, "MIXTURE")) return ControlMap::MIXTURE;
831 if(eq(name, "CONDLEVER")) return ControlMap::CONDLEVER;
832 if(eq(name, "STARTER")) return ControlMap::STARTER;
833 if(eq(name, "MAGNETOS")) return ControlMap::MAGNETOS;
834 if(eq(name, "ADVANCE")) return ControlMap::ADVANCE;
835 if(eq(name, "REHEAT")) return ControlMap::REHEAT;
836 if(eq(name, "BOOST")) return ControlMap::BOOST;
837 if(eq(name, "VECTOR")) return ControlMap::VECTOR;
838 if(eq(name, "PROP")) return ControlMap::PROP;
839 if(eq(name, "BRAKE")) return ControlMap::BRAKE;
840 if(eq(name, "STEER")) return ControlMap::STEER;
841 if(eq(name, "EXTEND")) return ControlMap::EXTEND;
842 if(eq(name, "HEXTEND")) return ControlMap::HEXTEND;
843 if(eq(name, "LEXTEND")) return ControlMap::LEXTEND;
844 if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
845 if(eq(name, "FLAP0")) return ControlMap::FLAP0;
846 if(eq(name, "FLAP1")) return ControlMap::FLAP1;
847 if(eq(name, "SLAT")) return ControlMap::SLAT;
848 if(eq(name, "SPOILER")) return ControlMap::SPOILER;
849 if(eq(name, "CASTERING")) return ControlMap::CASTERING;
850 if(eq(name, "PROPPITCH")) return ControlMap::PROPPITCH;
851 if(eq(name, "PROPFEATHER")) return ControlMap::PROPFEATHER;
852 if(eq(name, "COLLECTIVE")) return ControlMap::COLLECTIVE;
853 if(eq(name, "CYCLICAIL")) return ControlMap::CYCLICAIL;
854 if(eq(name, "CYCLICELE")) return ControlMap::CYCLICELE;
855 if(eq(name, "ROTORENGINEON")) return ControlMap::ROTORENGINEON;
856 if(eq(name, "REVERSE_THRUST")) return ControlMap::REVERSE_THRUST;
857 if(eq(name, "WASTEGATE")) return ControlMap::WASTEGATE;
858 SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
859 << name << "' in YASim aircraft description.");
864 void FGFDM::parseWeight(XMLAttributes* a)
866 WeightRec* wr = new WeightRec();
869 v[0] = attrf(a, "x");
870 v[1] = attrf(a, "y");
871 v[2] = attrf(a, "z");
873 wr->prop = dup(a->getValue("mass-prop"));
874 wr->size = attrf(a, "size", 0);
875 wr->handle = _airplane.addWeight(v, wr->size);
880 bool FGFDM::eq(const char* a, const char* b)
882 // Figure it out for yourself. :)
883 while(*a && *b && *a == *b) { a++; b++; }
887 char* FGFDM::dup(const char* s)
891 char* s2 = new char[len+1];
893 while((*p++ = *s++));
898 int FGFDM::attri(XMLAttributes* atts, char* attr)
900 if(!atts->hasAttribute(attr)) {
901 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
902 "' in YASim aircraft description");
905 return attri(atts, attr, 0);
908 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
910 const char* val = atts->getValue(attr);
911 if(val == 0) return def;
912 else return atol(val);
915 float FGFDM::attrf(XMLAttributes* atts, char* attr)
917 if(!atts->hasAttribute(attr)) {
918 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
919 "' in YASim aircraft description");
922 return attrf(atts, attr, 0);
925 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
927 const char* val = atts->getValue(attr);
928 if(val == 0) return def;
929 else return (float)atof(val);
932 // ACK: the dreaded ambiguous string boolean. Remind me to shoot Maik
933 // when I have a chance. :). Unless you have a parser that can check
934 // symbol constants (we don't), this kind of coding is just a Bad
935 // Idea. This implementation, for example, silently returns a boolean
936 // falsehood for values of "1", "yes", "True", and "TRUE". Which is
937 // especially annoying preexisting boolean attributes in the same
938 // parser want to see "1" and will choke on a "true"...
940 // Unfortunately, this usage creeped into existing configuration files
941 // while I wasn't active, and it's going to be hard to remove. Issue
942 // a warning to nag people into changing their ways for now...
943 bool FGFDM::attrb(XMLAttributes* atts, char* attr)
945 const char* val = atts->getValue(attr);
946 if(val == 0) return false;
949 SG_LOG(SG_FLIGHT, SG_ALERT, "Warning: " <<
950 "deprecated 'true' boolean in YASim configuration file. " <<
951 "Use numeric booleans (attribute=\"1\") instead");
954 return attri(atts, attr, 0) ? true : false;
957 }; // namespace yasim