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 if(a->hasAttribute("tsfc")) j->setTSFC(attrf(a, "tsfc"));
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 _airplane.addGear(g);
179 } else if(eq(name, "fuselage")) {
181 v[0] = attrf(a, "ax");
182 v[1] = attrf(a, "ay");
183 v[2] = attrf(a, "az");
184 b[0] = attrf(a, "bx");
185 b[1] = attrf(a, "by");
186 b[2] = attrf(a, "bz");
187 float taper = attrf(a, "taper", 1);
188 float mid = attrf(a, "midpoint", 0.5);
189 _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid);
190 } else if(eq(name, "tank")) {
191 v[0] = attrf(a, "x");
192 v[1] = attrf(a, "y");
193 v[2] = attrf(a, "z");
194 float density = 6.0; // gasoline, in lbs/gal
195 if(a->hasAttribute("jet")) density = 6.72;
196 density *= LBS2KG*CM2GALS;
197 _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
198 } else if(eq(name, "ballast")) {
199 v[0] = attrf(a, "x");
200 v[1] = attrf(a, "y");
201 v[2] = attrf(a, "z");
202 _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
203 } else if(eq(name, "weight")) {
205 } else if(eq(name, "stall")) {
206 Wing* w = (Wing*)_currObj;
207 w->setStall(attrf(a, "aoa") * DEG2RAD);
208 w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
209 w->setStallPeak(attrf(a, "peak", 1.5));
210 } else if(eq(name, "flap0")) {
211 ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
212 attrf(a, "lift"), attrf(a, "drag"));
213 } else if(eq(name, "flap1")) {
214 ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
215 attrf(a, "lift"), attrf(a, "drag"));
216 } else if(eq(name, "slat")) {
217 ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
218 attrf(a, "aoa"), attrf(a, "drag"));
219 } else if(eq(name, "spoiler")) {
220 ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
221 attrf(a, "lift"), attrf(a, "drag"));
222 } else if(eq(name, "actionpt")) {
223 v[0] = attrf(a, "x");
224 v[1] = attrf(a, "y");
225 v[2] = attrf(a, "z");
226 ((Thruster*)_currObj)->setPosition(v);
227 } else if(eq(name, "dir")) {
228 v[0] = attrf(a, "x");
229 v[1] = attrf(a, "y");
230 v[2] = attrf(a, "z");
231 ((Thruster*)_currObj)->setDirection(v);
232 } else if(eq(name, "control-setting")) {
233 // A cruise or approach control setting
234 const char* axis = a->getValue("axis");
235 float value = attrf(a, "value", 0);
237 _airplane.addCruiseControl(parseAxis(axis), value);
239 _airplane.addApproachControl(parseAxis(axis), value);
240 } else if(eq(name, "control-input")) {
242 // A mapping of input property to a control
243 int axis = parseAxis(a->getValue("axis"));
244 int control = parseOutput(a->getValue("control"));
246 opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
247 opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
248 opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
250 ControlMap* cm = _airplane.getControlMap();
251 if(a->hasAttribute("src0")) {
252 cm->addMapping(axis, control, _currObj, opt,
253 attrf(a, "src0"), attrf(a, "src1"),
254 attrf(a, "dst0"), attrf(a, "dst1"));
256 cm->addMapping(axis, control, _currObj, opt);
258 } else if(eq(name, "control-output")) {
259 // A property output for a control on the current object
260 ControlMap* cm = _airplane.getControlMap();
261 int type = parseOutput(a->getValue("control"));
262 int handle = cm->getOutputHandle(_currObj, type);
264 PropOut* p = new PropOut();
265 p->prop = fgGetNode(a->getValue("prop"), true);
268 p->left = !(a->hasAttribute("side") &&
269 eq("right", a->getValue("side")));
270 p->min = attrf(a, "min", cm->rangeMin(type));
271 p->max = attrf(a, "max", cm->rangeMax(type));
272 _controlProps.add(p);
274 } else if(eq(name, "control-speed")) {
275 ControlMap* cm = _airplane.getControlMap();
276 int type = parseOutput(a->getValue("control"));
277 int handle = cm->getOutputHandle(_currObj, type);
278 float time = attrf(a, "transition-time", 0);
280 cm->setTransitionTime(handle, time);
282 SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '"
283 << name << "' found in YASim aircraft description");
288 void FGFDM::getExternalInput(float dt)
291 ControlMap* cm = _airplane.getControlMap();
294 for(i=0; i<_axes.size(); i++) {
295 AxisRec* a = (AxisRec*)_axes.get(i);
296 float val = fgGetFloat(a->name, 0);
297 cm->setInput(a->handle, val);
299 cm->applyControls(dt);
302 for(i=0; i<_weights.size(); i++) {
303 WeightRec* wr = (WeightRec*)_weights.get(i);
304 _airplane.setWeight(wr->handle, fgGetFloat(wr->prop));
308 void FGFDM::setOutputProperties()
313 float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
314 fgSetFloat("/yasim/gross-weight-lbs", grossWgt);
316 ControlMap* cm = _airplane.getControlMap();
317 for(i=0; i<_controlProps.size(); i++) {
318 PropOut* p = (PropOut*)_controlProps.get(i);
320 ? cm->getOutput(p->handle)
321 : cm->getOutputR(p->handle));
322 float rmin = cm->rangeMin(p->type);
323 float rmax = cm->rangeMax(p->type);
324 float frac = (val - rmin) / (rmax - rmin);
325 val = frac*(p->max - p->min) + p->min;
326 p->prop->setFloatValue(val);
329 float fuelDensity = 718.95; // default to gasoline: ~6 lb/gal
330 for(i=0; i<_airplane.numTanks(); i++) {
331 fuelDensity = _airplane.getFuelDensity(i);
332 sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
333 fgSetFloat(buf, CM2GALS*_airplane.getFuel(i)/fuelDensity);
336 for(i=0; i<_thrusters.size(); i++) {
337 EngRec* er = (EngRec*)_thrusters.get(i);
338 Thruster* t = er->eng;
340 sprintf(buf, "%s/fuel-flow-gph", er->prefix);
341 fgSetFloat(buf, (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
343 if(t->getPropEngine()) {
344 PropEngine* p = t->getPropEngine();
346 sprintf(buf, "%s/rpm", er->prefix);
347 fgSetFloat(buf, p->getOmega() / RPM2RAD);
350 if(t->getPistonEngine()) {
351 PistonEngine* p = t->getPistonEngine();
353 sprintf(buf, "%s/mp-osi", er->prefix);
354 fgSetFloat(buf, p->getMP() * (1/INHG2PA));
356 sprintf(buf, "%s/egt-degf", er->prefix);
357 fgSetFloat(buf, p->getEGT() * K2DEGF + 459.4);
361 Jet* j = t->getJet();
363 sprintf(buf, "%s/n1", er->prefix);
364 fgSetFloat(buf, j->getN1());
366 sprintf(buf, "%s/n2", er->prefix);
367 fgSetFloat(buf, j->getN2());
369 sprintf(buf, "%s/epr", er->prefix);
370 fgSetFloat(buf, j->getEPR());
372 sprintf(buf, "%s/egt-degf", er->prefix);
373 fgSetFloat(buf, j->getEGT() * K2DEGF + 459.4);
378 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
380 Wing* w = new Wing();
383 if(eq(type, "vstab"))
389 pos[0] = attrf(a, "x");
390 pos[1] = attrf(a, "y");
391 pos[2] = attrf(a, "z");
394 w->setLength(attrf(a, "length"));
395 w->setChord(attrf(a, "chord"));
396 w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
397 w->setTaper(attrf(a, "taper", 1));
398 w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
399 w->setCamber(attrf(a, "camber", 0));
400 w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
402 float effect = attrf(a, "effectiveness", 1);
403 w->setDragScale(w->getDragScale()*effect);
409 void FGFDM::parsePropeller(XMLAttributes* a)
412 cg[0] = attrf(a, "x");
413 cg[1] = attrf(a, "y");
414 cg[2] = attrf(a, "z");
415 float mass = attrf(a, "mass") * LBS2KG;
416 float moment = attrf(a, "moment");
417 float radius = attrf(a, "radius");
418 float speed = attrf(a, "cruise-speed") * KTS2MPS;
419 float omega = attrf(a, "cruise-rpm") * RPM2RAD;
420 float power = attrf(a, "cruise-power") * HP2W;
421 float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
423 // Hack, fix this pronto:
424 float engP = attrf(a, "eng-power") * HP2W;
425 float engS = attrf(a, "eng-rpm") * RPM2RAD;
427 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
428 PistonEngine* eng = new PistonEngine(engP, engS);
429 PropEngine* thruster = new PropEngine(prop, eng, moment);
430 _airplane.addThruster(thruster, mass, cg);
432 if(a->hasAttribute("displacement"))
433 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
435 if(a->hasAttribute("compression"))
436 eng->setCompression(attrf(a, "compression"));
438 if(a->hasAttribute("turbo-mul")) {
439 float mul = attrf(a, "turbo-mul");
440 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
441 eng->setTurboParams(mul, mp);
444 if(a->hasAttribute("takeoff-power")) {
445 float power0 = attrf(a, "takeoff-power") * HP2W;
446 float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
447 prop->setTakeoff(omega0, power0);
450 if(a->hasAttribute("max-rpm")) {
451 float max = attrf(a, "max-rpm") * RPM2RAD;
452 float min = attrf(a, "min-rpm") * RPM2RAD;
453 thruster->setVariableProp(min, max);
457 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
458 EngRec* er = new EngRec();
460 er->prefix = dup(buf);
466 // Turns a string axis name into an integer for use by the
467 // ControlMap. Creates a new axis if this one hasn't been defined
469 int FGFDM::parseAxis(const char* name)
472 for(i=0; i<_axes.size(); i++) {
473 AxisRec* a = (AxisRec*)_axes.get(i);
474 if(eq(a->name, name))
478 // Not there, make a new one.
479 AxisRec* a = new AxisRec();
481 a->handle = _airplane.getControlMap()->newInput();
486 int FGFDM::parseOutput(const char* name)
488 if(eq(name, "THROTTLE")) return ControlMap::THROTTLE;
489 if(eq(name, "MIXTURE")) return ControlMap::MIXTURE;
490 if(eq(name, "STARTER")) return ControlMap::STARTER;
491 if(eq(name, "MAGNETOS")) return ControlMap::MAGNETOS;
492 if(eq(name, "ADVANCE")) return ControlMap::ADVANCE;
493 if(eq(name, "REHEAT")) return ControlMap::REHEAT;
494 if(eq(name, "BOOST")) return ControlMap::BOOST;
495 if(eq(name, "VECTOR")) return ControlMap::VECTOR;
496 if(eq(name, "PROP")) return ControlMap::PROP;
497 if(eq(name, "BRAKE")) return ControlMap::BRAKE;
498 if(eq(name, "STEER")) return ControlMap::STEER;
499 if(eq(name, "EXTEND")) return ControlMap::EXTEND;
500 if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
501 if(eq(name, "FLAP0")) return ControlMap::FLAP0;
502 if(eq(name, "FLAP1")) return ControlMap::FLAP1;
503 if(eq(name, "SLAT")) return ControlMap::SLAT;
504 if(eq(name, "SPOILER")) return ControlMap::SPOILER;
505 if(eq(name, "CASTERING")) return ControlMap::CASTERING;
506 SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
507 << name << "' in YASim aircraft description.");
512 void FGFDM::parseWeight(XMLAttributes* a)
514 WeightRec* wr = new WeightRec();
517 v[0] = attrf(a, "x");
518 v[1] = attrf(a, "y");
519 v[2] = attrf(a, "z");
521 wr->prop = dup(a->getValue("mass-prop"));
522 wr->size = attrf(a, "size", 0);
523 wr->handle = _airplane.addWeight(v, wr->size);
528 bool FGFDM::eq(const char* a, const char* b)
530 // Figure it out for yourself. :)
531 while(*a && *b && *a == *b) { a++; b++; }
535 char* FGFDM::dup(const char* s)
539 char* s2 = new char[len+1];
541 while((*p++ = *s++));
546 int FGFDM::attri(XMLAttributes* atts, char* attr)
548 if(!atts->hasAttribute(attr)) {
549 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
550 "' in YASim aircraft description");
553 return attri(atts, attr, 0);
556 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
558 const char* val = atts->getValue(attr);
559 if(val == 0) return def;
560 else return atol(val);
563 float FGFDM::attrf(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 attrf(atts, attr, 0);
573 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
575 const char* val = atts->getValue(attr);
576 if(val == 0) return def;
577 else return (float)atof(val);
580 }; // namespace yasim