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"
19 #include "Rotorblade.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);
88 void FGFDM::iterate(float dt)
91 _airplane.iterate(dt);
93 // Do fuel stuff (FIXME: should stash SGPropertyNode objects here)
95 for(int i=0; i<_airplane.numThrusters(); i++) {
96 Thruster* t = _airplane.getThruster(i);
98 sprintf(buf, "/engines/engine[%d]/out-of-fuel", i);
99 t->setFuelState(!fgGetBool(buf));
101 sprintf(buf, "/engines/engine[%d]/fuel-consumed-lbs", i);
102 double consumed = fgGetDouble(buf) + dt * KG2LBS * t->getFuelFlow();
103 fgSetDouble(buf, consumed);
105 for(int i=0; i<_airplane.numTanks(); i++) {
106 sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
107 _airplane.setFuel(i, LBS2KG * fgGetFloat(buf));
109 _airplane.calcFuelWeights();
111 setOutputProperties(dt);
114 Airplane* FGFDM::getAirplane()
121 // Allows the user to start with something other than full fuel
122 _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
124 // Read out the resulting fuel state
126 for(int i=0; i<_airplane.numTanks(); i++) {
127 sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
128 fgSetDouble(buf, _airplane.getFuel(i) * KG2LBS);
130 double density = _airplane.getFuelDensity(i);
131 sprintf(buf, "/consumables/fuel/tank[%d]/density-ppg", i);
132 fgSetDouble(buf, density * (KG2LBS/CM2GALS));
134 sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
135 fgSetDouble(buf, _airplane.getFuel(i) * CM2GALS / density);
137 sprintf(buf, "/consumables/fuel/tank[%d]/capacity-gal_us", i);
138 fgSetDouble(buf, CM2GALS * _airplane.getTankCapacity(i)/density);
141 // This has a nasty habit of being false at startup. That's not
143 fgSetBool("/controls/gear/gear-down", true);
145 _airplane.getModel()->setTurbulence(_turb);
148 // Not the worlds safest parser. But it's short & sweet.
149 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
151 XMLAttributes* a = (XMLAttributes*)&atts;
155 if(eq(name, "airplane")) {
156 _airplane.setWeight(attrf(a, "mass") * LBS2KG);
157 } else if(eq(name, "approach")) {
158 float spd = attrf(a, "speed") * KTS2MPS;
159 float alt = attrf(a, "alt", 0) * FT2M;
160 float aoa = attrf(a, "aoa", 0) * DEG2RAD;
161 _airplane.setApproach(spd, alt, aoa, attrf(a, "fuel", 0.2));
163 } else if(eq(name, "cruise")) {
164 float spd = attrf(a, "speed") * KTS2MPS;
165 float alt = attrf(a, "alt") * FT2M;
166 _airplane.setCruise(spd, alt, attrf(a, "fuel", 0.5));
168 } else if(eq(name, "solve-weight")) {
169 int idx = attri(a, "idx");
170 float wgt = attrf(a, "weight") * LBS2KG;
171 _airplane.addSolutionWeight(!_cruiseCurr, idx, wgt);
172 } else if(eq(name, "cockpit")) {
173 v[0] = attrf(a, "x");
174 v[1] = attrf(a, "y");
175 v[2] = attrf(a, "z");
176 _airplane.setPilotPos(v);
177 } else if(eq(name, "rotor")) {
178 _airplane.addRotor(parseRotor(a, name));
179 } else if(eq(name, "wing")) {
180 _airplane.setWing(parseWing(a, name));
181 } else if(eq(name, "hstab")) {
182 _airplane.setTail(parseWing(a, name));
183 } else if(eq(name, "vstab") || eq(name, "mstab")) {
184 _airplane.addVStab(parseWing(a, name));
185 } else if(eq(name, "piston-engine")) {
186 parsePistonEngine(a);
187 } else if(eq(name, "turbine-engine")) {
188 parseTurbineEngine(a);
189 } else if(eq(name, "propeller")) {
191 } else if(eq(name, "thruster")) {
192 SimpleJet* j = new SimpleJet();
194 v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
196 _airplane.addThruster(j, 0, v);
197 v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
199 j->setThrust(attrf(a, "thrust") * LBS2N);
200 } else if(eq(name, "jet")) {
203 v[0] = attrf(a, "x");
204 v[1] = attrf(a, "y");
205 v[2] = attrf(a, "z");
206 float mass = attrf(a, "mass") * LBS2KG;
207 j->setMaxThrust(attrf(a, "thrust") * LBS2N,
208 attrf(a, "afterburner", 0) * LBS2N);
209 j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
210 j->setReverseThrust(attrf(a, "reverse", 0.2));
212 float n1min = attrf(a, "n1-idle", 55);
213 float n1max = attrf(a, "n1-max", 102);
214 float n2min = attrf(a, "n2-idle", 73);
215 float n2max = attrf(a, "n2-max", 103);
216 j->setRPMs(n1min, n1max, n2min, n2max);
218 j->setTSFC(attrf(a, "tsfc", 0.8));
219 if(a->hasAttribute("egt")) j->setEGT(attrf(a, "egt"));
220 if(a->hasAttribute("epr")) j->setEPR(attrf(a, "epr"));
221 if(a->hasAttribute("exhaust-speed"))
222 j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
225 _airplane.addThruster(j, mass, v);
226 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
227 EngRec* er = new EngRec();
229 er->prefix = dup(buf);
231 } else if(eq(name, "gear")) {
232 Gear* g = new Gear();
234 v[0] = attrf(a, "x");
235 v[1] = attrf(a, "y");
236 v[2] = attrf(a, "z");
238 float nrm = Math::mag3(v);
239 if (_vehicle_radius < nrm)
240 _vehicle_radius = nrm;
243 v[2] = attrf(a, "compression", 1);
244 g->setCompression(v);
245 g->setBrake(attrf(a, "skid", 0));
246 g->setStaticFriction(attrf(a, "sfric", 0.8));
247 g->setDynamicFriction(attrf(a, "dfric", 0.7));
248 g->setSpring(attrf(a, "spring", 1));
249 g->setDamping(attrf(a, "damp", 1));
250 _airplane.addGear(g);
251 } else if(eq(name, "hook")) {
252 Hook* h = new Hook();
254 v[0] = attrf(a, "x");
255 v[1] = attrf(a, "y");
256 v[2] = attrf(a, "z");
258 float length = attrf(a, "length", 1.0);
259 h->setLength(length);
260 float nrm = length+Math::mag3(v);
261 if (_vehicle_radius < nrm)
262 _vehicle_radius = nrm;
263 h->setDownAngle(attrf(a, "down-angle", 70) * DEG2RAD);
264 h->setUpAngle(attrf(a, "up-angle", 0) * DEG2RAD);
265 _airplane.addHook(h);
266 } else if(eq(name, "launchbar")) {
267 Launchbar* l = new Launchbar();
269 v[0] = attrf(a, "x");
270 v[1] = attrf(a, "y");
271 v[2] = attrf(a, "z");
272 l->setLaunchbarMount(v);
273 v[0] = attrf(a, "holdback-x", v[0]);
274 v[1] = attrf(a, "holdback-y", v[1]);
275 v[2] = attrf(a, "holdback-z", v[2]);
276 l->setHoldbackMount(v);
277 float length = attrf(a, "length", 1.0);
278 l->setLength(length);
279 l->setDownAngle(attrf(a, "down-angle", 30) * DEG2RAD);
280 l->setUpAngle(attrf(a, "up-angle", -30) * DEG2RAD);
281 _airplane.addLaunchbar(l);
282 } else if(eq(name, "fuselage")) {
284 v[0] = attrf(a, "ax");
285 v[1] = attrf(a, "ay");
286 v[2] = attrf(a, "az");
287 b[0] = attrf(a, "bx");
288 b[1] = attrf(a, "by");
289 b[2] = attrf(a, "bz");
290 float taper = attrf(a, "taper", 1);
291 float mid = attrf(a, "midpoint", 0.5);
292 _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid);
293 } else if(eq(name, "tank")) {
294 v[0] = attrf(a, "x");
295 v[1] = attrf(a, "y");
296 v[2] = attrf(a, "z");
297 float density = 6.0; // gasoline, in lbs/gal
298 if(a->hasAttribute("jet")) density = 6.72;
299 density *= LBS2KG*CM2GALS;
300 _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
301 } else if(eq(name, "ballast")) {
302 v[0] = attrf(a, "x");
303 v[1] = attrf(a, "y");
304 v[2] = attrf(a, "z");
305 _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
306 } else if(eq(name, "weight")) {
308 } else if(eq(name, "stall")) {
309 Wing* w = (Wing*)_currObj;
310 w->setStall(attrf(a, "aoa") * DEG2RAD);
311 w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
312 w->setStallPeak(attrf(a, "peak", 1.5));
313 } else if(eq(name, "flap0")) {
314 ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
315 attrf(a, "lift"), attrf(a, "drag"));
316 } else if(eq(name, "flap1")) {
317 ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
318 attrf(a, "lift"), attrf(a, "drag"));
319 } else if(eq(name, "slat")) {
320 ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
321 attrf(a, "aoa"), attrf(a, "drag"));
322 } else if(eq(name, "spoiler")) {
323 ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
324 attrf(a, "lift"), attrf(a, "drag"));
325 /* } else if(eq(name, "collective")) {
326 ((Rotor*)_currObj)->setcollective(attrf(a, "min"), attrf(a, "max"));
327 } else if(eq(name, "cyclic")) {
328 ((Rotor*)_currObj)->setcyclic(attrf(a, "ail"), attrf(a, "ele"));
330 } else if(eq(name, "actionpt")) {
331 v[0] = attrf(a, "x");
332 v[1] = attrf(a, "y");
333 v[2] = attrf(a, "z");
334 ((Thruster*)_currObj)->setPosition(v);
335 } else if(eq(name, "dir")) {
336 v[0] = attrf(a, "x");
337 v[1] = attrf(a, "y");
338 v[2] = attrf(a, "z");
339 ((Thruster*)_currObj)->setDirection(v);
340 } else if(eq(name, "control-setting")) {
341 // A cruise or approach control setting
342 const char* axis = a->getValue("axis");
343 float value = attrf(a, "value", 0);
345 _airplane.addCruiseControl(parseAxis(axis), value);
347 _airplane.addApproachControl(parseAxis(axis), value);
348 } else if(eq(name, "control-input")) {
350 // A mapping of input property to a control
351 int axis = parseAxis(a->getValue("axis"));
352 int control = parseOutput(a->getValue("control"));
354 opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
355 opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
356 opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
358 ControlMap* cm = _airplane.getControlMap();
359 if(a->hasAttribute("src0")) {
360 cm->addMapping(axis, control, _currObj, opt,
361 attrf(a, "src0"), attrf(a, "src1"),
362 attrf(a, "dst0"), attrf(a, "dst1"));
364 cm->addMapping(axis, control, _currObj, opt);
366 } else if(eq(name, "control-output")) {
367 // A property output for a control on the current object
368 ControlMap* cm = _airplane.getControlMap();
369 int type = parseOutput(a->getValue("control"));
370 int handle = cm->getOutputHandle(_currObj, type);
372 PropOut* p = new PropOut();
373 p->prop = fgGetNode(a->getValue("prop"), true);
376 p->left = !(a->hasAttribute("side") &&
377 eq("right", a->getValue("side")));
378 p->min = attrf(a, "min", cm->rangeMin(type));
379 p->max = attrf(a, "max", cm->rangeMax(type));
380 _controlProps.add(p);
382 } else if(eq(name, "control-speed")) {
383 ControlMap* cm = _airplane.getControlMap();
384 int type = parseOutput(a->getValue("control"));
385 int handle = cm->getOutputHandle(_currObj, type);
386 float time = attrf(a, "transition-time", 0);
388 cm->setTransitionTime(handle, time);
390 SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '"
391 << name << "' found in YASim aircraft description");
396 void FGFDM::getExternalInput(float dt)
400 _turb->setMagnitude(fgGetFloat("/environment/turbulence/magnitude-norm"));
401 _turb->update(dt, fgGetFloat("/environment/turbulence/rate-hz"));
404 ControlMap* cm = _airplane.getControlMap();
407 for(i=0; i<_axes.size(); i++) {
408 AxisRec* a = (AxisRec*)_axes.get(i);
409 float val = fgGetFloat(a->name, 0);
410 cm->setInput(a->handle, val);
412 cm->applyControls(dt);
415 for(i=0; i<_weights.size(); i++) {
416 WeightRec* wr = (WeightRec*)_weights.get(i);
417 _airplane.setWeight(wr->handle, LBS2KG * fgGetFloat(wr->prop));
420 for(i=0; i<_thrusters.size(); i++) {
421 EngRec* er = (EngRec*)_thrusters.get(i);
422 Thruster* t = er->eng;
424 if(t->getPropEngine()) {
425 PropEngine* p = t->getPropEngine();
426 sprintf(buf, "%s/rpm", er->prefix);
427 p->setOmega(fgGetFloat(buf, 500) * RPM2RAD);
432 // Linearly "seeks" a property by the specified fraction of the way to
433 // the target value. Used to emulate "slowly changing" output values.
434 static void moveprop(SGPropertyNode* node, const char* prop,
435 float target, float frac)
437 float val = node->getFloatValue(prop);
438 if(frac > 1) frac = 1;
439 if(frac < 0) frac = 0;
440 val += (target - val) * frac;
441 node->setFloatValue(prop, val);
444 void FGFDM::setOutputProperties(float dt)
449 float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
450 fgSetFloat("/yasim/gross-weight-lbs", grossWgt);
452 ControlMap* cm = _airplane.getControlMap();
453 for(i=0; i<_controlProps.size(); i++) {
454 PropOut* p = (PropOut*)_controlProps.get(i);
456 ? cm->getOutput(p->handle)
457 : cm->getOutputR(p->handle));
458 float rmin = cm->rangeMin(p->type);
459 float rmax = cm->rangeMax(p->type);
460 float frac = (val - rmin) / (rmax - rmin);
461 val = frac*(p->max - p->min) + p->min;
462 p->prop->setFloatValue(val);
465 for(i=0; i<_airplane.getNumRotors(); i++) {
466 Rotor*r=(Rotor*)_airplane.getRotor(i);
470 while(j = r->getValueforFGSet(j, b, &f))
471 if(b[0]) fgSetFloat(b,f);
473 for(j=0; j < r->numRotorparts(); j++) {
474 Rotorpart* s = (Rotorpart*)r->getRotorpart(j);
478 b=s->getAlphaoutput(k);
479 if(b[0]) fgSetFloat(b, s->getAlpha(k));
482 for(j=0; j < r->numRotorblades(); j++) {
483 Rotorblade* s = (Rotorblade*)r->getRotorblade(j);
486 for (k=0; k<2; k++) {
487 b = s->getAlphaoutput(k);
488 if(b[0]) fgSetFloat(b, s->getAlpha(k));
493 float fuelDensity = _airplane.getFuelDensity(0); // HACK
494 for(i=0; i<_thrusters.size(); i++) {
495 EngRec* er = (EngRec*)_thrusters.get(i);
496 Thruster* t = er->eng;
497 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
499 // Set: running, cranking, prop-thrust, max-hp, power-pct
500 node->setBoolValue("running", t->isRunning());
501 node->setBoolValue("cranking", t->isCranking());
505 float lbs = Math::mag3(tmp) * (KG2LBS/9.8);
506 node->setFloatValue("prop-thrust", lbs); // Deprecated name
507 node->setFloatValue("thrust-lbs", lbs);
508 node->setFloatValue("fuel-flow-gph",
509 (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
511 if(t->getPropEngine()) {
512 PropEngine* p = t->getPropEngine();
513 node->setFloatValue("rpm", p->getOmega() * (1/RPM2RAD));
514 node->setFloatValue("torque-ftlb",
515 p->getEngine()->getTorque() * NM2FTLB);
517 if(p->getEngine()->isPistonEngine()) {
518 PistonEngine* pe = p->getEngine()->isPistonEngine();
519 node->setFloatValue("mp-osi", pe->getMP() * (1/INHG2PA));
520 node->setFloatValue("mp-inhg", pe->getMP() * (1/INHG2PA));
521 node->setFloatValue("egt-degf",
522 pe->getEGT() * K2DEGF + K2DEGFOFFSET);
523 } else if(p->getEngine()->isTurbineEngine()) {
524 TurbineEngine* te = p->getEngine()->isTurbineEngine();
525 node->setFloatValue("n2", te->getN2());
530 Jet* j = t->getJet();
531 node->setFloatValue("n1", j->getN1());
532 node->setFloatValue("n2", j->getN2());
533 node->setFloatValue("epr", j->getEPR());
534 node->setFloatValue("egr-degf",
535 j->getEGT() * K2DEGF + K2DEGFOFFSET);
537 // These are "unmodeled" values that are still needed for
538 // many cockpits. Tie them all to the N1 speed, but
539 // normalize the numbers to the range [0:1] so the
540 // cockpit code can scale them to the right values.
541 float pnorm = j->getPerfNorm();
542 moveprop(node, "oilp-norm", pnorm, dt/3); // 3s seek time
543 moveprop(node, "oilt-norm", pnorm, dt/30); // 30s
544 moveprop(node, "itt-norm", pnorm, dt/1); // 1s
549 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
551 Wing* w = new Wing();
554 if(eq(type, "vstab"))
560 pos[0] = attrf(a, "x");
561 pos[1] = attrf(a, "y");
562 pos[2] = attrf(a, "z");
565 w->setLength(attrf(a, "length"));
566 w->setChord(attrf(a, "chord"));
567 w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
568 w->setTaper(attrf(a, "taper", 1));
569 w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
570 w->setCamber(attrf(a, "camber", 0));
571 w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
572 w->setTwist(attrf(a, "twist", 0) * DEG2RAD);
574 // The 70% is a magic number that sorta kinda seems to match known
575 // throttle settings to approach speed.
576 w->setInducedDrag(0.7*attrf(a, "idrag", 1));
578 float effect = attrf(a, "effectiveness", 1);
579 w->setDragScale(w->getDragScale()*effect);
585 Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
587 Rotor* w = new Rotor();
589 // float defDihed = 0;
592 pos[0] = attrf(a, "x");
593 pos[1] = attrf(a, "y");
594 pos[2] = attrf(a, "z");
598 normal[0] = attrf(a, "nx");
599 normal[1] = attrf(a, "ny");
600 normal[2] = attrf(a, "nz");
601 w->setNormal(normal);
604 forward[0] = attrf(a, "fx");
605 forward[1] = attrf(a, "fy");
606 forward[2] = attrf(a, "fz");
607 w->setForward(forward);
609 w->setMaxCyclicail(attrf(a, "maxcyclicail", 7.6));
610 w->setMaxCyclicele(attrf(a, "maxcyclicele", 4.94));
611 w->setMinCyclicail(attrf(a, "mincyclicail", -7.6));
612 w->setMinCyclicele(attrf(a, "mincyclicele", -4.94));
613 w->setMaxCollective(attrf(a, "maxcollective", 15.8));
614 w->setMinCollective(attrf(a, "mincollective", -0.2));
615 w->setDiameter(attrf(a, "diameter", 10.2));
616 w->setWeightPerBlade(attrf(a, "weightperblade", 44));
617 w->setNumberOfBlades(attrf(a, "numblades", 4));
618 w->setRelBladeCenter(attrf(a, "relbladecenter", 0.7));
619 w->setDynamic(attrf(a, "dynamic", 0.7));
620 w->setDelta3(attrf(a, "delta3", 0));
621 w->setDelta(attrf(a, "delta", 0));
622 w->setTranslift(attrf(a, "translift", 0.05));
623 w->setC2(attrf(a, "dragfactor", 1));
624 w->setStepspersecond(attrf(a, "stepspersecond", 120));
625 w->setRPM(attrf(a, "rpm", 424));
626 w->setRelLenHinge(attrf(a, "rellenflaphinge", 0.07));
627 w->setAlpha0((attrf(a, "flap0", -5))*YASIM_PI/180);
628 w->setAlphamin((attrf(a, "flapmin", -15))/180*YASIM_PI);
629 w->setAlphamax((attrf(a, "flapmax", 15))*YASIM_PI/180);
630 w->setAlpha0factor(attrf(a, "flap0factor", 1));
631 w->setTeeterdamp(attrf(a,"teeterdamp",.0001));
632 w->setMaxteeterdamp(attrf(a,"maxteeterdamp",1000));
633 w->setRelLenTeeterHinge(attrf(a,"rellenteeterhinge",0.01));
634 void setAlphamin(float f);
635 void setAlphamax(float f);
636 void setAlpha0factor(float f);
641 if(a->hasAttribute("name"))
642 w->setName(a->getValue("name") );
643 if(a->hasAttribute("alphaout0"))
644 w->setAlphaoutput(0,a->getValue("alphaout0") );
645 if(a->hasAttribute("alphaout1")) w->setAlphaoutput(1,a->getValue("alphaout1") );
646 if(a->hasAttribute("alphaout2")) w->setAlphaoutput(2,a->getValue("alphaout2") );
647 if(a->hasAttribute("alphaout3")) w->setAlphaoutput(3,a->getValue("alphaout3") );
648 if(a->hasAttribute("coneout")) w->setAlphaoutput(4,a->getValue("coneout") );
649 if(a->hasAttribute("yawout")) w->setAlphaoutput(5,a->getValue("yawout") );
650 if(a->hasAttribute("rollout")) w->setAlphaoutput(6,a->getValue("rollout") );
652 w->setPitchA(attrf(a, "pitch_a", 10));
653 w->setPitchB(attrf(a, "pitch_b", 10));
654 w->setForceAtPitchA(attrf(a, "forceatpitch_a", 3000));
655 w->setPowerAtPitch0(attrf(a, "poweratpitch_0", 300));
656 w->setPowerAtPitchB(attrf(a, "poweratpitch_b", 3000));
657 if(attrb(a,"notorque"))
659 if(attrb(a,"simblades"))
666 void FGFDM::parsePistonEngine(XMLAttributes* a)
668 float engP = attrf(a, "eng-power") * HP2W;
669 float engS = attrf(a, "eng-rpm") * RPM2RAD;
671 PistonEngine* eng = new PistonEngine(engP, engS);
673 if(a->hasAttribute("displacement"))
674 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
676 if(a->hasAttribute("compression"))
677 eng->setCompression(attrf(a, "compression"));
679 if(a->hasAttribute("turbo-mul")) {
680 float mul = attrf(a, "turbo-mul");
681 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
682 eng->setTurboParams(mul, mp);
685 ((PropEngine*)_currObj)->setEngine(eng);
688 void FGFDM::parseTurbineEngine(XMLAttributes* a)
690 float power = attrf(a, "eng-power") * HP2W;
691 float omega = attrf(a, "eng-rpm") * RPM2RAD;
692 float alt = attrf(a, "alt") * FT2M;
693 float flatRating = attrf(a, "flat-rating") * HP2W;
694 TurbineEngine* eng = new TurbineEngine(power, omega, alt, flatRating);
696 if(a->hasAttribute("n2-low-idle"))
697 eng->setN2Range(attrf(a, "n2-low-idle"), attrf(a, "n2-high-idle"),
700 // Nasty units conversion: lbs/hr per hp -> kg/s per watt
701 if(a->hasAttribute("bsfc"))
702 eng->setFuelConsumption(attrf(a, "bsfc") * (LBS2KG/(3600*HP2W)));
704 ((PropEngine*)_currObj)->setEngine(eng);
707 void FGFDM::parsePropeller(XMLAttributes* a)
709 // Legacy Handling for the old engines syntax:
710 PistonEngine* eng = 0;
711 if(a->hasAttribute("eng-power")) {
712 SG_LOG(SG_FLIGHT,SG_ALERT, "WARNING: "
713 << "Legacy engine definition in YASim configuration file. "
715 float engP = attrf(a, "eng-power") * HP2W;
716 float engS = attrf(a, "eng-rpm") * RPM2RAD;
717 eng = new PistonEngine(engP, engS);
718 if(a->hasAttribute("displacement"))
719 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
720 if(a->hasAttribute("compression"))
721 eng->setCompression(attrf(a, "compression"));
722 if(a->hasAttribute("turbo-mul")) {
723 float mul = attrf(a, "turbo-mul");
724 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
725 eng->setTurboParams(mul, mp);
729 // Now parse the actual propeller definition:
731 cg[0] = attrf(a, "x");
732 cg[1] = attrf(a, "y");
733 cg[2] = attrf(a, "z");
734 float mass = attrf(a, "mass") * LBS2KG;
735 float moment = attrf(a, "moment");
736 float radius = attrf(a, "radius");
737 float speed = attrf(a, "cruise-speed") * KTS2MPS;
738 float omega = attrf(a, "cruise-rpm") * RPM2RAD;
739 float power = attrf(a, "cruise-power") * HP2W;
740 float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
742 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
743 PropEngine* thruster = new PropEngine(prop, eng, moment);
744 _airplane.addThruster(thruster, mass, cg);
746 if(a->hasAttribute("takeoff-power")) {
747 float power0 = attrf(a, "takeoff-power") * HP2W;
748 float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
749 prop->setTakeoff(omega0, power0);
752 if(a->hasAttribute("max-rpm")) {
753 float max = attrf(a, "max-rpm") * RPM2RAD;
754 float min = attrf(a, "min-rpm") * RPM2RAD;
755 thruster->setVariableProp(min, max);
758 if(attrb(a, "contra"))
759 thruster->setContraPair(true);
761 if(a->hasAttribute("manual-pitch")) {
762 prop->setManualPitch();
765 thruster->setGearRatio(attrf(a, "gear-ratio", 1));
768 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
769 EngRec* er = new EngRec();
771 er->prefix = dup(buf);
777 // Turns a string axis name into an integer for use by the
778 // ControlMap. Creates a new axis if this one hasn't been defined
780 int FGFDM::parseAxis(const char* name)
783 for(i=0; i<_axes.size(); i++) {
784 AxisRec* a = (AxisRec*)_axes.get(i);
785 if(eq(a->name, name))
789 // Not there, make a new one.
790 AxisRec* a = new AxisRec();
792 fgGetNode( a->name, true ); // make sure the property name exists
793 a->handle = _airplane.getControlMap()->newInput();
798 int FGFDM::parseOutput(const char* name)
800 if(eq(name, "THROTTLE")) return ControlMap::THROTTLE;
801 if(eq(name, "MIXTURE")) return ControlMap::MIXTURE;
802 if(eq(name, "CONDLEVER")) return ControlMap::CONDLEVER;
803 if(eq(name, "STARTER")) return ControlMap::STARTER;
804 if(eq(name, "MAGNETOS")) return ControlMap::MAGNETOS;
805 if(eq(name, "ADVANCE")) return ControlMap::ADVANCE;
806 if(eq(name, "REHEAT")) return ControlMap::REHEAT;
807 if(eq(name, "BOOST")) return ControlMap::BOOST;
808 if(eq(name, "VECTOR")) return ControlMap::VECTOR;
809 if(eq(name, "PROP")) return ControlMap::PROP;
810 if(eq(name, "BRAKE")) return ControlMap::BRAKE;
811 if(eq(name, "STEER")) return ControlMap::STEER;
812 if(eq(name, "EXTEND")) return ControlMap::EXTEND;
813 if(eq(name, "HEXTEND")) return ControlMap::HEXTEND;
814 if(eq(name, "LEXTEND")) return ControlMap::LEXTEND;
815 if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
816 if(eq(name, "FLAP0")) return ControlMap::FLAP0;
817 if(eq(name, "FLAP1")) return ControlMap::FLAP1;
818 if(eq(name, "SLAT")) return ControlMap::SLAT;
819 if(eq(name, "SPOILER")) return ControlMap::SPOILER;
820 if(eq(name, "CASTERING")) return ControlMap::CASTERING;
821 if(eq(name, "PROPPITCH")) return ControlMap::PROPPITCH;
822 if(eq(name, "PROPFEATHER")) return ControlMap::PROPFEATHER;
823 if(eq(name, "COLLECTIVE")) return ControlMap::COLLECTIVE;
824 if(eq(name, "CYCLICAIL")) return ControlMap::CYCLICAIL;
825 if(eq(name, "CYCLICELE")) return ControlMap::CYCLICELE;
826 if(eq(name, "ROTORENGINEON")) return ControlMap::ROTORENGINEON;
827 if(eq(name, "REVERSE_THRUST")) return ControlMap::REVERSE_THRUST;
828 SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
829 << name << "' in YASim aircraft description.");
834 void FGFDM::parseWeight(XMLAttributes* a)
836 WeightRec* wr = new WeightRec();
839 v[0] = attrf(a, "x");
840 v[1] = attrf(a, "y");
841 v[2] = attrf(a, "z");
843 wr->prop = dup(a->getValue("mass-prop"));
844 wr->size = attrf(a, "size", 0);
845 wr->handle = _airplane.addWeight(v, wr->size);
850 bool FGFDM::eq(const char* a, const char* b)
852 // Figure it out for yourself. :)
853 while(*a && *b && *a == *b) { a++; b++; }
857 char* FGFDM::dup(const char* s)
861 char* s2 = new char[len+1];
863 while((*p++ = *s++));
868 int FGFDM::attri(XMLAttributes* atts, char* attr)
870 if(!atts->hasAttribute(attr)) {
871 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
872 "' in YASim aircraft description");
875 return attri(atts, attr, 0);
878 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
880 const char* val = atts->getValue(attr);
881 if(val == 0) return def;
882 else return atol(val);
885 float FGFDM::attrf(XMLAttributes* atts, char* attr)
887 if(!atts->hasAttribute(attr)) {
888 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
889 "' in YASim aircraft description");
892 return attrf(atts, attr, 0);
895 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
897 const char* val = atts->getValue(attr);
898 if(val == 0) return def;
899 else return (float)atof(val);
902 // ACK: the dreaded ambiguous string boolean. Remind me to shoot Maik
903 // when I have a chance. :). Unless you have a parser that can check
904 // symbol constants (we don't), this kind of coding is just a Bad
905 // Idea. This implementation, for example, silently returns a boolean
906 // falsehood for values of "1", "yes", "True", and "TRUE". Which is
907 // especially annoying preexisting boolean attributes in the same
908 // parser want to see "1" and will choke on a "true"...
910 // Unfortunately, this usage creeped into existing configuration files
911 // while I wasn't active, and it's going to be hard to remove. Issue
912 // a warning to nag people into changing their ways for now...
913 bool FGFDM::attrb(XMLAttributes* atts, char* attr)
915 const char* val = atts->getValue(attr);
916 if(val == 0) return false;
919 SG_LOG(SG_FLIGHT, SG_ALERT, "Warning: " <<
920 "deprecated 'true' boolean in YASim configuration file. " <<
921 "Use numeric booleans (attribute=\"1\") instead");
924 return attri(atts, attr, 0) ? true : false;
927 }; // namespace yasim