1 #include <simgear/misc/sg_path.hxx>
2 #include <simgear/debug/logstream.hxx>
3 #include <simgear/xml/easyxml.hxx>
4 #include <Main/globals.hxx>
5 #include <Main/fg_props.hxx>
6 #include <Scenery/scenery.hxx>
9 #include "Atmosphere.hpp"
11 #include "Airplane.hpp"
13 #include "Integrator.hpp"
16 #include "PropEngine.hpp"
17 #include "PistonEngine.hpp"
21 using namespace yasim;
23 static const float RAD2DEG = 180/3.14159265358979323846;
24 static const float RAD2RPM = 9.54929658551;
25 static const float M2FT = 3.2808399;
26 static const float FT2M = 0.3048;
27 static const float MPS2KTS = 3600.0/1852.0;
28 static const float CM2GALS = 264.172037284; // gallons/cubic meter
29 static const float KG2LBS = 2.20462262185;
30 static const float W2HP = 1.3416e-3;
32 void YASim::printDEBUG()
34 static int debugCount = 0;
40 // printf("N1 %5.1f N2 %5.1f FF %7.1f EPR %4.2f EGT %6.1f\n",
41 // fgGetFloat("/engines/engine[0]/n1"),
42 // fgGetFloat("/engines/engine[0]/n2"),
43 // fgGetFloat("/engines/engine[0]/fuel-flow-gph"),
44 // fgGetFloat("/engines/engine[0]/epr"),
45 // fgGetFloat("/engines/engine[0]/egt"));
47 // printf("gear: %f\n", fgGetFloat("/controls/gear-down"));
49 // printf("alpha %5.1f beta %5.1f\n", get_Alpha()*57.3, get_Beta()*57.3);
51 // printf("pilot: %f %f %f\n",
52 // fgGetDouble("/sim/view/pilot/x-offset-m"),
53 // fgGetDouble("/sim/view/pilot/y-offset-m"),
54 // fgGetDouble("/sim/view/pilot/z-offset-m"));
58 YASim::YASim(double dt)
65 _fdm->getAirplane()->getModel()->getIntegrator()->setInterval(_dt);
70 Airplane* a = _fdm->getAirplane();
72 float aoa = a->getCruiseAoA() * RAD2DEG;
73 float tail = -1 * a->getTailIncidence() * RAD2DEG;
74 float drag = 1000 * a->getDragCoefficient();
76 SG_LOG(SG_FLIGHT,SG_INFO,"YASim solution results:");
77 SG_LOG(SG_FLIGHT,SG_INFO," Iterations: "<<a->getSolutionIterations());
78 SG_LOG(SG_FLIGHT,SG_INFO,"Drag Coefficient: "<< drag);
79 SG_LOG(SG_FLIGHT,SG_INFO," Lift Ratio: "<<a->getLiftRatio());
80 SG_LOG(SG_FLIGHT,SG_INFO," Cruise AoA: "<< aoa);
81 SG_LOG(SG_FLIGHT,SG_INFO," Tail Incidence: "<< tail);
85 a->getModel()->getBody()->getCG(cg);
86 sprintf(buf, " CG: %.1f, %.1f, %.1f", cg[0], cg[1], cg[2]);
87 SG_LOG(SG_FLIGHT, SG_INFO, buf);
89 if(a->getFailureMsg()) {
90 SG_LOG(SG_FLIGHT, SG_ALERT, "YASim SOLUTION FAILURE:");
91 SG_LOG(SG_FLIGHT, SG_ALERT, a->getFailureMsg());
98 // Run the superclass bind to set up a bunch of property ties
101 // Now UNtie the ones that we are going to set ourselves.
102 fgUntie("/consumables/fuel/tank[0]/level-gal_us");
103 fgUntie("/consumables/fuel/tank[1]/level-gal_us");
106 for(int i=0; i<_fdm->getAirplane()->getModel()->numThrusters(); i++) {
107 sprintf(buf, "/engines/engine[%d]/fuel-flow-gph", i); fgUntie(buf);
108 sprintf(buf, "/engines/engine[%d]/rpm", i); fgUntie(buf);
109 sprintf(buf, "/engines/engine[%d]/mp-osi", i); fgUntie(buf);
110 sprintf(buf, "/engines/engine[%d]/egt-degf", i); fgUntie(buf);
117 Airplane* a = _fdm->getAirplane();
118 Model* m = a->getModel();
123 m->setCrashed(false);
125 // Build a filename and parse it
126 SGPath f(globals->get_fg_root());
127 f.append("Aircraft-yasim");
128 f.append(fgGetString("/sim/aero"));
130 readXML(f.str(), *_fdm);
132 // Compile it into a real airplane, and tell the user what they got
138 // Create some FG{Eng|Gear}Interface objects
140 for(i=0; i<a->numGear(); i++) {
141 Gear* g = a->getGear(i);
142 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
145 node->setDoubleValue("xoffset-in", pos[0]);
146 node->setDoubleValue("yoffset-in", pos[1]);
147 node->setDoubleValue("zoffset-in", pos[2]);
149 for(i=0; i<m->numThrusters(); i++) {
150 // Sanify the initial input conditions
152 sprintf(buf, "/controls/throttle[%d]", i); fgSetFloat(buf, 0);
153 sprintf(buf, "/controls/mixture[%d]", i); fgSetFloat(buf, 1);
154 sprintf(buf, "/controls/propeller-pitch[%d]", i); fgSetFloat(buf, 1);
155 sprintf(buf, "/controls/afterburner[%d]", i); fgSetFloat(buf, 0);
158 fgSetFloat("/controls/slats", 0);
159 fgSetFloat("/controls/spoilers", 0);
161 // Are we at ground level? If so, lift the plane up so the gear
163 double runway_altitude =
164 fgGetDouble("/environment/ground-elevation-m") * SG_METER_TO_FEET;
165 fgSetBool("/controls/gear-down", false);
166 if(get_Altitude() - runway_altitude < 50) {
167 float minGearZ = 1e18;
168 for(i=0; i<a->numGear(); i++) {
169 Gear* g = a->getGear(i);
172 if(pos[2] < minGearZ)
175 _set_Altitude(runway_altitude - minGearZ*M2FT);
176 fgSetBool("/controls/gear-down", true);
179 // The pilot's eyepoint
181 a->getPilotPos(pilot);
182 fgSetFloat("/sim/view/pilot/x-offset-m", -pilot[0]);
183 fgSetFloat("/sim/view/pilot/y-offset-m", -pilot[1]);
184 fgSetFloat("/sim/view/pilot/z-offset-m", pilot[2]);
186 // Blank the state, and copy in ours
191 _fdm->getExternalInput();
192 _fdm->getAirplane()->initEngines();
197 void YASim::update(int iterations)
199 // If we're crashed, then we don't care
200 if(_fdm->getAirplane()->getModel()->isCrashed())
204 for(i=0; i<iterations; i++) {
213 void YASim::copyToYASim(bool copyState)
216 float lat = get_Latitude();
217 float lon = get_Longitude();
218 float alt = get_Altitude() * FT2M;
219 float roll = get_Phi();
220 float pitch = get_Theta();
221 float hdg = get_Psi();
225 wind[0] = get_V_north_airmass() * FT2M * -1.0;
226 wind[1] = get_V_east_airmass() * FT2M * -1.0;
227 wind[2] = get_V_down_airmass() * FT2M * -1.0;
229 // The ground elevation doesn't come from FGInterface; query it
230 // from the scenery and set it for others to find.
231 double ground = scenery.get_cur_elev();
232 _set_Runway_altitude(ground * FT2M);
233 // cout << "YASIM: ground = " << ground << endl;
235 // You'd this this would work, but it doesn't. These values are
236 // always zero. Use a "standard" pressure intstead.
238 // float pressure = get_Static_pressure();
239 // float temp = get_Static_temperature();
240 float pressure = Atmosphere::getStdPressure(alt);
241 float temp = Atmosphere::getStdTemperature(alt);
244 Model* model = _fdm->getAirplane()->getModel();
247 Glue::xyz2nedMat(lat, lon, xyz2ned);
250 Glue::geod2xyz(lat, lon, alt, s.pos);
253 Glue::euler2orient(roll, pitch, hdg, s.orient);
254 Math::mmul33(s.orient, xyz2ned, s.orient);
256 // Copy in the existing velocity for now...
257 Math::set3(model->getState()->v, s.v);
263 Math::tmul33(xyz2ned, wind, wind);
264 model->setWind(wind);
266 // ground. Calculate a cartesian coordinate for the ground under
267 // us, find the (geodetic) up vector normal to the ground, then
268 // use that to find the final (radius) term of the plane equation.
269 double xyz[3], gplane[3]; float up[3];
270 Glue::geod2xyz(lat, lon, ground, xyz);
271 Glue::geodUp(xyz, up); // FIXME, needless reverse computation...
273 for(i=0; i<3; i++) gplane[i] = up[i];
274 double rad = gplane[0]*xyz[0] + gplane[1]*xyz[1] + gplane[2]*xyz[2];
275 model->setGroundPlane(gplane, rad);
278 model->setAir(pressure, temp);
281 // All the settables:
283 // These are set below:
286 // _set_Accels_CG_Body
287 // _set_Accels_Pilot_Body
288 // _set_Accels_CG_Body_N
289 // _set_Velocities_Local
290 // _set_Velocities_Ground
291 // _set_Velocities_Wind_Body
296 // _set_V_ground_speed
298 // _set_V_calibrated_kts
306 // _set_Geodetic_Position
307 // _set_Runway_altitude
309 // Ignoring these, because they're unused:
310 // _set_Geocentric_Position
311 // _set_Geocentric_Rates
314 // _set_Earth_position_angle (WTF?)
315 // _set_Gamma_vert_rad
317 // _set_T_Local_to_Body
319 // _set_Sea_Level_Radius
321 // Externally set via the weather code:
322 // _set_Velocities_Local_Airmass
324 // _set_Static_pressure
325 // _set_Static_temperature
326 void YASim::copyFromYASim()
328 Airplane* airplane = _fdm->getAirplane();
329 Model* model = airplane->getModel();
330 State* s = model->getState();
333 double lat, lon, alt;
334 Glue::xyz2geod(s->pos, &lat, &lon, &alt);
335 _set_Geodetic_Position(lat, lon, alt*M2FT);
338 //_set_Geocentric_Position(Glue::geod2geocLat(lat), lon, alt*M2FT);
340 _set_Altitude_AGL(model->getAGL() * M2FT);
342 // useful conversion matrix
344 Glue::xyz2nedMat(lat, lon, xyz2ned);
348 Math::vmul33(xyz2ned, s->v, v);
349 _set_Velocities_Local(M2FT*v[0], M2FT*v[1], M2FT*v[2]);
350 _set_V_ground_speed(Math::sqrt(M2FT*v[0]*M2FT*v[0] +
351 M2FT*v[1]*M2FT*v[1]));
352 _set_Climb_Rate(-M2FT*v[2]);
354 // The HUD uses this, but inverts down (?!)
355 _set_Velocities_Ground(M2FT*v[0], M2FT*v[1], -M2FT*v[2]);
357 // _set_Geocentric_Rates(M2FT*v[0], M2FT*v[1], M2FT*v[2]); // UNUSED
361 wind[0] = get_V_north_airmass() * FT2M * -1.0; // Wind in NED
362 wind[1] = get_V_east_airmass() * FT2M * -1.0;
363 wind[2] = get_V_down_airmass() * FT2M * -1.0;
364 Math::tmul33(xyz2ned, wind, wind); // Wind in global
365 Math::sub3(s->v, wind, v); // V - wind in global
366 Math::vmul33(s->orient, s->v, v); // to body coordinates
367 _set_Velocities_Wind_Body(v[0]*M2FT, -v[1]*M2FT, -v[2]*M2FT);
368 _set_V_rel_wind(Math::mag3(v)*M2FT); // units?
371 // These don't work, use a dummy based on altitude
372 // float P = get_Static_pressure();
373 // float T = get_Static_temperature();
374 float P = Atmosphere::getStdPressure(alt);
375 float T = Atmosphere::getStdTemperature(alt);
376 _set_V_equiv_kts(Atmosphere::calcVEAS(v[0], P, T)*MPS2KTS);
377 _set_V_calibrated_kts(Atmosphere::calcVCAS(v[0], P, T)*MPS2KTS);
378 _set_Mach_number(Atmosphere::calcMach(v[0], T));
381 Math::vmul33(xyz2ned, s->acc, v);
382 _set_Accels_Local(M2FT*v[0], M2FT*v[1], M2FT*v[2]);
384 Math::vmul33(s->orient, s->acc, v);
385 _set_Accels_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]);
386 _set_Accels_CG_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]);
388 _fdm->getAirplane()->getPilotAccel(v);
389 _set_Accels_Pilot_Body(M2FT*v[0], M2FT*v[1], -M2FT*v[2]);
391 // The one appears (!) to want inverted pilot acceleration
392 // numbers, in G's...
393 Math::mul3(1.0/9.8, v, v);
394 _set_Accels_CG_Body_N(v[0], -v[1], -v[2]);
398 Glue::calcAlphaBeta(s, &alpha, &beta);
403 Math::trans33(xyz2ned, tmp);
404 Math::mmul33(s->orient, tmp, tmp);
405 float roll, pitch, hdg;
406 Glue::orient2euler(tmp, &roll, &pitch, &hdg);
407 _set_Euler_Angles(roll, pitch, hdg);
410 Math::vmul33(s->orient, s->rot, v);
411 _set_Omega_Body(v[0], -v[1], -v[2]);
413 Glue::calcEulerRates(s, &roll, &pitch, &hdg);
414 _set_Euler_Rates(roll, pitch, hdg);
416 // Fill out our engine and gear objects
418 for(i=0; i<airplane->numGear(); i++) {
419 Gear* g = airplane->getGear(i);
420 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
421 node->setBoolValue("has-brake", g->getBrake() != 0);
422 node->setBoolValue("wow", g->getCompressFraction() != 0);
423 node->setFloatValue("compression-norm", g->getCompressFraction());
426 for(i=0; i<model->numThrusters(); i++) {
427 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
428 Thruster* t = model->getThruster(i);
430 node->setBoolValue("running", t->isRunning());
431 node->setBoolValue("cranking", t->isCranking());
433 // Note: assumes all tanks have the same fuel density!
434 node->setDoubleValue("fuel-flow-gph", CM2GALS * t->getFuelFlow()
435 / airplane->getFuelDensity(0));
439 node->setDoubleValue("prop-thrust", Math::mag3(tmp) * KG2LBS / 9.8);
441 PropEngine* pe = t->getPropEngine();
443 node->setDoubleValue("rpm", pe->getOmega() * RAD2RPM);
446 float power = Math::mag3(tmp) * pe->getOmega();
447 float maxPower = pe->getPistonEngine()->getMaxPower();
449 node->setDoubleValue("max-hp", maxPower * W2HP);
450 node->setDoubleValue("power-pct", 100 * power/maxPower);