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);
60 for(i=0; i<_controlProps.size(); i++)
61 delete (PropOut*)_controlProps.get(i);
64 void FGFDM::iterate(float dt)
67 _airplane.iterate(dt);
68 setOutputProperties();
71 Airplane* FGFDM::getAirplane()
78 // Allows the user to start with something other than full fuel
79 _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
81 // This has a nasty habit of being false at startup. That's not
83 fgSetBool("/controls/gear-down", true);
86 // Not the worlds safest parser. But it's short & sweet.
87 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
89 XMLAttributes* a = (XMLAttributes*)&atts;
93 if(eq(name, "airplane")) {
94 _airplane.setWeight(attrf(a, "mass") * LBS2KG);
95 } else if(eq(name, "approach")) {
96 float spd = attrf(a, "speed") * KTS2MPS;
97 float alt = attrf(a, "alt", 0) * FT2M;
98 float aoa = attrf(a, "aoa", 0) * DEG2RAD;
99 _airplane.setApproach(spd, alt, aoa);
101 } else if(eq(name, "cruise")) {
102 float spd = attrf(a, "speed") * KTS2MPS;
103 float alt = attrf(a, "alt") * FT2M;
104 _airplane.setCruise(spd, alt);
106 } else if(eq(name, "cockpit")) {
107 v[0] = attrf(a, "x");
108 v[1] = attrf(a, "y");
109 v[2] = attrf(a, "z");
110 _airplane.setPilotPos(v);
111 } else if(eq(name, "wing")) {
112 _airplane.setWing(parseWing(a, name));
113 } else if(eq(name, "hstab")) {
114 _airplane.setTail(parseWing(a, name));
115 } else if(eq(name, "vstab")) {
116 _airplane.addVStab(parseWing(a, name));
117 } else if(eq(name, "propeller")) {
119 } else if(eq(name, "thruster")) {
120 SimpleJet* j = new SimpleJet();
122 v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
124 _airplane.addThruster(j, 0, v);
125 v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
127 j->setThrust(attrf(a, "thrust") * LBS2N);
128 } else if(eq(name, "jet")) {
131 v[0] = attrf(a, "x");
132 v[1] = attrf(a, "y");
133 v[2] = attrf(a, "z");
134 float mass = attrf(a, "mass") * LBS2KG;
135 j->setMaxThrust(attrf(a, "thrust") * LBS2N,
136 attrf(a, "afterburner", 0) * LBS2N);
137 j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
139 float n1min = attrf(a, "n1-idle", 55);
140 float n1max = attrf(a, "n1-max", 102);
141 float n2min = attrf(a, "n2-idle", 73);
142 float n2max = attrf(a, "n2-max", 103);
143 j->setRPMs(n1min, n1max, n2min, n2max);
145 if(a->hasAttribute("tsfc")) j->setTSFC(attrf(a, "tsfc"));
146 if(a->hasAttribute("egt")) j->setEGT(attrf(a, "egt"));
147 if(a->hasAttribute("epr")) j->setEPR(attrf(a, "epr"));
148 if(a->hasAttribute("exhaust-speed"))
149 j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
152 _airplane.addThruster(j, mass, v);
153 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
154 EngRec* er = new EngRec();
156 er->prefix = dup(buf);
158 } else if(eq(name, "gear")) {
159 Gear* g = new Gear();
161 v[0] = attrf(a, "x");
162 v[1] = attrf(a, "y");
163 v[2] = attrf(a, "z");
167 v[2] = attrf(a, "compression", 1);
168 g->setCompression(v);
169 g->setBrake(attrf(a, "skid", 0));
170 g->setStaticFriction(attrf(a, "sfric", 0.8));
171 g->setDynamicFriction(attrf(a, "dfric", 0.7));
172 if(a->hasAttribute("castering"))
173 g->setCastering(true);
174 _airplane.addGear(g);
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-setting")) {
229 // A cruise or approach control setting
230 const char* axis = a->getValue("axis");
231 float value = attrf(a, "value", 0);
233 _airplane.addCruiseControl(parseAxis(axis), value);
235 _airplane.addApproachControl(parseAxis(axis), value);
236 } else if(eq(name, "control-input")) {
238 // A mapping of input property to a control
239 int axis = parseAxis(a->getValue("axis"));
240 int control = parseOutput(a->getValue("control"));
242 opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
243 opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
244 opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
246 ControlMap* cm = _airplane.getControlMap();
247 if(a->hasAttribute("src0")) {
248 cm->addMapping(axis, control, _currObj, opt,
249 attrf(a, "src0"), attrf(a, "src1"),
250 attrf(a, "dst0"), attrf(a, "dst1"));
252 cm->addMapping(axis, control, _currObj, opt);
254 } else if(eq(name, "control-output")) {
255 // A property output for a control on the current object
256 ControlMap* cm = _airplane.getControlMap();
257 int type = parseOutput(a->getValue("control"));
258 int handle = cm->getOutputHandle(_currObj, type);
260 PropOut* p = new PropOut();
261 p->prop = fgGetNode(a->getValue("prop"), true);
264 p->left = !(a->hasAttribute("side") &&
265 eq("right", a->getValue("side")));
266 p->min = attrf(a, "min", cm->rangeMin(type));
267 p->max = attrf(a, "max", cm->rangeMax(type));
268 _controlProps.add(p);
270 } else if(eq(name, "control-speed")) {
271 ControlMap* cm = _airplane.getControlMap();
272 int type = parseOutput(a->getValue("control"));
273 int handle = cm->getOutputHandle(_currObj, type);
274 float time = attrf(a, "transition-time", 0);
276 cm->setTransitionTime(handle, time);
278 *(int*)0=0; // unexpected tag, boom
282 void FGFDM::getExternalInput(float dt)
285 ControlMap* cm = _airplane.getControlMap();
288 for(i=0; i<_axes.size(); i++) {
289 AxisRec* a = (AxisRec*)_axes.get(i);
290 float val = fgGetFloat(a->name, 0);
291 cm->setInput(a->handle, val);
293 cm->applyControls(dt);
296 for(i=0; i<_weights.size(); i++) {
297 WeightRec* wr = (WeightRec*)_weights.get(i);
298 _airplane.setWeight(wr->handle, fgGetFloat(wr->prop));
302 void FGFDM::setOutputProperties()
307 ControlMap* cm = _airplane.getControlMap();
308 for(i=0; i<_controlProps.size(); i++) {
309 PropOut* p = (PropOut*)_controlProps.get(i);
311 ? cm->getOutput(p->handle)
312 : cm->getOutputR(p->handle));
313 float rmin = cm->rangeMin(p->type);
314 float rmax = cm->rangeMax(p->type);
315 float frac = (val - rmin) / (rmax - rmin);
316 val = frac*(p->max - p->min) + p->min;
317 p->prop->setFloatValue(val);
320 float fuelDensity = 718.95; // default to gasoline: ~6 lb/gal
321 for(i=0; i<_airplane.numTanks(); i++) {
322 fuelDensity = _airplane.getFuelDensity(i);
323 sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
324 fgSetFloat(buf, CM2GALS*_airplane.getFuel(i)/fuelDensity);
327 for(i=0; i<_thrusters.size(); i++) {
328 EngRec* er = (EngRec*)_thrusters.get(i);
329 Thruster* t = er->eng;
331 sprintf(buf, "%s/fuel-flow-gph", er->prefix);
332 fgSetFloat(buf, (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
334 if(t->getPropEngine()) {
335 PropEngine* p = t->getPropEngine();
337 sprintf(buf, "%s/rpm", er->prefix);
338 fgSetFloat(buf, p->getOmega() / RPM2RAD);
341 if(t->getPistonEngine()) {
342 PistonEngine* p = t->getPistonEngine();
344 sprintf(buf, "%s/mp-osi", er->prefix);
345 fgSetFloat(buf, p->getMP() * (1/INHG2PA));
347 sprintf(buf, "%s/egt-degf", er->prefix);
348 fgSetFloat(buf, p->getEGT() * K2DEGF + 459.4);
352 Jet* j = t->getJet();
354 sprintf(buf, "%s/n1", er->prefix);
355 fgSetFloat(buf, j->getN1());
357 sprintf(buf, "%s/n2", er->prefix);
358 fgSetFloat(buf, j->getN2());
360 sprintf(buf, "%s/epr", er->prefix);
361 fgSetFloat(buf, j->getEPR());
363 sprintf(buf, "%s/egt-degf", er->prefix);
364 fgSetFloat(buf, j->getEGT() * K2DEGF + 459.4);
369 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
371 Wing* w = new Wing();
374 if(eq(type, "vstab"))
380 pos[0] = attrf(a, "x");
381 pos[1] = attrf(a, "y");
382 pos[2] = attrf(a, "z");
385 w->setLength(attrf(a, "length"));
386 w->setChord(attrf(a, "chord"));
387 w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
388 w->setTaper(attrf(a, "taper", 1));
389 w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
390 w->setCamber(attrf(a, "camber", 0));
391 w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
393 float effect = attrf(a, "effectiveness", 1);
394 w->setDragScale(w->getDragScale()*effect);
400 void FGFDM::parsePropeller(XMLAttributes* a)
403 cg[0] = attrf(a, "x");
404 cg[1] = attrf(a, "y");
405 cg[2] = attrf(a, "z");
406 float mass = attrf(a, "mass") * LBS2KG;
407 float moment = attrf(a, "moment");
408 float radius = attrf(a, "radius");
409 float speed = attrf(a, "cruise-speed") * KTS2MPS;
410 float omega = attrf(a, "cruise-rpm") * RPM2RAD;
411 float power = attrf(a, "cruise-power") * HP2W;
412 float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
414 // Hack, fix this pronto:
415 float engP = attrf(a, "eng-power") * HP2W;
416 float engS = attrf(a, "eng-rpm") * RPM2RAD;
418 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
419 PistonEngine* eng = new PistonEngine(engP, engS);
420 PropEngine* thruster = new PropEngine(prop, eng, moment);
421 _airplane.addThruster(thruster, mass, cg);
423 if(a->hasAttribute("displacement"))
424 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
426 if(a->hasAttribute("compression"))
427 eng->setCompression(attrf(a, "compression"));
429 if(a->hasAttribute("turbo-mul")) {
430 float mul = attrf(a, "turbo-mul");
431 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
432 eng->setTurboParams(mul, mp);
435 if(a->hasAttribute("takeoff-power")) {
436 float power0 = attrf(a, "takeoff-power") * HP2W;
437 float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
438 prop->setTakeoff(omega0, power0);
441 if(a->hasAttribute("max-rpm")) {
442 float max = attrf(a, "max-rpm") * RPM2RAD;
443 float min = attrf(a, "min-rpm") * RPM2RAD;
444 thruster->setVariableProp(min, max);
448 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
449 EngRec* er = new EngRec();
451 er->prefix = dup(buf);
457 // Turns a string axis name into an integer for use by the
458 // ControlMap. Creates a new axis if this one hasn't been defined
460 int FGFDM::parseAxis(const char* name)
463 for(i=0; i<_axes.size(); i++) {
464 AxisRec* a = (AxisRec*)_axes.get(i);
465 if(eq(a->name, name))
469 // Not there, make a new one.
470 AxisRec* a = new AxisRec();
472 a->handle = _airplane.getControlMap()->newInput();
477 int FGFDM::parseOutput(const char* name)
479 if(eq(name, "THROTTLE")) return ControlMap::THROTTLE;
480 if(eq(name, "MIXTURE")) return ControlMap::MIXTURE;
481 if(eq(name, "STARTER")) return ControlMap::STARTER;
482 if(eq(name, "MAGNETOS")) return ControlMap::MAGNETOS;
483 if(eq(name, "ADVANCE")) return ControlMap::ADVANCE;
484 if(eq(name, "REHEAT")) return ControlMap::REHEAT;
485 if(eq(name, "BOOST")) return ControlMap::BOOST;
486 if(eq(name, "VECTOR")) return ControlMap::VECTOR;
487 if(eq(name, "PROP")) return ControlMap::PROP;
488 if(eq(name, "BRAKE")) return ControlMap::BRAKE;
489 if(eq(name, "STEER")) return ControlMap::STEER;
490 if(eq(name, "EXTEND")) return ControlMap::EXTEND;
491 if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
492 if(eq(name, "FLAP0")) return ControlMap::FLAP0;
493 if(eq(name, "FLAP1")) return ControlMap::FLAP1;
494 if(eq(name, "SLAT")) return ControlMap::SLAT;
495 if(eq(name, "SPOILER")) return ControlMap::SPOILER;
499 void FGFDM::parseWeight(XMLAttributes* a)
501 WeightRec* wr = new WeightRec();
504 v[0] = attrf(a, "x");
505 v[1] = attrf(a, "y");
506 v[2] = attrf(a, "z");
508 wr->prop = dup(a->getValue("mass-prop"));
509 wr->size = attrf(a, "size", 0);
510 wr->handle = _airplane.addWeight(v, wr->size);
515 bool FGFDM::eq(const char* a, const char* b)
517 // Figure it out for yourself. :)
518 while(*a && *b && *a == *b) { a++; b++; }
522 char* FGFDM::dup(const char* s)
526 char* s2 = new char[len+1];
528 while((*p++ = *s++));
533 int FGFDM::attri(XMLAttributes* atts, char* attr)
535 if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
536 return attri(atts, attr, 0);
539 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
541 const char* val = atts->getValue(attr);
542 if(val == 0) return def;
543 else return atol(val);
546 float FGFDM::attrf(XMLAttributes* atts, char* attr)
548 if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
549 return attrf(atts, attr, 0);
552 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
554 const char* val = atts->getValue(attr);
555 if(val == 0) return def;
556 else return (float)atof(val);
559 }; // namespace yasim