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"
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);
89 void FGFDM::iterate(float dt)
92 _airplane.iterate(dt);
94 // Do fuel stuff (FIXME: should stash SGPropertyNode objects here)
96 for(int i=0; i<_airplane.numThrusters(); i++) {
97 Thruster* t = _airplane.getThruster(i);
99 sprintf(buf, "/engines/engine[%d]/out-of-fuel", i);
100 t->setFuelState(!fgGetBool(buf));
102 sprintf(buf, "/engines/engine[%d]/fuel-consumed-lbs", i);
103 double consumed = fgGetDouble(buf) + dt * KG2LBS * t->getFuelFlow();
104 fgSetDouble(buf, consumed);
106 for(int i=0; i<_airplane.numTanks(); i++) {
107 sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
108 _airplane.setFuel(i, LBS2KG * fgGetFloat(buf));
110 _airplane.calcFuelWeights();
112 setOutputProperties(dt);
115 Airplane* FGFDM::getAirplane()
122 // Allows the user to start with something other than full fuel
123 _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
125 // Read out the resulting fuel state
127 for(int i=0; i<_airplane.numTanks(); i++) {
128 sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
129 fgSetDouble(buf, _airplane.getFuel(i) * KG2LBS);
131 double density = _airplane.getFuelDensity(i);
132 sprintf(buf, "/consumables/fuel/tank[%d]/density-ppg", i);
133 fgSetDouble(buf, density * (KG2LBS/CM2GALS));
135 sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
136 fgSetDouble(buf, _airplane.getFuel(i) * CM2GALS / density);
138 sprintf(buf, "/consumables/fuel/tank[%d]/capacity-gal_us", i);
139 fgSetDouble(buf, CM2GALS * _airplane.getTankCapacity(i)/density);
142 // This has a nasty habit of being false at startup. That's not
144 fgSetBool("/controls/gear/gear-down", true);
146 _airplane.getModel()->setTurbulence(_turb);
149 // Not the worlds safest parser. But it's short & sweet.
150 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
152 XMLAttributes* a = (XMLAttributes*)&atts;
156 if(eq(name, "airplane")) {
157 _airplane.setWeight(attrf(a, "mass") * LBS2KG);
158 } else if(eq(name, "approach")) {
159 float spd = attrf(a, "speed") * KTS2MPS;
160 float alt = attrf(a, "alt", 0) * FT2M;
161 float aoa = attrf(a, "aoa", 0) * DEG2RAD;
162 float gla = attrf(a, "glide-angle", 0) * DEG2RAD;
163 _airplane.setApproach(spd, alt, aoa, attrf(a, "fuel", 0.2),gla);
165 } else if(eq(name, "cruise")) {
166 float spd = attrf(a, "speed") * KTS2MPS;
167 float alt = attrf(a, "alt") * FT2M;
168 float gla = attrf(a, "glide-angle", 0) * DEG2RAD;
169 _airplane.setCruise(spd, alt, attrf(a, "fuel", 0.5),gla);
171 } else if(eq(name, "solve-weight")) {
172 int idx = attri(a, "idx");
173 float wgt = attrf(a, "weight") * LBS2KG;
174 _airplane.addSolutionWeight(!_cruiseCurr, idx, wgt);
175 } else if(eq(name, "cockpit")) {
176 v[0] = attrf(a, "x");
177 v[1] = attrf(a, "y");
178 v[2] = attrf(a, "z");
179 _airplane.setPilotPos(v);
180 } else if(eq(name, "rotor")) {
181 _airplane.getModel()->getRotorgear()->addRotor(parseRotor(a, name));
182 } else if(eq(name, "rotorgear")) {
183 Rotorgear* r = _airplane.getModel()->getRotorgear();
185 #define p(x) if (a->hasAttribute(#x)) r->setParameter((char *)#x,attrf(a,#x) );
186 #define p2(x,y) if (a->hasAttribute(y)) r->setParameter((char *)#x,attrf(a,y) );
187 p2(max_power_engine,"max-power-engine")
188 p2(engine_prop_factor,"engine-prop-factor")
191 p2(max_power_rotor_brake,"max-power-rotor-brake")
192 p2(rotorgear_friction,"rotorgear-friction")
193 p2(engine_accel_limit,"engine-accel-limit")
197 } else if(eq(name, "wing")) {
198 _airplane.setWing(parseWing(a, name));
199 } else if(eq(name, "hstab")) {
200 _airplane.setTail(parseWing(a, name));
201 } else if(eq(name, "vstab") || eq(name, "mstab")) {
202 _airplane.addVStab(parseWing(a, name));
203 } else if(eq(name, "piston-engine")) {
204 parsePistonEngine(a);
205 } else if(eq(name, "turbine-engine")) {
206 parseTurbineEngine(a);
207 } else if(eq(name, "propeller")) {
209 } else if(eq(name, "thruster")) {
210 SimpleJet* j = new SimpleJet();
212 v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
214 _airplane.addThruster(j, 0, v);
215 v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
217 j->setThrust(attrf(a, "thrust") * LBS2N);
218 } else if(eq(name, "jet")) {
221 v[0] = attrf(a, "x");
222 v[1] = attrf(a, "y");
223 v[2] = attrf(a, "z");
224 float mass = attrf(a, "mass") * LBS2KG;
225 j->setMaxThrust(attrf(a, "thrust") * LBS2N,
226 attrf(a, "afterburner", 0) * LBS2N);
227 j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
228 j->setReverseThrust(attrf(a, "reverse", 0.2));
230 float n1min = attrf(a, "n1-idle", 55);
231 float n1max = attrf(a, "n1-max", 102);
232 float n2min = attrf(a, "n2-idle", 73);
233 float n2max = attrf(a, "n2-max", 103);
234 j->setRPMs(n1min, n1max, n2min, n2max);
236 j->setTSFC(attrf(a, "tsfc", 0.8));
237 if(a->hasAttribute("egt")) j->setEGT(attrf(a, "egt"));
238 if(a->hasAttribute("epr")) j->setEPR(attrf(a, "epr"));
239 if(a->hasAttribute("exhaust-speed"))
240 j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
241 if(a->hasAttribute("spool-time"))
242 j->setSpooling(attrf(a, "spool-time"));
245 _airplane.addThruster(j, mass, v);
246 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
247 EngRec* er = new EngRec();
249 er->prefix = dup(buf);
251 } else if(eq(name, "hitch")) {
252 Hitch* h = new Hitch(a->getValue("name"));
254 v[0] = attrf(a, "x");
255 v[1] = attrf(a, "y");
256 v[2] = attrf(a, "z");
258 if(a->hasAttribute("force-is-calculated-by-other")) h->setForceIsCalculatedByOther(attrb(a,"force-is-calculated-by-other"));
259 _airplane.addHitch(h);
260 } else if(eq(name, "tow")) {
261 Hitch* h = (Hitch*)_currObj;
262 if(a->hasAttribute("length"))
263 h->setTowLength(attrf(a, "length"));
264 if(a->hasAttribute("elastic-constant"))
265 h->setTowElasticConstant(attrf(a, "elastic-constant"));
266 if(a->hasAttribute("break-force"))
267 h->setTowBreakForce(attrf(a, "break-force"));
268 if(a->hasAttribute("weight-per-meter"))
269 h->setTowWeightPerM(attrf(a, "weight-per-meter"));
270 if(a->hasAttribute("mp-auto-connect-period"))
271 h->setMpAutoConnectPeriod(attrf(a, "mp-auto-connect-period"));
272 } else if(eq(name, "winch")) {
273 Hitch* h = (Hitch*)_currObj;
275 pos[0] = attrd(a, "x",0);
276 pos[1] = attrd(a, "y",0);
277 pos[2] = attrd(a, "z",0);
278 h->setWinchPosition(pos);
279 if(a->hasAttribute("max-speed"))
280 h->setWinchMaxSpeed(attrf(a, "max-speed"));
281 if(a->hasAttribute("power"))
282 h->setWinchPower(attrf(a, "power") * 1000);
283 if(a->hasAttribute("max-force"))
284 h->setWinchMaxForce(attrf(a, "max-force"));
285 if(a->hasAttribute("initial-tow-length"))
286 h->setWinchInitialTowLength(attrf(a, "initial-tow-length"));
287 if(a->hasAttribute("max-tow-length"))
288 h->setWinchMaxTowLength(attrf(a, "max-tow-length"));
289 if(a->hasAttribute("min-tow-length"))
290 h->setWinchMinTowLength(attrf(a, "min-tow-length"));
291 } else if(eq(name, "gear")) {
292 Gear* g = new Gear();
294 v[0] = attrf(a, "x");
295 v[1] = attrf(a, "y");
296 v[2] = attrf(a, "z");
298 float nrm = Math::mag3(v);
299 if (_vehicle_radius < nrm)
300 _vehicle_radius = nrm;
301 if(a->hasAttribute("upx")) {
302 v[0] = attrf(a, "upx");
303 v[1] = attrf(a, "upy");
304 v[2] = attrf(a, "upz");
311 for(int i=0; i<3; i++)
312 v[i] *= attrf(a, "compression", 1);
313 g->setCompression(v);
314 g->setBrake(attrf(a, "skid", 0));
315 g->setInitialLoad(attrf(a, "initial-load", 0));
316 g->setStaticFriction(attrf(a, "sfric", 0.8));
317 g->setDynamicFriction(attrf(a, "dfric", 0.7));
318 g->setSpring(attrf(a, "spring", 1));
319 g->setDamping(attrf(a, "damp", 1));
320 if(a->hasAttribute("on-water")) g->setOnWater(attrb(a,"on-water"));
321 if(a->hasAttribute("on-solid")) g->setOnSolid(attrb(a,"on-solid"));
322 if(a->hasAttribute("ignored-by-solver")) g->setIgnoreWhileSolving(attrb(a,"ignored-by-solver"));
323 g->setSpringFactorNotPlaning(attrf(a, "spring-factor-not-planing", 1));
324 g->setSpeedPlaning(attrf(a, "speed-planing", 0) * KTS2MPS);
325 g->setReduceFrictionByExtension(attrf(a, "reduce-friction-by-extension", 0));
326 _airplane.addGear(g);
327 } else if(eq(name, "hook")) {
328 Hook* h = new Hook();
330 v[0] = attrf(a, "x");
331 v[1] = attrf(a, "y");
332 v[2] = attrf(a, "z");
334 float length = attrf(a, "length", 1.0);
335 h->setLength(length);
336 float nrm = length+Math::mag3(v);
337 if (_vehicle_radius < nrm)
338 _vehicle_radius = nrm;
339 h->setDownAngle(attrf(a, "down-angle", 70) * DEG2RAD);
340 h->setUpAngle(attrf(a, "up-angle", 0) * DEG2RAD);
341 _airplane.addHook(h);
342 } else if(eq(name, "launchbar")) {
343 Launchbar* l = new Launchbar();
345 v[0] = attrf(a, "x");
346 v[1] = attrf(a, "y");
347 v[2] = attrf(a, "z");
348 l->setLaunchbarMount(v);
349 v[0] = attrf(a, "holdback-x", v[0]);
350 v[1] = attrf(a, "holdback-y", v[1]);
351 v[2] = attrf(a, "holdback-z", v[2]);
352 l->setHoldbackMount(v);
353 float length = attrf(a, "length", 1.0);
354 l->setLength(length);
355 l->setDownAngle(attrf(a, "down-angle", 45) * DEG2RAD);
356 l->setUpAngle(attrf(a, "up-angle", -45) * DEG2RAD);
357 l->setHoldbackLength(attrf(a, "holdback-length", 2.0));
358 _airplane.addLaunchbar(l);
359 } else if(eq(name, "fuselage")) {
361 v[0] = attrf(a, "ax");
362 v[1] = attrf(a, "ay");
363 v[2] = attrf(a, "az");
364 b[0] = attrf(a, "bx");
365 b[1] = attrf(a, "by");
366 b[2] = attrf(a, "bz");
367 float taper = attrf(a, "taper", 1);
368 float mid = attrf(a, "midpoint", 0.5);
369 float cx = attrf(a, "cx", 1);
370 float cy = attrf(a, "cy", 1);
371 float cz = attrf(a, "cz", 1);
372 float idrag = attrf(a, "idrag", 1);
373 _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid,
375 } else if(eq(name, "tank")) {
376 v[0] = attrf(a, "x");
377 v[1] = attrf(a, "y");
378 v[2] = attrf(a, "z");
379 float density = 6.0; // gasoline, in lbs/gal
380 if(a->hasAttribute("jet")) density = 6.72;
381 density *= LBS2KG*CM2GALS;
382 _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
383 } else if(eq(name, "ballast")) {
384 v[0] = attrf(a, "x");
385 v[1] = attrf(a, "y");
386 v[2] = attrf(a, "z");
387 _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
388 } else if(eq(name, "weight")) {
390 } else if(eq(name, "stall")) {
391 Wing* w = (Wing*)_currObj;
392 w->setStall(attrf(a, "aoa") * DEG2RAD);
393 w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
394 w->setStallPeak(attrf(a, "peak", 1.5));
395 } else if(eq(name, "flap0")) {
396 ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
397 attrf(a, "lift"), attrf(a, "drag"));
398 } else if(eq(name, "flap1")) {
399 ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
400 attrf(a, "lift"), attrf(a, "drag"));
401 } else if(eq(name, "slat")) {
402 ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
403 attrf(a, "aoa"), attrf(a, "drag"));
404 } else if(eq(name, "spoiler")) {
405 ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
406 attrf(a, "lift"), attrf(a, "drag"));
407 /* } else if(eq(name, "collective")) {
408 ((Rotor*)_currObj)->setcollective(attrf(a, "min"), attrf(a, "max"));
409 } else if(eq(name, "cyclic")) {
410 ((Rotor*)_currObj)->setcyclic(attrf(a, "ail"), attrf(a, "ele"));
412 } else if(eq(name, "actionpt")) {
413 v[0] = attrf(a, "x");
414 v[1] = attrf(a, "y");
415 v[2] = attrf(a, "z");
416 ((Thruster*)_currObj)->setPosition(v);
417 } else if(eq(name, "dir")) {
418 v[0] = attrf(a, "x");
419 v[1] = attrf(a, "y");
420 v[2] = attrf(a, "z");
421 ((Thruster*)_currObj)->setDirection(v);
422 } else if(eq(name, "control-setting")) {
423 // A cruise or approach control setting
424 const char* axis = a->getValue("axis");
425 float value = attrf(a, "value", 0);
427 _airplane.addCruiseControl(parseAxis(axis), value);
429 _airplane.addApproachControl(parseAxis(axis), value);
430 } else if(eq(name, "control-input")) {
432 // A mapping of input property to a control
433 int axis = parseAxis(a->getValue("axis"));
434 int control = parseOutput(a->getValue("control"));
436 opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
437 opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
438 opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
440 ControlMap* cm = _airplane.getControlMap();
441 if(a->hasAttribute("src0")) {
442 cm->addMapping(axis, control, _currObj, opt,
443 attrf(a, "src0"), attrf(a, "src1"),
444 attrf(a, "dst0"), attrf(a, "dst1"));
446 cm->addMapping(axis, control, _currObj, opt);
448 } else if(eq(name, "control-output")) {
449 // A property output for a control on the current object
450 ControlMap* cm = _airplane.getControlMap();
451 int type = parseOutput(a->getValue("control"));
452 int handle = cm->getOutputHandle(_currObj, type);
454 PropOut* p = new PropOut();
455 p->prop = fgGetNode(a->getValue("prop"), true);
458 p->left = !(a->hasAttribute("side") &&
459 eq("right", a->getValue("side")));
460 p->min = attrf(a, "min", cm->rangeMin(type));
461 p->max = attrf(a, "max", cm->rangeMax(type));
462 _controlProps.add(p);
464 } else if(eq(name, "control-speed")) {
465 ControlMap* cm = _airplane.getControlMap();
466 int type = parseOutput(a->getValue("control"));
467 int handle = cm->getOutputHandle(_currObj, type);
468 float time = attrf(a, "transition-time", 0);
470 cm->setTransitionTime(handle, time);
472 SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '"
473 << name << "' found in YASim aircraft description");
478 void FGFDM::getExternalInput(float dt)
482 _turb->setMagnitude(fgGetFloat("/environment/turbulence/magnitude-norm"));
483 _turb->update(dt, fgGetFloat("/environment/turbulence/rate-hz"));
486 ControlMap* cm = _airplane.getControlMap();
489 for(i=0; i<_axes.size(); i++) {
490 AxisRec* a = (AxisRec*)_axes.get(i);
491 float val = fgGetFloat(a->name, 0);
492 cm->setInput(a->handle, val);
494 cm->applyControls(dt);
497 for(i=0; i<_weights.size(); i++) {
498 WeightRec* wr = (WeightRec*)_weights.get(i);
499 _airplane.setWeight(wr->handle, LBS2KG * fgGetFloat(wr->prop));
502 for(i=0; i<_thrusters.size(); i++) {
503 EngRec* er = (EngRec*)_thrusters.get(i);
504 Thruster* t = er->eng;
506 if(t->getPropEngine()) {
507 PropEngine* p = t->getPropEngine();
508 sprintf(buf, "%s/rpm", er->prefix);
509 p->setOmega(fgGetFloat(buf, 500) * RPM2RAD);
514 // Linearly "seeks" a property by the specified fraction of the way to
515 // the target value. Used to emulate "slowly changing" output values.
516 static void moveprop(SGPropertyNode* node, const char* prop,
517 float target, float frac)
519 float val = node->getFloatValue(prop);
520 if(frac > 1) frac = 1;
521 if(frac < 0) frac = 0;
522 val += (target - val) * frac;
523 node->setFloatValue(prop, val);
526 void FGFDM::setOutputProperties(float dt)
531 float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
532 fgSetFloat("/yasim/gross-weight-lbs", grossWgt);
534 ControlMap* cm = _airplane.getControlMap();
535 for(i=0; i<_controlProps.size(); i++) {
536 PropOut* p = (PropOut*)_controlProps.get(i);
538 ? cm->getOutput(p->handle)
539 : cm->getOutputR(p->handle));
540 float rmin = cm->rangeMin(p->type);
541 float rmax = cm->rangeMax(p->type);
542 float frac = (val - rmin) / (rmax - rmin);
543 val = frac*(p->max - p->min) + p->min;
544 p->prop->setFloatValue(val);
547 for(i=0; i<_airplane.getRotorgear()->getNumRotors(); i++) {
548 Rotor*r=(Rotor*)_airplane.getRotorgear()->getRotor(i);
552 while((j = r->getValueforFGSet(j, b, &f)))
553 if(b[0]) fgSetFloat(b,f);
555 while((j = _airplane.getRotorgear()->getValueforFGSet(j, b, &f)))
556 if(b[0]) fgSetFloat(b,f);
557 for(j=0; j < r->numRotorparts(); j+=r->numRotorparts()>>2) {
558 Rotorpart* s = (Rotorpart*)r->getRotorpart(j);
562 b=s->getAlphaoutput(k);
563 if(b[0]) fgSetFloat(b, s->getAlpha(k));
568 // Use the density of the first tank, or a dummy value if no tanks
569 float fuelDensity = 1.0;
570 if(_airplane.numTanks())
571 fuelDensity = _airplane.getFuelDensity(0);
572 for(i=0; i<_thrusters.size(); i++) {
573 EngRec* er = (EngRec*)_thrusters.get(i);
574 Thruster* t = er->eng;
575 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
577 // Set: running, cranking, prop-thrust, max-hp, power-pct
578 node->setBoolValue("running", t->isRunning());
579 node->setBoolValue("cranking", t->isCranking());
583 float lbs = Math::mag3(tmp) * (KG2LBS/9.8);
584 node->setFloatValue("prop-thrust", lbs); // Deprecated name
585 node->setFloatValue("thrust-lbs", lbs);
586 node->setFloatValue("fuel-flow-gph",
587 (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
589 if(t->getPropEngine()) {
590 PropEngine* p = t->getPropEngine();
591 node->setFloatValue("rpm", p->getOmega() * (1/RPM2RAD));
592 node->setFloatValue("torque-ftlb",
593 p->getEngine()->getTorque() * NM2FTLB);
595 if(p->getEngine()->isPistonEngine()) {
596 PistonEngine* pe = p->getEngine()->isPistonEngine();
597 node->setFloatValue("mp-osi", pe->getMP() * (1/INHG2PA));
598 node->setFloatValue("mp-inhg", pe->getMP() * (1/INHG2PA));
599 node->setFloatValue("egt-degf",
600 pe->getEGT() * K2DEGF + K2DEGFOFFSET);
601 node->setFloatValue("oil-temperature-degf",
602 pe->getOilTemp() * K2DEGF + K2DEGFOFFSET);
603 node->setFloatValue("boost-gauge-inhg",
604 pe->getBoost() * (1/INHG2PA));
605 } else if(p->getEngine()->isTurbineEngine()) {
606 TurbineEngine* te = p->getEngine()->isTurbineEngine();
607 node->setFloatValue("n2", te->getN2());
612 Jet* j = t->getJet();
613 node->setFloatValue("n1", j->getN1());
614 node->setFloatValue("n2", j->getN2());
615 node->setFloatValue("epr", j->getEPR());
616 node->setFloatValue("egt-degf",
617 j->getEGT() * K2DEGF + K2DEGFOFFSET);
619 // These are "unmodeled" values that are still needed for
620 // many cockpits. Tie them all to the N1 speed, but
621 // normalize the numbers to the range [0:1] so the
622 // cockpit code can scale them to the right values.
623 float pnorm = j->getPerfNorm();
624 moveprop(node, "oilp-norm", pnorm, dt/3); // 3s seek time
625 moveprop(node, "oilt-norm", pnorm, dt/30); // 30s
626 moveprop(node, "itt-norm", pnorm, dt/1); // 1s
631 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
633 Wing* w = new Wing();
636 if(eq(type, "vstab"))
642 pos[0] = attrf(a, "x");
643 pos[1] = attrf(a, "y");
644 pos[2] = attrf(a, "z");
647 w->setLength(attrf(a, "length"));
648 w->setChord(attrf(a, "chord"));
649 w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
650 w->setTaper(attrf(a, "taper", 1));
651 w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
652 w->setCamber(attrf(a, "camber", 0));
654 // These come in with positive indicating positive AoA, but the
655 // internals expect a rotation about the left-pointing Y axis, so
657 w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD * -1);
658 w->setTwist(attrf(a, "twist", 0) * DEG2RAD * -1);
660 // The 70% is a magic number that sorta kinda seems to match known
661 // throttle settings to approach speed.
662 w->setInducedDrag(0.7*attrf(a, "idrag", 1));
664 float effect = attrf(a, "effectiveness", 1);
665 w->setDragScale(w->getDragScale()*effect);
671 Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
673 Rotor* w = new Rotor();
675 // float defDihed = 0;
678 pos[0] = attrf(a, "x");
679 pos[1] = attrf(a, "y");
680 pos[2] = attrf(a, "z");
684 normal[0] = attrf(a, "nx");
685 normal[1] = attrf(a, "ny");
686 normal[2] = attrf(a, "nz");
687 w->setNormal(normal);
690 forward[0] = attrf(a, "fx");
691 forward[1] = attrf(a, "fy");
692 forward[2] = attrf(a, "fz");
693 w->setForward(forward);
695 w->setMaxCyclicail(attrf(a, "maxcyclicail", 7.6));
696 w->setMaxCyclicele(attrf(a, "maxcyclicele", 4.94));
697 w->setMinCyclicail(attrf(a, "mincyclicail", -7.6));
698 w->setMinCyclicele(attrf(a, "mincyclicele", -4.94));
699 w->setMaxCollective(attrf(a, "maxcollective", 15.8));
700 w->setMinCollective(attrf(a, "mincollective", -0.2));
701 w->setDiameter(attrf(a, "diameter", 10.2));
702 w->setWeightPerBlade(attrf(a, "weightperblade", 44));
703 w->setNumberOfBlades(attrf(a, "numblades", 4));
704 w->setRelBladeCenter(attrf(a, "relbladecenter", 0.7));
705 w->setDynamic(attrf(a, "dynamic", 0.7));
706 w->setDelta3(attrf(a, "delta3", 0));
707 w->setDelta(attrf(a, "delta", 0));
708 w->setTranslift(attrf(a, "translift", 0.05));
709 w->setC2(attrf(a, "dragfactor", 1));
710 w->setStepspersecond(attrf(a, "stepspersecond", 120));
711 w->setPhiNull((attrf(a, "phi0", 0))*YASIM_PI/180);
712 w->setRPM(attrf(a, "rpm", 424));
713 w->setRelLenHinge(attrf(a, "rellenflaphinge", 0.07));
714 w->setAlpha0((attrf(a, "flap0", -5))*YASIM_PI/180);
715 w->setAlphamin((attrf(a, "flapmin", -15))/180*YASIM_PI);
716 w->setAlphamax((attrf(a, "flapmax", 15))*YASIM_PI/180);
717 w->setAlpha0factor(attrf(a, "flap0factor", 1));
718 w->setTeeterdamp(attrf(a,"teeterdamp",.0001));
719 w->setMaxteeterdamp(attrf(a,"maxteeterdamp",1000));
720 w->setRelLenTeeterHinge(attrf(a,"rellenteeterhinge",0.01));
721 w->setBalance(attrf(a,"balance",1.0));
722 w->setMinTiltYaw(attrf(a,"mintiltyaw",0.0));
723 w->setMinTiltPitch(attrf(a,"mintiltpitch",0.0));
724 w->setMinTiltRoll(attrf(a,"mintiltroll",0.0));
725 w->setMaxTiltYaw(attrf(a,"maxtiltyaw",0.0));
726 w->setMaxTiltPitch(attrf(a,"maxtiltpitch",0.0));
727 w->setMaxTiltRoll(attrf(a,"maxtiltroll",0.0));
728 w->setTiltCenterX(attrf(a,"tiltcenterx",0.0));
729 w->setTiltCenterY(attrf(a,"tiltcentery",0.0));
730 w->setTiltCenterZ(attrf(a,"tiltcenterz",0.0));
731 w->setDownwashFactor(attrf(a, "downwashfactor", 1));
734 if(attrb(a,"sharedflaphinge"))
735 w->setSharedFlapHinge(true);
737 if(a->hasAttribute("name"))
738 w->setName(a->getValue("name") );
739 if(a->hasAttribute("alphaout0"))
740 w->setAlphaoutput(0,a->getValue("alphaout0") );
741 if(a->hasAttribute("alphaout1")) w->setAlphaoutput(1,a->getValue("alphaout1") );
742 if(a->hasAttribute("alphaout2")) w->setAlphaoutput(2,a->getValue("alphaout2") );
743 if(a->hasAttribute("alphaout3")) w->setAlphaoutput(3,a->getValue("alphaout3") );
744 if(a->hasAttribute("coneout")) w->setAlphaoutput(4,a->getValue("coneout") );
745 if(a->hasAttribute("yawout")) w->setAlphaoutput(5,a->getValue("yawout") );
746 if(a->hasAttribute("rollout")) w->setAlphaoutput(6,a->getValue("rollout") );
748 w->setPitchA(attrf(a, "pitch-a", 10));
749 w->setPitchB(attrf(a, "pitch-b", 10));
750 w->setForceAtPitchA(attrf(a, "forceatpitch-a", 3000));
751 w->setPowerAtPitch0(attrf(a, "poweratpitch-0", 300));
752 w->setPowerAtPitchB(attrf(a, "poweratpitch-b", 3000));
753 if(attrb(a,"notorque"))
756 #define p(x) if (a->hasAttribute(#x)) w->setParameter((char *)#x,attrf(a,#x) );
757 #define p2(x,y) if (a->hasAttribute(y)) w->setParameter((char *)#x,attrf(a,y) );
758 p2(translift_ve,"translift-ve")
759 p2(translift_maxfactor,"translift-maxfactor")
760 p2(ground_effect_constant,"ground-effect-constant")
761 p2(vortex_state_lift_factor,"vortex-state-lift-factor")
762 p2(vortex_state_c1,"vortex-state-c1")
763 p2(vortex_state_c2,"vortex-state-c2")
764 p2(vortex_state_c3,"vortex-state_c3")
765 p2(vortex_state_e1,"vortex-state-e1")
766 p2(vortex_state_e2,"vortex-state-e2")
768 p2(number_of_segments,"number-of-segments")
769 p2(number_of_parts,"number-of-parts")
770 p2(rel_len_where_incidence_is_measured,"rel-len-where-incidence-is-measured")
773 p2(airfoil_incidence_no_lift,"airfoil-incidence-no-lift")
774 p2(rel_len_blade_start,"rel-len-blade-start")
775 p2(incidence_stall_zero_speed,"incidence-stall-zero-speed")
776 p2(incidence_stall_half_sonic_speed,"incidence-stall-half-sonic-speed")
777 p2(lift_factor_stall,"lift-factor-stall")
778 p2(stall_change_over,"stall-change-over")
779 p2(drag_factor_stall,"drag-factor-stall")
780 p2(airfoil_lift_coefficient,"airfoil-lift-coefficient")
781 p2(airfoil_drag_coefficient0,"airfoil-drag-coefficient0")
782 p2(airfoil_drag_coefficient1,"airfoil-drag-coefficient1")
783 p2(cyclic_factor,"cyclic-factor")
784 p2(rotor_correction_factor,"rotor-correction-factor")
791 void FGFDM::parsePistonEngine(XMLAttributes* a)
793 float engP = attrf(a, "eng-power") * HP2W;
794 float engS = attrf(a, "eng-rpm") * RPM2RAD;
796 PistonEngine* eng = new PistonEngine(engP, engS);
798 if(a->hasAttribute("displacement"))
799 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
801 if(a->hasAttribute("compression"))
802 eng->setCompression(attrf(a, "compression"));
804 if(a->hasAttribute("min-throttle"))
805 eng->setMinThrottle(attrf(a, "min-throttle"));
807 if(a->hasAttribute("turbo-mul")) {
808 float mul = attrf(a, "turbo-mul");
809 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
810 eng->setTurboParams(mul, mp);
811 eng->setTurboLag(attrf(a, "turbo-lag", 2));
814 if(a->hasAttribute("supercharger"))
815 eng->setSupercharger(attrb(a, "supercharger"));
817 ((PropEngine*)_currObj)->setEngine(eng);
820 void FGFDM::parseTurbineEngine(XMLAttributes* a)
822 float power = attrf(a, "eng-power") * HP2W;
823 float omega = attrf(a, "eng-rpm") * RPM2RAD;
824 float alt = attrf(a, "alt") * FT2M;
825 float flatRating = attrf(a, "flat-rating") * HP2W;
826 TurbineEngine* eng = new TurbineEngine(power, omega, alt, flatRating);
828 if(a->hasAttribute("n2-low-idle"))
829 eng->setN2Range(attrf(a, "n2-low-idle"), attrf(a, "n2-high-idle"),
832 // Nasty units conversion: lbs/hr per hp -> kg/s per watt
833 if(a->hasAttribute("bsfc"))
834 eng->setFuelConsumption(attrf(a, "bsfc") * (LBS2KG/(3600*HP2W)));
836 ((PropEngine*)_currObj)->setEngine(eng);
839 void FGFDM::parsePropeller(XMLAttributes* a)
841 // Legacy Handling for the old engines syntax:
842 PistonEngine* eng = 0;
843 if(a->hasAttribute("eng-power")) {
844 SG_LOG(SG_FLIGHT,SG_ALERT, "WARNING: "
845 << "Legacy engine definition in YASim configuration file. "
847 float engP = attrf(a, "eng-power") * HP2W;
848 float engS = attrf(a, "eng-rpm") * RPM2RAD;
849 eng = new PistonEngine(engP, engS);
850 if(a->hasAttribute("displacement"))
851 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
852 if(a->hasAttribute("compression"))
853 eng->setCompression(attrf(a, "compression"));
854 if(a->hasAttribute("turbo-mul")) {
855 float mul = attrf(a, "turbo-mul");
856 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
857 eng->setTurboParams(mul, mp);
861 // Now parse the actual propeller definition:
863 cg[0] = attrf(a, "x");
864 cg[1] = attrf(a, "y");
865 cg[2] = attrf(a, "z");
866 float mass = attrf(a, "mass") * LBS2KG;
867 float moment = attrf(a, "moment");
868 float radius = attrf(a, "radius");
869 float speed = attrf(a, "cruise-speed") * KTS2MPS;
870 float omega = attrf(a, "cruise-rpm") * RPM2RAD;
871 float power = attrf(a, "cruise-power") * HP2W;
872 float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
874 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
875 PropEngine* thruster = new PropEngine(prop, eng, moment);
876 _airplane.addThruster(thruster, mass, cg);
878 // Set the stops (fine = minimum pitch, coarse = maximum pitch)
879 float fine_stop = attrf(a, "fine-stop", 0.25f);
880 float coarse_stop = attrf(a, "coarse-stop", 4.0f);
881 prop->setStops(fine_stop, coarse_stop);
883 if(a->hasAttribute("takeoff-power")) {
884 float power0 = attrf(a, "takeoff-power") * HP2W;
885 float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
886 prop->setTakeoff(omega0, power0);
889 if(a->hasAttribute("max-rpm")) {
890 float max = attrf(a, "max-rpm") * RPM2RAD;
891 float min = attrf(a, "min-rpm") * RPM2RAD;
892 thruster->setVariableProp(min, max);
895 if(attrb(a, "contra"))
896 thruster->setContraPair(true);
898 if(a->hasAttribute("manual-pitch")) {
899 prop->setManualPitch();
902 thruster->setGearRatio(attrf(a, "gear-ratio", 1));
905 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
906 EngRec* er = new EngRec();
908 er->prefix = dup(buf);
914 // Turns a string axis name into an integer for use by the
915 // ControlMap. Creates a new axis if this one hasn't been defined
917 int FGFDM::parseAxis(const char* name)
920 for(i=0; i<_axes.size(); i++) {
921 AxisRec* a = (AxisRec*)_axes.get(i);
922 if(eq(a->name, name))
926 // Not there, make a new one.
927 AxisRec* a = new AxisRec();
929 fgGetNode( a->name, true ); // make sure the property name exists
930 a->handle = _airplane.getControlMap()->newInput();
935 int FGFDM::parseOutput(const char* name)
937 if(eq(name, "THROTTLE")) return ControlMap::THROTTLE;
938 if(eq(name, "MIXTURE")) return ControlMap::MIXTURE;
939 if(eq(name, "CONDLEVER")) return ControlMap::CONDLEVER;
940 if(eq(name, "STARTER")) return ControlMap::STARTER;
941 if(eq(name, "MAGNETOS")) return ControlMap::MAGNETOS;
942 if(eq(name, "ADVANCE")) return ControlMap::ADVANCE;
943 if(eq(name, "REHEAT")) return ControlMap::REHEAT;
944 if(eq(name, "BOOST")) return ControlMap::BOOST;
945 if(eq(name, "VECTOR")) return ControlMap::VECTOR;
946 if(eq(name, "PROP")) return ControlMap::PROP;
947 if(eq(name, "BRAKE")) return ControlMap::BRAKE;
948 if(eq(name, "STEER")) return ControlMap::STEER;
949 if(eq(name, "EXTEND")) return ControlMap::EXTEND;
950 if(eq(name, "HEXTEND")) return ControlMap::HEXTEND;
951 if(eq(name, "LEXTEND")) return ControlMap::LEXTEND;
952 if(eq(name, "LACCEL")) return ControlMap::LACCEL;
953 if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
954 if(eq(name, "FLAP0")) return ControlMap::FLAP0;
955 if(eq(name, "FLAP0EFFECTIVENESS")) return ControlMap::FLAP0EFFECTIVENESS;
956 if(eq(name, "FLAP1")) return ControlMap::FLAP1;
957 if(eq(name, "FLAP1EFFECTIVENESS")) return ControlMap::FLAP1EFFECTIVENESS;
958 if(eq(name, "SLAT")) return ControlMap::SLAT;
959 if(eq(name, "SPOILER")) return ControlMap::SPOILER;
960 if(eq(name, "CASTERING")) return ControlMap::CASTERING;
961 if(eq(name, "PROPPITCH")) return ControlMap::PROPPITCH;
962 if(eq(name, "PROPFEATHER")) return ControlMap::PROPFEATHER;
963 if(eq(name, "COLLECTIVE")) return ControlMap::COLLECTIVE;
964 if(eq(name, "CYCLICAIL")) return ControlMap::CYCLICAIL;
965 if(eq(name, "CYCLICELE")) return ControlMap::CYCLICELE;
966 if(eq(name, "TILTROLL")) return ControlMap::TILTROLL;
967 if(eq(name, "TILTPITCH")) return ControlMap::TILTPITCH;
968 if(eq(name, "TILTYAW")) return ControlMap::TILTYAW;
969 if(eq(name, "ROTORGEARENGINEON")) return ControlMap::ROTORENGINEON;
970 if(eq(name, "ROTORBRAKE")) return ControlMap::ROTORBRAKE;
971 if(eq(name, "ROTORENGINEMAXRELTORQUE"))
972 return ControlMap::ROTORENGINEMAXRELTORQUE;
973 if(eq(name, "ROTORRELTARGET")) return ControlMap::ROTORRELTARGET;
974 if(eq(name, "ROTORBALANCE")) return ControlMap::ROTORBALANCE;
975 if(eq(name, "REVERSE_THRUST")) return ControlMap::REVERSE_THRUST;
976 if(eq(name, "WASTEGATE")) return ControlMap::WASTEGATE;
977 if(eq(name, "WINCHRELSPEED")) return ControlMap::WINCHRELSPEED;
978 if(eq(name, "HITCHOPEN")) return ControlMap::HITCHOPEN;
979 if(eq(name, "PLACEWINCH")) return ControlMap::PLACEWINCH;
980 if(eq(name, "FINDAITOW")) return ControlMap::FINDAITOW;
982 SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
983 << name << "' in YASim aircraft description.");
988 void FGFDM::parseWeight(XMLAttributes* a)
990 WeightRec* wr = new WeightRec();
993 v[0] = attrf(a, "x");
994 v[1] = attrf(a, "y");
995 v[2] = attrf(a, "z");
997 wr->prop = dup(a->getValue("mass-prop"));
998 wr->size = attrf(a, "size", 0);
999 wr->handle = _airplane.addWeight(v, wr->size);
1004 bool FGFDM::eq(const char* a, const char* b)
1006 // Figure it out for yourself. :)
1007 while(*a && *b && *a == *b) { a++; b++; }
1011 char* FGFDM::dup(const char* s)
1015 char* s2 = new char[len+1];
1017 while((*p++ = *s++));
1022 int FGFDM::attri(XMLAttributes* atts, const char* attr)
1024 if(!atts->hasAttribute(attr)) {
1025 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1026 "' in YASim aircraft description");
1029 return attri(atts, attr, 0);
1032 int FGFDM::attri(XMLAttributes* atts, const char* attr, int def)
1034 const char* val = atts->getValue(attr);
1035 if(val == 0) return def;
1036 else return atol(val);
1039 float FGFDM::attrf(XMLAttributes* atts, const char* attr)
1041 if(!atts->hasAttribute(attr)) {
1042 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1043 "' in YASim aircraft description");
1046 return attrf(atts, attr, 0);
1049 float FGFDM::attrf(XMLAttributes* atts, const char* attr, float def)
1051 const char* val = atts->getValue(attr);
1052 if(val == 0) return def;
1053 else return (float)atof(val);
1056 double FGFDM::attrd(XMLAttributes* atts, const char* attr)
1058 if(!atts->hasAttribute(attr)) {
1059 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1060 "' in YASim aircraft description");
1063 return attrd(atts, attr, 0);
1066 double FGFDM::attrd(XMLAttributes* atts, const char* attr, double def)
1068 const char* val = atts->getValue(attr);
1069 if(val == 0) return def;
1070 else return atof(val);
1073 // ACK: the dreaded ambiguous string boolean. Remind me to shoot Maik
1074 // when I have a chance. :). Unless you have a parser that can check
1075 // symbol constants (we don't), this kind of coding is just a Bad
1076 // Idea. This implementation, for example, silently returns a boolean
1077 // falsehood for values of "1", "yes", "True", and "TRUE". Which is
1078 // especially annoying preexisting boolean attributes in the same
1079 // parser want to see "1" and will choke on a "true"...
1081 // Unfortunately, this usage creeped into existing configuration files
1082 // while I wasn't active, and it's going to be hard to remove. Issue
1083 // a warning to nag people into changing their ways for now...
1084 bool FGFDM::attrb(XMLAttributes* atts, const char* attr)
1086 const char* val = atts->getValue(attr);
1087 if(val == 0) return false;
1089 if(eq(val,"true")) {
1090 SG_LOG(SG_FLIGHT, SG_ALERT, "Warning: " <<
1091 "deprecated 'true' boolean in YASim configuration file. " <<
1092 "Use numeric booleans (attribute=\"1\") instead");
1095 return attri(atts, attr, 0) ? true : false;
1098 }; // namespace yasim