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/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/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);
74 setOutputProperties();
77 Airplane* FGFDM::getAirplane()
84 // Allows the user to start with something other than full fuel
85 _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
87 // This has a nasty habit of being false at startup. That's not
89 fgSetBool("/controls/gear-down", true);
92 // Not the worlds safest parser. But it's short & sweet.
93 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
95 XMLAttributes* a = (XMLAttributes*)&atts;
99 if(eq(name, "airplane")) {
100 _airplane.setWeight(attrf(a, "mass") * LBS2KG);
101 } else if(eq(name, "approach")) {
102 float spd = attrf(a, "speed") * KTS2MPS;
103 float alt = attrf(a, "alt", 0) * FT2M;
104 float aoa = attrf(a, "aoa", 0) * DEG2RAD;
105 _airplane.setApproach(spd, alt, aoa);
107 } else if(eq(name, "cruise")) {
108 float spd = attrf(a, "speed") * KTS2MPS;
109 float alt = attrf(a, "alt") * FT2M;
110 _airplane.setCruise(spd, alt);
112 } else if(eq(name, "cockpit")) {
113 v[0] = attrf(a, "x");
114 v[1] = attrf(a, "y");
115 v[2] = attrf(a, "z");
116 _airplane.setPilotPos(v);
117 } else if(eq(name, "wing")) {
118 _airplane.setWing(parseWing(a, name));
119 } else if(eq(name, "hstab")) {
120 _airplane.setTail(parseWing(a, name));
121 } else if(eq(name, "vstab")) {
122 _airplane.addVStab(parseWing(a, name));
123 } else if(eq(name, "propeller")) {
125 } else if(eq(name, "thruster")) {
126 SimpleJet* j = new SimpleJet();
128 v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
130 _airplane.addThruster(j, 0, v);
131 v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
133 j->setThrust(attrf(a, "thrust") * LBS2N);
134 } else if(eq(name, "jet")) {
137 v[0] = attrf(a, "x");
138 v[1] = attrf(a, "y");
139 v[2] = attrf(a, "z");
140 float mass = attrf(a, "mass") * LBS2KG;
141 j->setMaxThrust(attrf(a, "thrust") * LBS2N,
142 attrf(a, "afterburner", 0) * LBS2N);
143 j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
145 float n1min = attrf(a, "n1-idle", 55);
146 float n1max = attrf(a, "n1-max", 102);
147 float n2min = attrf(a, "n2-idle", 73);
148 float n2max = attrf(a, "n2-max", 103);
149 j->setRPMs(n1min, n1max, n2min, n2max);
151 j->setTSFC(attrf(a, "tsfc", 0.8));
152 if(a->hasAttribute("egt")) j->setEGT(attrf(a, "egt"));
153 if(a->hasAttribute("epr")) j->setEPR(attrf(a, "epr"));
154 if(a->hasAttribute("exhaust-speed"))
155 j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
158 _airplane.addThruster(j, mass, v);
159 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
160 EngRec* er = new EngRec();
162 er->prefix = dup(buf);
164 } else if(eq(name, "gear")) {
165 Gear* g = new Gear();
167 v[0] = attrf(a, "x");
168 v[1] = attrf(a, "y");
169 v[2] = attrf(a, "z");
173 v[2] = attrf(a, "compression", 1);
174 g->setCompression(v);
175 g->setBrake(attrf(a, "skid", 0));
176 g->setStaticFriction(attrf(a, "sfric", 0.8));
177 g->setDynamicFriction(attrf(a, "dfric", 0.7));
178 g->setSpring(attrf(a, "spring", 1));
179 g->setDamping(attrf(a, "damp", 1));
180 _airplane.addGear(g);
181 } else if(eq(name, "fuselage")) {
183 v[0] = attrf(a, "ax");
184 v[1] = attrf(a, "ay");
185 v[2] = attrf(a, "az");
186 b[0] = attrf(a, "bx");
187 b[1] = attrf(a, "by");
188 b[2] = attrf(a, "bz");
189 float taper = attrf(a, "taper", 1);
190 float mid = attrf(a, "midpoint", 0.5);
191 _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid);
192 } else if(eq(name, "tank")) {
193 v[0] = attrf(a, "x");
194 v[1] = attrf(a, "y");
195 v[2] = attrf(a, "z");
196 float density = 6.0; // gasoline, in lbs/gal
197 if(a->hasAttribute("jet")) density = 6.72;
198 density *= LBS2KG*CM2GALS;
199 _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
200 } else if(eq(name, "ballast")) {
201 v[0] = attrf(a, "x");
202 v[1] = attrf(a, "y");
203 v[2] = attrf(a, "z");
204 _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
205 } else if(eq(name, "weight")) {
207 } else if(eq(name, "stall")) {
208 Wing* w = (Wing*)_currObj;
209 w->setStall(attrf(a, "aoa") * DEG2RAD);
210 w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
211 w->setStallPeak(attrf(a, "peak", 1.5));
212 } else if(eq(name, "flap0")) {
213 ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
214 attrf(a, "lift"), attrf(a, "drag"));
215 } else if(eq(name, "flap1")) {
216 ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
217 attrf(a, "lift"), attrf(a, "drag"));
218 } else if(eq(name, "slat")) {
219 ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
220 attrf(a, "aoa"), attrf(a, "drag"));
221 } else if(eq(name, "spoiler")) {
222 ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
223 attrf(a, "lift"), attrf(a, "drag"));
224 } else if(eq(name, "actionpt")) {
225 v[0] = attrf(a, "x");
226 v[1] = attrf(a, "y");
227 v[2] = attrf(a, "z");
228 ((Thruster*)_currObj)->setPosition(v);
229 } else if(eq(name, "dir")) {
230 v[0] = attrf(a, "x");
231 v[1] = attrf(a, "y");
232 v[2] = attrf(a, "z");
233 ((Thruster*)_currObj)->setDirection(v);
234 } else if(eq(name, "control-setting")) {
235 // A cruise or approach control setting
236 const char* axis = a->getValue("axis");
237 float value = attrf(a, "value", 0);
239 _airplane.addCruiseControl(parseAxis(axis), value);
241 _airplane.addApproachControl(parseAxis(axis), value);
242 } else if(eq(name, "control-input")) {
244 // A mapping of input property to a control
245 int axis = parseAxis(a->getValue("axis"));
246 int control = parseOutput(a->getValue("control"));
248 opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
249 opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
250 opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
252 ControlMap* cm = _airplane.getControlMap();
253 if(a->hasAttribute("src0")) {
254 cm->addMapping(axis, control, _currObj, opt,
255 attrf(a, "src0"), attrf(a, "src1"),
256 attrf(a, "dst0"), attrf(a, "dst1"));
258 cm->addMapping(axis, control, _currObj, opt);
260 } else if(eq(name, "control-output")) {
261 // A property output for a control on the current object
262 ControlMap* cm = _airplane.getControlMap();
263 int type = parseOutput(a->getValue("control"));
264 int handle = cm->getOutputHandle(_currObj, type);
266 PropOut* p = new PropOut();
267 p->prop = fgGetNode(a->getValue("prop"), true);
270 p->left = !(a->hasAttribute("side") &&
271 eq("right", a->getValue("side")));
272 p->min = attrf(a, "min", cm->rangeMin(type));
273 p->max = attrf(a, "max", cm->rangeMax(type));
274 _controlProps.add(p);
276 } else if(eq(name, "control-speed")) {
277 ControlMap* cm = _airplane.getControlMap();
278 int type = parseOutput(a->getValue("control"));
279 int handle = cm->getOutputHandle(_currObj, type);
280 float time = attrf(a, "transition-time", 0);
282 cm->setTransitionTime(handle, time);
284 SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '"
285 << name << "' found in YASim aircraft description");
290 void FGFDM::getExternalInput(float dt)
293 ControlMap* cm = _airplane.getControlMap();
296 for(i=0; i<_axes.size(); i++) {
297 AxisRec* a = (AxisRec*)_axes.get(i);
298 float val = fgGetFloat(a->name, 0);
299 cm->setInput(a->handle, val);
301 cm->applyControls(dt);
304 for(i=0; i<_weights.size(); i++) {
305 WeightRec* wr = (WeightRec*)_weights.get(i);
306 _airplane.setWeight(wr->handle, LBS2KG * fgGetFloat(wr->prop));
310 void FGFDM::setOutputProperties()
315 float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
316 fgSetFloat("/yasim/gross-weight-lbs", grossWgt);
318 ControlMap* cm = _airplane.getControlMap();
319 for(i=0; i<_controlProps.size(); i++) {
320 PropOut* p = (PropOut*)_controlProps.get(i);
322 ? cm->getOutput(p->handle)
323 : cm->getOutputR(p->handle));
324 float rmin = cm->rangeMin(p->type);
325 float rmax = cm->rangeMax(p->type);
326 float frac = (val - rmin) / (rmax - rmin);
327 val = frac*(p->max - p->min) + p->min;
328 p->prop->setFloatValue(val);
331 float totalFuel = 0, totalCap = 0;
332 float fuelDensity = 720; // in kg/m^3, default to gasoline: ~6 lb/gal
333 for(i=0; i<_airplane.numTanks(); i++) {
334 fuelDensity = _airplane.getFuelDensity(i);
335 sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
336 fgSetFloat(buf, CM2GALS*_airplane.getFuel(i)/fuelDensity);
337 sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
338 fgSetFloat(buf, KG2LBS*_airplane.getFuel(i));
339 totalFuel += _airplane.getFuel(i);
340 totalCap += _airplane.getTankCapacity(i);
343 fgSetFloat("/consumables/fuel/total-fuel-lbs", KG2LBS*totalFuel);
344 fgSetFloat("/consumables/fuel/total-fuel-gals",
345 CM2GALS*totalFuel/fuelDensity);
346 fgSetFloat("/consumables/fuel/total-fuel-norm", totalFuel/totalCap);
349 for(i=0; i<_thrusters.size(); i++) {
350 EngRec* er = (EngRec*)_thrusters.get(i);
351 Thruster* t = er->eng;
353 sprintf(buf, "%s/fuel-flow-gph", er->prefix);
354 fgSetFloat(buf, (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
356 if(t->getPropEngine()) {
357 PropEngine* p = t->getPropEngine();
359 sprintf(buf, "%s/rpm", er->prefix);
360 fgSetFloat(buf, p->getOmega() / RPM2RAD);
363 if(t->getPistonEngine()) {
364 PistonEngine* p = t->getPistonEngine();
366 sprintf(buf, "%s/mp-osi", er->prefix);
367 fgSetFloat(buf, p->getMP() * (1/INHG2PA));
369 sprintf(buf, "%s/egt-degf", er->prefix);
370 fgSetFloat(buf, p->getEGT() * K2DEGF + 459.4);
374 Jet* j = t->getJet();
376 sprintf(buf, "%s/n1", er->prefix);
377 fgSetFloat(buf, j->getN1());
379 sprintf(buf, "%s/n2", er->prefix);
380 fgSetFloat(buf, j->getN2());
382 sprintf(buf, "%s/epr", er->prefix);
383 fgSetFloat(buf, j->getEPR());
385 sprintf(buf, "%s/egt-degf", er->prefix);
386 fgSetFloat(buf, j->getEGT() * K2DEGF + 459.4);
391 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
393 Wing* w = new Wing();
396 if(eq(type, "vstab"))
402 pos[0] = attrf(a, "x");
403 pos[1] = attrf(a, "y");
404 pos[2] = attrf(a, "z");
407 w->setLength(attrf(a, "length"));
408 w->setChord(attrf(a, "chord"));
409 w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
410 w->setTaper(attrf(a, "taper", 1));
411 w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
412 w->setCamber(attrf(a, "camber", 0));
413 w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
415 // The 70% is a magic number that sorta kinda seems to match known
416 // throttle settings to approach speed.
417 w->setInducedDrag(0.7*attrf(a, "idrag", 1));
419 float effect = attrf(a, "effectiveness", 1);
420 w->setDragScale(w->getDragScale()*effect);
426 void FGFDM::parsePropeller(XMLAttributes* a)
429 cg[0] = attrf(a, "x");
430 cg[1] = attrf(a, "y");
431 cg[2] = attrf(a, "z");
432 float mass = attrf(a, "mass") * LBS2KG;
433 float moment = attrf(a, "moment");
434 float radius = attrf(a, "radius");
435 float speed = attrf(a, "cruise-speed") * KTS2MPS;
436 float omega = attrf(a, "cruise-rpm") * RPM2RAD;
437 float power = attrf(a, "cruise-power") * HP2W;
438 float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
440 // Hack, fix this pronto:
441 float engP = attrf(a, "eng-power") * HP2W;
442 float engS = attrf(a, "eng-rpm") * RPM2RAD;
444 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
445 PistonEngine* eng = new PistonEngine(engP, engS);
446 PropEngine* thruster = new PropEngine(prop, eng, moment);
447 _airplane.addThruster(thruster, mass, cg);
449 if(a->hasAttribute("displacement"))
450 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
452 if(a->hasAttribute("compression"))
453 eng->setCompression(attrf(a, "compression"));
455 if(a->hasAttribute("turbo-mul")) {
456 float mul = attrf(a, "turbo-mul");
457 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
458 eng->setTurboParams(mul, mp);
461 if(a->hasAttribute("takeoff-power")) {
462 float power0 = attrf(a, "takeoff-power") * HP2W;
463 float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
464 prop->setTakeoff(omega0, power0);
467 if(a->hasAttribute("max-rpm")) {
468 float max = attrf(a, "max-rpm") * RPM2RAD;
469 float min = attrf(a, "min-rpm") * RPM2RAD;
470 thruster->setVariableProp(min, max);
474 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
475 EngRec* er = new EngRec();
477 er->prefix = dup(buf);
483 // Turns a string axis name into an integer for use by the
484 // ControlMap. Creates a new axis if this one hasn't been defined
486 int FGFDM::parseAxis(const char* name)
489 for(i=0; i<_axes.size(); i++) {
490 AxisRec* a = (AxisRec*)_axes.get(i);
491 if(eq(a->name, name))
495 // Not there, make a new one.
496 AxisRec* a = new AxisRec();
498 a->handle = _airplane.getControlMap()->newInput();
503 int FGFDM::parseOutput(const char* name)
505 if(eq(name, "THROTTLE")) return ControlMap::THROTTLE;
506 if(eq(name, "MIXTURE")) return ControlMap::MIXTURE;
507 if(eq(name, "STARTER")) return ControlMap::STARTER;
508 if(eq(name, "MAGNETOS")) return ControlMap::MAGNETOS;
509 if(eq(name, "ADVANCE")) return ControlMap::ADVANCE;
510 if(eq(name, "REHEAT")) return ControlMap::REHEAT;
511 if(eq(name, "BOOST")) return ControlMap::BOOST;
512 if(eq(name, "VECTOR")) return ControlMap::VECTOR;
513 if(eq(name, "PROP")) return ControlMap::PROP;
514 if(eq(name, "BRAKE")) return ControlMap::BRAKE;
515 if(eq(name, "STEER")) return ControlMap::STEER;
516 if(eq(name, "EXTEND")) return ControlMap::EXTEND;
517 if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
518 if(eq(name, "FLAP0")) return ControlMap::FLAP0;
519 if(eq(name, "FLAP1")) return ControlMap::FLAP1;
520 if(eq(name, "SLAT")) return ControlMap::SLAT;
521 if(eq(name, "SPOILER")) return ControlMap::SPOILER;
522 if(eq(name, "CASTERING")) return ControlMap::CASTERING;
523 SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
524 << name << "' in YASim aircraft description.");
529 void FGFDM::parseWeight(XMLAttributes* a)
531 WeightRec* wr = new WeightRec();
534 v[0] = attrf(a, "x");
535 v[1] = attrf(a, "y");
536 v[2] = attrf(a, "z");
538 wr->prop = dup(a->getValue("mass-prop"));
539 wr->size = attrf(a, "size", 0);
540 wr->handle = _airplane.addWeight(v, wr->size);
545 bool FGFDM::eq(const char* a, const char* b)
547 // Figure it out for yourself. :)
548 while(*a && *b && *a == *b) { a++; b++; }
552 char* FGFDM::dup(const char* s)
556 char* s2 = new char[len+1];
558 while((*p++ = *s++));
563 int FGFDM::attri(XMLAttributes* atts, char* attr)
565 if(!atts->hasAttribute(attr)) {
566 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
567 "' in YASim aircraft description");
570 return attri(atts, attr, 0);
573 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
575 const char* val = atts->getValue(attr);
576 if(val == 0) return def;
577 else return atol(val);
580 float FGFDM::attrf(XMLAttributes* atts, char* attr)
582 if(!atts->hasAttribute(attr)) {
583 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
584 "' in YASim aircraft description");
587 return attrf(atts, attr, 0);
590 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
592 const char* val = atts->getValue(attr);
593 if(val == 0) return def;
594 else return (float)atof(val);
597 }; // namespace yasim