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 <Model/acmodel.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;
31 static const float INHG2PA = 3386.389;
32 static const float SLUG2KG = 14.59390;
34 void YASim::printDEBUG()
36 static int debugCount = 0;
42 // printf("N1 %5.1f N2 %5.1f FF %7.1f EPR %4.2f EGT %6.1f\n",
43 // fgGetFloat("/engines/engine[0]/n1"),
44 // fgGetFloat("/engines/engine[0]/n2"),
45 // fgGetFloat("/engines/engine[0]/fuel-flow-gph"),
46 // fgGetFloat("/engines/engine[0]/epr"),
47 // fgGetFloat("/engines/engine[0]/egt"));
49 // printf("gear: %f\n", fgGetFloat("/controls/gear-down"));
51 // printf("alpha %5.1f beta %5.1f\n", get_Alpha()*57.3, get_Beta()*57.3);
53 // printf("pilot: %f %f %f\n",
54 // fgGetDouble("/sim/view/pilot/x-offset-m"),
55 // fgGetDouble("/sim/view/pilot/y-offset-m"),
56 // fgGetDouble("/sim/view/pilot/z-offset-m"));
60 YASim::YASim(double dt)
67 _fdm->getAirplane()->getModel()->getIntegrator()->setInterval(_dt);
72 Airplane* a = _fdm->getAirplane();
74 float aoa = a->getCruiseAoA() * RAD2DEG;
75 float tail = -1 * a->getTailIncidence() * RAD2DEG;
76 float drag = 1000 * a->getDragCoefficient();
78 SG_LOG(SG_FLIGHT,SG_INFO,"YASim solution results:");
79 SG_LOG(SG_FLIGHT,SG_INFO," Iterations: "<<a->getSolutionIterations());
80 SG_LOG(SG_FLIGHT,SG_INFO," Drag Coefficient: "<< drag);
81 SG_LOG(SG_FLIGHT,SG_INFO," Lift Ratio: "<<a->getLiftRatio());
82 SG_LOG(SG_FLIGHT,SG_INFO," Cruise AoA: "<< aoa);
83 SG_LOG(SG_FLIGHT,SG_INFO," Tail Incidence: "<< tail);
84 SG_LOG(SG_FLIGHT,SG_INFO,"Approach Elevator: "<<a->getApproachElevator());
89 a->getModel()->getBody()->getCG(cg);
90 sprintf(buf, " CG: %.1f, %.1f, %.1f", cg[0], cg[1], cg[2]);
91 SG_LOG(SG_FLIGHT, SG_INFO, buf);
93 if(a->getFailureMsg()) {
94 SG_LOG(SG_FLIGHT, SG_ALERT, "YASim SOLUTION FAILURE:");
95 SG_LOG(SG_FLIGHT, SG_ALERT, a->getFailureMsg());
102 // Run the superclass bind to set up a bunch of property ties
105 // Now UNtie the ones that we are going to set ourselves.
106 fgUntie("/consumables/fuel/tank[0]/level-gal_us");
107 fgUntie("/consumables/fuel/tank[1]/level-gal_us");
110 for(int i=0; i<_fdm->getAirplane()->getModel()->numThrusters(); i++) {
111 sprintf(buf, "/engines/engine[%d]/fuel-flow-gph", i); fgUntie(buf);
112 sprintf(buf, "/engines/engine[%d]/rpm", i); fgUntie(buf);
113 sprintf(buf, "/engines/engine[%d]/mp-osi", i); fgUntie(buf);
114 sprintf(buf, "/engines/engine[%d]/egt-degf", i); fgUntie(buf);
121 Airplane* a = _fdm->getAirplane();
122 Model* m = a->getModel();
127 m->setCrashed(false);
129 // Build a filename and parse it
130 SGPath f(globals->get_fg_root());
131 f.append("Aircraft-yasim");
132 f.append(fgGetString("/sim/aero"));
134 readXML(f.str(), *_fdm);
136 // Compile it into a real airplane, and tell the user what they got
142 // Create some FG{Eng|Gear}Interface objects
144 for(i=0; i<a->numGear(); i++) {
145 Gear* g = a->getGear(i);
146 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
149 node->setDoubleValue("xoffset-in", pos[0]);
150 node->setDoubleValue("yoffset-in", pos[1]);
151 node->setDoubleValue("zoffset-in", pos[2]);
153 for(i=0; i<m->numThrusters(); i++) {
154 // Sanify the initial input conditions
156 sprintf(buf, "/controls/throttle[%d]", i); fgSetFloat(buf, 0);
157 sprintf(buf, "/controls/mixture[%d]", i); fgSetFloat(buf, 1);
158 sprintf(buf, "/controls/propeller-pitch[%d]", i); fgSetFloat(buf, 1);
159 sprintf(buf, "/controls/afterburner[%d]", i); fgSetFloat(buf, 0);
162 fgSetFloat("/controls/slats", 0);
163 fgSetFloat("/controls/spoilers", 0);
165 // Are we at ground level? If so, lift the plane up so the gear
167 double runway_altitude = get_Runway_altitude();
168 fgSetBool("/controls/gear-down", false);
169 if(get_Altitude() - runway_altitude < 50) {
170 float minGearZ = 1e18;
171 for(i=0; i<a->numGear(); i++) {
172 Gear* g = a->getGear(i);
175 if(pos[2] < minGearZ)
178 _set_Altitude(runway_altitude - minGearZ*M2FT);
179 fgSetBool("/controls/gear-down", true);
182 // The pilot's eyepoint
184 a->getPilotPos(pilot);
185 // fgSetFloat("/sim/view/pilot/x-offset-m", -pilot[0]);
186 // fgSetFloat("/sim/view/pilot/y-offset-m", -pilot[1]);
187 // fgSetFloat("/sim/view/pilot/z-offset-m", pilot[2]);
189 // Blank the state, and copy in ours
194 _fdm->getExternalInput();
195 _fdm->getAirplane()->initEngines();
200 void YASim::update(double dt)
205 int iterations = _calc_multiloop(dt);
207 // If we're crashed, then we don't care
208 if(_fdm->getAirplane()->getModel()->isCrashed())
212 for(i=0; i<iterations; i++) {
221 void YASim::copyToYASim(bool copyState)
224 float lat = get_Latitude();
225 float lon = get_Longitude();
226 float alt = get_Altitude() * FT2M;
227 float roll = get_Phi();
228 float pitch = get_Theta();
229 float hdg = get_Psi();
233 wind[0] = get_V_north_airmass() * FT2M * -1.0;
234 wind[1] = get_V_east_airmass() * FT2M * -1.0;
235 wind[2] = get_V_down_airmass() * FT2M * -1.0;
237 // Get ground elevation from the FGinterface's FGlocation data
238 double ground = getACModel()->get3DModel()->getFGLocation()->get_cur_elev_m();
239 // cout << "YASIM: ground = " << ground << endl;
241 float pressure = fgGetFloat("/environment/pressure-inhg") * INHG2PA;
242 float temp = fgGetFloat("/environment/temperature-degc") + 273.15;
243 float dens = fgGetFloat("/environment/density-slugft3")
244 * SLUG2KG * M2FT*M2FT*M2FT;
247 Model* model = _fdm->getAirplane()->getModel();
250 Glue::xyz2nedMat(lat, lon, xyz2ned);
253 Glue::geod2xyz(lat, lon, alt, s.pos);
256 Glue::euler2orient(roll, pitch, hdg, s.orient);
257 Math::mmul33(s.orient, xyz2ned, s.orient);
259 // Copy in the existing velocity for now...
260 Math::set3(model->getState()->v, s.v);
266 Math::tmul33(xyz2ned, wind, wind);
267 model->setWind(wind);
269 // ground. Calculate a cartesian coordinate for the ground under
270 // us, find the (geodetic) up vector normal to the ground, then
271 // use that to find the final (radius) term of the plane equation.
272 double xyz[3], gplane[3]; float up[3];
273 Glue::geod2xyz(lat, lon, ground, xyz);
274 Glue::geodUp(xyz, up); // FIXME, needless reverse computation...
276 for(i=0; i<3; i++) gplane[i] = up[i];
277 double rad = gplane[0]*xyz[0] + gplane[1]*xyz[1] + gplane[2]*xyz[2];
278 model->setGroundPlane(gplane, rad);
281 model->setAir(pressure, temp, dens);
284 // All the settables:
286 // These are set below:
289 // _set_Accels_CG_Body
290 // _set_Accels_Pilot_Body
291 // _set_Accels_CG_Body_N
292 // _set_Velocities_Local
293 // _set_Velocities_Ground
294 // _set_Velocities_Wind_Body
299 // _set_V_ground_speed
301 // _set_V_calibrated_kts
309 // _set_Geodetic_Position
310 // _set_Runway_altitude
312 // Ignoring these, because they're unused:
313 // _set_Geocentric_Position
314 // _set_Geocentric_Rates
317 // _set_Earth_position_angle (WTF?)
318 // _set_Gamma_vert_rad
320 // _set_T_Local_to_Body
322 // _set_Sea_Level_Radius
324 // Externally set via the weather code:
325 // _set_Velocities_Local_Airmass
327 // _set_Static_pressure
328 // _set_Static_temperature
329 void YASim::copyFromYASim()
331 Airplane* airplane = _fdm->getAirplane();
332 Model* model = airplane->getModel();
333 State* s = model->getState();
336 double lat, lon, alt;
337 Glue::xyz2geod(s->pos, &lat, &lon, &alt);
338 _set_Geodetic_Position(lat, lon, alt*M2FT);
341 //_set_Geocentric_Position(Glue::geod2geocLat(lat), lon, alt*M2FT);
343 _set_Altitude_AGL(model->getAGL() * M2FT);
345 // useful conversion matrix
347 Glue::xyz2nedMat(lat, lon, xyz2ned);
351 Math::vmul33(xyz2ned, s->v, v);
352 _set_Velocities_Local(M2FT*v[0], M2FT*v[1], M2FT*v[2]);
353 _set_V_ground_speed(Math::sqrt(M2FT*v[0]*M2FT*v[0] +
354 M2FT*v[1]*M2FT*v[1]));
355 _set_Climb_Rate(-M2FT*v[2]);
357 // The HUD uses this, but inverts down (?!)
358 _set_Velocities_Ground(M2FT*v[0], M2FT*v[1], -M2FT*v[2]);
360 // _set_Geocentric_Rates(M2FT*v[0], M2FT*v[1], M2FT*v[2]); // UNUSED
364 wind[0] = get_V_north_airmass() * FT2M * -1.0; // Wind in NED
365 wind[1] = get_V_east_airmass() * FT2M * -1.0;
366 wind[2] = get_V_down_airmass() * FT2M * -1.0;
367 Math::tmul33(xyz2ned, wind, wind); // Wind in global
368 Math::sub3(s->v, wind, v); // V - wind in global
369 Math::vmul33(s->orient, s->v, v); // to body coordinates
370 _set_Velocities_Wind_Body(v[0]*M2FT, -v[1]*M2FT, -v[2]*M2FT);
371 _set_V_rel_wind(Math::mag3(v)*M2FT); // units?
373 float P = fgGetDouble("/environment/pressure-inhg") * INHG2PA;
374 float T = fgGetDouble("/environment/temperature-degC") + 273.15;
375 float D = fgGetFloat("/environment/density-slugft3")
376 *SLUG2KG * M2FT*M2FT*M2FT;
377 _set_V_equiv_kts(Atmosphere::calcVEAS(v[0], P, T, D)*MPS2KTS);
378 _set_V_calibrated_kts(Atmosphere::calcVCAS(v[0], P, T)*MPS2KTS);
379 _set_Mach_number(Atmosphere::calcMach(v[0], T));
382 Math::vmul33(xyz2ned, s->acc, v);
383 _set_Accels_Local(M2FT*v[0], M2FT*v[1], M2FT*v[2]);
385 Math::vmul33(s->orient, s->acc, v);
386 _set_Accels_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]);
387 _set_Accels_CG_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]);
389 _fdm->getAirplane()->getPilotAccel(v);
390 _set_Accels_Pilot_Body(M2FT*v[0], M2FT*v[1], -M2FT*v[2]);
392 // There is no property for pilot G's, but I need it for a panel
393 // instrument. Hack this in here, and REMOVE IT WHEN IT FINDS A
395 fgSetFloat("/accelerations/pilot-g", -v[2]/9.8);
397 // The one appears (!) to want inverted pilot acceleration
398 // numbers, in G's...
399 Math::mul3(1.0/9.8, v, v);
400 _set_Accels_CG_Body_N(v[0], -v[1], -v[2]);
404 Glue::calcAlphaBeta(s, &alpha, &beta);
409 Math::trans33(xyz2ned, tmp);
410 Math::mmul33(s->orient, tmp, tmp);
411 float roll, pitch, hdg;
412 Glue::orient2euler(tmp, &roll, &pitch, &hdg);
413 _set_Euler_Angles(roll, pitch, hdg);
416 Math::vmul33(s->orient, s->rot, v);
417 _set_Omega_Body(v[0], -v[1], -v[2]);
419 Glue::calcEulerRates(s, &roll, &pitch, &hdg);
420 _set_Euler_Rates(roll, pitch, hdg);
422 // Fill out our engine and gear objects
424 for(i=0; i<airplane->numGear(); i++) {
425 Gear* g = airplane->getGear(i);
426 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
427 node->setBoolValue("has-brake", g->getBrake() != 0);
428 node->setBoolValue("wow", g->getCompressFraction() != 0);
429 node->setFloatValue("compression-norm", g->getCompressFraction());
432 for(i=0; i<model->numThrusters(); i++) {
433 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
434 Thruster* t = model->getThruster(i);
436 node->setBoolValue("running", t->isRunning());
437 node->setBoolValue("cranking", t->isCranking());
441 node->setDoubleValue("prop-thrust", Math::mag3(tmp) * KG2LBS / 9.8);
443 PropEngine* pe = t->getPropEngine();
445 node->setDoubleValue("rpm", pe->getOmega() * RAD2RPM);
448 float power = Math::mag3(tmp) * pe->getOmega();
449 float maxPower = pe->getPistonEngine()->getMaxPower();
451 node->setDoubleValue("max-hp", maxPower * W2HP);
452 node->setDoubleValue("power-pct", 100 * power/maxPower);