FlightGear next
FGFDM.cpp
Go to the documentation of this file.
1#ifdef HAVE_CONFIG_H
2# include "config.h"
3#endif
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <cstring>
8
9#include <simgear/debug/logstream.hxx>
10
11#include <Main/fg_props.hxx>
12
13#include "yasim-common.hpp"
14#include "Math.hpp"
15#include "Jet.hpp"
16#include "SimpleJet.hpp"
17#include "Gear.hpp"
18#include "Hook.hpp"
19#include "Launchbar.hpp"
20#include "Atmosphere.hpp"
21#include "PropEngine.hpp"
22#include "Propeller.hpp"
23#include "PistonEngine.hpp"
24#include "TurbineEngine.hpp"
25#include "ElectricEngine.hpp"
26#include "Rotor.hpp"
27#include "Rotorpart.hpp"
28#include "Hitch.hpp"
29#include "Surface.hpp"
30
31#include "FGFDM.hpp"
32
33namespace yasim {
34
35// Stubs, so that this can be compiled without the FlightGear
36// binary. What's the best way to handle this?
37
38// float fgGetFloat(char* name, float def) { return 0; }
39// void fgSetFloat(char* name, float val) {}
40
41FGFDM::FGFDM()
42{
43 // FIXME: read seed from somewhere?
44 int seed = 0;
45 _turb = new Turbulence(10, seed);
46}
47
48FGFDM::~FGFDM()
49{
50 for(int i=0; i<_thrusters.size(); i++) {
51 EngRec* er = (EngRec*)_thrusters.get(i);
52 delete er->eng;
53 delete er;
54 }
55
56 for(int i=0; i<_weights.size(); i++) {
57 WeightRec* wr = (WeightRec*)_weights.get(i);
58 delete wr;
59 }
60
61 for(int i=0; i<_controlOutputs.size(); i++)
62 delete (ControlOutput*)_controlOutputs.get(i);
63
64 delete _turb;
65}
66
67void FGFDM::property_associations(
68 std::function<void(const std::string& from, const std::string& to)> fn
69 )
70{
71 for (auto& a: _property_to_properties) {
72 for (auto& b: a.second) {
73 fn(a.first, b);
74 }
75 }
76}
77
78void FGFDM::iterate(float dt)
79{
80 getExternalInput(dt);
81 _airplane.iterate(dt);
82
83 // Do fuel stuff
84 for(int i=0; i<_airplane.numThrusters(); i++) {
85 Thruster* t = _airplane.getThruster(i);
86
87 bool out_of_fuel = _fuel_props[i]._out_of_fuel->getBoolValue();
88 t->setFuelState(!out_of_fuel);
89
90 double consumed = _fuel_props[i]._fuel_consumed_lbs->getDoubleValue();
91 _fuel_props[i]._fuel_consumed_lbs->setDoubleValue(
92 consumed + dt * KG2LBS * t->getFuelFlow());
93 }
94 for(int i=0; i<_airplane.numTanks(); i++) {
95 _airplane.setFuel(i, LBS2KG * _tank_level_lbs[i]->getFloatValue());
96 }
97 _airplane.calcFuelWeights();
98
99 setOutputProperties(dt);
100}
101
102Airplane* FGFDM::getAirplane()
103{
104 return &_airplane;
105}
106
107void FGFDM::init()
108{
109 //reset id generator, needed on simulator reset/re-init
110 Surface::resetIDgen();
111 _turb_magnitude_norm = fgGetNode("/environment/turbulence/magnitude-norm", true);
112 _turb_rate_hz = fgGetNode("/environment/turbulence/rate-hz", true);
113
114 _yasimN = fgGetNode("/fdm/yasim", true);
115 _gross_weight_lbs = _yasimN->getNode("gross-weight-lbs", true);
116
117 // alias to older name
118 fgGetNode("/yasim/gross-weight-lbs", true)->alias(_gross_weight_lbs, false);
119
120 // write some compile time information to property tree
121 _yasimN->getNode("config-version",true)->setIntValue(_airplane.getVersion());
122 _yasimN->getNode("model/cg-x-min",true)->setFloatValue(_airplane.getCGHardLimitXMin());
123 _yasimN->getNode("model/cg-x-max",true)->setFloatValue(_airplane.getCGHardLimitXMax());
124
125 // prepare nodes for write at runtime
126 _cg_x = _yasimN->getNode("cg-x-m", true);
127 _cg_xmacN = _yasimN->getNode("cg-x-mac", true);
128 _cg_y = _yasimN->getNode("cg-y-m", true);
129 _cg_z = _yasimN->getNode("cg-z-m", true);
130 _vxN = _yasimN->getNode("velocities/v-x", true);
131 _vyN = _yasimN->getNode("velocities/v-y", true);
132 _vzN = _yasimN->getNode("velocities/v-z", true);
133 _vrxN = _yasimN->getNode("velocities/vrot-x", true);
134 _vryN = _yasimN->getNode("velocities/vrot-y", true);
135 _vrzN = _yasimN->getNode("velocities/vrot-z", true);
136 _axN = _yasimN->getNode("accelerations/a-x", true);
137 _ayN = _yasimN->getNode("accelerations/a-y", true);
138 _azN = _yasimN->getNode("accelerations/a-z", true);
139 _arxN = _yasimN->getNode("accelerations/arot-x", true);
140 _aryN = _yasimN->getNode("accelerations/arot-y", true);
141 _arzN = _yasimN->getNode("accelerations/arot-z", true);
142
143 // Allows the user to start with something other than full fuel
144 _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
145
146 // stash engine/thruster properties
147 _thrust_props.clear();
148 for (int i=0; i<_thrusters.size(); i++) {
149 SGPropertyNode_ptr node = fgGetNode("engines/engine", i, true);
150 Thruster* t = ((EngRec*)_thrusters.get(i))->eng;
151
152 ThrusterProps tp;
153 tp._running = node->getChild("running", 0, true);
154 tp._cranking = node->getChild("cranking", 0, true);
155 tp._prop_thrust = node->getChild("prop-thrust", 0, true); // Deprecated name
156 tp._thrust_lbs = node->getChild("thrust-lbs", 0, true);
157 tp._fuel_flow_gph = node->getChild("fuel-flow-gph", 0, true);
158
159 if(t->getPropEngine())
160 {
161 tp._rpm = node->getChild("rpm", 0, true);
162 tp._torque_ftlb = node->getChild("torque-ftlb", 0, true);
163
164 PropEngine* p = t->getPropEngine();
165 if(p->getEngine()->isPistonEngine())
166 {
167 tp._mp_osi = node->getChild("mp-osi", 0, true);
168 tp._mp_inhg = node->getChild("mp-inhg", 0, true);
169 tp._egt_degf = node->getChild("egt-degf", 0, true);
170
171 tp._oil_temperature_degf = node->getChild("oil-temperature-degf", 0, true);
172 tp._boost_gauge_inhg = node->getChild("boost-gauge-inhg", 0, true);
173 } else if(p->getEngine()->isTurbineEngine()) {
174 tp._n2 = node->getChild("n2", 0, true);
175 }
176 }
177
178 if(t->getJet())
179 {
180 tp._n1 = node->getChild("n1", 0, true);
181 tp._n2 = node->getChild("n2", 0, true);
182 tp._epr = node->getChild("epr", 0, true);
183 tp._egt_degf = node->getChild("egt-degf", 0, true);
184 }
185 _thrust_props.push_back(tp);
186 }
187
188 // stash properties for fuel state
189 _fuel_props.clear();
190 for(int i=0; i<_airplane.numThrusters(); i++) {
191 SGPropertyNode_ptr e = fgGetNode("engines/engine", i, true);
192 FuelProps f;
193 f._out_of_fuel = e->getChild("out-of-fuel", 0, true);
194 f._fuel_consumed_lbs = e->getChild("fuel-consumed-lbs", 0, true);
195 _fuel_props.push_back(f);
196 }
197
198 // initialize tanks and stash properties for tank level
199 _tank_level_lbs.clear();
200 for(int i=0; i<_airplane.numTanks(); i++) {
201 char buf[256];
202 snprintf(buf, 256, "/consumables/fuel/tank[%d]/level-lbs", i);
203 fgSetDouble(buf, _airplane.getFuel(i) * KG2LBS);
204 _tank_level_lbs.push_back(fgGetNode(buf, true));
205
206 double density = _airplane.getFuelDensity(i);
207 snprintf(buf, 256, "/consumables/fuel/tank[%d]/density-ppg", i);
208 fgSetDouble(buf, density * (KG2LBS/CM2GALS));
209
210// set in TankProperties class
211// sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
212// fgSetDouble(buf, _airplane.getFuel(i) * CM2GALS / density);
213
214 snprintf(buf, 256, "/consumables/fuel/tank[%d]/capacity-gal_us", i);
215 fgSetDouble(buf, CM2GALS * _airplane.getTankCapacity(i)/density);
216 }
217
218 if (_yasimN->getBoolValue("respect-external-gear-state") == false) {
219 fgSetBool("/controls/gear/gear-down", true);
220 }
221
222 _airplane.getModel()->setTurbulence(_turb);
223}
224
225void FGFDM::endElement(const char* name)
226{
227 _xml_depth -= 1;
228}
229
230// Not the worlds safest parser. But it's short & sweet.
231void FGFDM::startElement(const char* name, const XMLAttributes &a)
232{
233 _xml_depth += 1;
234 //XMLAttributes* a = (XMLAttributes*)&atts;
235 float v[3] {0,0,0};
236
237 if (_xml_depth < _xml_last_control_depth) {
238 // We use _control_to_input_properties and
239 // _control_to_output_properties to link input/output properties via
240 // their 'control' attributes. For example a vstab might have:
241 //
242 // <control-input control="FLAP0" axis="/controls/flight/flaps"/>
243 // <control-output control="FLAP0" prop="/surface-positions/flap-pos-norm"/>
244 //
245 // When parsing <control-input> we associate FLAP0 with
246 // /controls/flight/flaps in _control_to_input_properties. Similarly
247 // when parsing <control-output> we associate FLAPS0 with
248 // /surface-positions/flap-pos-norm in _control_to_output_properties.
249 //
250 // We can then use _control_to_input_properties and
251 // _control_to_output_properties to associate properties
252 // that are associated with the same control - in this case
253 // /controls/flight/flaps and /surface-positions/flap-pos-norm are both
254 // associated with FLAPS0.
255 //
256 // To make this work, we need to clear these maps when we have finished
257 // parsing each container of <control-input> and <control-output>,
258 // which we do here.
259 //
260 _control_to_input_properties.clear();
261 _control_to_output_properties.clear();
262 }
263
264 if(!strcmp(name, "airplane")) { parseAirplane(&a); }
265 else if(!strcmp(name, "approach") || !strcmp(name, "cruise")) {
266 parseApproachCruise(&a, name);
267 }
268 else if(!strcmp(name, "solve-weight")) { parseSolveWeight(&a); }
269 else if(!strcmp(name, "cockpit")) { parseCockpit(&a); }
270 else if(!strcmp(name, "rotor")) { parseRotor(&a, name); }
271 else if(!strcmp(name, "rotorgear")) { parseRotorGear(&a); }
272 else if(!strcmp(name, "wing") || !strcmp(name, "hstab") || !strcmp(name, "vstab") || !strcmp(name, "mstab")) {
273 parseWing(&a, name, &_airplane);
274 }
275 else if(!strcmp(name, "piston-engine")) { parsePistonEngine(&a); }
276 else if(!strcmp(name, "turbine-engine")) { parseTurbineEngine(&a); }
277 else if(!strcmp(name, "electric-engine")) { parseElectricEngine(&a); }
278 else if(!strcmp(name, "propeller")) { parsePropeller(&a); }
279 else if(!strcmp(name, "thruster")) { parseThruster(&a); }
280 else if(!strcmp(name, "jet")) { parseJet(&a); }
281 else if(!strcmp(name, "hitch")) { parseHitch(&a); }
282 else if(!strcmp(name, "tow")) { parseTow(&a); }
283 else if(!strcmp(name, "winch")) { parseWinch(&a); }
284 else if(!strcmp(name, "gear")) { parseGear(&a); }
285 else if(!strcmp(name, "hook")) { parseHook(&a); }
286 else if(!strcmp(name, "launchbar")) { parseLaunchbar(&a); }
287 else if(!strcmp(name, "fuselage")) { parseFuselage(&a); }
288 else if(!strcmp(name, "tank")) { parseTank(&a); }
289 else if(!strcmp(name, "ballast")) { parseBallast(&a); }
290 else if(!strcmp(name, "weight")) { parseWeight(&a); }
291 else if(!strcmp(name, "stall")) { parseStall(&a); }
292 else if(!strcmp(name, "flap0") || !strcmp(name, "flap1") || !strcmp(name, "spoiler") || !strcmp(name, "slat")) {
293 parseFlap(&a, name);
294 }
295 else if(!strcmp(name, "actionpt")) {
296 attrf_xyz(&a, v);
297 ((Thruster*)_currObj)->setPosition(v);
298 }
299 else if(!strcmp(name, "dir")) {
300 attrf_xyz(&a, v);
301 ((Thruster*)_currObj)->setDirection(v);
302 }
303 else if(!strcmp(name, "control-setting")) { parseControlSetting(&a); }
304 else if(!strcmp(name, "control-input")) {
305 _xml_last_control_depth = _xml_depth;
306 parseControlIn(&a);
307 }
308 else if(!strcmp(name, "control-output")) {
309 _xml_last_control_depth = _xml_depth;
310 parseControlOut(&a);
311 }
312 else if(!strcmp(name, "control-speed")) { parseControlSpeed(&a); }
313 else {
314 SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '" << name << "' found in YASim aircraft description");
315 exit(1);
316 }
317} // startElement
318
319void FGFDM::parseAirplane(const XMLAttributes* a)
320{
321 float f {0};
322 if(a->hasAttribute("mass")) { f = attrf(a, "mass") * LBS2KG; }
323 else if (a->hasAttribute("mass-lbs")) { f = attrf(a, "mass-lbs") * LBS2KG; }
324 else if (a->hasAttribute("mass-kg")) { f = attrf(a, "mass-kg"); }
325 else {
326 SG_LOG(SG_FLIGHT,SG_ALERT,"YASim fatal: missing attribute, airplane needs one of {mass-lbs, mass-kg}");
327 exit(1);
328 }
329 _airplane.setEmptyWeight(f);
330 if(a->hasAttribute("version")) { _airplane.setVersion(a->getValue("version")); }
331 if( !_airplane.isVersionOrNewer( YASIM_VERSION::CURRENT ) ) {
332 SG_LOG(SG_FLIGHT, SG_DEV_ALERT, "This aircraft does not use the latest yasim configuration version.");
333 }
334 _airplane.setDesiredCGRangeInPercentOfMAC(attrf(a, "cg-min", 0.25f), attrf(a, "cg-max", 0.3f));
335
336 if (a->hasAttribute("mtow-lbs")) { _airplane.setMTOW(attrf(a, "mtow-lbs") * LBS2KG); }
337 else if (a->hasAttribute("mtow-kg")) { _airplane.setMTOW(attrf(a, "mtow-kg")); }
338 if (a->hasAttribute("solver-mode")) { _airplane.setSolverMode(attri(a, "solver-mode")); }
339}
340
341void FGFDM::parseApproachCruise(const XMLAttributes* a, const char* name)
342{
343 float spd, alt = 0;
344 if (a->hasAttribute("speed")) { spd = attrf(a, "speed") * KTS2MPS; }
345 else if (a->hasAttribute("speed-kt")) { spd = attrf(a, "speed-kt") * KTS2MPS; }
346 else if (a->hasAttribute("speed-kmh")) { spd = attrf(a, "speed-kmh") * KMH2MPS; }
347 else {
348 SG_LOG(SG_FLIGHT,SG_ALERT,"YASim fatal: missing attribute, "<< name << " needs one of {speed-kt, speed-kmh}");
349 exit(1);
350 }
351 if (a->hasAttribute("alt")) { alt = attrf(a, "alt") * FT2M; }
352 else if (a->hasAttribute("alt-ft")) { alt = attrf(a, "alt-ft") * FT2M; }
353 else if (a->hasAttribute("alt-m")) { alt = attrf(a, "alt-m"); }
354 float gla = attrf(a, "glide-angle", 0) * DEG2RAD;
355 if (!strcmp(name, "approach")) {
356 float aoa = attrf(a, "aoa", 0) * DEG2RAD;
357 _airplaneCfg = Airplane::Configuration::APPROACH;
358 _airplane.setConfig(_airplaneCfg, spd, alt, attrf(a, "fuel", 0.2), gla, aoa);
359 }
360 else {
361 _airplaneCfg = Airplane::Configuration::CRUISE;
362 _airplane.setConfig(_airplaneCfg, spd, alt, attrf(a, "fuel", 0.5), gla);
363 }
364}
365
366void FGFDM::parseSolveWeight(const XMLAttributes* a)
367{
368 float f {0};
369 int idx = attri(a, "idx");
370 if(a->hasAttribute("weight")) { f = attrf(a, "weight") * LBS2KG; }
371 else if(a->hasAttribute("weight-lbs")) { f = attrf(a, "weight-lbs") * LBS2KG; }
372 else if(a->hasAttribute("weight-kg")) { f = attrf(a, "weight-kg"); }
373 else {
374 SG_LOG(SG_FLIGHT,SG_ALERT,"YASim fatal: missing attribute, solve-weight needs one of {weight-lbs, weight-kg}");
375 exit(1);
376 }
377 _airplane.addSolutionWeight(_airplaneCfg, idx, f);
378}
379
380void FGFDM::parseCockpit(const XMLAttributes* a)
381{
382 float v[3];
383 attrf_xyz(a, v);
384 _airplane.setPilotPos(v);
385}
386
387
388void FGFDM::getExternalInput(float dt)
389{
390 char buf[256];
391
392 _turb->setMagnitude(_turb_magnitude_norm->getFloatValue());
393 _turb->update(dt, _turb_rate_hz->getFloatValue());
394
395 // The control axes
396 ControlMap* cm = _airplane.getControlMap();
397 cm->reset();
398
399 for(int i=0; i < cm->numProperties(); i++) {
400 ControlMap::PropHandle *p = cm->getProperty(i);
401 float val = fgGetFloat(p->name, 0);
402 cm->setInput(p->handle, val);
403 }
404 cm->applyControls(dt);
405
406 // Weights
407 for(int i=0; i<_weights.size(); i++) {
408 WeightRec* wr = (WeightRec*)_weights.get(i);
409 _airplane.setWeight(wr->handle, LBS2KG * fgGetFloat(wr->prop));
410 }
411
412 for(int i=0; i<_thrusters.size(); i++) {
413 EngRec* er = (EngRec*)_thrusters.get(i);
414 Thruster* t = er->eng;
415
416 if(t->getPropEngine()) {
417 PropEngine* p = t->getPropEngine();
418 snprintf(buf, 256, "%s/rpm", er->prefix.c_str());
419 p->setOmega(fgGetFloat(buf, 500) * RPM2RAD);
420 }
421 }
422}
423
424// Linearly "seeks" a property by the specified fraction of the way to
425// the target value. Used to emulate "slowly changing" output values.
426static void moveprop(SGPropertyNode* node, const std::string& prop,
427 float target, float frac)
428{
429 float val = node->getFloatValue(prop);
430 if(frac > 1) frac = 1;
431 if(frac < 0) frac = 0;
432 val += (target - val) * frac;
433 node->setFloatValue(prop, val);
434}
435
436void FGFDM::setOutputProperties(float dt)
437{
438 float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
439 _gross_weight_lbs->setFloatValue(grossWgt);
440
441 float cg[3];
442 _airplane.getModel()->getBody()->getCG(cg);
443 _cg_x->setFloatValue(cg[0]);
444 _cg_y->setFloatValue(cg[1]);
445 _cg_z->setFloatValue(cg[2]);
446 _cg_xmacN->setFloatValue(_airplane.getCGMAC());
447
448 State* s = _airplane.getModel()->getState();
449 float v[3], acc[3], rot[3], racc[3];
450 Math::vmul33(s->orient, s->v, v);
451 Math::vmul33(s->orient, s->acc, acc);
452 Math::vmul33(s->orient, s->rot, rot);
453 Math::vmul33(s->orient, s->racc, racc);
454
455 _vxN->setFloatValue(v[0]);
456 _vyN->setFloatValue(v[1]);
457 _vzN->setFloatValue(v[2]);
458 _vrxN->setFloatValue(rot[0]);
459 _vryN->setFloatValue(rot[1]);
460 _vrzN->setFloatValue(rot[2]);
461 _axN->setFloatValue(acc[0]);
462 _ayN->setFloatValue(acc[1]);
463 _azN->setFloatValue(acc[2]);
464 _arxN->setFloatValue(racc[0]);
465 _aryN->setFloatValue(racc[1]);
466 _arzN->setFloatValue(racc[2]);
467
468 ControlMap* cm = _airplane.getControlMap();
469 for(int i=0; i<_controlOutputs.size(); i++) {
470 ControlOutput* p = (ControlOutput*)_controlOutputs.get(i);
471 float val = (p->left
472 ? cm->getOutput(p->handle)
473 : cm->getOutputR(p->handle));
474 float rmin = cm->rangeMin(p->control);
475 float rmax = cm->rangeMax(p->control);
476 float frac = (val - rmin) / (rmax - rmin);
477 // clamp output
478 val = frac*(p->max - p->min) + p->min;
479 p->prop->setFloatValue(val);
480 }
481
482 for(int i=0; i<_airplane.getRotorgear()->getNumRotors(); i++) {
483 Rotor*r=(Rotor*)_airplane.getRotorgear()->getRotor(i);
484 int j = 0;
485 float f;
486 char b[300];
487 while((j = r->getValueforFGSet(j, b, &f)))
488 if(b[0]) fgSetFloat(b,f);
489 j=0;
490 while((j = _airplane.getRotorgear()->getValueforFGSet(j, b, &f)))
491 if(b[0]) fgSetFloat(b,f);
492 for(j=0; j < r->numRotorparts(); j+=r->numRotorparts()>>2) {
493 Rotorpart* s = (Rotorpart*)r->getRotorpart(j);
494 char *b;
495 int k;
496 for(k=0; k<2; k++) {
497 b=s->getAlphaoutput(k);
498 if(b[0]) fgSetFloat(b, s->getAlpha(k));
499 }
500 }
501 }
502
503 // Use the density of the first tank, or a dummy value if no tanks
504 float fuelDensity = 1.0;
505 if(_airplane.numTanks())
506 fuelDensity = _airplane.getFuelDensity(0);
507 for(int i=0; i<_thrusters.size(); i++) {
508 EngRec* er = (EngRec*)_thrusters.get(i);
509 Thruster* t = er->eng;
510 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
511
512 ThrusterProps& tp = _thrust_props[i];
513
514 // Set: running, cranking, prop-thrust, max-hp, power-pct
515 tp._running->setBoolValue(t->isRunning());
516 tp._cranking->setBoolValue(t->isCranking());
517
518 float tmp[3];
519 t->getThrust(tmp);
520 float lbs = Math::mag3(tmp) * (KG2LBS/9.8);
521 if (tmp[0] < 0) lbs = -lbs; // Show negative thrust in properties.
522 tp._prop_thrust->setFloatValue(lbs); // Deprecated name
523 tp._thrust_lbs->setFloatValue(lbs);
524 tp._fuel_flow_gph->setFloatValue(
525 (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
526
527 if(t->getPropEngine()) {
528 PropEngine* p = t->getPropEngine();
529 tp._rpm->setFloatValue(p->getOmega() * (1/RPM2RAD));
530 tp._torque_ftlb->setFloatValue(
531 p->getEngine()->getTorque() * NM2FTLB);
532
533 if(p->getEngine()->isPistonEngine()) {
534 PistonEngine* pe = p->getEngine()->isPistonEngine();
535 tp._mp_osi->setFloatValue(pe->getMP() * (1/INHG2PA));
536 tp._mp_inhg->setFloatValue(pe->getMP() * (1/INHG2PA));
537 tp._egt_degf->setFloatValue(
538 pe->getEGT() * K2DEGF + K2DEGFOFFSET);
539 tp._oil_temperature_degf->setFloatValue(
540 pe->getOilTemp() * K2DEGF + K2DEGFOFFSET);
541 tp._boost_gauge_inhg->setFloatValue(
542 pe->getBoost() * (1/INHG2PA));
543 } else if(p->getEngine()->isTurbineEngine()) {
544 TurbineEngine* te = p->getEngine()->isTurbineEngine();
545 tp._n2->setFloatValue(te->getN2());
546 }
547 }
548
549 if(t->getJet()) {
550 Jet* j = t->getJet();
551 tp._n1->setFloatValue(j->getN1());
552 tp._n2->setFloatValue(j->getN2());
553 tp._epr->setFloatValue(j->getEPR());
554 tp._egt_degf->setFloatValue(
555 j->getEGT() * K2DEGF + K2DEGFOFFSET);
556
557 // These are "unmodeled" values that are still needed for
558 // many cockpits. Tie them all to the N1 speed, but
559 // normalize the numbers to the range [0:1] so the
560 // cockpit code can scale them to the right values.
561 float pnorm = j->getPerfNorm();
562 moveprop(node, "oilp-norm", pnorm, dt/3); // 3s seek time
563 moveprop(node, "oilt-norm", pnorm, dt/30); // 30s
564 moveprop(node, "itt-norm", pnorm, dt/1); // 1s
565 }
566 }
567}
568
569void FGFDM::parseWing(const XMLAttributes* a, const char* type, Airplane* airplane)
570{
571 float defDihed = 0;
572 bool mirror = true;
573
574 if(!strcmp(type, "vstab")) {
575 defDihed = 90;
576 mirror = false;
577 }
578
579 float base[3] {0,0,0};
580 float chord {0};
581 float length = attrf(a, "length");
582
583 // positive incidence/twist means positive AoA (leading edge up).
584 // Due to the coordinate system used in class Surface the sign will be inverted (only) there.
585 float incidence {0};
586 float twist = attrf(a, "twist", 0) * DEG2RAD;
587
588 // if this element is declared as section of a wing, skip attributes
589 // that are ignored in class Wing anyway because they are calculated
590 float isSection = attrb(a, "append");
591 if (!isSection) {
592 attrf_xyz(a, base);
593 chord = attrf(a, "chord");
594 incidence = attrf(a, "incidence", 0) * DEG2RAD;
595 }
596 else {
597 if (a->hasAttribute("x") || a->hasAttribute("y") || a->hasAttribute("z") ||
598 a->hasAttribute("chord") || a->hasAttribute("incidence")
599 ) {
600 SG_LOG(SG_FLIGHT, SG_WARN, "YASim warning: redundant attribute in wing definition \n"
601 "when using <wing append=\"1\" ...> x, y, z, chord and incidence will be ignored. ");
602 }
603 }
604
605 // optional attributes (with defaults)
606 float sweep = attrf(a, "sweep", 0) * DEG2RAD;
607 float taper = attrf(a, "taper", 1);
608 float dihedral = attrf(a, "dihedral", defDihed) * DEG2RAD;
609
610
611 float camber = attrf(a, "camber", 0);
612 if (!airplane->isVersionOrNewer(YASIM_VERSION::V_2017_2) && (camber == 0)) {
613 SG_LOG(SG_FLIGHT, SG_DEV_WARN, "YASIM warning: versions before 2017.2 are buggy for wings with camber=0");
614 }
615
616 // The 70% is a magic number that sorta kinda seems to match known
617 // throttle settings to approach speed.
618 float idrag = 0.7*attrf(a, "idrag", 1);
619
620 // get wing object by type
621 Wing* w;
622 if (!strcmp(type, "wing"))
623 {
624 w = airplane->getWing();
625 }
626 else if (!strcmp(type, "hstab")) {
627 w = airplane->getTail();
628 if (a->hasAttribute("incidence-max-deg")) w->setIncidenceMax(attrf(a, "incidence-max-deg") * DEG2RAD);
629 if (a->hasAttribute("incidence-min-deg")) w->setIncidenceMin(attrf(a, "incidence-min-deg") * DEG2RAD);
630 } else {
631 w = new Wing(airplane, mirror);
632 }
633 // add section; if wing object has already section, base will be overridden
634 // by tip of last section
635 _wingSection = w->addWingSection(base, chord, length, taper, sweep, dihedral, twist, camber, idrag, incidence);
636 if (!strcmp(type, "vstab") || !strcmp(type, "mstab"))
637 {
638 airplane->addVStab(w);
639 }
640
641 float dragFactor = attrf(a, "pdrag", 1);
642 if (a->hasAttribute("effectiveness")) {
643/* FIXME:
644 * check if all attibutes have "good" names and update parser AND documentation together
645 * only after that issue warnings
646 SG_LOG(SG_FLIGHT, SG_ALERT, "Warning: " <<
647 "deprecated attribute 'effectiveness' in YASim configuration file. " <<
648 "Use 'pdrag' instead to add parasitic drag.");
649*/
650 dragFactor = attrf(a, "effectiveness", 1);
651 }
652 w->setSectionDrag(_wingSection, dragFactor);
653 if (a->hasAttribute("flow")) {
654 const char* flowregime = a->getValue("flow");
655 if (!strcmp(flowregime,"TRANSONIC")) {
656 w->setFlowRegime(FLOW_TRANSONIC);
657 const float mcrit = attrf(a,"mcrit", 0.6f);
658 if ( (mcrit > 0.0f) && (mcrit <= 1.0f)) {
659 w->setCriticalMachNumber(mcrit);
660 } else {
661 SG_LOG(SG_FLIGHT, SG_ALERT, "YASim warning: invalid input for critical mach number. Defaulting to mcrit=0.6.");
662 }
663 }
664 } else {
665 w->setFlowRegime(FLOW_SUBSONIC);
666 }
667 _currObj = w;
668}
669
670void FGFDM::parseRotor(const XMLAttributes* a, const char* type)
671{
672 Rotor* w = new Rotor();
673
674 // float defDihed = 0;
675
676 float pos[3];
677 attrf_xyz(a, pos);
678 w->setBase(pos);
679
680 float normal[3];
681 normal[0] = attrf(a, "nx");
682 normal[1] = attrf(a, "ny");
683 normal[2] = attrf(a, "nz");
684 w->setNormal(normal);
685
686 float forward[3];
687 forward[0] = attrf(a, "fx");
688 forward[1] = attrf(a, "fy");
689 forward[2] = attrf(a, "fz");
690 w->setForward(forward);
691
692 w->setMaxCyclicail(attrf(a, "maxcyclicail", 7.6));
693 w->setMaxCyclicele(attrf(a, "maxcyclicele", 4.94));
694 w->setMinCyclicail(attrf(a, "mincyclicail", -7.6));
695 w->setMinCyclicele(attrf(a, "mincyclicele", -4.94));
696 w->setMaxCollective(attrf(a, "maxcollective", 15.8));
697 w->setMinCollective(attrf(a, "mincollective", -0.2));
698 w->setDiameter(attrf(a, "diameter", 10.2));
699 w->setWeightPerBlade(attrf(a, "weightperblade", 44));
700 w->setNumberOfBlades(attrf(a, "numblades", 4));
701 w->setRelBladeCenter(attrf(a, "relbladecenter", 0.7));
702 w->setDynamic(attrf(a, "dynamic", 0.7));
703 w->setDelta3(attrf(a, "delta3", 0));
704 w->setDelta(attrf(a, "delta", 0));
705 w->setTranslift(attrf(a, "translift", 0.05));
706 w->setC2(attrf(a, "dragfactor", 1));
707 w->setStepspersecond(attrf(a, "stepspersecond", 120));
708 w->setPhiNull((attrf(a, "phi0", 0))*YASIM_PI/180);
709 w->setRPM(attrf(a, "rpm", 424));
710 w->setRelLenHinge(attrf(a, "rellenflaphinge", 0.07));
711 w->setAlpha0((attrf(a, "flap0", -5))*YASIM_PI/180);
712 w->setAlphamin((attrf(a, "flapmin", -15))/180*YASIM_PI);
713 w->setAlphamax((attrf(a, "flapmax", 15))*YASIM_PI/180);
714 w->setAlpha0factor(attrf(a, "flap0factor", 1));
715 w->setTeeterdamp(attrf(a,"teeterdamp",.0001));
716 w->setMaxteeterdamp(attrf(a,"maxteeterdamp",1000));
717 w->setRelLenTeeterHinge(attrf(a,"rellenteeterhinge",0.01));
718 w->setBalance(attrf(a,"balance",1.0));
719 w->setMinTiltYaw(attrf(a,"mintiltyaw",0.0));
720 w->setMinTiltPitch(attrf(a,"mintiltpitch",0.0));
721 w->setMinTiltRoll(attrf(a,"mintiltroll",0.0));
722 w->setMaxTiltYaw(attrf(a,"maxtiltyaw",0.0));
723 w->setMaxTiltPitch(attrf(a,"maxtiltpitch",0.0));
724 w->setMaxTiltRoll(attrf(a,"maxtiltroll",0.0));
725 w->setTiltCenterX(attrf(a,"tiltcenterx",0.0));
726 w->setTiltCenterY(attrf(a,"tiltcentery",0.0));
727 w->setTiltCenterZ(attrf(a,"tiltcenterz",0.0));
728 w->setDownwashFactor(attrf(a, "downwashfactor", 1));
729 if(attrb(a,"ccw"))
730 w->setCcw(1);
731 if(attrb(a,"sharedflaphinge"))
732 w->setSharedFlapHinge(true);
733
734 if(a->hasAttribute("name")) w->setName(a->getValue("name"));
735 if(a->hasAttribute("alphaout0")) w->setAlphaoutput(0,a->getValue("alphaout0"));
736 if(a->hasAttribute("alphaout1")) w->setAlphaoutput(1,a->getValue("alphaout1"));
737 if(a->hasAttribute("alphaout2")) w->setAlphaoutput(2,a->getValue("alphaout2"));
738 if(a->hasAttribute("alphaout3")) w->setAlphaoutput(3,a->getValue("alphaout3"));
739 if(a->hasAttribute("coneout")) w->setAlphaoutput(4,a->getValue("coneout"));
740 if(a->hasAttribute("yawout")) w->setAlphaoutput(5,a->getValue("yawout"));
741 if(a->hasAttribute("rollout")) w->setAlphaoutput(6,a->getValue("rollout"));
742
743 w->setPitchA(attrf(a, "pitch-a", 10));
744 w->setPitchB(attrf(a, "pitch-b", 10));
745 w->setForceAtPitchA(attrf(a, "forceatpitch-a", 3000));
746 w->setPowerAtPitch0(attrf(a, "poweratpitch-0", 300));
747 w->setPowerAtPitchB(attrf(a, "poweratpitch-b", 3000));
748 if(attrb(a,"notorque")) w->setNotorque(1);
749
750#define p(x) if (a->hasAttribute(#x)) w->setParameter((char *)#x,attrf(a,#x) );
751#define p2(x,y) if (a->hasAttribute(y)) w->setParameter((char *)#x,attrf(a,y) );
752 p2(translift_ve,"translift-ve")
753 p2(translift_maxfactor,"translift-maxfactor")
754 p2(ground_effect_constant,"ground-effect-constant")
755 p2(vortex_state_lift_factor,"vortex-state-lift-factor")
756 p2(vortex_state_c1,"vortex-state-c1")
757 p2(vortex_state_c2,"vortex-state-c2")
758 p2(vortex_state_c3,"vortex-state_c3")
759 p2(vortex_state_e1,"vortex-state-e1")
760 p2(vortex_state_e2,"vortex-state-e2")
761 p(twist)
762 p2(number_of_segments,"number-of-segments")
763 p2(number_of_parts,"number-of-parts")
764 p2(rel_len_where_incidence_is_measured,"rel-len-where-incidence-is-measured")
765 p(chord)
766 p(taper)
767 p2(airfoil_incidence_no_lift,"airfoil-incidence-no-lift")
768 p2(rel_len_blade_start,"rel-len-blade-start")
769 p2(incidence_stall_zero_speed,"incidence-stall-zero-speed")
770 p2(incidence_stall_half_sonic_speed,"incidence-stall-half-sonic-speed")
771 p2(lift_factor_stall,"lift-factor-stall")
772 p2(stall_change_over,"stall-change-over")
773 p2(drag_factor_stall,"drag-factor-stall")
774 p2(airfoil_lift_coefficient,"airfoil-lift-coefficient")
775 p2(airfoil_drag_coefficient0,"airfoil-drag-coefficient0")
776 p2(airfoil_drag_coefficient1,"airfoil-drag-coefficient1")
777 p2(cyclic_factor,"cyclic-factor")
778 p2(rotor_correction_factor,"rotor-correction-factor")
779#undef p
780#undef p2
781 _currObj = w;
782 _airplane.getModel()->getRotorgear()->addRotor(w);
783} //parseRotor
784
785void FGFDM::parseRotorGear(const XMLAttributes* a)
786{
787 Rotorgear* r = _airplane.getModel()->getRotorgear();
788 _currObj = r;
789 #define p(x) if (a->hasAttribute(#x)) r->setParameter((char *)#x,attrf(a,#x) );
790 #define p2(x,y) if (a->hasAttribute(y)) r->setParameter((char *)#x,attrf(a,y) );
791 p2(max_power_engine,"max-power-engine")
792 p2(engine_prop_factor,"engine-prop-factor")
793 p(yasimdragfactor)
794 p(yasimliftfactor)
795 p2(max_power_rotor_brake,"max-power-rotor-brake")
796 p2(rotorgear_friction,"rotorgear-friction")
797 p2(engine_accel_limit,"engine-accel-limit")
798 #undef p
799 #undef p2
800 r->setInUse();
801}
802
803void FGFDM::parsePistonEngine(const XMLAttributes* a)
804{
805 float engP = attrf(a, "eng-power") * HP2W;
806 float engS = attrf(a, "eng-rpm") * RPM2RAD;
807
808 PistonEngine* eng = new PistonEngine(engP, engS);
809
810 if(a->hasAttribute("displacement"))
811 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
812
813 if(a->hasAttribute("compression"))
814 eng->setCompression(attrf(a, "compression"));
815
816 if(a->hasAttribute("min-throttle"))
817 eng->setMinThrottle(attrf(a, "min-throttle"));
818
819 if(a->hasAttribute("turbo-mul")) {
820 float mul = attrf(a, "turbo-mul");
821 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
822 eng->setTurboParams(mul, mp);
823 eng->setTurboLag(attrf(a, "turbo-lag", 2));
824 }
825
826 if(a->hasAttribute("supercharger"))
827 eng->setSupercharger(attrb(a, "supercharger"));
828
829 ((PropEngine*)_currObj)->setEngine(eng);
830}
831
832void FGFDM::parseTurbineEngine(const XMLAttributes* a)
833{
834 float power = attrf(a, "eng-power") * HP2W;
835 float omega = attrf(a, "eng-rpm") * RPM2RAD;
836 float alt = attrf(a, "alt") * FT2M;
837 float flatRating = attrf(a, "flat-rating") * HP2W;
838 TurbineEngine* eng = new TurbineEngine(power, omega, alt, flatRating);
839
840 if(a->hasAttribute("n2-low-idle"))
841 eng->setN2Range(attrf(a, "n2-low-idle"), attrf(a, "n2-high-idle"),
842 attrf(a, "n2-max"));
843
844 // Nasty units conversion: lbs/hr per hp -> kg/s per watt
845 if(a->hasAttribute("bsfc"))
846 eng->setFuelConsumption(attrf(a, "bsfc") * (LBS2KG/(3600*HP2W)));
847
848 ((PropEngine*)_currObj)->setEngine(eng);
849}
850
851void FGFDM::parseElectricEngine(const XMLAttributes* a)
852{
853 float Kv = attrf(a, "Kv") * RPM2RAD; //Kv is expected to be given as RPM per volt in XML
854 float voltage = attrf(a, "voltage"); // voltage applied at the motor windings in volts
855 float Rm = attrf(a, "Rm"); // winding resistance in Ohms
856 ElectricEngine* eng = new ElectricEngine(voltage, Kv, Rm);
857
858 ((PropEngine*)_currObj)->setEngine(eng);
859}
860
861
862void FGFDM::parsePropeller(const XMLAttributes* a)
863{
864 // Legacy Handling for the old engines syntax:
865 PistonEngine* eng = 0;
866 if(a->hasAttribute("eng-power")) {
867 SG_LOG(SG_FLIGHT,SG_ALERT, "WARNING: "
868 << "Legacy engine definition in YASim configuration file. "
869 << "Please fix.");
870 float engP = attrf(a, "eng-power") * HP2W;
871 float engS = attrf(a, "eng-rpm") * RPM2RAD;
872 eng = new PistonEngine(engP, engS);
873 if(a->hasAttribute("displacement"))
874 eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
875 if(a->hasAttribute("compression"))
876 eng->setCompression(attrf(a, "compression"));
877 if(a->hasAttribute("turbo-mul")) {
878 float mul = attrf(a, "turbo-mul");
879 float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
880 eng->setTurboParams(mul, mp);
881 }
882 }
883
884 // Now parse the actual propeller definition:
885 float cg[3];
886 attrf_xyz(a, cg);
887 float mass = attrf(a, "mass") * LBS2KG;
888 float moment = attrf(a, "moment");
889 float radius = attrf(a, "radius");
890 float speed = attrf(a, "cruise-speed") * KTS2MPS;
891 float omega = attrf(a, "cruise-rpm") * RPM2RAD;
892 float power = attrf(a, "cruise-power") * HP2W;
893 float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
894
895 Propeller* prop = new Propeller(radius, speed, omega, rho, power);
896 PropEngine* thruster = new PropEngine(prop, eng, moment);
897 _airplane.addThruster(thruster, mass, cg);
898
899 // Set the stops (fine = minimum pitch, coarse = maximum pitch)
900 float fine_stop = attrf(a, "fine-stop", 0.25f);
901 float coarse_stop = attrf(a, "coarse-stop", 4.0f);
902 prop->setStops(fine_stop, coarse_stop);
903
904 if(a->hasAttribute("takeoff-power")) {
905 float power0 = attrf(a, "takeoff-power") * HP2W;
906 float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
907 prop->setTakeoff(omega0, power0);
908 }
909
910 if(a->hasAttribute("max-rpm")) {
911 float max = attrf(a, "max-rpm") * RPM2RAD;
912 float min = attrf(a, "min-rpm") * RPM2RAD;
913 thruster->setVariableProp(min, max);
914 }
915
916 if(attrb(a, "contra"))
917 thruster->setContraPair(true);
918
919 if(a->hasAttribute("manual-pitch")) {
920 prop->setManualPitch();
921 }
922
923 thruster->setGearRatio(attrf(a, "gear-ratio", 1));
924
925 char buf[64];
926 snprintf(buf, 64, "/engines/engine[%d]", _nextEngine++);
927 EngRec* er = new EngRec();
928 er->eng = thruster;
929 er->prefix = buf;
930 _thrusters.add(er);
931
932 _currObj = thruster;
933}
934
935void FGFDM::parseThruster(const XMLAttributes* a)
936{
937 float v[3];
938 SimpleJet* j = new SimpleJet();
939 _currObj = j;
940 attrf_xyz(a, v);
941 j->setPosition(v);
942 _airplane.addThruster(j, 0, v);
943 v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
944 j->setDirection(v);
945 j->setThrust(attrf(a, "thrust") * LBS2N);
946}
947
948void FGFDM::parseJet(const XMLAttributes* a)
949{
950 float v[3];
951 Jet* j = new Jet();
952 _currObj = j;
953 attrf_xyz(a, v);
954 float mass;
955 if(a->hasAttribute("mass")) { mass = attrf(a, "mass") * LBS2KG; }
956 else if(a->hasAttribute("mass-lbs")) { mass = attrf(a, "mass-lbs") * LBS2KG; }
957 else if(a->hasAttribute("mass-kg")) { mass = attrf(a, "mass-kg"); }
958 else {
959 SG_LOG(SG_FLIGHT,SG_ALERT,"YASim fatal: missing attribute, jet needs one of {mass-lbs, mass-kg}");
960 exit(1);
961 }
962 j->setMaxThrust(attrf(a, "thrust") * LBS2N, attrf(a, "afterburner", 0) * LBS2N);
963 j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
964 j->setReverseThrust(attrf(a, "reverse", 0.2));
965
966 float n1min = attrf(a, "n1-idle", 55);
967 float n1max = attrf(a, "n1-max", 102);
968 float n2min = attrf(a, "n2-idle", 73);
969 float n2max = attrf(a, "n2-max", 103);
970 j->setRPMs(n1min, n1max, n2min, n2max);
971
972 j->setTSFC(attrf(a, "tsfc", 0.8));
973 j->setATSFC(attrf(a, "atsfc", 0.0));
974 if(a->hasAttribute("egt")) j->setEGT(attrf(a, "egt"));
975 if(a->hasAttribute("epr")) j->setEPR(attrf(a, "epr"));
976 if(a->hasAttribute("exhaust-speed"))
977 j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
978 if(a->hasAttribute("spool-time"))
979 j->setSpooling(attrf(a, "spool-time"));
980
981 j->setPosition(v);
982 _airplane.addThruster(j, mass, v);
983 char buf[64];
984 snprintf(buf, 64, "/engines/engine[%d]", _nextEngine++);
985 EngRec* er = new EngRec();
986 er->eng = j;
987 er->prefix = buf;
988 _thrusters.add(er);
989}
990
991void FGFDM::parseHitch(const XMLAttributes* a)
992{
993 float v[3];
994 Hitch* h = new Hitch(a->getValue("name"));
995 _currObj = h;
996 attrf_xyz(a, v);
997 h->setPosition(v);
998 if(a->hasAttribute("force-is-calculated-by-other")) h->setForceIsCalculatedByOther(attrb(a,"force-is-calculated-by-other"));
999 _airplane.addHitch(h);
1000}
1001
1002void FGFDM::parseTow(const XMLAttributes* a)
1003{
1004 Hitch* h = (Hitch*)_currObj;
1005 if(a->hasAttribute("length"))
1006 h->setTowLength(attrf(a, "length"));
1007 if(a->hasAttribute("elastic-constant"))
1008 h->setTowElasticConstant(attrf(a, "elastic-constant"));
1009 if(a->hasAttribute("break-force"))
1010 h->setTowBreakForce(attrf(a, "break-force"));
1011 if(a->hasAttribute("weight-per-meter"))
1012 h->setTowWeightPerM(attrf(a, "weight-per-meter"));
1013 if(a->hasAttribute("mp-auto-connect-period"))
1014 h->setMpAutoConnectPeriod(attrf(a, "mp-auto-connect-period"));
1015}
1016
1017void FGFDM::parseWinch(const XMLAttributes* a)
1018{
1019 Hitch* h = (Hitch*)_currObj;
1020 double pos[3];
1021 pos[0] = attrd(a, "x",0);
1022 pos[1] = attrd(a, "y",0);
1023 pos[2] = attrd(a, "z",0);
1024 h->setWinchPosition(pos);
1025 if(a->hasAttribute("max-speed"))
1026 h->setWinchMaxSpeed(attrf(a, "max-speed"));
1027 if(a->hasAttribute("power"))
1028 h->setWinchPower(attrf(a, "power") * 1000);
1029 if(a->hasAttribute("max-force"))
1030 h->setWinchMaxForce(attrf(a, "max-force"));
1031 if(a->hasAttribute("initial-tow-length"))
1032 h->setWinchInitialTowLength(attrf(a, "initial-tow-length"));
1033 if(a->hasAttribute("max-tow-length"))
1034 h->setWinchMaxTowLength(attrf(a, "max-tow-length"));
1035 if(a->hasAttribute("min-tow-length"))
1036 h->setWinchMinTowLength(attrf(a, "min-tow-length"));
1037}
1038
1039void FGFDM::parseGear(const XMLAttributes* a)
1040{
1041 float v[3];
1042 Gear* g = new Gear();
1043 _currObj = g;
1044
1045 /* Override (x, y, z) with wheel-* if specified. */
1046 if (a->hasAttribute("wheel-x")) {
1047 v[0] = attrf(a, "wheel-x");
1048 v[1] = attrf(a, "wheel-y");
1049 v[2] = attrf(a, "wheel-z");
1050 }
1051 else {
1052 attrf_xyz(a, v);
1053 }
1054 g->setPosition(v);
1055
1056 float nrm = Math::mag3(v);
1057 if (_vehicle_radius < nrm)
1058 _vehicle_radius = nrm;
1059 if(a->hasAttribute("upx")) {
1060 v[0] = attrf(a, "upx");
1061 v[1] = attrf(a, "upy");
1062 v[2] = attrf(a, "upz");
1063 Math::unit3(v, v);
1064 } else {
1065 v[0] = 0;
1066 v[1] = 0;
1067 v[2] = 1;
1068 }
1069 for(int i=0; i<3; i++)
1070 v[i] *= attrf(a, "compression", 1);
1071 g->setCompression(v);
1072
1073 if (a->hasAttribute("wheel-axle-x")) {
1074 v[0] = attrf(a, "wheel-axle-x");
1075 v[1] = attrf(a, "wheel-axle-y");
1076 v[2] = attrf(a, "wheel-axle-z");
1077 }
1078 else {
1079 v[0] = 0;
1080 v[1] = 1;
1081 v[2] = 0;
1082 }
1083 g->setWheelAxle( v);
1084
1085 g->setWheelRadius( attrf(a, "wheel-radius", 0));
1086 g->setTyreRadius( attrf(a, "tyre-radius", 0));
1087
1088 g->setBrake(attrf(a, "skid", 0));
1089 g->setInitialLoad(attrf(a, "initial-load", 0));
1090 g->setStaticFriction(attrf(a, "sfric", 0.8));
1091 g->setDynamicFriction(attrf(a, "dfric", 0.7));
1092 g->setSpring(attrf(a, "spring", 1));
1093 g->setSpring2(attrf(a, "spring2", 0));
1094 g->setDamping(attrf(a, "damp", 1));
1095 if(a->hasAttribute("on-water")) g->setOnWater(attrb(a,"on-water"));
1096 if(a->hasAttribute("on-solid")) g->setOnSolid(attrb(a,"on-solid"));
1097 if(a->hasAttribute("ignored-by-solver")) g->setIgnoreWhileSolving(attrb(a,"ignored-by-solver"));
1098 g->setSpringFactorNotPlaning(attrf(a, "spring-factor-not-planing", 1));
1099 g->setSpeedPlaning(attrf(a, "speed-planing", 0) * KTS2MPS);
1100 g->setReduceFrictionByExtension(attrf(a, "reduce-friction-by-extension", 0));
1101 g->setStiction(attrf(a, "stiction", 0));
1102 g->setStictionABS(attrf(a, "stiction-abs", 0));
1103 _airplane.addGear(g);
1104}
1105
1106void FGFDM::parseHook(const XMLAttributes* a)
1107{
1108 float v[3];
1109 Hook* h = new Hook();
1110 _currObj = h;
1111 attrf_xyz(a, v);
1112 h->setPosition(v);
1113 float length = attrf(a, "length", 1.0);
1114 h->setLength(length);
1115 float nrm = length+Math::mag3(v);
1116 if (_vehicle_radius < nrm)
1117 _vehicle_radius = nrm;
1118 h->setDownAngle(attrf(a, "down-angle", 70) * DEG2RAD);
1119 h->setUpAngle(attrf(a, "up-angle", 0) * DEG2RAD);
1120 _airplane.addHook(h);
1121}
1122
1123void FGFDM::parseLaunchbar(const XMLAttributes* a)
1124{
1125 float v[3];
1126 Launchbar* l = new Launchbar();
1127 _currObj = l;
1128 attrf_xyz(a, v);
1129 l->setLaunchbarMount(v);
1130 v[0] = attrf(a, "holdback-x", v[0]);
1131 v[1] = attrf(a, "holdback-y", v[1]);
1132 v[2] = attrf(a, "holdback-z", v[2]);
1133 l->setHoldbackMount(v);
1134 float length = attrf(a, "length", 1.0);
1135 l->setLength(length);
1136 l->setDownAngle(attrf(a, "down-angle", 45) * DEG2RAD);
1137 l->setUpAngle(attrf(a, "up-angle", -45) * DEG2RAD);
1138 l->setHoldbackLength(attrf(a, "holdback-length", 2.0));
1139 _airplane.addLaunchbar(l);
1140}
1141
1142void FGFDM::parseFuselage(const XMLAttributes* a)
1143{
1144 float v[3];
1145 float b[3];
1146 v[0] = attrf(a, "ax");
1147 v[1] = attrf(a, "ay");
1148 v[2] = attrf(a, "az");
1149 b[0] = attrf(a, "bx");
1150 b[1] = attrf(a, "by");
1151 b[2] = attrf(a, "bz");
1152 float taper = attrf(a, "taper", 1);
1153 float mid = attrf(a, "midpoint", 0.5);
1154 if (_airplane.isVersionOrNewer(YASIM_VERSION::V_32)) {
1155 // A fuselage's "midpoint" XML attribute is defined from the
1156 // fuselage's front end, but the Fuselage object's internal
1157 // "mid" attribute is actually defined from the rear end.
1158 // Thus YASim's original interpretation of "midpoint" was wrong.
1159 // Complement the "midpoint" value to ensure the fuselage
1160 // points the right way.
1161 mid = 1 - mid;
1162 }
1163 float cx = attrf(a, "cx", 1);
1164 float cy = attrf(a, "cy", 1);
1165 float cz = attrf(a, "cz", 1);
1166 float idrag = attrf(a, "idrag", 1);
1167 _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid, cx, cy, cz, idrag);
1168}
1169
1170void FGFDM::parseTank(const XMLAttributes* a)
1171{
1172 float v[3];
1173 attrf_xyz(a, v);
1174 float density = 6.0; // gasoline, in lbs/gal
1175 if(a->hasAttribute("jet")) density = 6.72;
1176 density *= LBS2KG*CM2GALS;
1177 float capacity = 0;
1178 if(a->hasAttribute("capacity")) { capacity = attrf(a, "capacity") * LBS2KG; }
1179 else if(a->hasAttribute("capacity-lbs")) { capacity = attrf(a, "capacity-lbs") * LBS2KG; }
1180 else if(a->hasAttribute("capacity-kg")) { capacity = attrf(a, "capacity-kg"); }
1181 else {
1182 SG_LOG(SG_FLIGHT,SG_ALERT,"YASim fatal: missing attribute, tank needs one of {capacity-lbs, capacity-kg}");
1183 exit(1);
1184 }
1185 _airplane.addTank(v, capacity, density);
1186}
1187
1188void FGFDM::parseBallast(const XMLAttributes* a)
1189{
1190 float v[3];
1191 float f;
1192 attrf_xyz(a, v);
1193 if(a->hasAttribute("mass")) { f = attrf(a, "mass") * LBS2KG; }
1194 else if (a->hasAttribute("mass-lbs")) { f = attrf(a, "mass-lbs") * LBS2KG; }
1195 else if (a->hasAttribute("mass-kg")) { f = attrf(a, "mass-kg"); }
1196 else {
1197 SG_LOG(SG_FLIGHT,SG_ALERT,"YASim fatal: missing attribute, airplane needs one of {mass-lbs, mass-kg}");
1198 exit(1);
1199 }
1200 _airplane.addBallast(v, f);
1201}
1202
1203/*
1204void FGFDM::parseXXX(const XMLAttributes* a)
1205{
1206 float v[3];
1207}
1208*/
1209
1210void FGFDM::parseWeight(const XMLAttributes* a)
1211{
1212 WeightRec* wr = new WeightRec();
1213
1214 float v[3];
1215 attrf_xyz(a, v);
1216 wr->prop = std::string{a->getValue("mass-prop")};
1217 wr->size = attrf(a, "size", 0);
1218 wr->handle = _airplane.addWeight(v, wr->size);
1219 _weights.add(wr);
1220}
1221
1222void FGFDM::parseStall(const XMLAttributes* a)
1223{
1224 Wing* w = (Wing*)_currObj;
1225 StallParams sp;
1226 sp.aoa = attrf(a, "aoa") * DEG2RAD;
1227 sp.width = attrf(a, "width", 2) * DEG2RAD;
1228 sp.peak = attrf(a, "peak", 1.5);
1229 w->setSectionStallParams(_wingSection, sp);
1230}
1231
1232void FGFDM::parseFlap(const XMLAttributes* a, const char* name)
1233{
1234 FlapParams fp;
1235 fp.start = attrf(a, "start");
1236 fp.end = attrf(a, "end");
1237 if (!strcmp(name, "slat")) {
1238 fp.aoa = attrf(a, "aoa");
1239 }
1240 else {
1241 fp.lift = attrf(a, "lift");
1242 }
1243 fp.drag = attrf(a, "drag");
1244 if (!strcmp(name, "flap0")) {
1245 ((Wing*)_currObj)->setFlapParams(_wingSection, WING_FLAP0, fp);
1246 }
1247 if (!strcmp(name, "flap1")) {
1248 ((Wing*)_currObj)->setFlapParams(_wingSection, WING_FLAP1, fp);
1249 }
1250 if (!strcmp(name, "spoiler")) {
1251 ((Wing*)_currObj)->setFlapParams(_wingSection, WING_SPOILER, fp);
1252 }
1253 if (!strcmp(name, "slat")) {
1254 ((Wing*)_currObj)->setFlapParams(_wingSection, WING_SLAT, fp);
1255 }
1256}
1257
1258void FGFDM::parseControlSetting(const XMLAttributes* a)
1259{
1260 // A cruise or approach control setting
1261 float value = attrf(a, "value", 0);
1262 _airplane.addControlSetting(_airplaneCfg, a->getValue("axis"), value);
1263}
1264
1265void FGFDM::parseControlIn(const XMLAttributes* a)
1266{
1267 // map input property to a YASim control
1268 ControlMap::ControlType control = ControlMap::parseControl(a->getValue("control"));
1269 int opt = 0;
1270 opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
1271 opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
1272 opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
1273 float src0, src1, dst0, dst1;
1274 src0 = dst0 = ControlMap::rangeMin(control);
1275 src1 = dst1 = ControlMap::rangeMax(control);
1276 if(a->hasAttribute("src0")) {
1277 src0 = attrf(a, "src0");
1278 src1 = attrf(a, "src1");
1279 dst0 = attrf(a, "dst0");
1280 dst1 = attrf(a, "dst1");
1281 }
1282 _airplane.addControlInput(a->getValue("axis"), control, _currObj, _wingSection, opt, src0, src1, dst0, dst1);
1283
1284 // Detect new associations between properties.
1285 std::string control_name = a->getValue("control");
1286 std::string in = a->getValue("axis");
1287 _control_to_input_properties[control_name].insert(in);
1288 for (auto& out: _control_to_output_properties[control_name]) {
1289 _property_to_properties[in].insert(out);
1290 }
1291}
1292
1293void FGFDM::parseControlOut(const XMLAttributes* a)
1294{
1295 // A property output for a control on the current object
1296 ControlMap* cm = _airplane.getControlMap();
1297 ControlMap::ControlType control = ControlMap::parseControl(a->getValue("control"));
1298 ControlMap::ObjectID oid = ControlMap::getObjectID(_currObj, _wingSection);
1299
1300 ControlOutput* p = new ControlOutput();
1301 p->prop = fgGetNode(a->getValue("prop"), true);
1302 p->handle = cm->getOutputHandle(oid, control);
1303 p->control = control;
1304 p->left = !(a->hasAttribute("side") &&
1305 !strcmp("right", a->getValue("side")));
1306 // for output clamping
1307 p->min = attrf(a, "min", cm->rangeMin(control));
1308 p->max = attrf(a, "max", cm->rangeMax(control));
1309 _controlOutputs.add(p);
1310
1311 // Detect new associations between properties.
1312 std::string control_name = a->getValue("control");
1313 std::string out = a->getValue("prop");
1314 _control_to_output_properties[control_name].insert(out);
1315 for (auto& in: _control_to_input_properties[control_name]) {
1316 _property_to_properties[in].insert(out);
1317 }
1318}
1319
1320void FGFDM::parseControlSpeed(const XMLAttributes* a)
1321{
1322 ControlMap* cm = _airplane.getControlMap();
1323 ControlMap::ControlType control = ControlMap::parseControl(a->getValue("control"));
1324 ControlMap::ObjectID oid = ControlMap::getObjectID(_currObj, _wingSection);
1325 int handle = cm->getOutputHandle(oid, control);
1326 float time = attrf(a, "transition-time", 0);
1327 cm->setTransitionTime(handle, time);
1328}
1329
1330int FGFDM::attri(const XMLAttributes* atts, const char* attr)
1331{
1332 if(!atts->hasAttribute(attr)) {
1333 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1334 "' in YASim aircraft description");
1335 exit(1);
1336 }
1337 return attri(atts, attr, 0);
1338}
1339
1340int FGFDM::attri(const XMLAttributes* atts, const char* attr, int def)
1341{
1342 const char* val = atts->getValue(attr);
1343 if(val == 0) return def;
1344 else return atol(val);
1345}
1346
1347float FGFDM::attrf(const XMLAttributes* atts, const char* attr)
1348{
1349 if(!atts->hasAttribute(attr)) {
1350 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1351 "' in YASim aircraft description");
1352 exit(1);
1353 }
1354 return attrf(atts, attr, 0);
1355}
1356
1357float FGFDM::attrf(const XMLAttributes* atts, const char* attr, float def)
1358{
1359 const char* val = atts->getValue(attr);
1360 if(val == 0) return def;
1361 else return (float)atof(val);
1362}
1363
1364void FGFDM::attrf_xyz(const XMLAttributes* atts, float* out)
1365{
1366 out[0] = attrf(atts, "x");
1367 out[1] = attrf(atts, "y");
1368 out[2] = attrf(atts, "z");
1369}
1370
1371double FGFDM::attrd(const XMLAttributes* atts, const char* attr)
1372{
1373 if(!atts->hasAttribute(attr)) {
1374 SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1375 "' in YASim aircraft description");
1376 exit(1);
1377 }
1378 return attrd(atts, attr, 0);
1379}
1380
1381double FGFDM::attrd(const XMLAttributes* atts, const char* attr, double def)
1382{
1383 const char* val = atts->getValue(attr);
1384 if(val == 0) return def;
1385 else return atof(val);
1386}
1387
1388// ACK: the dreaded ambiguous string boolean. Remind me to shoot Maik
1389// when I have a chance. :). Unless you have a parser that can check
1390// symbol constants (we don't), this kind of coding is just a Bad
1391// Idea. This implementation, for example, silently returns a boolean
1392// falsehood for values of "1", "yes", "True", and "TRUE". Which is
1393// especially annoying preexisting boolean attributes in the same
1394// parser want to see "1" and will choke on a "true"...
1395//
1396// Unfortunately, this usage creeped into existing configuration files
1397// while I wasn't active, and it's going to be hard to remove. Issue
1398// a warning to nag people into changing their ways for now...
1399bool FGFDM::attrb(const XMLAttributes* atts, const char* attr)
1400{
1401 const char* val = atts->getValue(attr);
1402 if(val == 0) return false;
1403
1404 if(!strcmp(val,"true")) {
1405 SG_LOG(SG_FLIGHT, SG_ALERT, "Warning: " <<
1406 "deprecated 'true' boolean in YASim configuration file. " <<
1407 "Use numeric booleans (attribute=\"1\") instead");
1408 return true;
1409 }
1410 return attri(atts, attr, 0) ? true : false;
1411}
1412
1413}; // namespace yasim
#define p2(x, y)
#define p(x)
#define min(X, Y)
#define i(x)
const char * name
static mt seed
Definition fgclouds.cxx:49
const double g(9.80665)
static const float YASIM_PI
Definition Gear.cpp:18
static const float DEG2RAD
Definition Gear.cpp:19
static void moveprop(SGPropertyNode *node, const std::string &prop, float target, float frac)
Definition FGFDM.cpp:426
static const float CIN2CM
static const float HP2W
static double atof(const string &str)
Definition options.cxx:107
bool fgSetDouble(const char *name, double defaultValue)
Set a double value for a property.
Definition proptest.cpp:31
bool fgSetBool(char const *name, bool val)
Set a bool value for a property.
Definition proptest.cpp:24
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.
Definition proptest.cpp:27
float fgGetFloat(const char *name, float defaultValue)
Get a float value for a property.
Definition proptest.cpp:29
bool fgSetFloat(const char *name, float val)
Set a float value for a property.
Definition proptest.cpp:23