1 // flight.cxx -- a general interface to the various flight models
3 // Written by Curtis Olson, started May 1997.
5 // Copyright (C) 1997 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <simgear/constants.h>
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/math/SGMath.hxx>
32 #include <simgear/timing/timestamp.hxx>
34 #include <Scenery/scenery.hxx>
35 #include <Main/globals.hxx>
36 #include <Main/fg_props.hxx>
37 #include <FDM/groundcache.hxx>
40 static inline void assign(double* ptr, const SGVec3d& vec)
48 FGInterface::FGInterface()
53 FGInterface::FGInterface( double dt )
59 FGInterface::~FGInterface() {
60 // unbind(); // FIXME: should be called explicitly
64 FGInterface::_calc_multiloop (double dt)
66 // Since some time the simulation time increments we get here are
67 // already a multiple of the basic update freqency.
68 // So, there is no need to do our own multiloop rounding with all bad
69 // roundoff problems when we already have nearly accurate values.
70 // Only the speedup thing must be still handled here
71 int hz = fgGetInt("/sim/model-hz");
72 int multiloop = SGMiscd::roundToInt(dt*hz);
73 int speedup = fgGetInt("/sim/speed-up");
74 return multiloop * speedup;
79 * Set default values for the state of the FDM.
81 * This method is invoked by the constructors.
84 FGInterface::_setup ()
89 d_cg_rp_body_v = SGVec3d::zeros();
90 v_dot_local_v = SGVec3d::zeros();
91 v_dot_body_v = SGVec3d::zeros();
92 a_cg_body_v = SGVec3d::zeros();
93 a_pilot_body_v = SGVec3d::zeros();
94 n_cg_body_v = SGVec3d::zeros();
95 v_local_v = SGVec3d::zeros();
96 v_local_rel_ground_v = SGVec3d::zeros();
97 v_local_airmass_v = SGVec3d::zeros();
98 v_wind_body_v = SGVec3d::zeros();
99 omega_body_v = SGVec3d::zeros();
100 euler_rates_v = SGVec3d::zeros();
101 geocentric_rates_v = SGVec3d::zeros();
102 geodetic_position_v = SGGeod::fromRadM(0, 0, 0);
103 cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
104 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
105 euler_angles_v = SGVec3d::zeros();
108 v_rel_wind=v_true_kts=0;
109 v_ground_speed=v_equiv_kts=0;
113 density=mach_number=0;
114 static_pressure=total_pressure=0;
116 static_temperature=total_temperature=0;
117 sea_level_radius=earth_position_angle=0;
125 FGInterface::init () {}
128 * Initialize the state of the FDM.
130 * Subclasses of FGInterface may do their own, additional initialization,
131 * but there is some that is common to all. Normally, they should call
132 * this before they begin their own init to make sure the basic structures
133 * are set up properly.
136 FGInterface::common_init ()
138 SG_LOG( SG_FLIGHT, SG_INFO, "Start common FDM init" );
142 ground_cache.set_cache_time_offset(globals->get_sim_time_sec());
144 // Set initial position
145 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing position..." );
146 double lon = fgGetDouble("/sim/presets/longitude-deg")
147 * SGD_DEGREES_TO_RADIANS;
148 double lat = fgGetDouble("/sim/presets/latitude-deg")
149 * SGD_DEGREES_TO_RADIANS;
150 double alt_ft = fgGetDouble("/sim/presets/altitude-ft");
151 double alt_m = alt_ft * SG_FEET_TO_METER;
152 set_Longitude( lon );
154 SG_LOG( SG_FLIGHT, SG_INFO, "Checking for lon = "
155 << lon*SGD_RADIANS_TO_DEGREES << "deg, lat = "
156 << lat*SGD_RADIANS_TO_DEGREES << "deg, alt = "
159 double ground_elev_m = get_groundlevel_m(lat, lon, alt_m);
160 double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET;
161 _set_Runway_altitude ( ground_elev_ft );
162 if ( fgGetBool("/sim/presets/onground") || alt_ft < ground_elev_ft ) {
163 fgSetDouble("/position/altitude-ft", ground_elev_ft + 0.1);
164 set_Altitude( ground_elev_ft + 0.1);
166 set_Altitude( alt_ft );
169 // Set ground elevation
170 SG_LOG( SG_FLIGHT, SG_INFO,
171 "...initializing ground elevation to " << ground_elev_ft
174 // Set sea-level radius
175 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing sea-level radius..." );
176 SG_LOG( SG_FLIGHT, SG_INFO, " lat = "
177 << fgGetDouble("/sim/presets/latitude-deg")
178 << " alt = " << get_Altitude() );
179 double slr = SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v);
180 _set_Sea_level_radius( slr * SG_METER_TO_FEET );
182 // Set initial velocities
183 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing velocities..." );
184 if ( !fgHasNode("/sim/presets/speed-set") ) {
185 set_V_calibrated_kts(0.0);
187 const string speedset = fgGetString("/sim/presets/speed-set");
188 if ( speedset == "knots" || speedset == "KNOTS" ) {
189 set_V_calibrated_kts( fgGetDouble("/sim/presets/airspeed-kt") );
190 } else if ( speedset == "mach" || speedset == "MACH" ) {
191 set_Mach_number( fgGetDouble("/sim/presets/mach") );
192 } else if ( speedset == "UVW" || speedset == "uvw" ) {
193 set_Velocities_Wind_Body(
194 fgGetDouble("/sim/presets/uBody-fps"),
195 fgGetDouble("/sim/presets/vBody-fps"),
196 fgGetDouble("/sim/presets/wBody-fps") );
197 } else if ( speedset == "NED" || speedset == "ned" ) {
198 set_Velocities_Local(
199 fgGetDouble("/sim/presets/speed-north-fps"),
200 fgGetDouble("/sim/presets/speed-east-fps"),
201 fgGetDouble("/sim/presets/speed-down-fps") );
203 SG_LOG( SG_FLIGHT, SG_ALERT,
204 "Unrecognized value for /sim/presets/speed-set: "
206 set_V_calibrated_kts( 0.0 );
210 // Set initial Euler angles
211 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing Euler angles..." );
212 set_Euler_Angles( fgGetDouble("/sim/presets/roll-deg")
213 * SGD_DEGREES_TO_RADIANS,
214 fgGetDouble("/sim/presets/pitch-deg")
215 * SGD_DEGREES_TO_RADIANS,
216 fgGetDouble("/sim/presets/heading-deg")
217 * SGD_DEGREES_TO_RADIANS );
219 SG_LOG( SG_FLIGHT, SG_INFO, "End common FDM init" );
224 * Bind getters and setters to properties.
226 * The bind() method will be invoked after init(). Note that unlike
227 * the usual implementations of FGSubsystem::bind(), this method does
228 * not automatically pick up existing values for the properties at
229 * bind time; instead, all values are set explicitly in the init()
238 fgTie("/position/latitude-deg", this,
239 &FGInterface::get_Latitude_deg,
240 &FGInterface::set_Latitude_deg,
242 fgSetArchivable("/position/latitude-deg");
243 fgTie("/position/longitude-deg", this,
244 &FGInterface::get_Longitude_deg,
245 &FGInterface::set_Longitude_deg,
247 fgSetArchivable("/position/longitude-deg");
248 fgTie("/position/altitude-ft", this,
249 &FGInterface::get_Altitude,
250 &FGInterface::set_Altitude,
252 fgSetArchivable("/position/altitude-ft");
253 fgTie("/position/altitude-agl-ft", this,
254 &FGInterface::get_Altitude_AGL, &FGInterface::set_AltitudeAGL);
255 fgSetArchivable("/position/ground-elev-ft");
256 fgTie("/position/ground-elev-ft", this,
257 &FGInterface::get_Runway_altitude); // read-only
258 fgSetArchivable("/position/ground-elev-m");
259 fgTie("/position/ground-elev-m", this,
260 &FGInterface::get_Runway_altitude_m); // read-only
261 fgTie("/environment/ground-elevation-m", this,
262 &FGInterface::get_Runway_altitude_m); // read-only
263 fgSetArchivable("/position/sea-level-radius-ft");
264 fgTie("/position/sea-level-radius-ft", this,
265 &FGInterface::get_Sea_level_radius,
266 &FGInterface::_set_Sea_level_radius);
269 fgTie("/orientation/roll-deg", this,
270 &FGInterface::get_Phi_deg,
271 &FGInterface::set_Phi_deg);
272 fgSetArchivable("/orientation/roll-deg");
273 fgTie("/orientation/pitch-deg", this,
274 &FGInterface::get_Theta_deg,
275 &FGInterface::set_Theta_deg);
276 fgSetArchivable("/orientation/pitch-deg");
277 fgTie("/orientation/heading-deg", this,
278 &FGInterface::get_Psi_deg,
279 &FGInterface::set_Psi_deg);
280 fgSetArchivable("/orientation/heading-deg");
281 fgTie("/orientation/track-deg", this,
282 &FGInterface::get_Track);
284 // Body-axis "euler rates" (rotation speed, but in a funny
286 fgTie("/orientation/roll-rate-degps", this,
287 &FGInterface::get_Phi_dot_degps, &FGInterface::set_Phi_dot_degps);
288 fgTie("/orientation/pitch-rate-degps", this,
289 &FGInterface::get_Theta_dot_degps, &FGInterface::set_Theta_dot_degps);
290 fgTie("/orientation/yaw-rate-degps", this,
291 &FGInterface::get_Psi_dot_degps, &FGInterface::set_Psi_dot_degps);
293 fgTie("/orientation/p-body", this, &FGInterface::get_P_body);
294 fgTie("/orientation/q-body", this, &FGInterface::get_Q_body);
295 fgTie("/orientation/r-body", this, &FGInterface::get_R_body);
297 // Ground speed knots
298 fgTie("/velocities/groundspeed-kt", this,
299 &FGInterface::get_V_ground_speed_kt);
301 // Calibrated airspeed
302 fgTie("/velocities/airspeed-kt", this,
303 &FGInterface::get_V_calibrated_kts,
304 &FGInterface::set_V_calibrated_kts,
307 fgTie("/velocities/equivalent-kt", this,
308 &FGInterface::get_V_equiv_kts);
311 fgTie("/velocities/mach", this,
312 &FGInterface::get_Mach_number,
313 &FGInterface::set_Mach_number,
317 // fgTie("/velocities/speed-north-fps", this,
318 // &FGInterface::get_V_north,
319 // &FGInterface::set_V_north);
320 // fgSetArchivable("/velocities/speed-north-fps");
321 // fgTie("/velocities/speed-east-fps", this,
322 // &FGInterface::get_V_east,
323 // &FGInterface::set_V_east);
324 // fgSetArchivable("/velocities/speed-east-fps");
325 // fgTie("/velocities/speed-down-fps", this,
326 // &FGInterface::get_V_down,
327 // &FGInterface::set_V_down);
328 // fgSetArchivable("/velocities/speed-down-fps");
329 // FIXME: Temporarily read-only, until the
330 // incompatibilities between JSBSim and
331 // LaRCSim are fixed (LaRCSim adds the
332 // earth's rotation to the east velocity).
333 fgTie("/velocities/speed-north-fps", this,
334 &FGInterface::get_V_north, &FGInterface::set_V_north);
335 fgTie("/velocities/speed-east-fps", this,
336 &FGInterface::get_V_east, &FGInterface::set_V_east);
337 fgTie("/velocities/speed-down-fps", this,
338 &FGInterface::get_V_down, &FGInterface::set_V_down);
340 fgTie("/velocities/north-relground-fps", this,
341 &FGInterface::get_V_north_rel_ground);
342 fgTie("/velocities/east-relground-fps", this,
343 &FGInterface::get_V_east_rel_ground);
344 fgTie("/velocities/down-relground-fps", this,
345 &FGInterface::get_V_down_rel_ground);
349 // FIXME: temporarily archivable, until
350 // the NED problem is fixed.
351 fgTie("/velocities/uBody-fps", this,
352 &FGInterface::get_uBody,
353 &FGInterface::set_uBody,
355 fgSetArchivable("/velocities/uBody-fps");
356 fgTie("/velocities/vBody-fps", this,
357 &FGInterface::get_vBody,
358 &FGInterface::set_vBody,
360 fgSetArchivable("/velocities/vBody-fps");
361 fgTie("/velocities/wBody-fps", this,
362 &FGInterface::get_wBody,
363 &FGInterface::set_wBody,
365 fgSetArchivable("/velocities/wBody-fps");
367 // Climb and slip (read-only)
368 fgTie("/velocities/vertical-speed-fps", this,
369 &FGInterface::get_Climb_Rate,
370 &FGInterface::set_Climb_Rate );
371 fgTie("/velocities/glideslope", this,
372 &FGInterface::get_Gamma_vert_rad,
373 &FGInterface::set_Gamma_vert_rad );
374 fgTie("/orientation/side-slip-rad", this,
375 &FGInterface::get_Beta, &FGInterface::_set_Beta);
376 fgTie("/orientation/side-slip-deg", this,
377 &FGInterface::get_Beta_deg); // read-only
378 fgTie("/orientation/alpha-deg", this,
379 &FGInterface::get_Alpha_deg, &FGInterface::set_Alpha_deg); // read-only
380 fgTie("/accelerations/nlf", this,
381 &FGInterface::get_Nlf); // read-only
384 fgTie("/accelerations/ned/north-accel-fps_sec",
385 this, &FGInterface::get_V_dot_north);
386 fgTie("/accelerations/ned/east-accel-fps_sec",
387 this, &FGInterface::get_V_dot_east);
388 fgTie("/accelerations/ned/down-accel-fps_sec",
389 this, &FGInterface::get_V_dot_down);
391 // Pilot accelerations
392 fgTie("/accelerations/pilot/x-accel-fps_sec",
393 this, &FGInterface::get_A_X_pilot, &FGInterface::set_A_X_pilot);
394 fgTie("/accelerations/pilot/y-accel-fps_sec",
395 this, &FGInterface::get_A_Y_pilot, &FGInterface::set_A_Y_pilot);
396 fgTie("/accelerations/pilot/z-accel-fps_sec",
397 this, &FGInterface::get_A_Z_pilot, &FGInterface::set_A_Z_pilot);
399 fgTie("/accelerations/n-z-cg-fps_sec", this, &FGInterface::get_N_Z_cg);
405 * Unbind any properties bound to this FDM.
407 * This method allows the FDM to release properties so that a new
408 * FDM can bind them instead.
411 FGInterface::unbind ()
415 fgUntie("/position/latitude-deg");
416 fgUntie("/position/longitude-deg");
417 fgUntie("/position/altitude-ft");
418 fgUntie("/position/altitude-agl-ft");
419 fgUntie("/position/ground-elev-ft");
420 fgUntie("/position/ground-elev-m");
421 fgUntie("/environment/ground-elevation-m");
422 fgUntie("/position/sea-level-radius-ft");
423 fgUntie("/orientation/roll-deg");
424 fgUntie("/orientation/pitch-deg");
425 fgUntie("/orientation/heading-deg");
426 fgUntie("/orientation/track-deg");
427 fgUntie("/orientation/roll-rate-degps");
428 fgUntie("/orientation/pitch-rate-degps");
429 fgUntie("/orientation/yaw-rate-degps");
430 fgUntie("/orientation/p-body");
431 fgUntie("/orientation/q-body");
432 fgUntie("/orientation/r-body");
433 fgUntie("/orientation/side-slip-rad");
434 fgUntie("/orientation/side-slip-deg");
435 fgUntie("/orientation/alpha-deg");
436 fgUntie("/velocities/airspeed-kt");
437 fgUntie("/velocities/groundspeed-kt");
438 fgUntie("/velocities/equivalent-kt");
439 fgUntie("/velocities/mach");
440 fgUntie("/velocities/speed-north-fps");
441 fgUntie("/velocities/speed-east-fps");
442 fgUntie("/velocities/speed-down-fps");
443 fgUntie("/velocities/north-relground-fps");
444 fgUntie("/velocities/east-relground-fps");
445 fgUntie("/velocities/down-relground-fps");
446 fgUntie("/velocities/uBody-fps");
447 fgUntie("/velocities/vBody-fps");
448 fgUntie("/velocities/wBody-fps");
449 fgUntie("/velocities/vertical-speed-fps");
450 fgUntie("/velocities/glideslope");
451 fgUntie("/accelerations/nlf");
452 fgUntie("/accelerations/pilot/x-accel-fps_sec");
453 fgUntie("/accelerations/pilot/y-accel-fps_sec");
454 fgUntie("/accelerations/pilot/z-accel-fps_sec");
455 fgUntie("/accelerations/ned/north-accel-fps_sec");
456 fgUntie("/accelerations/ned/east-accel-fps_sec");
457 fgUntie("/accelerations/ned/down-accel-fps_sec");
458 fgUntie("/accelerations/n-z-cg-fps_sec");
462 * Update the state of the FDM (i.e. run the equations of motion).
465 FGInterface::update (double dt)
467 SG_LOG(SG_FLIGHT, SG_ALERT, "dummy update() ... SHOULDN'T BE CALLED!");
471 void FGInterface::_updatePositionM(const SGVec3d& cartPos)
473 TrackComputer tracker( track, geodetic_position_v );
474 cartesian_position_v = cartPos;
475 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
476 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
477 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
478 _update_ground_elev_at_pos();
482 void FGInterface::_updatePosition(const SGGeod& geod)
484 TrackComputer tracker( track, geodetic_position_v );
485 geodetic_position_v = geod;
486 cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
487 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
489 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
490 _update_ground_elev_at_pos();
494 void FGInterface::_updatePosition(const SGGeoc& geoc)
496 TrackComputer tracker( track, geodetic_position_v );
497 geocentric_position_v = geoc;
498 cartesian_position_v = SGVec3d::fromGeoc(geocentric_position_v);
499 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
501 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
502 _update_ground_elev_at_pos();
506 void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
508 _updatePosition(SGGeod::fromRadFt(lon, lat, alt));
512 void FGInterface::_updateGeocentricPosition( double lat, double lon,
515 _updatePosition(SGGeoc::fromRadFt(lon, lat, get_Sea_level_radius() + alt));
518 void FGInterface::_update_ground_elev_at_pos( void ) {
519 double groundlevel_m = get_groundlevel_m(geodetic_position_v);
520 _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
524 void FGInterface::set_Latitude(double lat) {
525 geodetic_position_v.setLatitudeRad(lat);
528 void FGInterface::set_Longitude(double lon) {
529 geodetic_position_v.setLongitudeRad(lon);
532 void FGInterface::set_Altitude(double alt) {
533 geodetic_position_v.setElevationFt(alt);
536 void FGInterface::set_AltitudeAGL(double altagl) {
541 void FGInterface::set_V_calibrated_kts(double vc) {
542 v_calibrated_kts = vc;
545 void FGInterface::set_Mach_number(double mach) {
549 void FGInterface::set_Velocities_Local( double north,
552 v_local_v[0] = north;
557 void FGInterface::set_Velocities_Wind_Body( double u,
560 v_wind_body_v[0] = u;
561 v_wind_body_v[1] = v;
562 v_wind_body_v[2] = w;
566 void FGInterface::set_Euler_Angles( double phi,
569 euler_angles_v[0] = phi;
570 euler_angles_v[1] = theta;
571 euler_angles_v[2] = psi;
575 void FGInterface::set_Climb_Rate( double roc) {
579 void FGInterface::set_Gamma_vert_rad( double gamma) {
580 gamma_vert_rad = gamma;
583 void FGInterface::set_Static_pressure(double p) { static_pressure = p; }
584 void FGInterface::set_Static_temperature(double T) { static_temperature = T; }
585 void FGInterface::set_Density(double rho) { density = rho; }
587 void FGInterface::set_Velocities_Local_Airmass (double wnorth,
590 v_local_airmass_v[0] = wnorth;
591 v_local_airmass_v[1] = weast;
592 v_local_airmass_v[2] = wdown;
596 void FGInterface::_busdump(void) {
598 SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rp_body_v: " << d_cg_rp_body_v);
599 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_local_v: " << v_dot_local_v);
600 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_body_v: " << v_dot_body_v);
601 SG_LOG(SG_FLIGHT,SG_INFO,"a_cg_body_v: " << a_cg_body_v);
602 SG_LOG(SG_FLIGHT,SG_INFO,"a_pilot_body_v: " << a_pilot_body_v);
603 SG_LOG(SG_FLIGHT,SG_INFO,"n_cg_body_v: " << n_cg_body_v);
604 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v: " << v_local_v);
605 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_rel_ground_v: " << v_local_rel_ground_v);
606 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_airmass_v: " << v_local_airmass_v);
607 SG_LOG(SG_FLIGHT,SG_INFO,"v_wind_body_v: " << v_wind_body_v);
608 SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v: " << omega_body_v);
609 SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v: " << euler_rates_v);
610 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v: " << geocentric_rates_v);
611 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v: " << geocentric_position_v);
612 SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v: " << geodetic_position_v);
613 SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v: " << euler_angles_v);
615 SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << nlf );
616 SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << v_rel_wind );
617 SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << v_true_kts );
618 SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << v_ground_speed );
619 SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << v_equiv_kts );
620 SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << v_calibrated_kts );
621 SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << alpha );
622 SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << beta );
623 SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << gamma_vert_rad );
624 SG_LOG(SG_FLIGHT,SG_INFO,"density: " << density );
625 SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << mach_number );
626 SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << static_pressure );
627 SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << total_pressure );
628 SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << dynamic_pressure );
629 SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << static_temperature );
630 SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << total_temperature );
631 SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << sea_level_radius );
632 SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << earth_position_angle );
633 SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << runway_altitude );
634 SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << climb_rate );
635 SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << altitude_agl );
639 FGInterface::prepare_ground_cache_m(double startSimTime, double endSimTime,
640 const double pt[3], double rad)
642 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
647 FGInterface::prepare_ground_cache_ft(double startSimTime, double endSimTime,
648 const double pt[3], double rad)
650 // Convert units and do the real work.
651 SGVec3d pt_ft = SG_FEET_TO_METER*SGVec3d(pt);
652 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
653 pt_ft, rad*SG_FEET_TO_METER);
657 FGInterface::is_valid_m(double *ref_time, double pt[3], double *rad)
660 bool valid = ground_cache.is_valid(*ref_time, _pt, *rad);
665 bool FGInterface::is_valid_ft(double *ref_time, double pt[3], double *rad)
667 // Convert units and do the real work.
669 bool found_ground = ground_cache.is_valid(*ref_time, _pt, *rad);
670 assign(pt, SG_METER_TO_FEET*_pt);
671 *rad *= SG_METER_TO_FEET;
676 FGInterface::get_cat_m(double t, const double pt[3],
677 double end[2][3], double vel[2][3])
679 SGVec3d _end[2], _vel[2];
680 double dist = ground_cache.get_cat(t, SGVec3d(pt), _end, _vel);
681 for (int k=0; k<2; ++k) {
682 assign( end[k], _end[k] );
683 assign( vel[k], _vel[k] );
689 FGInterface::get_cat_ft(double t, const double pt[3],
690 double end[2][3], double vel[2][3])
692 // Convert units and do the real work.
693 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
694 SGVec3d _end[2], _vel[2];
695 double dist = ground_cache.get_cat(t, pt_m, _end, _vel);
696 for (int k=0; k<2; ++k) {
697 assign( end[k], SG_METER_TO_FEET*_end[k] );
698 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
700 return dist*SG_METER_TO_FEET;
704 FGInterface::get_body_m(double t, simgear::BVHNode::Id id,
705 double bodyToWorld[16], double linearVel[3],
706 double angularVel[3])
708 SGMatrixd _bodyToWorld;
709 SGVec3d _linearVel, _angularVel;
710 if (!ground_cache.get_body(t, _bodyToWorld, _linearVel, _angularVel, id))
713 assign(linearVel, _linearVel);
714 assign(angularVel, _angularVel);
715 for (unsigned i = 0; i < 16; ++i)
716 bodyToWorld[i] = _bodyToWorld.data()[i];
722 FGInterface::get_agl_m(double t, const double pt[3], double max_altoff,
723 double contact[3], double normal[3],
724 double linearVel[3], double angularVel[3],
725 SGMaterial const*& material, simgear::BVHNode::Id& id)
727 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
728 SGVec3d _contact, _normal, _linearVel, _angularVel;
730 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
731 _angularVel, id, material);
732 // correct the linear velocity, since the line intersector delivers
733 // values for the start point and the get_agl function should
734 // traditionally deliver for the contact point
735 _linearVel += cross(_angularVel, _contact - pt_m);
737 assign(contact, _contact);
738 assign(normal, _normal);
739 assign(linearVel, _linearVel);
740 assign(angularVel, _angularVel);
745 FGInterface::get_agl_ft(double t, const double pt[3], double max_altoff,
746 double contact[3], double normal[3],
747 double linearVel[3], double angularVel[3],
748 SGMaterial const*& material, simgear::BVHNode::Id& id)
750 // Convert units and do the real work.
751 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
752 pt_m *= SG_FEET_TO_METER;
753 SGVec3d _contact, _normal, _linearVel, _angularVel;
755 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
756 _angularVel, id, material);
757 // correct the linear velocity, since the line intersector delivers
758 // values for the start point and the get_agl function should
759 // traditionally deliver for the contact point
760 _linearVel += cross(_angularVel, _contact - pt_m);
762 // Convert units back ...
763 assign( contact, SG_METER_TO_FEET*_contact );
764 assign( normal, _normal );
765 assign( linearVel, SG_METER_TO_FEET*_linearVel );
766 assign( angularVel, _angularVel );
771 FGInterface::get_nearest_m(double t, const double pt[3], double maxDist,
772 double contact[3], double normal[3],
773 double linearVel[3], double angularVel[3],
774 SGMaterial const*& material,
775 simgear::BVHNode::Id& id)
777 SGVec3d _contact, _linearVel, _angularVel;
778 if (!ground_cache.get_nearest(t, SGVec3d(pt), maxDist, _contact, _linearVel,
779 _angularVel, id, material))
782 assign(contact, _contact);
783 assign(linearVel, _linearVel);
784 assign(angularVel, _angularVel);
789 FGInterface::get_nearest_ft(double t, const double pt[3], double maxDist,
790 double contact[3], double normal[3],
791 double linearVel[3], double angularVel[3],
792 SGMaterial const*& material,
793 simgear::BVHNode::Id& id)
795 SGVec3d _contact, _linearVel, _angularVel;
796 if (!ground_cache.get_nearest(t, SG_FEET_TO_METER*SGVec3d(pt),
797 SG_FEET_TO_METER*maxDist, _contact, _linearVel,
798 _angularVel, id, material))
801 assign(contact, SG_METER_TO_FEET*_contact);
802 assign(linearVel, SG_METER_TO_FEET*_linearVel);
803 assign(angularVel, _angularVel);
808 FGInterface::get_groundlevel_m(double lat, double lon, double alt)
810 return get_groundlevel_m(SGGeod::fromRadM(lon, lat, alt));
814 FGInterface::get_groundlevel_m(const SGGeod& geod)
816 // Compute the cartesian position of the given lat/lon/alt.
817 SGVec3d pos = SGVec3d::fromGeod(geod);
819 // FIXME: how to handle t - ref_time differences ???
821 double ref_time = 0, radius;
822 // Prepare the ground cache for that position.
823 if (!is_valid_m(&ref_time, cpos.data(), &radius)) {
824 double startTime = ref_time;
825 double endTime = startTime + 1;
826 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), 10);
827 /// This is most likely the case when the given altitude is
828 /// too low, try with a new altitude of 10000m, that should be
829 /// sufficient to find a ground level below everywhere on our planet
831 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
832 /// If there is still no ground, return sea level radius
833 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), 10))
836 } else if (radius*radius <= distSqr(pos, cpos)) {
837 double startTime = ref_time;
838 double endTime = startTime + 1;
840 /// We reuse the old radius value, but only if it is at least 10 Meters ..
841 if (!(10 < radius)) // Well this strange compare is nan safe
844 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), radius);
845 /// This is most likely the case when the given altitude is
846 /// too low, try with a new altitude of 10000m, that should be
847 /// sufficient to find a ground level below everywhere on our planet
849 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
850 /// If there is still no ground, return sea level radius
851 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), radius))
856 double contact[3], normal[3], vel[3], angvel[3];
857 const SGMaterial* material;
858 simgear::BVHNode::Id id;
859 // Ignore the return value here, since it just tells us if
860 // the returns stem from the groundcache or from the coarse
861 // computations below the groundcache. The contact point is still something
862 // valid, the normals and the other returns just contain some defaults.
863 get_agl_m(ref_time, pos.data(), 2.0, contact, normal, vel, angvel,
865 return SGGeod::fromCart(SGVec3d(contact)).getElevationM();
869 FGInterface::caught_wire_m(double t, const double pt[4][3])
872 for (int i=0; i<4; ++i)
873 pt_m[i] = SGVec3d(pt[i]);
875 return ground_cache.caught_wire(t, pt_m);
879 FGInterface::caught_wire_ft(double t, const double pt[4][3])
881 // Convert units and do the real work.
883 for (int i=0; i<4; ++i)
884 pt_m[i] = SG_FEET_TO_METER*SGVec3d(pt[i]);
886 return ground_cache.caught_wire(t, pt_m);
890 FGInterface::get_wire_ends_m(double t, double end[2][3], double vel[2][3])
892 SGVec3d _end[2], _vel[2];
893 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
894 for (int k=0; k<2; ++k) {
895 assign( end[k], _end[k] );
896 assign( vel[k], _vel[k] );
902 FGInterface::get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
904 // Convert units and do the real work.
905 SGVec3d _end[2], _vel[2];
906 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
907 for (int k=0; k<2; ++k) {
908 assign( end[k], SG_METER_TO_FEET*_end[k] );
909 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
915 FGInterface::release_wire(void)
917 ground_cache.release_wire();