1 // flight.cxx -- a general interface to the various flight models
3 // Written by Curtis Olson, started May 1997.
5 // Copyright (C) 1997 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <simgear/constants.h>
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/math/SGMath.hxx>
32 #include <simgear/timing/timestamp.hxx>
34 #include <Scenery/scenery.hxx>
35 #include <Main/globals.hxx>
36 #include <Main/fg_props.hxx>
37 #include <FDM/groundcache.hxx>
40 static inline void assign(double* ptr, const SGVec3d& vec)
48 FGInterface::FGInterface()
53 FGInterface::FGInterface( double dt )
59 FGInterface::~FGInterface() {
60 // unbind(); // FIXME: should be called explicitly
64 FGInterface::_calc_multiloop (double dt)
66 // Since some time the simulation time increments we get here are
67 // already a multiple of the basic update freqency.
68 // So, there is no need to do our own multiloop rounding with all bad
69 // roundoff problems when we already have nearly accurate values.
70 // Only the speedup thing must be still handled here
71 int hz = fgGetInt("/sim/model-hz");
72 int multiloop = SGMiscd::roundToInt(dt*hz);
73 int speedup = fgGetInt("/sim/speed-up");
74 return multiloop * speedup;
79 * Set default values for the state of the FDM.
81 * This method is invoked by the constructors.
84 FGInterface::_setup ()
89 d_cg_rp_body_v = SGVec3d::zeros();
90 v_dot_local_v = SGVec3d::zeros();
91 v_dot_body_v = SGVec3d::zeros();
92 a_cg_body_v = SGVec3d::zeros();
93 a_pilot_body_v = SGVec3d::zeros();
94 n_cg_body_v = SGVec3d::zeros();
95 v_local_v = SGVec3d::zeros();
96 v_local_rel_ground_v = SGVec3d::zeros();
97 v_local_airmass_v = SGVec3d::zeros();
98 v_wind_body_v = SGVec3d::zeros();
99 omega_body_v = SGVec3d::zeros();
100 euler_rates_v = SGVec3d::zeros();
101 geocentric_rates_v = SGVec3d::zeros();
102 geodetic_position_v = SGGeod::fromRadM(0, 0, 0);
103 cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
104 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
105 euler_angles_v = SGVec3d::zeros();
108 v_rel_wind=v_true_kts=0;
109 v_ground_speed=v_equiv_kts=0;
113 density=mach_number=0;
114 static_pressure=total_pressure=0;
116 static_temperature=total_temperature=0;
117 sea_level_radius=earth_position_angle=0;
125 FGInterface::init () {}
128 * Initialize the state of the FDM.
130 * Subclasses of FGInterface may do their own, additional initialization,
131 * but there is some that is common to all. Normally, they should call
132 * this before they begin their own init to make sure the basic structures
133 * are set up properly.
136 FGInterface::common_init ()
138 SG_LOG( SG_FLIGHT, SG_INFO, "Start common FDM init" );
142 ground_cache.set_cache_time_offset(globals->get_sim_time_sec());
144 // Set initial position
145 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing position..." );
146 double lon = fgGetDouble("/sim/presets/longitude-deg")
147 * SGD_DEGREES_TO_RADIANS;
148 double lat = fgGetDouble("/sim/presets/latitude-deg")
149 * SGD_DEGREES_TO_RADIANS;
150 double alt_ft = fgGetDouble("/sim/presets/altitude-ft");
151 double alt_m = alt_ft * SG_FEET_TO_METER;
152 set_Longitude( lon );
154 SG_LOG( SG_FLIGHT, SG_INFO, "Checking for lon = "
155 << lon*SGD_RADIANS_TO_DEGREES << "deg, lat = "
156 << lat*SGD_RADIANS_TO_DEGREES << "deg, alt = "
159 double ground_elev_m = get_groundlevel_m(lat, lon, alt_m);
160 double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET;
161 _set_Runway_altitude ( ground_elev_ft );
162 if ( fgGetBool("/sim/presets/onground") || alt_ft < ground_elev_ft ) {
163 fgSetDouble("/position/altitude-ft", ground_elev_ft + 0.1);
164 set_Altitude( ground_elev_ft + 0.1);
166 set_Altitude( alt_ft );
169 // Set ground elevation
170 SG_LOG( SG_FLIGHT, SG_INFO,
171 "...initializing ground elevation to " << ground_elev_ft
174 // Set sea-level radius
175 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing sea-level radius..." );
176 SG_LOG( SG_FLIGHT, SG_INFO, " lat = "
177 << fgGetDouble("/sim/presets/latitude-deg")
178 << " alt = " << get_Altitude() );
179 double slr = SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v);
180 _set_Sea_level_radius( slr * SG_METER_TO_FEET );
182 // Set initial Euler angles
183 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing Euler angles..." );
184 set_Euler_Angles( fgGetDouble("/sim/presets/roll-deg")
185 * SGD_DEGREES_TO_RADIANS,
186 fgGetDouble("/sim/presets/pitch-deg")
187 * SGD_DEGREES_TO_RADIANS,
188 fgGetDouble("/sim/presets/heading-deg")
189 * SGD_DEGREES_TO_RADIANS );
191 // Set initial velocities
192 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing velocities..." );
193 if ( !fgHasNode("/sim/presets/speed-set") ) {
194 set_V_calibrated_kts(0.0);
196 const string speedset = fgGetString("/sim/presets/speed-set");
197 if ( speedset == "knots" || speedset == "KNOTS" ) {
198 set_V_calibrated_kts( fgGetDouble("/sim/presets/airspeed-kt") );
199 } else if ( speedset == "mach" || speedset == "MACH" ) {
200 set_Mach_number( fgGetDouble("/sim/presets/mach") );
201 } else if ( speedset == "UVW" || speedset == "uvw" ) {
202 set_Velocities_Wind_Body(
203 fgGetDouble("/sim/presets/uBody-fps"),
204 fgGetDouble("/sim/presets/vBody-fps"),
205 fgGetDouble("/sim/presets/wBody-fps") );
206 } else if ( speedset == "NED" || speedset == "ned" ) {
207 set_Velocities_Local(
208 fgGetDouble("/sim/presets/speed-north-fps"),
209 fgGetDouble("/sim/presets/speed-east-fps"),
210 fgGetDouble("/sim/presets/speed-down-fps") );
212 SG_LOG( SG_FLIGHT, SG_ALERT,
213 "Unrecognized value for /sim/presets/speed-set: "
215 set_V_calibrated_kts( 0.0 );
219 if ( fgHasNode("/sim/presets/glideslope-deg") )
220 set_Gamma_vert_rad( fgGetDouble("/sim/presets/glideslope-deg")
221 * SGD_DEGREES_TO_RADIANS );
222 else if ( fgHasNode( "/velocities/vertical-speed-fps") )
223 set_Climb_Rate( fgGetDouble("/velocities/vertical-speed-fps") );
225 SG_LOG( SG_FLIGHT, SG_INFO, "End common FDM init" );
230 * Bind getters and setters to properties.
232 * The bind() method will be invoked after init(). Note that unlike
233 * the usual implementations of FGSubsystem::bind(), this method does
234 * not automatically pick up existing values for the properties at
235 * bind time; instead, all values are set explicitly in the init()
243 _tiedProperties.setRoot(globals->get_props());
245 _tiedProperties.Tie("/position/latitude-deg", this,
246 &FGInterface::get_Latitude_deg,
247 &FGInterface::set_Latitude_deg,
249 fgSetArchivable("/position/latitude-deg");
250 _tiedProperties.Tie("/position/longitude-deg", this,
251 &FGInterface::get_Longitude_deg,
252 &FGInterface::set_Longitude_deg,
254 fgSetArchivable("/position/longitude-deg");
255 _tiedProperties.Tie("/position/altitude-ft", this,
256 &FGInterface::get_Altitude,
257 &FGInterface::set_Altitude,
259 fgSetArchivable("/position/altitude-ft");
260 _tiedProperties.Tie("/position/altitude-agl-ft", this,
261 &FGInterface::get_Altitude_AGL, &FGInterface::set_AltitudeAGL, false);
262 fgSetArchivable("/position/ground-elev-ft");
263 _tiedProperties.Tie("/position/ground-elev-ft", this,
264 &FGInterface::get_Runway_altitude); // read-only
265 fgSetArchivable("/position/ground-elev-m");
266 _tiedProperties.Tie("/position/ground-elev-m", this,
267 &FGInterface::get_Runway_altitude_m); // read-only
268 _tiedProperties.Tie("/environment/ground-elevation-m", this,
269 &FGInterface::get_Runway_altitude_m); // read-only
270 fgSetArchivable("/position/sea-level-radius-ft");
271 _tiedProperties.Tie("/position/sea-level-radius-ft", this,
272 &FGInterface::get_Sea_level_radius,
273 &FGInterface::_set_Sea_level_radius, false);
276 _tiedProperties.Tie("/orientation/roll-deg", this,
277 &FGInterface::get_Phi_deg,
278 &FGInterface::set_Phi_deg, false);
279 fgSetArchivable("/orientation/roll-deg");
280 _tiedProperties.Tie("/orientation/pitch-deg", this,
281 &FGInterface::get_Theta_deg,
282 &FGInterface::set_Theta_deg, false);
283 fgSetArchivable("/orientation/pitch-deg");
284 _tiedProperties.Tie("/orientation/heading-deg", this,
285 &FGInterface::get_Psi_deg,
286 &FGInterface::set_Psi_deg, false);
287 fgSetArchivable("/orientation/heading-deg");
288 _tiedProperties.Tie("/orientation/track-deg", this,
289 &FGInterface::get_Track); // read-only
291 // Body-axis "euler rates" (rotation speed, but in a funny
293 _tiedProperties.Tie("/orientation/roll-rate-degps", this,
294 &FGInterface::get_Phi_dot_degps,
295 &FGInterface::set_Phi_dot_degps, false);
296 _tiedProperties.Tie("/orientation/pitch-rate-degps", this,
297 &FGInterface::get_Theta_dot_degps,
298 &FGInterface::set_Theta_dot_degps, false);
299 _tiedProperties.Tie("/orientation/yaw-rate-degps", this,
300 &FGInterface::get_Psi_dot_degps,
301 &FGInterface::set_Psi_dot_degps, false);
303 _tiedProperties.Tie("/orientation/p-body", this, &FGInterface::get_P_body); // read-only
304 _tiedProperties.Tie("/orientation/q-body", this, &FGInterface::get_Q_body); // read-only
305 _tiedProperties.Tie("/orientation/r-body", this, &FGInterface::get_R_body); // read-only
307 // Ground speed knots
308 _tiedProperties.Tie("/velocities/groundspeed-kt", this,
309 &FGInterface::get_V_ground_speed_kt,
310 &FGInterface::set_V_ground_speed_kt); // read-only
312 // Calibrated airspeed
313 _tiedProperties.Tie("/velocities/airspeed-kt", this,
314 &FGInterface::get_V_calibrated_kts,
315 &FGInterface::set_V_calibrated_kts,
318 _tiedProperties.Tie("/velocities/equivalent-kt", this,
319 &FGInterface::get_V_equiv_kts); // read-only
322 _tiedProperties.Tie("/velocities/mach", this,
323 &FGInterface::get_Mach_number,
324 &FGInterface::set_Mach_number,
328 // _tiedProperties.Tie("/velocities/speed-north-fps", this,
329 // &FGInterface::get_V_north,
330 // &FGInterface::set_V_north);
331 // fgSetArchivable("/velocities/speed-north-fps");
332 // _tiedProperties.Tie("/velocities/speed-east-fps", this,
333 // &FGInterface::get_V_east,
334 // &FGInterface::set_V_east);
335 // fgSetArchivable("/velocities/speed-east-fps");
336 // _tiedProperties.Tie("/velocities/speed-down-fps", this,
337 // &FGInterface::get_V_down,
338 // &FGInterface::set_V_down);
339 // fgSetArchivable("/velocities/speed-down-fps");
341 // FIXME: Temporarily read-only, until the
342 // incompatibilities between JSBSim and
343 // LaRCSim are fixed (LaRCSim adds the
344 // earth's rotation to the east velocity).
345 _tiedProperties.Tie("/velocities/speed-north-fps", this,
346 &FGInterface::get_V_north, &FGInterface::set_V_north, false);
347 _tiedProperties.Tie("/velocities/speed-east-fps", this,
348 &FGInterface::get_V_east, &FGInterface::set_V_east, false);
349 _tiedProperties.Tie("/velocities/speed-down-fps", this,
350 &FGInterface::get_V_down, &FGInterface::set_V_down, false);
352 _tiedProperties.Tie("/velocities/north-relground-fps", this,
353 &FGInterface::get_V_north_rel_ground); // read-only
354 _tiedProperties.Tie("/velocities/east-relground-fps", this,
355 &FGInterface::get_V_east_rel_ground); // read-only
356 _tiedProperties.Tie("/velocities/down-relground-fps", this,
357 &FGInterface::get_V_down_rel_ground); // read-only
360 // FIXME: temporarily archivable, until the NED problem is fixed.
361 _tiedProperties.Tie("/velocities/uBody-fps", this,
362 &FGInterface::get_uBody,
363 &FGInterface::set_uBody,
365 fgSetArchivable("/velocities/uBody-fps");
366 _tiedProperties.Tie("/velocities/vBody-fps", this,
367 &FGInterface::get_vBody,
368 &FGInterface::set_vBody,
370 fgSetArchivable("/velocities/vBody-fps");
371 _tiedProperties.Tie("/velocities/wBody-fps", this,
372 &FGInterface::get_wBody,
373 &FGInterface::set_wBody,
375 fgSetArchivable("/velocities/wBody-fps");
377 // Climb and slip (read-only)
378 _tiedProperties.Tie("/velocities/vertical-speed-fps", this,
379 &FGInterface::get_Climb_Rate,
380 &FGInterface::set_Climb_Rate, false );
381 _tiedProperties.Tie("/velocities/glideslope", this,
382 &FGInterface::get_Gamma_vert_rad,
383 &FGInterface::set_Gamma_vert_rad, false );
384 _tiedProperties.Tie("/orientation/side-slip-rad", this,
385 &FGInterface::get_Beta, &FGInterface::_set_Beta, false);
386 _tiedProperties.Tie("/orientation/side-slip-deg", this,
387 &FGInterface::get_Beta_deg); // read-only
388 _tiedProperties.Tie("/orientation/alpha-deg", this,
389 &FGInterface::get_Alpha_deg, &FGInterface::set_Alpha_deg, false);
390 _tiedProperties.Tie("/accelerations/nlf", this,
391 &FGInterface::get_Nlf); // read-only
394 _tiedProperties.Tie("/accelerations/ned/north-accel-fps_sec",
395 this, &FGInterface::get_V_dot_north); // read-only
396 _tiedProperties.Tie("/accelerations/ned/east-accel-fps_sec",
397 this, &FGInterface::get_V_dot_east); // read-only
398 _tiedProperties.Tie("/accelerations/ned/down-accel-fps_sec",
399 this, &FGInterface::get_V_dot_down); // read-only
401 // Pilot accelerations
402 _tiedProperties.Tie("/accelerations/pilot/x-accel-fps_sec",
403 this, &FGInterface::get_A_X_pilot, &FGInterface::set_A_X_pilot, false);
404 _tiedProperties.Tie("/accelerations/pilot/y-accel-fps_sec",
405 this, &FGInterface::get_A_Y_pilot, &FGInterface::set_A_Y_pilot, false);
406 _tiedProperties.Tie("/accelerations/pilot/z-accel-fps_sec",
407 this, &FGInterface::get_A_Z_pilot, &FGInterface::set_A_Z_pilot, false);
409 _tiedProperties.Tie("/accelerations/n-z-cg-fps_sec",
410 this, &FGInterface::get_N_Z_cg); // read-only
415 * Unbind any properties bound to this FDM.
417 * This method allows the FDM to release properties so that a new
418 * FDM can bind them instead.
421 FGInterface::unbind ()
423 _tiedProperties.Untie();
428 * Update the state of the FDM (i.e. run the equations of motion).
431 FGInterface::update (double dt)
433 SG_LOG(SG_FLIGHT, SG_ALERT, "dummy update() ... SHOULDN'T BE CALLED!");
437 void FGInterface::_updatePositionM(const SGVec3d& cartPos)
439 TrackComputer tracker( track, geodetic_position_v );
440 cartesian_position_v = cartPos;
441 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
442 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
443 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
444 _update_ground_elev_at_pos();
448 void FGInterface::_updatePosition(const SGGeod& geod)
450 TrackComputer tracker( track, geodetic_position_v );
451 geodetic_position_v = geod;
452 cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
453 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
455 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
456 _update_ground_elev_at_pos();
460 void FGInterface::_updatePosition(const SGGeoc& geoc)
462 TrackComputer tracker( track, geodetic_position_v );
463 geocentric_position_v = geoc;
464 cartesian_position_v = SGVec3d::fromGeoc(geocentric_position_v);
465 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
467 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
468 _update_ground_elev_at_pos();
472 void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
474 _updatePosition(SGGeod::fromRadFt(lon, lat, alt));
478 void FGInterface::_updateGeocentricPosition( double lat, double lon,
481 _updatePosition(SGGeoc::fromRadFt(lon, lat, get_Sea_level_radius() + alt));
484 void FGInterface::_update_ground_elev_at_pos( void ) {
485 double groundlevel_m = get_groundlevel_m(geodetic_position_v);
486 _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
490 void FGInterface::set_Latitude(double lat) {
491 geodetic_position_v.setLatitudeRad(lat);
494 void FGInterface::set_Longitude(double lon) {
495 geodetic_position_v.setLongitudeRad(lon);
498 void FGInterface::set_Altitude(double alt) {
499 geodetic_position_v.setElevationFt(alt);
502 void FGInterface::set_AltitudeAGL(double altagl) {
507 void FGInterface::set_V_calibrated_kts(double vc) {
508 v_calibrated_kts = vc;
511 void FGInterface::set_Mach_number(double mach) {
515 void FGInterface::set_Velocities_Local( double north,
518 v_local_v[0] = north;
523 void FGInterface::set_Velocities_Wind_Body( double u,
526 v_wind_body_v[0] = u;
527 v_wind_body_v[1] = v;
528 v_wind_body_v[2] = w;
532 void FGInterface::set_Euler_Angles( double phi,
535 euler_angles_v[0] = phi;
536 euler_angles_v[1] = theta;
537 euler_angles_v[2] = psi;
541 void FGInterface::set_Climb_Rate( double roc) {
545 void FGInterface::set_Gamma_vert_rad( double gamma) {
546 gamma_vert_rad = gamma;
549 void FGInterface::set_Static_pressure(double p) { static_pressure = p; }
550 void FGInterface::set_Static_temperature(double T) { static_temperature = T; }
551 void FGInterface::set_Density(double rho) { density = rho; }
553 void FGInterface::set_Velocities_Local_Airmass (double wnorth,
556 v_local_airmass_v[0] = wnorth;
557 v_local_airmass_v[1] = weast;
558 v_local_airmass_v[2] = wdown;
562 void FGInterface::_busdump(void)
564 SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rp_body_v: " << d_cg_rp_body_v);
565 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_local_v: " << v_dot_local_v);
566 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_body_v: " << v_dot_body_v);
567 SG_LOG(SG_FLIGHT,SG_INFO,"a_cg_body_v: " << a_cg_body_v);
568 SG_LOG(SG_FLIGHT,SG_INFO,"a_pilot_body_v: " << a_pilot_body_v);
569 SG_LOG(SG_FLIGHT,SG_INFO,"n_cg_body_v: " << n_cg_body_v);
570 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v: " << v_local_v);
571 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_rel_ground_v: " << v_local_rel_ground_v);
572 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_airmass_v: " << v_local_airmass_v);
573 SG_LOG(SG_FLIGHT,SG_INFO,"v_wind_body_v: " << v_wind_body_v);
574 SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v: " << omega_body_v);
575 SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v: " << euler_rates_v);
576 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v: " << geocentric_rates_v);
577 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v: " << geocentric_position_v);
578 SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v: " << geodetic_position_v);
579 SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v: " << euler_angles_v);
581 SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << nlf );
582 SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << v_rel_wind );
583 SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << v_true_kts );
584 SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << v_ground_speed );
585 SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << v_equiv_kts );
586 SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << v_calibrated_kts );
587 SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << alpha );
588 SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << beta );
589 SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << gamma_vert_rad );
590 SG_LOG(SG_FLIGHT,SG_INFO,"density: " << density );
591 SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << mach_number );
592 SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << static_pressure );
593 SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << total_pressure );
594 SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << dynamic_pressure );
595 SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << static_temperature );
596 SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << total_temperature );
597 SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << sea_level_radius );
598 SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << earth_position_angle );
599 SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << runway_altitude );
600 SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << climb_rate );
601 SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << altitude_agl );
605 FGInterface::prepare_ground_cache_m(double startSimTime, double endSimTime,
606 const double pt[3], double rad)
608 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
613 FGInterface::prepare_ground_cache_ft(double startSimTime, double endSimTime,
614 const double pt[3], double rad)
616 // Convert units and do the real work.
617 SGVec3d pt_ft = SG_FEET_TO_METER*SGVec3d(pt);
618 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
619 pt_ft, rad*SG_FEET_TO_METER);
623 FGInterface::is_valid_m(double *ref_time, double pt[3], double *rad)
626 bool valid = ground_cache.is_valid(*ref_time, _pt, *rad);
631 bool FGInterface::is_valid_ft(double *ref_time, double pt[3], double *rad)
633 // Convert units and do the real work.
635 bool found_ground = ground_cache.is_valid(*ref_time, _pt, *rad);
636 assign(pt, SG_METER_TO_FEET*_pt);
637 *rad *= SG_METER_TO_FEET;
642 FGInterface::get_cat_m(double t, const double pt[3],
643 double end[2][3], double vel[2][3])
645 SGVec3d _end[2], _vel[2];
646 double dist = ground_cache.get_cat(t, SGVec3d(pt), _end, _vel);
647 for (int k=0; k<2; ++k) {
648 assign( end[k], _end[k] );
649 assign( vel[k], _vel[k] );
655 FGInterface::get_cat_ft(double t, const double pt[3],
656 double end[2][3], double vel[2][3])
658 // Convert units and do the real work.
659 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
660 SGVec3d _end[2], _vel[2];
661 double dist = ground_cache.get_cat(t, pt_m, _end, _vel);
662 for (int k=0; k<2; ++k) {
663 assign( end[k], SG_METER_TO_FEET*_end[k] );
664 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
666 return dist*SG_METER_TO_FEET;
670 FGInterface::get_body_m(double t, simgear::BVHNode::Id id,
671 double bodyToWorld[16], double linearVel[3],
672 double angularVel[3])
674 SGMatrixd _bodyToWorld;
675 SGVec3d _linearVel, _angularVel;
676 if (!ground_cache.get_body(t, _bodyToWorld, _linearVel, _angularVel, id))
679 assign(linearVel, _linearVel);
680 assign(angularVel, _angularVel);
681 for (unsigned i = 0; i < 16; ++i)
682 bodyToWorld[i] = _bodyToWorld.data()[i];
688 FGInterface::get_agl_m(double t, const double pt[3], double max_altoff,
689 double contact[3], double normal[3],
690 double linearVel[3], double angularVel[3],
691 SGMaterial const*& material, simgear::BVHNode::Id& id)
693 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
694 SGVec3d _contact, _normal, _linearVel, _angularVel;
696 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
697 _angularVel, id, material);
698 // correct the linear velocity, since the line intersector delivers
699 // values for the start point and the get_agl function should
700 // traditionally deliver for the contact point
701 _linearVel += cross(_angularVel, _contact - pt_m);
703 assign(contact, _contact);
704 assign(normal, _normal);
705 assign(linearVel, _linearVel);
706 assign(angularVel, _angularVel);
711 FGInterface::get_agl_ft(double t, const double pt[3], double max_altoff,
712 double contact[3], double normal[3],
713 double linearVel[3], double angularVel[3],
714 SGMaterial const*& material, simgear::BVHNode::Id& id)
716 // Convert units and do the real work.
717 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
718 pt_m *= SG_FEET_TO_METER;
719 SGVec3d _contact, _normal, _linearVel, _angularVel;
721 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
722 _angularVel, id, material);
723 // correct the linear velocity, since the line intersector delivers
724 // values for the start point and the get_agl function should
725 // traditionally deliver for the contact point
726 _linearVel += cross(_angularVel, _contact - pt_m);
728 // Convert units back ...
729 assign( contact, SG_METER_TO_FEET*_contact );
730 assign( normal, _normal );
731 assign( linearVel, SG_METER_TO_FEET*_linearVel );
732 assign( angularVel, _angularVel );
737 FGInterface::get_nearest_m(double t, const double pt[3], double maxDist,
738 double contact[3], double normal[3],
739 double linearVel[3], double angularVel[3],
740 SGMaterial const*& material,
741 simgear::BVHNode::Id& id)
743 SGVec3d _contact, _linearVel, _angularVel;
744 if (!ground_cache.get_nearest(t, SGVec3d(pt), maxDist, _contact, _linearVel,
745 _angularVel, id, material))
748 assign(contact, _contact);
749 assign(linearVel, _linearVel);
750 assign(angularVel, _angularVel);
755 FGInterface::get_nearest_ft(double t, const double pt[3], double maxDist,
756 double contact[3], double normal[3],
757 double linearVel[3], double angularVel[3],
758 SGMaterial const*& material,
759 simgear::BVHNode::Id& id)
761 SGVec3d _contact, _linearVel, _angularVel;
762 if (!ground_cache.get_nearest(t, SG_FEET_TO_METER*SGVec3d(pt),
763 SG_FEET_TO_METER*maxDist, _contact, _linearVel,
764 _angularVel, id, material))
767 assign(contact, SG_METER_TO_FEET*_contact);
768 assign(linearVel, SG_METER_TO_FEET*_linearVel);
769 assign(angularVel, _angularVel);
774 FGInterface::get_groundlevel_m(double lat, double lon, double alt)
776 return get_groundlevel_m(SGGeod::fromRadM(lon, lat, alt));
780 FGInterface::get_groundlevel_m(const SGGeod& geod)
782 // Compute the cartesian position of the given lat/lon/alt.
783 SGVec3d pos = SGVec3d::fromGeod(geod);
785 // FIXME: how to handle t - ref_time differences ???
787 double ref_time = 0, radius;
788 // Prepare the ground cache for that position.
789 if (!is_valid_m(&ref_time, cpos.data(), &radius)) {
790 double startTime = ref_time;
791 double endTime = startTime + 1;
792 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), 10);
793 /// This is most likely the case when the given altitude is
794 /// too low, try with a new altitude of 10000m, that should be
795 /// sufficient to find a ground level below everywhere on our planet
797 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
798 /// If there is still no ground, return sea level radius
799 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), 10))
802 } else if (radius*radius <= distSqr(pos, cpos)) {
803 double startTime = ref_time;
804 double endTime = startTime + 1;
806 /// We reuse the old radius value, but only if it is at least 10 Meters ..
807 if (!(10 < radius)) // Well this strange compare is nan safe
810 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), radius);
811 /// This is most likely the case when the given altitude is
812 /// too low, try with a new altitude of 10000m, that should be
813 /// sufficient to find a ground level below everywhere on our planet
815 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
816 /// If there is still no ground, return sea level radius
817 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), radius))
822 double contact[3], normal[3], vel[3], angvel[3];
823 const SGMaterial* material;
824 simgear::BVHNode::Id id;
825 // Ignore the return value here, since it just tells us if
826 // the returns stem from the groundcache or from the coarse
827 // computations below the groundcache. The contact point is still something
828 // valid, the normals and the other returns just contain some defaults.
829 get_agl_m(ref_time, pos.data(), 2.0, contact, normal, vel, angvel,
831 return SGGeod::fromCart(SGVec3d(contact)).getElevationM();
835 FGInterface::caught_wire_m(double t, const double pt[4][3])
838 for (int i=0; i<4; ++i)
839 pt_m[i] = SGVec3d(pt[i]);
841 return ground_cache.caught_wire(t, pt_m);
845 FGInterface::caught_wire_ft(double t, const double pt[4][3])
847 // Convert units and do the real work.
849 for (int i=0; i<4; ++i)
850 pt_m[i] = SG_FEET_TO_METER*SGVec3d(pt[i]);
852 return ground_cache.caught_wire(t, pt_m);
856 FGInterface::get_wire_ends_m(double t, double end[2][3], double vel[2][3])
858 SGVec3d _end[2], _vel[2];
859 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
860 for (int k=0; k<2; ++k) {
861 assign( end[k], _end[k] );
862 assign( vel[k], _vel[k] );
868 FGInterface::get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
870 // Convert units and do the real work.
871 SGVec3d _end[2], _vel[2];
872 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
873 for (int k=0; k<2; ++k) {
874 assign( end[k], SG_METER_TO_FEET*_end[k] );
875 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
881 FGInterface::release_wire(void)
883 ground_cache.release_wire();