4 #include <Main/fg_props.hxx>
7 #include "SimpleJet.hpp"
9 #include "Atmosphere.hpp"
10 #include "PropEngine.hpp"
11 #include "Propeller.hpp"
12 #include "PistonEngine.hpp"
17 // Some conversion factors
18 static const float KTS2MPS = 0.514444444444;
19 static const float FT2M = 0.3048;
20 static const float DEG2RAD = 0.0174532925199;
21 static const float RPM2RAD = 0.10471975512;
22 static const float LBS2N = 4.44822;
23 static const float LBS2KG = 0.45359237;
24 static const float KG2LBS = 2.2046225;
25 static const float CM2GALS = 264.172037284;
26 static const float HP2W = 745.700;
27 static const float INHG2PA = 3386.389;
28 static const float K2DEGF = 1.8;
29 static const float CIN2CM = 1.6387064e-5;
31 // Stubs, so that this can be compiled without the FlightGear
32 // binary. What's the best way to handle this?
34 // float fgGetFloat(char* name, float def) { return 0; }
35 // void fgSetFloat(char* name, float val) {}
41 // Map /controls/flight/elevator to the approach elevator control. This
42 // should probably be settable, but there are very few aircraft
43 // who trim their approaches using things other than elevator.
44 _airplane.setElevatorControl(parseAxis("/controls/flight/elevator-trim"));
50 for(i=0; i<_axes.size(); i++) {
51 AxisRec* a = (AxisRec*)_axes.get(i);
55 for(i=0; i<_thrusters.size(); i++) {
56 EngRec* er = (EngRec*)_thrusters.get(i);
61 for(i=0; i<_weights.size(); i++) {
62 WeightRec* wr = (WeightRec*)_weights.get(i);
66 for(i=0; i<_controlProps.size(); i++)
67 delete (PropOut*)_controlProps.get(i);
70 void FGFDM::iterate(float dt)
73 _airplane.iterate(dt);
75 if(fgGetBool("/sim/freeze/fuel") != true)
76 _airplane.consumeFuel(dt);
78 setOutputProperties();
81 Airplane* FGFDM::getAirplane()
88 // Allows the user to start with something other than full fuel
89 _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
91 // This has a nasty habit of being false at startup. That's not
93 fgSetBool("/controls/gear/gear-down", true);
96 // Not the worlds safest parser. But it's short & sweet.
97 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
99 XMLAttributes* a = (XMLAttributes*)&atts;
103 if(eq(name, "airplane")) {
104 _airplane.setWeight(attrf(a, "mass") * LBS2KG);
105 } else if(eq(name, "approach")) {
106 float spd = attrf(a, "speed") * KTS2MPS;
107 float alt = attrf(a, "alt", 0) * FT2M;
108 float aoa = attrf(a, "aoa", 0) * DEG2RAD;
109 _airplane.setApproach(spd, alt, aoa);
111 } else if(eq(name, "cruise")) {
112 float spd = attrf(a, "speed") * KTS2MPS;
113 float alt = attrf(a, "alt") * FT2M;
114 _airplane.setCruise(spd, alt);
116 } else if(eq(name, "cockpit")) {
117 v[0] = attrf(a, "x");
118 v[1] = attrf(a, "y");
119 v[2] = attrf(a, "z");
120 _airplane.setPilotPos(v);
121 } else if(eq(name, "wing")) {
122 _airplane.setWing(parseWing(a, name));
123 } else if(eq(name, "hstab")) {
124 _airplane.setTail(parseWing(a, name));
125 } else if(eq(name, "vstab")) {
126 _airplane.addVStab(parseWing(a, name));
127 } else if(eq(name, "propeller")) {
129 } else if(eq(name, "thruster")) {
130 SimpleJet* j = new SimpleJet();
132 v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
134 _airplane.addThruster(j, 0, v);
135 v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
137 j->setThrust(attrf(a, "thrust") * LBS2N);
138 } else if(eq(name, "jet")) {
141 v[0] = attrf(a, "x");
142 v[1] = attrf(a, "y");
143 v[2] = attrf(a, "z");
144 float mass = attrf(a, "mass") * LBS2KG;
145 j->setMaxThrust(attrf(a, "thrust") * LBS2N,
146 attrf(a, "afterburner", 0) * LBS2N);
147 j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
149 float n1min = attrf(a, "n1-idle", 55);
150 float n1max = attrf(a, "n1-max", 102);
151 float n2min = attrf(a, "n2-idle", 73);
152 float n2max = attrf(a, "n2-max", 103);
153 j->setRPMs(n1min, n1max, n2min, n2max);
155 j->setTSFC(attrf(a, "tsfc", 0.8));
156 if(a->hasAttribute("egt")) j->setEGT(attrf(a, "egt"));
157 if(a->hasAttribute("epr")) j->setEPR(attrf(a, "epr"));
158 if(a->hasAttribute("exhaust-speed"))
159 j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
162 _airplane.addThruster(j, mass, v);
163 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
164 EngRec* er = new EngRec();
166 er->prefix = dup(buf);
168 } else if(eq(name, "gear")) {
169 Gear* g = new Gear();
171 v[0] = attrf(a, "x");
172 v[1] = attrf(a, "y");
173 v[2] = attrf(a, "z");
177 v[2] = attrf(a, "compression", 1);
178 g->setCompression(v);
179 g->setBrake(attrf(a, "skid", 0));
180 g->setStaticFriction(attrf(a, "sfric", 0.8));
181 g->setDynamicFriction(attrf(a, "dfric", 0.7));
182 g->setSpring(attrf(a, "spring", 1));
183 g->setDamping(attrf(a, "damp", 1));
184 _airplane.addGear(g);
185 } else if(eq(name, "fuselage")) {
187 v[0] = attrf(a, "ax");
188 v[1] = attrf(a, "ay");
189 v[2] = attrf(a, "az");
190 b[0] = attrf(a, "bx");
191 b[1] = attrf(a, "by");
192 b[2] = attrf(a, "bz");
193 float taper = attrf(a, "taper", 1);
194 float mid = attrf(a, "midpoint", 0.5);
195 _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid);
196 } else if(eq(name, "tank")) {
197 v[0] = attrf(a, "x");
198 v[1] = attrf(a, "y");
199 v[2] = attrf(a, "z");
200 float density = 6.0; // gasoline, in lbs/gal
201 if(a->hasAttribute("jet")) density = 6.72;
202 density *= LBS2KG*CM2GALS;
203 _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
204 } else if(eq(name, "ballast")) {
205 v[0] = attrf(a, "x");
206 v[1] = attrf(a, "y");
207 v[2] = attrf(a, "z");
208 _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
209 } else if(eq(name, "weight")) {
211 } else if(eq(name, "stall")) {
212 Wing* w = (Wing*)_currObj;
213 w->setStall(attrf(a, "aoa") * DEG2RAD);
214 w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
215 w->setStallPeak(attrf(a, "peak", 1.5));
216 } else if(eq(name, "flap0")) {
217 ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
218 attrf(a, "lift"), attrf(a, "drag"));
219 } else if(eq(name, "flap1")) {
220 ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
221 attrf(a, "lift"), attrf(a, "drag"));
222 } else if(eq(name, "slat")) {
223 ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
224 attrf(a, "aoa"), attrf(a, "drag"));
225 } else if(eq(name, "spoiler")) {
226 ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
227 attrf(a, "lift"), attrf(a, "drag"));
228 } else if(eq(name, "actionpt")) {
229 v[0] = attrf(a, "x");
230 v[1] = attrf(a, "y");
231 v[2] = attrf(a, "z");
232 ((Thruster*)_currObj)->setPosition(v);
233 } else if(eq(name, "dir")) {
234 v[0] = attrf(a, "x");
235 v[1] = attrf(a, "y");
236 v[2] = attrf(a, "z");
237 ((Thruster*)_currObj)->setDirection(v);
238 } else if(eq(name, "control-setting")) {
239 // A cruise or approach control setting
240 const char* axis = a->getValue("axis");
241 float value = attrf(a, "value", 0);
243 _airplane.addCruiseControl(parseAxis(axis), value);
245 _airplane.addApproachControl(parseAxis(axis), value);
246 } else if(eq(name, "control-input")) {
248 // A mapping of input property to a control
249 int axis = parseAxis(a->getValue("axis"));
250 int control = parseOutput(a->getValue("control"));
252 opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
253 opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
254 opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
256 ControlMap* cm = _airplane.getControlMap();
257 if(a->hasAttribute("src0")) {
258 cm->addMapping(axis, control, _currObj, opt,
259 attrf(a, "src0"), attrf(a, "src1"),
260 attrf(a, "dst0"), attrf(a, "dst1"));
262 cm->addMapping(axis, control, _currObj, opt);
264 } else if(eq(name, "control-output")) {
265 // A property output for a control on the current object
266 ControlMap* cm = _airplane.getControlMap();
267 int type = parseOutput(a->getValue("control"));
268 int handle = cm->getOutputHandle(_currObj, type);
270 PropOut* p = new PropOut();
271 p->prop = fgGetNode(a->getValue("prop"), true);
274 p->left = !(a->hasAttribute("side") &&
275 eq("right", a->getValue("side")));
276 p->min = attrf(a, "min", cm->rangeMin(type));
277 p->max = attrf(a, "max", cm->rangeMax(type));
278 _controlProps.add(p);
280 } else if(eq(name, "control-speed")) {
281 ControlMap* cm = _airplane.getControlMap();
282 int type = parseOutput(a->getValue("control"));
283 int handle = cm->getOutputHandle(_currObj, type);
284 float time = attrf(a, "transition-time", 0);
286 cm->setTransitionTime(handle, time);
288 SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '"
289 << name << "' found in YASim aircraft description");
294 void FGFDM::getExternalInput(float dt)
297 ControlMap* cm = _airplane.getControlMap();
300 for(i=0; i<_axes.size(); i++) {
301 AxisRec* a = (AxisRec*)_axes.get(i);
302 float val = fgGetFloat(a->name, 0);
303 cm->setInput(a->handle, val);
305 cm->applyControls(dt);
308 for(i=0; i<_weights.size(); i++) {
309 WeightRec* wr = (WeightRec*)_weights.get(i);
310 _airplane.setWeight(wr->handle, LBS2KG * fgGetFloat(wr->prop));
314 void FGFDM::setOutputProperties()
319 float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
320 fgSetFloat("/yasim/gross-weight-lbs", grossWgt);
322 ControlMap* cm = _airplane.getControlMap();
323 for(i=0; i<_controlProps.size(); i++) {
324 PropOut* p = (PropOut*)_controlProps.get(i);
326 ? cm->getOutput(p->handle)
327 : cm->getOutputR(p->handle));
328 float rmin = cm->rangeMin(p->type);
329 float rmax = cm->rangeMax(p->type);
330 float frac = (val - rmin) / (rmax - rmin);
331 val = frac*(p->max - p->min) + p->min;
332 p->prop->setFloatValue(val);
335 float totalFuel = 0, totalCap = 0;
336 float fuelDensity = 720; // in kg/m^3, default to gasoline: ~6 lb/gal
337 for(i=0; i<_airplane.numTanks(); i++) {
338 fuelDensity = _airplane.getFuelDensity(i);
339 sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
340 fgSetFloat(buf, CM2GALS*_airplane.getFuel(i)/fuelDensity);
341 sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
342 fgSetFloat(buf, KG2LBS*_airplane.getFuel(i));
343 totalFuel += _airplane.getFuel(i);
344 totalCap += _airplane.getTankCapacity(i);
347 fgSetFloat("/consumables/fuel/total-fuel-lbs", KG2LBS*totalFuel);
348 fgSetFloat("/consumables/fuel/total-fuel-gals",
349 CM2GALS*totalFuel/fuelDensity);
350 fgSetFloat("/consumables/fuel/total-fuel-norm", totalFuel/totalCap);
353 for(i=0; i<_thrusters.size(); i++) {
354 EngRec* er = (EngRec*)_thrusters.get(i);
355 Thruster* t = er->eng;
357 sprintf(buf, "%s/fuel-flow-gph", er->prefix);
358 fgSetFloat(buf, (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
360 if(t->getPropEngine()) {
361 PropEngine* p = t->getPropEngine();
363 sprintf(buf, "%s/rpm", er->prefix);
364 fgSetFloat(buf, p->getOmega() / RPM2RAD);
367 if(t->getPistonEngine()) {
368 PistonEngine* p = t->getPistonEngine();
370 sprintf(buf, "%s/mp-osi", er->prefix);
371 fgSetFloat(buf, p->getMP() * (1/INHG2PA));
373 sprintf(buf, "%s/egt-degf", er->prefix);
374 fgSetFloat(buf, p->getEGT() * K2DEGF + 459.4);
378 Jet* j = t->getJet();
380 sprintf(buf, "%s/n1", er->prefix);
381 fgSetFloat(buf, j->getN1());
383 sprintf(buf, "%s/n2", er->prefix);
384 fgSetFloat(buf, j->getN2());
386 sprintf(buf, "%s/epr", er->prefix);
387 fgSetFloat(buf, j->getEPR());
389 sprintf(buf, "%s/egt-degf", er->prefix);
390 fgSetFloat(buf, j->getEGT() * K2DEGF + 459.4);
395 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
397 Wing* w = new Wing();
400 if(eq(type, "vstab"))
406 pos[0] = attrf(a, "x");
407 pos[1] = attrf(a, "y");
408 pos[2] = attrf(a, "z");
411 w->setLength(attrf(a, "length"));
412 w->setChord(attrf(a, "chord"));
413 w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
414 w->setTaper(attrf(a, "taper", 1));
415 w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
416 w->setCamber(attrf(a, "camber", 0));
417 w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
418 w->setTwist(attrf(a, "twist", 0) * DEG2RAD);
420 // The 70% is a magic number that sorta kinda seems to match known
421 // throttle settings to approach speed.
422 w->setInducedDrag(0.7*attrf(a, "idrag", 1));
424 float effect = attrf(a, "effectiveness", 1);
425 w->setDragScale(w->getDragScale()*effect);
431 void FGFDM::parsePropeller(XMLAttributes* a)
434 cg[0] = attrf(a, "x");
435 cg[1] = attrf(a, "y");
436 cg[2] = attrf(a, "z");
437 float mass = attrf(a, "mass") * LBS2KG;
438 float moment = attrf(a, "moment");
439 float radius = attrf(a, "radius");
440 float speed = attrf(a, "cruise-speed") * KTS2MPS;
441 float omega = attrf(a, "cruise-rpm") * RPM2RAD;
442 float power = attrf(a, "cruise-power") * HP2W;
443 float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
445 // Hack, fix this pronto:
446 float engP = attrf(a, "eng-power") * HP2W;
447 float engS = attrf(a, "eng-rpm") * RPM2RAD;
449 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
450 PistonEngine* eng = new PistonEngine(engP, engS);
451 PropEngine* thruster = new PropEngine(prop, eng, moment);
452 _airplane.addThruster(thruster, mass, cg);
454 if(a->hasAttribute("displacement"))
455 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
457 if(a->hasAttribute("compression"))
458 eng->setCompression(attrf(a, "compression"));
460 if(a->hasAttribute("turbo-mul")) {
461 float mul = attrf(a, "turbo-mul");
462 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
463 eng->setTurboParams(mul, mp);
466 if(a->hasAttribute("takeoff-power")) {
467 float power0 = attrf(a, "takeoff-power") * HP2W;
468 float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
469 prop->setTakeoff(omega0, power0);
472 if(a->hasAttribute("max-rpm")) {
473 float max = attrf(a, "max-rpm") * RPM2RAD;
474 float min = attrf(a, "min-rpm") * RPM2RAD;
475 thruster->setVariableProp(min, max);
479 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
480 EngRec* er = new EngRec();
482 er->prefix = dup(buf);
488 // Turns a string axis name into an integer for use by the
489 // ControlMap. Creates a new axis if this one hasn't been defined
491 int FGFDM::parseAxis(const char* name)
494 for(i=0; i<_axes.size(); i++) {
495 AxisRec* a = (AxisRec*)_axes.get(i);
496 if(eq(a->name, name))
500 // Not there, make a new one.
501 AxisRec* a = new AxisRec();
503 a->handle = _airplane.getControlMap()->newInput();
508 int FGFDM::parseOutput(const char* name)
510 if(eq(name, "THROTTLE")) return ControlMap::THROTTLE;
511 if(eq(name, "MIXTURE")) return ControlMap::MIXTURE;
512 if(eq(name, "STARTER")) return ControlMap::STARTER;
513 if(eq(name, "MAGNETOS")) return ControlMap::MAGNETOS;
514 if(eq(name, "ADVANCE")) return ControlMap::ADVANCE;
515 if(eq(name, "REHEAT")) return ControlMap::REHEAT;
516 if(eq(name, "BOOST")) return ControlMap::BOOST;
517 if(eq(name, "VECTOR")) return ControlMap::VECTOR;
518 if(eq(name, "PROP")) return ControlMap::PROP;
519 if(eq(name, "BRAKE")) return ControlMap::BRAKE;
520 if(eq(name, "STEER")) return ControlMap::STEER;
521 if(eq(name, "EXTEND")) return ControlMap::EXTEND;
522 if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
523 if(eq(name, "FLAP0")) return ControlMap::FLAP0;
524 if(eq(name, "FLAP1")) return ControlMap::FLAP1;
525 if(eq(name, "SLAT")) return ControlMap::SLAT;
526 if(eq(name, "SPOILER")) return ControlMap::SPOILER;
527 if(eq(name, "CASTERING")) return ControlMap::CASTERING;
528 SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
529 << name << "' in YASim aircraft description.");
534 void FGFDM::parseWeight(XMLAttributes* a)
536 WeightRec* wr = new WeightRec();
539 v[0] = attrf(a, "x");
540 v[1] = attrf(a, "y");
541 v[2] = attrf(a, "z");
543 wr->prop = dup(a->getValue("mass-prop"));
544 wr->size = attrf(a, "size", 0);
545 wr->handle = _airplane.addWeight(v, wr->size);
550 bool FGFDM::eq(const char* a, const char* b)
552 // Figure it out for yourself. :)
553 while(*a && *b && *a == *b) { a++; b++; }
557 char* FGFDM::dup(const char* s)
561 char* s2 = new char[len+1];
563 while((*p++ = *s++));
568 int FGFDM::attri(XMLAttributes* atts, char* attr)
570 if(!atts->hasAttribute(attr)) {
571 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
572 "' in YASim aircraft description");
575 return attri(atts, attr, 0);
578 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
580 const char* val = atts->getValue(attr);
581 if(val == 0) return def;
582 else return atol(val);
585 float FGFDM::attrf(XMLAttributes* atts, char* attr)
587 if(!atts->hasAttribute(attr)) {
588 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
589 "' in YASim aircraft description");
592 return attrf(atts, attr, 0);
595 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
597 const char* val = atts->getValue(attr);
598 if(val == 0) return def;
599 else return (float)atof(val);
602 }; // namespace yasim