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