1 #include <simgear/debug/logstream.hxx>
2 #include <simgear/misc/sg_path.hxx>
3 #include <simgear/scene/model/location.hxx>
4 #include <simgear/xml/easyxml.hxx>
6 #include <Main/globals.hxx>
7 #include <Main/fg_props.hxx>
8 #include <Model/acmodel.hxx>
9 #include <Model/model.hxx>
12 #include "Atmosphere.hpp"
14 #include "Airplane.hpp"
16 #include "Integrator.hpp"
19 #include "PropEngine.hpp"
20 #include "PistonEngine.hpp"
24 using namespace yasim;
26 static const float RAD2DEG = 180/3.14159265358979323846;
27 static const float RAD2RPM = 9.54929658551;
28 static const float M2FT = 3.2808399;
29 static const float FT2M = 0.3048;
30 static const float MPS2KTS = 3600.0/1852.0;
31 static const float CM2GALS = 264.172037284; // gallons/cubic meter
32 static const float KG2LBS = 2.20462262185;
33 static const float W2HP = 1.3416e-3;
34 static const float INHG2PA = 3386.389;
35 static const float SLUG2KG = 14.59390;
37 void YASim::printDEBUG()
39 static int debugCount = 0;
45 // printf("N1 %5.1f N2 %5.1f FF %7.1f EPR %4.2f EGT %6.1f\n",
46 // fgGetFloat("/engines/engine[0]/n1"),
47 // fgGetFloat("/engines/engine[0]/n2"),
48 // fgGetFloat("/engines/engine[0]/fuel-flow-gph"),
49 // fgGetFloat("/engines/engine[0]/epr"),
50 // fgGetFloat("/engines/engine[0]/egt"));
52 // printf("gear: %f\n", fgGetFloat("/controls/gear/gear-down"));
54 // printf("alpha %5.1f beta %5.1f\n", get_Alpha()*57.3, get_Beta()*57.3);
56 // printf("pilot: %f %f %f\n",
57 // fgGetDouble("/sim/view/pilot/x-offset-m"),
58 // fgGetDouble("/sim/view/pilot/y-offset-m"),
59 // fgGetDouble("/sim/view/pilot/z-offset-m"));
63 YASim::YASim(double dt)
70 _fdm->getAirplane()->getModel()->getIntegrator()->setInterval(_dt);
75 Airplane* a = _fdm->getAirplane();
77 float aoa = a->getCruiseAoA() * RAD2DEG;
78 float tail = -1 * a->getTailIncidence() * RAD2DEG;
79 float drag = 1000 * a->getDragCoefficient();
81 SG_LOG(SG_FLIGHT,SG_INFO,"YASim solution results:");
82 SG_LOG(SG_FLIGHT,SG_INFO," Iterations: "<<a->getSolutionIterations());
83 SG_LOG(SG_FLIGHT,SG_INFO," Drag Coefficient: "<< drag);
84 SG_LOG(SG_FLIGHT,SG_INFO," Lift Ratio: "<<a->getLiftRatio());
85 SG_LOG(SG_FLIGHT,SG_INFO," Cruise AoA: "<< aoa);
86 SG_LOG(SG_FLIGHT,SG_INFO," Tail Incidence: "<< tail);
87 SG_LOG(SG_FLIGHT,SG_INFO,"Approach Elevator: "<<a->getApproachElevator());
92 a->getModel()->getBody()->getCG(cg);
93 sprintf(buf, " CG: %.3f, %.3f, %.3f", cg[0], cg[1], cg[2]);
94 SG_LOG(SG_FLIGHT, SG_INFO, buf);
96 if(a->getFailureMsg()) {
97 SG_LOG(SG_FLIGHT, SG_ALERT, "YASim SOLUTION FAILURE:");
98 SG_LOG(SG_FLIGHT, SG_ALERT, a->getFailureMsg());
105 // Run the superclass bind to set up a bunch of property ties
108 // Now UNtie the ones that we are going to set ourselves.
109 fgUntie("/consumables/fuel/tank[0]/level-gal_us");
110 fgUntie("/consumables/fuel/tank[1]/level-gal_us");
113 for(int i=0; i<_fdm->getAirplane()->getModel()->numThrusters(); i++) {
114 sprintf(buf, "/engines/engine[%d]/fuel-flow-gph", i); fgUntie(buf);
115 sprintf(buf, "/engines/engine[%d]/rpm", i); fgUntie(buf);
116 sprintf(buf, "/engines/engine[%d]/mp-osi", i); fgUntie(buf);
117 sprintf(buf, "/engines/engine[%d]/egt-degf", i); fgUntie(buf);
124 Airplane* a = _fdm->getAirplane();
125 Model* m = a->getModel();
130 m->setCrashed(false);
132 // Figure out the initial speed type
133 string speed_set = fgGetString("/sim/presets/speed-set", "UVW");
134 if (speed_set == "NED")
136 else if (speed_set == "UVW")
138 else if (speed_set == "knots")
140 else if (speed_set == "mach")
144 SG_LOG(SG_FLIGHT, SG_ALERT, "Unknown speed type " << speed_set);
147 // Build a filename and parse it
148 SGPath f(globals->get_fg_root());
149 f.append("Aircraft-yasim");
150 f.append(fgGetString("/sim/aero"));
152 readXML(f.str(), *_fdm);
154 // Compile it into a real airplane, and tell the user what they got
160 // Create some FG{Eng|Gear}Interface objects
162 for(i=0; i<a->numGear(); i++) {
163 Gear* g = a->getGear(i);
164 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
167 node->setDoubleValue("xoffset-in", pos[0]);
168 node->setDoubleValue("yoffset-in", pos[1]);
169 node->setDoubleValue("zoffset-in", pos[2]);
171 // for(i=0; i<m->numThrusters(); i++) {
172 // // Sanify the initial input conditions
174 // sprintf(buf, "/controls/engines/engine[%d]/throttle", i); fgSetFloat(buf, 0);
175 // sprintf(buf, "/controls/engines/engine[%d]/mixture", i); fgSetFloat(buf, 1);
176 // sprintf(buf, "/controls/engines/engine[%d]/propeller-pitch", i); fgSetFloat(buf, 1);
177 // sprintf(buf, "/controls/engines/engine[%d]/augmentation", i); fgSetFloat(buf, 0);
180 // fgSetFloat("/controls/flight/slats", 0);
181 // fgSetFloat("/controls/flight/spoilers", 0);
183 // Are we at ground level? If so, lift the plane up so the gear
185 double runway_altitude = get_Runway_altitude();
186 if(get_Altitude() - runway_altitude < 50) {
187 fgSetBool("/controls/gear/gear-down", false);
188 float minGearZ = 1e18;
189 for(i=0; i<a->numGear(); i++) {
190 Gear* g = a->getGear(i);
193 if(pos[2] < minGearZ)
196 _set_Altitude(runway_altitude - minGearZ*M2FT);
197 fgSetBool("/controls/gear/gear-down", true);
200 // The pilot's eyepoint
202 a->getPilotPos(pilot);
203 // fgSetFloat("/sim/view/pilot/x-offset-m", -pilot[0]);
204 // fgSetFloat("/sim/view/pilot/y-offset-m", -pilot[1]);
205 // fgSetFloat("/sim/view/pilot/z-offset-m", pilot[2]);
207 // Blank the state, and copy in ours
212 _fdm->getExternalInput();
213 _fdm->getAirplane()->initEngines();
218 void YASim::update(double dt)
223 int iterations = _calc_multiloop(dt);
225 // If we're crashed, then we don't care
226 if(_fdm->getAirplane()->getModel()->isCrashed())
230 for(i=0; i<iterations; i++) {
239 void YASim::copyToYASim(bool copyState)
242 float lat = get_Latitude();
243 float lon = get_Longitude();
244 float alt = get_Altitude() * FT2M;
245 float roll = get_Phi();
246 float pitch = get_Theta();
247 float hdg = get_Psi();
251 wind[0] = get_V_north_airmass() * FT2M * -1.0;
252 wind[1] = get_V_east_airmass() * FT2M * -1.0;
253 wind[2] = get_V_down_airmass() * FT2M * -1.0;
255 // Get ground elevation from the FGinterface's FGlocation data
256 double ground = getACModel()->get3DModel()->getFGLocation()->get_cur_elev_m();
257 // cout << "YASIM: ground = " << ground << endl;
259 float pressure = fgGetFloat("/environment/pressure-inhg") * INHG2PA;
260 float temp = fgGetFloat("/environment/temperature-degc") + 273.15;
261 float dens = fgGetFloat("/environment/density-slugft3")
262 * SLUG2KG * M2FT*M2FT*M2FT;
265 Model* model = _fdm->getAirplane()->getModel();
268 Glue::xyz2nedMat(lat, lon, xyz2ned);
271 Glue::geod2xyz(lat, lon, alt, s.pos);
274 Glue::euler2orient(roll, pitch, hdg, s.orient);
275 Math::mmul33(s.orient, xyz2ned, s.orient);
278 string speed_set = fgGetString("/sim/presets/speed-set", "UVW");
280 bool needCopy = false;
281 switch (_speed_set) {
283 v[0] = get_V_north() * FT2M * -1.0;
284 v[1] = get_V_east() * FT2M * -1.0;
285 v[2] = get_V_down() * FT2M * -1.0;
288 v[0] = get_uBody() * FT2M;
289 v[1] = get_vBody() * FT2M;
290 v[2] = get_wBody() * FT2M;
291 Math::tmul33(s.orient, v, v);
294 v[0] = Atmosphere::spdFromVCAS(get_V_calibrated_kts()/MPS2KTS,
298 Math::tmul33(s.orient, v, v);
302 v[0] = Atmosphere::spdFromMach(get_Mach_number(), temp);
305 Math::tmul33(s.orient, v, v);
315 _speed_set = UVW; // change to this after initial setting
318 if(copyState || needCopy)
322 Math::tmul33(xyz2ned, wind, wind);
323 model->setWind(wind);
325 // ground. Calculate a cartesian coordinate for the ground under
326 // us, find the (geodetic) up vector normal to the ground, then
327 // use that to find the final (radius) term of the plane equation.
328 double xyz[3], gplane[3]; float up[3];
329 Glue::geod2xyz(lat, lon, ground, xyz);
330 Glue::geodUp(xyz, up); // FIXME, needless reverse computation...
332 for(i=0; i<3; i++) gplane[i] = up[i];
333 double rad = gplane[0]*xyz[0] + gplane[1]*xyz[1] + gplane[2]*xyz[2];
334 model->setGroundPlane(gplane, rad);
337 model->setAir(pressure, temp, dens);
340 // All the settables:
342 // These are set below:
345 // _set_Accels_CG_Body
346 // _set_Accels_Pilot_Body
347 // _set_Accels_CG_Body_N
348 // _set_Velocities_Local
349 // _set_Velocities_Ground
350 // _set_Velocities_Wind_Body
355 // _set_V_ground_speed
357 // _set_V_calibrated_kts
365 // _set_Geodetic_Position
366 // _set_Runway_altitude
368 // Ignoring these, because they're unused:
369 // _set_Geocentric_Position
370 // _set_Geocentric_Rates
373 // _set_Earth_position_angle (WTF?)
374 // _set_Gamma_vert_rad
376 // _set_T_Local_to_Body
378 // _set_Sea_Level_Radius
380 // Externally set via the weather code:
381 // _set_Velocities_Local_Airmass
383 // _set_Static_pressure
384 // _set_Static_temperature
385 void YASim::copyFromYASim()
387 Airplane* airplane = _fdm->getAirplane();
388 Model* model = airplane->getModel();
389 State* s = model->getState();
392 double lat, lon, alt;
393 Glue::xyz2geod(s->pos, &lat, &lon, &alt);
394 _set_Geodetic_Position(lat, lon, alt*M2FT);
397 //_set_Geocentric_Position(Glue::geod2geocLat(lat), lon, alt*M2FT);
399 _set_Altitude_AGL(model->getAGL() * M2FT);
401 // useful conversion matrix
403 Glue::xyz2nedMat(lat, lon, xyz2ned);
407 Math::vmul33(xyz2ned, s->v, v);
408 _set_Velocities_Local(M2FT*v[0], M2FT*v[1], M2FT*v[2]);
409 _set_V_ground_speed(Math::sqrt(M2FT*v[0]*M2FT*v[0] +
410 M2FT*v[1]*M2FT*v[1]));
411 _set_Climb_Rate(-M2FT*v[2]);
413 // The HUD uses this, but inverts down (?!)
414 _set_Velocities_Ground(M2FT*v[0], M2FT*v[1], -M2FT*v[2]);
416 // _set_Geocentric_Rates(M2FT*v[0], M2FT*v[1], M2FT*v[2]); // UNUSED
420 wind[0] = get_V_north_airmass() * FT2M * -1.0; // Wind in NED
421 wind[1] = get_V_east_airmass() * FT2M * -1.0;
422 wind[2] = get_V_down_airmass() * FT2M * -1.0;
423 Math::tmul33(xyz2ned, wind, wind); // Wind in global
424 Math::sub3(s->v, wind, v); // V - wind in global
425 Math::vmul33(s->orient, v, v); // to body coordinates
426 _set_Velocities_Wind_Body(v[0]*M2FT, -v[1]*M2FT, -v[2]*M2FT);
427 _set_V_rel_wind(Math::mag3(v)*M2FT); // units?
429 float P = fgGetDouble("/environment/pressure-inhg") * INHG2PA;
430 float T = fgGetDouble("/environment/temperature-degC") + 273.15;
431 float D = fgGetFloat("/environment/density-slugft3")
432 *SLUG2KG * M2FT*M2FT*M2FT;
433 _set_V_equiv_kts(Atmosphere::calcVEAS(v[0], P, T, D)*MPS2KTS);
434 _set_V_calibrated_kts(Atmosphere::calcVCAS(v[0], P, T)*MPS2KTS);
435 _set_Mach_number(Atmosphere::calcMach(v[0], T));
438 Math::vmul33(xyz2ned, s->acc, v);
439 _set_Accels_Local(M2FT*v[0], M2FT*v[1], M2FT*v[2]);
441 Math::vmul33(s->orient, s->acc, v);
442 _set_Accels_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]);
443 _set_Accels_CG_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]);
445 _fdm->getAirplane()->getPilotAccel(v);
446 _set_Accels_Pilot_Body(-M2FT*v[0], M2FT*v[1], M2FT*v[2]);
448 // There is no property for pilot G's, but I need it for a panel
449 // instrument. Hack this in here, and REMOVE IT WHEN IT FINDS A
451 fgSetFloat("/accelerations/pilot-g", -v[2]/9.8);
453 // The one appears (!) to want inverted pilot acceleration
454 // numbers, in G's...
455 Math::mul3(1.0/9.8, v, v);
456 _set_Accels_CG_Body_N(v[0], -v[1], -v[2]);
460 Glue::calcAlphaBeta(s, &alpha, &beta);
465 Math::trans33(xyz2ned, tmp);
466 Math::mmul33(s->orient, tmp, tmp);
467 float roll, pitch, hdg;
468 Glue::orient2euler(tmp, &roll, &pitch, &hdg);
469 _set_Euler_Angles(roll, pitch, hdg);
472 Math::vmul33(s->orient, s->rot, v);
473 _set_Omega_Body(v[0], -v[1], -v[2]);
475 Glue::calcEulerRates(s, &roll, &pitch, &hdg);
476 _set_Euler_Rates(roll, pitch, hdg);
478 // Fill out our engine and gear objects
480 for(i=0; i<airplane->numGear(); i++) {
481 Gear* g = airplane->getGear(i);
482 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
483 node->setBoolValue("has-brake", g->getBrake() != 0);
484 node->setBoolValue("wow", g->getCompressFraction() != 0);
485 node->setFloatValue("compression-norm", g->getCompressFraction());
488 for(i=0; i<model->numThrusters(); i++) {
489 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
490 Thruster* t = model->getThruster(i);
492 node->setBoolValue("running", t->isRunning());
493 node->setBoolValue("cranking", t->isCranking());
497 node->setDoubleValue("prop-thrust", Math::mag3(tmp) * KG2LBS / 9.8);
499 PropEngine* pe = t->getPropEngine();
501 node->setDoubleValue("rpm", pe->getOmega() * RAD2RPM);
504 float power = Math::mag3(tmp) * pe->getOmega();
505 float maxPower = pe->getPistonEngine()->getMaxPower();
507 node->setDoubleValue("max-hp", maxPower * W2HP);
508 node->setDoubleValue("power-pct", 100 * power/maxPower);