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);
71 for(int i=0; i<_axes.size(); i++) {
72 AxisRec* a = (AxisRec*)_axes.get(i);
77 for(int i=0; i<_thrusters.size(); i++) {
78 EngRec* er = (EngRec*)_thrusters.get(i);
84 for(int i=0; i<_weights.size(); i++) {
85 WeightRec* wr = (WeightRec*)_weights.get(i);
90 for(int i=0; i<_controlProps.size(); i++)
91 delete (PropOut*)_controlProps.get(i);
96 void FGFDM::iterate(float dt)
99 _airplane.iterate(dt);
102 for(int i=0; i<_airplane.numThrusters(); i++) {
103 Thruster* t = _airplane.getThruster(i);
105 bool out_of_fuel = _fuel_props[i]._out_of_fuel->getBoolValue();
106 t->setFuelState(!out_of_fuel);
108 double consumed = _fuel_props[i]._fuel_consumed_lbs->getDoubleValue();
109 _fuel_props[i]._fuel_consumed_lbs->setDoubleValue(
110 consumed + dt * KG2LBS * t->getFuelFlow());
112 for(int i=0; i<_airplane.numTanks(); i++) {
113 _airplane.setFuel(i, LBS2KG * _tank_level_lbs[i]->getFloatValue());
115 _airplane.calcFuelWeights();
117 setOutputProperties(dt);
120 Airplane* FGFDM::getAirplane()
127 _turb_magnitude_norm = fgGetNode("/environment/turbulence/magnitude-norm", true);
128 _turb_rate_hz = fgGetNode("/environment/turbulence/rate-hz", true);
129 _gross_weight_lbs = fgGetNode("/yasim/gross-weight-lbs", true);
131 // Allows the user to start with something other than full fuel
132 _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
134 // Read out the resulting fuel state and stash engine/thruster properties
135 _thrust_props.clear();
136 for (int i=0; i<_thrusters.size(); i++) {
137 SGPropertyNode_ptr node = fgGetNode("engines/engine", i, true);
138 Thruster* t = ((EngRec*)_thrusters.get(i))->eng;
141 tp._running = node->getChild("running", 0, true);
142 tp._cranking = node->getChild("cranking", 0, true);
143 tp._prop_thrust = node->getChild("prop-thrust", 0, true); // Deprecated name
144 tp._thrust_lbs = node->getChild("thrust-lbs", 0, true);
145 tp._fuel_flow_gph = node->getChild("fuel-flow-gph", 0, true);
147 if(t->getPropEngine())
149 tp._rpm = node->getChild("rpm", 0, true);
150 tp._torque_ftlb = node->getChild("torque-ftlb", 0, true);
152 PropEngine* p = t->getPropEngine();
153 if(p->getEngine()->isPistonEngine())
155 tp._mp_osi = node->getChild("mp-osi", 0, true);
156 tp._mp_inhg = node->getChild("mp-inhg", 0, true);
157 tp._egt_degf = node->getChild("egt-degf", 0, true);
159 tp._oil_temperature_degf = node->getChild("oil-temperature-degf", 0, true);
160 tp._boost_gauge_inhg = node->getChild("boost-gauge-inhg", 0, true);
161 } else if(p->getEngine()->isTurbineEngine()) {
162 tp._n2 = node->getChild("n2", 0, true);
168 tp._n1 = node->getChild("n1", 0, true);
169 tp._n2 = node->getChild("n2", 0, true);
170 tp._epr = node->getChild("epr", 0, true);
171 tp._egt_degf = node->getChild("egt-degf", 0, true);
173 _thrust_props.push_back(tp);
176 // stash properties for engines/fuel state
177 _thrust_props.clear();
178 for(int i=0; i<_airplane.numThrusters(); i++) {
179 SGPropertyNode_ptr e = fgGetNode("engines/engine", i, true);
181 f._out_of_fuel = e->getChild("out-of-fuel", 0, true);
182 f._fuel_consumed_lbs = e->getChild("fuel-consumed-lbs", 0, true);
183 _fuel_props.push_back(f);
186 // initialize tanks and stash properties for tank level
187 _tank_level_lbs.clear();
188 for(int i=0; i<_airplane.numTanks(); i++) {
190 sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
191 fgSetDouble(buf, _airplane.getFuel(i) * KG2LBS);
192 _tank_level_lbs.push_back(fgGetNode(buf, true));
194 double density = _airplane.getFuelDensity(i);
195 sprintf(buf, "/consumables/fuel/tank[%d]/density-ppg", i);
196 fgSetDouble(buf, density * (KG2LBS/CM2GALS));
198 // set in TankProperties class
199 // sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
200 // fgSetDouble(buf, _airplane.getFuel(i) * CM2GALS / density);
202 sprintf(buf, "/consumables/fuel/tank[%d]/capacity-gal_us", i);
203 fgSetDouble(buf, CM2GALS * _airplane.getTankCapacity(i)/density);
206 // This has a nasty habit of being false at startup. That's not
208 fgSetBool("/controls/gear/gear-down", true);
210 _airplane.getModel()->setTurbulence(_turb);
213 // Not the worlds safest parser. But it's short & sweet.
214 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
216 XMLAttributes* a = (XMLAttributes*)&atts;
220 if(eq(name, "airplane")) {
221 _airplane.setWeight(attrf(a, "mass") * LBS2KG);
222 } else if(eq(name, "approach")) {
223 float spd = attrf(a, "speed") * KTS2MPS;
224 float alt = attrf(a, "alt", 0) * FT2M;
225 float aoa = attrf(a, "aoa", 0) * DEG2RAD;
226 float gla = attrf(a, "glide-angle", 0) * DEG2RAD;
227 _airplane.setApproach(spd, alt, aoa, attrf(a, "fuel", 0.2),gla);
229 } else if(eq(name, "cruise")) {
230 float spd = attrf(a, "speed") * KTS2MPS;
231 float alt = attrf(a, "alt") * FT2M;
232 float gla = attrf(a, "glide-angle", 0) * DEG2RAD;
233 _airplane.setCruise(spd, alt, attrf(a, "fuel", 0.5),gla);
235 } else if(eq(name, "solve-weight")) {
236 int idx = attri(a, "idx");
237 float wgt = attrf(a, "weight") * LBS2KG;
238 _airplane.addSolutionWeight(!_cruiseCurr, idx, wgt);
239 } else if(eq(name, "cockpit")) {
240 v[0] = attrf(a, "x");
241 v[1] = attrf(a, "y");
242 v[2] = attrf(a, "z");
243 _airplane.setPilotPos(v);
244 } else if(eq(name, "rotor")) {
245 _airplane.getModel()->getRotorgear()->addRotor(parseRotor(a, name));
246 } else if(eq(name, "rotorgear")) {
247 Rotorgear* r = _airplane.getModel()->getRotorgear();
249 #define p(x) if (a->hasAttribute(#x)) r->setParameter((char *)#x,attrf(a,#x) );
250 #define p2(x,y) if (a->hasAttribute(y)) r->setParameter((char *)#x,attrf(a,y) );
251 p2(max_power_engine,"max-power-engine")
252 p2(engine_prop_factor,"engine-prop-factor")
255 p2(max_power_rotor_brake,"max-power-rotor-brake")
256 p2(rotorgear_friction,"rotorgear-friction")
257 p2(engine_accel_limit,"engine-accel-limit")
261 } else if(eq(name, "wing")) {
262 _airplane.setWing(parseWing(a, name));
263 } else if(eq(name, "hstab")) {
264 _airplane.setTail(parseWing(a, name));
265 } else if(eq(name, "vstab") || eq(name, "mstab")) {
266 _airplane.addVStab(parseWing(a, name));
267 } else if(eq(name, "piston-engine")) {
268 parsePistonEngine(a);
269 } else if(eq(name, "turbine-engine")) {
270 parseTurbineEngine(a);
271 } else if(eq(name, "propeller")) {
273 } else if(eq(name, "thruster")) {
274 SimpleJet* j = new SimpleJet();
276 v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
278 _airplane.addThruster(j, 0, v);
279 v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
281 j->setThrust(attrf(a, "thrust") * LBS2N);
282 } else if(eq(name, "jet")) {
285 v[0] = attrf(a, "x");
286 v[1] = attrf(a, "y");
287 v[2] = attrf(a, "z");
288 float mass = attrf(a, "mass") * LBS2KG;
289 j->setMaxThrust(attrf(a, "thrust") * LBS2N,
290 attrf(a, "afterburner", 0) * LBS2N);
291 j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
292 j->setReverseThrust(attrf(a, "reverse", 0.2));
294 float n1min = attrf(a, "n1-idle", 55);
295 float n1max = attrf(a, "n1-max", 102);
296 float n2min = attrf(a, "n2-idle", 73);
297 float n2max = attrf(a, "n2-max", 103);
298 j->setRPMs(n1min, n1max, n2min, n2max);
300 j->setTSFC(attrf(a, "tsfc", 0.8));
301 if(a->hasAttribute("egt")) j->setEGT(attrf(a, "egt"));
302 if(a->hasAttribute("epr")) j->setEPR(attrf(a, "epr"));
303 if(a->hasAttribute("exhaust-speed"))
304 j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
305 if(a->hasAttribute("spool-time"))
306 j->setSpooling(attrf(a, "spool-time"));
309 _airplane.addThruster(j, mass, v);
310 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
311 EngRec* er = new EngRec();
313 er->prefix = dup(buf);
315 } else if(eq(name, "hitch")) {
316 Hitch* h = new Hitch(a->getValue("name"));
318 v[0] = attrf(a, "x");
319 v[1] = attrf(a, "y");
320 v[2] = attrf(a, "z");
322 if(a->hasAttribute("force-is-calculated-by-other")) h->setForceIsCalculatedByOther(attrb(a,"force-is-calculated-by-other"));
323 _airplane.addHitch(h);
324 } else if(eq(name, "tow")) {
325 Hitch* h = (Hitch*)_currObj;
326 if(a->hasAttribute("length"))
327 h->setTowLength(attrf(a, "length"));
328 if(a->hasAttribute("elastic-constant"))
329 h->setTowElasticConstant(attrf(a, "elastic-constant"));
330 if(a->hasAttribute("break-force"))
331 h->setTowBreakForce(attrf(a, "break-force"));
332 if(a->hasAttribute("weight-per-meter"))
333 h->setTowWeightPerM(attrf(a, "weight-per-meter"));
334 if(a->hasAttribute("mp-auto-connect-period"))
335 h->setMpAutoConnectPeriod(attrf(a, "mp-auto-connect-period"));
336 } else if(eq(name, "winch")) {
337 Hitch* h = (Hitch*)_currObj;
339 pos[0] = attrd(a, "x",0);
340 pos[1] = attrd(a, "y",0);
341 pos[2] = attrd(a, "z",0);
342 h->setWinchPosition(pos);
343 if(a->hasAttribute("max-speed"))
344 h->setWinchMaxSpeed(attrf(a, "max-speed"));
345 if(a->hasAttribute("power"))
346 h->setWinchPower(attrf(a, "power") * 1000);
347 if(a->hasAttribute("max-force"))
348 h->setWinchMaxForce(attrf(a, "max-force"));
349 if(a->hasAttribute("initial-tow-length"))
350 h->setWinchInitialTowLength(attrf(a, "initial-tow-length"));
351 if(a->hasAttribute("max-tow-length"))
352 h->setWinchMaxTowLength(attrf(a, "max-tow-length"));
353 if(a->hasAttribute("min-tow-length"))
354 h->setWinchMinTowLength(attrf(a, "min-tow-length"));
355 } else if(eq(name, "gear")) {
356 Gear* g = new Gear();
358 v[0] = attrf(a, "x");
359 v[1] = attrf(a, "y");
360 v[2] = attrf(a, "z");
362 float nrm = Math::mag3(v);
363 if (_vehicle_radius < nrm)
364 _vehicle_radius = nrm;
365 if(a->hasAttribute("upx")) {
366 v[0] = attrf(a, "upx");
367 v[1] = attrf(a, "upy");
368 v[2] = attrf(a, "upz");
375 for(int i=0; i<3; i++)
376 v[i] *= attrf(a, "compression", 1);
377 g->setCompression(v);
378 g->setBrake(attrf(a, "skid", 0));
379 g->setInitialLoad(attrf(a, "initial-load", 0));
380 g->setStaticFriction(attrf(a, "sfric", 0.8));
381 g->setDynamicFriction(attrf(a, "dfric", 0.7));
382 g->setSpring(attrf(a, "spring", 1));
383 g->setDamping(attrf(a, "damp", 1));
384 if(a->hasAttribute("on-water")) g->setOnWater(attrb(a,"on-water"));
385 if(a->hasAttribute("on-solid")) g->setOnSolid(attrb(a,"on-solid"));
386 if(a->hasAttribute("ignored-by-solver")) g->setIgnoreWhileSolving(attrb(a,"ignored-by-solver"));
387 g->setSpringFactorNotPlaning(attrf(a, "spring-factor-not-planing", 1));
388 g->setSpeedPlaning(attrf(a, "speed-planing", 0) * KTS2MPS);
389 g->setReduceFrictionByExtension(attrf(a, "reduce-friction-by-extension", 0));
390 _airplane.addGear(g);
391 } else if(eq(name, "hook")) {
392 Hook* h = new Hook();
394 v[0] = attrf(a, "x");
395 v[1] = attrf(a, "y");
396 v[2] = attrf(a, "z");
398 float length = attrf(a, "length", 1.0);
399 h->setLength(length);
400 float nrm = length+Math::mag3(v);
401 if (_vehicle_radius < nrm)
402 _vehicle_radius = nrm;
403 h->setDownAngle(attrf(a, "down-angle", 70) * DEG2RAD);
404 h->setUpAngle(attrf(a, "up-angle", 0) * DEG2RAD);
405 _airplane.addHook(h);
406 } else if(eq(name, "launchbar")) {
407 Launchbar* l = new Launchbar();
409 v[0] = attrf(a, "x");
410 v[1] = attrf(a, "y");
411 v[2] = attrf(a, "z");
412 l->setLaunchbarMount(v);
413 v[0] = attrf(a, "holdback-x", v[0]);
414 v[1] = attrf(a, "holdback-y", v[1]);
415 v[2] = attrf(a, "holdback-z", v[2]);
416 l->setHoldbackMount(v);
417 float length = attrf(a, "length", 1.0);
418 l->setLength(length);
419 l->setDownAngle(attrf(a, "down-angle", 45) * DEG2RAD);
420 l->setUpAngle(attrf(a, "up-angle", -45) * DEG2RAD);
421 l->setHoldbackLength(attrf(a, "holdback-length", 2.0));
422 _airplane.addLaunchbar(l);
423 } else if(eq(name, "fuselage")) {
425 v[0] = attrf(a, "ax");
426 v[1] = attrf(a, "ay");
427 v[2] = attrf(a, "az");
428 b[0] = attrf(a, "bx");
429 b[1] = attrf(a, "by");
430 b[2] = attrf(a, "bz");
431 float taper = attrf(a, "taper", 1);
432 float mid = attrf(a, "midpoint", 0.5);
433 float cx = attrf(a, "cx", 1);
434 float cy = attrf(a, "cy", 1);
435 float cz = attrf(a, "cz", 1);
436 float idrag = attrf(a, "idrag", 1);
437 _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid,
439 } else if(eq(name, "tank")) {
440 v[0] = attrf(a, "x");
441 v[1] = attrf(a, "y");
442 v[2] = attrf(a, "z");
443 float density = 6.0; // gasoline, in lbs/gal
444 if(a->hasAttribute("jet")) density = 6.72;
445 density *= LBS2KG*CM2GALS;
446 _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
447 } else if(eq(name, "ballast")) {
448 v[0] = attrf(a, "x");
449 v[1] = attrf(a, "y");
450 v[2] = attrf(a, "z");
451 _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
452 } else if(eq(name, "weight")) {
454 } else if(eq(name, "stall")) {
455 Wing* w = (Wing*)_currObj;
456 w->setStall(attrf(a, "aoa") * DEG2RAD);
457 w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
458 w->setStallPeak(attrf(a, "peak", 1.5));
459 } else if(eq(name, "flap0")) {
460 ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
461 attrf(a, "lift"), attrf(a, "drag"));
462 } else if(eq(name, "flap1")) {
463 ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
464 attrf(a, "lift"), attrf(a, "drag"));
465 } else if(eq(name, "slat")) {
466 ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
467 attrf(a, "aoa"), attrf(a, "drag"));
468 } else if(eq(name, "spoiler")) {
469 ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
470 attrf(a, "lift"), attrf(a, "drag"));
471 /* } else if(eq(name, "collective")) {
472 ((Rotor*)_currObj)->setcollective(attrf(a, "min"), attrf(a, "max"));
473 } else if(eq(name, "cyclic")) {
474 ((Rotor*)_currObj)->setcyclic(attrf(a, "ail"), attrf(a, "ele"));
476 } else if(eq(name, "actionpt")) {
477 v[0] = attrf(a, "x");
478 v[1] = attrf(a, "y");
479 v[2] = attrf(a, "z");
480 ((Thruster*)_currObj)->setPosition(v);
481 } else if(eq(name, "dir")) {
482 v[0] = attrf(a, "x");
483 v[1] = attrf(a, "y");
484 v[2] = attrf(a, "z");
485 ((Thruster*)_currObj)->setDirection(v);
486 } else if(eq(name, "control-setting")) {
487 // A cruise or approach control setting
488 const char* axis = a->getValue("axis");
489 float value = attrf(a, "value", 0);
491 _airplane.addCruiseControl(parseAxis(axis), value);
493 _airplane.addApproachControl(parseAxis(axis), value);
494 } else if(eq(name, "control-input")) {
496 // A mapping of input property to a control
497 int axis = parseAxis(a->getValue("axis"));
498 int control = parseOutput(a->getValue("control"));
500 opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
501 opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
502 opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
504 ControlMap* cm = _airplane.getControlMap();
505 if(a->hasAttribute("src0")) {
506 cm->addMapping(axis, control, _currObj, opt,
507 attrf(a, "src0"), attrf(a, "src1"),
508 attrf(a, "dst0"), attrf(a, "dst1"));
510 cm->addMapping(axis, control, _currObj, opt);
512 } else if(eq(name, "control-output")) {
513 // A property output for a control on the current object
514 ControlMap* cm = _airplane.getControlMap();
515 int type = parseOutput(a->getValue("control"));
516 int handle = cm->getOutputHandle(_currObj, type);
518 PropOut* p = new PropOut();
519 p->prop = fgGetNode(a->getValue("prop"), true);
522 p->left = !(a->hasAttribute("side") &&
523 eq("right", a->getValue("side")));
524 p->min = attrf(a, "min", cm->rangeMin(type));
525 p->max = attrf(a, "max", cm->rangeMax(type));
526 _controlProps.add(p);
528 } else if(eq(name, "control-speed")) {
529 ControlMap* cm = _airplane.getControlMap();
530 int type = parseOutput(a->getValue("control"));
531 int handle = cm->getOutputHandle(_currObj, type);
532 float time = attrf(a, "transition-time", 0);
534 cm->setTransitionTime(handle, time);
536 SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '"
537 << name << "' found in YASim aircraft description");
542 void FGFDM::getExternalInput(float dt)
546 _turb->setMagnitude(_turb_magnitude_norm->getFloatValue());
547 _turb->update(dt, _turb_rate_hz->getFloatValue());
550 ControlMap* cm = _airplane.getControlMap();
553 for(int i=0; i<_axes.size(); i++) {
554 AxisRec* a = (AxisRec*)_axes.get(i);
555 float val = fgGetFloat(a->name, 0);
556 cm->setInput(a->handle, val);
558 cm->applyControls(dt);
561 for(int i=0; i<_weights.size(); i++) {
562 WeightRec* wr = (WeightRec*)_weights.get(i);
563 _airplane.setWeight(wr->handle, LBS2KG * fgGetFloat(wr->prop));
566 for(int i=0; i<_thrusters.size(); i++) {
567 EngRec* er = (EngRec*)_thrusters.get(i);
568 Thruster* t = er->eng;
570 if(t->getPropEngine()) {
571 PropEngine* p = t->getPropEngine();
572 sprintf(buf, "%s/rpm", er->prefix);
573 p->setOmega(fgGetFloat(buf, 500) * RPM2RAD);
578 // Linearly "seeks" a property by the specified fraction of the way to
579 // the target value. Used to emulate "slowly changing" output values.
580 static void moveprop(SGPropertyNode* node, const char* prop,
581 float target, float frac)
583 float val = node->getFloatValue(prop);
584 if(frac > 1) frac = 1;
585 if(frac < 0) frac = 0;
586 val += (target - val) * frac;
587 node->setFloatValue(prop, val);
590 void FGFDM::setOutputProperties(float dt)
592 float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
593 _gross_weight_lbs->setFloatValue(grossWgt);
595 ControlMap* cm = _airplane.getControlMap();
596 for(int i=0; i<_controlProps.size(); i++) {
597 PropOut* p = (PropOut*)_controlProps.get(i);
599 ? cm->getOutput(p->handle)
600 : cm->getOutputR(p->handle));
601 float rmin = cm->rangeMin(p->type);
602 float rmax = cm->rangeMax(p->type);
603 float frac = (val - rmin) / (rmax - rmin);
604 val = frac*(p->max - p->min) + p->min;
605 p->prop->setFloatValue(val);
608 for(int i=0; i<_airplane.getRotorgear()->getNumRotors(); i++) {
609 Rotor*r=(Rotor*)_airplane.getRotorgear()->getRotor(i);
613 while((j = r->getValueforFGSet(j, b, &f)))
614 if(b[0]) fgSetFloat(b,f);
616 while((j = _airplane.getRotorgear()->getValueforFGSet(j, b, &f)))
617 if(b[0]) fgSetFloat(b,f);
618 for(j=0; j < r->numRotorparts(); j+=r->numRotorparts()>>2) {
619 Rotorpart* s = (Rotorpart*)r->getRotorpart(j);
623 b=s->getAlphaoutput(k);
624 if(b[0]) fgSetFloat(b, s->getAlpha(k));
629 // Use the density of the first tank, or a dummy value if no tanks
630 float fuelDensity = 1.0;
631 if(_airplane.numTanks())
632 fuelDensity = _airplane.getFuelDensity(0);
633 for(int i=0; i<_thrusters.size(); i++) {
634 EngRec* er = (EngRec*)_thrusters.get(i);
635 Thruster* t = er->eng;
636 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
638 ThrusterProps& tp = _thrust_props[i];
640 // Set: running, cranking, prop-thrust, max-hp, power-pct
641 tp._running->setBoolValue(t->isRunning());
642 tp._cranking->setBoolValue(t->isCranking());
646 float lbs = Math::mag3(tmp) * (KG2LBS/9.8);
647 tp._prop_thrust->setFloatValue(lbs); // Deprecated name
648 tp._thrust_lbs->setFloatValue(lbs);
649 tp._fuel_flow_gph->setFloatValue(
650 (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
652 if(t->getPropEngine()) {
653 PropEngine* p = t->getPropEngine();
654 tp._rpm->setFloatValue(p->getOmega() * (1/RPM2RAD));
655 tp._torque_ftlb->setFloatValue(
656 p->getEngine()->getTorque() * NM2FTLB);
658 if(p->getEngine()->isPistonEngine()) {
659 PistonEngine* pe = p->getEngine()->isPistonEngine();
660 tp._mp_osi->setFloatValue(pe->getMP() * (1/INHG2PA));
661 tp._mp_inhg->setFloatValue(pe->getMP() * (1/INHG2PA));
662 tp._egt_degf->setFloatValue(
663 pe->getEGT() * K2DEGF + K2DEGFOFFSET);
664 tp._oil_temperature_degf->setFloatValue(
665 pe->getOilTemp() * K2DEGF + K2DEGFOFFSET);
666 tp._boost_gauge_inhg->setFloatValue(
667 pe->getBoost() * (1/INHG2PA));
668 } else if(p->getEngine()->isTurbineEngine()) {
669 TurbineEngine* te = p->getEngine()->isTurbineEngine();
670 tp._n2->setFloatValue(te->getN2());
675 Jet* j = t->getJet();
676 tp._n1->setFloatValue(j->getN1());
677 tp._n2->setFloatValue(j->getN2());
678 tp._epr->setFloatValue(j->getEPR());
679 tp._egt_degf->setFloatValue(
680 j->getEGT() * K2DEGF + K2DEGFOFFSET);
682 // These are "unmodeled" values that are still needed for
683 // many cockpits. Tie them all to the N1 speed, but
684 // normalize the numbers to the range [0:1] so the
685 // cockpit code can scale them to the right values.
686 float pnorm = j->getPerfNorm();
687 moveprop(node, "oilp-norm", pnorm, dt/3); // 3s seek time
688 moveprop(node, "oilt-norm", pnorm, dt/30); // 30s
689 moveprop(node, "itt-norm", pnorm, dt/1); // 1s
694 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
696 Wing* w = new Wing();
699 if(eq(type, "vstab"))
705 pos[0] = attrf(a, "x");
706 pos[1] = attrf(a, "y");
707 pos[2] = attrf(a, "z");
710 w->setLength(attrf(a, "length"));
711 w->setChord(attrf(a, "chord"));
712 w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
713 w->setTaper(attrf(a, "taper", 1));
714 w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
715 w->setCamber(attrf(a, "camber", 0));
717 // These come in with positive indicating positive AoA, but the
718 // internals expect a rotation about the left-pointing Y axis, so
720 w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD * -1);
721 w->setTwist(attrf(a, "twist", 0) * DEG2RAD * -1);
723 // The 70% is a magic number that sorta kinda seems to match known
724 // throttle settings to approach speed.
725 w->setInducedDrag(0.7*attrf(a, "idrag", 1));
727 float effect = attrf(a, "effectiveness", 1);
728 w->setDragScale(w->getDragScale()*effect);
734 Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
736 Rotor* w = new Rotor();
738 // float defDihed = 0;
741 pos[0] = attrf(a, "x");
742 pos[1] = attrf(a, "y");
743 pos[2] = attrf(a, "z");
747 normal[0] = attrf(a, "nx");
748 normal[1] = attrf(a, "ny");
749 normal[2] = attrf(a, "nz");
750 w->setNormal(normal);
753 forward[0] = attrf(a, "fx");
754 forward[1] = attrf(a, "fy");
755 forward[2] = attrf(a, "fz");
756 w->setForward(forward);
758 w->setMaxCyclicail(attrf(a, "maxcyclicail", 7.6));
759 w->setMaxCyclicele(attrf(a, "maxcyclicele", 4.94));
760 w->setMinCyclicail(attrf(a, "mincyclicail", -7.6));
761 w->setMinCyclicele(attrf(a, "mincyclicele", -4.94));
762 w->setMaxCollective(attrf(a, "maxcollective", 15.8));
763 w->setMinCollective(attrf(a, "mincollective", -0.2));
764 w->setDiameter(attrf(a, "diameter", 10.2));
765 w->setWeightPerBlade(attrf(a, "weightperblade", 44));
766 w->setNumberOfBlades(attrf(a, "numblades", 4));
767 w->setRelBladeCenter(attrf(a, "relbladecenter", 0.7));
768 w->setDynamic(attrf(a, "dynamic", 0.7));
769 w->setDelta3(attrf(a, "delta3", 0));
770 w->setDelta(attrf(a, "delta", 0));
771 w->setTranslift(attrf(a, "translift", 0.05));
772 w->setC2(attrf(a, "dragfactor", 1));
773 w->setStepspersecond(attrf(a, "stepspersecond", 120));
774 w->setPhiNull((attrf(a, "phi0", 0))*YASIM_PI/180);
775 w->setRPM(attrf(a, "rpm", 424));
776 w->setRelLenHinge(attrf(a, "rellenflaphinge", 0.07));
777 w->setAlpha0((attrf(a, "flap0", -5))*YASIM_PI/180);
778 w->setAlphamin((attrf(a, "flapmin", -15))/180*YASIM_PI);
779 w->setAlphamax((attrf(a, "flapmax", 15))*YASIM_PI/180);
780 w->setAlpha0factor(attrf(a, "flap0factor", 1));
781 w->setTeeterdamp(attrf(a,"teeterdamp",.0001));
782 w->setMaxteeterdamp(attrf(a,"maxteeterdamp",1000));
783 w->setRelLenTeeterHinge(attrf(a,"rellenteeterhinge",0.01));
784 w->setBalance(attrf(a,"balance",1.0));
785 w->setMinTiltYaw(attrf(a,"mintiltyaw",0.0));
786 w->setMinTiltPitch(attrf(a,"mintiltpitch",0.0));
787 w->setMinTiltRoll(attrf(a,"mintiltroll",0.0));
788 w->setMaxTiltYaw(attrf(a,"maxtiltyaw",0.0));
789 w->setMaxTiltPitch(attrf(a,"maxtiltpitch",0.0));
790 w->setMaxTiltRoll(attrf(a,"maxtiltroll",0.0));
791 w->setTiltCenterX(attrf(a,"tiltcenterx",0.0));
792 w->setTiltCenterY(attrf(a,"tiltcentery",0.0));
793 w->setTiltCenterZ(attrf(a,"tiltcenterz",0.0));
794 w->setDownwashFactor(attrf(a, "downwashfactor", 1));
797 if(attrb(a,"sharedflaphinge"))
798 w->setSharedFlapHinge(true);
800 if(a->hasAttribute("name"))
801 w->setName(a->getValue("name") );
802 if(a->hasAttribute("alphaout0"))
803 w->setAlphaoutput(0,a->getValue("alphaout0") );
804 if(a->hasAttribute("alphaout1")) w->setAlphaoutput(1,a->getValue("alphaout1") );
805 if(a->hasAttribute("alphaout2")) w->setAlphaoutput(2,a->getValue("alphaout2") );
806 if(a->hasAttribute("alphaout3")) w->setAlphaoutput(3,a->getValue("alphaout3") );
807 if(a->hasAttribute("coneout")) w->setAlphaoutput(4,a->getValue("coneout") );
808 if(a->hasAttribute("yawout")) w->setAlphaoutput(5,a->getValue("yawout") );
809 if(a->hasAttribute("rollout")) w->setAlphaoutput(6,a->getValue("rollout") );
811 w->setPitchA(attrf(a, "pitch-a", 10));
812 w->setPitchB(attrf(a, "pitch-b", 10));
813 w->setForceAtPitchA(attrf(a, "forceatpitch-a", 3000));
814 w->setPowerAtPitch0(attrf(a, "poweratpitch-0", 300));
815 w->setPowerAtPitchB(attrf(a, "poweratpitch-b", 3000));
816 if(attrb(a,"notorque"))
819 #define p(x) if (a->hasAttribute(#x)) w->setParameter((char *)#x,attrf(a,#x) );
820 #define p2(x,y) if (a->hasAttribute(y)) w->setParameter((char *)#x,attrf(a,y) );
821 p2(translift_ve,"translift-ve")
822 p2(translift_maxfactor,"translift-maxfactor")
823 p2(ground_effect_constant,"ground-effect-constant")
824 p2(vortex_state_lift_factor,"vortex-state-lift-factor")
825 p2(vortex_state_c1,"vortex-state-c1")
826 p2(vortex_state_c2,"vortex-state-c2")
827 p2(vortex_state_c3,"vortex-state_c3")
828 p2(vortex_state_e1,"vortex-state-e1")
829 p2(vortex_state_e2,"vortex-state-e2")
831 p2(number_of_segments,"number-of-segments")
832 p2(number_of_parts,"number-of-parts")
833 p2(rel_len_where_incidence_is_measured,"rel-len-where-incidence-is-measured")
836 p2(airfoil_incidence_no_lift,"airfoil-incidence-no-lift")
837 p2(rel_len_blade_start,"rel-len-blade-start")
838 p2(incidence_stall_zero_speed,"incidence-stall-zero-speed")
839 p2(incidence_stall_half_sonic_speed,"incidence-stall-half-sonic-speed")
840 p2(lift_factor_stall,"lift-factor-stall")
841 p2(stall_change_over,"stall-change-over")
842 p2(drag_factor_stall,"drag-factor-stall")
843 p2(airfoil_lift_coefficient,"airfoil-lift-coefficient")
844 p2(airfoil_drag_coefficient0,"airfoil-drag-coefficient0")
845 p2(airfoil_drag_coefficient1,"airfoil-drag-coefficient1")
846 p2(cyclic_factor,"cyclic-factor")
847 p2(rotor_correction_factor,"rotor-correction-factor")
854 void FGFDM::parsePistonEngine(XMLAttributes* a)
856 float engP = attrf(a, "eng-power") * HP2W;
857 float engS = attrf(a, "eng-rpm") * RPM2RAD;
859 PistonEngine* eng = new PistonEngine(engP, engS);
861 if(a->hasAttribute("displacement"))
862 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
864 if(a->hasAttribute("compression"))
865 eng->setCompression(attrf(a, "compression"));
867 if(a->hasAttribute("min-throttle"))
868 eng->setMinThrottle(attrf(a, "min-throttle"));
870 if(a->hasAttribute("turbo-mul")) {
871 float mul = attrf(a, "turbo-mul");
872 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
873 eng->setTurboParams(mul, mp);
874 eng->setTurboLag(attrf(a, "turbo-lag", 2));
877 if(a->hasAttribute("supercharger"))
878 eng->setSupercharger(attrb(a, "supercharger"));
880 ((PropEngine*)_currObj)->setEngine(eng);
883 void FGFDM::parseTurbineEngine(XMLAttributes* a)
885 float power = attrf(a, "eng-power") * HP2W;
886 float omega = attrf(a, "eng-rpm") * RPM2RAD;
887 float alt = attrf(a, "alt") * FT2M;
888 float flatRating = attrf(a, "flat-rating") * HP2W;
889 TurbineEngine* eng = new TurbineEngine(power, omega, alt, flatRating);
891 if(a->hasAttribute("n2-low-idle"))
892 eng->setN2Range(attrf(a, "n2-low-idle"), attrf(a, "n2-high-idle"),
895 // Nasty units conversion: lbs/hr per hp -> kg/s per watt
896 if(a->hasAttribute("bsfc"))
897 eng->setFuelConsumption(attrf(a, "bsfc") * (LBS2KG/(3600*HP2W)));
899 ((PropEngine*)_currObj)->setEngine(eng);
902 void FGFDM::parsePropeller(XMLAttributes* a)
904 // Legacy Handling for the old engines syntax:
905 PistonEngine* eng = 0;
906 if(a->hasAttribute("eng-power")) {
907 SG_LOG(SG_FLIGHT,SG_ALERT, "WARNING: "
908 << "Legacy engine definition in YASim configuration file. "
910 float engP = attrf(a, "eng-power") * HP2W;
911 float engS = attrf(a, "eng-rpm") * RPM2RAD;
912 eng = new PistonEngine(engP, engS);
913 if(a->hasAttribute("displacement"))
914 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
915 if(a->hasAttribute("compression"))
916 eng->setCompression(attrf(a, "compression"));
917 if(a->hasAttribute("turbo-mul")) {
918 float mul = attrf(a, "turbo-mul");
919 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
920 eng->setTurboParams(mul, mp);
924 // Now parse the actual propeller definition:
926 cg[0] = attrf(a, "x");
927 cg[1] = attrf(a, "y");
928 cg[2] = attrf(a, "z");
929 float mass = attrf(a, "mass") * LBS2KG;
930 float moment = attrf(a, "moment");
931 float radius = attrf(a, "radius");
932 float speed = attrf(a, "cruise-speed") * KTS2MPS;
933 float omega = attrf(a, "cruise-rpm") * RPM2RAD;
934 float power = attrf(a, "cruise-power") * HP2W;
935 float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
937 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
938 PropEngine* thruster = new PropEngine(prop, eng, moment);
939 _airplane.addThruster(thruster, mass, cg);
941 // Set the stops (fine = minimum pitch, coarse = maximum pitch)
942 float fine_stop = attrf(a, "fine-stop", 0.25f);
943 float coarse_stop = attrf(a, "coarse-stop", 4.0f);
944 prop->setStops(fine_stop, coarse_stop);
946 if(a->hasAttribute("takeoff-power")) {
947 float power0 = attrf(a, "takeoff-power") * HP2W;
948 float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
949 prop->setTakeoff(omega0, power0);
952 if(a->hasAttribute("max-rpm")) {
953 float max = attrf(a, "max-rpm") * RPM2RAD;
954 float min = attrf(a, "min-rpm") * RPM2RAD;
955 thruster->setVariableProp(min, max);
958 if(attrb(a, "contra"))
959 thruster->setContraPair(true);
961 if(a->hasAttribute("manual-pitch")) {
962 prop->setManualPitch();
965 thruster->setGearRatio(attrf(a, "gear-ratio", 1));
968 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
969 EngRec* er = new EngRec();
971 er->prefix = dup(buf);
977 // Turns a string axis name into an integer for use by the
978 // ControlMap. Creates a new axis if this one hasn't been defined
980 int FGFDM::parseAxis(const char* name)
982 for(int i=0; i<_axes.size(); i++) {
983 AxisRec* a = (AxisRec*)_axes.get(i);
984 if(eq(a->name, name))
988 // Not there, make a new one.
989 AxisRec* a = new AxisRec();
991 fgGetNode( a->name, true ); // make sure the property name exists
992 a->handle = _airplane.getControlMap()->newInput();
997 int FGFDM::parseOutput(const char* name)
999 if(eq(name, "THROTTLE")) return ControlMap::THROTTLE;
1000 if(eq(name, "MIXTURE")) return ControlMap::MIXTURE;
1001 if(eq(name, "CONDLEVER")) return ControlMap::CONDLEVER;
1002 if(eq(name, "STARTER")) return ControlMap::STARTER;
1003 if(eq(name, "MAGNETOS")) return ControlMap::MAGNETOS;
1004 if(eq(name, "ADVANCE")) return ControlMap::ADVANCE;
1005 if(eq(name, "REHEAT")) return ControlMap::REHEAT;
1006 if(eq(name, "BOOST")) return ControlMap::BOOST;
1007 if(eq(name, "VECTOR")) return ControlMap::VECTOR;
1008 if(eq(name, "PROP")) return ControlMap::PROP;
1009 if(eq(name, "BRAKE")) return ControlMap::BRAKE;
1010 if(eq(name, "STEER")) return ControlMap::STEER;
1011 if(eq(name, "EXTEND")) return ControlMap::EXTEND;
1012 if(eq(name, "HEXTEND")) return ControlMap::HEXTEND;
1013 if(eq(name, "LEXTEND")) return ControlMap::LEXTEND;
1014 if(eq(name, "LACCEL")) return ControlMap::LACCEL;
1015 if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
1016 if(eq(name, "FLAP0")) return ControlMap::FLAP0;
1017 if(eq(name, "FLAP0EFFECTIVENESS")) return ControlMap::FLAP0EFFECTIVENESS;
1018 if(eq(name, "FLAP1")) return ControlMap::FLAP1;
1019 if(eq(name, "FLAP1EFFECTIVENESS")) return ControlMap::FLAP1EFFECTIVENESS;
1020 if(eq(name, "SLAT")) return ControlMap::SLAT;
1021 if(eq(name, "SPOILER")) return ControlMap::SPOILER;
1022 if(eq(name, "CASTERING")) return ControlMap::CASTERING;
1023 if(eq(name, "PROPPITCH")) return ControlMap::PROPPITCH;
1024 if(eq(name, "PROPFEATHER")) return ControlMap::PROPFEATHER;
1025 if(eq(name, "COLLECTIVE")) return ControlMap::COLLECTIVE;
1026 if(eq(name, "CYCLICAIL")) return ControlMap::CYCLICAIL;
1027 if(eq(name, "CYCLICELE")) return ControlMap::CYCLICELE;
1028 if(eq(name, "TILTROLL")) return ControlMap::TILTROLL;
1029 if(eq(name, "TILTPITCH")) return ControlMap::TILTPITCH;
1030 if(eq(name, "TILTYAW")) return ControlMap::TILTYAW;
1031 if(eq(name, "ROTORGEARENGINEON")) return ControlMap::ROTORENGINEON;
1032 if(eq(name, "ROTORBRAKE")) return ControlMap::ROTORBRAKE;
1033 if(eq(name, "ROTORENGINEMAXRELTORQUE"))
1034 return ControlMap::ROTORENGINEMAXRELTORQUE;
1035 if(eq(name, "ROTORRELTARGET")) return ControlMap::ROTORRELTARGET;
1036 if(eq(name, "ROTORBALANCE")) return ControlMap::ROTORBALANCE;
1037 if(eq(name, "REVERSE_THRUST")) return ControlMap::REVERSE_THRUST;
1038 if(eq(name, "WASTEGATE")) return ControlMap::WASTEGATE;
1039 if(eq(name, "WINCHRELSPEED")) return ControlMap::WINCHRELSPEED;
1040 if(eq(name, "HITCHOPEN")) return ControlMap::HITCHOPEN;
1041 if(eq(name, "PLACEWINCH")) return ControlMap::PLACEWINCH;
1042 if(eq(name, "FINDAITOW")) return ControlMap::FINDAITOW;
1044 SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
1045 << name << "' in YASim aircraft description.");
1050 void FGFDM::parseWeight(XMLAttributes* a)
1052 WeightRec* wr = new WeightRec();
1055 v[0] = attrf(a, "x");
1056 v[1] = attrf(a, "y");
1057 v[2] = attrf(a, "z");
1059 wr->prop = dup(a->getValue("mass-prop"));
1060 wr->size = attrf(a, "size", 0);
1061 wr->handle = _airplane.addWeight(v, wr->size);
1066 bool FGFDM::eq(const char* a, const char* b)
1068 // Figure it out for yourself. :)
1069 while(*a && *b && *a == *b) { a++; b++; }
1073 char* FGFDM::dup(const char* s)
1077 char* s2 = new char[len+1];
1079 while((*p++ = *s++));
1084 int FGFDM::attri(XMLAttributes* atts, const char* attr)
1086 if(!atts->hasAttribute(attr)) {
1087 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1088 "' in YASim aircraft description");
1091 return attri(atts, attr, 0);
1094 int FGFDM::attri(XMLAttributes* atts, const char* attr, int def)
1096 const char* val = atts->getValue(attr);
1097 if(val == 0) return def;
1098 else return atol(val);
1101 float FGFDM::attrf(XMLAttributes* atts, const char* attr)
1103 if(!atts->hasAttribute(attr)) {
1104 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1105 "' in YASim aircraft description");
1108 return attrf(atts, attr, 0);
1111 float FGFDM::attrf(XMLAttributes* atts, const char* attr, float def)
1113 const char* val = atts->getValue(attr);
1114 if(val == 0) return def;
1115 else return (float)atof(val);
1118 double FGFDM::attrd(XMLAttributes* atts, const char* attr)
1120 if(!atts->hasAttribute(attr)) {
1121 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1122 "' in YASim aircraft description");
1125 return attrd(atts, attr, 0);
1128 double FGFDM::attrd(XMLAttributes* atts, const char* attr, double def)
1130 const char* val = atts->getValue(attr);
1131 if(val == 0) return def;
1132 else return atof(val);
1135 // ACK: the dreaded ambiguous string boolean. Remind me to shoot Maik
1136 // when I have a chance. :). Unless you have a parser that can check
1137 // symbol constants (we don't), this kind of coding is just a Bad
1138 // Idea. This implementation, for example, silently returns a boolean
1139 // falsehood for values of "1", "yes", "True", and "TRUE". Which is
1140 // especially annoying preexisting boolean attributes in the same
1141 // parser want to see "1" and will choke on a "true"...
1143 // Unfortunately, this usage creeped into existing configuration files
1144 // while I wasn't active, and it's going to be hard to remove. Issue
1145 // a warning to nag people into changing their ways for now...
1146 bool FGFDM::attrb(XMLAttributes* atts, const char* attr)
1148 const char* val = atts->getValue(attr);
1149 if(val == 0) return false;
1151 if(eq(val,"true")) {
1152 SG_LOG(SG_FLIGHT, SG_ALERT, "Warning: " <<
1153 "deprecated 'true' boolean in YASim configuration file. " <<
1154 "Use numeric booleans (attribute=\"1\") instead");
1157 return attri(atts, attr, 0) ? true : false;
1160 }; // namespace yasim