]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/FGFDM.cpp
Finally fixed the flap drag issue. Drag modifications need to be based on
[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 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;
30
31 // Stubs, so that this can be compiled without the FlightGear
32 // binary.  What's the best way to handle this?
33
34 //     float fgGetFloat(char* name, float def) { return 0; }
35 //     void fgSetFloat(char* name, float val) {}
36
37 FGFDM::FGFDM()
38 {
39     _nextEngine = 0;
40
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"));
45 }
46
47 FGFDM::~FGFDM()
48 {
49     int i;
50     for(i=0; i<_axes.size(); i++) {
51         AxisRec* a = (AxisRec*)_axes.get(i);
52         delete[] a->name;
53         delete a;
54     }
55     for(i=0; i<_thrusters.size(); i++) {
56         EngRec* er = (EngRec*)_thrusters.get(i);
57         delete[] er->prefix;
58         delete er->eng;
59         delete er;
60     }
61     for(i=0; i<_weights.size(); i++) {
62         WeightRec* wr = (WeightRec*)_weights.get(i);
63         delete[] wr->prop;
64         delete wr;
65     }
66     for(i=0; i<_controlProps.size(); i++)
67         delete (PropOut*)_controlProps.get(i);
68 }
69
70 void FGFDM::iterate(float dt)
71 {
72     getExternalInput(dt);
73     _airplane.iterate(dt);
74     setOutputProperties();
75 }
76
77 Airplane* FGFDM::getAirplane()
78 {
79     return &_airplane;
80 }
81
82 void FGFDM::init()
83 {
84     // Allows the user to start with something other than full fuel
85     _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
86
87     // This has a nasty habit of being false at startup.  That's not
88     // good.
89     fgSetBool("/controls/gear-down", true);
90 }
91
92 // Not the worlds safest parser.  But it's short & sweet.
93 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
94 {
95     XMLAttributes* a = (XMLAttributes*)&atts;
96     float v[3];
97     char buf[64];
98
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);
106         _cruiseCurr = false;
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);
111         _cruiseCurr = true;
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")) {
124         parsePropeller(a);
125     } else if(eq(name, "thruster")) {
126         SimpleJet* j = new SimpleJet();
127         _currObj = j;
128         v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
129         j->setPosition(v);
130         _airplane.addThruster(j, 0, v);
131         v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
132         j->setDirection(v);
133         j->setThrust(attrf(a, "thrust") * LBS2N);
134     } else if(eq(name, "jet")) {
135         Jet* j = new Jet();
136         _currObj = j;
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);
144
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);
150
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);
156         
157         j->setPosition(v);
158         _airplane.addThruster(j, mass, v);
159         sprintf(buf, "/engines/engine[%d]", _nextEngine++);
160         EngRec* er = new EngRec();
161         er->eng = j;
162         er->prefix = dup(buf);
163         _thrusters.add(er);
164     } else if(eq(name, "gear")) {
165         Gear* g = new Gear();
166         _currObj = g;
167         v[0] = attrf(a, "x");
168         v[1] = attrf(a, "y");
169         v[2] = attrf(a, "z");
170         g->setPosition(v);
171         v[0] = 0;
172         v[1] = 0;
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")) {
180         float b[3];
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")) {
204         parseWeight(a);
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);
236         if(_cruiseCurr)
237             _airplane.addCruiseControl(parseAxis(axis), value);
238         else
239             _airplane.addApproachControl(parseAxis(axis), value);
240     } else if(eq(name, "control-input")) {
241
242         // A mapping of input property to a control
243         int axis = parseAxis(a->getValue("axis"));
244         int control = parseOutput(a->getValue("control"));
245         int opt = 0;
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;
249         
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"));
255         } else {
256             cm->addMapping(axis, control, _currObj, opt);
257         }
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);
263
264         PropOut* p = new PropOut();
265         p->prop = fgGetNode(a->getValue("prop"), true);
266         p->handle = handle;
267         p->type = type;
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);
273
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);
279         
280         cm->setTransitionTime(handle, time);
281     } else {
282         SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '"
283                << name << "' found in YASim aircraft description");
284         exit(1);
285     }
286 }
287
288 void FGFDM::getExternalInput(float dt)
289 {
290     // The control axes
291     ControlMap* cm = _airplane.getControlMap();
292     cm->reset();
293     int i;
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);
298     }
299     cm->applyControls(dt);
300
301     // Weights
302     for(i=0; i<_weights.size(); i++) {
303         WeightRec* wr = (WeightRec*)_weights.get(i);
304         _airplane.setWeight(wr->handle, fgGetFloat(wr->prop));
305     }
306 }
307
308 void FGFDM::setOutputProperties()
309 {
310     char buf[256];
311     int i;
312
313     float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
314     fgSetFloat("/yasim/gross-weight-lbs", grossWgt);
315
316     ControlMap* cm = _airplane.getControlMap();
317     for(i=0; i<_controlProps.size(); i++) {
318         PropOut* p = (PropOut*)_controlProps.get(i);
319         float val = (p->left
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);
327     }
328
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);
334     }
335
336     for(i=0; i<_thrusters.size(); i++) {
337         EngRec* er = (EngRec*)_thrusters.get(i);
338         Thruster* t = er->eng;
339
340         sprintf(buf, "%s/fuel-flow-gph", er->prefix);
341         fgSetFloat(buf, (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
342
343         if(t->getPropEngine()) {
344             PropEngine* p = t->getPropEngine();
345
346             sprintf(buf, "%s/rpm", er->prefix);
347             fgSetFloat(buf, p->getOmega() / RPM2RAD);
348         }
349
350         if(t->getPistonEngine()) {
351             PistonEngine* p = t->getPistonEngine();
352             
353             sprintf(buf, "%s/mp-osi", er->prefix);
354             fgSetFloat(buf, p->getMP() * (1/INHG2PA));
355
356             sprintf(buf, "%s/egt-degf", er->prefix);
357             fgSetFloat(buf, p->getEGT() * K2DEGF + 459.4);
358         }
359
360         if(t->getJet()) {
361             Jet* j = t->getJet();
362
363             sprintf(buf, "%s/n1", er->prefix);
364             fgSetFloat(buf, j->getN1());
365
366             sprintf(buf, "%s/n2", er->prefix);
367             fgSetFloat(buf, j->getN2());
368
369             sprintf(buf, "%s/epr", er->prefix);
370             fgSetFloat(buf, j->getEPR());
371
372             sprintf(buf, "%s/egt-degf", er->prefix);
373             fgSetFloat(buf, j->getEGT() * K2DEGF + 459.4);
374         }
375     }
376 }
377
378 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
379 {
380     Wing* w = new Wing();
381
382     float defDihed = 0;
383     if(eq(type, "vstab"))
384         defDihed = 90;
385     else
386         w->setMirror(true);
387
388     float pos[3];
389     pos[0] = attrf(a, "x");
390     pos[1] = attrf(a, "y");
391     pos[2] = attrf(a, "z");
392     w->setBase(pos);
393
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);
401
402     float effect = attrf(a, "effectiveness", 1);
403     w->setDragScale(w->getDragScale()*effect);
404
405     _currObj = w;
406     return w;
407 }
408
409 void FGFDM::parsePropeller(XMLAttributes* a)
410 {
411     float cg[3];
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);
422
423     // Hack, fix this pronto:
424     float engP = attrf(a, "eng-power") * HP2W;
425     float engS = attrf(a, "eng-rpm") * RPM2RAD;
426
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);
431
432     if(a->hasAttribute("displacement"))
433         eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
434
435     if(a->hasAttribute("compression"))
436         eng->setCompression(attrf(a, "compression"));        
437
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);
442     }
443
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);
448     }
449
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);
454     }
455
456     char buf[64];
457     sprintf(buf, "/engines/engine[%d]", _nextEngine++);
458     EngRec* er = new EngRec();
459     er->eng = thruster;
460     er->prefix = dup(buf);
461     _thrusters.add(er);
462
463     _currObj = thruster;
464 }
465
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
468 // yet.
469 int FGFDM::parseAxis(const char* name)
470 {
471     int i;
472     for(i=0; i<_axes.size(); i++) {
473         AxisRec* a = (AxisRec*)_axes.get(i);
474         if(eq(a->name, name))
475             return a->handle;
476     }
477
478     // Not there, make a new one.
479     AxisRec* a = new AxisRec();
480     a->name = dup(name);
481     a->handle = _airplane.getControlMap()->newInput();
482     _axes.add(a);
483     return a->handle;
484 }
485
486 int FGFDM::parseOutput(const char* name)
487 {
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.");
508     exit(1);
509
510 }
511
512 void FGFDM::parseWeight(XMLAttributes* a)
513 {
514     WeightRec* wr = new WeightRec();
515
516     float v[3];
517     v[0] = attrf(a, "x");
518     v[1] = attrf(a, "y");
519     v[2] = attrf(a, "z");
520
521     wr->prop = dup(a->getValue("mass-prop"));
522     wr->size = attrf(a, "size", 0);
523     wr->handle = _airplane.addWeight(v, wr->size);
524
525     _weights.add(wr);
526 }
527
528 bool FGFDM::eq(const char* a, const char* b)
529 {
530     // Figure it out for yourself. :)
531     while(*a && *b && *a == *b) { a++; b++; }
532     return !(*a || *b);
533 }
534
535 char* FGFDM::dup(const char* s)
536 {
537     int len=0;
538     while(s[len++]);
539     char* s2 = new char[len+1];
540     char* p = s2;
541     while((*p++ = *s++));
542     s2[len] = 0;
543     return s2;
544 }
545
546 int FGFDM::attri(XMLAttributes* atts, char* attr)
547 {
548     if(!atts->hasAttribute(attr)) {
549         SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
550                "' in YASim aircraft description");
551         exit(1);
552     }
553     return attri(atts, attr, 0);
554 }
555
556 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
557 {
558     const char* val = atts->getValue(attr);
559     if(val == 0) return def;
560     else         return atol(val);
561 }
562
563 float FGFDM::attrf(XMLAttributes* atts, char* attr)
564 {
565     if(!atts->hasAttribute(attr)) {
566         SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
567                "' in YASim aircraft description");
568         exit(1);
569     }
570     return attrf(atts, attr, 0);
571 }
572
573 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
574 {
575     const char* val = atts->getValue(attr);
576     if(val == 0) return def;
577     else         return (float)atof(val);    
578 }
579
580 }; // namespace yasim