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/placement.hxx>
6 #include <simgear/xml/easyxml.hxx>
8 #include <Main/globals.hxx>
9 #include <Main/fg_props.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 YASIM_PI = 3.14159265358979323846;
27 static const float RAD2DEG = 180/YASIM_PI;
28 static const float PI2 = YASIM_PI*2;
29 static const float RAD2RPM = 9.54929658551;
30 static const float M2FT = 3.2808399;
31 static const float FT2M = 0.3048;
32 static const float MPS2KTS = 3600.0/1852.0;
33 static const float CM2GALS = 264.172037284; // gallons/cubic meter
34 static const float KG2LBS = 2.20462262185;
35 static const float W2HP = 1.3416e-3;
36 static const float INHG2PA = 3386.389;
37 static const float SLUG2KG = 14.59390;
39 YASim::YASim(double dt)
46 _fdm->getAirplane()->getModel()->getIntegrator()->setInterval(_dt);
51 Airplane* a = _fdm->getAirplane();
53 float aoa = a->getCruiseAoA() * RAD2DEG;
54 float tail = -1 * a->getTailIncidence() * RAD2DEG;
55 float drag = 1000 * a->getDragCoefficient();
57 SG_LOG(SG_FLIGHT,SG_INFO,"YASim solution results:");
58 SG_LOG(SG_FLIGHT,SG_INFO," Iterations: "<<a->getSolutionIterations());
59 SG_LOG(SG_FLIGHT,SG_INFO," Drag Coefficient: "<< drag);
60 SG_LOG(SG_FLIGHT,SG_INFO," Lift Ratio: "<<a->getLiftRatio());
61 SG_LOG(SG_FLIGHT,SG_INFO," Cruise AoA: "<< aoa);
62 SG_LOG(SG_FLIGHT,SG_INFO," Tail Incidence: "<< tail);
63 SG_LOG(SG_FLIGHT,SG_INFO,"Approach Elevator: "<<a->getApproachElevator());
68 a->getModel()->getBody()->getCG(cg);
69 sprintf(buf, " CG: %.3f, %.3f, %.3f", cg[0], cg[1], cg[2]);
70 SG_LOG(SG_FLIGHT, SG_INFO, buf);
72 if(a->getFailureMsg()) {
73 SG_LOG(SG_FLIGHT, SG_ALERT, "YASim SOLUTION FAILURE:");
74 SG_LOG(SG_FLIGHT, SG_ALERT, a->getFailureMsg());
81 // Run the superclass bind to set up a bunch of property ties
84 // Now UNtie the ones that we are going to set ourselves.
85 fgUntie("/consumables/fuel/tank[0]/level-gal_us");
86 fgUntie("/consumables/fuel/tank[1]/level-gal_us");
89 for(int i=0; i<_fdm->getAirplane()->getModel()->numThrusters(); i++) {
90 sprintf(buf, "/engines/engine[%d]/fuel-flow-gph", i); fgUntie(buf);
91 sprintf(buf, "/engines/engine[%d]/rpm", i); fgUntie(buf);
92 sprintf(buf, "/engines/engine[%d]/mp-osi", i); fgUntie(buf);
93 sprintf(buf, "/engines/engine[%d]/egt-degf", i); fgUntie(buf);
99 Airplane* a = _fdm->getAirplane();
100 Model* m = a->getModel();
105 m->setCrashed(false);
107 // Figure out the initial speed type
108 string speed_set = fgGetString("/sim/presets/speed-set", "UVW");
109 if (speed_set == "NED")
111 else if (speed_set == "UVW")
113 else if (speed_set == "knots")
115 else if (speed_set == "mach")
119 SG_LOG(SG_FLIGHT, SG_ALERT, "Unknown speed type " << speed_set);
122 // Build a filename and parse it
123 SGPath f(fgGetString("/sim/aircraft-dir"));
124 f.append(fgGetString("/sim/aero"));
126 readXML(f.str(), *_fdm);
128 // Compile it into a real airplane, and tell the user what they got
134 // Create some FG{Eng|Gear}Interface objects
136 for(i=0; i<a->numGear(); i++) {
137 Gear* g = a->getGear(i);
138 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
141 node->setDoubleValue("xoffset-in", pos[0]);
142 node->setDoubleValue("yoffset-in", pos[1]);
143 node->setDoubleValue("zoffset-in", pos[2]);
146 // Are we at ground level? If so, lift the plane up so the gear
148 double runway_altitude = get_Runway_altitude();
149 if(get_Altitude() - runway_altitude < 50) {
150 fgSetBool("/controls/gear/gear-down", false);
151 float minGearZ = 1e18;
152 for(i=0; i<a->numGear(); i++) {
153 Gear* g = a->getGear(i);
156 if(pos[2] < minGearZ)
159 _set_Altitude(runway_altitude - minGearZ*M2FT);
160 fgSetBool("/controls/gear/gear-down", true);
163 // Blank the state, and copy in ours
168 _fdm->getExternalInput();
169 _fdm->getAirplane()->initEngines();
174 void YASim::update(double dt)
179 int iterations = _calc_multiloop(dt);
181 // If we're crashed, then we don't care
182 if(_fdm->getAirplane()->getModel()->isCrashed()) {
183 if(!fgGetBool("/sim/crashed"))
184 fgSetBool("/sim/crashed", true);
189 for(i=0; i<iterations; i++) {
196 void YASim::copyToYASim(bool copyState)
199 float lat = get_Latitude();
200 float lon = get_Longitude();
201 float alt = get_Altitude() * FT2M;
202 float roll = get_Phi();
203 float pitch = get_Theta();
204 float hdg = get_Psi();
208 wind[0] = get_V_north_airmass() * FT2M * -1.0;
209 wind[1] = get_V_east_airmass() * FT2M * -1.0;
210 wind[2] = get_V_down_airmass() * FT2M * -1.0;
212 // Get ground elevation
213 double ground = fgGetDouble("/position/ground-elev-m");
214 // cout << "YASIM: ground = " << ground << endl;
216 float pressure = fgGetFloat("/environment/pressure-inhg") * INHG2PA;
217 float temp = fgGetFloat("/environment/temperature-degc") + 273.15;
218 float dens = fgGetFloat("/environment/density-slugft3")
219 * SLUG2KG * M2FT*M2FT*M2FT;
222 Model* model = _fdm->getAirplane()->getModel();
225 Glue::xyz2nedMat(lat, lon, xyz2ned);
228 sgGeodToCart(lat, lon, alt, s.pos);
231 Glue::euler2orient(roll, pitch, hdg, s.orient);
232 Math::mmul33(s.orient, xyz2ned, s.orient);
235 string speed_set = fgGetString("/sim/presets/speed-set", "UVW");
237 bool needCopy = false;
238 switch (_speed_set) {
240 v[0] = get_V_north() * FT2M * -1.0;
241 v[1] = get_V_east() * FT2M * -1.0;
242 v[2] = get_V_down() * FT2M * -1.0;
245 v[0] = get_uBody() * FT2M;
246 v[1] = get_vBody() * FT2M;
247 v[2] = get_wBody() * FT2M;
248 Math::tmul33(s.orient, v, v);
251 v[0] = Atmosphere::spdFromVCAS(get_V_calibrated_kts()/MPS2KTS,
255 Math::tmul33(s.orient, v, v);
259 v[0] = Atmosphere::spdFromMach(get_Mach_number(), temp);
262 Math::tmul33(s.orient, v, v);
272 _speed_set = UVW; // change to this after initial setting
275 if(copyState || needCopy)
279 Math::tmul33(xyz2ned, wind, wind);
280 model->setWind(wind);
282 // ground. Calculate a cartesian coordinate for the ground under
283 // us, find the (geodetic) up vector normal to the ground, then
284 // use that to find the final (radius) term of the plane equation.
285 double xyz[3], gplane[3]; float up[3];
286 sgGeodToCart(lat, lon, ground, xyz);
287 Glue::geodUp(lat, lon, up); // FIXME, needless reverse computation...
289 for(i=0; i<3; i++) gplane[i] = up[i];
290 double rad = gplane[0]*xyz[0] + gplane[1]*xyz[1] + gplane[2]*xyz[2];
291 model->setGroundPlane(gplane, rad);
294 model->setAir(pressure, temp, dens);
297 // All the settables:
299 // These are set below:
302 // _set_Accels_CG_Body
303 // _set_Accels_Pilot_Body
304 // _set_Accels_CG_Body_N
305 // _set_Velocities_Local
306 // _set_Velocities_Ground
307 // _set_Velocities_Wind_Body
312 // _set_V_ground_speed
314 // _set_V_calibrated_kts
322 // _set_Geodetic_Position
323 // _set_Runway_altitude
325 // Ignoring these, because they're unused:
326 // _set_Geocentric_Position
327 // _set_Geocentric_Rates
330 // _set_Earth_position_angle (WTF?)
331 // _set_Gamma_vert_rad
333 // _set_T_Local_to_Body
335 // _set_Sea_Level_Radius
337 // Externally set via the weather code:
338 // _set_Velocities_Local_Airmass
340 // _set_Static_pressure
341 // _set_Static_temperature
342 void YASim::copyFromYASim()
344 Airplane* airplane = _fdm->getAirplane();
345 Model* model = airplane->getModel();
346 State* s = model->getState();
349 double lat, lon, alt;
350 sgCartToGeod(s->pos, &lat, &lon, &alt);
351 _set_Geodetic_Position(lat, lon, alt*M2FT);
354 //_set_Geocentric_Position(Glue::geod2geocLat(lat), lon, alt*M2FT);
356 _set_Altitude_AGL(model->getAGL() * M2FT);
358 // useful conversion matrix
360 Glue::xyz2nedMat(lat, lon, xyz2ned);
364 Math::vmul33(xyz2ned, s->v, v);
365 _set_Velocities_Local(M2FT*v[0], M2FT*v[1], M2FT*v[2]);
366 _set_V_ground_speed(Math::sqrt(M2FT*v[0]*M2FT*v[0] +
367 M2FT*v[1]*M2FT*v[1]));
368 _set_Climb_Rate(-M2FT*v[2]);
370 // The HUD uses this, but inverts down (?!)
371 _set_Velocities_Ground(M2FT*v[0], M2FT*v[1], -M2FT*v[2]);
373 // _set_Geocentric_Rates(M2FT*v[0], M2FT*v[1], M2FT*v[2]); // UNUSED
377 wind[0] = get_V_north_airmass() * FT2M * -1.0; // Wind in NED
378 wind[1] = get_V_east_airmass() * FT2M * -1.0;
379 wind[2] = get_V_down_airmass() * FT2M * -1.0;
380 Math::tmul33(xyz2ned, wind, wind); // Wind in global
381 Math::sub3(s->v, wind, v); // V - wind in global
382 Math::vmul33(s->orient, v, v); // to body coordinates
383 _set_Velocities_Wind_Body(v[0]*M2FT, -v[1]*M2FT, -v[2]*M2FT);
384 _set_V_rel_wind(Math::mag3(v)*M2FT); // units?
386 float P = fgGetDouble("/environment/pressure-inhg") * INHG2PA;
387 float T = fgGetDouble("/environment/temperature-degC") + 273.15;
388 float D = fgGetFloat("/environment/density-slugft3")
389 *SLUG2KG * M2FT*M2FT*M2FT;
390 _set_V_equiv_kts(Atmosphere::calcVEAS(v[0], P, T, D)*MPS2KTS);
391 _set_V_calibrated_kts(Atmosphere::calcVCAS(v[0], P, T)*MPS2KTS);
392 _set_Mach_number(Atmosphere::calcMach(v[0], T));
395 Math::vmul33(xyz2ned, s->acc, v);
396 _set_Accels_Local(M2FT*v[0], M2FT*v[1], M2FT*v[2]);
398 Math::vmul33(s->orient, s->acc, v);
399 _set_Accels_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]);
400 _set_Accels_CG_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]);
402 _fdm->getAirplane()->getPilotAccel(v);
403 _set_Accels_Pilot_Body(-M2FT*v[0], M2FT*v[1], M2FT*v[2]);
405 // There is no property for pilot G's, but I need it for a panel
406 // instrument. Hack this in here, and REMOVE IT WHEN IT FINDS A
408 fgSetFloat("/accelerations/pilot-g", -v[2]/9.8);
410 // The one appears (!) to want inverted pilot acceleration
411 // numbers, in G's...
412 Math::mul3(1.0/9.8, v, v);
413 _set_Accels_CG_Body_N(v[0], -v[1], -v[2]);
417 Glue::calcAlphaBeta(s, &alpha, &beta);
422 Math::trans33(xyz2ned, tmp);
423 Math::mmul33(s->orient, tmp, tmp);
424 float roll, pitch, hdg;
425 Glue::orient2euler(tmp, &roll, &pitch, &hdg);
426 // make heading positive value
427 if(hdg < 0.0) hdg += PI2;
428 _set_Euler_Angles(roll, pitch, hdg);
431 Math::vmul33(s->orient, s->rot, v);
432 _set_Omega_Body(v[0], -v[1], -v[2]);
434 Glue::calcEulerRates(s, &roll, &pitch, &hdg);
435 _set_Euler_Rates(roll, pitch, hdg);
437 // Fill out our engine and gear objects
439 for(i=0; i<airplane->numGear(); i++) {
440 Gear* g = airplane->getGear(i);
441 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
442 node->setBoolValue("has-brake", g->getBrake() != 0);
443 node->setBoolValue("wow", g->getCompressFraction() != 0);
444 node->setFloatValue("compression-norm", g->getCompressFraction());
447 for(i=0; i<model->numThrusters(); i++) {
448 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
449 Thruster* t = model->getThruster(i);
451 node->setBoolValue("running", t->isRunning());
452 node->setBoolValue("cranking", t->isCranking());
456 node->setDoubleValue("prop-thrust", Math::mag3(tmp) * KG2LBS / 9.8);
458 PropEngine* pe = t->getPropEngine();
460 node->setDoubleValue("rpm", pe->getOmega() * RAD2RPM);
463 float power = Math::mag3(tmp) * pe->getOmega();
464 float maxPower = pe->getPistonEngine()->getMaxPower();
466 node->setDoubleValue("max-hp", maxPower * W2HP);
467 node->setDoubleValue("power-pct", 100 * power/maxPower);