4 #include <Main/fg_props.hxx>
8 #include "Atmosphere.hpp"
9 #include "PropEngine.hpp"
10 #include "Propeller.hpp"
11 #include "PistonEngine.hpp"
16 // Some conversion factors
17 static const float KTS2MPS = 0.514444444444;
18 static const float FT2M = 0.3048;
19 static const float DEG2RAD = 0.0174532925199;
20 static const float RPM2RAD = 0.10471975512;
21 static const float LBS2N = 4.44822;
22 static const float LBS2KG = 0.45359237;
23 static const float CM2GALS = 264.172037284;
24 static const float HP2W = 745.700;
25 static const float INHG2PA = 3386.389;
27 // Stubs, so that this can be compiled without the FlightGear
28 // binary. What's the best way to handle this?
30 // float fgGetFloat(char* name, float def) { return 0; }
31 // void fgSetFloat(char* name, float val) {}
41 for(i=0; i<_axes.size(); i++) {
42 AxisRec* a = (AxisRec*)_axes.get(i);
46 for(i=0; i<_pistons.size(); i++) {
47 EngRec* er = (EngRec*)_pistons.get(i);
49 delete (PropEngine*)er->eng;
52 for(i=0; i<_jets.size(); i++) {
53 EngRec* er = (EngRec*)_pistons.get(i);
58 for(i=0; i<_weights.size(); i++) {
59 WeightRec* wr = (WeightRec*)_weights.get(i);
66 void FGFDM::iterate(float dt)
69 _airplane.iterate(dt);
70 setOutputProperties();
73 Airplane* FGFDM::getAirplane()
80 // We don't want to use these ties (we set the values ourselves)
81 fgUntie("/consumables/fuel/tank[0]/level-gal_us");
82 fgUntie("/consumables/fuel/tank[1]/level-gal_us");
84 // Allows the user to start with something other than full fuel
85 _airplane.setFuelFraction(fgGetFloat("/yasim/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, "jet")) {
128 v[0] = attrf(a, "x");
129 v[1] = attrf(a, "y");
130 v[2] = attrf(a, "z");
131 float mass = attrf(a, "mass") * LBS2KG;
132 j->setDryThrust(attrf(a, "thrust") * LBS2N);
133 j->setReheatThrust(attrf(a, "afterburner", 0) * LBS2N);
135 _airplane.addThruster(j, mass, v);
136 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
137 EngRec* er = new EngRec();
139 er->prefix = dup(buf);
141 } else if(eq(name, "gear")) {
142 Gear* g = new Gear();
144 v[0] = attrf(a, "x");
145 v[1] = attrf(a, "y");
146 v[2] = attrf(a, "z");
150 v[2] = attrf(a, "compression", 1);
151 g->setCompression(v);
152 g->setStaticFriction(attrf(a, "sfric", 0.8));
153 g->setDynamicFriction(attrf(a, "dfric", 0.7));
154 float transitionTime = attrf(a, "retract-time", 0);
155 _airplane.addGear(g, transitionTime);
156 } else if(eq(name, "fuselage")) {
158 v[0] = attrf(a, "ax");
159 v[1] = attrf(a, "ay");
160 v[2] = attrf(a, "az");
161 b[0] = attrf(a, "bx");
162 b[1] = attrf(a, "by");
163 b[2] = attrf(a, "bz");
164 _airplane.addFuselage(v, b, attrf(a, "width"));
165 } else if(eq(name, "tank")) {
166 v[0] = attrf(a, "x");
167 v[1] = attrf(a, "y");
168 v[2] = attrf(a, "z");
169 float density = 6.0; // gasoline, in lbs/gal
170 if(a->hasAttribute("jet")) density = 6.72;
171 density *= LBS2KG/CM2GALS;
172 _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
173 } else if(eq(name, "ballast")) {
174 v[0] = attrf(a, "x");
175 v[1] = attrf(a, "y");
176 v[2] = attrf(a, "z");
177 _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
178 } else if(eq(name, "weight")) {
180 } else if(eq(name, "stall")) {
181 Wing* w = (Wing*)_currObj;
182 w->setStall(attrf(a, "aoa") * DEG2RAD);
183 w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
184 w->setStallPeak(attrf(a, "peak", 1.5));
185 } else if(eq(name, "flap0")) {
186 ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
187 attrf(a, "lift"), attrf(a, "drag"));
188 } else if(eq(name, "flap1")) {
189 ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
190 attrf(a, "lift"), attrf(a, "drag"));
191 } else if(eq(name, "slat")) {
192 ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
193 attrf(a, "aoa"), attrf(a, "drag"));
194 } else if(eq(name, "spoiler")) {
195 ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
196 attrf(a, "lift"), attrf(a, "drag"));
197 } else if(eq(name, "actionpt")) {
198 v[0] = attrf(a, "x");
199 v[1] = attrf(a, "y");
200 v[2] = attrf(a, "z");
201 ((Thruster*)_currObj)->setPosition(v);
202 } else if(eq(name, "dir")) {
203 v[0] = attrf(a, "x");
204 v[1] = attrf(a, "y");
205 v[2] = attrf(a, "z");
206 ((Thruster*)_currObj)->setDirection(v);
207 } else if(eq(name, "control")) {
208 const char* axis = a->getValue("axis");
209 if(a->hasAttribute("output")) {
210 // assert: output type must match _currObj type!
211 const char* output = a->getValue("output");
213 opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
214 opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
215 opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
216 _airplane.getControlMap()->addMapping(parseAxis(axis),
221 // assert: must be under a "cruise" or "approach" tag
222 float value = attrf(a, "value", 0);
224 _airplane.addCruiseControl(parseAxis(axis), value);
226 _airplane.addApproachControl(parseAxis(axis), value);
229 *(int*)0=0; // unexpected tag, boom
233 void FGFDM::getExternalInput(float dt)
236 ControlMap* cm = _airplane.getControlMap();
239 for(i=0; i<_axes.size(); i++) {
240 AxisRec* a = (AxisRec*)_axes.get(i);
241 float val = fgGetFloat(a->name, 0);
242 cm->setInput(a->handle, val);
247 for(i=0; i<_weights.size(); i++) {
248 WeightRec* wr = (WeightRec*)_weights.get(i);
249 _airplane.setWeight(wr->handle, fgGetFloat(wr->prop));
253 _airplane.setGearState(fgGetBool("/controls/gear-down"), dt);
256 void FGFDM::setOutputProperties()
260 for(i=0; i<_airplane.numTanks(); i++) {
261 sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
263 CM2GALS*_airplane.getFuel(i)/_airplane.getFuelDensity(i));
266 for(i=0; i<_pistons.size(); i++) {
267 EngRec* er = (EngRec*)_pistons.get(i);
268 PropEngine* p = (PropEngine*)er->eng;
270 sprintf(buf, "%s/rpm", er->prefix);
271 fgSetFloat(buf, p->getOmega() / RPM2RAD);
273 sprintf(buf, "%s/fuel-flow-gph", er->prefix);
274 fgSetFloat(buf, p->getFuelFlow() * (3600*2.2/5)); // FIXME, wrong
277 for(i=0; i<_jets.size(); i++) {
278 EngRec* er = (EngRec*)_jets.get(i);
279 Jet* j = (Jet*)er->eng;
281 sprintf(buf, "%s/fuel-flow-gph", er->prefix);
282 fgSetFloat(buf, j->getFuelFlow() * (3600*2.2/6)); // FIXME, wrong
286 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
288 Wing* w = new Wing();
291 if(eq(type, "vstab"))
297 pos[0] = attrf(a, "x");
298 pos[1] = attrf(a, "y");
299 pos[2] = attrf(a, "z");
302 w->setLength(attrf(a, "length"));
303 w->setChord(attrf(a, "chord"));
304 w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
305 w->setTaper(attrf(a, "taper", 1));
306 w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
307 w->setCamber(attrf(a, "camber", 0));
308 w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
310 float effect = attrf(a, "effectiveness", 1);
311 w->setDragScale(w->getDragScale()*effect);
317 void FGFDM::parsePropeller(XMLAttributes* a)
320 cg[0] = attrf(a, "x");
321 cg[1] = attrf(a, "y");
322 cg[2] = attrf(a, "z");
323 float mass = attrf(a, "mass") * LBS2KG;
324 float moment = attrf(a, "moment");
325 float radius = attrf(a, "radius");
326 float speed = attrf(a, "cruise-speed") * KTS2MPS;
327 float omega = attrf(a, "cruise-rpm") * RPM2RAD;
328 float power = attrf(a, "cruise-power") * HP2W;
329 float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
331 // Hack, fix this pronto:
332 float engP = attrf(a, "eng-power") * HP2W;
333 float engS = attrf(a, "eng-rpm") * RPM2RAD;
335 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
336 PistonEngine* eng = new PistonEngine(engP, engS);
337 PropEngine* thruster = new PropEngine(prop, eng, moment);
338 _airplane.addThruster(thruster, mass, cg);
340 if(a->hasAttribute("turbo-mul")) {
341 float mul = attrf(a, "turbo-mul");
342 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
343 eng->setTurboParams(mul, mp);
346 if(a->hasAttribute("takeoff-power")) {
347 float power0 = attrf(a, "takeoff-power") * HP2W;
348 float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
349 prop->setTakeoff(omega0, power0);
352 if(a->hasAttribute("max-rpm")) {
353 float max = attrf(a, "max-rpm") * RPM2RAD;
354 float min = attrf(a, "min-rpm") * RPM2RAD;
355 thruster->setVariableProp(min, max);
359 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
360 EngRec* er = new EngRec();
362 er->prefix = dup(buf);
368 // Turns a string axis name into an integer for use by the
369 // ControlMap. Creates a new axis if this one hasn't been defined
371 int FGFDM::parseAxis(const char* name)
374 for(i=0; i<_axes.size(); i++) {
375 AxisRec* a = (AxisRec*)_axes.get(i);
376 if(eq(a->name, name))
380 // Not there, make a new one.
381 AxisRec* a = new AxisRec();
383 a->handle = _airplane.getControlMap()->newInput();
388 int FGFDM::parseOutput(const char* name)
390 if(eq(name, "THROTTLE")) return ControlMap::THROTTLE;
391 if(eq(name, "MIXTURE")) return ControlMap::MIXTURE;
392 if(eq(name, "ADVANCE")) return ControlMap::ADVANCE;
393 if(eq(name, "REHEAT")) return ControlMap::REHEAT;
394 if(eq(name, "PROP")) return ControlMap::PROP;
395 if(eq(name, "BRAKE")) return ControlMap::BRAKE;
396 if(eq(name, "STEER")) return ControlMap::STEER;
397 if(eq(name, "EXTEND")) return ControlMap::EXTEND;
398 if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
399 if(eq(name, "FLAP0")) return ControlMap::FLAP0;
400 if(eq(name, "FLAP1")) return ControlMap::FLAP1;
401 if(eq(name, "SLAT")) return ControlMap::SLAT;
402 if(eq(name, "SPOILER")) return ControlMap::SPOILER;
407 void FGFDM::parseWeight(XMLAttributes* a)
409 WeightRec* wr = new WeightRec();
412 v[0] = attrf(a, "x");
413 v[1] = attrf(a, "y");
414 v[2] = attrf(a, "z");
416 wr->prop = dup(a->getValue("mass-prop"));
417 wr->size = attrf(a, "size", 0);
418 wr->handle = _airplane.addWeight(v, wr->size);
423 bool FGFDM::eq(const char* a, const char* b)
425 // Figure it out for yourself. :)
426 while(*a && *b && *a++ == *b++);
430 char* FGFDM::dup(const char* s)
434 char* s2 = new char[len+1];
436 while((*p++ = *s++));
441 int FGFDM::attri(XMLAttributes* atts, char* attr)
443 if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
444 return attri(atts, attr, 0);
447 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
449 const char* val = atts->getValue(attr);
450 if(val == 0) return def;
451 else return atol(val);
454 float FGFDM::attrf(XMLAttributes* atts, char* attr)
456 if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
457 return attrf(atts, attr, 0);
460 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
462 const char* val = atts->getValue(attr);
463 if(val == 0) return def;
464 else return (float)atof(val);
467 }; // namespace yasim