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) {}
40 // Map /controls/elevator to the approach elevator control. This
41 // should probably be settable, but there are very few aircraft
42 // who trim their approaches using things other than elevator.
43 _airplane.setElevatorControl(parseAxis("/controls/elevator"));
49 for(i=0; i<_axes.size(); i++) {
50 AxisRec* a = (AxisRec*)_axes.get(i);
54 for(i=0; i<_thrusters.size(); i++) {
55 EngRec* er = (EngRec*)_thrusters.get(i);
60 for(i=0; i<_weights.size(); i++) {
61 WeightRec* wr = (WeightRec*)_weights.get(i);
65 for(i=0; i<_controlProps.size(); i++)
66 delete (PropOut*)_controlProps.get(i);
69 void FGFDM::iterate(float dt)
72 _airplane.iterate(dt);
73 setOutputProperties();
76 Airplane* FGFDM::getAirplane()
83 // Allows the user to start with something other than full fuel
84 _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
86 // This has a nasty habit of being false at startup. That's not
88 fgSetBool("/controls/gear-down", true);
91 // Not the worlds safest parser. But it's short & sweet.
92 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
94 XMLAttributes* a = (XMLAttributes*)&atts;
98 if(eq(name, "airplane")) {
99 _airplane.setWeight(attrf(a, "mass") * LBS2KG);
100 } else if(eq(name, "approach")) {
101 float spd = attrf(a, "speed") * KTS2MPS;
102 float alt = attrf(a, "alt", 0) * FT2M;
103 float aoa = attrf(a, "aoa", 0) * DEG2RAD;
104 _airplane.setApproach(spd, alt, aoa);
106 } else if(eq(name, "cruise")) {
107 float spd = attrf(a, "speed") * KTS2MPS;
108 float alt = attrf(a, "alt") * FT2M;
109 _airplane.setCruise(spd, alt);
111 } else if(eq(name, "cockpit")) {
112 v[0] = attrf(a, "x");
113 v[1] = attrf(a, "y");
114 v[2] = attrf(a, "z");
115 _airplane.setPilotPos(v);
116 } else if(eq(name, "wing")) {
117 _airplane.setWing(parseWing(a, name));
118 } else if(eq(name, "hstab")) {
119 _airplane.setTail(parseWing(a, name));
120 } else if(eq(name, "vstab")) {
121 _airplane.addVStab(parseWing(a, name));
122 } else if(eq(name, "propeller")) {
124 } else if(eq(name, "thruster")) {
125 SimpleJet* j = new SimpleJet();
127 v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
129 _airplane.addThruster(j, 0, v);
130 v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
132 j->setThrust(attrf(a, "thrust") * LBS2N);
133 } else if(eq(name, "jet")) {
136 v[0] = attrf(a, "x");
137 v[1] = attrf(a, "y");
138 v[2] = attrf(a, "z");
139 float mass = attrf(a, "mass") * LBS2KG;
140 j->setMaxThrust(attrf(a, "thrust") * LBS2N,
141 attrf(a, "afterburner", 0) * LBS2N);
142 j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
144 float n1min = attrf(a, "n1-idle", 55);
145 float n1max = attrf(a, "n1-max", 102);
146 float n2min = attrf(a, "n2-idle", 73);
147 float n2max = attrf(a, "n2-max", 103);
148 j->setRPMs(n1min, n1max, n2min, n2max);
150 if(a->hasAttribute("tsfc")) j->setTSFC(attrf(a, "tsfc"));
151 if(a->hasAttribute("egt")) j->setEGT(attrf(a, "egt"));
152 if(a->hasAttribute("epr")) j->setEPR(attrf(a, "epr"));
153 if(a->hasAttribute("exhaust-speed"))
154 j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
157 _airplane.addThruster(j, mass, v);
158 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
159 EngRec* er = new EngRec();
161 er->prefix = dup(buf);
163 } else if(eq(name, "gear")) {
164 Gear* g = new Gear();
166 v[0] = attrf(a, "x");
167 v[1] = attrf(a, "y");
168 v[2] = attrf(a, "z");
172 v[2] = attrf(a, "compression", 1);
173 g->setCompression(v);
174 g->setBrake(attrf(a, "skid", 0));
175 g->setStaticFriction(attrf(a, "sfric", 0.8));
176 g->setDynamicFriction(attrf(a, "dfric", 0.7));
177 if(a->hasAttribute("castering"))
178 g->setCastering(true);
179 _airplane.addGear(g);
180 } else if(eq(name, "fuselage")) {
182 v[0] = attrf(a, "ax");
183 v[1] = attrf(a, "ay");
184 v[2] = attrf(a, "az");
185 b[0] = attrf(a, "bx");
186 b[1] = attrf(a, "by");
187 b[2] = attrf(a, "bz");
188 float taper = attrf(a, "taper", 1);
189 float mid = attrf(a, "midpoint", 0.5);
190 _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid);
191 } else if(eq(name, "tank")) {
192 v[0] = attrf(a, "x");
193 v[1] = attrf(a, "y");
194 v[2] = attrf(a, "z");
195 float density = 6.0; // gasoline, in lbs/gal
196 if(a->hasAttribute("jet")) density = 6.72;
197 density *= LBS2KG*CM2GALS;
198 _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
199 } else if(eq(name, "ballast")) {
200 v[0] = attrf(a, "x");
201 v[1] = attrf(a, "y");
202 v[2] = attrf(a, "z");
203 _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
204 } else if(eq(name, "weight")) {
206 } else if(eq(name, "stall")) {
207 Wing* w = (Wing*)_currObj;
208 w->setStall(attrf(a, "aoa") * DEG2RAD);
209 w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
210 w->setStallPeak(attrf(a, "peak", 1.5));
211 } else if(eq(name, "flap0")) {
212 ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
213 attrf(a, "lift"), attrf(a, "drag"));
214 } else if(eq(name, "flap1")) {
215 ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
216 attrf(a, "lift"), attrf(a, "drag"));
217 } else if(eq(name, "slat")) {
218 ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
219 attrf(a, "aoa"), attrf(a, "drag"));
220 } else if(eq(name, "spoiler")) {
221 ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
222 attrf(a, "lift"), attrf(a, "drag"));
223 } else if(eq(name, "actionpt")) {
224 v[0] = attrf(a, "x");
225 v[1] = attrf(a, "y");
226 v[2] = attrf(a, "z");
227 ((Thruster*)_currObj)->setPosition(v);
228 } else if(eq(name, "dir")) {
229 v[0] = attrf(a, "x");
230 v[1] = attrf(a, "y");
231 v[2] = attrf(a, "z");
232 ((Thruster*)_currObj)->setDirection(v);
233 } else if(eq(name, "control-setting")) {
234 // A cruise or approach control setting
235 const char* axis = a->getValue("axis");
236 float value = attrf(a, "value", 0);
238 _airplane.addCruiseControl(parseAxis(axis), value);
240 _airplane.addApproachControl(parseAxis(axis), value);
241 } else if(eq(name, "control-input")) {
243 // A mapping of input property to a control
244 int axis = parseAxis(a->getValue("axis"));
245 int control = parseOutput(a->getValue("control"));
247 opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
248 opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
249 opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
251 ControlMap* cm = _airplane.getControlMap();
252 if(a->hasAttribute("src0")) {
253 cm->addMapping(axis, control, _currObj, opt,
254 attrf(a, "src0"), attrf(a, "src1"),
255 attrf(a, "dst0"), attrf(a, "dst1"));
257 cm->addMapping(axis, control, _currObj, opt);
259 } else if(eq(name, "control-output")) {
260 // A property output for a control on the current object
261 ControlMap* cm = _airplane.getControlMap();
262 int type = parseOutput(a->getValue("control"));
263 int handle = cm->getOutputHandle(_currObj, type);
265 PropOut* p = new PropOut();
266 p->prop = fgGetNode(a->getValue("prop"), true);
269 p->left = !(a->hasAttribute("side") &&
270 eq("right", a->getValue("side")));
271 p->min = attrf(a, "min", cm->rangeMin(type));
272 p->max = attrf(a, "max", cm->rangeMax(type));
273 _controlProps.add(p);
275 } else if(eq(name, "control-speed")) {
276 ControlMap* cm = _airplane.getControlMap();
277 int type = parseOutput(a->getValue("control"));
278 int handle = cm->getOutputHandle(_currObj, type);
279 float time = attrf(a, "transition-time", 0);
281 cm->setTransitionTime(handle, time);
283 SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '"
284 << name << "' found in YASim aircraft description");
289 void FGFDM::getExternalInput(float dt)
292 ControlMap* cm = _airplane.getControlMap();
295 for(i=0; i<_axes.size(); i++) {
296 AxisRec* a = (AxisRec*)_axes.get(i);
297 float val = fgGetFloat(a->name, 0);
298 cm->setInput(a->handle, val);
300 cm->applyControls(dt);
303 for(i=0; i<_weights.size(); i++) {
304 WeightRec* wr = (WeightRec*)_weights.get(i);
305 _airplane.setWeight(wr->handle, fgGetFloat(wr->prop));
309 void FGFDM::setOutputProperties()
314 ControlMap* cm = _airplane.getControlMap();
315 for(i=0; i<_controlProps.size(); i++) {
316 PropOut* p = (PropOut*)_controlProps.get(i);
318 ? cm->getOutput(p->handle)
319 : cm->getOutputR(p->handle));
320 float rmin = cm->rangeMin(p->type);
321 float rmax = cm->rangeMax(p->type);
322 float frac = (val - rmin) / (rmax - rmin);
323 val = frac*(p->max - p->min) + p->min;
324 p->prop->setFloatValue(val);
327 float fuelDensity = 718.95; // default to gasoline: ~6 lb/gal
328 for(i=0; i<_airplane.numTanks(); i++) {
329 fuelDensity = _airplane.getFuelDensity(i);
330 sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
331 fgSetFloat(buf, CM2GALS*_airplane.getFuel(i)/fuelDensity);
334 for(i=0; i<_thrusters.size(); i++) {
335 EngRec* er = (EngRec*)_thrusters.get(i);
336 Thruster* t = er->eng;
338 sprintf(buf, "%s/fuel-flow-gph", er->prefix);
339 fgSetFloat(buf, (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
341 if(t->getPropEngine()) {
342 PropEngine* p = t->getPropEngine();
344 sprintf(buf, "%s/rpm", er->prefix);
345 fgSetFloat(buf, p->getOmega() / RPM2RAD);
348 if(t->getPistonEngine()) {
349 PistonEngine* p = t->getPistonEngine();
351 sprintf(buf, "%s/mp-osi", er->prefix);
352 fgSetFloat(buf, p->getMP() * (1/INHG2PA));
354 sprintf(buf, "%s/egt-degf", er->prefix);
355 fgSetFloat(buf, p->getEGT() * K2DEGF + 459.4);
359 Jet* j = t->getJet();
361 sprintf(buf, "%s/n1", er->prefix);
362 fgSetFloat(buf, j->getN1());
364 sprintf(buf, "%s/n2", er->prefix);
365 fgSetFloat(buf, j->getN2());
367 sprintf(buf, "%s/epr", er->prefix);
368 fgSetFloat(buf, j->getEPR());
370 sprintf(buf, "%s/egt-degf", er->prefix);
371 fgSetFloat(buf, j->getEGT() * K2DEGF + 459.4);
376 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
378 Wing* w = new Wing();
381 if(eq(type, "vstab"))
387 pos[0] = attrf(a, "x");
388 pos[1] = attrf(a, "y");
389 pos[2] = attrf(a, "z");
392 w->setLength(attrf(a, "length"));
393 w->setChord(attrf(a, "chord"));
394 w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
395 w->setTaper(attrf(a, "taper", 1));
396 w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
397 w->setCamber(attrf(a, "camber", 0));
398 w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
400 float effect = attrf(a, "effectiveness", 1);
401 w->setDragScale(w->getDragScale()*effect);
407 void FGFDM::parsePropeller(XMLAttributes* a)
410 cg[0] = attrf(a, "x");
411 cg[1] = attrf(a, "y");
412 cg[2] = attrf(a, "z");
413 float mass = attrf(a, "mass") * LBS2KG;
414 float moment = attrf(a, "moment");
415 float radius = attrf(a, "radius");
416 float speed = attrf(a, "cruise-speed") * KTS2MPS;
417 float omega = attrf(a, "cruise-rpm") * RPM2RAD;
418 float power = attrf(a, "cruise-power") * HP2W;
419 float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
421 // Hack, fix this pronto:
422 float engP = attrf(a, "eng-power") * HP2W;
423 float engS = attrf(a, "eng-rpm") * RPM2RAD;
425 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
426 PistonEngine* eng = new PistonEngine(engP, engS);
427 PropEngine* thruster = new PropEngine(prop, eng, moment);
428 _airplane.addThruster(thruster, mass, cg);
430 if(a->hasAttribute("displacement"))
431 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
433 if(a->hasAttribute("compression"))
434 eng->setCompression(attrf(a, "compression"));
436 if(a->hasAttribute("turbo-mul")) {
437 float mul = attrf(a, "turbo-mul");
438 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
439 eng->setTurboParams(mul, mp);
442 if(a->hasAttribute("takeoff-power")) {
443 float power0 = attrf(a, "takeoff-power") * HP2W;
444 float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
445 prop->setTakeoff(omega0, power0);
448 if(a->hasAttribute("max-rpm")) {
449 float max = attrf(a, "max-rpm") * RPM2RAD;
450 float min = attrf(a, "min-rpm") * RPM2RAD;
451 thruster->setVariableProp(min, max);
455 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
456 EngRec* er = new EngRec();
458 er->prefix = dup(buf);
464 // Turns a string axis name into an integer for use by the
465 // ControlMap. Creates a new axis if this one hasn't been defined
467 int FGFDM::parseAxis(const char* name)
470 for(i=0; i<_axes.size(); i++) {
471 AxisRec* a = (AxisRec*)_axes.get(i);
472 if(eq(a->name, name))
476 // Not there, make a new one.
477 AxisRec* a = new AxisRec();
479 a->handle = _airplane.getControlMap()->newInput();
484 int FGFDM::parseOutput(const char* name)
486 if(eq(name, "THROTTLE")) return ControlMap::THROTTLE;
487 if(eq(name, "MIXTURE")) return ControlMap::MIXTURE;
488 if(eq(name, "STARTER")) return ControlMap::STARTER;
489 if(eq(name, "MAGNETOS")) return ControlMap::MAGNETOS;
490 if(eq(name, "ADVANCE")) return ControlMap::ADVANCE;
491 if(eq(name, "REHEAT")) return ControlMap::REHEAT;
492 if(eq(name, "BOOST")) return ControlMap::BOOST;
493 if(eq(name, "VECTOR")) return ControlMap::VECTOR;
494 if(eq(name, "PROP")) return ControlMap::PROP;
495 if(eq(name, "BRAKE")) return ControlMap::BRAKE;
496 if(eq(name, "STEER")) return ControlMap::STEER;
497 if(eq(name, "EXTEND")) return ControlMap::EXTEND;
498 if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
499 if(eq(name, "FLAP0")) return ControlMap::FLAP0;
500 if(eq(name, "FLAP1")) return ControlMap::FLAP1;
501 if(eq(name, "SLAT")) return ControlMap::SLAT;
502 if(eq(name, "SPOILER")) return ControlMap::SPOILER;
503 SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
504 << name << "' in YASim aircraft description.");
509 void FGFDM::parseWeight(XMLAttributes* a)
511 WeightRec* wr = new WeightRec();
514 v[0] = attrf(a, "x");
515 v[1] = attrf(a, "y");
516 v[2] = attrf(a, "z");
518 wr->prop = dup(a->getValue("mass-prop"));
519 wr->size = attrf(a, "size", 0);
520 wr->handle = _airplane.addWeight(v, wr->size);
525 bool FGFDM::eq(const char* a, const char* b)
527 // Figure it out for yourself. :)
528 while(*a && *b && *a == *b) { a++; b++; }
532 char* FGFDM::dup(const char* s)
536 char* s2 = new char[len+1];
538 while((*p++ = *s++));
543 int FGFDM::attri(XMLAttributes* atts, char* attr)
545 if(!atts->hasAttribute(attr)) {
546 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
547 "' in YASim aircraft description");
550 return attri(atts, attr, 0);
553 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
555 const char* val = atts->getValue(attr);
556 if(val == 0) return def;
557 else return atol(val);
560 float FGFDM::attrf(XMLAttributes* atts, char* attr)
562 if(!atts->hasAttribute(attr)) {
563 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
564 "' in YASim aircraft description");
567 return attrf(atts, attr, 0);
570 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
572 const char* val = atts->getValue(attr);
573 if(val == 0) return def;
574 else return (float)atof(val);
577 }; // namespace yasim