]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/FGFDM.cpp
Support a "skid" property for non-rolling gear objects like tail skids.
[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->setBrake(attrf(a, "skid", 0));
169         g->setStaticFriction(attrf(a, "sfric", 0.8));
170         g->setDynamicFriction(attrf(a, "dfric", 0.7));
171         if(a->hasAttribute("castering"))
172             g->setCastering(true);
173         float transitionTime = attrf(a, "retract-time", 0);
174         _airplane.addGear(g, transitionTime);
175     } else if(eq(name, "fuselage")) {
176         float b[3];
177         v[0] = attrf(a, "ax");
178         v[1] = attrf(a, "ay");
179         v[2] = attrf(a, "az");
180         b[0] = attrf(a, "bx");
181         b[1] = attrf(a, "by");
182         b[2] = attrf(a, "bz");
183         float taper = attrf(a, "taper", 1);
184         float mid = attrf(a, "midpoint", 0.5);
185         _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid);
186     } else if(eq(name, "tank")) {
187         v[0] = attrf(a, "x");
188         v[1] = attrf(a, "y");
189         v[2] = attrf(a, "z");
190         float density = 6.0; // gasoline, in lbs/gal
191         if(a->hasAttribute("jet")) density = 6.72; 
192         density *= LBS2KG*CM2GALS;
193         _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
194     } else if(eq(name, "ballast")) {
195         v[0] = attrf(a, "x");
196         v[1] = attrf(a, "y");
197         v[2] = attrf(a, "z");
198         _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
199     } else if(eq(name, "weight")) {
200         parseWeight(a);
201     } else if(eq(name, "stall")) {
202         Wing* w = (Wing*)_currObj;
203         w->setStall(attrf(a, "aoa") * DEG2RAD);
204         w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
205         w->setStallPeak(attrf(a, "peak", 1.5));
206     } else if(eq(name, "flap0")) {
207         ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
208                                     attrf(a, "lift"), attrf(a, "drag"));
209     } else if(eq(name, "flap1")) {
210         ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
211                                     attrf(a, "lift"), attrf(a, "drag"));
212     } else if(eq(name, "slat")) {
213         ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
214                                    attrf(a, "aoa"), attrf(a, "drag"));
215     } else if(eq(name, "spoiler")) {
216         ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
217                                       attrf(a, "lift"), attrf(a, "drag"));
218     } else if(eq(name, "actionpt")) {
219         v[0] = attrf(a, "x");
220         v[1] = attrf(a, "y");
221         v[2] = attrf(a, "z");
222         ((Thruster*)_currObj)->setPosition(v);
223     } else if(eq(name, "dir")) {
224         v[0] = attrf(a, "x");
225         v[1] = attrf(a, "y");
226         v[2] = attrf(a, "z");
227         ((Thruster*)_currObj)->setDirection(v);
228     } else if(eq(name, "control")) {
229         const char* axis = a->getValue("axis");
230         if(a->hasAttribute("output")) {
231             // assert: output type must match _currObj type!
232             const char* output = a->getValue("output");
233             int opt = 0;
234             opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
235             opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
236             opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
237
238             ControlMap* cm = _airplane.getControlMap();
239             if(a->hasAttribute("src0")) {
240                 cm->addMapping(parseAxis(axis), parseOutput(output),
241                                _currObj, opt,
242                                attrf(a, "src0"), attrf(a, "src1"), 
243                                attrf(a, "dst0"), attrf(a, "dst1"));
244             } else {
245                 cm->addMapping(parseAxis(axis), parseOutput(output),
246                                _currObj, opt);
247             }
248         } else {
249             // assert: must be under a "cruise" or "approach" tag
250             float value = attrf(a, "value", 0);
251             if(_cruiseCurr)
252                 _airplane.addCruiseControl(parseAxis(axis), value);
253             else
254                 _airplane.addApproachControl(parseAxis(axis), value);
255         }
256     } else {
257         *(int*)0=0; // unexpected tag, boom
258     }
259 }
260
261 void FGFDM::getExternalInput(float dt)
262 {
263     // The control axes
264     ControlMap* cm = _airplane.getControlMap();
265     cm->reset();
266     int i;
267     for(i=0; i<_axes.size(); i++) {
268         AxisRec* a = (AxisRec*)_axes.get(i);
269         float val = fgGetFloat(a->name, 0);
270         cm->setInput(a->handle, val);
271     }
272     cm->applyControls();
273
274     // Weights
275     for(i=0; i<_weights.size(); i++) {
276         WeightRec* wr = (WeightRec*)_weights.get(i);
277         _airplane.setWeight(wr->handle, fgGetFloat(wr->prop));
278     }
279
280     // Gear state
281     _airplane.setGearState(fgGetBool("/controls/gear-down"), dt);
282 }
283
284 void FGFDM::setOutputProperties()
285 {
286     char buf[256];
287     int i;
288     float fuelDensity = 718.95; // default to gasoline: ~6 lb/gal
289     for(i=0; i<_airplane.numTanks(); i++) {
290         fuelDensity = _airplane.getFuelDensity(i);
291         sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
292         fgSetFloat(buf, CM2GALS*_airplane.getFuel(i)/fuelDensity);
293     }
294
295     for(i=0; i<_thrusters.size(); i++) {
296         EngRec* er = (EngRec*)_thrusters.get(i);
297         Thruster* t = er->eng;
298
299         sprintf(buf, "%s/fuel-flow-gph", er->prefix);
300         fgSetFloat(buf, (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
301
302         if(t->getPropEngine()) {
303             PropEngine* p = t->getPropEngine();
304
305             sprintf(buf, "%s/rpm", er->prefix);
306             fgSetFloat(buf, p->getOmega() / RPM2RAD);
307         }
308
309         if(t->getPistonEngine()) {
310             PistonEngine* p = t->getPistonEngine();
311             
312             sprintf(buf, "%s/mp-osi", er->prefix);
313             fgSetFloat(buf, p->getMP() * (1/INHG2PA));
314
315             sprintf(buf, "%s/egt-degf", er->prefix);
316             fgSetFloat(buf, p->getEGT() * K2DEGF + 459.4);
317         }
318
319         if(t->getJet()) {
320             Jet* j = t->getJet();
321
322             sprintf(buf, "%s/n1", er->prefix);
323             fgSetFloat(buf, j->getN1());
324
325             sprintf(buf, "%s/n2", er->prefix);
326             fgSetFloat(buf, j->getN2());
327
328             sprintf(buf, "%s/epr", er->prefix);
329             fgSetFloat(buf, j->getEPR());
330
331             sprintf(buf, "%s/egt-degf", er->prefix);
332             fgSetFloat(buf, j->getEGT() * K2DEGF + 459.4);
333         }
334     }
335 }
336
337 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
338 {
339     Wing* w = new Wing();
340
341     float defDihed = 0;
342     if(eq(type, "vstab"))
343         defDihed = 90;
344     else
345         w->setMirror(true);
346
347     float pos[3];
348     pos[0] = attrf(a, "x");
349     pos[1] = attrf(a, "y");
350     pos[2] = attrf(a, "z");
351     w->setBase(pos);
352
353     w->setLength(attrf(a, "length"));
354     w->setChord(attrf(a, "chord"));
355     w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
356     w->setTaper(attrf(a, "taper", 1));
357     w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
358     w->setCamber(attrf(a, "camber", 0));
359     w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
360
361     float effect = attrf(a, "effectiveness", 1);
362     w->setDragScale(w->getDragScale()*effect);
363
364     _currObj = w;
365     return w;
366 }
367
368 void FGFDM::parsePropeller(XMLAttributes* a)
369 {
370     float cg[3];
371     cg[0] = attrf(a, "x");
372     cg[1] = attrf(a, "y");
373     cg[2] = attrf(a, "z");
374     float mass = attrf(a, "mass") * LBS2KG;
375     float moment = attrf(a, "moment");
376     float radius = attrf(a, "radius");
377     float speed = attrf(a, "cruise-speed") * KTS2MPS;
378     float omega = attrf(a, "cruise-rpm") * RPM2RAD;
379     float power = attrf(a, "cruise-power") * HP2W;
380     float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
381
382     // Hack, fix this pronto:
383     float engP = attrf(a, "eng-power") * HP2W;
384     float engS = attrf(a, "eng-rpm") * RPM2RAD;
385
386     Propeller* prop = new Propeller(radius, speed, omega, rho, power);
387     PistonEngine* eng = new PistonEngine(engP, engS);
388     PropEngine* thruster = new PropEngine(prop, eng, moment);
389     _airplane.addThruster(thruster, mass, cg);
390
391     if(a->hasAttribute("displacement"))
392         eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
393
394     if(a->hasAttribute("compression"))
395         eng->setCompression(attrf(a, "compression"));        
396
397     if(a->hasAttribute("turbo-mul")) {
398         float mul = attrf(a, "turbo-mul");
399         float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
400         eng->setTurboParams(mul, mp);
401     }
402
403     if(a->hasAttribute("takeoff-power")) {
404         float power0 = attrf(a, "takeoff-power") * HP2W;
405         float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
406         prop->setTakeoff(omega0, power0);
407     }
408
409     if(a->hasAttribute("max-rpm")) {
410         float max = attrf(a, "max-rpm") * RPM2RAD;
411         float min = attrf(a, "min-rpm") * RPM2RAD;
412         thruster->setVariableProp(min, max);
413     }
414
415     char buf[64];
416     sprintf(buf, "/engines/engine[%d]", _nextEngine++);
417     EngRec* er = new EngRec();
418     er->eng = thruster;
419     er->prefix = dup(buf);
420     _thrusters.add(er);
421
422     _currObj = thruster;
423 }
424
425 // Turns a string axis name into an integer for use by the
426 // ControlMap.  Creates a new axis if this one hasn't been defined
427 // yet.
428 int FGFDM::parseAxis(const char* name)
429 {
430     int i;
431     for(i=0; i<_axes.size(); i++) {
432         AxisRec* a = (AxisRec*)_axes.get(i);
433         if(eq(a->name, name))
434             return a->handle;
435     }
436
437     // Not there, make a new one.
438     AxisRec* a = new AxisRec();
439     a->name = dup(name);
440     a->handle = _airplane.getControlMap()->newInput();
441     _axes.add(a);
442     return a->handle;
443 }
444
445 int FGFDM::parseOutput(const char* name)
446 {
447     if(eq(name, "THROTTLE"))  return ControlMap::THROTTLE;
448     if(eq(name, "MIXTURE"))   return ControlMap::MIXTURE;
449     if(eq(name, "STARTER"))   return ControlMap::STARTER;
450     if(eq(name, "MAGNETOS"))  return ControlMap::MAGNETOS;
451     if(eq(name, "ADVANCE"))   return ControlMap::ADVANCE;
452     if(eq(name, "REHEAT"))    return ControlMap::REHEAT;
453     if(eq(name, "BOOST"))     return ControlMap::BOOST;
454     if(eq(name, "VECTOR"))    return ControlMap::VECTOR;
455     if(eq(name, "PROP"))      return ControlMap::PROP;
456     if(eq(name, "BRAKE"))     return ControlMap::BRAKE;
457     if(eq(name, "STEER"))     return ControlMap::STEER;
458     if(eq(name, "EXTEND"))    return ControlMap::EXTEND;
459     if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
460     if(eq(name, "FLAP0"))     return ControlMap::FLAP0;
461     if(eq(name, "FLAP1"))     return ControlMap::FLAP1;
462     if(eq(name, "SLAT"))      return ControlMap::SLAT;
463     if(eq(name, "SPOILER"))   return ControlMap::SPOILER;
464     // error here...
465     return *(int*)0;
466 }
467
468 void FGFDM::parseWeight(XMLAttributes* a)
469 {
470     WeightRec* wr = new WeightRec();
471
472     float v[3];
473     v[0] = attrf(a, "x");
474     v[1] = attrf(a, "y");
475     v[2] = attrf(a, "z");
476
477     wr->prop = dup(a->getValue("mass-prop"));
478     wr->size = attrf(a, "size", 0);
479     wr->handle = _airplane.addWeight(v, wr->size);
480
481     _weights.add(wr);
482 }
483
484 bool FGFDM::eq(const char* a, const char* b)
485 {
486     // Figure it out for yourself. :)
487     while(*a && *b && *a++ == *b++);
488     return !(*a || *b);
489 }
490
491 char* FGFDM::dup(const char* s)
492 {
493     int len=0;
494     while(s[len++]);
495     char* s2 = new char[len+1];
496     char* p = s2;
497     while((*p++ = *s++));
498     s2[len] = 0;
499     return s2;
500 }
501
502 int FGFDM::attri(XMLAttributes* atts, char* attr)
503 {
504     if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
505     return attri(atts, attr, 0);
506 }
507
508 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
509 {
510     const char* val = atts->getValue(attr);
511     if(val == 0) return def;
512     else         return atol(val);
513 }
514
515 float FGFDM::attrf(XMLAttributes* atts, char* attr)
516 {
517     if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
518     return attrf(atts, attr, 0);
519 }
520
521 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
522 {
523     const char* val = atts->getValue(attr);
524     if(val == 0) return def;
525     else         return (float)atof(val);    
526 }
527
528 }; // namespace yasim