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/timing/timestamp.hxx>
32 #include <simgear/scene/material/mat.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 frequency.
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 double speedup = fgGetDouble("/sim/speed-up");
73 double loops = dt * hz * speedup + delta_loops;
74 int iloops = SGMiscd::roundToInt(loops);
75 delta_loops = loops-iloops; // delta_loops required for speed-ups < 1 (to do one iteration every n-th step)
81 * Set default values for the state of the FDM.
83 * This method is invoked by the constructors.
86 FGInterface::_setup ()
91 d_cg_rp_body_v = SGVec3d::zeros();
92 v_dot_local_v = SGVec3d::zeros();
93 v_dot_body_v = SGVec3d::zeros();
94 a_cg_body_v = SGVec3d::zeros();
95 a_pilot_body_v = SGVec3d::zeros();
96 n_cg_body_v = SGVec3d::zeros();
97 v_local_v = SGVec3d::zeros();
98 v_local_rel_ground_v = SGVec3d::zeros();
99 v_local_airmass_v = SGVec3d::zeros();
100 v_wind_body_v = SGVec3d::zeros();
101 omega_body_v = SGVec3d::zeros();
102 euler_rates_v = SGVec3d::zeros();
103 geocentric_rates_v = SGVec3d::zeros();
104 geodetic_position_v = SGGeod::fromRadM(0, 0, 0);
105 cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
106 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
107 euler_angles_v = SGVec3d::zeros();
110 v_rel_wind=v_true_kts=0;
111 v_ground_speed=v_equiv_kts=0;
115 density=mach_number=0;
116 static_pressure=total_pressure=0;
118 static_temperature=total_temperature=0;
119 sea_level_radius=earth_position_angle=0;
128 FGInterface::init () {}
131 * Initialize the state of the FDM.
133 * Subclasses of FGInterface may do their own, additional initialization,
134 * but there is some that is common to all. Normally, they should call
135 * this before they begin their own init to make sure the basic structures
136 * are set up properly.
139 FGInterface::common_init ()
141 SG_LOG( SG_FLIGHT, SG_INFO, "Start common FDM init" );
145 ground_cache.set_cache_time_offset(globals->get_sim_time_sec());
147 // Set initial position
148 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing position..." );
149 double lon = fgGetDouble("/sim/presets/longitude-deg")
150 * SGD_DEGREES_TO_RADIANS;
151 double lat = fgGetDouble("/sim/presets/latitude-deg")
152 * SGD_DEGREES_TO_RADIANS;
153 double alt_ft = fgGetDouble("/sim/presets/altitude-ft");
154 double alt_m = alt_ft * SG_FEET_TO_METER;
155 set_Longitude( lon );
157 SG_LOG( SG_FLIGHT, SG_INFO, "Checking for lon = "
158 << lon*SGD_RADIANS_TO_DEGREES << "deg, lat = "
159 << lat*SGD_RADIANS_TO_DEGREES << "deg, alt = "
162 double ground_elev_m = get_groundlevel_m(lat, lon, alt_m);
163 double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET;
164 _set_Runway_altitude ( ground_elev_ft );
166 // Set aircraft altitude
167 if ( fgGetBool("/sim/presets/onground") || alt_ft < ground_elev_ft ) {
168 fgSetDouble("/position/altitude-ft", ground_elev_ft + 0.1);
169 set_Altitude( ground_elev_ft + 0.1);
171 set_Altitude( alt_ft );
174 // Set ground elevation
175 SG_LOG( SG_FLIGHT, SG_INFO,
176 "...initializing ground elevation to " << ground_elev_ft
179 // Set sea-level radius
180 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing sea-level radius..." );
181 SG_LOG( SG_FLIGHT, SG_INFO, " lat = "
182 << fgGetDouble("/sim/presets/latitude-deg")
183 << " alt = " << get_Altitude() );
184 double slr = SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v);
185 _set_Sea_level_radius( slr * SG_METER_TO_FEET );
187 // Set initial Euler angles
188 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing Euler angles..." );
189 set_Euler_Angles( fgGetDouble("/sim/presets/roll-deg")
190 * SGD_DEGREES_TO_RADIANS,
191 fgGetDouble("/sim/presets/pitch-deg")
192 * SGD_DEGREES_TO_RADIANS,
193 fgGetDouble("/sim/presets/heading-deg")
194 * SGD_DEGREES_TO_RADIANS );
196 // Set initial velocities
197 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing velocities..." );
198 if ( !fgHasNode("/sim/presets/speed-set") ) {
199 set_V_calibrated_kts(0.0);
201 const string speedset = fgGetString("/sim/presets/speed-set");
202 if ( speedset == "knots" || speedset == "KNOTS" ) {
203 set_V_calibrated_kts( fgGetDouble("/sim/presets/airspeed-kt") );
204 } else if ( speedset == "mach" || speedset == "MACH" ) {
205 set_Mach_number( fgGetDouble("/sim/presets/mach") );
206 } else if ( speedset == "UVW" || speedset == "uvw" ) {
207 set_Velocities_Wind_Body(
208 fgGetDouble("/sim/presets/uBody-fps"),
209 fgGetDouble("/sim/presets/vBody-fps"),
210 fgGetDouble("/sim/presets/wBody-fps") );
211 } else if ( speedset == "NED" || speedset == "ned" ) {
212 set_Velocities_Local(
213 fgGetDouble("/sim/presets/speed-north-fps"),
214 fgGetDouble("/sim/presets/speed-east-fps"),
215 fgGetDouble("/sim/presets/speed-down-fps") );
217 SG_LOG( SG_FLIGHT, SG_ALERT,
218 "Unrecognized value for /sim/presets/speed-set: "
220 set_V_calibrated_kts( 0.0 );
224 if ( fgHasNode("/sim/presets/glideslope-deg") )
225 set_Gamma_vert_rad( fgGetDouble("/sim/presets/glideslope-deg")
226 * SGD_DEGREES_TO_RADIANS );
227 else if ( fgHasNode("/sim/presets/speed-set") &&
228 fgHasNode( "/sim/presets/vertical-speed-fps") )
230 set_Climb_Rate( fgGetDouble("/sim/presets/vertical-speed-fps") );
233 SG_LOG( SG_FLIGHT, SG_INFO, "End common FDM init" );
238 * Bind getters and setters to properties.
240 * The bind() method will be invoked after init(). Note that unlike
241 * the usual implementations of FGSubsystem::bind(), this method does
242 * not automatically pick up existing values for the properties at
243 * bind time; instead, all values are set explicitly in the init()
251 _tiedProperties.setRoot(globals->get_props());
253 _tiedProperties.Tie("/position/latitude-deg", this,
254 &FGInterface::get_Latitude_deg,
255 &FGInterface::set_Latitude_deg,
257 fgSetArchivable("/position/latitude-deg");
258 _tiedProperties.Tie("/position/longitude-deg", this,
259 &FGInterface::get_Longitude_deg,
260 &FGInterface::set_Longitude_deg,
262 fgSetArchivable("/position/longitude-deg");
263 _tiedProperties.Tie("/position/altitude-ft", this,
264 &FGInterface::get_Altitude,
265 &FGInterface::set_Altitude,
267 fgSetArchivable("/position/altitude-ft");
268 _tiedProperties.Tie("/position/altitude-agl-ft", this,
269 &FGInterface::get_Altitude_AGL, &FGInterface::set_AltitudeAGL, false);
270 fgSetArchivable("/position/ground-elev-ft");
271 _tiedProperties.Tie("/position/ground-elev-ft", this,
272 &FGInterface::get_Runway_altitude); // read-only
273 fgSetArchivable("/position/ground-elev-m");
274 _tiedProperties.Tie("/position/ground-elev-m", this,
275 &FGInterface::get_Runway_altitude_m); // read-only
276 _tiedProperties.Tie("/environment/ground-elevation-m", this,
277 &FGInterface::get_Runway_altitude_m); // read-only
278 fgSetArchivable("/position/sea-level-radius-ft");
279 _tiedProperties.Tie("/position/sea-level-radius-ft", this,
280 &FGInterface::get_Sea_level_radius,
281 &FGInterface::_set_Sea_level_radius, false);
284 _tiedProperties.Tie("/orientation/roll-deg", this,
285 &FGInterface::get_Phi_deg,
286 &FGInterface::set_Phi_deg, false);
287 fgSetArchivable("/orientation/roll-deg");
288 _tiedProperties.Tie("/orientation/pitch-deg", this,
289 &FGInterface::get_Theta_deg,
290 &FGInterface::set_Theta_deg, false);
291 fgSetArchivable("/orientation/pitch-deg");
292 _tiedProperties.Tie("/orientation/heading-deg", this,
293 &FGInterface::get_Psi_deg,
294 &FGInterface::set_Psi_deg, false);
295 fgSetArchivable("/orientation/heading-deg");
296 _tiedProperties.Tie("/orientation/track-deg", this,
297 &FGInterface::get_Track); // read-only
299 // Body-axis "euler rates" (rotation speed, but in a funny
301 _tiedProperties.Tie("/orientation/roll-rate-degps", this,
302 &FGInterface::get_Phi_dot_degps,
303 &FGInterface::set_Phi_dot_degps, false);
304 _tiedProperties.Tie("/orientation/pitch-rate-degps", this,
305 &FGInterface::get_Theta_dot_degps,
306 &FGInterface::set_Theta_dot_degps, false);
307 _tiedProperties.Tie("/orientation/yaw-rate-degps", this,
308 &FGInterface::get_Psi_dot_degps,
309 &FGInterface::set_Psi_dot_degps, false);
311 _tiedProperties.Tie("/orientation/p-body", this, &FGInterface::get_P_body); // read-only
312 _tiedProperties.Tie("/orientation/q-body", this, &FGInterface::get_Q_body); // read-only
313 _tiedProperties.Tie("/orientation/r-body", this, &FGInterface::get_R_body); // read-only
315 // Ground speed knots
316 _tiedProperties.Tie("/velocities/groundspeed-kt", this,
317 &FGInterface::get_V_ground_speed_kt,
318 &FGInterface::set_V_ground_speed_kt); // read-only
320 // Calibrated airspeed
321 _tiedProperties.Tie("/velocities/airspeed-kt", this,
322 &FGInterface::get_V_calibrated_kts,
323 &FGInterface::set_V_calibrated_kts,
326 _tiedProperties.Tie("/velocities/equivalent-kt", this,
327 &FGInterface::get_V_equiv_kts); // read-only
330 _tiedProperties.Tie("/velocities/mach", this,
331 &FGInterface::get_Mach_number,
332 &FGInterface::set_Mach_number,
336 // _tiedProperties.Tie("/velocities/speed-north-fps", this,
337 // &FGInterface::get_V_north,
338 // &FGInterface::set_V_north);
339 // fgSetArchivable("/velocities/speed-north-fps");
340 // _tiedProperties.Tie("/velocities/speed-east-fps", this,
341 // &FGInterface::get_V_east,
342 // &FGInterface::set_V_east);
343 // fgSetArchivable("/velocities/speed-east-fps");
344 // _tiedProperties.Tie("/velocities/speed-down-fps", this,
345 // &FGInterface::get_V_down,
346 // &FGInterface::set_V_down);
347 // fgSetArchivable("/velocities/speed-down-fps");
349 // FIXME: Temporarily read-only, until the
350 // incompatibilities between JSBSim and
351 // LaRCSim are fixed (LaRCSim adds the
352 // earth's rotation to the east velocity).
353 _tiedProperties.Tie("/velocities/speed-north-fps", this,
354 &FGInterface::get_V_north, &FGInterface::set_V_north, false);
355 _tiedProperties.Tie("/velocities/speed-east-fps", this,
356 &FGInterface::get_V_east, &FGInterface::set_V_east, false);
357 _tiedProperties.Tie("/velocities/speed-down-fps", this,
358 &FGInterface::get_V_down, &FGInterface::set_V_down, false);
360 _tiedProperties.Tie("/velocities/north-relground-fps", this,
361 &FGInterface::get_V_north_rel_ground); // read-only
362 _tiedProperties.Tie("/velocities/east-relground-fps", this,
363 &FGInterface::get_V_east_rel_ground); // read-only
364 _tiedProperties.Tie("/velocities/down-relground-fps", this,
365 &FGInterface::get_V_down_rel_ground); // read-only
368 // FIXME: temporarily archivable, until the NED problem is fixed.
369 _tiedProperties.Tie("/velocities/uBody-fps", this,
370 &FGInterface::get_uBody,
371 &FGInterface::set_uBody,
373 fgSetArchivable("/velocities/uBody-fps");
374 _tiedProperties.Tie("/velocities/vBody-fps", this,
375 &FGInterface::get_vBody,
376 &FGInterface::set_vBody,
378 fgSetArchivable("/velocities/vBody-fps");
379 _tiedProperties.Tie("/velocities/wBody-fps", this,
380 &FGInterface::get_wBody,
381 &FGInterface::set_wBody,
383 fgSetArchivable("/velocities/wBody-fps");
385 // Climb and slip (read-only)
386 _tiedProperties.Tie("/velocities/vertical-speed-fps", this,
387 &FGInterface::get_Climb_Rate,
388 &FGInterface::set_Climb_Rate, false );
389 _tiedProperties.Tie("/velocities/glideslope", this,
390 &FGInterface::get_Gamma_vert_rad,
391 &FGInterface::set_Gamma_vert_rad, false );
392 _tiedProperties.Tie("/orientation/side-slip-rad", this,
393 &FGInterface::get_Beta, &FGInterface::_set_Beta, false);
394 _tiedProperties.Tie("/orientation/side-slip-deg", this,
395 &FGInterface::get_Beta_deg); // read-only
396 _tiedProperties.Tie("/orientation/alpha-deg", this,
397 &FGInterface::get_Alpha_deg, &FGInterface::set_Alpha_deg, false);
398 _tiedProperties.Tie("/accelerations/nlf", this,
399 &FGInterface::get_Nlf); // read-only
402 _tiedProperties.Tie("/accelerations/ned/north-accel-fps_sec",
403 this, &FGInterface::get_V_dot_north); // read-only
404 _tiedProperties.Tie("/accelerations/ned/east-accel-fps_sec",
405 this, &FGInterface::get_V_dot_east); // read-only
406 _tiedProperties.Tie("/accelerations/ned/down-accel-fps_sec",
407 this, &FGInterface::get_V_dot_down); // read-only
409 // Pilot accelerations
410 _tiedProperties.Tie("/accelerations/pilot/x-accel-fps_sec",
411 this, &FGInterface::get_A_X_pilot, &FGInterface::set_A_X_pilot, false);
412 _tiedProperties.Tie("/accelerations/pilot/y-accel-fps_sec",
413 this, &FGInterface::get_A_Y_pilot, &FGInterface::set_A_Y_pilot, false);
414 _tiedProperties.Tie("/accelerations/pilot/z-accel-fps_sec",
415 this, &FGInterface::get_A_Z_pilot, &FGInterface::set_A_Z_pilot, false);
417 _tiedProperties.Tie("/accelerations/n-z-cg-fps_sec",
418 this, &FGInterface::get_N_Z_cg); // read-only
423 * Unbind any properties bound to this FDM.
425 * This method allows the FDM to release properties so that a new
426 * FDM can bind them instead.
429 FGInterface::unbind ()
431 _tiedProperties.Untie();
436 * Update the state of the FDM (i.e. run the equations of motion).
439 FGInterface::update (double dt)
441 SG_LOG(SG_FLIGHT, SG_ALERT, "dummy update() ... SHOULDN'T BE CALLED!");
445 void FGInterface::_updatePositionM(const SGVec3d& cartPos)
447 TrackComputer tracker( track, geodetic_position_v );
448 cartesian_position_v = cartPos;
449 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
450 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
451 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
452 _update_ground_elev_at_pos();
456 void FGInterface::_updatePosition(const SGGeod& geod)
458 TrackComputer tracker( track, geodetic_position_v );
459 geodetic_position_v = geod;
460 cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
461 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
463 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
464 _update_ground_elev_at_pos();
468 void FGInterface::_updatePosition(const SGGeoc& geoc)
470 TrackComputer tracker( track, geodetic_position_v );
471 geocentric_position_v = geoc;
472 cartesian_position_v = SGVec3d::fromGeoc(geocentric_position_v);
473 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
475 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
476 _update_ground_elev_at_pos();
480 void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
482 _updatePosition(SGGeod::fromRadFt(lon, lat, alt));
486 void FGInterface::_updateGeocentricPosition( double lat, double lon,
489 _updatePosition(SGGeoc::fromRadFt(lon, lat, get_Sea_level_radius() + alt));
492 void FGInterface::_update_ground_elev_at_pos( void ) {
493 double groundlevel_m = get_groundlevel_m(geodetic_position_v);
494 _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
498 void FGInterface::set_Latitude(double lat) {
499 geodetic_position_v.setLatitudeRad(lat);
502 void FGInterface::set_Longitude(double lon) {
503 geodetic_position_v.setLongitudeRad(lon);
506 void FGInterface::set_Altitude(double alt) {
507 geodetic_position_v.setElevationFt(alt);
510 void FGInterface::set_AltitudeAGL(double altagl) {
515 void FGInterface::set_V_calibrated_kts(double vc) {
516 v_calibrated_kts = vc;
519 void FGInterface::set_Mach_number(double mach) {
523 void FGInterface::set_Velocities_Local( double north,
526 v_local_v[0] = north;
531 void FGInterface::set_Velocities_Wind_Body( double u,
534 v_wind_body_v[0] = u;
535 v_wind_body_v[1] = v;
536 v_wind_body_v[2] = w;
540 void FGInterface::set_Euler_Angles( double phi,
543 euler_angles_v[0] = phi;
544 euler_angles_v[1] = theta;
545 euler_angles_v[2] = psi;
549 void FGInterface::set_Climb_Rate( double roc) {
553 void FGInterface::set_Gamma_vert_rad( double gamma) {
554 gamma_vert_rad = gamma;
557 void FGInterface::set_Static_pressure(double p) { static_pressure = p; }
558 void FGInterface::set_Static_temperature(double T) { static_temperature = T; }
559 void FGInterface::set_Density(double rho) { density = rho; }
561 void FGInterface::set_Velocities_Local_Airmass (double wnorth,
564 v_local_airmass_v[0] = wnorth;
565 v_local_airmass_v[1] = weast;
566 v_local_airmass_v[2] = wdown;
570 void FGInterface::_busdump(void)
572 SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rp_body_v: " << d_cg_rp_body_v);
573 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_local_v: " << v_dot_local_v);
574 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_body_v: " << v_dot_body_v);
575 SG_LOG(SG_FLIGHT,SG_INFO,"a_cg_body_v: " << a_cg_body_v);
576 SG_LOG(SG_FLIGHT,SG_INFO,"a_pilot_body_v: " << a_pilot_body_v);
577 SG_LOG(SG_FLIGHT,SG_INFO,"n_cg_body_v: " << n_cg_body_v);
578 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v: " << v_local_v);
579 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_rel_ground_v: " << v_local_rel_ground_v);
580 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_airmass_v: " << v_local_airmass_v);
581 SG_LOG(SG_FLIGHT,SG_INFO,"v_wind_body_v: " << v_wind_body_v);
582 SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v: " << omega_body_v);
583 SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v: " << euler_rates_v);
584 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v: " << geocentric_rates_v);
585 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v: " << geocentric_position_v);
586 SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v: " << geodetic_position_v);
587 SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v: " << euler_angles_v);
589 SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << nlf );
590 SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << v_rel_wind );
591 SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << v_true_kts );
592 SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << v_ground_speed );
593 SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << v_equiv_kts );
594 SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << v_calibrated_kts );
595 SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << alpha );
596 SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << beta );
597 SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << gamma_vert_rad );
598 SG_LOG(SG_FLIGHT,SG_INFO,"density: " << density );
599 SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << mach_number );
600 SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << static_pressure );
601 SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << total_pressure );
602 SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << dynamic_pressure );
603 SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << static_temperature );
604 SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << total_temperature );
605 SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << sea_level_radius );
606 SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << earth_position_angle );
607 SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << runway_altitude );
608 SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << climb_rate );
609 SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << altitude_agl );
613 FGInterface::prepare_ground_cache_m(double startSimTime, double endSimTime,
614 const double pt[3], double rad)
616 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
621 FGInterface::prepare_ground_cache_ft(double startSimTime, double endSimTime,
622 const double pt[3], double rad)
624 // Convert units and do the real work.
625 SGVec3d pt_ft = SG_FEET_TO_METER*SGVec3d(pt);
626 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
627 pt_ft, rad*SG_FEET_TO_METER);
631 FGInterface::is_valid_m(double *ref_time, double pt[3], double *rad)
634 bool valid = ground_cache.is_valid(*ref_time, _pt, *rad);
639 bool FGInterface::is_valid_ft(double *ref_time, double pt[3], double *rad)
641 // Convert units and do the real work.
643 bool found_ground = ground_cache.is_valid(*ref_time, _pt, *rad);
644 assign(pt, SG_METER_TO_FEET*_pt);
645 *rad *= SG_METER_TO_FEET;
650 FGInterface::get_cat_m(double t, const double pt[3],
651 double end[2][3], double vel[2][3])
653 SGVec3d _end[2], _vel[2];
654 double dist = ground_cache.get_cat(t, SGVec3d(pt), _end, _vel);
655 for (int k=0; k<2; ++k) {
656 assign( end[k], _end[k] );
657 assign( vel[k], _vel[k] );
663 FGInterface::get_cat_ft(double t, const double pt[3],
664 double end[2][3], double vel[2][3])
666 // Convert units and do the real work.
667 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
668 SGVec3d _end[2], _vel[2];
669 double dist = ground_cache.get_cat(t, pt_m, _end, _vel);
670 for (int k=0; k<2; ++k) {
671 assign( end[k], SG_METER_TO_FEET*_end[k] );
672 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
674 return dist*SG_METER_TO_FEET;
678 FGInterface::get_body_m(double t, simgear::BVHNode::Id id,
679 double bodyToWorld[16], double linearVel[3],
680 double angularVel[3])
682 SGMatrixd _bodyToWorld;
683 SGVec3d _linearVel, _angularVel;
684 if (!ground_cache.get_body(t, _bodyToWorld, _linearVel, _angularVel, id))
687 assign(linearVel, _linearVel);
688 assign(angularVel, _angularVel);
689 for (unsigned i = 0; i < 16; ++i)
690 bodyToWorld[i] = _bodyToWorld.data()[i];
696 FGInterface::get_agl_m(double t, const double pt[3], double max_altoff,
697 double contact[3], double normal[3],
698 double linearVel[3], double angularVel[3],
699 simgear::BVHMaterial const*& material, simgear::BVHNode::Id& id)
701 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
702 SGVec3d _contact, _normal, _linearVel, _angularVel;
704 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
705 _angularVel, id, material);
706 // correct the linear velocity, since the line intersector delivers
707 // values for the start point and the get_agl function should
708 // traditionally deliver for the contact point
709 _linearVel += cross(_angularVel, _contact - pt_m);
711 assign(contact, _contact);
712 assign(normal, _normal);
713 assign(linearVel, _linearVel);
714 assign(angularVel, _angularVel);
719 FGInterface::get_agl_ft(double t, const double pt[3], double max_altoff,
720 double contact[3], double normal[3],
721 double linearVel[3], double angularVel[3],
722 simgear::BVHMaterial const*& material, simgear::BVHNode::Id& id)
724 // Convert units and do the real work.
725 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
726 pt_m *= SG_FEET_TO_METER;
727 SGVec3d _contact, _normal, _linearVel, _angularVel;
729 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
730 _angularVel, id, material);
731 // correct the linear velocity, since the line intersector delivers
732 // values for the start point and the get_agl function should
733 // traditionally deliver for the contact point
734 _linearVel += cross(_angularVel, _contact - pt_m);
736 // Convert units back ...
737 assign( contact, SG_METER_TO_FEET*_contact );
738 assign( normal, _normal );
739 assign( linearVel, SG_METER_TO_FEET*_linearVel );
740 assign( angularVel, _angularVel );
745 FGInterface::get_nearest_m(double t, const double pt[3], double maxDist,
746 double contact[3], double normal[3],
747 double linearVel[3], double angularVel[3],
748 simgear::BVHMaterial const*& material,
749 simgear::BVHNode::Id& id)
751 SGVec3d _contact, _linearVel, _angularVel;
753 if (!ground_cache.get_nearest(t, SGVec3d(pt), maxDist, _contact, _linearVel,
754 _angularVel, id, material))
757 assign(contact, _contact);
758 assign(linearVel, _linearVel);
759 assign(angularVel, _angularVel);
764 FGInterface::get_nearest_ft(double t, const double pt[3], double maxDist,
765 double contact[3], double normal[3],
766 double linearVel[3], double angularVel[3],
767 simgear::BVHMaterial const*& material,
768 simgear::BVHNode::Id& id)
770 SGVec3d _contact, _linearVel, _angularVel;
772 if (!ground_cache.get_nearest(t, SG_FEET_TO_METER*SGVec3d(pt),
773 SG_FEET_TO_METER*maxDist, _contact, _linearVel,
774 _angularVel, id, material))
777 assign(contact, SG_METER_TO_FEET*_contact);
778 assign(linearVel, SG_METER_TO_FEET*_linearVel);
779 assign(angularVel, _angularVel);
784 FGInterface::get_groundlevel_m(double lat, double lon, double alt)
786 return get_groundlevel_m(SGGeod::fromRadM(lon, lat, alt));
790 FGInterface::get_groundlevel_m(const SGGeod& geod)
792 // Compute the cartesian position of the given lat/lon/alt.
793 SGVec3d pos = SGVec3d::fromGeod(geod);
795 // FIXME: how to handle t - ref_time differences ???
797 double ref_time = 0, radius;
798 // Prepare the ground cache for that position.
799 if (!is_valid_m(&ref_time, cpos.data(), &radius)) {
800 double startTime = ref_time;
801 double endTime = startTime + 1;
802 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), 10);
803 /// This is most likely the case when the given altitude is
804 /// too low, try with a new altitude of 10000m, that should be
805 /// sufficient to find a ground level below everywhere on our planet
807 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
808 /// If there is still no ground, return sea level radius
809 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), 10))
812 } else if (radius*radius <= distSqr(pos, cpos)) {
813 double startTime = ref_time;
814 double endTime = startTime + 1;
816 /// We reuse the old radius value, but only if it is at least 10 Meters ..
817 if (!(10 < radius)) // Well this strange compare is nan safe
820 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), radius);
821 /// This is most likely the case when the given altitude is
822 /// too low, try with a new altitude of 10000m, that should be
823 /// sufficient to find a ground level below everywhere on our planet
825 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
826 /// If there is still no ground, return sea level radius
827 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), radius))
832 double contact[3], normal[3], vel[3], angvel[3];
833 const simgear::BVHMaterial* material;
834 simgear::BVHNode::Id id;
835 // Ignore the return value here, since it just tells us if
836 // the returns stem from the groundcache or from the coarse
837 // computations below the groundcache. The contact point is still something
838 // valid, the normals and the other returns just contain some defaults.
839 get_agl_m(ref_time, pos.data(), 2.0, contact, normal, vel, angvel,
841 return SGGeod::fromCart(SGVec3d(contact)).getElevationM();
845 FGInterface::caught_wire_m(double t, const double pt[4][3])
848 for (int i=0; i<4; ++i)
849 pt_m[i] = SGVec3d(pt[i]);
851 return ground_cache.caught_wire(t, pt_m);
855 FGInterface::caught_wire_ft(double t, const double pt[4][3])
857 // Convert units and do the real work.
859 for (int i=0; i<4; ++i)
860 pt_m[i] = SG_FEET_TO_METER*SGVec3d(pt[i]);
862 return ground_cache.caught_wire(t, pt_m);
866 FGInterface::get_wire_ends_m(double t, double end[2][3], double vel[2][3])
868 SGVec3d _end[2], _vel[2];
869 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
870 for (int k=0; k<2; ++k) {
871 assign( end[k], _end[k] );
872 assign( vel[k], _vel[k] );
878 FGInterface::get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
880 // Convert units and do the real work.
881 SGVec3d _end[2], _vel[2];
882 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
883 for (int k=0; k<2; ++k) {
884 assign( end[k], SG_METER_TO_FEET*_end[k] );
885 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
891 FGInterface::release_wire(void)
893 ground_cache.release_wire();