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/scene/material/mat.hxx>
33 #include <simgear/timing/timestamp.hxx>
35 #include <Scenery/scenery.hxx>
36 #include <Main/globals.hxx>
37 #include <Main/fg_props.hxx>
38 #include <FDM/groundcache.hxx>
41 static inline void assign(double* ptr, const SGVec3d& vec)
48 // base_fdm_state is the internal state that is updated in integer
49 // multiples of "dt". This leads to "jitter" with respect to the real
50 // world time, so we introduce cur_fdm_state which is extrapolated by
51 // the difference between sim time and real world time
53 FGInterface *cur_fdm_state = 0;
54 FGInterface base_fdm_state;
57 FGInterface::FGInterface()
63 FGInterface::FGInterface( double dt )
70 FGInterface::~FGInterface() {
71 // unbind(); // FIXME: should be called explicitly
76 FGInterface::_calc_multiloop (double dt)
78 int hz = fgGetInt("/sim/model-hz");
79 int speedup = fgGetInt("/sim/speed-up");
84 // Avoid roundoff problems by adding the roundoff itself.
85 // ... ok, two times the roundoff to have enough room.
86 int multiloop = int(floor(ml * (1.0 + 2.0*DBL_EPSILON)));
87 remainder = (ml - multiloop) / hz;
89 // If we artificially inflate ml above by a tiny amount to get the
90 // closest integer, then subtract the integer from the original
91 // slightly smaller value, we can get a negative remainder.
92 // Logically this should never happen, and we definitely don't want
93 // to carry a negative remainder over to the next iteration, so
94 // never let the remainder go below zero.
96 // Note: this fixes a problem where we run 1, 3, 1, 3, 1, 3... loops
97 // of the FDM when in fact we want to run 2, 2, 2, 2, 2...
98 if ( remainder < 0 ) { remainder = 0; }
100 return (multiloop * speedup);
105 * Set default values for the state of the FDM.
107 * This method is invoked by the constructors.
110 FGInterface::_setup ()
115 d_cg_rp_body_v = SGVec3d::zeros();
116 v_dot_local_v = SGVec3d::zeros();
117 v_dot_body_v = SGVec3d::zeros();
118 a_cg_body_v = SGVec3d::zeros();
119 a_pilot_body_v = SGVec3d::zeros();
120 n_cg_body_v = SGVec3d::zeros();
121 v_local_v = SGVec3d::zeros();
122 v_local_rel_ground_v = SGVec3d::zeros();
123 v_local_airmass_v = SGVec3d::zeros();
124 v_wind_body_v = SGVec3d::zeros();
125 omega_body_v = SGVec3d::zeros();
126 euler_rates_v = SGVec3d::zeros();
127 geocentric_rates_v = SGVec3d::zeros();
128 geodetic_position_v = SGGeod::fromRadM(0, 0, 0);
129 cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
130 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
131 euler_angles_v = SGVec3d::zeros();
133 mass=i_xx=i_yy=i_zz=i_xz=0;
135 v_rel_wind=v_true_kts=0;
136 v_ground_speed=v_equiv_kts=0;
140 density=mach_number=0;
141 static_pressure=total_pressure=0;
143 static_temperature=total_temperature=0;
144 sea_level_radius=earth_position_angle=0;
151 FGInterface::init () {}
154 * Initialize the state of the FDM.
156 * Subclasses of FGInterface may do their own, additional initialization,
157 * but there is some that is common to all. Normally, they should call
158 * this before they begin their own init to make sure the basic structures
159 * are set up properly.
162 FGInterface::common_init ()
164 SG_LOG( SG_FLIGHT, SG_INFO, "Start common FDM init" );
169 // set_remainder( 0 );
171 // Set initial position
172 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing position..." );
173 double lon = fgGetDouble("/sim/presets/longitude-deg")
174 * SGD_DEGREES_TO_RADIANS;
175 double lat = fgGetDouble("/sim/presets/latitude-deg")
176 * SGD_DEGREES_TO_RADIANS;
177 double alt_ft = fgGetDouble("/sim/presets/altitude-ft");
178 double alt_m = alt_ft * SG_FEET_TO_METER;
179 set_Longitude( lon );
181 SG_LOG( SG_FLIGHT, SG_INFO, "Checking for lon = "
182 << lon*SGD_RADIANS_TO_DEGREES << "deg, lat = "
183 << lat*SGD_RADIANS_TO_DEGREES << "deg, alt = "
186 double ground_elev_m = get_groundlevel_m(lat, lon, alt_m);
187 double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET;
188 _set_Runway_altitude ( ground_elev_ft );
189 if ( fgGetBool("/sim/presets/onground") || alt_ft < ground_elev_ft ) {
190 fgSetDouble("/position/altitude-ft", ground_elev_ft + 0.1);
191 set_Altitude( ground_elev_ft + 0.1);
193 set_Altitude( alt_ft );
196 // Set ground elevation
197 SG_LOG( SG_FLIGHT, SG_INFO,
198 "...initializing ground elevation to " << ground_elev_ft
201 // Set sea-level radius
202 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing sea-level radius..." );
203 SG_LOG( SG_FLIGHT, SG_INFO, " lat = "
204 << fgGetDouble("/sim/presets/latitude-deg")
205 << " alt = " << get_Altitude() );
206 double slr = SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v);
207 _set_Sea_level_radius( slr * SG_METER_TO_FEET );
209 // Set initial velocities
210 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing velocities..." );
211 if ( !fgHasNode("/sim/presets/speed-set") ) {
212 set_V_calibrated_kts(0.0);
214 const string speedset = fgGetString("/sim/presets/speed-set");
215 if ( speedset == "knots" || speedset == "KNOTS" ) {
216 set_V_calibrated_kts( fgGetDouble("/sim/presets/airspeed-kt") );
217 } else if ( speedset == "mach" || speedset == "MACH" ) {
218 set_Mach_number( fgGetDouble("/sim/presets/mach") );
219 } else if ( speedset == "UVW" || speedset == "uvw" ) {
220 set_Velocities_Wind_Body(
221 fgGetDouble("/sim/presets/uBody-fps"),
222 fgGetDouble("/sim/presets/vBody-fps"),
223 fgGetDouble("/sim/presets/wBody-fps") );
224 } else if ( speedset == "NED" || speedset == "ned" ) {
225 set_Velocities_Local(
226 fgGetDouble("/sim/presets/speed-north-fps"),
227 fgGetDouble("/sim/presets/speed-east-fps"),
228 fgGetDouble("/sim/presets/speed-down-fps") );
230 SG_LOG( SG_FLIGHT, SG_ALERT,
231 "Unrecognized value for /sim/presets/speed-set: "
233 set_V_calibrated_kts( 0.0 );
237 // Set initial Euler angles
238 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing Euler angles..." );
239 set_Euler_Angles( fgGetDouble("/sim/presets/roll-deg")
240 * SGD_DEGREES_TO_RADIANS,
241 fgGetDouble("/sim/presets/pitch-deg")
242 * SGD_DEGREES_TO_RADIANS,
243 fgGetDouble("/sim/presets/heading-deg")
244 * SGD_DEGREES_TO_RADIANS );
246 SG_LOG( SG_FLIGHT, SG_INFO, "End common FDM init" );
251 * Bind getters and setters to properties.
253 * The bind() method will be invoked after init(). Note that unlike
254 * the usual implementations of FGSubsystem::bind(), this method does
255 * not automatically pick up existing values for the properties at
256 * bind time; instead, all values are set explicitly in the init()
264 // Time management (read-only)
265 // fgTie("/fdm/time/delta_t", this,
266 // &FGInterface::get_delta_t); // read-only
267 // fgTie("/fdm/time/elapsed", this,
268 // &FGInterface::get_elapsed); // read-only
269 // fgTie("/fdm/time/remainder", this,
270 // &FGInterface::get_remainder); // read-only
271 // fgTie("/fdm/time/multi_loop", this,
272 // &FGInterface::get_multi_loop); // read-only
275 fgTie("/position/latitude-deg", this,
276 &FGInterface::get_Latitude_deg,
277 &FGInterface::set_Latitude_deg,
279 fgSetArchivable("/position/latitude-deg");
280 fgTie("/position/longitude-deg", this,
281 &FGInterface::get_Longitude_deg,
282 &FGInterface::set_Longitude_deg,
284 fgSetArchivable("/position/longitude-deg");
285 fgTie("/position/altitude-ft", this,
286 &FGInterface::get_Altitude,
287 &FGInterface::set_Altitude,
289 fgSetArchivable("/position/altitude-ft");
290 fgTie("/position/altitude-agl-ft", this,
291 &FGInterface::get_Altitude_AGL); // read-only
292 fgSetArchivable("/position/ground-elev-ft");
293 fgTie("/position/ground-elev-ft", this,
294 &FGInterface::get_Runway_altitude); // read-only
295 fgSetArchivable("/position/ground-elev-m");
296 fgTie("/position/ground-elev-m", this,
297 &FGInterface::get_Runway_altitude_m); // read-only
298 fgTie("/environment/ground-elevation-m", this,
299 &FGInterface::get_Runway_altitude_m); // read-only
300 fgSetArchivable("/position/sea-level-radius-ft");
301 fgTie("/position/sea-level-radius-ft", this,
302 &FGInterface::get_Sea_level_radius); // read-only
305 fgTie("/orientation/roll-deg", this,
306 &FGInterface::get_Phi_deg,
307 &FGInterface::set_Phi_deg);
308 fgSetArchivable("/orientation/roll-deg");
309 fgTie("/orientation/pitch-deg", this,
310 &FGInterface::get_Theta_deg,
311 &FGInterface::set_Theta_deg);
312 fgSetArchivable("/orientation/pitch-deg");
313 fgTie("/orientation/heading-deg", this,
314 &FGInterface::get_Psi_deg,
315 &FGInterface::set_Psi_deg);
316 fgSetArchivable("/orientation/heading-deg");
318 // Body-axis "euler rates" (rotation speed, but in a funny
320 fgTie("/orientation/roll-rate-degps", this,
321 &FGInterface::get_Phi_dot_degps);
322 fgTie("/orientation/pitch-rate-degps", this,
323 &FGInterface::get_Theta_dot_degps);
324 fgTie("/orientation/yaw-rate-degps", this,
325 &FGInterface::get_Psi_dot_degps);
327 // Ground speed knots
328 fgTie("/velocities/groundspeed-kt", this,
329 &FGInterface::get_V_ground_speed_kt);
331 // Calibrated airspeed
332 fgTie("/velocities/airspeed-kt", this,
333 &FGInterface::get_V_calibrated_kts,
334 &FGInterface::set_V_calibrated_kts,
338 fgTie("/velocities/mach", this,
339 &FGInterface::get_Mach_number,
340 &FGInterface::set_Mach_number,
344 // fgTie("/velocities/speed-north-fps", this,
345 // &FGInterface::get_V_north,
346 // &FGInterface::set_V_north);
347 // fgSetArchivable("/velocities/speed-north-fps");
348 // fgTie("/velocities/speed-east-fps", this,
349 // &FGInterface::get_V_east,
350 // &FGInterface::set_V_east);
351 // fgSetArchivable("/velocities/speed-east-fps");
352 // fgTie("/velocities/speed-down-fps", this,
353 // &FGInterface::get_V_down,
354 // &FGInterface::set_V_down);
355 // fgSetArchivable("/velocities/speed-down-fps");
356 // FIXME: Temporarily read-only, until the
357 // incompatibilities between JSBSim and
358 // LaRCSim are fixed (LaRCSim adds the
359 // earth's rotation to the east velocity).
360 fgTie("/velocities/speed-north-fps", this,
361 &FGInterface::get_V_north);
362 fgTie("/velocities/speed-east-fps", this,
363 &FGInterface::get_V_east);
364 fgTie("/velocities/speed-down-fps", this,
365 &FGInterface::get_V_down);
368 // FIXME: temporarily archivable, until
369 // the NED problem is fixed.
370 fgTie("/velocities/uBody-fps", this,
371 &FGInterface::get_uBody,
372 &FGInterface::set_uBody,
374 fgSetArchivable("/velocities/uBody-fps");
375 fgTie("/velocities/vBody-fps", this,
376 &FGInterface::get_vBody,
377 &FGInterface::set_vBody,
379 fgSetArchivable("/velocities/vBody-fps");
380 fgTie("/velocities/wBody-fps", this,
381 &FGInterface::get_wBody,
382 &FGInterface::set_wBody,
384 fgSetArchivable("/velocities/wBody-fps");
386 // Climb and slip (read-only)
387 fgTie("/velocities/vertical-speed-fps", this,
388 &FGInterface::get_Climb_Rate,
389 &FGInterface::set_Climb_Rate );
390 fgTie("/velocities/glideslope", this,
391 &FGInterface::get_Gamma_vert_rad,
392 &FGInterface::set_Gamma_vert_rad );
393 fgTie("/orientation/side-slip-rad", this,
394 &FGInterface::get_Beta); // read-only
395 fgTie("/orientation/side-slip-deg", this,
396 &FGInterface::get_Beta_deg); // read-only
397 fgTie("/orientation/alpha-deg", this,
398 &FGInterface::get_Alpha_deg); // read-only
399 fgTie("/accelerations/nlf", this,
400 &FGInterface::get_Nlf); // read-only
403 fgTie("/accelerations/ned/north-accel-fps_sec",
404 this, &FGInterface::get_V_dot_north);
405 fgTie("/accelerations/ned/east-accel-fps_sec",
406 this, &FGInterface::get_V_dot_east);
407 fgTie("/accelerations/ned/down-accel-fps_sec",
408 this, &FGInterface::get_V_dot_down);
410 // Pilot accelerations
411 fgTie("/accelerations/pilot/x-accel-fps_sec",
412 this, &FGInterface::get_A_X_pilot);
413 fgTie("/accelerations/pilot/y-accel-fps_sec",
414 this, &FGInterface::get_A_Y_pilot);
415 fgTie("/accelerations/pilot/z-accel-fps_sec",
416 this, &FGInterface::get_A_Z_pilot);
422 * Unbind any properties bound to this FDM.
424 * This method allows the FDM to release properties so that a new
425 * FDM can bind them instead.
428 FGInterface::unbind ()
432 // fgUntie("/fdm/time/delta_t");
433 // fgUntie("/fdm/time/elapsed");
434 // fgUntie("/fdm/time/remainder");
435 // fgUntie("/fdm/time/multi_loop");
436 fgUntie("/position/latitude-deg");
437 fgUntie("/position/longitude-deg");
438 fgUntie("/position/altitude-ft");
439 fgUntie("/position/altitude-agl-ft");
440 fgUntie("/position/ground-elev-ft");
441 fgUntie("/position/ground-elev-m");
442 fgUntie("/environment/ground-elevation-m");
443 fgUntie("/position/sea-level-radius-ft");
444 fgUntie("/orientation/roll-deg");
445 fgUntie("/orientation/pitch-deg");
446 fgUntie("/orientation/heading-deg");
447 fgUntie("/orientation/roll-rate-degps");
448 fgUntie("/orientation/pitch-rate-degps");
449 fgUntie("/orientation/yaw-rate-degps");
450 fgUntie("/orientation/side-slip-rad");
451 fgUntie("/orientation/side-slip-deg");
452 fgUntie("/orientation/alpha-deg");
453 fgUntie("/velocities/airspeed-kt");
454 fgUntie("/velocities/groundspeed-kt");
455 fgUntie("/velocities/mach");
456 fgUntie("/velocities/speed-north-fps");
457 fgUntie("/velocities/speed-east-fps");
458 fgUntie("/velocities/speed-down-fps");
459 fgUntie("/velocities/uBody-fps");
460 fgUntie("/velocities/vBody-fps");
461 fgUntie("/velocities/wBody-fps");
462 fgUntie("/velocities/vertical-speed-fps");
463 fgUntie("/velocities/glideslope");
464 fgUntie("/accelerations/nlf");
465 fgUntie("/accelerations/pilot/x-accel-fps_sec");
466 fgUntie("/accelerations/pilot/y-accel-fps_sec");
467 fgUntie("/accelerations/pilot/z-accel-fps_sec");
468 fgUntie("/accelerations/ned/north-accel-fps_sec");
469 fgUntie("/accelerations/ned/east-accel-fps_sec");
470 fgUntie("/accelerations/ned/down-accel-fps_sec");
474 * Update the state of the FDM (i.e. run the equations of motion).
477 FGInterface::update (double dt)
479 SG_LOG(SG_FLIGHT, SG_ALERT, "dummy update() ... SHOULDN'T BE CALLED!");
483 void FGInterface::_updatePositionM(const SGVec3d& cartPos)
485 cartesian_position_v = cartPos;
486 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
487 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
488 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
489 _update_ground_elev_at_pos();
493 void FGInterface::_updatePosition(const SGGeod& geod)
495 geodetic_position_v = geod;
496 cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
497 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
499 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
500 _update_ground_elev_at_pos();
504 void FGInterface::_updatePosition(const SGGeoc& geoc)
506 geocentric_position_v = geoc;
507 cartesian_position_v = SGVec3d::fromGeoc(geocentric_position_v);
508 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
510 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
511 _update_ground_elev_at_pos();
515 void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
517 _updatePosition(SGGeod::fromRadFt(lon, lat, alt));
521 void FGInterface::_updateGeocentricPosition( double lat, double lon,
524 _updatePosition(SGGeoc::fromRadFt(lon, lat, get_Sea_level_radius() + alt));
527 void FGInterface::_update_ground_elev_at_pos( void ) {
528 double groundlevel_m = get_groundlevel_m(geodetic_position_v);
529 _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
533 void FGInterface::set_Latitude(double lat) {
534 geodetic_position_v.setLatitudeRad(lat);
537 void FGInterface::set_Longitude(double lon) {
538 geodetic_position_v.setLongitudeRad(lon);
541 void FGInterface::set_Altitude(double alt) {
542 geodetic_position_v.setElevationFt(alt);
545 void FGInterface::set_AltitudeAGL(double altagl) {
550 void FGInterface::set_V_calibrated_kts(double vc) {
551 v_calibrated_kts = vc;
554 void FGInterface::set_Mach_number(double mach) {
558 void FGInterface::set_Velocities_Local( double north,
561 v_local_v[0] = north;
566 void FGInterface::set_Velocities_Wind_Body( double u,
569 v_wind_body_v[0] = u;
570 v_wind_body_v[1] = v;
571 v_wind_body_v[2] = w;
575 void FGInterface::set_Euler_Angles( double phi,
578 euler_angles_v[0] = phi;
579 euler_angles_v[1] = theta;
580 euler_angles_v[2] = psi;
584 void FGInterface::set_Climb_Rate( double roc) {
588 void FGInterface::set_Gamma_vert_rad( double gamma) {
589 gamma_vert_rad = gamma;
592 void FGInterface::set_Static_pressure(double p) { static_pressure = p; }
593 void FGInterface::set_Static_temperature(double T) { static_temperature = T; }
594 void FGInterface::set_Density(double rho) { density = rho; }
596 void FGInterface::set_Velocities_Local_Airmass (double wnorth,
599 v_local_airmass_v[0] = wnorth;
600 v_local_airmass_v[1] = weast;
601 v_local_airmass_v[2] = wdown;
605 void FGInterface::_busdump(void) {
607 SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rp_body_v: " << d_cg_rp_body_v);
608 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_local_v: " << v_dot_local_v);
609 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_body_v: " << v_dot_body_v);
610 SG_LOG(SG_FLIGHT,SG_INFO,"a_cg_body_v: " << a_cg_body_v);
611 SG_LOG(SG_FLIGHT,SG_INFO,"a_pilot_body_v: " << a_pilot_body_v);
612 SG_LOG(SG_FLIGHT,SG_INFO,"n_cg_body_v: " << n_cg_body_v);
613 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v: " << v_local_v);
614 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_rel_ground_v: " << v_local_rel_ground_v);
615 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_airmass_v: " << v_local_airmass_v);
616 SG_LOG(SG_FLIGHT,SG_INFO,"v_wind_body_v: " << v_wind_body_v);
617 SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v: " << omega_body_v);
618 SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v: " << euler_rates_v);
619 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v: " << geocentric_rates_v);
620 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v: " << geocentric_position_v);
621 SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v: " << geodetic_position_v);
622 SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v: " << euler_angles_v);
624 SG_LOG(SG_FLIGHT,SG_INFO,"mass: " << mass );
625 SG_LOG(SG_FLIGHT,SG_INFO,"i_xx: " << i_xx );
626 SG_LOG(SG_FLIGHT,SG_INFO,"i_yy: " << i_yy );
627 SG_LOG(SG_FLIGHT,SG_INFO,"i_zz: " << i_zz );
628 SG_LOG(SG_FLIGHT,SG_INFO,"i_xz: " << i_xz );
629 SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << nlf );
630 SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << v_rel_wind );
631 SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << v_true_kts );
632 SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << v_ground_speed );
633 SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << v_equiv_kts );
634 SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << v_calibrated_kts );
635 SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << alpha );
636 SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << beta );
637 SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << gamma_vert_rad );
638 SG_LOG(SG_FLIGHT,SG_INFO,"density: " << density );
639 SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << mach_number );
640 SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << static_pressure );
641 SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << total_pressure );
642 SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << dynamic_pressure );
643 SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << static_temperature );
644 SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << total_temperature );
645 SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << sea_level_radius );
646 SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << earth_position_angle );
647 SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << runway_altitude );
648 SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << climb_rate );
649 SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << altitude_agl );
653 FGInterface::prepare_ground_cache_m(double ref_time, const double pt[3],
656 return ground_cache.prepare_ground_cache(ref_time, SGVec3d(pt), rad);
659 bool FGInterface::prepare_ground_cache_ft(double ref_time, const double pt[3],
662 // Convert units and do the real work.
663 SGVec3d pt_ft = SG_FEET_TO_METER*SGVec3d(pt);
664 return ground_cache.prepare_ground_cache(ref_time, pt_ft, rad*SG_FEET_TO_METER);
668 FGInterface::is_valid_m(double *ref_time, double pt[3], double *rad)
671 bool valid = ground_cache.is_valid(*ref_time, _pt, *rad);
676 bool FGInterface::is_valid_ft(double *ref_time, double pt[3], double *rad)
678 // Convert units and do the real work.
680 bool found_ground = ground_cache.is_valid(*ref_time, _pt, *rad);
681 assign(pt, SG_METER_TO_FEET*_pt);
682 *rad *= SG_METER_TO_FEET;
687 FGInterface::get_cat_m(double t, const double pt[3],
688 double end[2][3], double vel[2][3])
690 SGVec3d _end[2], _vel[2];
691 double dist = ground_cache.get_cat(t, SGVec3d(pt), _end, _vel);
692 for (int k=0; k<2; ++k) {
693 assign( end[k], _end[k] );
694 assign( vel[k], _vel[k] );
700 FGInterface::get_cat_ft(double t, const double pt[3],
701 double end[2][3], double vel[2][3])
703 // Convert units and do the real work.
704 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
705 SGVec3d _end[2], _vel[2];
706 double dist = ground_cache.get_cat(t, pt_m, _end, _vel);
707 for (int k=0; k<2; ++k) {
708 assign( end[k], SG_METER_TO_FEET*_end[k] );
709 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
711 return dist*SG_METER_TO_FEET;
714 // Legacy interface just kept because of JSBSim
716 FGInterface::get_agl_m(double t, const double pt[3],
717 double contact[3], double normal[3], double vel[3],
718 int *type, double *loadCapacity,
719 double *frictionFactor, double *agl)
721 const SGMaterial* material;
722 SGVec3d _contact, _normal, _vel;
723 bool ret = ground_cache.get_agl(t, SGVec3d(pt), 2.0, _contact, _normal,
724 _vel, type, &material, agl);
725 assign(contact, _contact);
726 assign(normal, _normal);
729 *loadCapacity = material->get_load_resistance();
730 *frictionFactor = material->get_friction_factor();
733 *loadCapacity = DBL_MAX;
734 *frictionFactor = 1.0;
740 FGInterface::get_agl_m(double t, const double pt[3],
741 double contact[3], double normal[3], double vel[3],
742 int *type, const SGMaterial **material, double *agl)
744 SGVec3d _contact, _normal, _vel;
745 bool ret = ground_cache.get_agl(t, SGVec3d(pt), 2.0, _contact, _normal,
746 _vel, type, material, agl);
747 assign(contact, _contact);
748 assign(normal, _normal);
753 // Legacy interface just kept because of JSBSim
755 FGInterface::get_agl_ft(double t, const double pt[3],
756 double contact[3], double normal[3], double vel[3],
757 int *type, double *loadCapacity,
758 double *frictionFactor, double *agl)
760 // Convert units and do the real work.
761 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
763 const SGMaterial* material;
764 SGVec3d _contact, _normal, _vel;
765 bool ret = ground_cache.get_agl(t, pt_m, 2.0, _contact, _normal, _vel,
766 type, &material, agl);
767 // Convert units back ...
768 assign( contact, SG_METER_TO_FEET*_contact );
769 assign( vel, SG_METER_TO_FEET*_vel );
770 assign( normal, _normal );
771 *agl *= SG_METER_TO_FEET;
773 // return material properties if available
775 // FIXME: convert units?? now pascal to lbf/ft^2
776 *loadCapacity = 0.020885434*material->get_load_resistance();
777 *frictionFactor = material->get_friction_factor();
779 *loadCapacity = DBL_MAX;
780 *frictionFactor = 1.0;
786 FGInterface::get_agl_m(double t, const double pt[3], double max_altoff,
787 double contact[3], double normal[3], double vel[3],
788 int *type, const SGMaterial** material, double *agl)
790 SGVec3d _contact, _normal, _vel;
791 bool found = ground_cache.get_agl(t, SGVec3d(pt), max_altoff, _contact,
792 _normal, _vel, type, material, agl);
793 assign(contact, _contact);
794 assign(normal, _normal);
800 FGInterface::get_agl_ft(double t, const double pt[3], double max_altoff,
801 double contact[3], double normal[3], double vel[3],
802 int *type, const SGMaterial** material, double *agl)
804 // Convert units and do the real work.
805 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
806 SGVec3d _contact, _normal, _vel;
807 bool ret = ground_cache.get_agl(t, pt_m, SG_FEET_TO_METER * max_altoff,
808 _contact, _normal, _vel,
809 type, material, agl);
810 // Convert units back ...
811 assign( contact, SG_METER_TO_FEET*_contact );
812 assign( vel, SG_METER_TO_FEET*_vel );
813 assign( normal, _normal );
814 *agl *= SG_METER_TO_FEET;
819 FGInterface::get_groundlevel_m(double lat, double lon, double alt)
821 return get_groundlevel_m(SGGeod::fromRadM(lon, lat, alt));
825 FGInterface::get_groundlevel_m(const SGGeod& geod)
827 // Compute the cartesian position of the given lat/lon/alt.
828 SGVec3d pos = SGVec3d::fromGeod(geod);
830 // FIXME: how to handle t - ref_time differences ???
832 double ref_time, radius;
833 // Prepare the ground cache for that position.
834 if (!is_valid_m(&ref_time, cpos.data(), &radius)) {
835 bool ok = prepare_ground_cache_m(ref_time, pos.data(), 10);
836 /// This is most likely the case when the given altitude is
837 /// too low, try with a new altitude of 10000m, that should be
838 /// sufficient to find a ground level below everywhere on our planet
840 pos = SGVec3d::fromGeod(SGGeod::fromRadM(geod.getLongitudeRad(), geod.getLatitudeRad(), 10000));
841 /// If there is still no ground, return sea level radius
842 if (!prepare_ground_cache_m(ref_time, pos.data(), 10))
845 } else if (radius*radius <= distSqr(pos, cpos)) {
846 /// We reuse the old radius value, but only if it is at least 10 Meters ..
847 if (!(10 < radius)) // Well this strange compare is nan safe
850 bool ok = prepare_ground_cache_m(ref_time, pos.data(), radius);
851 /// This is most likely the case when the given altitude is
852 /// too low, try with a new altitude of 10000m, that should be
853 /// sufficient to find a ground level below everywhere on our planet
855 pos = SGVec3d::fromGeod(SGGeod::fromRadM(geod.getLongitudeRad(), geod.getLatitudeRad(), 10000));
856 /// If there is still no ground, return sea level radius
857 if (!prepare_ground_cache_m(ref_time, pos.data(), radius))
862 double contact[3], normal[3], vel[3], agl;
864 // Ignore the return value here, since it just tells us if
865 // the returns stem from the groundcache or from the coarse
866 // computations below the groundcache. The contact point is still something
867 // valid, the normals and the other returns just contain some defaults.
868 get_agl_m(ref_time, pos.data(), 2.0, contact, normal, vel, &type, 0, &agl);
869 return SGGeod::fromCart(SGVec3d(contact)).getElevationM();
873 FGInterface::caught_wire_m(double t, const double pt[4][3])
876 for (int i=0; i<4; ++i)
877 pt_m[i] = SGVec3d(pt[i]);
879 return ground_cache.caught_wire(t, pt_m);
883 FGInterface::caught_wire_ft(double t, const double pt[4][3])
885 // Convert units and do the real work.
887 for (int i=0; i<4; ++i)
888 pt_m[i] = SG_FEET_TO_METER*SGVec3d(pt[i]);
890 return ground_cache.caught_wire(t, pt_m);
894 FGInterface::get_wire_ends_m(double t, double end[2][3], double vel[2][3])
896 SGVec3d _end[2], _vel[2];
897 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
898 for (int k=0; k<2; ++k) {
899 assign( end[k], _end[k] );
900 assign( vel[k], _vel[k] );
906 FGInterface::get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
908 // Convert units and do the real work.
909 SGVec3d _end[2], _vel[2];
910 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
911 for (int k=0; k<2; ++k) {
912 assign( end[k], SG_METER_TO_FEET*_end[k] );
913 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
919 FGInterface::release_wire(void)
921 ground_cache.release_wire();
924 void fgToggleFDMdataLogging(void) {
925 cur_fdm_state->ToggleDataLogging();