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"
24 // Some conversion factors
25 static const float KTS2MPS = 0.514444444444;
26 static const float FT2M = 0.3048;
27 static const float DEG2RAD = 0.0174532925199;
28 static const float RPM2RAD = 0.10471975512;
29 static const float LBS2N = 4.44822;
30 static const float LBS2KG = 0.45359237;
31 static const float KG2LBS = 2.2046225;
32 static const float CM2GALS = 264.172037284;
33 static const float HP2W = 745.700;
34 static const float INHG2PA = 3386.389;
35 static const float K2DEGF = 1.8;
36 static const float K2DEGFOFFSET = -459.4;
37 static const float CIN2CM = 1.6387064e-5;
38 static const float YASIM_PI = 3.14159265358979323846;
40 static const float NM2FTLB = (1/(LBS2N*FT2M));
42 // Stubs, so that this can be compiled without the FlightGear
43 // binary. What's the best way to handle this?
45 // float fgGetFloat(char* name, float def) { return 0; }
46 // void fgSetFloat(char* name, float val) {}
50 _vehicle_radius = 0.0f;
54 // Map /controls/flight/elevator to the approach elevator control. This
55 // should probably be settable, but there are very few aircraft
56 // who trim their approaches using things other than elevator.
57 _airplane.setElevatorControl(parseAxis("/controls/flight/elevator-trim"));
59 // FIXME: read seed from somewhere?
61 _turb = new Turbulence(10, seed);
67 for(i=0; i<_axes.size(); i++) {
68 AxisRec* a = (AxisRec*)_axes.get(i);
72 for(i=0; i<_thrusters.size(); i++) {
73 EngRec* er = (EngRec*)_thrusters.get(i);
78 for(i=0; i<_weights.size(); i++) {
79 WeightRec* wr = (WeightRec*)_weights.get(i);
83 for(i=0; i<_controlProps.size(); i++)
84 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.getModel()->getRotorgear()->addRotor(parseRotor(a, name));
179 } else if(eq(name, "rotorgear")) {
180 Rotorgear* r = _airplane.getModel()->getRotorgear();
182 #define p(x) if (a->hasAttribute(#x)) r->setParameter((char *)#x,attrf(a,#x) );
183 #define p2(x,y) if (a->hasAttribute(#x)) {r->setParameter((char *)#y,attrf(a,#x) ); SG_LOG(SG_INPUT, SG_ALERT,"Warning: Aircraft defnition files uses outdated tag '" <<#x<<"', use '"<<#y<<"' instead (see README.YASim)" <<endl);}
185 p(engine_prop_factor)
188 p(max_power_rotor_brake)
189 p(rotorgear_friction)
190 p(engine_accel_limit)
191 p2(engine_accell_limit,engine_accel_limit)
195 } else if(eq(name, "wing")) {
196 _airplane.setWing(parseWing(a, name));
197 } else if(eq(name, "hstab")) {
198 _airplane.setTail(parseWing(a, name));
199 } else if(eq(name, "vstab") || eq(name, "mstab")) {
200 _airplane.addVStab(parseWing(a, name));
201 } else if(eq(name, "piston-engine")) {
202 parsePistonEngine(a);
203 } else if(eq(name, "turbine-engine")) {
204 parseTurbineEngine(a);
205 } else if(eq(name, "propeller")) {
207 } else if(eq(name, "thruster")) {
208 SimpleJet* j = new SimpleJet();
210 v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
212 _airplane.addThruster(j, 0, v);
213 v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
215 j->setThrust(attrf(a, "thrust") * LBS2N);
216 } else if(eq(name, "jet")) {
219 v[0] = attrf(a, "x");
220 v[1] = attrf(a, "y");
221 v[2] = attrf(a, "z");
222 float mass = attrf(a, "mass") * LBS2KG;
223 j->setMaxThrust(attrf(a, "thrust") * LBS2N,
224 attrf(a, "afterburner", 0) * LBS2N);
225 j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
226 j->setReverseThrust(attrf(a, "reverse", 0.2));
228 float n1min = attrf(a, "n1-idle", 55);
229 float n1max = attrf(a, "n1-max", 102);
230 float n2min = attrf(a, "n2-idle", 73);
231 float n2max = attrf(a, "n2-max", 103);
232 j->setRPMs(n1min, n1max, n2min, n2max);
234 j->setTSFC(attrf(a, "tsfc", 0.8));
235 if(a->hasAttribute("egt")) j->setEGT(attrf(a, "egt"));
236 if(a->hasAttribute("epr")) j->setEPR(attrf(a, "epr"));
237 if(a->hasAttribute("exhaust-speed"))
238 j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
239 if(a->hasAttribute("spool-time"))
240 j->setSpooling(attrf(a, "spool-time"));
243 _airplane.addThruster(j, mass, v);
244 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
245 EngRec* er = new EngRec();
247 er->prefix = dup(buf);
249 } else if(eq(name, "gear")) {
250 Gear* g = new Gear();
252 v[0] = attrf(a, "x");
253 v[1] = attrf(a, "y");
254 v[2] = attrf(a, "z");
256 float nrm = Math::mag3(v);
257 if (_vehicle_radius < nrm)
258 _vehicle_radius = nrm;
259 if(a->hasAttribute("upx")) {
260 v[0] = attrf(a, "upx");
261 v[1] = attrf(a, "upy");
262 v[2] = attrf(a, "upz");
269 for(int i=0; i<3; i++)
270 v[i] *= attrf(a, "compression", 1);
271 g->setCompression(v);
272 g->setBrake(attrf(a, "skid", 0));
273 g->setStaticFriction(attrf(a, "sfric", 0.8));
274 g->setDynamicFriction(attrf(a, "dfric", 0.7));
275 g->setSpring(attrf(a, "spring", 1));
276 g->setDamping(attrf(a, "damp", 1));
277 _airplane.addGear(g);
278 } else if(eq(name, "hook")) {
279 Hook* h = new Hook();
281 v[0] = attrf(a, "x");
282 v[1] = attrf(a, "y");
283 v[2] = attrf(a, "z");
285 float length = attrf(a, "length", 1.0);
286 h->setLength(length);
287 float nrm = length+Math::mag3(v);
288 if (_vehicle_radius < nrm)
289 _vehicle_radius = nrm;
290 h->setDownAngle(attrf(a, "down-angle", 70) * DEG2RAD);
291 h->setUpAngle(attrf(a, "up-angle", 0) * DEG2RAD);
292 _airplane.addHook(h);
293 } else if(eq(name, "launchbar")) {
294 Launchbar* l = new Launchbar();
296 v[0] = attrf(a, "x");
297 v[1] = attrf(a, "y");
298 v[2] = attrf(a, "z");
299 l->setLaunchbarMount(v);
300 v[0] = attrf(a, "holdback-x", v[0]);
301 v[1] = attrf(a, "holdback-y", v[1]);
302 v[2] = attrf(a, "holdback-z", v[2]);
303 l->setHoldbackMount(v);
304 float length = attrf(a, "length", 1.0);
305 l->setLength(length);
306 l->setDownAngle(attrf(a, "down-angle", 45) * DEG2RAD);
307 l->setUpAngle(attrf(a, "up-angle", -45) * DEG2RAD);
308 l->setHoldbackLength(attrf(a, "holdback-length", 2.0));
309 _airplane.addLaunchbar(l);
310 } else if(eq(name, "fuselage")) {
312 v[0] = attrf(a, "ax");
313 v[1] = attrf(a, "ay");
314 v[2] = attrf(a, "az");
315 b[0] = attrf(a, "bx");
316 b[1] = attrf(a, "by");
317 b[2] = attrf(a, "bz");
318 float taper = attrf(a, "taper", 1);
319 float mid = attrf(a, "midpoint", 0.5);
320 _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid);
321 } else if(eq(name, "tank")) {
322 v[0] = attrf(a, "x");
323 v[1] = attrf(a, "y");
324 v[2] = attrf(a, "z");
325 float density = 6.0; // gasoline, in lbs/gal
326 if(a->hasAttribute("jet")) density = 6.72;
327 density *= LBS2KG*CM2GALS;
328 _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
329 } else if(eq(name, "ballast")) {
330 v[0] = attrf(a, "x");
331 v[1] = attrf(a, "y");
332 v[2] = attrf(a, "z");
333 _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
334 } else if(eq(name, "weight")) {
336 } else if(eq(name, "stall")) {
337 Wing* w = (Wing*)_currObj;
338 w->setStall(attrf(a, "aoa") * DEG2RAD);
339 w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
340 w->setStallPeak(attrf(a, "peak", 1.5));
341 } else if(eq(name, "flap0")) {
342 ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
343 attrf(a, "lift"), attrf(a, "drag"));
344 } else if(eq(name, "flap1")) {
345 ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
346 attrf(a, "lift"), attrf(a, "drag"));
347 } else if(eq(name, "slat")) {
348 ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
349 attrf(a, "aoa"), attrf(a, "drag"));
350 } else if(eq(name, "spoiler")) {
351 ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
352 attrf(a, "lift"), attrf(a, "drag"));
353 /* } else if(eq(name, "collective")) {
354 ((Rotor*)_currObj)->setcollective(attrf(a, "min"), attrf(a, "max"));
355 } else if(eq(name, "cyclic")) {
356 ((Rotor*)_currObj)->setcyclic(attrf(a, "ail"), attrf(a, "ele"));
358 } else if(eq(name, "actionpt")) {
359 v[0] = attrf(a, "x");
360 v[1] = attrf(a, "y");
361 v[2] = attrf(a, "z");
362 ((Thruster*)_currObj)->setPosition(v);
363 } else if(eq(name, "dir")) {
364 v[0] = attrf(a, "x");
365 v[1] = attrf(a, "y");
366 v[2] = attrf(a, "z");
367 ((Thruster*)_currObj)->setDirection(v);
368 } else if(eq(name, "control-setting")) {
369 // A cruise or approach control setting
370 const char* axis = a->getValue("axis");
371 float value = attrf(a, "value", 0);
373 _airplane.addCruiseControl(parseAxis(axis), value);
375 _airplane.addApproachControl(parseAxis(axis), value);
376 } else if(eq(name, "control-input")) {
378 // A mapping of input property to a control
379 int axis = parseAxis(a->getValue("axis"));
380 int control = parseOutput(a->getValue("control"));
382 opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
383 opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
384 opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
386 ControlMap* cm = _airplane.getControlMap();
387 if(a->hasAttribute("src0")) {
388 cm->addMapping(axis, control, _currObj, opt,
389 attrf(a, "src0"), attrf(a, "src1"),
390 attrf(a, "dst0"), attrf(a, "dst1"));
392 cm->addMapping(axis, control, _currObj, opt);
394 } else if(eq(name, "control-output")) {
395 // A property output for a control on the current object
396 ControlMap* cm = _airplane.getControlMap();
397 int type = parseOutput(a->getValue("control"));
398 int handle = cm->getOutputHandle(_currObj, type);
400 PropOut* p = new PropOut();
401 p->prop = fgGetNode(a->getValue("prop"), true);
404 p->left = !(a->hasAttribute("side") &&
405 eq("right", a->getValue("side")));
406 p->min = attrf(a, "min", cm->rangeMin(type));
407 p->max = attrf(a, "max", cm->rangeMax(type));
408 _controlProps.add(p);
410 } else if(eq(name, "control-speed")) {
411 ControlMap* cm = _airplane.getControlMap();
412 int type = parseOutput(a->getValue("control"));
413 int handle = cm->getOutputHandle(_currObj, type);
414 float time = attrf(a, "transition-time", 0);
416 cm->setTransitionTime(handle, time);
418 SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '"
419 << name << "' found in YASim aircraft description");
424 void FGFDM::getExternalInput(float dt)
428 _turb->setMagnitude(fgGetFloat("/environment/turbulence/magnitude-norm"));
429 _turb->update(dt, fgGetFloat("/environment/turbulence/rate-hz"));
432 ControlMap* cm = _airplane.getControlMap();
435 for(i=0; i<_axes.size(); i++) {
436 AxisRec* a = (AxisRec*)_axes.get(i);
437 float val = fgGetFloat(a->name, 0);
438 cm->setInput(a->handle, val);
440 cm->applyControls(dt);
443 for(i=0; i<_weights.size(); i++) {
444 WeightRec* wr = (WeightRec*)_weights.get(i);
445 _airplane.setWeight(wr->handle, LBS2KG * fgGetFloat(wr->prop));
448 for(i=0; i<_thrusters.size(); i++) {
449 EngRec* er = (EngRec*)_thrusters.get(i);
450 Thruster* t = er->eng;
452 if(t->getPropEngine()) {
453 PropEngine* p = t->getPropEngine();
454 sprintf(buf, "%s/rpm", er->prefix);
455 p->setOmega(fgGetFloat(buf, 500) * RPM2RAD);
460 // Linearly "seeks" a property by the specified fraction of the way to
461 // the target value. Used to emulate "slowly changing" output values.
462 static void moveprop(SGPropertyNode* node, const char* prop,
463 float target, float frac)
465 float val = node->getFloatValue(prop);
466 if(frac > 1) frac = 1;
467 if(frac < 0) frac = 0;
468 val += (target - val) * frac;
469 node->setFloatValue(prop, val);
472 void FGFDM::setOutputProperties(float dt)
477 float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
478 fgSetFloat("/yasim/gross-weight-lbs", grossWgt);
480 ControlMap* cm = _airplane.getControlMap();
481 for(i=0; i<_controlProps.size(); i++) {
482 PropOut* p = (PropOut*)_controlProps.get(i);
484 ? cm->getOutput(p->handle)
485 : cm->getOutputR(p->handle));
486 float rmin = cm->rangeMin(p->type);
487 float rmax = cm->rangeMax(p->type);
488 float frac = (val - rmin) / (rmax - rmin);
489 val = frac*(p->max - p->min) + p->min;
490 p->prop->setFloatValue(val);
493 for(i=0; i<_airplane.getRotorgear()->getNumRotors(); i++) {
494 Rotor*r=(Rotor*)_airplane.getRotorgear()->getRotor(i);
498 while((j = r->getValueforFGSet(j, b, &f)))
499 if(b[0]) fgSetFloat(b,f);
501 while((j = _airplane.getRotorgear()->getValueforFGSet(j, b, &f)))
502 if(b[0]) fgSetFloat(b,f);
503 for(j=0; j < r->numRotorparts(); j+=r->numRotorparts()>>2) {
504 Rotorpart* s = (Rotorpart*)r->getRotorpart(j);
508 b=s->getAlphaoutput(k);
509 if(b[0]) fgSetFloat(b, s->getAlpha(k));
514 float fuelDensity = _airplane.getFuelDensity(0); // HACK
515 for(i=0; i<_thrusters.size(); i++) {
516 EngRec* er = (EngRec*)_thrusters.get(i);
517 Thruster* t = er->eng;
518 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
520 // Set: running, cranking, prop-thrust, max-hp, power-pct
521 node->setBoolValue("running", t->isRunning());
522 node->setBoolValue("cranking", t->isCranking());
526 float lbs = Math::mag3(tmp) * (KG2LBS/9.8);
527 node->setFloatValue("prop-thrust", lbs); // Deprecated name
528 node->setFloatValue("thrust-lbs", lbs);
529 node->setFloatValue("fuel-flow-gph",
530 (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
532 if(t->getPropEngine()) {
533 PropEngine* p = t->getPropEngine();
534 node->setFloatValue("rpm", p->getOmega() * (1/RPM2RAD));
535 node->setFloatValue("torque-ftlb",
536 p->getEngine()->getTorque() * NM2FTLB);
538 if(p->getEngine()->isPistonEngine()) {
539 PistonEngine* pe = p->getEngine()->isPistonEngine();
540 node->setFloatValue("mp-osi", pe->getMP() * (1/INHG2PA));
541 node->setFloatValue("mp-inhg", pe->getMP() * (1/INHG2PA));
542 node->setFloatValue("egt-degf",
543 pe->getEGT() * K2DEGF + K2DEGFOFFSET);
544 node->setFloatValue("oil-temperature-degf",
545 pe->getOilTemp() * K2DEGF + K2DEGFOFFSET);
546 node->setFloatValue("boost-gauge-inhg",
547 pe->getBoost() * (1/INHG2PA));
548 } else if(p->getEngine()->isTurbineEngine()) {
549 TurbineEngine* te = p->getEngine()->isTurbineEngine();
550 node->setFloatValue("n2", te->getN2());
555 Jet* j = t->getJet();
556 node->setFloatValue("n1", j->getN1());
557 node->setFloatValue("n2", j->getN2());
558 node->setFloatValue("epr", j->getEPR());
559 node->setFloatValue("egt-degf",
560 j->getEGT() * K2DEGF + K2DEGFOFFSET);
562 // These are "unmodeled" values that are still needed for
563 // many cockpits. Tie them all to the N1 speed, but
564 // normalize the numbers to the range [0:1] so the
565 // cockpit code can scale them to the right values.
566 float pnorm = j->getPerfNorm();
567 moveprop(node, "oilp-norm", pnorm, dt/3); // 3s seek time
568 moveprop(node, "oilt-norm", pnorm, dt/30); // 30s
569 moveprop(node, "itt-norm", pnorm, dt/1); // 1s
574 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
576 Wing* w = new Wing();
579 if(eq(type, "vstab"))
585 pos[0] = attrf(a, "x");
586 pos[1] = attrf(a, "y");
587 pos[2] = attrf(a, "z");
590 w->setLength(attrf(a, "length"));
591 w->setChord(attrf(a, "chord"));
592 w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
593 w->setTaper(attrf(a, "taper", 1));
594 w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
595 w->setCamber(attrf(a, "camber", 0));
597 // These come in with positive indicating positive AoA, but the
598 // internals expect a rotation about the left-pointing Y axis, so
600 w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD * -1);
601 w->setTwist(attrf(a, "twist", 0) * DEG2RAD * -1);
603 // The 70% is a magic number that sorta kinda seems to match known
604 // throttle settings to approach speed.
605 w->setInducedDrag(0.7*attrf(a, "idrag", 1));
607 float effect = attrf(a, "effectiveness", 1);
608 w->setDragScale(w->getDragScale()*effect);
614 Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
616 Rotor* w = new Rotor();
618 // float defDihed = 0;
621 pos[0] = attrf(a, "x");
622 pos[1] = attrf(a, "y");
623 pos[2] = attrf(a, "z");
627 normal[0] = attrf(a, "nx");
628 normal[1] = attrf(a, "ny");
629 normal[2] = attrf(a, "nz");
630 w->setNormal(normal);
633 forward[0] = attrf(a, "fx");
634 forward[1] = attrf(a, "fy");
635 forward[2] = attrf(a, "fz");
636 w->setForward(forward);
638 w->setMaxCyclicail(attrf(a, "maxcyclicail", 7.6));
639 w->setMaxCyclicele(attrf(a, "maxcyclicele", 4.94));
640 w->setMinCyclicail(attrf(a, "mincyclicail", -7.6));
641 w->setMinCyclicele(attrf(a, "mincyclicele", -4.94));
642 w->setMaxCollective(attrf(a, "maxcollective", 15.8));
643 w->setMinCollective(attrf(a, "mincollective", -0.2));
644 w->setDiameter(attrf(a, "diameter", 10.2));
645 w->setWeightPerBlade(attrf(a, "weightperblade", 44));
646 w->setNumberOfBlades(attrf(a, "numblades", 4));
647 w->setRelBladeCenter(attrf(a, "relbladecenter", 0.7));
648 w->setDynamic(attrf(a, "dynamic", 0.7));
649 w->setDelta3(attrf(a, "delta3", 0));
650 w->setDelta(attrf(a, "delta", 0));
651 w->setTranslift(attrf(a, "translift", 0.05));
652 w->setC2(attrf(a, "dragfactor", 1));
653 w->setStepspersecond(attrf(a, "stepspersecond", 120));
654 w->setRPM(attrf(a, "rpm", 424));
655 w->setRelLenHinge(attrf(a, "rellenflaphinge", 0.07));
656 w->setAlpha0((attrf(a, "flap0", -5))*YASIM_PI/180);
657 w->setAlphamin((attrf(a, "flapmin", -15))/180*YASIM_PI);
658 w->setAlphamax((attrf(a, "flapmax", 15))*YASIM_PI/180);
659 w->setAlpha0factor(attrf(a, "flap0factor", 1));
660 w->setTeeterdamp(attrf(a,"teeterdamp",.0001));
661 w->setMaxteeterdamp(attrf(a,"maxteeterdamp",1000));
662 w->setRelLenTeeterHinge(attrf(a,"rellenteeterhinge",0.01));
663 void setAlphamin(float f);
664 void setAlphamax(float f);
665 void setAlpha0factor(float f);
670 if(a->hasAttribute("name"))
671 w->setName(a->getValue("name") );
672 if(a->hasAttribute("alphaout0"))
673 w->setAlphaoutput(0,a->getValue("alphaout0") );
674 if(a->hasAttribute("alphaout1")) w->setAlphaoutput(1,a->getValue("alphaout1") );
675 if(a->hasAttribute("alphaout2")) w->setAlphaoutput(2,a->getValue("alphaout2") );
676 if(a->hasAttribute("alphaout3")) w->setAlphaoutput(3,a->getValue("alphaout3") );
677 if(a->hasAttribute("coneout")) w->setAlphaoutput(4,a->getValue("coneout") );
678 if(a->hasAttribute("yawout")) w->setAlphaoutput(5,a->getValue("yawout") );
679 if(a->hasAttribute("rollout")) w->setAlphaoutput(6,a->getValue("rollout") );
681 w->setPitchA(attrf(a, "pitch_a", 10));
682 w->setPitchB(attrf(a, "pitch_b", 10));
683 w->setForceAtPitchA(attrf(a, "forceatpitch_a", 3000));
684 w->setPowerAtPitch0(attrf(a, "poweratpitch_0", 300));
685 w->setPowerAtPitchB(attrf(a, "poweratpitch_b", 3000));
686 if(attrb(a,"notorque"))
689 #define p(x) if (a->hasAttribute(#x)) w->setParameter((char *)#x,attrf(a,#x) );
691 p(translift_maxfactor)
692 p(ground_effect_constant)
693 p(vortex_state_lift_factor)
700 p(number_of_segments)
702 p(rel_len_where_incidence_is_measured)
705 p(airfoil_incidence_no_lift)
706 p(rel_len_blade_start)
707 p(incidence_stall_zero_speed)
708 p(incidence_stall_half_sonic_speed)
712 p(airfoil_lift_coefficient)
713 p(airfoil_drag_coefficient0)
714 p(airfoil_drag_coefficient1)
716 p(rotor_correction_factor)
722 void FGFDM::parsePistonEngine(XMLAttributes* a)
724 float engP = attrf(a, "eng-power") * HP2W;
725 float engS = attrf(a, "eng-rpm") * RPM2RAD;
727 PistonEngine* eng = new PistonEngine(engP, engS);
729 if(a->hasAttribute("displacement"))
730 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
732 if(a->hasAttribute("compression"))
733 eng->setCompression(attrf(a, "compression"));
735 if(a->hasAttribute("turbo-mul")) {
736 float mul = attrf(a, "turbo-mul");
737 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
738 eng->setTurboParams(mul, mp);
739 eng->setTurboLag(attrf(a, "turbo-lag", 2));
742 if(a->hasAttribute("supercharger"))
743 eng->setSupercharger(attrb(a, "supercharger"));
745 ((PropEngine*)_currObj)->setEngine(eng);
748 void FGFDM::parseTurbineEngine(XMLAttributes* a)
750 float power = attrf(a, "eng-power") * HP2W;
751 float omega = attrf(a, "eng-rpm") * RPM2RAD;
752 float alt = attrf(a, "alt") * FT2M;
753 float flatRating = attrf(a, "flat-rating") * HP2W;
754 TurbineEngine* eng = new TurbineEngine(power, omega, alt, flatRating);
756 if(a->hasAttribute("n2-low-idle"))
757 eng->setN2Range(attrf(a, "n2-low-idle"), attrf(a, "n2-high-idle"),
760 // Nasty units conversion: lbs/hr per hp -> kg/s per watt
761 if(a->hasAttribute("bsfc"))
762 eng->setFuelConsumption(attrf(a, "bsfc") * (LBS2KG/(3600*HP2W)));
764 ((PropEngine*)_currObj)->setEngine(eng);
767 void FGFDM::parsePropeller(XMLAttributes* a)
769 // Legacy Handling for the old engines syntax:
770 PistonEngine* eng = 0;
771 if(a->hasAttribute("eng-power")) {
772 SG_LOG(SG_FLIGHT,SG_ALERT, "WARNING: "
773 << "Legacy engine definition in YASim configuration file. "
775 float engP = attrf(a, "eng-power") * HP2W;
776 float engS = attrf(a, "eng-rpm") * RPM2RAD;
777 eng = new PistonEngine(engP, engS);
778 if(a->hasAttribute("displacement"))
779 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
780 if(a->hasAttribute("compression"))
781 eng->setCompression(attrf(a, "compression"));
782 if(a->hasAttribute("turbo-mul")) {
783 float mul = attrf(a, "turbo-mul");
784 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
785 eng->setTurboParams(mul, mp);
789 // Now parse the actual propeller definition:
791 cg[0] = attrf(a, "x");
792 cg[1] = attrf(a, "y");
793 cg[2] = attrf(a, "z");
794 float mass = attrf(a, "mass") * LBS2KG;
795 float moment = attrf(a, "moment");
796 float radius = attrf(a, "radius");
797 float speed = attrf(a, "cruise-speed") * KTS2MPS;
798 float omega = attrf(a, "cruise-rpm") * RPM2RAD;
799 float power = attrf(a, "cruise-power") * HP2W;
800 float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
802 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
803 PropEngine* thruster = new PropEngine(prop, eng, moment);
804 _airplane.addThruster(thruster, mass, cg);
806 // Set the stops (fine = minimum pitch, coarse = maximum pitch)
807 float fine_stop = attrf(a, "fine-stop", 0.25f);
808 float coarse_stop = attrf(a, "coarse-stop", 4.0f);
809 prop->setStops(fine_stop, coarse_stop);
811 if(a->hasAttribute("takeoff-power")) {
812 float power0 = attrf(a, "takeoff-power") * HP2W;
813 float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
814 prop->setTakeoff(omega0, power0);
817 if(a->hasAttribute("max-rpm")) {
818 float max = attrf(a, "max-rpm") * RPM2RAD;
819 float min = attrf(a, "min-rpm") * RPM2RAD;
820 thruster->setVariableProp(min, max);
823 if(attrb(a, "contra"))
824 thruster->setContraPair(true);
826 if(a->hasAttribute("manual-pitch")) {
827 prop->setManualPitch();
830 thruster->setGearRatio(attrf(a, "gear-ratio", 1));
833 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
834 EngRec* er = new EngRec();
836 er->prefix = dup(buf);
842 // Turns a string axis name into an integer for use by the
843 // ControlMap. Creates a new axis if this one hasn't been defined
845 int FGFDM::parseAxis(const char* name)
848 for(i=0; i<_axes.size(); i++) {
849 AxisRec* a = (AxisRec*)_axes.get(i);
850 if(eq(a->name, name))
854 // Not there, make a new one.
855 AxisRec* a = new AxisRec();
857 fgGetNode( a->name, true ); // make sure the property name exists
858 a->handle = _airplane.getControlMap()->newInput();
863 int FGFDM::parseOutput(const char* name)
865 if(eq(name, "THROTTLE")) return ControlMap::THROTTLE;
866 if(eq(name, "MIXTURE")) return ControlMap::MIXTURE;
867 if(eq(name, "CONDLEVER")) return ControlMap::CONDLEVER;
868 if(eq(name, "STARTER")) return ControlMap::STARTER;
869 if(eq(name, "MAGNETOS")) return ControlMap::MAGNETOS;
870 if(eq(name, "ADVANCE")) return ControlMap::ADVANCE;
871 if(eq(name, "REHEAT")) return ControlMap::REHEAT;
872 if(eq(name, "BOOST")) return ControlMap::BOOST;
873 if(eq(name, "VECTOR")) return ControlMap::VECTOR;
874 if(eq(name, "PROP")) return ControlMap::PROP;
875 if(eq(name, "BRAKE")) return ControlMap::BRAKE;
876 if(eq(name, "STEER")) return ControlMap::STEER;
877 if(eq(name, "EXTEND")) return ControlMap::EXTEND;
878 if(eq(name, "HEXTEND")) return ControlMap::HEXTEND;
879 if(eq(name, "LEXTEND")) return ControlMap::LEXTEND;
880 if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
881 if(eq(name, "FLAP0")) return ControlMap::FLAP0;
882 if(eq(name, "FLAP1")) return ControlMap::FLAP1;
883 if(eq(name, "SLAT")) return ControlMap::SLAT;
884 if(eq(name, "SPOILER")) return ControlMap::SPOILER;
885 if(eq(name, "CASTERING")) return ControlMap::CASTERING;
886 if(eq(name, "PROPPITCH")) return ControlMap::PROPPITCH;
887 if(eq(name, "PROPFEATHER")) return ControlMap::PROPFEATHER;
888 if(eq(name, "COLLECTIVE")) return ControlMap::COLLECTIVE;
889 if(eq(name, "CYCLICAIL")) return ControlMap::CYCLICAIL;
890 if(eq(name, "CYCLICELE")) return ControlMap::CYCLICELE;
891 if(eq(name, "ROTORGEARENGINEON")) return ControlMap::ROTORENGINEON;
892 if(eq(name, "ROTORBRAKE")) return ControlMap::ROTORBRAKE;
893 if(eq(name, "REVERSE_THRUST")) return ControlMap::REVERSE_THRUST;
894 if(eq(name, "WASTEGATE")) return ControlMap::WASTEGATE;
895 SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
896 << name << "' in YASim aircraft description.");
901 void FGFDM::parseWeight(XMLAttributes* a)
903 WeightRec* wr = new WeightRec();
906 v[0] = attrf(a, "x");
907 v[1] = attrf(a, "y");
908 v[2] = attrf(a, "z");
910 wr->prop = dup(a->getValue("mass-prop"));
911 wr->size = attrf(a, "size", 0);
912 wr->handle = _airplane.addWeight(v, wr->size);
917 bool FGFDM::eq(const char* a, const char* b)
919 // Figure it out for yourself. :)
920 while(*a && *b && *a == *b) { a++; b++; }
924 char* FGFDM::dup(const char* s)
928 char* s2 = new char[len+1];
930 while((*p++ = *s++));
935 int FGFDM::attri(XMLAttributes* atts, char* attr)
937 if(!atts->hasAttribute(attr)) {
938 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
939 "' in YASim aircraft description");
942 return attri(atts, attr, 0);
945 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
947 const char* val = atts->getValue(attr);
948 if(val == 0) return def;
949 else return atol(val);
952 float FGFDM::attrf(XMLAttributes* atts, char* attr)
954 if(!atts->hasAttribute(attr)) {
955 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
956 "' in YASim aircraft description");
959 return attrf(atts, attr, 0);
962 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
964 const char* val = atts->getValue(attr);
965 if(val == 0) return def;
966 else return (float)atof(val);
969 // ACK: the dreaded ambiguous string boolean. Remind me to shoot Maik
970 // when I have a chance. :). Unless you have a parser that can check
971 // symbol constants (we don't), this kind of coding is just a Bad
972 // Idea. This implementation, for example, silently returns a boolean
973 // falsehood for values of "1", "yes", "True", and "TRUE". Which is
974 // especially annoying preexisting boolean attributes in the same
975 // parser want to see "1" and will choke on a "true"...
977 // Unfortunately, this usage creeped into existing configuration files
978 // while I wasn't active, and it's going to be hard to remove. Issue
979 // a warning to nag people into changing their ways for now...
980 bool FGFDM::attrb(XMLAttributes* atts, char* attr)
982 const char* val = atts->getValue(attr);
983 if(val == 0) return false;
986 SG_LOG(SG_FLIGHT, SG_ALERT, "Warning: " <<
987 "deprecated 'true' boolean in YASim configuration file. " <<
988 "Use numeric booleans (attribute=\"1\") instead");
991 return attri(atts, attr, 0) ? true : false;
994 }; // namespace yasim