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->setBrake(attrf(a, "skid", 0));
169 g->setStaticFriction(attrf(a, "sfric", 0.8));
170 g->setDynamicFriction(attrf(a, "dfric", 0.7));
171 if(a->hasAttribute("castering"))
172 g->setCastering(true);
173 float transitionTime = attrf(a, "retract-time", 0);
174 _airplane.addGear(g, transitionTime);
175 } else if(eq(name, "fuselage")) {
177 v[0] = attrf(a, "ax");
178 v[1] = attrf(a, "ay");
179 v[2] = attrf(a, "az");
180 b[0] = attrf(a, "bx");
181 b[1] = attrf(a, "by");
182 b[2] = attrf(a, "bz");
183 float taper = attrf(a, "taper", 1);
184 float mid = attrf(a, "midpoint", 0.5);
185 _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid);
186 } else if(eq(name, "tank")) {
187 v[0] = attrf(a, "x");
188 v[1] = attrf(a, "y");
189 v[2] = attrf(a, "z");
190 float density = 6.0; // gasoline, in lbs/gal
191 if(a->hasAttribute("jet")) density = 6.72;
192 density *= LBS2KG*CM2GALS;
193 _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
194 } else if(eq(name, "ballast")) {
195 v[0] = attrf(a, "x");
196 v[1] = attrf(a, "y");
197 v[2] = attrf(a, "z");
198 _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
199 } else if(eq(name, "weight")) {
201 } else if(eq(name, "stall")) {
202 Wing* w = (Wing*)_currObj;
203 w->setStall(attrf(a, "aoa") * DEG2RAD);
204 w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
205 w->setStallPeak(attrf(a, "peak", 1.5));
206 } else if(eq(name, "flap0")) {
207 ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
208 attrf(a, "lift"), attrf(a, "drag"));
209 } else if(eq(name, "flap1")) {
210 ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
211 attrf(a, "lift"), attrf(a, "drag"));
212 } else if(eq(name, "slat")) {
213 ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
214 attrf(a, "aoa"), attrf(a, "drag"));
215 } else if(eq(name, "spoiler")) {
216 ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
217 attrf(a, "lift"), attrf(a, "drag"));
218 } else if(eq(name, "actionpt")) {
219 v[0] = attrf(a, "x");
220 v[1] = attrf(a, "y");
221 v[2] = attrf(a, "z");
222 ((Thruster*)_currObj)->setPosition(v);
223 } else if(eq(name, "dir")) {
224 v[0] = attrf(a, "x");
225 v[1] = attrf(a, "y");
226 v[2] = attrf(a, "z");
227 ((Thruster*)_currObj)->setDirection(v);
228 } else if(eq(name, "control")) {
229 const char* axis = a->getValue("axis");
230 if(a->hasAttribute("output")) {
231 // assert: output type must match _currObj type!
232 const char* output = a->getValue("output");
234 opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
235 opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
236 opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
238 ControlMap* cm = _airplane.getControlMap();
239 if(a->hasAttribute("src0")) {
240 cm->addMapping(parseAxis(axis), parseOutput(output),
242 attrf(a, "src0"), attrf(a, "src1"),
243 attrf(a, "dst0"), attrf(a, "dst1"));
245 cm->addMapping(parseAxis(axis), parseOutput(output),
249 // assert: must be under a "cruise" or "approach" tag
250 float value = attrf(a, "value", 0);
252 _airplane.addCruiseControl(parseAxis(axis), value);
254 _airplane.addApproachControl(parseAxis(axis), value);
257 *(int*)0=0; // unexpected tag, boom
261 void FGFDM::getExternalInput(float dt)
264 ControlMap* cm = _airplane.getControlMap();
267 for(i=0; i<_axes.size(); i++) {
268 AxisRec* a = (AxisRec*)_axes.get(i);
269 float val = fgGetFloat(a->name, 0);
270 cm->setInput(a->handle, val);
275 for(i=0; i<_weights.size(); i++) {
276 WeightRec* wr = (WeightRec*)_weights.get(i);
277 _airplane.setWeight(wr->handle, fgGetFloat(wr->prop));
281 _airplane.setGearState(fgGetBool("/controls/gear-down"), dt);
284 void FGFDM::setOutputProperties()
288 float fuelDensity = 718.95; // default to gasoline: ~6 lb/gal
289 for(i=0; i<_airplane.numTanks(); i++) {
290 fuelDensity = _airplane.getFuelDensity(i);
291 sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
292 fgSetFloat(buf, CM2GALS*_airplane.getFuel(i)/fuelDensity);
295 for(i=0; i<_thrusters.size(); i++) {
296 EngRec* er = (EngRec*)_thrusters.get(i);
297 Thruster* t = er->eng;
299 sprintf(buf, "%s/fuel-flow-gph", er->prefix);
300 fgSetFloat(buf, (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
302 if(t->getPropEngine()) {
303 PropEngine* p = t->getPropEngine();
305 sprintf(buf, "%s/rpm", er->prefix);
306 fgSetFloat(buf, p->getOmega() / RPM2RAD);
309 if(t->getPistonEngine()) {
310 PistonEngine* p = t->getPistonEngine();
312 sprintf(buf, "%s/mp-osi", er->prefix);
313 fgSetFloat(buf, p->getMP() * (1/INHG2PA));
315 sprintf(buf, "%s/egt-degf", er->prefix);
316 fgSetFloat(buf, p->getEGT() * K2DEGF + 459.4);
320 Jet* j = t->getJet();
322 sprintf(buf, "%s/n1", er->prefix);
323 fgSetFloat(buf, j->getN1());
325 sprintf(buf, "%s/n2", er->prefix);
326 fgSetFloat(buf, j->getN2());
328 sprintf(buf, "%s/epr", er->prefix);
329 fgSetFloat(buf, j->getEPR());
331 sprintf(buf, "%s/egt-degf", er->prefix);
332 fgSetFloat(buf, j->getEGT() * K2DEGF + 459.4);
337 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
339 Wing* w = new Wing();
342 if(eq(type, "vstab"))
348 pos[0] = attrf(a, "x");
349 pos[1] = attrf(a, "y");
350 pos[2] = attrf(a, "z");
353 w->setLength(attrf(a, "length"));
354 w->setChord(attrf(a, "chord"));
355 w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
356 w->setTaper(attrf(a, "taper", 1));
357 w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
358 w->setCamber(attrf(a, "camber", 0));
359 w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
361 float effect = attrf(a, "effectiveness", 1);
362 w->setDragScale(w->getDragScale()*effect);
368 void FGFDM::parsePropeller(XMLAttributes* a)
371 cg[0] = attrf(a, "x");
372 cg[1] = attrf(a, "y");
373 cg[2] = attrf(a, "z");
374 float mass = attrf(a, "mass") * LBS2KG;
375 float moment = attrf(a, "moment");
376 float radius = attrf(a, "radius");
377 float speed = attrf(a, "cruise-speed") * KTS2MPS;
378 float omega = attrf(a, "cruise-rpm") * RPM2RAD;
379 float power = attrf(a, "cruise-power") * HP2W;
380 float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
382 // Hack, fix this pronto:
383 float engP = attrf(a, "eng-power") * HP2W;
384 float engS = attrf(a, "eng-rpm") * RPM2RAD;
386 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
387 PistonEngine* eng = new PistonEngine(engP, engS);
388 PropEngine* thruster = new PropEngine(prop, eng, moment);
389 _airplane.addThruster(thruster, mass, cg);
391 if(a->hasAttribute("displacement"))
392 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
394 if(a->hasAttribute("compression"))
395 eng->setCompression(attrf(a, "compression"));
397 if(a->hasAttribute("turbo-mul")) {
398 float mul = attrf(a, "turbo-mul");
399 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
400 eng->setTurboParams(mul, mp);
403 if(a->hasAttribute("takeoff-power")) {
404 float power0 = attrf(a, "takeoff-power") * HP2W;
405 float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
406 prop->setTakeoff(omega0, power0);
409 if(a->hasAttribute("max-rpm")) {
410 float max = attrf(a, "max-rpm") * RPM2RAD;
411 float min = attrf(a, "min-rpm") * RPM2RAD;
412 thruster->setVariableProp(min, max);
416 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
417 EngRec* er = new EngRec();
419 er->prefix = dup(buf);
425 // Turns a string axis name into an integer for use by the
426 // ControlMap. Creates a new axis if this one hasn't been defined
428 int FGFDM::parseAxis(const char* name)
431 for(i=0; i<_axes.size(); i++) {
432 AxisRec* a = (AxisRec*)_axes.get(i);
433 if(eq(a->name, name))
437 // Not there, make a new one.
438 AxisRec* a = new AxisRec();
440 a->handle = _airplane.getControlMap()->newInput();
445 int FGFDM::parseOutput(const char* name)
447 if(eq(name, "THROTTLE")) return ControlMap::THROTTLE;
448 if(eq(name, "MIXTURE")) return ControlMap::MIXTURE;
449 if(eq(name, "STARTER")) return ControlMap::STARTER;
450 if(eq(name, "MAGNETOS")) return ControlMap::MAGNETOS;
451 if(eq(name, "ADVANCE")) return ControlMap::ADVANCE;
452 if(eq(name, "REHEAT")) return ControlMap::REHEAT;
453 if(eq(name, "BOOST")) return ControlMap::BOOST;
454 if(eq(name, "VECTOR")) return ControlMap::VECTOR;
455 if(eq(name, "PROP")) return ControlMap::PROP;
456 if(eq(name, "BRAKE")) return ControlMap::BRAKE;
457 if(eq(name, "STEER")) return ControlMap::STEER;
458 if(eq(name, "EXTEND")) return ControlMap::EXTEND;
459 if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
460 if(eq(name, "FLAP0")) return ControlMap::FLAP0;
461 if(eq(name, "FLAP1")) return ControlMap::FLAP1;
462 if(eq(name, "SLAT")) return ControlMap::SLAT;
463 if(eq(name, "SPOILER")) return ControlMap::SPOILER;
468 void FGFDM::parseWeight(XMLAttributes* a)
470 WeightRec* wr = new WeightRec();
473 v[0] = attrf(a, "x");
474 v[1] = attrf(a, "y");
475 v[2] = attrf(a, "z");
477 wr->prop = dup(a->getValue("mass-prop"));
478 wr->size = attrf(a, "size", 0);
479 wr->handle = _airplane.addWeight(v, wr->size);
484 bool FGFDM::eq(const char* a, const char* b)
486 // Figure it out for yourself. :)
487 while(*a && *b && *a++ == *b++);
491 char* FGFDM::dup(const char* s)
495 char* s2 = new char[len+1];
497 while((*p++ = *s++));
502 int FGFDM::attri(XMLAttributes* atts, char* attr)
504 if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
505 return attri(atts, attr, 0);
508 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
510 const char* val = atts->getValue(attr);
511 if(val == 0) return def;
512 else return atol(val);
515 float FGFDM::attrf(XMLAttributes* atts, char* attr)
517 if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
518 return attrf(atts, attr, 0);
521 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
523 const char* val = atts->getValue(attr);
524 if(val == 0) return def;
525 else return (float)atof(val);
528 }; // namespace yasim