8 #include <Main/fg_props.hxx>
12 #include "SimpleJet.hpp"
15 #include "Launchbar.hpp"
16 #include "Atmosphere.hpp"
17 #include "PropEngine.hpp"
18 #include "Propeller.hpp"
19 #include "PistonEngine.hpp"
20 #include "TurbineEngine.hpp"
22 #include "Rotorpart.hpp"
29 // Some conversion factors
30 static const float KTS2MPS = 0.514444444444;
31 static const float FT2M = 0.3048;
32 static const float DEG2RAD = 0.0174532925199;
33 static const float RPM2RAD = 0.10471975512;
34 static const float LBS2N = 4.44822;
35 static const float LBS2KG = 0.45359237;
36 static const float KG2LBS = 2.2046225;
37 static const float CM2GALS = 264.172037284;
38 static const float HP2W = 745.700;
39 static const float INHG2PA = 3386.389;
40 static const float K2DEGF = 1.8;
41 static const float K2DEGFOFFSET = -459.4;
42 static const float CIN2CM = 1.6387064e-5;
43 static const float YASIM_PI = 3.14159265358979323846;
45 static const float NM2FTLB = (1/(LBS2N*FT2M));
47 // Stubs, so that this can be compiled without the FlightGear
48 // binary. What's the best way to handle this?
50 // float fgGetFloat(char* name, float def) { return 0; }
51 // void fgSetFloat(char* name, float val) {}
55 _vehicle_radius = 0.0f;
59 // Map /controls/flight/elevator to the approach elevator control. This
60 // should probably be settable, but there are very few aircraft
61 // who trim their approaches using things other than elevator.
62 _airplane.setElevatorControl(parseAxis("/controls/flight/elevator-trim"));
64 // FIXME: read seed from somewhere?
66 _turb = new Turbulence(10, seed);
72 for(i=0; i<_axes.size(); i++) {
73 AxisRec* a = (AxisRec*)_axes.get(i);
77 for(i=0; i<_thrusters.size(); i++) {
78 EngRec* er = (EngRec*)_thrusters.get(i);
83 for(i=0; i<_weights.size(); i++) {
84 WeightRec* wr = (WeightRec*)_weights.get(i);
88 for(i=0; i<_controlProps.size(); i++)
89 delete (PropOut*)_controlProps.get(i);
93 void FGFDM::iterate(float dt)
96 _airplane.iterate(dt);
98 // Do fuel stuff (FIXME: should stash SGPropertyNode objects here)
100 for(int i=0; i<_airplane.numThrusters(); i++) {
101 Thruster* t = _airplane.getThruster(i);
103 sprintf(buf, "/engines/engine[%d]/out-of-fuel", i);
104 t->setFuelState(!fgGetBool(buf));
106 sprintf(buf, "/engines/engine[%d]/fuel-consumed-lbs", i);
107 double consumed = fgGetDouble(buf) + dt * KG2LBS * t->getFuelFlow();
108 fgSetDouble(buf, consumed);
110 for(int i=0; i<_airplane.numTanks(); i++) {
111 sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
112 _airplane.setFuel(i, LBS2KG * fgGetFloat(buf));
114 _airplane.calcFuelWeights();
116 setOutputProperties(dt);
119 Airplane* FGFDM::getAirplane()
126 // Allows the user to start with something other than full fuel
127 _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
129 // Read out the resulting fuel state
131 for(int i=0; i<_airplane.numTanks(); i++) {
132 sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
133 fgSetDouble(buf, _airplane.getFuel(i) * KG2LBS);
135 double density = _airplane.getFuelDensity(i);
136 sprintf(buf, "/consumables/fuel/tank[%d]/density-ppg", i);
137 fgSetDouble(buf, density * (KG2LBS/CM2GALS));
139 // set in TankProperties class
140 // sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
141 // fgSetDouble(buf, _airplane.getFuel(i) * CM2GALS / density);
143 sprintf(buf, "/consumables/fuel/tank[%d]/capacity-gal_us", i);
144 fgSetDouble(buf, CM2GALS * _airplane.getTankCapacity(i)/density);
147 // This has a nasty habit of being false at startup. That's not
149 fgSetBool("/controls/gear/gear-down", true);
151 _airplane.getModel()->setTurbulence(_turb);
154 // Not the worlds safest parser. But it's short & sweet.
155 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
157 XMLAttributes* a = (XMLAttributes*)&atts;
161 if(eq(name, "airplane")) {
162 _airplane.setWeight(attrf(a, "mass") * LBS2KG);
163 } else if(eq(name, "approach")) {
164 float spd = attrf(a, "speed") * KTS2MPS;
165 float alt = attrf(a, "alt", 0) * FT2M;
166 float aoa = attrf(a, "aoa", 0) * DEG2RAD;
167 float gla = attrf(a, "glide-angle", 0) * DEG2RAD;
168 _airplane.setApproach(spd, alt, aoa, attrf(a, "fuel", 0.2),gla);
170 } else if(eq(name, "cruise")) {
171 float spd = attrf(a, "speed") * KTS2MPS;
172 float alt = attrf(a, "alt") * FT2M;
173 float gla = attrf(a, "glide-angle", 0) * DEG2RAD;
174 _airplane.setCruise(spd, alt, attrf(a, "fuel", 0.5),gla);
176 } else if(eq(name, "solve-weight")) {
177 int idx = attri(a, "idx");
178 float wgt = attrf(a, "weight") * LBS2KG;
179 _airplane.addSolutionWeight(!_cruiseCurr, idx, wgt);
180 } else if(eq(name, "cockpit")) {
181 v[0] = attrf(a, "x");
182 v[1] = attrf(a, "y");
183 v[2] = attrf(a, "z");
184 _airplane.setPilotPos(v);
185 } else if(eq(name, "rotor")) {
186 _airplane.getModel()->getRotorgear()->addRotor(parseRotor(a, name));
187 } else if(eq(name, "rotorgear")) {
188 Rotorgear* r = _airplane.getModel()->getRotorgear();
190 #define p(x) if (a->hasAttribute(#x)) r->setParameter((char *)#x,attrf(a,#x) );
191 #define p2(x,y) if (a->hasAttribute(y)) r->setParameter((char *)#x,attrf(a,y) );
192 p2(max_power_engine,"max-power-engine")
193 p2(engine_prop_factor,"engine-prop-factor")
196 p2(max_power_rotor_brake,"max-power-rotor-brake")
197 p2(rotorgear_friction,"rotorgear-friction")
198 p2(engine_accel_limit,"engine-accel-limit")
202 } else if(eq(name, "wing")) {
203 _airplane.setWing(parseWing(a, name));
204 } else if(eq(name, "hstab")) {
205 _airplane.setTail(parseWing(a, name));
206 } else if(eq(name, "vstab") || eq(name, "mstab")) {
207 _airplane.addVStab(parseWing(a, name));
208 } else if(eq(name, "piston-engine")) {
209 parsePistonEngine(a);
210 } else if(eq(name, "turbine-engine")) {
211 parseTurbineEngine(a);
212 } else if(eq(name, "propeller")) {
214 } else if(eq(name, "thruster")) {
215 SimpleJet* j = new SimpleJet();
217 v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
219 _airplane.addThruster(j, 0, v);
220 v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
222 j->setThrust(attrf(a, "thrust") * LBS2N);
223 } else if(eq(name, "jet")) {
226 v[0] = attrf(a, "x");
227 v[1] = attrf(a, "y");
228 v[2] = attrf(a, "z");
229 float mass = attrf(a, "mass") * LBS2KG;
230 j->setMaxThrust(attrf(a, "thrust") * LBS2N,
231 attrf(a, "afterburner", 0) * LBS2N);
232 j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
233 j->setReverseThrust(attrf(a, "reverse", 0.2));
235 float n1min = attrf(a, "n1-idle", 55);
236 float n1max = attrf(a, "n1-max", 102);
237 float n2min = attrf(a, "n2-idle", 73);
238 float n2max = attrf(a, "n2-max", 103);
239 j->setRPMs(n1min, n1max, n2min, n2max);
241 j->setTSFC(attrf(a, "tsfc", 0.8));
242 if(a->hasAttribute("egt")) j->setEGT(attrf(a, "egt"));
243 if(a->hasAttribute("epr")) j->setEPR(attrf(a, "epr"));
244 if(a->hasAttribute("exhaust-speed"))
245 j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
246 if(a->hasAttribute("spool-time"))
247 j->setSpooling(attrf(a, "spool-time"));
250 _airplane.addThruster(j, mass, v);
251 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
252 EngRec* er = new EngRec();
254 er->prefix = dup(buf);
256 } else if(eq(name, "hitch")) {
257 Hitch* h = new Hitch(a->getValue("name"));
259 v[0] = attrf(a, "x");
260 v[1] = attrf(a, "y");
261 v[2] = attrf(a, "z");
263 if(a->hasAttribute("force-is-calculated-by-other")) h->setForceIsCalculatedByOther(attrb(a,"force-is-calculated-by-other"));
264 _airplane.addHitch(h);
265 } else if(eq(name, "tow")) {
266 Hitch* h = (Hitch*)_currObj;
267 if(a->hasAttribute("length"))
268 h->setTowLength(attrf(a, "length"));
269 if(a->hasAttribute("elastic-constant"))
270 h->setTowElasticConstant(attrf(a, "elastic-constant"));
271 if(a->hasAttribute("break-force"))
272 h->setTowBreakForce(attrf(a, "break-force"));
273 if(a->hasAttribute("weight-per-meter"))
274 h->setTowWeightPerM(attrf(a, "weight-per-meter"));
275 if(a->hasAttribute("mp-auto-connect-period"))
276 h->setMpAutoConnectPeriod(attrf(a, "mp-auto-connect-period"));
277 } else if(eq(name, "winch")) {
278 Hitch* h = (Hitch*)_currObj;
280 pos[0] = attrd(a, "x",0);
281 pos[1] = attrd(a, "y",0);
282 pos[2] = attrd(a, "z",0);
283 h->setWinchPosition(pos);
284 if(a->hasAttribute("max-speed"))
285 h->setWinchMaxSpeed(attrf(a, "max-speed"));
286 if(a->hasAttribute("power"))
287 h->setWinchPower(attrf(a, "power") * 1000);
288 if(a->hasAttribute("max-force"))
289 h->setWinchMaxForce(attrf(a, "max-force"));
290 if(a->hasAttribute("initial-tow-length"))
291 h->setWinchInitialTowLength(attrf(a, "initial-tow-length"));
292 if(a->hasAttribute("max-tow-length"))
293 h->setWinchMaxTowLength(attrf(a, "max-tow-length"));
294 if(a->hasAttribute("min-tow-length"))
295 h->setWinchMinTowLength(attrf(a, "min-tow-length"));
296 } else if(eq(name, "gear")) {
297 Gear* g = new Gear();
299 v[0] = attrf(a, "x");
300 v[1] = attrf(a, "y");
301 v[2] = attrf(a, "z");
303 float nrm = Math::mag3(v);
304 if (_vehicle_radius < nrm)
305 _vehicle_radius = nrm;
306 if(a->hasAttribute("upx")) {
307 v[0] = attrf(a, "upx");
308 v[1] = attrf(a, "upy");
309 v[2] = attrf(a, "upz");
316 for(int i=0; i<3; i++)
317 v[i] *= attrf(a, "compression", 1);
318 g->setCompression(v);
319 g->setBrake(attrf(a, "skid", 0));
320 g->setInitialLoad(attrf(a, "initial-load", 0));
321 g->setStaticFriction(attrf(a, "sfric", 0.8));
322 g->setDynamicFriction(attrf(a, "dfric", 0.7));
323 g->setSpring(attrf(a, "spring", 1));
324 g->setDamping(attrf(a, "damp", 1));
325 if(a->hasAttribute("on-water")) g->setOnWater(attrb(a,"on-water"));
326 if(a->hasAttribute("on-solid")) g->setOnSolid(attrb(a,"on-solid"));
327 if(a->hasAttribute("ignored-by-solver")) g->setIgnoreWhileSolving(attrb(a,"ignored-by-solver"));
328 g->setSpringFactorNotPlaning(attrf(a, "spring-factor-not-planing", 1));
329 g->setSpeedPlaning(attrf(a, "speed-planing", 0) * KTS2MPS);
330 g->setReduceFrictionByExtension(attrf(a, "reduce-friction-by-extension", 0));
331 _airplane.addGear(g);
332 } else if(eq(name, "hook")) {
333 Hook* h = new Hook();
335 v[0] = attrf(a, "x");
336 v[1] = attrf(a, "y");
337 v[2] = attrf(a, "z");
339 float length = attrf(a, "length", 1.0);
340 h->setLength(length);
341 float nrm = length+Math::mag3(v);
342 if (_vehicle_radius < nrm)
343 _vehicle_radius = nrm;
344 h->setDownAngle(attrf(a, "down-angle", 70) * DEG2RAD);
345 h->setUpAngle(attrf(a, "up-angle", 0) * DEG2RAD);
346 _airplane.addHook(h);
347 } else if(eq(name, "launchbar")) {
348 Launchbar* l = new Launchbar();
350 v[0] = attrf(a, "x");
351 v[1] = attrf(a, "y");
352 v[2] = attrf(a, "z");
353 l->setLaunchbarMount(v);
354 v[0] = attrf(a, "holdback-x", v[0]);
355 v[1] = attrf(a, "holdback-y", v[1]);
356 v[2] = attrf(a, "holdback-z", v[2]);
357 l->setHoldbackMount(v);
358 float length = attrf(a, "length", 1.0);
359 l->setLength(length);
360 l->setDownAngle(attrf(a, "down-angle", 45) * DEG2RAD);
361 l->setUpAngle(attrf(a, "up-angle", -45) * DEG2RAD);
362 l->setHoldbackLength(attrf(a, "holdback-length", 2.0));
363 _airplane.addLaunchbar(l);
364 } else if(eq(name, "fuselage")) {
366 v[0] = attrf(a, "ax");
367 v[1] = attrf(a, "ay");
368 v[2] = attrf(a, "az");
369 b[0] = attrf(a, "bx");
370 b[1] = attrf(a, "by");
371 b[2] = attrf(a, "bz");
372 float taper = attrf(a, "taper", 1);
373 float mid = attrf(a, "midpoint", 0.5);
374 float cx = attrf(a, "cx", 1);
375 float cy = attrf(a, "cy", 1);
376 float cz = attrf(a, "cz", 1);
377 float idrag = attrf(a, "idrag", 1);
378 _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid,
380 } else if(eq(name, "tank")) {
381 v[0] = attrf(a, "x");
382 v[1] = attrf(a, "y");
383 v[2] = attrf(a, "z");
384 float density = 6.0; // gasoline, in lbs/gal
385 if(a->hasAttribute("jet")) density = 6.72;
386 density *= LBS2KG*CM2GALS;
387 _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
388 } else if(eq(name, "ballast")) {
389 v[0] = attrf(a, "x");
390 v[1] = attrf(a, "y");
391 v[2] = attrf(a, "z");
392 _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
393 } else if(eq(name, "weight")) {
395 } else if(eq(name, "stall")) {
396 Wing* w = (Wing*)_currObj;
397 w->setStall(attrf(a, "aoa") * DEG2RAD);
398 w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
399 w->setStallPeak(attrf(a, "peak", 1.5));
400 } else if(eq(name, "flap0")) {
401 ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
402 attrf(a, "lift"), attrf(a, "drag"));
403 } else if(eq(name, "flap1")) {
404 ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
405 attrf(a, "lift"), attrf(a, "drag"));
406 } else if(eq(name, "slat")) {
407 ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
408 attrf(a, "aoa"), attrf(a, "drag"));
409 } else if(eq(name, "spoiler")) {
410 ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
411 attrf(a, "lift"), attrf(a, "drag"));
412 /* } else if(eq(name, "collective")) {
413 ((Rotor*)_currObj)->setcollective(attrf(a, "min"), attrf(a, "max"));
414 } else if(eq(name, "cyclic")) {
415 ((Rotor*)_currObj)->setcyclic(attrf(a, "ail"), attrf(a, "ele"));
417 } else if(eq(name, "actionpt")) {
418 v[0] = attrf(a, "x");
419 v[1] = attrf(a, "y");
420 v[2] = attrf(a, "z");
421 ((Thruster*)_currObj)->setPosition(v);
422 } else if(eq(name, "dir")) {
423 v[0] = attrf(a, "x");
424 v[1] = attrf(a, "y");
425 v[2] = attrf(a, "z");
426 ((Thruster*)_currObj)->setDirection(v);
427 } else if(eq(name, "control-setting")) {
428 // A cruise or approach control setting
429 const char* axis = a->getValue("axis");
430 float value = attrf(a, "value", 0);
432 _airplane.addCruiseControl(parseAxis(axis), value);
434 _airplane.addApproachControl(parseAxis(axis), value);
435 } else if(eq(name, "control-input")) {
437 // A mapping of input property to a control
438 int axis = parseAxis(a->getValue("axis"));
439 int control = parseOutput(a->getValue("control"));
441 opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
442 opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
443 opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
445 ControlMap* cm = _airplane.getControlMap();
446 if(a->hasAttribute("src0")) {
447 cm->addMapping(axis, control, _currObj, opt,
448 attrf(a, "src0"), attrf(a, "src1"),
449 attrf(a, "dst0"), attrf(a, "dst1"));
451 cm->addMapping(axis, control, _currObj, opt);
453 } else if(eq(name, "control-output")) {
454 // A property output for a control on the current object
455 ControlMap* cm = _airplane.getControlMap();
456 int type = parseOutput(a->getValue("control"));
457 int handle = cm->getOutputHandle(_currObj, type);
459 PropOut* p = new PropOut();
460 p->prop = fgGetNode(a->getValue("prop"), true);
463 p->left = !(a->hasAttribute("side") &&
464 eq("right", a->getValue("side")));
465 p->min = attrf(a, "min", cm->rangeMin(type));
466 p->max = attrf(a, "max", cm->rangeMax(type));
467 _controlProps.add(p);
469 } else if(eq(name, "control-speed")) {
470 ControlMap* cm = _airplane.getControlMap();
471 int type = parseOutput(a->getValue("control"));
472 int handle = cm->getOutputHandle(_currObj, type);
473 float time = attrf(a, "transition-time", 0);
475 cm->setTransitionTime(handle, time);
477 SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '"
478 << name << "' found in YASim aircraft description");
483 void FGFDM::getExternalInput(float dt)
487 _turb->setMagnitude(fgGetFloat("/environment/turbulence/magnitude-norm"));
488 _turb->update(dt, fgGetFloat("/environment/turbulence/rate-hz"));
491 ControlMap* cm = _airplane.getControlMap();
494 for(i=0; i<_axes.size(); i++) {
495 AxisRec* a = (AxisRec*)_axes.get(i);
496 float val = fgGetFloat(a->name, 0);
497 cm->setInput(a->handle, val);
499 cm->applyControls(dt);
502 for(i=0; i<_weights.size(); i++) {
503 WeightRec* wr = (WeightRec*)_weights.get(i);
504 _airplane.setWeight(wr->handle, LBS2KG * fgGetFloat(wr->prop));
507 for(i=0; i<_thrusters.size(); i++) {
508 EngRec* er = (EngRec*)_thrusters.get(i);
509 Thruster* t = er->eng;
511 if(t->getPropEngine()) {
512 PropEngine* p = t->getPropEngine();
513 sprintf(buf, "%s/rpm", er->prefix);
514 p->setOmega(fgGetFloat(buf, 500) * RPM2RAD);
519 // Linearly "seeks" a property by the specified fraction of the way to
520 // the target value. Used to emulate "slowly changing" output values.
521 static void moveprop(SGPropertyNode* node, const char* prop,
522 float target, float frac)
524 float val = node->getFloatValue(prop);
525 if(frac > 1) frac = 1;
526 if(frac < 0) frac = 0;
527 val += (target - val) * frac;
528 node->setFloatValue(prop, val);
531 void FGFDM::setOutputProperties(float dt)
536 float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
537 fgSetFloat("/yasim/gross-weight-lbs", grossWgt);
539 ControlMap* cm = _airplane.getControlMap();
540 for(i=0; i<_controlProps.size(); i++) {
541 PropOut* p = (PropOut*)_controlProps.get(i);
543 ? cm->getOutput(p->handle)
544 : cm->getOutputR(p->handle));
545 float rmin = cm->rangeMin(p->type);
546 float rmax = cm->rangeMax(p->type);
547 float frac = (val - rmin) / (rmax - rmin);
548 val = frac*(p->max - p->min) + p->min;
549 p->prop->setFloatValue(val);
552 for(i=0; i<_airplane.getRotorgear()->getNumRotors(); i++) {
553 Rotor*r=(Rotor*)_airplane.getRotorgear()->getRotor(i);
557 while((j = r->getValueforFGSet(j, b, &f)))
558 if(b[0]) fgSetFloat(b,f);
560 while((j = _airplane.getRotorgear()->getValueforFGSet(j, b, &f)))
561 if(b[0]) fgSetFloat(b,f);
562 for(j=0; j < r->numRotorparts(); j+=r->numRotorparts()>>2) {
563 Rotorpart* s = (Rotorpart*)r->getRotorpart(j);
567 b=s->getAlphaoutput(k);
568 if(b[0]) fgSetFloat(b, s->getAlpha(k));
573 // Use the density of the first tank, or a dummy value if no tanks
574 float fuelDensity = 1.0;
575 if(_airplane.numTanks())
576 fuelDensity = _airplane.getFuelDensity(0);
577 for(i=0; i<_thrusters.size(); i++) {
578 EngRec* er = (EngRec*)_thrusters.get(i);
579 Thruster* t = er->eng;
580 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
582 // Set: running, cranking, prop-thrust, max-hp, power-pct
583 node->setBoolValue("running", t->isRunning());
584 node->setBoolValue("cranking", t->isCranking());
588 float lbs = Math::mag3(tmp) * (KG2LBS/9.8);
589 node->setFloatValue("prop-thrust", lbs); // Deprecated name
590 node->setFloatValue("thrust-lbs", lbs);
591 node->setFloatValue("fuel-flow-gph",
592 (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
594 if(t->getPropEngine()) {
595 PropEngine* p = t->getPropEngine();
596 node->setFloatValue("rpm", p->getOmega() * (1/RPM2RAD));
597 node->setFloatValue("torque-ftlb",
598 p->getEngine()->getTorque() * NM2FTLB);
600 if(p->getEngine()->isPistonEngine()) {
601 PistonEngine* pe = p->getEngine()->isPistonEngine();
602 node->setFloatValue("mp-osi", pe->getMP() * (1/INHG2PA));
603 node->setFloatValue("mp-inhg", pe->getMP() * (1/INHG2PA));
604 node->setFloatValue("egt-degf",
605 pe->getEGT() * K2DEGF + K2DEGFOFFSET);
606 node->setFloatValue("oil-temperature-degf",
607 pe->getOilTemp() * K2DEGF + K2DEGFOFFSET);
608 node->setFloatValue("boost-gauge-inhg",
609 pe->getBoost() * (1/INHG2PA));
610 } else if(p->getEngine()->isTurbineEngine()) {
611 TurbineEngine* te = p->getEngine()->isTurbineEngine();
612 node->setFloatValue("n2", te->getN2());
617 Jet* j = t->getJet();
618 node->setFloatValue("n1", j->getN1());
619 node->setFloatValue("n2", j->getN2());
620 node->setFloatValue("epr", j->getEPR());
621 node->setFloatValue("egt-degf",
622 j->getEGT() * K2DEGF + K2DEGFOFFSET);
624 // These are "unmodeled" values that are still needed for
625 // many cockpits. Tie them all to the N1 speed, but
626 // normalize the numbers to the range [0:1] so the
627 // cockpit code can scale them to the right values.
628 float pnorm = j->getPerfNorm();
629 moveprop(node, "oilp-norm", pnorm, dt/3); // 3s seek time
630 moveprop(node, "oilt-norm", pnorm, dt/30); // 30s
631 moveprop(node, "itt-norm", pnorm, dt/1); // 1s
636 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
638 Wing* w = new Wing();
641 if(eq(type, "vstab"))
647 pos[0] = attrf(a, "x");
648 pos[1] = attrf(a, "y");
649 pos[2] = attrf(a, "z");
652 w->setLength(attrf(a, "length"));
653 w->setChord(attrf(a, "chord"));
654 w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
655 w->setTaper(attrf(a, "taper", 1));
656 w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
657 w->setCamber(attrf(a, "camber", 0));
659 // These come in with positive indicating positive AoA, but the
660 // internals expect a rotation about the left-pointing Y axis, so
662 w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD * -1);
663 w->setTwist(attrf(a, "twist", 0) * DEG2RAD * -1);
665 // The 70% is a magic number that sorta kinda seems to match known
666 // throttle settings to approach speed.
667 w->setInducedDrag(0.7*attrf(a, "idrag", 1));
669 float effect = attrf(a, "effectiveness", 1);
670 w->setDragScale(w->getDragScale()*effect);
676 Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
678 Rotor* w = new Rotor();
680 // float defDihed = 0;
683 pos[0] = attrf(a, "x");
684 pos[1] = attrf(a, "y");
685 pos[2] = attrf(a, "z");
689 normal[0] = attrf(a, "nx");
690 normal[1] = attrf(a, "ny");
691 normal[2] = attrf(a, "nz");
692 w->setNormal(normal);
695 forward[0] = attrf(a, "fx");
696 forward[1] = attrf(a, "fy");
697 forward[2] = attrf(a, "fz");
698 w->setForward(forward);
700 w->setMaxCyclicail(attrf(a, "maxcyclicail", 7.6));
701 w->setMaxCyclicele(attrf(a, "maxcyclicele", 4.94));
702 w->setMinCyclicail(attrf(a, "mincyclicail", -7.6));
703 w->setMinCyclicele(attrf(a, "mincyclicele", -4.94));
704 w->setMaxCollective(attrf(a, "maxcollective", 15.8));
705 w->setMinCollective(attrf(a, "mincollective", -0.2));
706 w->setDiameter(attrf(a, "diameter", 10.2));
707 w->setWeightPerBlade(attrf(a, "weightperblade", 44));
708 w->setNumberOfBlades(attrf(a, "numblades", 4));
709 w->setRelBladeCenter(attrf(a, "relbladecenter", 0.7));
710 w->setDynamic(attrf(a, "dynamic", 0.7));
711 w->setDelta3(attrf(a, "delta3", 0));
712 w->setDelta(attrf(a, "delta", 0));
713 w->setTranslift(attrf(a, "translift", 0.05));
714 w->setC2(attrf(a, "dragfactor", 1));
715 w->setStepspersecond(attrf(a, "stepspersecond", 120));
716 w->setPhiNull((attrf(a, "phi0", 0))*YASIM_PI/180);
717 w->setRPM(attrf(a, "rpm", 424));
718 w->setRelLenHinge(attrf(a, "rellenflaphinge", 0.07));
719 w->setAlpha0((attrf(a, "flap0", -5))*YASIM_PI/180);
720 w->setAlphamin((attrf(a, "flapmin", -15))/180*YASIM_PI);
721 w->setAlphamax((attrf(a, "flapmax", 15))*YASIM_PI/180);
722 w->setAlpha0factor(attrf(a, "flap0factor", 1));
723 w->setTeeterdamp(attrf(a,"teeterdamp",.0001));
724 w->setMaxteeterdamp(attrf(a,"maxteeterdamp",1000));
725 w->setRelLenTeeterHinge(attrf(a,"rellenteeterhinge",0.01));
726 w->setBalance(attrf(a,"balance",1.0));
727 w->setMinTiltYaw(attrf(a,"mintiltyaw",0.0));
728 w->setMinTiltPitch(attrf(a,"mintiltpitch",0.0));
729 w->setMinTiltRoll(attrf(a,"mintiltroll",0.0));
730 w->setMaxTiltYaw(attrf(a,"maxtiltyaw",0.0));
731 w->setMaxTiltPitch(attrf(a,"maxtiltpitch",0.0));
732 w->setMaxTiltRoll(attrf(a,"maxtiltroll",0.0));
733 w->setTiltCenterX(attrf(a,"tiltcenterx",0.0));
734 w->setTiltCenterY(attrf(a,"tiltcentery",0.0));
735 w->setTiltCenterZ(attrf(a,"tiltcenterz",0.0));
736 w->setDownwashFactor(attrf(a, "downwashfactor", 1));
739 if(attrb(a,"sharedflaphinge"))
740 w->setSharedFlapHinge(true);
742 if(a->hasAttribute("name"))
743 w->setName(a->getValue("name") );
744 if(a->hasAttribute("alphaout0"))
745 w->setAlphaoutput(0,a->getValue("alphaout0") );
746 if(a->hasAttribute("alphaout1")) w->setAlphaoutput(1,a->getValue("alphaout1") );
747 if(a->hasAttribute("alphaout2")) w->setAlphaoutput(2,a->getValue("alphaout2") );
748 if(a->hasAttribute("alphaout3")) w->setAlphaoutput(3,a->getValue("alphaout3") );
749 if(a->hasAttribute("coneout")) w->setAlphaoutput(4,a->getValue("coneout") );
750 if(a->hasAttribute("yawout")) w->setAlphaoutput(5,a->getValue("yawout") );
751 if(a->hasAttribute("rollout")) w->setAlphaoutput(6,a->getValue("rollout") );
753 w->setPitchA(attrf(a, "pitch-a", 10));
754 w->setPitchB(attrf(a, "pitch-b", 10));
755 w->setForceAtPitchA(attrf(a, "forceatpitch-a", 3000));
756 w->setPowerAtPitch0(attrf(a, "poweratpitch-0", 300));
757 w->setPowerAtPitchB(attrf(a, "poweratpitch-b", 3000));
758 if(attrb(a,"notorque"))
761 #define p(x) if (a->hasAttribute(#x)) w->setParameter((char *)#x,attrf(a,#x) );
762 #define p2(x,y) if (a->hasAttribute(y)) w->setParameter((char *)#x,attrf(a,y) );
763 p2(translift_ve,"translift-ve")
764 p2(translift_maxfactor,"translift-maxfactor")
765 p2(ground_effect_constant,"ground-effect-constant")
766 p2(vortex_state_lift_factor,"vortex-state-lift-factor")
767 p2(vortex_state_c1,"vortex-state-c1")
768 p2(vortex_state_c2,"vortex-state-c2")
769 p2(vortex_state_c3,"vortex-state_c3")
770 p2(vortex_state_e1,"vortex-state-e1")
771 p2(vortex_state_e2,"vortex-state-e2")
773 p2(number_of_segments,"number-of-segments")
774 p2(number_of_parts,"number-of-parts")
775 p2(rel_len_where_incidence_is_measured,"rel-len-where-incidence-is-measured")
778 p2(airfoil_incidence_no_lift,"airfoil-incidence-no-lift")
779 p2(rel_len_blade_start,"rel-len-blade-start")
780 p2(incidence_stall_zero_speed,"incidence-stall-zero-speed")
781 p2(incidence_stall_half_sonic_speed,"incidence-stall-half-sonic-speed")
782 p2(lift_factor_stall,"lift-factor-stall")
783 p2(stall_change_over,"stall-change-over")
784 p2(drag_factor_stall,"drag-factor-stall")
785 p2(airfoil_lift_coefficient,"airfoil-lift-coefficient")
786 p2(airfoil_drag_coefficient0,"airfoil-drag-coefficient0")
787 p2(airfoil_drag_coefficient1,"airfoil-drag-coefficient1")
788 p2(cyclic_factor,"cyclic-factor")
789 p2(rotor_correction_factor,"rotor-correction-factor")
796 void FGFDM::parsePistonEngine(XMLAttributes* a)
798 float engP = attrf(a, "eng-power") * HP2W;
799 float engS = attrf(a, "eng-rpm") * RPM2RAD;
801 PistonEngine* eng = new PistonEngine(engP, engS);
803 if(a->hasAttribute("displacement"))
804 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
806 if(a->hasAttribute("compression"))
807 eng->setCompression(attrf(a, "compression"));
809 if(a->hasAttribute("min-throttle"))
810 eng->setMinThrottle(attrf(a, "min-throttle"));
812 if(a->hasAttribute("turbo-mul")) {
813 float mul = attrf(a, "turbo-mul");
814 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
815 eng->setTurboParams(mul, mp);
816 eng->setTurboLag(attrf(a, "turbo-lag", 2));
819 if(a->hasAttribute("supercharger"))
820 eng->setSupercharger(attrb(a, "supercharger"));
822 ((PropEngine*)_currObj)->setEngine(eng);
825 void FGFDM::parseTurbineEngine(XMLAttributes* a)
827 float power = attrf(a, "eng-power") * HP2W;
828 float omega = attrf(a, "eng-rpm") * RPM2RAD;
829 float alt = attrf(a, "alt") * FT2M;
830 float flatRating = attrf(a, "flat-rating") * HP2W;
831 TurbineEngine* eng = new TurbineEngine(power, omega, alt, flatRating);
833 if(a->hasAttribute("n2-low-idle"))
834 eng->setN2Range(attrf(a, "n2-low-idle"), attrf(a, "n2-high-idle"),
837 // Nasty units conversion: lbs/hr per hp -> kg/s per watt
838 if(a->hasAttribute("bsfc"))
839 eng->setFuelConsumption(attrf(a, "bsfc") * (LBS2KG/(3600*HP2W)));
841 ((PropEngine*)_currObj)->setEngine(eng);
844 void FGFDM::parsePropeller(XMLAttributes* a)
846 // Legacy Handling for the old engines syntax:
847 PistonEngine* eng = 0;
848 if(a->hasAttribute("eng-power")) {
849 SG_LOG(SG_FLIGHT,SG_ALERT, "WARNING: "
850 << "Legacy engine definition in YASim configuration file. "
852 float engP = attrf(a, "eng-power") * HP2W;
853 float engS = attrf(a, "eng-rpm") * RPM2RAD;
854 eng = new PistonEngine(engP, engS);
855 if(a->hasAttribute("displacement"))
856 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
857 if(a->hasAttribute("compression"))
858 eng->setCompression(attrf(a, "compression"));
859 if(a->hasAttribute("turbo-mul")) {
860 float mul = attrf(a, "turbo-mul");
861 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
862 eng->setTurboParams(mul, mp);
866 // Now parse the actual propeller definition:
868 cg[0] = attrf(a, "x");
869 cg[1] = attrf(a, "y");
870 cg[2] = attrf(a, "z");
871 float mass = attrf(a, "mass") * LBS2KG;
872 float moment = attrf(a, "moment");
873 float radius = attrf(a, "radius");
874 float speed = attrf(a, "cruise-speed") * KTS2MPS;
875 float omega = attrf(a, "cruise-rpm") * RPM2RAD;
876 float power = attrf(a, "cruise-power") * HP2W;
877 float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
879 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
880 PropEngine* thruster = new PropEngine(prop, eng, moment);
881 _airplane.addThruster(thruster, mass, cg);
883 // Set the stops (fine = minimum pitch, coarse = maximum pitch)
884 float fine_stop = attrf(a, "fine-stop", 0.25f);
885 float coarse_stop = attrf(a, "coarse-stop", 4.0f);
886 prop->setStops(fine_stop, coarse_stop);
888 if(a->hasAttribute("takeoff-power")) {
889 float power0 = attrf(a, "takeoff-power") * HP2W;
890 float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
891 prop->setTakeoff(omega0, power0);
894 if(a->hasAttribute("max-rpm")) {
895 float max = attrf(a, "max-rpm") * RPM2RAD;
896 float min = attrf(a, "min-rpm") * RPM2RAD;
897 thruster->setVariableProp(min, max);
900 if(attrb(a, "contra"))
901 thruster->setContraPair(true);
903 if(a->hasAttribute("manual-pitch")) {
904 prop->setManualPitch();
907 thruster->setGearRatio(attrf(a, "gear-ratio", 1));
910 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
911 EngRec* er = new EngRec();
913 er->prefix = dup(buf);
919 // Turns a string axis name into an integer for use by the
920 // ControlMap. Creates a new axis if this one hasn't been defined
922 int FGFDM::parseAxis(const char* name)
925 for(i=0; i<_axes.size(); i++) {
926 AxisRec* a = (AxisRec*)_axes.get(i);
927 if(eq(a->name, name))
931 // Not there, make a new one.
932 AxisRec* a = new AxisRec();
934 fgGetNode( a->name, true ); // make sure the property name exists
935 a->handle = _airplane.getControlMap()->newInput();
940 int FGFDM::parseOutput(const char* name)
942 if(eq(name, "THROTTLE")) return ControlMap::THROTTLE;
943 if(eq(name, "MIXTURE")) return ControlMap::MIXTURE;
944 if(eq(name, "CONDLEVER")) return ControlMap::CONDLEVER;
945 if(eq(name, "STARTER")) return ControlMap::STARTER;
946 if(eq(name, "MAGNETOS")) return ControlMap::MAGNETOS;
947 if(eq(name, "ADVANCE")) return ControlMap::ADVANCE;
948 if(eq(name, "REHEAT")) return ControlMap::REHEAT;
949 if(eq(name, "BOOST")) return ControlMap::BOOST;
950 if(eq(name, "VECTOR")) return ControlMap::VECTOR;
951 if(eq(name, "PROP")) return ControlMap::PROP;
952 if(eq(name, "BRAKE")) return ControlMap::BRAKE;
953 if(eq(name, "STEER")) return ControlMap::STEER;
954 if(eq(name, "EXTEND")) return ControlMap::EXTEND;
955 if(eq(name, "HEXTEND")) return ControlMap::HEXTEND;
956 if(eq(name, "LEXTEND")) return ControlMap::LEXTEND;
957 if(eq(name, "LACCEL")) return ControlMap::LACCEL;
958 if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
959 if(eq(name, "FLAP0")) return ControlMap::FLAP0;
960 if(eq(name, "FLAP0EFFECTIVENESS")) return ControlMap::FLAP0EFFECTIVENESS;
961 if(eq(name, "FLAP1")) return ControlMap::FLAP1;
962 if(eq(name, "FLAP1EFFECTIVENESS")) return ControlMap::FLAP1EFFECTIVENESS;
963 if(eq(name, "SLAT")) return ControlMap::SLAT;
964 if(eq(name, "SPOILER")) return ControlMap::SPOILER;
965 if(eq(name, "CASTERING")) return ControlMap::CASTERING;
966 if(eq(name, "PROPPITCH")) return ControlMap::PROPPITCH;
967 if(eq(name, "PROPFEATHER")) return ControlMap::PROPFEATHER;
968 if(eq(name, "COLLECTIVE")) return ControlMap::COLLECTIVE;
969 if(eq(name, "CYCLICAIL")) return ControlMap::CYCLICAIL;
970 if(eq(name, "CYCLICELE")) return ControlMap::CYCLICELE;
971 if(eq(name, "TILTROLL")) return ControlMap::TILTROLL;
972 if(eq(name, "TILTPITCH")) return ControlMap::TILTPITCH;
973 if(eq(name, "TILTYAW")) return ControlMap::TILTYAW;
974 if(eq(name, "ROTORGEARENGINEON")) return ControlMap::ROTORENGINEON;
975 if(eq(name, "ROTORBRAKE")) return ControlMap::ROTORBRAKE;
976 if(eq(name, "ROTORENGINEMAXRELTORQUE"))
977 return ControlMap::ROTORENGINEMAXRELTORQUE;
978 if(eq(name, "ROTORRELTARGET")) return ControlMap::ROTORRELTARGET;
979 if(eq(name, "ROTORBALANCE")) return ControlMap::ROTORBALANCE;
980 if(eq(name, "REVERSE_THRUST")) return ControlMap::REVERSE_THRUST;
981 if(eq(name, "WASTEGATE")) return ControlMap::WASTEGATE;
982 if(eq(name, "WINCHRELSPEED")) return ControlMap::WINCHRELSPEED;
983 if(eq(name, "HITCHOPEN")) return ControlMap::HITCHOPEN;
984 if(eq(name, "PLACEWINCH")) return ControlMap::PLACEWINCH;
985 if(eq(name, "FINDAITOW")) return ControlMap::FINDAITOW;
987 SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
988 << name << "' in YASim aircraft description.");
993 void FGFDM::parseWeight(XMLAttributes* a)
995 WeightRec* wr = new WeightRec();
998 v[0] = attrf(a, "x");
999 v[1] = attrf(a, "y");
1000 v[2] = attrf(a, "z");
1002 wr->prop = dup(a->getValue("mass-prop"));
1003 wr->size = attrf(a, "size", 0);
1004 wr->handle = _airplane.addWeight(v, wr->size);
1009 bool FGFDM::eq(const char* a, const char* b)
1011 // Figure it out for yourself. :)
1012 while(*a && *b && *a == *b) { a++; b++; }
1016 char* FGFDM::dup(const char* s)
1020 char* s2 = new char[len+1];
1022 while((*p++ = *s++));
1027 int FGFDM::attri(XMLAttributes* atts, const char* attr)
1029 if(!atts->hasAttribute(attr)) {
1030 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1031 "' in YASim aircraft description");
1034 return attri(atts, attr, 0);
1037 int FGFDM::attri(XMLAttributes* atts, const char* attr, int def)
1039 const char* val = atts->getValue(attr);
1040 if(val == 0) return def;
1041 else return atol(val);
1044 float FGFDM::attrf(XMLAttributes* atts, const char* attr)
1046 if(!atts->hasAttribute(attr)) {
1047 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1048 "' in YASim aircraft description");
1051 return attrf(atts, attr, 0);
1054 float FGFDM::attrf(XMLAttributes* atts, const char* attr, float def)
1056 const char* val = atts->getValue(attr);
1057 if(val == 0) return def;
1058 else return (float)atof(val);
1061 double FGFDM::attrd(XMLAttributes* atts, const char* attr)
1063 if(!atts->hasAttribute(attr)) {
1064 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1065 "' in YASim aircraft description");
1068 return attrd(atts, attr, 0);
1071 double FGFDM::attrd(XMLAttributes* atts, const char* attr, double def)
1073 const char* val = atts->getValue(attr);
1074 if(val == 0) return def;
1075 else return atof(val);
1078 // ACK: the dreaded ambiguous string boolean. Remind me to shoot Maik
1079 // when I have a chance. :). Unless you have a parser that can check
1080 // symbol constants (we don't), this kind of coding is just a Bad
1081 // Idea. This implementation, for example, silently returns a boolean
1082 // falsehood for values of "1", "yes", "True", and "TRUE". Which is
1083 // especially annoying preexisting boolean attributes in the same
1084 // parser want to see "1" and will choke on a "true"...
1086 // Unfortunately, this usage creeped into existing configuration files
1087 // while I wasn't active, and it's going to be hard to remove. Issue
1088 // a warning to nag people into changing their ways for now...
1089 bool FGFDM::attrb(XMLAttributes* atts, const char* attr)
1091 const char* val = atts->getValue(attr);
1092 if(val == 0) return false;
1094 if(eq(val,"true")) {
1095 SG_LOG(SG_FLIGHT, SG_ALERT, "Warning: " <<
1096 "deprecated 'true' boolean in YASim configuration file. " <<
1097 "Use numeric booleans (attribute=\"1\") instead");
1100 return attri(atts, attr, 0) ? true : false;
1103 }; // namespace yasim