2 #include <simgear/debug/logstream.hxx>
3 #include <simgear/math/sg_geodesy.hxx>
4 #include <simgear/misc/sg_path.hxx>
5 #include <simgear/scene/model/location.hxx>
6 #include <simgear/scene/model/placement.hxx>
7 #include <simgear/xml/easyxml.hxx>
9 #include <Main/globals.hxx>
10 #include <Main/fg_props.hxx>
11 #include <Model/acmodel.hxx>
14 #include "Atmosphere.hpp"
16 #include "Airplane.hpp"
18 #include "Integrator.hpp"
21 #include "PropEngine.hpp"
22 #include "PistonEngine.hpp"
26 using namespace yasim;
28 static const float YASIM_PI = 3.14159265358979323846;
29 static const float RAD2DEG = 180/YASIM_PI;
30 static const float PI2 = YASIM_PI*2;
31 static const float RAD2RPM = 9.54929658551;
32 static const float M2FT = 3.2808399;
33 static const float FT2M = 0.3048;
34 static const float MPS2KTS = 3600.0/1852.0;
35 static const float CM2GALS = 264.172037284; // gallons/cubic meter
36 static const float KG2LBS = 2.20462262185;
37 static const float W2HP = 1.3416e-3;
38 static const float INHG2PA = 3386.389;
39 static const float SLUG2KG = 14.59390;
41 YASim::YASim(double dt)
48 _fdm->getAirplane()->getModel()->getIntegrator()->setInterval(_dt);
53 Airplane* a = _fdm->getAirplane();
55 float aoa = a->getCruiseAoA() * RAD2DEG;
56 float tail = -1 * a->getTailIncidence() * RAD2DEG;
57 float drag = 1000 * a->getDragCoefficient();
59 SG_LOG(SG_FLIGHT,SG_INFO,"YASim solution results:");
60 SG_LOG(SG_FLIGHT,SG_INFO," Iterations: "<<a->getSolutionIterations());
61 SG_LOG(SG_FLIGHT,SG_INFO," Drag Coefficient: "<< drag);
62 SG_LOG(SG_FLIGHT,SG_INFO," Lift Ratio: "<<a->getLiftRatio());
63 SG_LOG(SG_FLIGHT,SG_INFO," Cruise AoA: "<< aoa);
64 SG_LOG(SG_FLIGHT,SG_INFO," Tail Incidence: "<< tail);
65 SG_LOG(SG_FLIGHT,SG_INFO,"Approach Elevator: "<<a->getApproachElevator());
70 a->getModel()->getBody()->getCG(cg);
71 sprintf(buf, " CG: %.3f, %.3f, %.3f", cg[0], cg[1], cg[2]);
72 SG_LOG(SG_FLIGHT, SG_INFO, buf);
74 if(a->getFailureMsg()) {
75 SG_LOG(SG_FLIGHT, SG_ALERT, "YASim SOLUTION FAILURE:");
76 SG_LOG(SG_FLIGHT, SG_ALERT, a->getFailureMsg());
83 // Run the superclass bind to set up a bunch of property ties
86 // Now UNtie the ones that we are going to set ourselves.
87 fgUntie("/consumables/fuel/tank[0]/level-gal_us");
88 fgUntie("/consumables/fuel/tank[1]/level-gal_us");
91 for(int i=0; i<_fdm->getAirplane()->getModel()->numThrusters(); i++) {
92 sprintf(buf, "/engines/engine[%d]/fuel-flow-gph", i); fgUntie(buf);
93 sprintf(buf, "/engines/engine[%d]/rpm", i); fgUntie(buf);
94 sprintf(buf, "/engines/engine[%d]/mp-osi", i); fgUntie(buf);
95 sprintf(buf, "/engines/engine[%d]/egt-degf", i); fgUntie(buf);
101 Airplane* a = _fdm->getAirplane();
102 Model* m = a->getModel();
107 m->setCrashed(false);
109 // Figure out the initial speed type
110 string speed_set = fgGetString("/sim/presets/speed-set", "UVW");
111 if (speed_set == "NED")
113 else if (speed_set == "UVW")
115 else if (speed_set == "knots")
117 else if (speed_set == "mach")
121 SG_LOG(SG_FLIGHT, SG_ALERT, "Unknown speed type " << speed_set);
124 // Build a filename and parse it
125 SGPath f(fgGetString("/sim/aircraft-dir"));
126 f.append(fgGetString("/sim/aero"));
128 readXML(f.str(), *_fdm);
130 // Compile it into a real airplane, and tell the user what they got
136 // Create some FG{Eng|Gear}Interface objects
138 for(i=0; i<a->numGear(); i++) {
139 Gear* g = a->getGear(i);
140 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
143 node->setDoubleValue("xoffset-in", pos[0]);
144 node->setDoubleValue("yoffset-in", pos[1]);
145 node->setDoubleValue("zoffset-in", pos[2]);
148 // Are we at ground level? If so, lift the plane up so the gear
150 double runway_altitude = get_Runway_altitude();
151 if(get_Altitude() - runway_altitude < 50) {
152 fgSetBool("/controls/gear/gear-down", false);
153 float minGearZ = 1e18;
154 for(i=0; i<a->numGear(); i++) {
155 Gear* g = a->getGear(i);
158 if(pos[2] < minGearZ)
161 _set_Altitude(runway_altitude - minGearZ*M2FT);
162 fgSetBool("/controls/gear/gear-down", true);
165 // Blank the state, and copy in ours
170 _fdm->getExternalInput();
171 _fdm->getAirplane()->initEngines();
176 void YASim::update(double dt)
181 int iterations = _calc_multiloop(dt);
183 // If we're crashed, then we don't care
184 if(_fdm->getAirplane()->getModel()->isCrashed()) {
185 if(!fgGetBool("/sim/crashed"))
186 fgSetBool("/sim/crashed", true);
191 for(i=0; i<iterations; i++) {
198 void YASim::copyToYASim(bool copyState)
201 float lat = get_Latitude();
202 float lon = get_Longitude();
203 float alt = get_Altitude() * FT2M;
204 float roll = get_Phi();
205 float pitch = get_Theta();
206 float hdg = get_Psi();
210 wind[0] = get_V_north_airmass() * FT2M * -1.0;
211 wind[1] = get_V_east_airmass() * FT2M * -1.0;
212 wind[2] = get_V_down_airmass() * FT2M * -1.0;
214 // Get ground elevation from the FGinterface's FGlocation data
215 double ground = getACModel()->get3DModel()->getSGLocation()->get_cur_elev_m();
216 // cout << "YASIM: ground = " << ground << endl;
218 float pressure = fgGetFloat("/environment/pressure-inhg") * INHG2PA;
219 float temp = fgGetFloat("/environment/temperature-degc") + 273.15;
220 float dens = fgGetFloat("/environment/density-slugft3")
221 * SLUG2KG * M2FT*M2FT*M2FT;
224 Model* model = _fdm->getAirplane()->getModel();
227 Glue::xyz2nedMat(lat, lon, xyz2ned);
230 sgGeodToCart(lat, lon, alt, s.pos);
233 Glue::euler2orient(roll, pitch, hdg, s.orient);
234 Math::mmul33(s.orient, xyz2ned, s.orient);
237 string speed_set = fgGetString("/sim/presets/speed-set", "UVW");
239 bool needCopy = false;
240 switch (_speed_set) {
242 v[0] = get_V_north() * FT2M * -1.0;
243 v[1] = get_V_east() * FT2M * -1.0;
244 v[2] = get_V_down() * FT2M * -1.0;
247 v[0] = get_uBody() * FT2M;
248 v[1] = get_vBody() * FT2M;
249 v[2] = get_wBody() * FT2M;
250 Math::tmul33(s.orient, v, v);
253 v[0] = Atmosphere::spdFromVCAS(get_V_calibrated_kts()/MPS2KTS,
257 Math::tmul33(s.orient, v, v);
261 v[0] = Atmosphere::spdFromMach(get_Mach_number(), temp);
264 Math::tmul33(s.orient, v, v);
274 _speed_set = UVW; // change to this after initial setting
277 if(copyState || needCopy)
281 Math::tmul33(xyz2ned, wind, wind);
282 model->setWind(wind);
284 // ground. Calculate a cartesian coordinate for the ground under
285 // us, find the (geodetic) up vector normal to the ground, then
286 // use that to find the final (radius) term of the plane equation.
287 double xyz[3], gplane[3]; float up[3];
288 sgGeodToCart(lat, lon, ground, xyz);
289 Glue::geodUp(lat, lon, up); // FIXME, needless reverse computation...
291 for(i=0; i<3; i++) gplane[i] = up[i];
292 double rad = gplane[0]*xyz[0] + gplane[1]*xyz[1] + gplane[2]*xyz[2];
293 model->setGroundPlane(gplane, rad);
296 model->setAir(pressure, temp, dens);
299 // All the settables:
301 // These are set below:
304 // _set_Accels_CG_Body
305 // _set_Accels_Pilot_Body
306 // _set_Accels_CG_Body_N
307 // _set_Velocities_Local
308 // _set_Velocities_Ground
309 // _set_Velocities_Wind_Body
314 // _set_V_ground_speed
316 // _set_V_calibrated_kts
324 // _set_Geodetic_Position
325 // _set_Runway_altitude
327 // Ignoring these, because they're unused:
328 // _set_Geocentric_Position
329 // _set_Geocentric_Rates
332 // _set_Earth_position_angle (WTF?)
333 // _set_Gamma_vert_rad
335 // _set_T_Local_to_Body
337 // _set_Sea_Level_Radius
339 // Externally set via the weather code:
340 // _set_Velocities_Local_Airmass
342 // _set_Static_pressure
343 // _set_Static_temperature
344 void YASim::copyFromYASim()
346 Airplane* airplane = _fdm->getAirplane();
347 Model* model = airplane->getModel();
348 State* s = model->getState();
351 double lat, lon, alt;
352 sgCartToGeod(s->pos, &lat, &lon, &alt);
353 _set_Geodetic_Position(lat, lon, alt*M2FT);
356 //_set_Geocentric_Position(Glue::geod2geocLat(lat), lon, alt*M2FT);
358 _set_Altitude_AGL(model->getAGL() * M2FT);
360 // useful conversion matrix
362 Glue::xyz2nedMat(lat, lon, xyz2ned);
366 Math::vmul33(xyz2ned, s->v, v);
367 _set_Velocities_Local(M2FT*v[0], M2FT*v[1], M2FT*v[2]);
368 _set_V_ground_speed(Math::sqrt(M2FT*v[0]*M2FT*v[0] +
369 M2FT*v[1]*M2FT*v[1]));
370 _set_Climb_Rate(-M2FT*v[2]);
372 // The HUD uses this, but inverts down (?!)
373 _set_Velocities_Ground(M2FT*v[0], M2FT*v[1], -M2FT*v[2]);
375 // _set_Geocentric_Rates(M2FT*v[0], M2FT*v[1], M2FT*v[2]); // UNUSED
379 wind[0] = get_V_north_airmass() * FT2M * -1.0; // Wind in NED
380 wind[1] = get_V_east_airmass() * FT2M * -1.0;
381 wind[2] = get_V_down_airmass() * FT2M * -1.0;
382 Math::tmul33(xyz2ned, wind, wind); // Wind in global
383 Math::sub3(s->v, wind, v); // V - wind in global
384 Math::vmul33(s->orient, v, v); // to body coordinates
385 _set_Velocities_Wind_Body(v[0]*M2FT, -v[1]*M2FT, -v[2]*M2FT);
386 _set_V_rel_wind(Math::mag3(v)*M2FT); // units?
388 float P = fgGetDouble("/environment/pressure-inhg") * INHG2PA;
389 float T = fgGetDouble("/environment/temperature-degC") + 273.15;
390 float D = fgGetFloat("/environment/density-slugft3")
391 *SLUG2KG * M2FT*M2FT*M2FT;
392 _set_V_equiv_kts(Atmosphere::calcVEAS(v[0], P, T, D)*MPS2KTS);
393 _set_V_calibrated_kts(Atmosphere::calcVCAS(v[0], P, T)*MPS2KTS);
394 _set_Mach_number(Atmosphere::calcMach(v[0], T));
397 Math::vmul33(xyz2ned, s->acc, v);
398 _set_Accels_Local(M2FT*v[0], M2FT*v[1], M2FT*v[2]);
400 Math::vmul33(s->orient, s->acc, v);
401 _set_Accels_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]);
402 _set_Accels_CG_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]);
404 _fdm->getAirplane()->getPilotAccel(v);
405 _set_Accels_Pilot_Body(-M2FT*v[0], M2FT*v[1], M2FT*v[2]);
407 // There is no property for pilot G's, but I need it for a panel
408 // instrument. Hack this in here, and REMOVE IT WHEN IT FINDS A
410 fgSetFloat("/accelerations/pilot-g", -v[2]/9.8);
412 // The one appears (!) to want inverted pilot acceleration
413 // numbers, in G's...
414 Math::mul3(1.0/9.8, v, v);
415 _set_Accels_CG_Body_N(v[0], -v[1], -v[2]);
419 Glue::calcAlphaBeta(s, &alpha, &beta);
424 Math::trans33(xyz2ned, tmp);
425 Math::mmul33(s->orient, tmp, tmp);
426 float roll, pitch, hdg;
427 Glue::orient2euler(tmp, &roll, &pitch, &hdg);
428 // make heading positive value
429 if(hdg < 0.0) hdg += PI2;
430 _set_Euler_Angles(roll, pitch, hdg);
433 Math::vmul33(s->orient, s->rot, v);
434 _set_Omega_Body(v[0], -v[1], -v[2]);
436 Glue::calcEulerRates(s, &roll, &pitch, &hdg);
437 _set_Euler_Rates(roll, pitch, hdg);
439 // Fill out our engine and gear objects
441 for(i=0; i<airplane->numGear(); i++) {
442 Gear* g = airplane->getGear(i);
443 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
444 node->setBoolValue("has-brake", g->getBrake() != 0);
445 node->setBoolValue("wow", g->getCompressFraction() != 0);
446 node->setFloatValue("compression-norm", g->getCompressFraction());
449 for(i=0; i<model->numThrusters(); i++) {
450 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
451 Thruster* t = model->getThruster(i);
453 node->setBoolValue("running", t->isRunning());
454 node->setBoolValue("cranking", t->isCranking());
458 node->setDoubleValue("prop-thrust", Math::mag3(tmp) * KG2LBS / 9.8);
460 PropEngine* pe = t->getPropEngine();
462 node->setDoubleValue("rpm", pe->getOmega() * RAD2RPM);
465 float power = Math::mag3(tmp) * pe->getOmega();
466 float maxPower = pe->getPistonEngine()->getMaxPower();
468 node->setDoubleValue("max-hp", maxPower * W2HP);
469 node->setDoubleValue("power-pct", 100 * power/maxPower);