3 #include <simgear/misc/sg_path.hxx>
4 #include <simgear/debug/logstream.hxx>
5 #include <simgear/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("RPM %.1f FF %.1f\n",
43 // fgGetFloat("/engines/engine[0]/rpm"),
44 // fgGetFloat("/engines/engine[0]/fuel-flow-gph"));
46 // printf("gear: %f\n", fgGetFloat("/controls/gear-down"));
48 // printf("alpha %5.1f beta %5.1f\n", get_Alpha()*57.3, get_Beta()*57.3);
50 // printf("pilot: %f %f %f\n",
51 // fgGetDouble("/sim/view/pilot/x-offset-m"),
52 // fgGetDouble("/sim/view/pilot/y-offset-m"),
53 // fgGetDouble("/sim/view/pilot/z-offset-m"));
57 YASim::YASim(double dt)
62 // Because the integration method is via fourth-order Runge-Kutta,
63 // we only get an "output" state for every 4 times the internal
64 // forces are calculated. So divide dt by four to account for
65 // this, and only run an iteration every fourth time through
68 _fdm->getAirplane()->getModel()->getIntegrator()->setInterval(_dt);
74 Airplane* a = _fdm->getAirplane();
76 float aoa = a->getCruiseAoA() * RAD2DEG;
77 float tail = -1 * a->getTailIncidence() * RAD2DEG;
78 float drag = 1000 * a->getDragCoefficient();
80 SG_LOG(SG_FLIGHT,SG_INFO,"YASim solution results:");
81 SG_LOG(SG_FLIGHT,SG_INFO," Iterations: "<<a->getSolutionIterations());
82 SG_LOG(SG_FLIGHT,SG_INFO,"Drag Coefficient: "<< drag);
83 SG_LOG(SG_FLIGHT,SG_INFO," Lift Ratio: "<<a->getLiftRatio());
84 SG_LOG(SG_FLIGHT,SG_INFO," Cruise AoA: "<< aoa);
85 SG_LOG(SG_FLIGHT,SG_INFO," Tail Incidence: "<< tail);
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 Airplane* a = _fdm->getAirplane();
103 Model* m = a->getModel();
108 m->setCrashed(false);
110 // Build a filename and parse it
111 SGPath f(globals->get_fg_root());
112 f.append("Aircraft-yasim");
113 f.append(fgGetString("/sim/aero"));
115 readXML(f.str(), *_fdm);
117 // Compile it into a real airplane, and tell the user what they got
123 // Create some FG{Eng|Gear}Interface objects
125 for(i=0; i<a->numGear(); i++) {
126 Gear* g = a->getGear(i);
130 fgg.SetX(pos[0]); fgg.SetY(-pos[1]); fgg.SetZ(-pos[2]);
133 for(i=0; i<m->numThrusters(); i++) {
137 // Sanify the initial input conditions
139 sprintf(buf, "/controls/throttle[%d]", i); fgSetFloat(buf, 0);
140 sprintf(buf, "/controls/mixture[%d]", i); fgSetFloat(buf, 1);
141 sprintf(buf, "/controls/propeller-pitch[%d]", i); fgSetFloat(buf, 1);
142 sprintf(buf, "/controls/afterburner[%d]", i); fgSetFloat(buf, 0);
146 // Are we at ground level? If so, lift the plane up so the gear
148 if(get_Altitude() - get_Runway_altitude() < 50) {
149 float minGearZ = 1e18;
150 for(i=0; i<a->numGear(); i++) {
151 Gear* g = a->getGear(i);
154 if(pos[2] < minGearZ)
157 _set_Altitude(get_Runway_altitude() - minGearZ*M2FT);
160 // The pilot's eyepoint
162 a->getPilotPos(pilot);
163 fgSetFloat("/sim/view/pilot/x-offset-m", -pilot[0]);
164 fgSetFloat("/sim/view/pilot/y-offset-m", -pilot[1]);
165 fgSetFloat("/sim/view/pilot/z-offset-m", pilot[2]);
167 // Blank the state, and copy in ours
175 bool YASim::update(int iterations)
177 // If we're crashed, then we don't care
178 if(_fdm->getAirplane()->getModel()->isCrashed())
182 for(i=0; i<iterations; i++) {
183 // Remember, update only every 4th call
185 if(_updateCount >= 4) {
197 return true; // what does this mean?
200 void YASim::copyToYASim(bool copyState)
203 float lat = get_Latitude();
204 float lon = get_Longitude();
205 float alt = get_Altitude() * FT2M;
206 float roll = get_Phi();
207 float pitch = get_Theta();
208 float hdg = get_Psi();
212 wind[0] = get_V_north_airmass() * FT2M;
213 wind[1] = get_V_east_airmass() * FT2M;
214 wind[2] = get_V_down_airmass() * FT2M;
215 double ground = get_Runway_altitude() * FT2M;
217 // You'd this this would work, but it doesn't. These values are
218 // always zero. Use a "standard" pressure intstead.
220 // float pressure = get_Static_pressure();
221 // float temp = get_Static_temperature();
222 float pressure = Atmosphere::getStdPressure(alt);
223 float temp = Atmosphere::getStdTemperature(alt);
226 Model* model = _fdm->getAirplane()->getModel();
229 Glue::xyz2nedMat(lat, lon, xyz2ned);
232 Glue::geod2xyz(lat, lon, alt, s.pos);
235 Glue::euler2orient(roll, pitch, hdg, s.orient);
236 Math::mmul33(s.orient, xyz2ned, s.orient);
238 // Copy in the existing velocity for now...
239 Math::set3(model->getState()->v, s.v);
245 Math::tmul33(xyz2ned, wind, wind);
246 model->setWind(wind);
248 // ground. Calculate a cartesian coordinate for the ground under
249 // us, find the (geodetic) up vector normal to the ground, then
250 // use that to find the final (radius) term of the plane equation.
251 double xyz[3], gplane[3]; float up[3];
252 Glue::geod2xyz(lat, lon, ground, xyz);
253 Glue::geodUp(xyz, up); // FIXME, needless reverse computation...
255 for(i=0; i<3; i++) gplane[i] = up[i];
256 double rad = gplane[0]*xyz[0] + gplane[1]*xyz[1] + gplane[2]*xyz[2];
257 model->setGroundPlane(gplane, rad);
260 model->setAir(pressure, temp);
263 // All the settables:
265 // These are set below:
268 // _set_Accels_CG_Body
269 // _set_Accels_Pilot_Body
270 // _set_Accels_CG_Body_N
271 // _set_Velocities_Local
272 // _set_Velocities_Ground
273 // _set_Velocities_Wind_Body
278 // _set_V_ground_speed
280 // _set_V_calibrated_kts
288 // _set_Geodetic_Position
289 // _set_Runway_altitude
291 // Ignoring these, because they're unused:
292 // _set_Geocentric_Position
293 // _set_Geocentric_Rates
296 // _set_Earth_position_angle (WTF?)
297 // _set_Gamma_vert_rad
299 // _set_T_Local_to_Body
301 // _set_Sea_Level_Radius
303 // Externally set via the weather code:
304 // _set_Velocities_Local_Airmass
306 // _set_Static_pressure
307 // _set_Static_temperature
308 void YASim::copyFromYASim()
310 Airplane* airplane = _fdm->getAirplane();
311 Model* model = airplane->getModel();
312 State* s = model->getState();
315 double lat, lon, alt;
316 Glue::xyz2geod(s->pos, &lat, &lon, &alt);
317 _set_Geodetic_Position(lat, lon, alt*M2FT);
320 //_set_Geocentric_Position(Glue::geod2geocLat(lat), lon, alt*M2FT);
322 _set_Altitude_AGL(model->getAGL() * M2FT);
324 // useful conversion matrix
326 Glue::xyz2nedMat(lat, lon, xyz2ned);
330 Math::vmul33(xyz2ned, s->v, v);
331 _set_Velocities_Local(M2FT*v[0], M2FT*v[1], M2FT*v[2]);
332 _set_V_ground_speed(Math::sqrt(M2FT*v[0]*M2FT*v[0] +
333 M2FT*v[1]*M2FT*v[1]));
334 _set_Climb_Rate(-M2FT*v[2]);
336 // The HUD uses this, but inverts down (?!)
337 _set_Velocities_Ground(M2FT*v[0], M2FT*v[1], -M2FT*v[2]);
339 // _set_Geocentric_Rates(M2FT*v[0], M2FT*v[1], M2FT*v[2]); // UNUSED
341 Math::vmul33(s->orient, s->v, v);
342 _set_Velocities_Wind_Body(v[0]*M2FT, -v[1]*M2FT, -v[2]*M2FT);
343 _set_V_rel_wind(Math::mag3(v)*M2FT); // units?
345 // These don't work, use a dummy based on altitude
346 // float P = get_Static_pressure();
347 // float T = get_Static_temperature();
348 float P = Atmosphere::getStdPressure(alt);
349 float T = Atmosphere::getStdTemperature(alt);
350 _set_V_equiv_kts(Atmosphere::calcVEAS(v[0], P, T)*MPS2KTS);
351 _set_V_calibrated_kts(Atmosphere::calcVCAS(v[0], P, T)*MPS2KTS);
352 _set_Mach_number(Atmosphere::calcMach(v[0], T));
355 Math::vmul33(xyz2ned, s->acc, v);
356 _set_Accels_Local(M2FT*v[0], M2FT*v[1], M2FT*v[2]);
358 Math::vmul33(s->orient, s->acc, v);
359 _set_Accels_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]);
360 _set_Accels_CG_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]);
362 _fdm->getAirplane()->getPilotAccel(v);
363 _set_Accels_Pilot_Body(M2FT*v[0], -M2FT*v[1], -M2FT*v[2]);
365 // The one appears (!) to want inverted pilot acceleration
366 // numbers, in G's...
367 Math::mul3(1.0/9.8, v, v);
368 _set_Accels_CG_Body_N(v[0], -v[1], -v[2]);
372 Glue::calcAlphaBeta(s, &alpha, &beta);
377 Math::trans33(xyz2ned, tmp);
378 Math::mmul33(s->orient, tmp, tmp);
379 float roll, pitch, hdg;
380 Glue::orient2euler(tmp, &roll, &pitch, &hdg);
381 _set_Euler_Angles(roll, pitch, hdg);
384 Math::vmul33(s->orient, s->rot, v);
385 _set_Omega_Body(v[0], -v[1], -v[2]);
387 Glue::calcEulerRates(s, &roll, &pitch, &hdg);
388 _set_Euler_Rates(roll, pitch, hdg);
390 // Fill out our engine and gear objects
392 for(i=0; i<get_num_gear(); i++) {
393 FGGearInterface* fgg = get_gear_unit(i);
394 Gear* g = airplane->getGear(i);
395 if(g->getBrake() != 0)
397 if(g->getCompressFraction() != 0)
401 fgg->SetPosition(g->getExtension());
404 for(i=0; i<get_num_engines(); i++) {
405 FGEngInterface* fge = get_engine(i);
406 Thruster* t = model->getThruster(i);
408 fge->set_Running_Flag(true);
409 fge->set_Cranking_Flag(false);
411 // Note: assumes all tanks have the same fuel density!
412 fge->set_Fuel_Flow(CM2GALS * t->getFuelFlow()
413 / airplane->getFuelDensity(0));
417 fge->set_prop_thrust(Math::mag3(tmp) * KG2LBS / 9.8);
419 PropEngine* pe = t->getPropEngine();
421 fge->set_RPM(pe->getOmega() * RAD2RPM);
424 float power = Math::mag3(tmp) * pe->getOmega();
425 float maxPower = pe->getPistonEngine()->getPower();
427 fge->set_MaxHP(maxPower * W2HP);
428 fge->set_Percentage_Power(100 * power/maxPower);