]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/FGFDM.cpp
David Megginson writes:
[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 "Gear.hpp"
8 #include "Atmosphere.hpp"
9 #include "PropEngine.hpp"
10 #include "Propeller.hpp"
11 #include "PistonEngine.hpp"
12
13 #include "FGFDM.hpp"
14 namespace yasim {
15
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;
26
27 // Stubs, so that this can be compiled without the FlightGear
28 // binary.  What's the best way to handle this?
29
30 //     float fgGetFloat(char* name, float def) { return 0; }
31 //     void fgSetFloat(char* name, float val) {}
32
33 FGFDM::FGFDM()
34 {
35     _nextEngine = 0;
36 }
37
38 FGFDM::~FGFDM()
39 {
40     int i;
41     for(i=0; i<_axes.size(); i++) {
42         AxisRec* a = (AxisRec*)_axes.get(i);
43         delete[] a->name;
44         delete a;
45     }
46     for(i=0; i<_pistons.size(); i++) {
47         EngRec* er = (EngRec*)_pistons.get(i);
48         delete[] er->prefix;
49         delete (PropEngine*)er->eng;
50         delete er;
51     }
52     for(i=0; i<_jets.size(); i++) {
53         EngRec* er = (EngRec*)_pistons.get(i);
54         delete[] er->prefix;
55         delete (Jet*)er->eng;
56         delete er;
57     }
58     for(i=0; i<_weights.size(); i++) {
59         WeightRec* wr = (WeightRec*)_weights.get(i);
60         delete[] wr->prop;
61         delete wr;
62     }
63     
64 }
65
66 void FGFDM::iterate(float dt)
67 {
68     getExternalInput(dt);
69     _airplane.iterate(dt);
70     setOutputProperties();
71 }
72
73 Airplane* FGFDM::getAirplane()
74 {
75     return &_airplane;
76 }
77
78 void FGFDM::init()
79 {
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");
92
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);
96
97     // Allows the user to start with something other than full fuel
98     _airplane.setFuelFraction(fgGetFloat("/yasim/fuel-fraction", 1));
99
100     // This has a nasty habit of being false at startup.  That's not
101     // good.
102     fgSetBool("/controls/gear-down", true);
103 }
104
105 // Not the worlds safest parser.  But it's short & sweet.
106 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
107 {
108     XMLAttributes* a = (XMLAttributes*)&atts;
109     float v[3];
110     char buf[64];
111
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);
119         _cruiseCurr = false;
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);
124         _cruiseCurr = true;
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")) {
137         parsePropeller(a);
138     } else if(eq(name, "jet")) {
139         Jet* j = new Jet();
140         _currObj = j;
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);
147         j->setPosition(v);
148         _airplane.addThruster(j, mass, v);
149         sprintf(buf, "/engines/engine[%d]", _nextEngine++);
150         EngRec* er = new EngRec();
151         er->eng = j;
152         er->prefix = dup(buf);
153         _jets.add(er);
154     } else if(eq(name, "gear")) {
155         Gear* g = new Gear();
156         _currObj = g;
157         v[0] = attrf(a, "x");
158         v[1] = attrf(a, "y");
159         v[2] = attrf(a, "z");
160         g->setPosition(v);
161         v[0] = 0;
162         v[1] = 0;
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")) {
170         float b[3];
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")) {
192         parseWeight(a);
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");
225             int opt = 0;
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),
230                                                   parseOutput(output),
231                                                   _currObj,
232                                                   opt);
233         } else {
234             // assert: must be under a "cruise" or "approach" tag
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         }
241     } else {
242         *(int*)0=0; // unexpected tag, boom
243     }
244 }
245
246 void FGFDM::getExternalInput(float dt)
247 {
248     // The control axes
249     ControlMap* cm = _airplane.getControlMap();
250     cm->reset();
251     int i;
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);
256     }
257     cm->applyControls();
258
259     // Weights
260     for(i=0; i<_weights.size(); i++) {
261         WeightRec* wr = (WeightRec*)_weights.get(i);
262         _airplane.setWeight(wr->handle, fgGetFloat(wr->prop));
263     }
264
265     // Gear state
266     _airplane.setGearState(fgGetBool("/controls/gear-down"), dt);
267 }
268
269 void FGFDM::setOutputProperties()
270 {
271     char buf[256];
272     int i;
273     for(i=0; i<_airplane.numTanks(); i++) {
274         sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
275         fgSetFloat(buf,
276                    CM2GALS*_airplane.getFuel(i)/_airplane.getFuelDensity(i));
277     }
278
279     for(i=0; i<_pistons.size(); i++) {
280         EngRec* er = (EngRec*)_pistons.get(i);
281         PropEngine* p = (PropEngine*)er->eng;
282
283         sprintf(buf, "%s/rpm", er->prefix);
284         fgSetFloat(buf, p->getOmega() / RPM2RAD);
285
286         sprintf(buf, "%s/fuel-flow-gph", er->prefix);
287         fgSetFloat(buf, p->getFuelFlow() * (3600*2.2/5)); // FIXME, wrong
288     }
289
290     for(i=0; i<_jets.size(); i++) {
291         EngRec* er = (EngRec*)_jets.get(i);
292         Jet* j = (Jet*)er->eng;
293         
294         sprintf(buf, "%s/fuel-flow-gph", er->prefix);
295         fgSetFloat(buf, j->getFuelFlow() * (3600*2.2/6)); // FIXME, wrong
296     }
297 }
298
299 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
300 {
301     Wing* w = new Wing();
302
303     float defDihed = 0;
304     if(eq(type, "vstab"))
305         defDihed = 90;
306     else
307         w->setMirror(true);
308
309     float pos[3];
310     pos[0] = attrf(a, "x");
311     pos[1] = attrf(a, "y");
312     pos[2] = attrf(a, "z");
313     w->setBase(pos);
314
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);
322
323     float effect = attrf(a, "effectiveness", 1);
324     w->setDragScale(w->getDragScale()*effect);
325
326     _currObj = w;
327     return w;
328 }
329
330 void FGFDM::parsePropeller(XMLAttributes* a)
331 {
332     float cg[3];
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);
343
344     // Hack, fix this pronto:
345     float engP = attrf(a, "eng-power") * HP2W;
346     float engS = attrf(a, "eng-rpm") * RPM2RAD;
347
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);
352
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);
357     }
358
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);
363     }
364
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);
369     }
370
371     char buf[64];
372     sprintf(buf, "/engines/engine[%d]", _nextEngine++);
373     EngRec* er = new EngRec();
374     er->eng = thruster;
375     er->prefix = dup(buf);
376     _pistons.add(er);
377
378     _currObj = thruster;
379 }
380
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
383 // yet.
384 int FGFDM::parseAxis(const char* name)
385 {
386     int i;
387     for(i=0; i<_axes.size(); i++) {
388         AxisRec* a = (AxisRec*)_axes.get(i);
389         if(eq(a->name, name))
390             return a->handle;
391     }
392
393     // Not there, make a new one.
394     AxisRec* a = new AxisRec();
395     a->name = dup(name);
396     a->handle = _airplane.getControlMap()->newInput();
397     _axes.add(a);
398     return a->handle;
399 }
400
401 int FGFDM::parseOutput(const char* name)
402 {
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;
416     // error here...
417     return -1;
418 }
419
420 void FGFDM::parseWeight(XMLAttributes* a)
421 {
422     WeightRec* wr = new WeightRec();
423
424     float v[3];
425     v[0] = attrf(a, "x");
426     v[1] = attrf(a, "y");
427     v[2] = attrf(a, "z");
428
429     wr->prop = dup(a->getValue("mass-prop"));
430     wr->size = attrf(a, "size", 0);
431     wr->handle = _airplane.addWeight(v, wr->size);
432
433     _weights.add(wr);
434 }
435
436 bool FGFDM::eq(const char* a, const char* b)
437 {
438     // Figure it out for yourself. :)
439     while(*a && *b && *a++ == *b++);
440     return !(*a || *b);
441 }
442
443 char* FGFDM::dup(const char* s)
444 {
445     int len=0;
446     while(s[len++]);
447     char* s2 = new char[len+1];
448     char* p = s2;
449     while((*p++ = *s++));
450     s2[len] = 0;
451     return s2;
452 }
453
454 int FGFDM::attri(XMLAttributes* atts, char* attr)
455 {
456     if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
457     return attri(atts, attr, 0);
458 }
459
460 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
461 {
462     const char* val = atts->getValue(attr);
463     if(val == 0) return def;
464     else         return atol(val);
465 }
466
467 float FGFDM::attrf(XMLAttributes* atts, char* attr)
468 {
469     if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
470     return attrf(atts, attr, 0);
471 }
472
473 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
474 {
475     const char* val = atts->getValue(attr);
476     if(val == 0) return def;
477     else         return (float)atof(val);    
478 }
479
480 }; // namespace yasim