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)
47 // base_fdm_state is the internal state that is updated in integer
48 // multiples of "dt". This leads to "jitter" with respect to the real
49 // world time, so we introduce cur_fdm_state which is extrapolated by
50 // the difference between sim time and real world time
52 FGInterface *cur_fdm_state = 0;
53 FGInterface base_fdm_state;
56 FGInterface::FGInterface()
62 FGInterface::FGInterface( double dt )
69 FGInterface::~FGInterface() {
70 // unbind(); // FIXME: should be called explicitly
75 FGInterface::_calc_multiloop (double dt)
77 int hz = fgGetInt("/sim/model-hz");
78 int speedup = fgGetInt("/sim/speed-up");
83 // Avoid roundoff problems by adding the roundoff itself.
84 // ... ok, two times the roundoff to have enough room.
85 int multiloop = int(floor(ml * (1.0 + 2.0*DBL_EPSILON)));
86 remainder = (ml - multiloop) / hz;
88 // If we artificially inflate ml above by a tiny amount to get the
89 // closest integer, then subtract the integer from the original
90 // slightly smaller value, we can get a negative remainder.
91 // Logically this should never happen, and we definitely don't want
92 // to carry a negative remainder over to the next iteration, so
93 // never let the remainder go below zero.
95 // Note: this fixes a problem where we run 1, 3, 1, 3, 1, 3... loops
96 // of the FDM when in fact we want to run 2, 2, 2, 2, 2...
97 if ( remainder < 0 ) { remainder = 0; }
99 return (multiloop * speedup);
104 * Set default values for the state of the FDM.
106 * This method is invoked by the constructors.
109 FGInterface::_setup ()
114 d_cg_rp_body_v = SGVec3d::zeros();
115 v_dot_local_v = SGVec3d::zeros();
116 v_dot_body_v = SGVec3d::zeros();
117 a_cg_body_v = SGVec3d::zeros();
118 a_pilot_body_v = SGVec3d::zeros();
119 n_cg_body_v = SGVec3d::zeros();
120 v_local_v = SGVec3d::zeros();
121 v_local_rel_ground_v = SGVec3d::zeros();
122 v_local_airmass_v = SGVec3d::zeros();
123 v_wind_body_v = SGVec3d::zeros();
124 omega_body_v = SGVec3d::zeros();
125 euler_rates_v = SGVec3d::zeros();
126 geocentric_rates_v = SGVec3d::zeros();
127 geodetic_position_v = SGGeod::fromRadM(0, 0, 0);
128 cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
129 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
130 euler_angles_v = SGVec3d::zeros();
133 v_rel_wind=v_true_kts=0;
134 v_ground_speed=v_equiv_kts=0;
138 density=mach_number=0;
139 static_pressure=total_pressure=0;
141 static_temperature=total_temperature=0;
142 sea_level_radius=earth_position_angle=0;
149 FGInterface::init () {}
152 * Initialize the state of the FDM.
154 * Subclasses of FGInterface may do their own, additional initialization,
155 * but there is some that is common to all. Normally, they should call
156 * this before they begin their own init to make sure the basic structures
157 * are set up properly.
160 FGInterface::common_init ()
162 SG_LOG( SG_FLIGHT, SG_INFO, "Start common FDM init" );
167 // set_remainder( 0 );
169 // Set initial position
170 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing position..." );
171 double lon = fgGetDouble("/sim/presets/longitude-deg")
172 * SGD_DEGREES_TO_RADIANS;
173 double lat = fgGetDouble("/sim/presets/latitude-deg")
174 * SGD_DEGREES_TO_RADIANS;
175 double alt_ft = fgGetDouble("/sim/presets/altitude-ft");
176 double alt_m = alt_ft * SG_FEET_TO_METER;
177 set_Longitude( lon );
179 SG_LOG( SG_FLIGHT, SG_INFO, "Checking for lon = "
180 << lon*SGD_RADIANS_TO_DEGREES << "deg, lat = "
181 << lat*SGD_RADIANS_TO_DEGREES << "deg, alt = "
184 double ground_elev_m = get_groundlevel_m(lat, lon, alt_m);
185 double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET;
186 _set_Runway_altitude ( ground_elev_ft );
187 if ( fgGetBool("/sim/presets/onground") || alt_ft < ground_elev_ft ) {
188 fgSetDouble("/position/altitude-ft", ground_elev_ft + 0.1);
189 set_Altitude( ground_elev_ft + 0.1);
191 set_Altitude( alt_ft );
194 // Set ground elevation
195 SG_LOG( SG_FLIGHT, SG_INFO,
196 "...initializing ground elevation to " << ground_elev_ft
199 // Set sea-level radius
200 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing sea-level radius..." );
201 SG_LOG( SG_FLIGHT, SG_INFO, " lat = "
202 << fgGetDouble("/sim/presets/latitude-deg")
203 << " alt = " << get_Altitude() );
204 double slr = SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v);
205 _set_Sea_level_radius( slr * SG_METER_TO_FEET );
207 // Set initial velocities
208 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing velocities..." );
209 if ( !fgHasNode("/sim/presets/speed-set") ) {
210 set_V_calibrated_kts(0.0);
212 const string speedset = fgGetString("/sim/presets/speed-set");
213 if ( speedset == "knots" || speedset == "KNOTS" ) {
214 set_V_calibrated_kts( fgGetDouble("/sim/presets/airspeed-kt") );
215 } else if ( speedset == "mach" || speedset == "MACH" ) {
216 set_Mach_number( fgGetDouble("/sim/presets/mach") );
217 } else if ( speedset == "UVW" || speedset == "uvw" ) {
218 set_Velocities_Wind_Body(
219 fgGetDouble("/sim/presets/uBody-fps"),
220 fgGetDouble("/sim/presets/vBody-fps"),
221 fgGetDouble("/sim/presets/wBody-fps") );
222 } else if ( speedset == "NED" || speedset == "ned" ) {
223 set_Velocities_Local(
224 fgGetDouble("/sim/presets/speed-north-fps"),
225 fgGetDouble("/sim/presets/speed-east-fps"),
226 fgGetDouble("/sim/presets/speed-down-fps") );
228 SG_LOG( SG_FLIGHT, SG_ALERT,
229 "Unrecognized value for /sim/presets/speed-set: "
231 set_V_calibrated_kts( 0.0 );
235 // Set initial Euler angles
236 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing Euler angles..." );
237 set_Euler_Angles( fgGetDouble("/sim/presets/roll-deg")
238 * SGD_DEGREES_TO_RADIANS,
239 fgGetDouble("/sim/presets/pitch-deg")
240 * SGD_DEGREES_TO_RADIANS,
241 fgGetDouble("/sim/presets/heading-deg")
242 * SGD_DEGREES_TO_RADIANS );
244 SG_LOG( SG_FLIGHT, SG_INFO, "End common FDM init" );
249 * Bind getters and setters to properties.
251 * The bind() method will be invoked after init(). Note that unlike
252 * the usual implementations of FGSubsystem::bind(), this method does
253 * not automatically pick up existing values for the properties at
254 * bind time; instead, all values are set explicitly in the init()
262 // Time management (read-only)
263 // fgTie("/fdm/time/delta_t", this,
264 // &FGInterface::get_delta_t); // read-only
265 // fgTie("/fdm/time/elapsed", this,
266 // &FGInterface::get_elapsed); // read-only
267 // fgTie("/fdm/time/remainder", this,
268 // &FGInterface::get_remainder); // read-only
269 // fgTie("/fdm/time/multi_loop", this,
270 // &FGInterface::get_multi_loop); // read-only
273 fgTie("/position/latitude-deg", this,
274 &FGInterface::get_Latitude_deg,
275 &FGInterface::set_Latitude_deg,
277 fgSetArchivable("/position/latitude-deg");
278 fgTie("/position/longitude-deg", this,
279 &FGInterface::get_Longitude_deg,
280 &FGInterface::set_Longitude_deg,
282 fgSetArchivable("/position/longitude-deg");
283 fgTie("/position/altitude-ft", this,
284 &FGInterface::get_Altitude,
285 &FGInterface::set_Altitude,
287 fgSetArchivable("/position/altitude-ft");
288 fgTie("/position/altitude-agl-ft", this,
289 &FGInterface::get_Altitude_AGL); // read-only
290 fgSetArchivable("/position/ground-elev-ft");
291 fgTie("/position/ground-elev-ft", this,
292 &FGInterface::get_Runway_altitude); // read-only
293 fgSetArchivable("/position/ground-elev-m");
294 fgTie("/position/ground-elev-m", this,
295 &FGInterface::get_Runway_altitude_m); // read-only
296 fgTie("/environment/ground-elevation-m", this,
297 &FGInterface::get_Runway_altitude_m); // read-only
298 fgSetArchivable("/position/sea-level-radius-ft");
299 fgTie("/position/sea-level-radius-ft", this,
300 &FGInterface::get_Sea_level_radius); // read-only
303 fgTie("/orientation/roll-deg", this,
304 &FGInterface::get_Phi_deg,
305 &FGInterface::set_Phi_deg);
306 fgSetArchivable("/orientation/roll-deg");
307 fgTie("/orientation/pitch-deg", this,
308 &FGInterface::get_Theta_deg,
309 &FGInterface::set_Theta_deg);
310 fgSetArchivable("/orientation/pitch-deg");
311 fgTie("/orientation/heading-deg", this,
312 &FGInterface::get_Psi_deg,
313 &FGInterface::set_Psi_deg);
314 fgSetArchivable("/orientation/heading-deg");
316 // Body-axis "euler rates" (rotation speed, but in a funny
318 fgTie("/orientation/roll-rate-degps", this,
319 &FGInterface::get_Phi_dot_degps);
320 fgTie("/orientation/pitch-rate-degps", this,
321 &FGInterface::get_Theta_dot_degps);
322 fgTie("/orientation/yaw-rate-degps", this,
323 &FGInterface::get_Psi_dot_degps);
325 // Ground speed knots
326 fgTie("/velocities/groundspeed-kt", this,
327 &FGInterface::get_V_ground_speed_kt);
329 // Calibrated airspeed
330 fgTie("/velocities/airspeed-kt", this,
331 &FGInterface::get_V_calibrated_kts,
332 &FGInterface::set_V_calibrated_kts,
336 fgTie("/velocities/mach", this,
337 &FGInterface::get_Mach_number,
338 &FGInterface::set_Mach_number,
342 // fgTie("/velocities/speed-north-fps", this,
343 // &FGInterface::get_V_north,
344 // &FGInterface::set_V_north);
345 // fgSetArchivable("/velocities/speed-north-fps");
346 // fgTie("/velocities/speed-east-fps", this,
347 // &FGInterface::get_V_east,
348 // &FGInterface::set_V_east);
349 // fgSetArchivable("/velocities/speed-east-fps");
350 // fgTie("/velocities/speed-down-fps", this,
351 // &FGInterface::get_V_down,
352 // &FGInterface::set_V_down);
353 // fgSetArchivable("/velocities/speed-down-fps");
354 // FIXME: Temporarily read-only, until the
355 // incompatibilities between JSBSim and
356 // LaRCSim are fixed (LaRCSim adds the
357 // earth's rotation to the east velocity).
358 fgTie("/velocities/speed-north-fps", this,
359 &FGInterface::get_V_north);
360 fgTie("/velocities/speed-east-fps", this,
361 &FGInterface::get_V_east);
362 fgTie("/velocities/speed-down-fps", this,
363 &FGInterface::get_V_down);
366 // FIXME: temporarily archivable, until
367 // the NED problem is fixed.
368 fgTie("/velocities/uBody-fps", this,
369 &FGInterface::get_uBody,
370 &FGInterface::set_uBody,
372 fgSetArchivable("/velocities/uBody-fps");
373 fgTie("/velocities/vBody-fps", this,
374 &FGInterface::get_vBody,
375 &FGInterface::set_vBody,
377 fgSetArchivable("/velocities/vBody-fps");
378 fgTie("/velocities/wBody-fps", this,
379 &FGInterface::get_wBody,
380 &FGInterface::set_wBody,
382 fgSetArchivable("/velocities/wBody-fps");
384 // Climb and slip (read-only)
385 fgTie("/velocities/vertical-speed-fps", this,
386 &FGInterface::get_Climb_Rate,
387 &FGInterface::set_Climb_Rate );
388 fgTie("/velocities/glideslope", this,
389 &FGInterface::get_Gamma_vert_rad,
390 &FGInterface::set_Gamma_vert_rad );
391 fgTie("/orientation/side-slip-rad", this,
392 &FGInterface::get_Beta); // read-only
393 fgTie("/orientation/side-slip-deg", this,
394 &FGInterface::get_Beta_deg); // read-only
395 fgTie("/orientation/alpha-deg", this,
396 &FGInterface::get_Alpha_deg); // read-only
397 fgTie("/accelerations/nlf", this,
398 &FGInterface::get_Nlf); // read-only
401 fgTie("/accelerations/ned/north-accel-fps_sec",
402 this, &FGInterface::get_V_dot_north);
403 fgTie("/accelerations/ned/east-accel-fps_sec",
404 this, &FGInterface::get_V_dot_east);
405 fgTie("/accelerations/ned/down-accel-fps_sec",
406 this, &FGInterface::get_V_dot_down);
408 // Pilot accelerations
409 fgTie("/accelerations/pilot/x-accel-fps_sec",
410 this, &FGInterface::get_A_X_pilot);
411 fgTie("/accelerations/pilot/y-accel-fps_sec",
412 this, &FGInterface::get_A_Y_pilot);
413 fgTie("/accelerations/pilot/z-accel-fps_sec",
414 this, &FGInterface::get_A_Z_pilot);
420 * Unbind any properties bound to this FDM.
422 * This method allows the FDM to release properties so that a new
423 * FDM can bind them instead.
426 FGInterface::unbind ()
430 // fgUntie("/fdm/time/delta_t");
431 // fgUntie("/fdm/time/elapsed");
432 // fgUntie("/fdm/time/remainder");
433 // fgUntie("/fdm/time/multi_loop");
434 fgUntie("/position/latitude-deg");
435 fgUntie("/position/longitude-deg");
436 fgUntie("/position/altitude-ft");
437 fgUntie("/position/altitude-agl-ft");
438 fgUntie("/position/ground-elev-ft");
439 fgUntie("/position/ground-elev-m");
440 fgUntie("/environment/ground-elevation-m");
441 fgUntie("/position/sea-level-radius-ft");
442 fgUntie("/orientation/roll-deg");
443 fgUntie("/orientation/pitch-deg");
444 fgUntie("/orientation/heading-deg");
445 fgUntie("/orientation/roll-rate-degps");
446 fgUntie("/orientation/pitch-rate-degps");
447 fgUntie("/orientation/yaw-rate-degps");
448 fgUntie("/orientation/side-slip-rad");
449 fgUntie("/orientation/side-slip-deg");
450 fgUntie("/orientation/alpha-deg");
451 fgUntie("/velocities/airspeed-kt");
452 fgUntie("/velocities/groundspeed-kt");
453 fgUntie("/velocities/mach");
454 fgUntie("/velocities/speed-north-fps");
455 fgUntie("/velocities/speed-east-fps");
456 fgUntie("/velocities/speed-down-fps");
457 fgUntie("/velocities/uBody-fps");
458 fgUntie("/velocities/vBody-fps");
459 fgUntie("/velocities/wBody-fps");
460 fgUntie("/velocities/vertical-speed-fps");
461 fgUntie("/velocities/glideslope");
462 fgUntie("/accelerations/nlf");
463 fgUntie("/accelerations/pilot/x-accel-fps_sec");
464 fgUntie("/accelerations/pilot/y-accel-fps_sec");
465 fgUntie("/accelerations/pilot/z-accel-fps_sec");
466 fgUntie("/accelerations/ned/north-accel-fps_sec");
467 fgUntie("/accelerations/ned/east-accel-fps_sec");
468 fgUntie("/accelerations/ned/down-accel-fps_sec");
472 * Update the state of the FDM (i.e. run the equations of motion).
475 FGInterface::update (double dt)
477 SG_LOG(SG_FLIGHT, SG_ALERT, "dummy update() ... SHOULDN'T BE CALLED!");
481 void FGInterface::_updatePositionM(const SGVec3d& cartPos)
483 cartesian_position_v = cartPos;
484 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
485 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
486 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
487 _update_ground_elev_at_pos();
491 void FGInterface::_updatePosition(const SGGeod& geod)
493 geodetic_position_v = geod;
494 cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
495 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
497 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
498 _update_ground_elev_at_pos();
502 void FGInterface::_updatePosition(const SGGeoc& geoc)
504 geocentric_position_v = geoc;
505 cartesian_position_v = SGVec3d::fromGeoc(geocentric_position_v);
506 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
508 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
509 _update_ground_elev_at_pos();
513 void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
515 _updatePosition(SGGeod::fromRadFt(lon, lat, alt));
519 void FGInterface::_updateGeocentricPosition( double lat, double lon,
522 _updatePosition(SGGeoc::fromRadFt(lon, lat, get_Sea_level_radius() + alt));
525 void FGInterface::_update_ground_elev_at_pos( void ) {
526 double groundlevel_m = get_groundlevel_m(geodetic_position_v);
527 _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
531 void FGInterface::set_Latitude(double lat) {
532 geodetic_position_v.setLatitudeRad(lat);
535 void FGInterface::set_Longitude(double lon) {
536 geodetic_position_v.setLongitudeRad(lon);
539 void FGInterface::set_Altitude(double alt) {
540 geodetic_position_v.setElevationFt(alt);
543 void FGInterface::set_AltitudeAGL(double altagl) {
548 void FGInterface::set_V_calibrated_kts(double vc) {
549 v_calibrated_kts = vc;
552 void FGInterface::set_Mach_number(double mach) {
556 void FGInterface::set_Velocities_Local( double north,
559 v_local_v[0] = north;
564 void FGInterface::set_Velocities_Wind_Body( double u,
567 v_wind_body_v[0] = u;
568 v_wind_body_v[1] = v;
569 v_wind_body_v[2] = w;
573 void FGInterface::set_Euler_Angles( double phi,
576 euler_angles_v[0] = phi;
577 euler_angles_v[1] = theta;
578 euler_angles_v[2] = psi;
582 void FGInterface::set_Climb_Rate( double roc) {
586 void FGInterface::set_Gamma_vert_rad( double gamma) {
587 gamma_vert_rad = gamma;
590 void FGInterface::set_Static_pressure(double p) { static_pressure = p; }
591 void FGInterface::set_Static_temperature(double T) { static_temperature = T; }
592 void FGInterface::set_Density(double rho) { density = rho; }
594 void FGInterface::set_Velocities_Local_Airmass (double wnorth,
597 v_local_airmass_v[0] = wnorth;
598 v_local_airmass_v[1] = weast;
599 v_local_airmass_v[2] = wdown;
603 void FGInterface::_busdump(void) {
605 SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rp_body_v: " << d_cg_rp_body_v);
606 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_local_v: " << v_dot_local_v);
607 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_body_v: " << v_dot_body_v);
608 SG_LOG(SG_FLIGHT,SG_INFO,"a_cg_body_v: " << a_cg_body_v);
609 SG_LOG(SG_FLIGHT,SG_INFO,"a_pilot_body_v: " << a_pilot_body_v);
610 SG_LOG(SG_FLIGHT,SG_INFO,"n_cg_body_v: " << n_cg_body_v);
611 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v: " << v_local_v);
612 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_rel_ground_v: " << v_local_rel_ground_v);
613 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_airmass_v: " << v_local_airmass_v);
614 SG_LOG(SG_FLIGHT,SG_INFO,"v_wind_body_v: " << v_wind_body_v);
615 SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v: " << omega_body_v);
616 SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v: " << euler_rates_v);
617 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v: " << geocentric_rates_v);
618 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v: " << geocentric_position_v);
619 SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v: " << geodetic_position_v);
620 SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v: " << euler_angles_v);
622 SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << nlf );
623 SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << v_rel_wind );
624 SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << v_true_kts );
625 SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << v_ground_speed );
626 SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << v_equiv_kts );
627 SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << v_calibrated_kts );
628 SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << alpha );
629 SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << beta );
630 SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << gamma_vert_rad );
631 SG_LOG(SG_FLIGHT,SG_INFO,"density: " << density );
632 SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << mach_number );
633 SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << static_pressure );
634 SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << total_pressure );
635 SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << dynamic_pressure );
636 SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << static_temperature );
637 SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << total_temperature );
638 SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << sea_level_radius );
639 SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << earth_position_angle );
640 SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << runway_altitude );
641 SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << climb_rate );
642 SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << altitude_agl );
646 FGInterface::prepare_ground_cache_m(double ref_time, const double pt[3],
649 return ground_cache.prepare_ground_cache(ref_time, SGVec3d(pt), rad);
652 bool FGInterface::prepare_ground_cache_ft(double ref_time, const double pt[3],
655 // Convert units and do the real work.
656 SGVec3d pt_ft = SG_FEET_TO_METER*SGVec3d(pt);
657 return ground_cache.prepare_ground_cache(ref_time, pt_ft, rad*SG_FEET_TO_METER);
661 FGInterface::is_valid_m(double *ref_time, double pt[3], double *rad)
664 bool valid = ground_cache.is_valid(*ref_time, _pt, *rad);
669 bool FGInterface::is_valid_ft(double *ref_time, double pt[3], double *rad)
671 // Convert units and do the real work.
673 bool found_ground = ground_cache.is_valid(*ref_time, _pt, *rad);
674 assign(pt, SG_METER_TO_FEET*_pt);
675 *rad *= SG_METER_TO_FEET;
680 FGInterface::get_cat_m(double t, const double pt[3],
681 double end[2][3], double vel[2][3])
683 SGVec3d _end[2], _vel[2];
684 double dist = ground_cache.get_cat(t, SGVec3d(pt), _end, _vel);
685 for (int k=0; k<2; ++k) {
686 assign( end[k], _end[k] );
687 assign( vel[k], _vel[k] );
693 FGInterface::get_cat_ft(double t, const double pt[3],
694 double end[2][3], double vel[2][3])
696 // Convert units and do the real work.
697 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
698 SGVec3d _end[2], _vel[2];
699 double dist = ground_cache.get_cat(t, pt_m, _end, _vel);
700 for (int k=0; k<2; ++k) {
701 assign( end[k], SG_METER_TO_FEET*_end[k] );
702 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
704 return dist*SG_METER_TO_FEET;
708 FGInterface::get_body_m(double t, simgear::BVHNode::Id id,
709 double bodyToWorld[16], double linearVel[3],
710 double angularVel[3])
712 SGMatrixd _bodyToWorld;
713 SGVec3d _linearVel, _angularVel;
714 if (!ground_cache.get_body(t, _bodyToWorld, _linearVel, _angularVel, id))
717 assign(linearVel, _linearVel);
718 assign(angularVel, _angularVel);
719 for (unsigned i = 0; i < 16; ++i)
720 bodyToWorld[i] = _bodyToWorld.data()[i];
726 FGInterface::get_agl_m(double t, const double pt[3], double max_altoff,
727 double contact[3], double normal[3],
728 double linearVel[3], double angularVel[3],
729 SGMaterial const*& material, simgear::BVHNode::Id& id)
731 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
732 SGVec3d _contact, _normal, _linearVel, _angularVel;
734 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
735 _angularVel, id, material);
736 // correct the linear velocity, since the line intersector delivers
737 // values for the start point and the get_agl function should
738 // traditionally deliver for the contact point
739 _linearVel += cross(_angularVel, _contact - pt_m);
741 assign(contact, _contact);
742 assign(normal, _normal);
743 assign(linearVel, _linearVel);
744 assign(angularVel, _angularVel);
749 FGInterface::get_agl_ft(double t, const double pt[3], double max_altoff,
750 double contact[3], double normal[3],
751 double linearVel[3], double angularVel[3],
752 SGMaterial const*& material, simgear::BVHNode::Id& id)
754 // Convert units and do the real work.
755 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
756 pt_m *= SG_FEET_TO_METER;
757 SGVec3d _contact, _normal, _linearVel, _angularVel;
759 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
760 _angularVel, id, material);
761 // correct the linear velocity, since the line intersector delivers
762 // values for the start point and the get_agl function should
763 // traditionally deliver for the contact point
764 _linearVel += cross(_angularVel, _contact - pt_m);
766 // Convert units back ...
767 assign( contact, SG_METER_TO_FEET*_contact );
768 assign( normal, _normal );
769 assign( linearVel, SG_METER_TO_FEET*_linearVel );
770 assign( angularVel, _angularVel );
775 FGInterface::get_nearest_m(double t, const double pt[3], double maxDist,
776 double contact[3], double normal[3],
777 double linearVel[3], double angularVel[3],
778 SGMaterial const*& material,
779 simgear::BVHNode::Id& id)
781 SGVec3d _contact, _linearVel, _angularVel;
782 if (!ground_cache.get_nearest(t, SGVec3d(pt), maxDist, _contact, _linearVel,
783 _angularVel, id, material))
786 assign(contact, _contact);
787 assign(linearVel, _linearVel);
788 assign(angularVel, _angularVel);
793 FGInterface::get_nearest_ft(double t, const double pt[3], double maxDist,
794 double contact[3], double normal[3],
795 double linearVel[3], double angularVel[3],
796 SGMaterial const*& material,
797 simgear::BVHNode::Id& id)
799 SGVec3d _contact, _linearVel, _angularVel;
800 if (!ground_cache.get_nearest(t, SG_FEET_TO_METER*SGVec3d(pt),
801 SG_FEET_TO_METER*maxDist, _contact, _linearVel,
802 _angularVel, id, material))
805 assign(contact, SG_METER_TO_FEET*_contact);
806 assign(linearVel, SG_METER_TO_FEET*_linearVel);
807 assign(angularVel, _angularVel);
812 FGInterface::get_groundlevel_m(double lat, double lon, double alt)
814 return get_groundlevel_m(SGGeod::fromRadM(lon, lat, alt));
818 FGInterface::get_groundlevel_m(const SGGeod& geod)
820 // Compute the cartesian position of the given lat/lon/alt.
821 SGVec3d pos = SGVec3d::fromGeod(geod);
823 // FIXME: how to handle t - ref_time differences ???
825 double ref_time, radius;
826 // Prepare the ground cache for that position.
827 if (!is_valid_m(&ref_time, cpos.data(), &radius)) {
828 bool ok = prepare_ground_cache_m(ref_time, pos.data(), 10);
829 /// This is most likely the case when the given altitude is
830 /// too low, try with a new altitude of 10000m, that should be
831 /// sufficient to find a ground level below everywhere on our planet
833 pos = SGVec3d::fromGeod(SGGeod::fromRadM(geod.getLongitudeRad(), geod.getLatitudeRad(), 10000));
834 /// If there is still no ground, return sea level radius
835 if (!prepare_ground_cache_m(ref_time, pos.data(), 10))
838 } else if (radius*radius <= distSqr(pos, cpos)) {
839 /// We reuse the old radius value, but only if it is at least 10 Meters ..
840 if (!(10 < radius)) // Well this strange compare is nan safe
843 bool ok = prepare_ground_cache_m(ref_time, pos.data(), radius);
844 /// This is most likely the case when the given altitude is
845 /// too low, try with a new altitude of 10000m, that should be
846 /// sufficient to find a ground level below everywhere on our planet
848 pos = SGVec3d::fromGeod(SGGeod::fromRadM(geod.getLongitudeRad(), geod.getLatitudeRad(), 10000));
849 /// If there is still no ground, return sea level radius
850 if (!prepare_ground_cache_m(ref_time, pos.data(), radius))
855 double contact[3], normal[3], vel[3], angvel[3];
856 const SGMaterial* material;
857 simgear::BVHNode::Id id;
858 // Ignore the return value here, since it just tells us if
859 // the returns stem from the groundcache or from the coarse
860 // computations below the groundcache. The contact point is still something
861 // valid, the normals and the other returns just contain some defaults.
862 get_agl_m(ref_time, pos.data(), 2.0, contact, normal, vel, angvel,
864 return SGGeod::fromCart(SGVec3d(contact)).getElevationM();
868 FGInterface::caught_wire_m(double t, const double pt[4][3])
871 for (int i=0; i<4; ++i)
872 pt_m[i] = SGVec3d(pt[i]);
874 return ground_cache.caught_wire(t, pt_m);
878 FGInterface::caught_wire_ft(double t, const double pt[4][3])
880 // Convert units and do the real work.
882 for (int i=0; i<4; ++i)
883 pt_m[i] = SG_FEET_TO_METER*SGVec3d(pt[i]);
885 return ground_cache.caught_wire(t, pt_m);
889 FGInterface::get_wire_ends_m(double t, double end[2][3], double vel[2][3])
891 SGVec3d _end[2], _vel[2];
892 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
893 for (int k=0; k<2; ++k) {
894 assign( end[k], _end[k] );
895 assign( vel[k], _vel[k] );
901 FGInterface::get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
903 // Convert units and do the real work.
904 SGVec3d _end[2], _vel[2];
905 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
906 for (int k=0; k<2; ++k) {
907 assign( end[k], SG_METER_TO_FEET*_end[k] );
908 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
914 FGInterface::release_wire(void)
916 ground_cache.release_wire();
919 void fgToggleFDMdataLogging(void) {
920 cur_fdm_state->ToggleDataLogging();