]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/FGFDM.cpp
Latest YASim changes.
[flightgear.git] / src / FDM / YASim / FGFDM.cpp
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include <Main/fg_props.hxx>
5
6 #include "Jet.hpp"
7 #include "SimpleJet.hpp"
8 #include "Gear.hpp"
9 #include "Atmosphere.hpp"
10 #include "PropEngine.hpp"
11 #include "Propeller.hpp"
12 #include "PistonEngine.hpp"
13
14 #include "FGFDM.hpp"
15 namespace yasim {
16
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;
29
30 // Stubs, so that this can be compiled without the FlightGear
31 // binary.  What's the best way to handle this?
32
33 //     float fgGetFloat(char* name, float def) { return 0; }
34 //     void fgSetFloat(char* name, float val) {}
35
36 FGFDM::FGFDM()
37 {
38     _nextEngine = 0;
39 }
40
41 FGFDM::~FGFDM()
42 {
43     int i;
44     for(i=0; i<_axes.size(); i++) {
45         AxisRec* a = (AxisRec*)_axes.get(i);
46         delete[] a->name;
47         delete a;
48     }
49     for(i=0; i<_thrusters.size(); i++) {
50         EngRec* er = (EngRec*)_thrusters.get(i);
51         delete[] er->prefix;
52         delete er->eng;
53         delete er;
54     }
55     for(i=0; i<_weights.size(); i++) {
56         WeightRec* wr = (WeightRec*)_weights.get(i);
57         delete[] wr->prop;
58         delete wr;
59     }
60     
61 }
62
63 void FGFDM::iterate(float dt)
64 {
65     getExternalInput(dt);
66     _airplane.iterate(dt);
67     setOutputProperties();
68 }
69
70 Airplane* FGFDM::getAirplane()
71 {
72     return &_airplane;
73 }
74
75 void FGFDM::init()
76 {
77     // Allows the user to start with something other than full fuel
78     _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
79
80     // This has a nasty habit of being false at startup.  That's not
81     // good.
82     fgSetBool("/controls/gear-down", true);
83 }
84
85 // Not the worlds safest parser.  But it's short & sweet.
86 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
87 {
88     XMLAttributes* a = (XMLAttributes*)&atts;
89     float v[3];
90     char buf[64];
91
92     if(eq(name, "airplane")) {
93         _airplane.setWeight(attrf(a, "mass") * LBS2KG);
94     } else if(eq(name, "approach")) {
95         float spd = attrf(a, "speed") * KTS2MPS;
96         float alt = attrf(a, "alt", 0) * FT2M;
97         float aoa = attrf(a, "aoa", 0) * DEG2RAD;
98         _airplane.setApproach(spd, alt, aoa);
99         _cruiseCurr = false;
100     } else if(eq(name, "cruise")) {
101         float spd = attrf(a, "speed") * KTS2MPS;
102         float alt = attrf(a, "alt") * FT2M;
103         _airplane.setCruise(spd, alt);
104         _cruiseCurr = true;
105     } else if(eq(name, "cockpit")) {
106         v[0] = attrf(a, "x");
107         v[1] = attrf(a, "y");
108         v[2] = attrf(a, "z");
109         _airplane.setPilotPos(v);
110     } else if(eq(name, "wing")) {
111         _airplane.setWing(parseWing(a, name));
112     } else if(eq(name, "hstab")) {
113         _airplane.setTail(parseWing(a, name));
114     } else if(eq(name, "vstab")) {
115         _airplane.addVStab(parseWing(a, name));
116     } else if(eq(name, "propeller")) {
117         parsePropeller(a);
118     } else if(eq(name, "thruster")) {
119         SimpleJet* j = new SimpleJet();
120         _currObj = j;
121         v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
122         j->setPosition(v);
123         _airplane.addThruster(j, 0, v);
124         v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
125         j->setDirection(v);
126         j->setThrust(attrf(a, "thrust") * LBS2N);
127     } else if(eq(name, "jet")) {
128         Jet* j = new Jet();
129         _currObj = j;
130         v[0] = attrf(a, "x");
131         v[1] = attrf(a, "y");
132         v[2] = attrf(a, "z");
133         float mass = attrf(a, "mass") * LBS2KG;
134         j->setMaxThrust(attrf(a, "thrust") * LBS2N,
135                         attrf(a, "afterburner", 0) * LBS2N);
136         j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
137
138         float n1min = attrf(a, "n1-idle", 55);
139         float n1max = attrf(a, "n1-max", 102);
140         float n2min = attrf(a, "n2-idle", 73);
141         float n2max = attrf(a, "n2-max", 103);
142         j->setRPMs(n1min, n1max, n2min, n2max);
143
144         if(a->hasAttribute("tsfc")) j->setTSFC(attrf(a, "tsfc"));
145         if(a->hasAttribute("egt"))  j->setEGT(attrf(a, "egt"));
146         if(a->hasAttribute("epr"))  j->setEPR(attrf(a, "epr"));
147         if(a->hasAttribute("exhaust-speed"))
148             j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
149         
150         j->setPosition(v);
151         _airplane.addThruster(j, mass, v);
152         sprintf(buf, "/engines/engine[%d]", _nextEngine++);
153         EngRec* er = new EngRec();
154         er->eng = j;
155         er->prefix = dup(buf);
156         _thrusters.add(er);
157     } else if(eq(name, "gear")) {
158         Gear* g = new Gear();
159         _currObj = g;
160         v[0] = attrf(a, "x");
161         v[1] = attrf(a, "y");
162         v[2] = attrf(a, "z");
163         g->setPosition(v);
164         v[0] = 0;
165         v[1] = 0;
166         v[2] = attrf(a, "compression", 1);
167         g->setCompression(v);
168         g->setStaticFriction(attrf(a, "sfric", 0.8));
169         g->setDynamicFriction(attrf(a, "dfric", 0.7));
170         if(a->hasAttribute("castering"))
171             g->setCastering(true);
172         float transitionTime = attrf(a, "retract-time", 0);
173         _airplane.addGear(g, transitionTime);
174     } else if(eq(name, "fuselage")) {
175         float b[3];
176         v[0] = attrf(a, "ax");
177         v[1] = attrf(a, "ay");
178         v[2] = attrf(a, "az");
179         b[0] = attrf(a, "bx");
180         b[1] = attrf(a, "by");
181         b[2] = attrf(a, "bz");
182         float taper = attrf(a, "taper", 1);
183         float mid = attrf(a, "midpoint", 0.5);
184         _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid);
185     } else if(eq(name, "tank")) {
186         v[0] = attrf(a, "x");
187         v[1] = attrf(a, "y");
188         v[2] = attrf(a, "z");
189         float density = 6.0; // gasoline, in lbs/gal
190         if(a->hasAttribute("jet")) density = 6.72; 
191         density *= LBS2KG*CM2GALS;
192         _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
193     } else if(eq(name, "ballast")) {
194         v[0] = attrf(a, "x");
195         v[1] = attrf(a, "y");
196         v[2] = attrf(a, "z");
197         _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
198     } else if(eq(name, "weight")) {
199         parseWeight(a);
200     } else if(eq(name, "stall")) {
201         Wing* w = (Wing*)_currObj;
202         w->setStall(attrf(a, "aoa") * DEG2RAD);
203         w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
204         w->setStallPeak(attrf(a, "peak", 1.5));
205     } else if(eq(name, "flap0")) {
206         ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
207                                     attrf(a, "lift"), attrf(a, "drag"));
208     } else if(eq(name, "flap1")) {
209         ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
210                                     attrf(a, "lift"), attrf(a, "drag"));
211     } else if(eq(name, "slat")) {
212         ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
213                                    attrf(a, "aoa"), attrf(a, "drag"));
214     } else if(eq(name, "spoiler")) {
215         ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
216                                       attrf(a, "lift"), attrf(a, "drag"));
217     } else if(eq(name, "actionpt")) {
218         v[0] = attrf(a, "x");
219         v[1] = attrf(a, "y");
220         v[2] = attrf(a, "z");
221         ((Thruster*)_currObj)->setPosition(v);
222     } else if(eq(name, "dir")) {
223         v[0] = attrf(a, "x");
224         v[1] = attrf(a, "y");
225         v[2] = attrf(a, "z");
226         ((Thruster*)_currObj)->setDirection(v);
227     } else if(eq(name, "control")) {
228         const char* axis = a->getValue("axis");
229         if(a->hasAttribute("output")) {
230             // assert: output type must match _currObj type!
231             const char* output = a->getValue("output");
232             int opt = 0;
233             opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
234             opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
235             opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
236
237             ControlMap* cm = _airplane.getControlMap();
238             if(a->hasAttribute("src0")) {
239                 cm->addMapping(parseAxis(axis), parseOutput(output),
240                                _currObj, opt,
241                                attrf(a, "src0"), attrf(a, "src1"), 
242                                attrf(a, "dst0"), attrf(a, "dst1"));
243             } else {
244                 cm->addMapping(parseAxis(axis), parseOutput(output),
245                                _currObj, opt);
246             }
247         } else {
248             // assert: must be under a "cruise" or "approach" tag
249             float value = attrf(a, "value", 0);
250             if(_cruiseCurr)
251                 _airplane.addCruiseControl(parseAxis(axis), value);
252             else
253                 _airplane.addApproachControl(parseAxis(axis), value);
254         }
255     } else {
256         *(int*)0=0; // unexpected tag, boom
257     }
258 }
259
260 void FGFDM::getExternalInput(float dt)
261 {
262     // The control axes
263     ControlMap* cm = _airplane.getControlMap();
264     cm->reset();
265     int i;
266     for(i=0; i<_axes.size(); i++) {
267         AxisRec* a = (AxisRec*)_axes.get(i);
268         float val = fgGetFloat(a->name, 0);
269         cm->setInput(a->handle, val);
270     }
271     cm->applyControls();
272
273     // Weights
274     for(i=0; i<_weights.size(); i++) {
275         WeightRec* wr = (WeightRec*)_weights.get(i);
276         _airplane.setWeight(wr->handle, fgGetFloat(wr->prop));
277     }
278
279     // Gear state
280     _airplane.setGearState(fgGetBool("/controls/gear-down"), dt);
281 }
282
283 void FGFDM::setOutputProperties()
284 {
285     char buf[256];
286     int i;
287     float fuelDensity = 718.95; // default to gasoline: ~6 lb/gal
288     for(i=0; i<_airplane.numTanks(); i++) {
289         fuelDensity = _airplane.getFuelDensity(i);
290         sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
291         fgSetFloat(buf, CM2GALS*_airplane.getFuel(i)/fuelDensity);
292     }
293
294     for(i=0; i<_thrusters.size(); i++) {
295         EngRec* er = (EngRec*)_thrusters.get(i);
296         Thruster* t = er->eng;
297
298         sprintf(buf, "%s/fuel-flow-gph", er->prefix);
299         fgSetFloat(buf, (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
300
301         if(t->getPropEngine()) {
302             PropEngine* p = t->getPropEngine();
303
304             sprintf(buf, "%s/rpm", er->prefix);
305             fgSetFloat(buf, p->getOmega() / RPM2RAD);
306         }
307
308         if(t->getPistonEngine()) {
309             PistonEngine* p = t->getPistonEngine();
310             
311             sprintf(buf, "%s/mp-osi", er->prefix);
312             fgSetFloat(buf, p->getMP() * (1/INHG2PA));
313
314             sprintf(buf, "%s/egt-degf", er->prefix);
315             fgSetFloat(buf, p->getEGT() * K2DEGF + 459.4);
316         }
317
318         if(t->getJet()) {
319             Jet* j = t->getJet();
320
321             sprintf(buf, "%s/n1", er->prefix);
322             fgSetFloat(buf, j->getN1());
323
324             sprintf(buf, "%s/n2", er->prefix);
325             fgSetFloat(buf, j->getN2());
326
327             sprintf(buf, "%s/epr", er->prefix);
328             fgSetFloat(buf, j->getEPR());
329
330             sprintf(buf, "%s/egt-degf", er->prefix);
331             fgSetFloat(buf, j->getEGT() * K2DEGF + 459.4);
332         }
333     }
334 }
335
336 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
337 {
338     Wing* w = new Wing();
339
340     float defDihed = 0;
341     if(eq(type, "vstab"))
342         defDihed = 90;
343     else
344         w->setMirror(true);
345
346     float pos[3];
347     pos[0] = attrf(a, "x");
348     pos[1] = attrf(a, "y");
349     pos[2] = attrf(a, "z");
350     w->setBase(pos);
351
352     w->setLength(attrf(a, "length"));
353     w->setChord(attrf(a, "chord"));
354     w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
355     w->setTaper(attrf(a, "taper", 1));
356     w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
357     w->setCamber(attrf(a, "camber", 0));
358     w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
359
360     float effect = attrf(a, "effectiveness", 1);
361     w->setDragScale(w->getDragScale()*effect);
362
363     _currObj = w;
364     return w;
365 }
366
367 void FGFDM::parsePropeller(XMLAttributes* a)
368 {
369     float cg[3];
370     cg[0] = attrf(a, "x");
371     cg[1] = attrf(a, "y");
372     cg[2] = attrf(a, "z");
373     float mass = attrf(a, "mass") * LBS2KG;
374     float moment = attrf(a, "moment");
375     float radius = attrf(a, "radius");
376     float speed = attrf(a, "cruise-speed") * KTS2MPS;
377     float omega = attrf(a, "cruise-rpm") * RPM2RAD;
378     float power = attrf(a, "cruise-power") * HP2W;
379     float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
380
381     // Hack, fix this pronto:
382     float engP = attrf(a, "eng-power") * HP2W;
383     float engS = attrf(a, "eng-rpm") * RPM2RAD;
384
385     Propeller* prop = new Propeller(radius, speed, omega, rho, power);
386     PistonEngine* eng = new PistonEngine(engP, engS);
387     PropEngine* thruster = new PropEngine(prop, eng, moment);
388     _airplane.addThruster(thruster, mass, cg);
389
390     if(a->hasAttribute("displacement"))
391         eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
392
393     if(a->hasAttribute("compression"))
394         eng->setCompression(attrf(a, "compression"));        
395
396     if(a->hasAttribute("turbo-mul")) {
397         float mul = attrf(a, "turbo-mul");
398         float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
399         eng->setTurboParams(mul, mp);
400     }
401
402     if(a->hasAttribute("takeoff-power")) {
403         float power0 = attrf(a, "takeoff-power") * HP2W;
404         float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
405         prop->setTakeoff(omega0, power0);
406     }
407
408     if(a->hasAttribute("max-rpm")) {
409         float max = attrf(a, "max-rpm") * RPM2RAD;
410         float min = attrf(a, "min-rpm") * RPM2RAD;
411         thruster->setVariableProp(min, max);
412     }
413
414     char buf[64];
415     sprintf(buf, "/engines/engine[%d]", _nextEngine++);
416     EngRec* er = new EngRec();
417     er->eng = thruster;
418     er->prefix = dup(buf);
419     _thrusters.add(er);
420
421     _currObj = thruster;
422 }
423
424 // Turns a string axis name into an integer for use by the
425 // ControlMap.  Creates a new axis if this one hasn't been defined
426 // yet.
427 int FGFDM::parseAxis(const char* name)
428 {
429     int i;
430     for(i=0; i<_axes.size(); i++) {
431         AxisRec* a = (AxisRec*)_axes.get(i);
432         if(eq(a->name, name))
433             return a->handle;
434     }
435
436     // Not there, make a new one.
437     AxisRec* a = new AxisRec();
438     a->name = dup(name);
439     a->handle = _airplane.getControlMap()->newInput();
440     _axes.add(a);
441     return a->handle;
442 }
443
444 int FGFDM::parseOutput(const char* name)
445 {
446     if(eq(name, "THROTTLE"))  return ControlMap::THROTTLE;
447     if(eq(name, "MIXTURE"))   return ControlMap::MIXTURE;
448     if(eq(name, "ADVANCE"))   return ControlMap::ADVANCE;
449     if(eq(name, "REHEAT"))    return ControlMap::REHEAT;
450     if(eq(name, "BOOST"))     return ControlMap::BOOST;
451     if(eq(name, "VECTOR"))    return ControlMap::VECTOR;
452     if(eq(name, "PROP"))      return ControlMap::PROP;
453     if(eq(name, "BRAKE"))     return ControlMap::BRAKE;
454     if(eq(name, "STEER"))     return ControlMap::STEER;
455     if(eq(name, "EXTEND"))    return ControlMap::EXTEND;
456     if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
457     if(eq(name, "FLAP0"))     return ControlMap::FLAP0;
458     if(eq(name, "FLAP1"))     return ControlMap::FLAP1;
459     if(eq(name, "SLAT"))      return ControlMap::SLAT;
460     if(eq(name, "SPOILER"))   return ControlMap::SPOILER;
461     // error here...
462     return *(int*)0;
463 }
464
465 void FGFDM::parseWeight(XMLAttributes* a)
466 {
467     WeightRec* wr = new WeightRec();
468
469     float v[3];
470     v[0] = attrf(a, "x");
471     v[1] = attrf(a, "y");
472     v[2] = attrf(a, "z");
473
474     wr->prop = dup(a->getValue("mass-prop"));
475     wr->size = attrf(a, "size", 0);
476     wr->handle = _airplane.addWeight(v, wr->size);
477
478     _weights.add(wr);
479 }
480
481 bool FGFDM::eq(const char* a, const char* b)
482 {
483     // Figure it out for yourself. :)
484     while(*a && *b && *a++ == *b++);
485     return !(*a || *b);
486 }
487
488 char* FGFDM::dup(const char* s)
489 {
490     int len=0;
491     while(s[len++]);
492     char* s2 = new char[len+1];
493     char* p = s2;
494     while((*p++ = *s++));
495     s2[len] = 0;
496     return s2;
497 }
498
499 int FGFDM::attri(XMLAttributes* atts, char* attr)
500 {
501     if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
502     return attri(atts, attr, 0);
503 }
504
505 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
506 {
507     const char* val = atts->getValue(attr);
508     if(val == 0) return def;
509     else         return atol(val);
510 }
511
512 float FGFDM::attrf(XMLAttributes* atts, char* attr)
513 {
514     if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
515     return attrf(atts, attr, 0);
516 }
517
518 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
519 {
520     const char* val = atts->getValue(attr);
521     if(val == 0) return def;
522     else         return (float)atof(val);    
523 }
524
525 }; // namespace yasim