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();
134 v_rel_wind=v_true_kts=0;
135 v_ground_speed=v_equiv_kts=0;
139 density=mach_number=0;
140 static_pressure=total_pressure=0;
142 static_temperature=total_temperature=0;
143 sea_level_radius=earth_position_angle=0;
150 FGInterface::init () {}
153 * Initialize the state of the FDM.
155 * Subclasses of FGInterface may do their own, additional initialization,
156 * but there is some that is common to all. Normally, they should call
157 * this before they begin their own init to make sure the basic structures
158 * are set up properly.
161 FGInterface::common_init ()
163 SG_LOG( SG_FLIGHT, SG_INFO, "Start common FDM init" );
168 // set_remainder( 0 );
170 // Set initial position
171 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing position..." );
172 double lon = fgGetDouble("/sim/presets/longitude-deg")
173 * SGD_DEGREES_TO_RADIANS;
174 double lat = fgGetDouble("/sim/presets/latitude-deg")
175 * SGD_DEGREES_TO_RADIANS;
176 double alt_ft = fgGetDouble("/sim/presets/altitude-ft");
177 double alt_m = alt_ft * SG_FEET_TO_METER;
178 set_Longitude( lon );
180 SG_LOG( SG_FLIGHT, SG_INFO, "Checking for lon = "
181 << lon*SGD_RADIANS_TO_DEGREES << "deg, lat = "
182 << lat*SGD_RADIANS_TO_DEGREES << "deg, alt = "
185 double ground_elev_m = get_groundlevel_m(lat, lon, alt_m);
186 double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET;
187 _set_Runway_altitude ( ground_elev_ft );
188 if ( fgGetBool("/sim/presets/onground") || alt_ft < ground_elev_ft ) {
189 fgSetDouble("/position/altitude-ft", ground_elev_ft + 0.1);
190 set_Altitude( ground_elev_ft + 0.1);
192 set_Altitude( alt_ft );
195 // Set ground elevation
196 SG_LOG( SG_FLIGHT, SG_INFO,
197 "...initializing ground elevation to " << ground_elev_ft
200 // Set sea-level radius
201 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing sea-level radius..." );
202 SG_LOG( SG_FLIGHT, SG_INFO, " lat = "
203 << fgGetDouble("/sim/presets/latitude-deg")
204 << " alt = " << get_Altitude() );
205 double slr = SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v);
206 _set_Sea_level_radius( slr * SG_METER_TO_FEET );
208 // Set initial velocities
209 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing velocities..." );
210 if ( !fgHasNode("/sim/presets/speed-set") ) {
211 set_V_calibrated_kts(0.0);
213 const string speedset = fgGetString("/sim/presets/speed-set");
214 if ( speedset == "knots" || speedset == "KNOTS" ) {
215 set_V_calibrated_kts( fgGetDouble("/sim/presets/airspeed-kt") );
216 } else if ( speedset == "mach" || speedset == "MACH" ) {
217 set_Mach_number( fgGetDouble("/sim/presets/mach") );
218 } else if ( speedset == "UVW" || speedset == "uvw" ) {
219 set_Velocities_Wind_Body(
220 fgGetDouble("/sim/presets/uBody-fps"),
221 fgGetDouble("/sim/presets/vBody-fps"),
222 fgGetDouble("/sim/presets/wBody-fps") );
223 } else if ( speedset == "NED" || speedset == "ned" ) {
224 set_Velocities_Local(
225 fgGetDouble("/sim/presets/speed-north-fps"),
226 fgGetDouble("/sim/presets/speed-east-fps"),
227 fgGetDouble("/sim/presets/speed-down-fps") );
229 SG_LOG( SG_FLIGHT, SG_ALERT,
230 "Unrecognized value for /sim/presets/speed-set: "
232 set_V_calibrated_kts( 0.0 );
236 // Set initial Euler angles
237 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing Euler angles..." );
238 set_Euler_Angles( fgGetDouble("/sim/presets/roll-deg")
239 * SGD_DEGREES_TO_RADIANS,
240 fgGetDouble("/sim/presets/pitch-deg")
241 * SGD_DEGREES_TO_RADIANS,
242 fgGetDouble("/sim/presets/heading-deg")
243 * SGD_DEGREES_TO_RADIANS );
245 SG_LOG( SG_FLIGHT, SG_INFO, "End common FDM init" );
250 * Bind getters and setters to properties.
252 * The bind() method will be invoked after init(). Note that unlike
253 * the usual implementations of FGSubsystem::bind(), this method does
254 * not automatically pick up existing values for the properties at
255 * bind time; instead, all values are set explicitly in the init()
263 // Time management (read-only)
264 // fgTie("/fdm/time/delta_t", this,
265 // &FGInterface::get_delta_t); // read-only
266 // fgTie("/fdm/time/elapsed", this,
267 // &FGInterface::get_elapsed); // read-only
268 // fgTie("/fdm/time/remainder", this,
269 // &FGInterface::get_remainder); // read-only
270 // fgTie("/fdm/time/multi_loop", this,
271 // &FGInterface::get_multi_loop); // read-only
274 fgTie("/position/latitude-deg", this,
275 &FGInterface::get_Latitude_deg,
276 &FGInterface::set_Latitude_deg,
278 fgSetArchivable("/position/latitude-deg");
279 fgTie("/position/longitude-deg", this,
280 &FGInterface::get_Longitude_deg,
281 &FGInterface::set_Longitude_deg,
283 fgSetArchivable("/position/longitude-deg");
284 fgTie("/position/altitude-ft", this,
285 &FGInterface::get_Altitude,
286 &FGInterface::set_Altitude,
288 fgSetArchivable("/position/altitude-ft");
289 fgTie("/position/altitude-agl-ft", this,
290 &FGInterface::get_Altitude_AGL); // read-only
291 fgSetArchivable("/position/ground-elev-ft");
292 fgTie("/position/ground-elev-ft", this,
293 &FGInterface::get_Runway_altitude); // read-only
294 fgSetArchivable("/position/ground-elev-m");
295 fgTie("/position/ground-elev-m", this,
296 &FGInterface::get_Runway_altitude_m); // read-only
297 fgTie("/environment/ground-elevation-m", this,
298 &FGInterface::get_Runway_altitude_m); // read-only
299 fgSetArchivable("/position/sea-level-radius-ft");
300 fgTie("/position/sea-level-radius-ft", this,
301 &FGInterface::get_Sea_level_radius); // read-only
304 fgTie("/orientation/roll-deg", this,
305 &FGInterface::get_Phi_deg,
306 &FGInterface::set_Phi_deg);
307 fgSetArchivable("/orientation/roll-deg");
308 fgTie("/orientation/pitch-deg", this,
309 &FGInterface::get_Theta_deg,
310 &FGInterface::set_Theta_deg);
311 fgSetArchivable("/orientation/pitch-deg");
312 fgTie("/orientation/heading-deg", this,
313 &FGInterface::get_Psi_deg,
314 &FGInterface::set_Psi_deg);
315 fgSetArchivable("/orientation/heading-deg");
317 // Body-axis "euler rates" (rotation speed, but in a funny
319 fgTie("/orientation/roll-rate-degps", this,
320 &FGInterface::get_Phi_dot_degps);
321 fgTie("/orientation/pitch-rate-degps", this,
322 &FGInterface::get_Theta_dot_degps);
323 fgTie("/orientation/yaw-rate-degps", this,
324 &FGInterface::get_Psi_dot_degps);
326 // Ground speed knots
327 fgTie("/velocities/groundspeed-kt", this,
328 &FGInterface::get_V_ground_speed_kt);
330 // Calibrated airspeed
331 fgTie("/velocities/airspeed-kt", this,
332 &FGInterface::get_V_calibrated_kts,
333 &FGInterface::set_V_calibrated_kts,
337 fgTie("/velocities/mach", this,
338 &FGInterface::get_Mach_number,
339 &FGInterface::set_Mach_number,
343 // fgTie("/velocities/speed-north-fps", this,
344 // &FGInterface::get_V_north,
345 // &FGInterface::set_V_north);
346 // fgSetArchivable("/velocities/speed-north-fps");
347 // fgTie("/velocities/speed-east-fps", this,
348 // &FGInterface::get_V_east,
349 // &FGInterface::set_V_east);
350 // fgSetArchivable("/velocities/speed-east-fps");
351 // fgTie("/velocities/speed-down-fps", this,
352 // &FGInterface::get_V_down,
353 // &FGInterface::set_V_down);
354 // fgSetArchivable("/velocities/speed-down-fps");
355 // FIXME: Temporarily read-only, until the
356 // incompatibilities between JSBSim and
357 // LaRCSim are fixed (LaRCSim adds the
358 // earth's rotation to the east velocity).
359 fgTie("/velocities/speed-north-fps", this,
360 &FGInterface::get_V_north);
361 fgTie("/velocities/speed-east-fps", this,
362 &FGInterface::get_V_east);
363 fgTie("/velocities/speed-down-fps", this,
364 &FGInterface::get_V_down);
367 // FIXME: temporarily archivable, until
368 // the NED problem is fixed.
369 fgTie("/velocities/uBody-fps", this,
370 &FGInterface::get_uBody,
371 &FGInterface::set_uBody,
373 fgSetArchivable("/velocities/uBody-fps");
374 fgTie("/velocities/vBody-fps", this,
375 &FGInterface::get_vBody,
376 &FGInterface::set_vBody,
378 fgSetArchivable("/velocities/vBody-fps");
379 fgTie("/velocities/wBody-fps", this,
380 &FGInterface::get_wBody,
381 &FGInterface::set_wBody,
383 fgSetArchivable("/velocities/wBody-fps");
385 // Climb and slip (read-only)
386 fgTie("/velocities/vertical-speed-fps", this,
387 &FGInterface::get_Climb_Rate,
388 &FGInterface::set_Climb_Rate );
389 fgTie("/velocities/glideslope", this,
390 &FGInterface::get_Gamma_vert_rad,
391 &FGInterface::set_Gamma_vert_rad );
392 fgTie("/orientation/side-slip-rad", this,
393 &FGInterface::get_Beta); // read-only
394 fgTie("/orientation/side-slip-deg", this,
395 &FGInterface::get_Beta_deg); // read-only
396 fgTie("/orientation/alpha-deg", this,
397 &FGInterface::get_Alpha_deg); // read-only
398 fgTie("/accelerations/nlf", this,
399 &FGInterface::get_Nlf); // read-only
402 fgTie("/accelerations/ned/north-accel-fps_sec",
403 this, &FGInterface::get_V_dot_north);
404 fgTie("/accelerations/ned/east-accel-fps_sec",
405 this, &FGInterface::get_V_dot_east);
406 fgTie("/accelerations/ned/down-accel-fps_sec",
407 this, &FGInterface::get_V_dot_down);
409 // Pilot accelerations
410 fgTie("/accelerations/pilot/x-accel-fps_sec",
411 this, &FGInterface::get_A_X_pilot);
412 fgTie("/accelerations/pilot/y-accel-fps_sec",
413 this, &FGInterface::get_A_Y_pilot);
414 fgTie("/accelerations/pilot/z-accel-fps_sec",
415 this, &FGInterface::get_A_Z_pilot);
421 * Unbind any properties bound to this FDM.
423 * This method allows the FDM to release properties so that a new
424 * FDM can bind them instead.
427 FGInterface::unbind ()
431 // fgUntie("/fdm/time/delta_t");
432 // fgUntie("/fdm/time/elapsed");
433 // fgUntie("/fdm/time/remainder");
434 // fgUntie("/fdm/time/multi_loop");
435 fgUntie("/position/latitude-deg");
436 fgUntie("/position/longitude-deg");
437 fgUntie("/position/altitude-ft");
438 fgUntie("/position/altitude-agl-ft");
439 fgUntie("/position/ground-elev-ft");
440 fgUntie("/position/ground-elev-m");
441 fgUntie("/environment/ground-elevation-m");
442 fgUntie("/position/sea-level-radius-ft");
443 fgUntie("/orientation/roll-deg");
444 fgUntie("/orientation/pitch-deg");
445 fgUntie("/orientation/heading-deg");
446 fgUntie("/orientation/roll-rate-degps");
447 fgUntie("/orientation/pitch-rate-degps");
448 fgUntie("/orientation/yaw-rate-degps");
449 fgUntie("/orientation/side-slip-rad");
450 fgUntie("/orientation/side-slip-deg");
451 fgUntie("/orientation/alpha-deg");
452 fgUntie("/velocities/airspeed-kt");
453 fgUntie("/velocities/groundspeed-kt");
454 fgUntie("/velocities/mach");
455 fgUntie("/velocities/speed-north-fps");
456 fgUntie("/velocities/speed-east-fps");
457 fgUntie("/velocities/speed-down-fps");
458 fgUntie("/velocities/uBody-fps");
459 fgUntie("/velocities/vBody-fps");
460 fgUntie("/velocities/wBody-fps");
461 fgUntie("/velocities/vertical-speed-fps");
462 fgUntie("/velocities/glideslope");
463 fgUntie("/accelerations/nlf");
464 fgUntie("/accelerations/pilot/x-accel-fps_sec");
465 fgUntie("/accelerations/pilot/y-accel-fps_sec");
466 fgUntie("/accelerations/pilot/z-accel-fps_sec");
467 fgUntie("/accelerations/ned/north-accel-fps_sec");
468 fgUntie("/accelerations/ned/east-accel-fps_sec");
469 fgUntie("/accelerations/ned/down-accel-fps_sec");
473 * Update the state of the FDM (i.e. run the equations of motion).
476 FGInterface::update (double dt)
478 SG_LOG(SG_FLIGHT, SG_ALERT, "dummy update() ... SHOULDN'T BE CALLED!");
482 void FGInterface::_updatePositionM(const SGVec3d& cartPos)
484 cartesian_position_v = cartPos;
485 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
486 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
487 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
488 _update_ground_elev_at_pos();
492 void FGInterface::_updatePosition(const SGGeod& geod)
494 geodetic_position_v = geod;
495 cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
496 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
498 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
499 _update_ground_elev_at_pos();
503 void FGInterface::_updatePosition(const SGGeoc& geoc)
505 geocentric_position_v = geoc;
506 cartesian_position_v = SGVec3d::fromGeoc(geocentric_position_v);
507 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
509 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
510 _update_ground_elev_at_pos();
514 void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
516 _updatePosition(SGGeod::fromRadFt(lon, lat, alt));
520 void FGInterface::_updateGeocentricPosition( double lat, double lon,
523 _updatePosition(SGGeoc::fromRadFt(lon, lat, get_Sea_level_radius() + alt));
526 void FGInterface::_update_ground_elev_at_pos( void ) {
527 double groundlevel_m = get_groundlevel_m(geodetic_position_v);
528 _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
532 void FGInterface::set_Latitude(double lat) {
533 geodetic_position_v.setLatitudeRad(lat);
536 void FGInterface::set_Longitude(double lon) {
537 geodetic_position_v.setLongitudeRad(lon);
540 void FGInterface::set_Altitude(double alt) {
541 geodetic_position_v.setElevationFt(alt);
544 void FGInterface::set_AltitudeAGL(double altagl) {
549 void FGInterface::set_V_calibrated_kts(double vc) {
550 v_calibrated_kts = vc;
553 void FGInterface::set_Mach_number(double mach) {
557 void FGInterface::set_Velocities_Local( double north,
560 v_local_v[0] = north;
565 void FGInterface::set_Velocities_Wind_Body( double u,
568 v_wind_body_v[0] = u;
569 v_wind_body_v[1] = v;
570 v_wind_body_v[2] = w;
574 void FGInterface::set_Euler_Angles( double phi,
577 euler_angles_v[0] = phi;
578 euler_angles_v[1] = theta;
579 euler_angles_v[2] = psi;
583 void FGInterface::set_Climb_Rate( double roc) {
587 void FGInterface::set_Gamma_vert_rad( double gamma) {
588 gamma_vert_rad = gamma;
591 void FGInterface::set_Static_pressure(double p) { static_pressure = p; }
592 void FGInterface::set_Static_temperature(double T) { static_temperature = T; }
593 void FGInterface::set_Density(double rho) { density = rho; }
595 void FGInterface::set_Velocities_Local_Airmass (double wnorth,
598 v_local_airmass_v[0] = wnorth;
599 v_local_airmass_v[1] = weast;
600 v_local_airmass_v[2] = wdown;
604 void FGInterface::_busdump(void) {
606 SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rp_body_v: " << d_cg_rp_body_v);
607 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_local_v: " << v_dot_local_v);
608 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_body_v: " << v_dot_body_v);
609 SG_LOG(SG_FLIGHT,SG_INFO,"a_cg_body_v: " << a_cg_body_v);
610 SG_LOG(SG_FLIGHT,SG_INFO,"a_pilot_body_v: " << a_pilot_body_v);
611 SG_LOG(SG_FLIGHT,SG_INFO,"n_cg_body_v: " << n_cg_body_v);
612 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v: " << v_local_v);
613 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_rel_ground_v: " << v_local_rel_ground_v);
614 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_airmass_v: " << v_local_airmass_v);
615 SG_LOG(SG_FLIGHT,SG_INFO,"v_wind_body_v: " << v_wind_body_v);
616 SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v: " << omega_body_v);
617 SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v: " << euler_rates_v);
618 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v: " << geocentric_rates_v);
619 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v: " << geocentric_position_v);
620 SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v: " << geodetic_position_v);
621 SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v: " << euler_angles_v);
623 SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << nlf );
624 SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << v_rel_wind );
625 SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << v_true_kts );
626 SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << v_ground_speed );
627 SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << v_equiv_kts );
628 SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << v_calibrated_kts );
629 SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << alpha );
630 SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << beta );
631 SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << gamma_vert_rad );
632 SG_LOG(SG_FLIGHT,SG_INFO,"density: " << density );
633 SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << mach_number );
634 SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << static_pressure );
635 SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << total_pressure );
636 SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << dynamic_pressure );
637 SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << static_temperature );
638 SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << total_temperature );
639 SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << sea_level_radius );
640 SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << earth_position_angle );
641 SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << runway_altitude );
642 SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << climb_rate );
643 SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << altitude_agl );
647 FGInterface::prepare_ground_cache_m(double ref_time, const double pt[3],
650 return ground_cache.prepare_ground_cache(ref_time, SGVec3d(pt), rad);
653 bool FGInterface::prepare_ground_cache_ft(double ref_time, const double pt[3],
656 // Convert units and do the real work.
657 SGVec3d pt_ft = SG_FEET_TO_METER*SGVec3d(pt);
658 return ground_cache.prepare_ground_cache(ref_time, pt_ft, rad*SG_FEET_TO_METER);
662 FGInterface::is_valid_m(double *ref_time, double pt[3], double *rad)
665 bool valid = ground_cache.is_valid(*ref_time, _pt, *rad);
670 bool FGInterface::is_valid_ft(double *ref_time, double pt[3], double *rad)
672 // Convert units and do the real work.
674 bool found_ground = ground_cache.is_valid(*ref_time, _pt, *rad);
675 assign(pt, SG_METER_TO_FEET*_pt);
676 *rad *= SG_METER_TO_FEET;
681 FGInterface::get_cat_m(double t, const double pt[3],
682 double end[2][3], double vel[2][3])
684 SGVec3d _end[2], _vel[2];
685 double dist = ground_cache.get_cat(t, SGVec3d(pt), _end, _vel);
686 for (int k=0; k<2; ++k) {
687 assign( end[k], _end[k] );
688 assign( vel[k], _vel[k] );
694 FGInterface::get_cat_ft(double t, const double pt[3],
695 double end[2][3], double vel[2][3])
697 // Convert units and do the real work.
698 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
699 SGVec3d _end[2], _vel[2];
700 double dist = ground_cache.get_cat(t, pt_m, _end, _vel);
701 for (int k=0; k<2; ++k) {
702 assign( end[k], SG_METER_TO_FEET*_end[k] );
703 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
705 return dist*SG_METER_TO_FEET;
708 // Legacy interface just kept because of JSBSim
710 FGInterface::get_agl_m(double t, const double pt[3],
711 double contact[3], double normal[3], double vel[3],
712 int *type, double *loadCapacity,
713 double *frictionFactor, double *agl)
715 const SGMaterial* material;
716 SGVec3d _contact, _normal, _vel;
717 bool ret = ground_cache.get_agl(t, SGVec3d(pt), 2.0, _contact, _normal,
718 _vel, type, &material, agl);
719 assign(contact, _contact);
720 assign(normal, _normal);
723 *loadCapacity = material->get_load_resistance();
724 *frictionFactor = material->get_friction_factor();
727 *loadCapacity = DBL_MAX;
728 *frictionFactor = 1.0;
734 FGInterface::get_agl_m(double t, const double pt[3],
735 double contact[3], double normal[3], double vel[3],
736 int *type, const SGMaterial **material, double *agl)
738 SGVec3d _contact, _normal, _vel;
739 bool ret = ground_cache.get_agl(t, SGVec3d(pt), 2.0, _contact, _normal,
740 _vel, type, material, agl);
741 assign(contact, _contact);
742 assign(normal, _normal);
747 // Legacy interface just kept because of JSBSim
749 FGInterface::get_agl_ft(double t, const double pt[3],
750 double contact[3], double normal[3], double vel[3],
751 int *type, double *loadCapacity,
752 double *frictionFactor, double *agl)
754 // Convert units and do the real work.
755 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
757 const SGMaterial* material;
758 SGVec3d _contact, _normal, _vel;
759 bool ret = ground_cache.get_agl(t, pt_m, 2.0, _contact, _normal, _vel,
760 type, &material, agl);
761 // Convert units back ...
762 assign( contact, SG_METER_TO_FEET*_contact );
763 assign( vel, SG_METER_TO_FEET*_vel );
764 assign( normal, _normal );
765 *agl *= SG_METER_TO_FEET;
767 // return material properties if available
769 // FIXME: convert units?? now pascal to lbf/ft^2
770 *loadCapacity = 0.020885434*material->get_load_resistance();
771 *frictionFactor = material->get_friction_factor();
773 *loadCapacity = DBL_MAX;
774 *frictionFactor = 1.0;
780 FGInterface::get_agl_m(double t, const double pt[3], double max_altoff,
781 double contact[3], double normal[3], double vel[3],
782 int *type, const SGMaterial** material, double *agl)
784 SGVec3d _contact, _normal, _vel;
785 bool found = ground_cache.get_agl(t, SGVec3d(pt), max_altoff, _contact,
786 _normal, _vel, type, material, agl);
787 assign(contact, _contact);
788 assign(normal, _normal);
794 FGInterface::get_agl_ft(double t, const double pt[3], double max_altoff,
795 double contact[3], double normal[3], double vel[3],
796 int *type, const SGMaterial** material, double *agl)
798 // Convert units and do the real work.
799 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
800 SGVec3d _contact, _normal, _vel;
801 bool ret = ground_cache.get_agl(t, pt_m, SG_FEET_TO_METER * max_altoff,
802 _contact, _normal, _vel,
803 type, material, agl);
804 // Convert units back ...
805 assign( contact, SG_METER_TO_FEET*_contact );
806 assign( vel, SG_METER_TO_FEET*_vel );
807 assign( normal, _normal );
808 *agl *= SG_METER_TO_FEET;
813 FGInterface::get_groundlevel_m(double lat, double lon, double alt)
815 return get_groundlevel_m(SGGeod::fromRadM(lon, lat, alt));
819 FGInterface::get_groundlevel_m(const SGGeod& geod)
821 // Compute the cartesian position of the given lat/lon/alt.
822 SGVec3d pos = SGVec3d::fromGeod(geod);
824 // FIXME: how to handle t - ref_time differences ???
826 double ref_time, radius;
827 // Prepare the ground cache for that position.
828 if (!is_valid_m(&ref_time, cpos.data(), &radius)) {
829 bool ok = prepare_ground_cache_m(ref_time, pos.data(), 10);
830 /// This is most likely the case when the given altitude is
831 /// too low, try with a new altitude of 10000m, that should be
832 /// sufficient to find a ground level below everywhere on our planet
834 pos = SGVec3d::fromGeod(SGGeod::fromRadM(geod.getLongitudeRad(), geod.getLatitudeRad(), 10000));
835 /// If there is still no ground, return sea level radius
836 if (!prepare_ground_cache_m(ref_time, pos.data(), 10))
839 } else if (radius*radius <= distSqr(pos, cpos)) {
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(ref_time, 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::fromRadM(geod.getLongitudeRad(), geod.getLatitudeRad(), 10000));
850 /// If there is still no ground, return sea level radius
851 if (!prepare_ground_cache_m(ref_time, pos.data(), radius))
856 double contact[3], normal[3], vel[3], agl;
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, &type, 0, &agl);
863 return SGGeod::fromCart(SGVec3d(contact)).getElevationM();
867 FGInterface::caught_wire_m(double t, const double pt[4][3])
870 for (int i=0; i<4; ++i)
871 pt_m[i] = SGVec3d(pt[i]);
873 return ground_cache.caught_wire(t, pt_m);
877 FGInterface::caught_wire_ft(double t, const double pt[4][3])
879 // Convert units and do the real work.
881 for (int i=0; i<4; ++i)
882 pt_m[i] = SG_FEET_TO_METER*SGVec3d(pt[i]);
884 return ground_cache.caught_wire(t, pt_m);
888 FGInterface::get_wire_ends_m(double t, double end[2][3], double vel[2][3])
890 SGVec3d _end[2], _vel[2];
891 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
892 for (int k=0; k<2; ++k) {
893 assign( end[k], _end[k] );
894 assign( vel[k], _vel[k] );
900 FGInterface::get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
902 // Convert units and do the real work.
903 SGVec3d _end[2], _vel[2];
904 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
905 for (int k=0; k<2; ++k) {
906 assign( end[k], SG_METER_TO_FEET*_end[k] );
907 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
913 FGInterface::release_wire(void)
915 ground_cache.release_wire();
918 void fgToggleFDMdataLogging(void) {
919 cur_fdm_state->ToggleDataLogging();