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 // and this works only for the first piston engine anyway).
82 fgUntie("/engines/engine[0]/rpm");
83 fgUntie("/engines/engine[0]/egt-degf");
84 fgUntie("/engines/engine[0]/cht-degf");
85 fgUntie("/engines/engine[0]/oil-temperature-degf");
86 fgUntie("/engines/engine[0]/mp-osi");
87 fgUntie("/engines/engine[0]/fuel-flow-gph");
88 fgUntie("/engines/engine[0]/running");
89 fgUntie("/engines/engine[0]/cranking");
90 fgUntie("/consumables/fuel/tank[0]/level-gal_us");
91 fgUntie("/consumables/fuel/tank[1]/level-gal_us");
93 // Set these to sane values. We don't support engine start yet.
94 fgSetBool("/engines/engine[0]/running", true);
95 fgSetBool("/engines/engine[0]/cranking", false);
97 // Allows the user to start with something other than full fuel
98 _airplane.setFuelFraction(fgGetFloat("/yasim/fuel-fraction", 1));
100 // This has a nasty habit of being false at startup. That's not
102 fgSetBool("/controls/gear-down", true);
105 // Not the worlds safest parser. But it's short & sweet.
106 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
108 XMLAttributes* a = (XMLAttributes*)&atts;
112 if(eq(name, "airplane")) {
113 _airplane.setWeight(attrf(a, "mass") * LBS2KG);
114 } else if(eq(name, "approach")) {
115 float spd = attrf(a, "speed") * KTS2MPS;
116 float alt = attrf(a, "alt", 0) * FT2M;
117 float aoa = attrf(a, "aoa", 0) * DEG2RAD;
118 _airplane.setApproach(spd, alt, aoa);
120 } else if(eq(name, "cruise")) {
121 float spd = attrf(a, "speed") * KTS2MPS;
122 float alt = attrf(a, "alt") * FT2M;
123 _airplane.setCruise(spd, alt);
125 } else if(eq(name, "cockpit")) {
126 v[0] = attrf(a, "x");
127 v[1] = attrf(a, "y");
128 v[2] = attrf(a, "z");
129 _airplane.setPilotPos(v);
130 } else if(eq(name, "wing")) {
131 _airplane.setWing(parseWing(a, name));
132 } else if(eq(name, "hstab")) {
133 _airplane.setTail(parseWing(a, name));
134 } else if(eq(name, "vstab")) {
135 _airplane.addVStab(parseWing(a, name));
136 } else if(eq(name, "propeller")) {
138 } else if(eq(name, "jet")) {
141 v[0] = attrf(a, "x");
142 v[1] = attrf(a, "y");
143 v[2] = attrf(a, "z");
144 float mass = attrf(a, "mass") * LBS2KG;
145 j->setDryThrust(attrf(a, "thrust") * LBS2N);
146 j->setReheatThrust(attrf(a, "afterburner", 0) * LBS2N);
148 _airplane.addThruster(j, mass, v);
149 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
150 EngRec* er = new EngRec();
152 er->prefix = dup(buf);
154 } else if(eq(name, "gear")) {
155 Gear* g = new Gear();
157 v[0] = attrf(a, "x");
158 v[1] = attrf(a, "y");
159 v[2] = attrf(a, "z");
163 v[2] = attrf(a, "compression", 1);
164 g->setCompression(v);
165 g->setStaticFriction(attrf(a, "sfric", 0.8));
166 g->setDynamicFriction(attrf(a, "dfric", 0.7));
167 float transitionTime = attrf(a, "retract-time", 0);
168 _airplane.addGear(g, transitionTime);
169 } else if(eq(name, "fuselage")) {
171 v[0] = attrf(a, "ax");
172 v[1] = attrf(a, "ay");
173 v[2] = attrf(a, "az");
174 b[0] = attrf(a, "bx");
175 b[1] = attrf(a, "by");
176 b[2] = attrf(a, "bz");
177 _airplane.addFuselage(v, b, attrf(a, "width"));
178 } else if(eq(name, "tank")) {
179 v[0] = attrf(a, "x");
180 v[1] = attrf(a, "y");
181 v[2] = attrf(a, "z");
182 float density = 6.0; // gasoline, in lbs/gal
183 if(a->hasAttribute("jet")) density = 6.72;
184 density *= LBS2KG/CM2GALS;
185 _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
186 } else if(eq(name, "ballast")) {
187 v[0] = attrf(a, "x");
188 v[1] = attrf(a, "y");
189 v[2] = attrf(a, "z");
190 _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
191 } else if(eq(name, "weight")) {
193 } else if(eq(name, "stall")) {
194 Wing* w = (Wing*)_currObj;
195 w->setStall(attrf(a, "aoa") * DEG2RAD);
196 w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
197 w->setStallPeak(attrf(a, "peak", 1.5));
198 } else if(eq(name, "flap0")) {
199 ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
200 attrf(a, "lift"), attrf(a, "drag"));
201 } else if(eq(name, "flap1")) {
202 ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
203 attrf(a, "lift"), attrf(a, "drag"));
204 } else if(eq(name, "slat")) {
205 ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
206 attrf(a, "aoa"), attrf(a, "drag"));
207 } else if(eq(name, "spoiler")) {
208 ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
209 attrf(a, "lift"), attrf(a, "drag"));
210 } else if(eq(name, "actionpt")) {
211 v[0] = attrf(a, "x");
212 v[1] = attrf(a, "y");
213 v[2] = attrf(a, "z");
214 ((Thruster*)_currObj)->setPosition(v);
215 } else if(eq(name, "dir")) {
216 v[0] = attrf(a, "x");
217 v[1] = attrf(a, "y");
218 v[2] = attrf(a, "z");
219 ((Thruster*)_currObj)->setDirection(v);
220 } else if(eq(name, "control")) {
221 const char* axis = a->getValue("axis");
222 if(a->hasAttribute("output")) {
223 // assert: output type must match _currObj type!
224 const char* output = a->getValue("output");
226 opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
227 opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
228 opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
229 _airplane.getControlMap()->addMapping(parseAxis(axis),
234 // assert: must be under a "cruise" or "approach" tag
235 float value = attrf(a, "value", 0);
237 _airplane.addCruiseControl(parseAxis(axis), value);
239 _airplane.addApproachControl(parseAxis(axis), value);
242 *(int*)0=0; // unexpected tag, boom
246 void FGFDM::getExternalInput(float dt)
249 ControlMap* cm = _airplane.getControlMap();
252 for(i=0; i<_axes.size(); i++) {
253 AxisRec* a = (AxisRec*)_axes.get(i);
254 float val = fgGetFloat(a->name, 0);
255 cm->setInput(a->handle, val);
260 for(i=0; i<_weights.size(); i++) {
261 WeightRec* wr = (WeightRec*)_weights.get(i);
262 _airplane.setWeight(wr->handle, fgGetFloat(wr->prop));
266 _airplane.setGearState(fgGetBool("/controls/gear-down"), dt);
269 void FGFDM::setOutputProperties()
273 for(i=0; i<_airplane.numTanks(); i++) {
274 sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
276 CM2GALS*_airplane.getFuel(i)/_airplane.getFuelDensity(i));
279 for(i=0; i<_pistons.size(); i++) {
280 EngRec* er = (EngRec*)_pistons.get(i);
281 PropEngine* p = (PropEngine*)er->eng;
283 sprintf(buf, "%s/rpm", er->prefix);
284 fgSetFloat(buf, p->getOmega() / RPM2RAD);
286 sprintf(buf, "%s/fuel-flow-gph", er->prefix);
287 fgSetFloat(buf, p->getFuelFlow() * (3600*2.2/5)); // FIXME, wrong
290 for(i=0; i<_jets.size(); i++) {
291 EngRec* er = (EngRec*)_jets.get(i);
292 Jet* j = (Jet*)er->eng;
294 sprintf(buf, "%s/fuel-flow-gph", er->prefix);
295 fgSetFloat(buf, j->getFuelFlow() * (3600*2.2/6)); // FIXME, wrong
299 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
301 Wing* w = new Wing();
304 if(eq(type, "vstab"))
310 pos[0] = attrf(a, "x");
311 pos[1] = attrf(a, "y");
312 pos[2] = attrf(a, "z");
315 w->setLength(attrf(a, "length"));
316 w->setChord(attrf(a, "chord"));
317 w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
318 w->setTaper(attrf(a, "taper", 1));
319 w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
320 w->setCamber(attrf(a, "camber", 0));
321 w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
323 float effect = attrf(a, "effectiveness", 1);
324 w->setDragScale(w->getDragScale()*effect);
330 void FGFDM::parsePropeller(XMLAttributes* a)
333 cg[0] = attrf(a, "x");
334 cg[1] = attrf(a, "y");
335 cg[2] = attrf(a, "z");
336 float mass = attrf(a, "mass") * LBS2KG;
337 float moment = attrf(a, "moment");
338 float radius = attrf(a, "radius");
339 float speed = attrf(a, "cruise-speed") * KTS2MPS;
340 float omega = attrf(a, "cruise-rpm") * RPM2RAD;
341 float power = attrf(a, "cruise-power") * HP2W;
342 float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
344 // Hack, fix this pronto:
345 float engP = attrf(a, "eng-power") * HP2W;
346 float engS = attrf(a, "eng-rpm") * RPM2RAD;
348 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
349 PistonEngine* eng = new PistonEngine(engP, engS);
350 PropEngine* thruster = new PropEngine(prop, eng, moment);
351 _airplane.addThruster(thruster, mass, cg);
353 if(a->hasAttribute("turbo-mul")) {
354 float mul = attrf(a, "turbo-mul");
355 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
356 eng->setTurboParams(mul, mp);
359 if(a->hasAttribute("takeoff-power")) {
360 float power0 = attrf(a, "takeoff-power") * HP2W;
361 float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
362 prop->setTakeoff(omega0, power0);
365 if(a->hasAttribute("max-rpm")) {
366 float max = attrf(a, "max-rpm") * RPM2RAD;
367 float min = attrf(a, "min-rpm") * RPM2RAD;
368 thruster->setVariableProp(min, max);
372 sprintf(buf, "/engines/engine[%d]", _nextEngine++);
373 EngRec* er = new EngRec();
375 er->prefix = dup(buf);
381 // Turns a string axis name into an integer for use by the
382 // ControlMap. Creates a new axis if this one hasn't been defined
384 int FGFDM::parseAxis(const char* name)
387 for(i=0; i<_axes.size(); i++) {
388 AxisRec* a = (AxisRec*)_axes.get(i);
389 if(eq(a->name, name))
393 // Not there, make a new one.
394 AxisRec* a = new AxisRec();
396 a->handle = _airplane.getControlMap()->newInput();
401 int FGFDM::parseOutput(const char* name)
403 if(eq(name, "THROTTLE")) return ControlMap::THROTTLE;
404 if(eq(name, "MIXTURE")) return ControlMap::MIXTURE;
405 if(eq(name, "ADVANCE")) return ControlMap::ADVANCE;
406 if(eq(name, "REHEAT")) return ControlMap::REHEAT;
407 if(eq(name, "PROP")) return ControlMap::PROP;
408 if(eq(name, "BRAKE")) return ControlMap::BRAKE;
409 if(eq(name, "STEER")) return ControlMap::STEER;
410 if(eq(name, "EXTEND")) return ControlMap::EXTEND;
411 if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
412 if(eq(name, "FLAP0")) return ControlMap::FLAP0;
413 if(eq(name, "FLAP1")) return ControlMap::FLAP1;
414 if(eq(name, "SLAT")) return ControlMap::SLAT;
415 if(eq(name, "SPOILER")) return ControlMap::SPOILER;
420 void FGFDM::parseWeight(XMLAttributes* a)
422 WeightRec* wr = new WeightRec();
425 v[0] = attrf(a, "x");
426 v[1] = attrf(a, "y");
427 v[2] = attrf(a, "z");
429 wr->prop = dup(a->getValue("mass-prop"));
430 wr->size = attrf(a, "size", 0);
431 wr->handle = _airplane.addWeight(v, wr->size);
436 bool FGFDM::eq(const char* a, const char* b)
438 // Figure it out for yourself. :)
439 while(*a && *b && *a++ == *b++);
443 char* FGFDM::dup(const char* s)
447 char* s2 = new char[len+1];
449 while((*p++ = *s++));
454 int FGFDM::attri(XMLAttributes* atts, char* attr)
456 if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
457 return attri(atts, attr, 0);
460 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
462 const char* val = atts->getValue(attr);
463 if(val == 0) return def;
464 else return atol(val);
467 float FGFDM::attrf(XMLAttributes* atts, char* attr)
469 if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
470 return attrf(atts, attr, 0);
473 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
475 const char* val = atts->getValue(attr);
476 if(val == 0) return def;
477 else return (float)atof(val);
480 }; // namespace yasim