3 #include <simgear/misc/sg_path.hxx>
4 #include <simgear/debug/logstream.hxx>
5 #include <simgear/xml/easyxml.hxx>
6 #include <Main/globals.hxx>
7 #include <Main/fg_props.hxx>
8 #include <Scenery/scenery.hxx>
11 #include "Atmosphere.hpp"
13 #include "Airplane.hpp"
15 #include "Integrator.hpp"
18 #include "PropEngine.hpp"
19 #include "PistonEngine.hpp"
23 using namespace yasim;
25 static const float RAD2DEG = 180/3.14159265358979323846;
26 static const float RAD2RPM = 9.54929658551;
27 static const float M2FT = 3.2808399;
28 static const float FT2M = 0.3048;
29 static const float MPS2KTS = 3600.0/1852.0;
30 static const float CM2GALS = 264.172037284; // gallons/cubic meter
31 static const float KG2LBS = 2.20462262185;
32 static const float W2HP = 1.3416e-3;
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);
87 a->getModel()->getBody()->getCG(cg);
88 sprintf(buf, " CG: %.1f, %.1f, %.1f", cg[0], cg[1], cg[2]);
89 SG_LOG(SG_FLIGHT, SG_INFO, buf);
91 if(a->getFailureMsg()) {
92 SG_LOG(SG_FLIGHT, SG_ALERT, "YASim SOLUTION FAILURE:");
93 SG_LOG(SG_FLIGHT, SG_ALERT, a->getFailureMsg());
100 // Run the superclass bind to set up a bunch of property ties
103 // Now UNtie the ones that we are going to set ourselves.
104 fgUntie("/consumables/fuel/tank[0]/level-gal_us");
105 fgUntie("/consumables/fuel/tank[1]/level-gal_us");
108 for(int i=0; i<_fdm->getAirplane()->getModel()->numThrusters(); i++) {
109 sprintf(buf, "/engines/engine[%d]/fuel-flow-gph", i); fgUntie(buf);
110 sprintf(buf, "/engines/engine[%d]/rpm", i); fgUntie(buf);
111 sprintf(buf, "/engines/engine[%d]/mp-osi", i); fgUntie(buf);
112 sprintf(buf, "/engines/engine[%d]/egt-degf", i); fgUntie(buf);
119 Airplane* a = _fdm->getAirplane();
120 Model* m = a->getModel();
125 m->setCrashed(false);
127 // Build a filename and parse it
128 SGPath f(globals->get_fg_root());
129 f.append("Aircraft-yasim");
130 f.append(fgGetString("/sim/aero"));
132 readXML(f.str(), *_fdm);
134 // Compile it into a real airplane, and tell the user what they got
140 // Create some FG{Eng|Gear}Interface objects
142 for(i=0; i<a->numGear(); i++) {
143 Gear* g = a->getGear(i);
144 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
147 node->setDoubleValue("xoffset-in", pos[0]);
148 node->setDoubleValue("yoffset-in", pos[1]);
149 node->setDoubleValue("zoffset-in", pos[2]);
151 for(i=0; i<m->numThrusters(); i++) {
152 // Sanify the initial input conditions
154 sprintf(buf, "/controls/throttle[%d]", i); fgSetFloat(buf, 0);
155 sprintf(buf, "/controls/mixture[%d]", i); fgSetFloat(buf, 1);
156 sprintf(buf, "/controls/propeller-pitch[%d]", i); fgSetFloat(buf, 1);
157 sprintf(buf, "/controls/afterburner[%d]", i); fgSetFloat(buf, 0);
160 fgSetFloat("/controls/slats", 0);
161 fgSetFloat("/controls/spoilers", 0);
163 // Are we at ground level? If so, lift the plane up so the gear
165 if(get_Altitude() - get_Runway_altitude() < 50) {
166 float minGearZ = 1e18;
167 for(i=0; i<a->numGear(); i++) {
168 Gear* g = a->getGear(i);
171 if(pos[2] < minGearZ)
174 _set_Altitude(get_Runway_altitude() - minGearZ*M2FT);
177 // The pilot's eyepoint
179 a->getPilotPos(pilot);
180 fgSetFloat("/sim/view/pilot/x-offset-m", -pilot[0]);
181 fgSetFloat("/sim/view/pilot/y-offset-m", -pilot[1]);
182 fgSetFloat("/sim/view/pilot/z-offset-m", pilot[2]);
184 // Blank the state, and copy in ours
189 _fdm->getExternalInput();
190 _fdm->getAirplane()->stabilizeThrust();
195 void YASim::update(int iterations)
197 // If we're crashed, then we don't care
198 if(_fdm->getAirplane()->getModel()->isCrashed())
202 for(i=0; i<iterations; i++) {
211 void YASim::copyToYASim(bool copyState)
214 float lat = get_Latitude();
215 float lon = get_Longitude();
216 float alt = get_Altitude() * FT2M;
217 float roll = get_Phi();
218 float pitch = get_Theta();
219 float hdg = get_Psi();
223 wind[0] = get_V_north_airmass() * FT2M;
224 wind[1] = get_V_east_airmass() * FT2M;
225 wind[2] = get_V_down_airmass() * FT2M;
227 // The ground elevation doesn't come from FGInterface; query it
228 // from the scenery and set it for others to find.
229 double ground = scenery.get_cur_elev();
230 _set_Runway_altitude(ground * FT2M);
231 // cout << "YASIM: ground = " << ground << endl;
233 // You'd this this would work, but it doesn't. These values are
234 // always zero. Use a "standard" pressure intstead.
236 // float pressure = get_Static_pressure();
237 // float temp = get_Static_temperature();
238 float pressure = Atmosphere::getStdPressure(alt);
239 float temp = Atmosphere::getStdTemperature(alt);
242 Model* model = _fdm->getAirplane()->getModel();
245 Glue::xyz2nedMat(lat, lon, xyz2ned);
248 Glue::geod2xyz(lat, lon, alt, s.pos);
251 Glue::euler2orient(roll, pitch, hdg, s.orient);
252 Math::mmul33(s.orient, xyz2ned, s.orient);
254 // Copy in the existing velocity for now...
255 Math::set3(model->getState()->v, s.v);
261 Math::tmul33(xyz2ned, wind, wind);
262 model->setWind(wind);
264 // ground. Calculate a cartesian coordinate for the ground under
265 // us, find the (geodetic) up vector normal to the ground, then
266 // use that to find the final (radius) term of the plane equation.
267 double xyz[3], gplane[3]; float up[3];
268 Glue::geod2xyz(lat, lon, ground, xyz);
269 Glue::geodUp(xyz, up); // FIXME, needless reverse computation...
271 for(i=0; i<3; i++) gplane[i] = up[i];
272 double rad = gplane[0]*xyz[0] + gplane[1]*xyz[1] + gplane[2]*xyz[2];
273 model->setGroundPlane(gplane, rad);
276 model->setAir(pressure, temp);
279 // All the settables:
281 // These are set below:
284 // _set_Accels_CG_Body
285 // _set_Accels_Pilot_Body
286 // _set_Accels_CG_Body_N
287 // _set_Velocities_Local
288 // _set_Velocities_Ground
289 // _set_Velocities_Wind_Body
294 // _set_V_ground_speed
296 // _set_V_calibrated_kts
304 // _set_Geodetic_Position
305 // _set_Runway_altitude
307 // Ignoring these, because they're unused:
308 // _set_Geocentric_Position
309 // _set_Geocentric_Rates
312 // _set_Earth_position_angle (WTF?)
313 // _set_Gamma_vert_rad
315 // _set_T_Local_to_Body
317 // _set_Sea_Level_Radius
319 // Externally set via the weather code:
320 // _set_Velocities_Local_Airmass
322 // _set_Static_pressure
323 // _set_Static_temperature
324 void YASim::copyFromYASim()
326 Airplane* airplane = _fdm->getAirplane();
327 Model* model = airplane->getModel();
328 State* s = model->getState();
331 double lat, lon, alt;
332 Glue::xyz2geod(s->pos, &lat, &lon, &alt);
333 _set_Geodetic_Position(lat, lon, alt*M2FT);
336 //_set_Geocentric_Position(Glue::geod2geocLat(lat), lon, alt*M2FT);
338 _set_Altitude_AGL(model->getAGL() * M2FT);
340 // useful conversion matrix
342 Glue::xyz2nedMat(lat, lon, xyz2ned);
346 Math::vmul33(xyz2ned, s->v, v);
347 _set_Velocities_Local(M2FT*v[0], M2FT*v[1], M2FT*v[2]);
348 _set_V_ground_speed(Math::sqrt(M2FT*v[0]*M2FT*v[0] +
349 M2FT*v[1]*M2FT*v[1]));
350 _set_Climb_Rate(-M2FT*v[2]);
352 // The HUD uses this, but inverts down (?!)
353 _set_Velocities_Ground(M2FT*v[0], M2FT*v[1], -M2FT*v[2]);
355 // _set_Geocentric_Rates(M2FT*v[0], M2FT*v[1], M2FT*v[2]); // UNUSED
357 Math::vmul33(s->orient, s->v, v);
358 _set_Velocities_Wind_Body(v[0]*M2FT, -v[1]*M2FT, -v[2]*M2FT);
359 _set_V_rel_wind(Math::mag3(v)*M2FT); // units?
361 // These don't work, use a dummy based on altitude
362 // float P = get_Static_pressure();
363 // float T = get_Static_temperature();
364 float P = Atmosphere::getStdPressure(alt);
365 float T = Atmosphere::getStdTemperature(alt);
366 _set_V_equiv_kts(Atmosphere::calcVEAS(v[0], P, T)*MPS2KTS);
367 _set_V_calibrated_kts(Atmosphere::calcVCAS(v[0], P, T)*MPS2KTS);
368 _set_Mach_number(Atmosphere::calcMach(v[0], T));
371 Math::vmul33(xyz2ned, s->acc, v);
372 _set_Accels_Local(M2FT*v[0], M2FT*v[1], M2FT*v[2]);
374 Math::vmul33(s->orient, s->acc, v);
375 _set_Accels_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]);
376 _set_Accels_CG_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]);
378 _fdm->getAirplane()->getPilotAccel(v);
379 _set_Accels_Pilot_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]);
381 // The one appears (!) to want inverted pilot acceleration
382 // numbers, in G's...
383 Math::mul3(1.0/9.8, v, v);
384 _set_Accels_CG_Body_N(v[0], -v[1], -v[2]);
388 Glue::calcAlphaBeta(s, &alpha, &beta);
393 Math::trans33(xyz2ned, tmp);
394 Math::mmul33(s->orient, tmp, tmp);
395 float roll, pitch, hdg;
396 Glue::orient2euler(tmp, &roll, &pitch, &hdg);
397 _set_Euler_Angles(roll, pitch, hdg);
400 Math::vmul33(s->orient, s->rot, v);
401 _set_Omega_Body(v[0], -v[1], -v[2]);
403 Glue::calcEulerRates(s, &roll, &pitch, &hdg);
404 _set_Euler_Rates(roll, pitch, hdg);
406 // Fill out our engine and gear objects
408 for(i=0; i<airplane->numGear(); i++) {
409 Gear* g = airplane->getGear(i);
410 SGPropertyNode * node = fgGetNode("gear/gear", i, true);
411 node->setBoolValue("has-brake", g->getBrake() != 0);
412 node->setBoolValue("wow", g->getCompressFraction() != 0);
413 node->setBoolValue("position", g->getExtension());
416 for(i=0; i<model->numThrusters(); i++) {
417 SGPropertyNode * node = fgGetNode("engines/engine", i, true);
418 Thruster* t = model->getThruster(i);
420 node->setBoolValue("running", true);
421 node->setBoolValue("cranking", false);
423 // Note: assumes all tanks have the same fuel density!
424 node->setDoubleValue("fuel-flow-gph", CM2GALS * t->getFuelFlow()
425 / airplane->getFuelDensity(0));
429 node->setDoubleValue("prop-thrust", Math::mag3(tmp) * KG2LBS / 9.8);
431 PropEngine* pe = t->getPropEngine();
433 node->setDoubleValue("rpm", pe->getOmega() * RAD2RPM);
436 float power = Math::mag3(tmp) * pe->getOmega();
437 float maxPower = pe->getPistonEngine()->getMaxPower();
439 node->setDoubleValue("max-hp", maxPower * W2HP);
440 node->setDoubleValue("power-pct", 100 * power/maxPower);