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