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 CM2GALS = 264.172037284;
25 static const float HP2W = 745.700;
26 static const float INHG2PA = 3386.389;
27 static const float K2DEGF = 1.8;
28 static const float CIN2CM = 1.6387064e-5;
30 // Stubs, so that this can be compiled without the FlightGear
31 // binary. What's the best way to handle this?
33 // float fgGetFloat(char* name, float def) { return 0; }
34 // void fgSetFloat(char* name, float val) {}
44 for(i=0; i<_axes.size(); i++) {
45 AxisRec* a = (AxisRec*)_axes.get(i);
49 for(i=0; i<_thrusters.size(); i++) {
50 EngRec* er = (EngRec*)_thrusters.get(i);
55 for(i=0; i<_weights.size(); i++) {
56 WeightRec* wr = (WeightRec*)_weights.get(i);
63 void FGFDM::iterate(float dt)
66 _airplane.iterate(dt);
67 setOutputProperties();
70 Airplane* FGFDM::getAirplane()
77 // Allows the user to start with something other than full fuel
78 _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
80 // This has a nasty habit of being false at startup. That's not
82 fgSetBool("/controls/gear-down", true);
85 // Not the worlds safest parser. But it's short & sweet.
86 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
88 XMLAttributes* a = (XMLAttributes*)&atts;
92 if(eq(name, "airplane")) {
93 _airplane.setWeight(attrf(a, "mass") * LBS2KG);
94 } else if(eq(name, "approach")) {
95 float spd = attrf(a, "speed") * KTS2MPS;
96 float alt = attrf(a, "alt", 0) * FT2M;
97 float aoa = attrf(a, "aoa", 0) * DEG2RAD;
98 _airplane.setApproach(spd, alt, aoa);
100 } else if(eq(name, "cruise")) {
101 float spd = attrf(a, "speed") * KTS2MPS;
102 float alt = attrf(a, "alt") * FT2M;
103 _airplane.setCruise(spd, alt);
105 } else if(eq(name, "cockpit")) {
106 v[0] = attrf(a, "x");
107 v[1] = attrf(a, "y");
108 v[2] = attrf(a, "z");
109 _airplane.setPilotPos(v);
110 } else if(eq(name, "wing")) {
111 _airplane.setWing(parseWing(a, name));
112 } else if(eq(name, "hstab")) {
113 _airplane.setTail(parseWing(a, name));
114 } else if(eq(name, "vstab")) {
115 _airplane.addVStab(parseWing(a, name));
116 } else if(eq(name, "propeller")) {
118 } else if(eq(name, "thruster")) {
119 SimpleJet* j = new SimpleJet();
121 v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
123 _airplane.addThruster(j, 0, v);
124 v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
126 j->setThrust(attrf(a, "thrust") * LBS2N);
127 } else if(eq(name, "jet")) {
130 v[0] = attrf(a, "x");
131 v[1] = attrf(a, "y");
132 v[2] = attrf(a, "z");
133 float mass = attrf(a, "mass") * LBS2KG;
134 j->setMaxThrust(attrf(a, "thrust") * LBS2N,
135 attrf(a, "afterburner", 0) * LBS2N);
136 j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
138 float n1min = attrf(a, "n1-idle", 55);
139 float n1max = attrf(a, "n1-max", 102);
140 float n2min = attrf(a, "n2-idle", 73);
141 float n2max = attrf(a, "n2-max", 103);
142 j->setRPMs(n1min, n1max, n2min, n2max);
144 if(a->hasAttribute("tsfc")) j->setTSFC(attrf(a, "tsfc"));
145 if(a->hasAttribute("egt")) j->setEGT(attrf(a, "egt"));
146 if(a->hasAttribute("epr")) j->setEPR(attrf(a, "epr"));
147 if(a->hasAttribute("exhaust-speed"))
148 j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
151 _airplane.addThruster(j, mass, v);
152 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
153 EngRec* er = new EngRec();
155 er->prefix = dup(buf);
157 } else if(eq(name, "gear")) {
158 Gear* g = new Gear();
160 v[0] = attrf(a, "x");
161 v[1] = attrf(a, "y");
162 v[2] = attrf(a, "z");
166 v[2] = attrf(a, "compression", 1);
167 g->setCompression(v);
168 g->setStaticFriction(attrf(a, "sfric", 0.8));
169 g->setDynamicFriction(attrf(a, "dfric", 0.7));
170 if(a->hasAttribute("castering"))
171 g->setCastering(true);
172 float transitionTime = attrf(a, "retract-time", 0);
173 _airplane.addGear(g, transitionTime);
174 } else if(eq(name, "fuselage")) {
176 v[0] = attrf(a, "ax");
177 v[1] = attrf(a, "ay");
178 v[2] = attrf(a, "az");
179 b[0] = attrf(a, "bx");
180 b[1] = attrf(a, "by");
181 b[2] = attrf(a, "bz");
182 float taper = attrf(a, "taper", 1);
183 float mid = attrf(a, "midpoint", 0.5);
184 _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid);
185 } else if(eq(name, "tank")) {
186 v[0] = attrf(a, "x");
187 v[1] = attrf(a, "y");
188 v[2] = attrf(a, "z");
189 float density = 6.0; // gasoline, in lbs/gal
190 if(a->hasAttribute("jet")) density = 6.72;
191 density *= LBS2KG*CM2GALS;
192 _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
193 } else if(eq(name, "ballast")) {
194 v[0] = attrf(a, "x");
195 v[1] = attrf(a, "y");
196 v[2] = attrf(a, "z");
197 _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
198 } else if(eq(name, "weight")) {
200 } else if(eq(name, "stall")) {
201 Wing* w = (Wing*)_currObj;
202 w->setStall(attrf(a, "aoa") * DEG2RAD);
203 w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
204 w->setStallPeak(attrf(a, "peak", 1.5));
205 } else if(eq(name, "flap0")) {
206 ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
207 attrf(a, "lift"), attrf(a, "drag"));
208 } else if(eq(name, "flap1")) {
209 ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
210 attrf(a, "lift"), attrf(a, "drag"));
211 } else if(eq(name, "slat")) {
212 ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
213 attrf(a, "aoa"), attrf(a, "drag"));
214 } else if(eq(name, "spoiler")) {
215 ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
216 attrf(a, "lift"), attrf(a, "drag"));
217 } else if(eq(name, "actionpt")) {
218 v[0] = attrf(a, "x");
219 v[1] = attrf(a, "y");
220 v[2] = attrf(a, "z");
221 ((Thruster*)_currObj)->setPosition(v);
222 } else if(eq(name, "dir")) {
223 v[0] = attrf(a, "x");
224 v[1] = attrf(a, "y");
225 v[2] = attrf(a, "z");
226 ((Thruster*)_currObj)->setDirection(v);
227 } else if(eq(name, "control")) {
228 const char* axis = a->getValue("axis");
229 if(a->hasAttribute("output")) {
230 // assert: output type must match _currObj type!
231 const char* output = a->getValue("output");
233 opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
234 opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
235 opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
237 ControlMap* cm = _airplane.getControlMap();
238 if(a->hasAttribute("src0")) {
239 cm->addMapping(parseAxis(axis), parseOutput(output),
241 attrf(a, "src0"), attrf(a, "src1"),
242 attrf(a, "dst0"), attrf(a, "dst1"));
244 cm->addMapping(parseAxis(axis), parseOutput(output),
248 // assert: must be under a "cruise" or "approach" tag
249 float value = attrf(a, "value", 0);
251 _airplane.addCruiseControl(parseAxis(axis), value);
253 _airplane.addApproachControl(parseAxis(axis), value);
256 *(int*)0=0; // unexpected tag, boom
260 void FGFDM::getExternalInput(float dt)
263 ControlMap* cm = _airplane.getControlMap();
266 for(i=0; i<_axes.size(); i++) {
267 AxisRec* a = (AxisRec*)_axes.get(i);
268 float val = fgGetFloat(a->name, 0);
269 cm->setInput(a->handle, val);
274 for(i=0; i<_weights.size(); i++) {
275 WeightRec* wr = (WeightRec*)_weights.get(i);
276 _airplane.setWeight(wr->handle, fgGetFloat(wr->prop));
280 _airplane.setGearState(fgGetBool("/controls/gear-down"), dt);
283 void FGFDM::setOutputProperties()
287 float fuelDensity = 718.95; // default to gasoline: ~6 lb/gal
288 for(i=0; i<_airplane.numTanks(); i++) {
289 fuelDensity = _airplane.getFuelDensity(i);
290 sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
291 fgSetFloat(buf, CM2GALS*_airplane.getFuel(i)/fuelDensity);
294 for(i=0; i<_thrusters.size(); i++) {
295 EngRec* er = (EngRec*)_thrusters.get(i);
296 Thruster* t = er->eng;
298 sprintf(buf, "%s/fuel-flow-gph", er->prefix);
299 fgSetFloat(buf, (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
301 if(t->getPropEngine()) {
302 PropEngine* p = t->getPropEngine();
304 sprintf(buf, "%s/rpm", er->prefix);
305 fgSetFloat(buf, p->getOmega() / RPM2RAD);
308 if(t->getPistonEngine()) {
309 PistonEngine* p = t->getPistonEngine();
311 sprintf(buf, "%s/mp-osi", er->prefix);
312 fgSetFloat(buf, p->getMP() * (1/INHG2PA));
314 sprintf(buf, "%s/egt-degf", er->prefix);
315 fgSetFloat(buf, p->getEGT() * K2DEGF + 459.4);
319 Jet* j = t->getJet();
321 sprintf(buf, "%s/n1", er->prefix);
322 fgSetFloat(buf, j->getN1());
324 sprintf(buf, "%s/n2", er->prefix);
325 fgSetFloat(buf, j->getN2());
327 sprintf(buf, "%s/epr", er->prefix);
328 fgSetFloat(buf, j->getEPR());
330 sprintf(buf, "%s/egt-degf", er->prefix);
331 fgSetFloat(buf, j->getEGT() * K2DEGF + 459.4);
336 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
338 Wing* w = new Wing();
341 if(eq(type, "vstab"))
347 pos[0] = attrf(a, "x");
348 pos[1] = attrf(a, "y");
349 pos[2] = attrf(a, "z");
352 w->setLength(attrf(a, "length"));
353 w->setChord(attrf(a, "chord"));
354 w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
355 w->setTaper(attrf(a, "taper", 1));
356 w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
357 w->setCamber(attrf(a, "camber", 0));
358 w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
360 float effect = attrf(a, "effectiveness", 1);
361 w->setDragScale(w->getDragScale()*effect);
367 void FGFDM::parsePropeller(XMLAttributes* a)
370 cg[0] = attrf(a, "x");
371 cg[1] = attrf(a, "y");
372 cg[2] = attrf(a, "z");
373 float mass = attrf(a, "mass") * LBS2KG;
374 float moment = attrf(a, "moment");
375 float radius = attrf(a, "radius");
376 float speed = attrf(a, "cruise-speed") * KTS2MPS;
377 float omega = attrf(a, "cruise-rpm") * RPM2RAD;
378 float power = attrf(a, "cruise-power") * HP2W;
379 float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
381 // Hack, fix this pronto:
382 float engP = attrf(a, "eng-power") * HP2W;
383 float engS = attrf(a, "eng-rpm") * RPM2RAD;
385 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
386 PistonEngine* eng = new PistonEngine(engP, engS);
387 PropEngine* thruster = new PropEngine(prop, eng, moment);
388 _airplane.addThruster(thruster, mass, cg);
390 if(a->hasAttribute("displacement"))
391 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
393 if(a->hasAttribute("compression"))
394 eng->setCompression(attrf(a, "compression"));
396 if(a->hasAttribute("turbo-mul")) {
397 float mul = attrf(a, "turbo-mul");
398 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
399 eng->setTurboParams(mul, mp);
402 if(a->hasAttribute("takeoff-power")) {
403 float power0 = attrf(a, "takeoff-power") * HP2W;
404 float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
405 prop->setTakeoff(omega0, power0);
408 if(a->hasAttribute("max-rpm")) {
409 float max = attrf(a, "max-rpm") * RPM2RAD;
410 float min = attrf(a, "min-rpm") * RPM2RAD;
411 thruster->setVariableProp(min, max);
415 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
416 EngRec* er = new EngRec();
418 er->prefix = dup(buf);
424 // Turns a string axis name into an integer for use by the
425 // ControlMap. Creates a new axis if this one hasn't been defined
427 int FGFDM::parseAxis(const char* name)
430 for(i=0; i<_axes.size(); i++) {
431 AxisRec* a = (AxisRec*)_axes.get(i);
432 if(eq(a->name, name))
436 // Not there, make a new one.
437 AxisRec* a = new AxisRec();
439 a->handle = _airplane.getControlMap()->newInput();
444 int FGFDM::parseOutput(const char* name)
446 if(eq(name, "THROTTLE")) return ControlMap::THROTTLE;
447 if(eq(name, "MIXTURE")) return ControlMap::MIXTURE;
448 if(eq(name, "STARTER")) return ControlMap::STARTER;
449 if(eq(name, "MAGNETOS")) return ControlMap::MAGNETOS;
450 if(eq(name, "ADVANCE")) return ControlMap::ADVANCE;
451 if(eq(name, "REHEAT")) return ControlMap::REHEAT;
452 if(eq(name, "BOOST")) return ControlMap::BOOST;
453 if(eq(name, "VECTOR")) return ControlMap::VECTOR;
454 if(eq(name, "PROP")) return ControlMap::PROP;
455 if(eq(name, "BRAKE")) return ControlMap::BRAKE;
456 if(eq(name, "STEER")) return ControlMap::STEER;
457 if(eq(name, "EXTEND")) return ControlMap::EXTEND;
458 if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
459 if(eq(name, "FLAP0")) return ControlMap::FLAP0;
460 if(eq(name, "FLAP1")) return ControlMap::FLAP1;
461 if(eq(name, "SLAT")) return ControlMap::SLAT;
462 if(eq(name, "SPOILER")) return ControlMap::SPOILER;
467 void FGFDM::parseWeight(XMLAttributes* a)
469 WeightRec* wr = new WeightRec();
472 v[0] = attrf(a, "x");
473 v[1] = attrf(a, "y");
474 v[2] = attrf(a, "z");
476 wr->prop = dup(a->getValue("mass-prop"));
477 wr->size = attrf(a, "size", 0);
478 wr->handle = _airplane.addWeight(v, wr->size);
483 bool FGFDM::eq(const char* a, const char* b)
485 // Figure it out for yourself. :)
486 while(*a && *b && *a++ == *b++);
490 char* FGFDM::dup(const char* s)
494 char* s2 = new char[len+1];
496 while((*p++ = *s++));
501 int FGFDM::attri(XMLAttributes* atts, char* attr)
503 if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
504 return attri(atts, attr, 0);
507 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
509 const char* val = atts->getValue(attr);
510 if(val == 0) return def;
511 else return atol(val);
514 float FGFDM::attrf(XMLAttributes* atts, char* attr)
516 if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
517 return attrf(atts, attr, 0);
520 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
522 const char* val = atts->getValue(attr);
523 if(val == 0) return def;
524 else return (float)atof(val);
527 }; // namespace yasim