]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/FGFDM.cpp
-Removed .cvsignore from itself, since .cvsignore is now in the CVS
[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     fgUntie("/consumables/fuel/tank[0]/level-gal_us");
82     fgUntie("/consumables/fuel/tank[1]/level-gal_us");
83
84     // Allows the user to start with something other than full fuel
85     _airplane.setFuelFraction(fgGetFloat("/yasim/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, "jet")) {
126         Jet* j = new Jet();
127         _currObj = j;
128         v[0] = attrf(a, "x");
129         v[1] = attrf(a, "y");
130         v[2] = attrf(a, "z");
131         float mass = attrf(a, "mass") * LBS2KG;
132         j->setDryThrust(attrf(a, "thrust") * LBS2N);
133         j->setReheatThrust(attrf(a, "afterburner", 0) * LBS2N);
134         j->setPosition(v);
135         _airplane.addThruster(j, mass, v);
136         sprintf(buf, "/engines/engine[%d]", _nextEngine++);
137         EngRec* er = new EngRec();
138         er->eng = j;
139         er->prefix = dup(buf);
140         _jets.add(er);
141     } else if(eq(name, "gear")) {
142         Gear* g = new Gear();
143         _currObj = g;
144         v[0] = attrf(a, "x");
145         v[1] = attrf(a, "y");
146         v[2] = attrf(a, "z");
147         g->setPosition(v);
148         v[0] = 0;
149         v[1] = 0;
150         v[2] = attrf(a, "compression", 1);
151         g->setCompression(v);
152         g->setStaticFriction(attrf(a, "sfric", 0.8));
153         g->setDynamicFriction(attrf(a, "dfric", 0.7));
154         float transitionTime = attrf(a, "retract-time", 0);
155         _airplane.addGear(g, transitionTime);
156     } else if(eq(name, "fuselage")) {
157         float b[3];
158         v[0] = attrf(a, "ax");
159         v[1] = attrf(a, "ay");
160         v[2] = attrf(a, "az");
161         b[0] = attrf(a, "bx");
162         b[1] = attrf(a, "by");
163         b[2] = attrf(a, "bz");
164         _airplane.addFuselage(v, b, attrf(a, "width"));
165     } else if(eq(name, "tank")) {
166         v[0] = attrf(a, "x");
167         v[1] = attrf(a, "y");
168         v[2] = attrf(a, "z");
169         float density = 6.0; // gasoline, in lbs/gal
170         if(a->hasAttribute("jet")) density = 6.72; 
171         density *= LBS2KG/CM2GALS;
172         _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
173     } else if(eq(name, "ballast")) {
174         v[0] = attrf(a, "x");
175         v[1] = attrf(a, "y");
176         v[2] = attrf(a, "z");
177         _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
178     } else if(eq(name, "weight")) {
179         parseWeight(a);
180     } else if(eq(name, "stall")) {
181         Wing* w = (Wing*)_currObj;
182         w->setStall(attrf(a, "aoa") * DEG2RAD);
183         w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
184         w->setStallPeak(attrf(a, "peak", 1.5));
185     } else if(eq(name, "flap0")) {
186         ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
187                                     attrf(a, "lift"), attrf(a, "drag"));
188     } else if(eq(name, "flap1")) {
189         ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
190                                     attrf(a, "lift"), attrf(a, "drag"));
191     } else if(eq(name, "slat")) {
192         ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
193                                    attrf(a, "aoa"), attrf(a, "drag"));
194     } else if(eq(name, "spoiler")) {
195         ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
196                                       attrf(a, "lift"), attrf(a, "drag"));
197     } else if(eq(name, "actionpt")) {
198         v[0] = attrf(a, "x");
199         v[1] = attrf(a, "y");
200         v[2] = attrf(a, "z");
201         ((Thruster*)_currObj)->setPosition(v);
202     } else if(eq(name, "dir")) {
203         v[0] = attrf(a, "x");
204         v[1] = attrf(a, "y");
205         v[2] = attrf(a, "z");
206         ((Thruster*)_currObj)->setDirection(v);
207     } else if(eq(name, "control")) {
208         const char* axis = a->getValue("axis");
209         if(a->hasAttribute("output")) {
210             // assert: output type must match _currObj type!
211             const char* output = a->getValue("output");
212             int opt = 0;
213             opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
214             opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
215             opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
216             _airplane.getControlMap()->addMapping(parseAxis(axis),
217                                                   parseOutput(output),
218                                                   _currObj,
219                                                   opt);
220         } else {
221             // assert: must be under a "cruise" or "approach" tag
222             float value = attrf(a, "value", 0);
223             if(_cruiseCurr)
224                 _airplane.addCruiseControl(parseAxis(axis), value);
225             else
226                 _airplane.addApproachControl(parseAxis(axis), value);
227         }
228     } else {
229         *(int*)0=0; // unexpected tag, boom
230     }
231 }
232
233 void FGFDM::getExternalInput(float dt)
234 {
235     // The control axes
236     ControlMap* cm = _airplane.getControlMap();
237     cm->reset();
238     int i;
239     for(i=0; i<_axes.size(); i++) {
240         AxisRec* a = (AxisRec*)_axes.get(i);
241         float val = fgGetFloat(a->name, 0);
242         cm->setInput(a->handle, val);
243     }
244     cm->applyControls();
245
246     // Weights
247     for(i=0; i<_weights.size(); i++) {
248         WeightRec* wr = (WeightRec*)_weights.get(i);
249         _airplane.setWeight(wr->handle, fgGetFloat(wr->prop));
250     }
251
252     // Gear state
253     _airplane.setGearState(fgGetBool("/controls/gear-down"), dt);
254 }
255
256 void FGFDM::setOutputProperties()
257 {
258     char buf[256];
259     int i;
260     for(i=0; i<_airplane.numTanks(); i++) {
261         sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
262         fgSetFloat(buf,
263                    CM2GALS*_airplane.getFuel(i)/_airplane.getFuelDensity(i));
264     }
265
266     for(i=0; i<_pistons.size(); i++) {
267         EngRec* er = (EngRec*)_pistons.get(i);
268         PropEngine* p = (PropEngine*)er->eng;
269
270         sprintf(buf, "%s/rpm", er->prefix);
271         fgSetFloat(buf, p->getOmega() / RPM2RAD);
272
273         sprintf(buf, "%s/fuel-flow-gph", er->prefix);
274         fgSetFloat(buf, p->getFuelFlow() * (3600*2.2/5)); // FIXME, wrong
275     }
276
277     for(i=0; i<_jets.size(); i++) {
278         EngRec* er = (EngRec*)_jets.get(i);
279         Jet* j = (Jet*)er->eng;
280         
281         sprintf(buf, "%s/fuel-flow-gph", er->prefix);
282         fgSetFloat(buf, j->getFuelFlow() * (3600*2.2/6)); // FIXME, wrong
283     }
284 }
285
286 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
287 {
288     Wing* w = new Wing();
289
290     float defDihed = 0;
291     if(eq(type, "vstab"))
292         defDihed = 90;
293     else
294         w->setMirror(true);
295
296     float pos[3];
297     pos[0] = attrf(a, "x");
298     pos[1] = attrf(a, "y");
299     pos[2] = attrf(a, "z");
300     w->setBase(pos);
301
302     w->setLength(attrf(a, "length"));
303     w->setChord(attrf(a, "chord"));
304     w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
305     w->setTaper(attrf(a, "taper", 1));
306     w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
307     w->setCamber(attrf(a, "camber", 0));
308     w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD);
309
310     float effect = attrf(a, "effectiveness", 1);
311     w->setDragScale(w->getDragScale()*effect);
312
313     _currObj = w;
314     return w;
315 }
316
317 void FGFDM::parsePropeller(XMLAttributes* a)
318 {
319     float cg[3];
320     cg[0] = attrf(a, "x");
321     cg[1] = attrf(a, "y");
322     cg[2] = attrf(a, "z");
323     float mass = attrf(a, "mass") * LBS2KG;
324     float moment = attrf(a, "moment");
325     float radius = attrf(a, "radius");
326     float speed = attrf(a, "cruise-speed") * KTS2MPS;
327     float omega = attrf(a, "cruise-rpm") * RPM2RAD;
328     float power = attrf(a, "cruise-power") * HP2W;
329     float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
330
331     // Hack, fix this pronto:
332     float engP = attrf(a, "eng-power") * HP2W;
333     float engS = attrf(a, "eng-rpm") * RPM2RAD;
334
335     Propeller* prop = new Propeller(radius, speed, omega, rho, power);
336     PistonEngine* eng = new PistonEngine(engP, engS);
337     PropEngine* thruster = new PropEngine(prop, eng, moment);
338     _airplane.addThruster(thruster, mass, cg);
339
340     if(a->hasAttribute("turbo-mul")) {
341         float mul = attrf(a, "turbo-mul");
342         float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
343         eng->setTurboParams(mul, mp);
344     }
345
346     if(a->hasAttribute("takeoff-power")) {
347         float power0 = attrf(a, "takeoff-power") * HP2W;
348         float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
349         prop->setTakeoff(omega0, power0);
350     }
351
352     if(a->hasAttribute("max-rpm")) {
353         float max = attrf(a, "max-rpm") * RPM2RAD;
354         float min = attrf(a, "min-rpm") * RPM2RAD;
355         thruster->setVariableProp(min, max);
356     }
357
358     char buf[64];
359     sprintf(buf, "/engines/engine[%d]", _nextEngine++);
360     EngRec* er = new EngRec();
361     er->eng = thruster;
362     er->prefix = dup(buf);
363     _pistons.add(er);
364
365     _currObj = thruster;
366 }
367
368 // Turns a string axis name into an integer for use by the
369 // ControlMap.  Creates a new axis if this one hasn't been defined
370 // yet.
371 int FGFDM::parseAxis(const char* name)
372 {
373     int i;
374     for(i=0; i<_axes.size(); i++) {
375         AxisRec* a = (AxisRec*)_axes.get(i);
376         if(eq(a->name, name))
377             return a->handle;
378     }
379
380     // Not there, make a new one.
381     AxisRec* a = new AxisRec();
382     a->name = dup(name);
383     a->handle = _airplane.getControlMap()->newInput();
384     _axes.add(a);
385     return a->handle;
386 }
387
388 int FGFDM::parseOutput(const char* name)
389 {
390     if(eq(name, "THROTTLE"))  return ControlMap::THROTTLE;
391     if(eq(name, "MIXTURE"))   return ControlMap::MIXTURE;
392     if(eq(name, "ADVANCE"))   return ControlMap::ADVANCE;
393     if(eq(name, "REHEAT"))    return ControlMap::REHEAT;
394     if(eq(name, "PROP"))      return ControlMap::PROP;
395     if(eq(name, "BRAKE"))     return ControlMap::BRAKE;
396     if(eq(name, "STEER"))     return ControlMap::STEER;
397     if(eq(name, "EXTEND"))    return ControlMap::EXTEND;
398     if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
399     if(eq(name, "FLAP0"))     return ControlMap::FLAP0;
400     if(eq(name, "FLAP1"))     return ControlMap::FLAP1;
401     if(eq(name, "SLAT"))      return ControlMap::SLAT;
402     if(eq(name, "SPOILER"))   return ControlMap::SPOILER;
403     // error here...
404     return -1;
405 }
406
407 void FGFDM::parseWeight(XMLAttributes* a)
408 {
409     WeightRec* wr = new WeightRec();
410
411     float v[3];
412     v[0] = attrf(a, "x");
413     v[1] = attrf(a, "y");
414     v[2] = attrf(a, "z");
415
416     wr->prop = dup(a->getValue("mass-prop"));
417     wr->size = attrf(a, "size", 0);
418     wr->handle = _airplane.addWeight(v, wr->size);
419
420     _weights.add(wr);
421 }
422
423 bool FGFDM::eq(const char* a, const char* b)
424 {
425     // Figure it out for yourself. :)
426     while(*a && *b && *a++ == *b++);
427     return !(*a || *b);
428 }
429
430 char* FGFDM::dup(const char* s)
431 {
432     int len=0;
433     while(s[len++]);
434     char* s2 = new char[len+1];
435     char* p = s2;
436     while((*p++ = *s++));
437     s2[len] = 0;
438     return s2;
439 }
440
441 int FGFDM::attri(XMLAttributes* atts, char* attr)
442 {
443     if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
444     return attri(atts, attr, 0);
445 }
446
447 int FGFDM::attri(XMLAttributes* atts, char* attr, int def)
448 {
449     const char* val = atts->getValue(attr);
450     if(val == 0) return def;
451     else         return atol(val);
452 }
453
454 float FGFDM::attrf(XMLAttributes* atts, char* attr)
455 {
456     if(!atts->hasAttribute(attr)) *(int*)0=0; // boom
457     return attrf(atts, attr, 0);
458 }
459
460 float FGFDM::attrf(XMLAttributes* atts, char* attr, float def)
461 {
462     const char* val = atts->getValue(attr);
463     if(val == 0) return def;
464     else         return (float)atof(val);    
465 }
466
467 }; // namespace yasim