1 // flight.cxx -- a general interface to the various flight models
3 // Written by Curtis Olson, started May 1997.
5 // Copyright (C) 1997 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <simgear/constants.h>
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/timing/timestamp.hxx>
33 #include <Scenery/scenery.hxx>
34 #include <Main/globals.hxx>
35 #include <Main/fg_props.hxx>
36 #include <FDM/groundcache.hxx>
39 static inline void assign(double* ptr, const SGVec3d& vec)
47 FGInterface::FGInterface()
52 FGInterface::FGInterface( double dt )
58 FGInterface::~FGInterface() {
59 // unbind(); // FIXME: should be called explicitly
63 FGInterface::_calc_multiloop (double dt)
65 // Since some time the simulation time increments we get here are
66 // already a multiple of the basic update freqency.
67 // So, there is no need to do our own multiloop rounding with all bad
68 // roundoff problems when we already have nearly accurate values.
69 // Only the speedup thing must be still handled here
70 int hz = fgGetInt("/sim/model-hz");
71 int multiloop = SGMiscd::roundToInt(dt*hz);
72 int speedup = fgGetInt("/sim/speed-up");
73 return multiloop * speedup;
78 * Set default values for the state of the FDM.
80 * This method is invoked by the constructors.
83 FGInterface::_setup ()
88 d_cg_rp_body_v = SGVec3d::zeros();
89 v_dot_local_v = SGVec3d::zeros();
90 v_dot_body_v = SGVec3d::zeros();
91 a_cg_body_v = SGVec3d::zeros();
92 a_pilot_body_v = SGVec3d::zeros();
93 n_cg_body_v = SGVec3d::zeros();
94 v_local_v = SGVec3d::zeros();
95 v_local_rel_ground_v = SGVec3d::zeros();
96 v_local_airmass_v = SGVec3d::zeros();
97 v_wind_body_v = SGVec3d::zeros();
98 omega_body_v = SGVec3d::zeros();
99 euler_rates_v = SGVec3d::zeros();
100 geocentric_rates_v = SGVec3d::zeros();
101 geodetic_position_v = SGGeod::fromRadM(0, 0, 0);
102 cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
103 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
104 euler_angles_v = SGVec3d::zeros();
107 v_rel_wind=v_true_kts=0;
108 v_ground_speed=v_equiv_kts=0;
112 density=mach_number=0;
113 static_pressure=total_pressure=0;
115 static_temperature=total_temperature=0;
116 sea_level_radius=earth_position_angle=0;
124 FGInterface::init () {}
127 * Initialize the state of the FDM.
129 * Subclasses of FGInterface may do their own, additional initialization,
130 * but there is some that is common to all. Normally, they should call
131 * this before they begin their own init to make sure the basic structures
132 * are set up properly.
135 FGInterface::common_init ()
137 SG_LOG( SG_FLIGHT, SG_INFO, "Start common FDM init" );
141 ground_cache.set_cache_time_offset(globals->get_sim_time_sec());
143 // Set initial position
144 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing position..." );
145 double lon = fgGetDouble("/sim/presets/longitude-deg")
146 * SGD_DEGREES_TO_RADIANS;
147 double lat = fgGetDouble("/sim/presets/latitude-deg")
148 * SGD_DEGREES_TO_RADIANS;
149 double alt_ft = fgGetDouble("/sim/presets/altitude-ft");
150 double alt_m = alt_ft * SG_FEET_TO_METER;
151 set_Longitude( lon );
153 SG_LOG( SG_FLIGHT, SG_INFO, "Checking for lon = "
154 << lon*SGD_RADIANS_TO_DEGREES << "deg, lat = "
155 << lat*SGD_RADIANS_TO_DEGREES << "deg, alt = "
158 double ground_elev_m = get_groundlevel_m(lat, lon, alt_m);
159 double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET;
160 _set_Runway_altitude ( ground_elev_ft );
161 if ( fgGetBool("/sim/presets/onground") || alt_ft < ground_elev_ft ) {
162 fgSetDouble("/position/altitude-ft", ground_elev_ft + 0.1);
163 set_Altitude( ground_elev_ft + 0.1);
165 set_Altitude( alt_ft );
168 // Set ground elevation
169 SG_LOG( SG_FLIGHT, SG_INFO,
170 "...initializing ground elevation to " << ground_elev_ft
173 // Set sea-level radius
174 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing sea-level radius..." );
175 SG_LOG( SG_FLIGHT, SG_INFO, " lat = "
176 << fgGetDouble("/sim/presets/latitude-deg")
177 << " alt = " << get_Altitude() );
178 double slr = SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v);
179 _set_Sea_level_radius( slr * SG_METER_TO_FEET );
181 // Set initial Euler angles
182 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing Euler angles..." );
183 set_Euler_Angles( fgGetDouble("/sim/presets/roll-deg")
184 * SGD_DEGREES_TO_RADIANS,
185 fgGetDouble("/sim/presets/pitch-deg")
186 * SGD_DEGREES_TO_RADIANS,
187 fgGetDouble("/sim/presets/heading-deg")
188 * SGD_DEGREES_TO_RADIANS );
190 // Set initial velocities
191 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing velocities..." );
192 if ( !fgHasNode("/sim/presets/speed-set") ) {
193 set_V_calibrated_kts(0.0);
195 const string speedset = fgGetString("/sim/presets/speed-set");
196 if ( speedset == "knots" || speedset == "KNOTS" ) {
197 set_V_calibrated_kts( fgGetDouble("/sim/presets/airspeed-kt") );
198 } else if ( speedset == "mach" || speedset == "MACH" ) {
199 set_Mach_number( fgGetDouble("/sim/presets/mach") );
200 } else if ( speedset == "UVW" || speedset == "uvw" ) {
201 set_Velocities_Wind_Body(
202 fgGetDouble("/sim/presets/uBody-fps"),
203 fgGetDouble("/sim/presets/vBody-fps"),
204 fgGetDouble("/sim/presets/wBody-fps") );
205 } else if ( speedset == "NED" || speedset == "ned" ) {
206 set_Velocities_Local(
207 fgGetDouble("/sim/presets/speed-north-fps"),
208 fgGetDouble("/sim/presets/speed-east-fps"),
209 fgGetDouble("/sim/presets/speed-down-fps") );
211 SG_LOG( SG_FLIGHT, SG_ALERT,
212 "Unrecognized value for /sim/presets/speed-set: "
214 set_V_calibrated_kts( 0.0 );
218 if ( fgHasNode("/sim/presets/glideslope-deg") )
219 set_Gamma_vert_rad( fgGetDouble("/sim/presets/glideslope-deg")
220 * SGD_DEGREES_TO_RADIANS );
221 else if ( fgHasNode( "/velocities/vertical-speed-fps") )
222 set_Climb_Rate( fgGetDouble("/velocities/vertical-speed-fps") );
224 SG_LOG( SG_FLIGHT, SG_INFO, "End common FDM init" );
229 * Bind getters and setters to properties.
231 * The bind() method will be invoked after init(). Note that unlike
232 * the usual implementations of FGSubsystem::bind(), this method does
233 * not automatically pick up existing values for the properties at
234 * bind time; instead, all values are set explicitly in the init()
242 _tiedProperties.setRoot(globals->get_props());
244 _tiedProperties.Tie("/position/latitude-deg", this,
245 &FGInterface::get_Latitude_deg,
246 &FGInterface::set_Latitude_deg,
248 fgSetArchivable("/position/latitude-deg");
249 _tiedProperties.Tie("/position/longitude-deg", this,
250 &FGInterface::get_Longitude_deg,
251 &FGInterface::set_Longitude_deg,
253 fgSetArchivable("/position/longitude-deg");
254 _tiedProperties.Tie("/position/altitude-ft", this,
255 &FGInterface::get_Altitude,
256 &FGInterface::set_Altitude,
258 fgSetArchivable("/position/altitude-ft");
259 _tiedProperties.Tie("/position/altitude-agl-ft", this,
260 &FGInterface::get_Altitude_AGL, &FGInterface::set_AltitudeAGL, false);
261 fgSetArchivable("/position/ground-elev-ft");
262 _tiedProperties.Tie("/position/ground-elev-ft", this,
263 &FGInterface::get_Runway_altitude); // read-only
264 fgSetArchivable("/position/ground-elev-m");
265 _tiedProperties.Tie("/position/ground-elev-m", this,
266 &FGInterface::get_Runway_altitude_m); // read-only
267 _tiedProperties.Tie("/environment/ground-elevation-m", this,
268 &FGInterface::get_Runway_altitude_m); // read-only
269 fgSetArchivable("/position/sea-level-radius-ft");
270 _tiedProperties.Tie("/position/sea-level-radius-ft", this,
271 &FGInterface::get_Sea_level_radius,
272 &FGInterface::_set_Sea_level_radius, false);
275 _tiedProperties.Tie("/orientation/roll-deg", this,
276 &FGInterface::get_Phi_deg,
277 &FGInterface::set_Phi_deg, false);
278 fgSetArchivable("/orientation/roll-deg");
279 _tiedProperties.Tie("/orientation/pitch-deg", this,
280 &FGInterface::get_Theta_deg,
281 &FGInterface::set_Theta_deg, false);
282 fgSetArchivable("/orientation/pitch-deg");
283 _tiedProperties.Tie("/orientation/heading-deg", this,
284 &FGInterface::get_Psi_deg,
285 &FGInterface::set_Psi_deg, false);
286 fgSetArchivable("/orientation/heading-deg");
287 _tiedProperties.Tie("/orientation/track-deg", this,
288 &FGInterface::get_Track); // read-only
290 // Body-axis "euler rates" (rotation speed, but in a funny
292 _tiedProperties.Tie("/orientation/roll-rate-degps", this,
293 &FGInterface::get_Phi_dot_degps,
294 &FGInterface::set_Phi_dot_degps, false);
295 _tiedProperties.Tie("/orientation/pitch-rate-degps", this,
296 &FGInterface::get_Theta_dot_degps,
297 &FGInterface::set_Theta_dot_degps, false);
298 _tiedProperties.Tie("/orientation/yaw-rate-degps", this,
299 &FGInterface::get_Psi_dot_degps,
300 &FGInterface::set_Psi_dot_degps, false);
302 _tiedProperties.Tie("/orientation/p-body", this, &FGInterface::get_P_body); // read-only
303 _tiedProperties.Tie("/orientation/q-body", this, &FGInterface::get_Q_body); // read-only
304 _tiedProperties.Tie("/orientation/r-body", this, &FGInterface::get_R_body); // read-only
306 // Ground speed knots
307 _tiedProperties.Tie("/velocities/groundspeed-kt", this,
308 &FGInterface::get_V_ground_speed_kt,
309 &FGInterface::set_V_ground_speed_kt); // read-only
311 // Calibrated airspeed
312 _tiedProperties.Tie("/velocities/airspeed-kt", this,
313 &FGInterface::get_V_calibrated_kts,
314 &FGInterface::set_V_calibrated_kts,
317 _tiedProperties.Tie("/velocities/equivalent-kt", this,
318 &FGInterface::get_V_equiv_kts); // read-only
321 _tiedProperties.Tie("/velocities/mach", this,
322 &FGInterface::get_Mach_number,
323 &FGInterface::set_Mach_number,
327 // _tiedProperties.Tie("/velocities/speed-north-fps", this,
328 // &FGInterface::get_V_north,
329 // &FGInterface::set_V_north);
330 // fgSetArchivable("/velocities/speed-north-fps");
331 // _tiedProperties.Tie("/velocities/speed-east-fps", this,
332 // &FGInterface::get_V_east,
333 // &FGInterface::set_V_east);
334 // fgSetArchivable("/velocities/speed-east-fps");
335 // _tiedProperties.Tie("/velocities/speed-down-fps", this,
336 // &FGInterface::get_V_down,
337 // &FGInterface::set_V_down);
338 // fgSetArchivable("/velocities/speed-down-fps");
340 // FIXME: Temporarily read-only, until the
341 // incompatibilities between JSBSim and
342 // LaRCSim are fixed (LaRCSim adds the
343 // earth's rotation to the east velocity).
344 _tiedProperties.Tie("/velocities/speed-north-fps", this,
345 &FGInterface::get_V_north, &FGInterface::set_V_north, false);
346 _tiedProperties.Tie("/velocities/speed-east-fps", this,
347 &FGInterface::get_V_east, &FGInterface::set_V_east, false);
348 _tiedProperties.Tie("/velocities/speed-down-fps", this,
349 &FGInterface::get_V_down, &FGInterface::set_V_down, false);
351 _tiedProperties.Tie("/velocities/north-relground-fps", this,
352 &FGInterface::get_V_north_rel_ground); // read-only
353 _tiedProperties.Tie("/velocities/east-relground-fps", this,
354 &FGInterface::get_V_east_rel_ground); // read-only
355 _tiedProperties.Tie("/velocities/down-relground-fps", this,
356 &FGInterface::get_V_down_rel_ground); // read-only
359 // FIXME: temporarily archivable, until the NED problem is fixed.
360 _tiedProperties.Tie("/velocities/uBody-fps", this,
361 &FGInterface::get_uBody,
362 &FGInterface::set_uBody,
364 fgSetArchivable("/velocities/uBody-fps");
365 _tiedProperties.Tie("/velocities/vBody-fps", this,
366 &FGInterface::get_vBody,
367 &FGInterface::set_vBody,
369 fgSetArchivable("/velocities/vBody-fps");
370 _tiedProperties.Tie("/velocities/wBody-fps", this,
371 &FGInterface::get_wBody,
372 &FGInterface::set_wBody,
374 fgSetArchivable("/velocities/wBody-fps");
376 // Climb and slip (read-only)
377 _tiedProperties.Tie("/velocities/vertical-speed-fps", this,
378 &FGInterface::get_Climb_Rate,
379 &FGInterface::set_Climb_Rate, false );
380 _tiedProperties.Tie("/velocities/glideslope", this,
381 &FGInterface::get_Gamma_vert_rad,
382 &FGInterface::set_Gamma_vert_rad, false );
383 _tiedProperties.Tie("/orientation/side-slip-rad", this,
384 &FGInterface::get_Beta, &FGInterface::_set_Beta, false);
385 _tiedProperties.Tie("/orientation/side-slip-deg", this,
386 &FGInterface::get_Beta_deg); // read-only
387 _tiedProperties.Tie("/orientation/alpha-deg", this,
388 &FGInterface::get_Alpha_deg, &FGInterface::set_Alpha_deg, false);
389 _tiedProperties.Tie("/accelerations/nlf", this,
390 &FGInterface::get_Nlf); // read-only
393 _tiedProperties.Tie("/accelerations/ned/north-accel-fps_sec",
394 this, &FGInterface::get_V_dot_north); // read-only
395 _tiedProperties.Tie("/accelerations/ned/east-accel-fps_sec",
396 this, &FGInterface::get_V_dot_east); // read-only
397 _tiedProperties.Tie("/accelerations/ned/down-accel-fps_sec",
398 this, &FGInterface::get_V_dot_down); // read-only
400 // Pilot accelerations
401 _tiedProperties.Tie("/accelerations/pilot/x-accel-fps_sec",
402 this, &FGInterface::get_A_X_pilot, &FGInterface::set_A_X_pilot, false);
403 _tiedProperties.Tie("/accelerations/pilot/y-accel-fps_sec",
404 this, &FGInterface::get_A_Y_pilot, &FGInterface::set_A_Y_pilot, false);
405 _tiedProperties.Tie("/accelerations/pilot/z-accel-fps_sec",
406 this, &FGInterface::get_A_Z_pilot, &FGInterface::set_A_Z_pilot, false);
408 _tiedProperties.Tie("/accelerations/n-z-cg-fps_sec",
409 this, &FGInterface::get_N_Z_cg); // read-only
414 * Unbind any properties bound to this FDM.
416 * This method allows the FDM to release properties so that a new
417 * FDM can bind them instead.
420 FGInterface::unbind ()
422 _tiedProperties.Untie();
427 * Update the state of the FDM (i.e. run the equations of motion).
430 FGInterface::update (double dt)
432 SG_LOG(SG_FLIGHT, SG_ALERT, "dummy update() ... SHOULDN'T BE CALLED!");
436 void FGInterface::_updatePositionM(const SGVec3d& cartPos)
438 TrackComputer tracker( track, geodetic_position_v );
439 cartesian_position_v = cartPos;
440 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
441 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
442 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
443 _update_ground_elev_at_pos();
447 void FGInterface::_updatePosition(const SGGeod& geod)
449 TrackComputer tracker( track, geodetic_position_v );
450 geodetic_position_v = geod;
451 cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
452 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
454 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
455 _update_ground_elev_at_pos();
459 void FGInterface::_updatePosition(const SGGeoc& geoc)
461 TrackComputer tracker( track, geodetic_position_v );
462 geocentric_position_v = geoc;
463 cartesian_position_v = SGVec3d::fromGeoc(geocentric_position_v);
464 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
466 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
467 _update_ground_elev_at_pos();
471 void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
473 _updatePosition(SGGeod::fromRadFt(lon, lat, alt));
477 void FGInterface::_updateGeocentricPosition( double lat, double lon,
480 _updatePosition(SGGeoc::fromRadFt(lon, lat, get_Sea_level_radius() + alt));
483 void FGInterface::_update_ground_elev_at_pos( void ) {
484 double groundlevel_m = get_groundlevel_m(geodetic_position_v);
485 _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
489 void FGInterface::set_Latitude(double lat) {
490 geodetic_position_v.setLatitudeRad(lat);
493 void FGInterface::set_Longitude(double lon) {
494 geodetic_position_v.setLongitudeRad(lon);
497 void FGInterface::set_Altitude(double alt) {
498 geodetic_position_v.setElevationFt(alt);
501 void FGInterface::set_AltitudeAGL(double altagl) {
506 void FGInterface::set_V_calibrated_kts(double vc) {
507 v_calibrated_kts = vc;
510 void FGInterface::set_Mach_number(double mach) {
514 void FGInterface::set_Velocities_Local( double north,
517 v_local_v[0] = north;
522 void FGInterface::set_Velocities_Wind_Body( double u,
525 v_wind_body_v[0] = u;
526 v_wind_body_v[1] = v;
527 v_wind_body_v[2] = w;
531 void FGInterface::set_Euler_Angles( double phi,
534 euler_angles_v[0] = phi;
535 euler_angles_v[1] = theta;
536 euler_angles_v[2] = psi;
540 void FGInterface::set_Climb_Rate( double roc) {
544 void FGInterface::set_Gamma_vert_rad( double gamma) {
545 gamma_vert_rad = gamma;
548 void FGInterface::set_Static_pressure(double p) { static_pressure = p; }
549 void FGInterface::set_Static_temperature(double T) { static_temperature = T; }
550 void FGInterface::set_Density(double rho) { density = rho; }
552 void FGInterface::set_Velocities_Local_Airmass (double wnorth,
555 v_local_airmass_v[0] = wnorth;
556 v_local_airmass_v[1] = weast;
557 v_local_airmass_v[2] = wdown;
561 void FGInterface::_busdump(void)
563 SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rp_body_v: " << d_cg_rp_body_v);
564 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_local_v: " << v_dot_local_v);
565 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_body_v: " << v_dot_body_v);
566 SG_LOG(SG_FLIGHT,SG_INFO,"a_cg_body_v: " << a_cg_body_v);
567 SG_LOG(SG_FLIGHT,SG_INFO,"a_pilot_body_v: " << a_pilot_body_v);
568 SG_LOG(SG_FLIGHT,SG_INFO,"n_cg_body_v: " << n_cg_body_v);
569 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v: " << v_local_v);
570 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_rel_ground_v: " << v_local_rel_ground_v);
571 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_airmass_v: " << v_local_airmass_v);
572 SG_LOG(SG_FLIGHT,SG_INFO,"v_wind_body_v: " << v_wind_body_v);
573 SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v: " << omega_body_v);
574 SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v: " << euler_rates_v);
575 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v: " << geocentric_rates_v);
576 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v: " << geocentric_position_v);
577 SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v: " << geodetic_position_v);
578 SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v: " << euler_angles_v);
580 SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << nlf );
581 SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << v_rel_wind );
582 SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << v_true_kts );
583 SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << v_ground_speed );
584 SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << v_equiv_kts );
585 SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << v_calibrated_kts );
586 SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << alpha );
587 SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << beta );
588 SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << gamma_vert_rad );
589 SG_LOG(SG_FLIGHT,SG_INFO,"density: " << density );
590 SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << mach_number );
591 SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << static_pressure );
592 SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << total_pressure );
593 SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << dynamic_pressure );
594 SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << static_temperature );
595 SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << total_temperature );
596 SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << sea_level_radius );
597 SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << earth_position_angle );
598 SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << runway_altitude );
599 SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << climb_rate );
600 SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << altitude_agl );
604 FGInterface::prepare_ground_cache_m(double startSimTime, double endSimTime,
605 const double pt[3], double rad)
607 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
612 FGInterface::prepare_ground_cache_ft(double startSimTime, double endSimTime,
613 const double pt[3], double rad)
615 // Convert units and do the real work.
616 SGVec3d pt_ft = SG_FEET_TO_METER*SGVec3d(pt);
617 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
618 pt_ft, rad*SG_FEET_TO_METER);
622 FGInterface::is_valid_m(double *ref_time, double pt[3], double *rad)
625 bool valid = ground_cache.is_valid(*ref_time, _pt, *rad);
630 bool FGInterface::is_valid_ft(double *ref_time, double pt[3], double *rad)
632 // Convert units and do the real work.
634 bool found_ground = ground_cache.is_valid(*ref_time, _pt, *rad);
635 assign(pt, SG_METER_TO_FEET*_pt);
636 *rad *= SG_METER_TO_FEET;
641 FGInterface::get_cat_m(double t, const double pt[3],
642 double end[2][3], double vel[2][3])
644 SGVec3d _end[2], _vel[2];
645 double dist = ground_cache.get_cat(t, SGVec3d(pt), _end, _vel);
646 for (int k=0; k<2; ++k) {
647 assign( end[k], _end[k] );
648 assign( vel[k], _vel[k] );
654 FGInterface::get_cat_ft(double t, const double pt[3],
655 double end[2][3], double vel[2][3])
657 // Convert units and do the real work.
658 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
659 SGVec3d _end[2], _vel[2];
660 double dist = ground_cache.get_cat(t, pt_m, _end, _vel);
661 for (int k=0; k<2; ++k) {
662 assign( end[k], SG_METER_TO_FEET*_end[k] );
663 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
665 return dist*SG_METER_TO_FEET;
669 FGInterface::get_body_m(double t, simgear::BVHNode::Id id,
670 double bodyToWorld[16], double linearVel[3],
671 double angularVel[3])
673 SGMatrixd _bodyToWorld;
674 SGVec3d _linearVel, _angularVel;
675 if (!ground_cache.get_body(t, _bodyToWorld, _linearVel, _angularVel, id))
678 assign(linearVel, _linearVel);
679 assign(angularVel, _angularVel);
680 for (unsigned i = 0; i < 16; ++i)
681 bodyToWorld[i] = _bodyToWorld.data()[i];
687 FGInterface::get_agl_m(double t, const double pt[3], double max_altoff,
688 double contact[3], double normal[3],
689 double linearVel[3], double angularVel[3],
690 SGMaterial const*& material, simgear::BVHNode::Id& id)
692 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
693 SGVec3d _contact, _normal, _linearVel, _angularVel;
695 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
696 _angularVel, id, material);
697 // correct the linear velocity, since the line intersector delivers
698 // values for the start point and the get_agl function should
699 // traditionally deliver for the contact point
700 _linearVel += cross(_angularVel, _contact - pt_m);
702 assign(contact, _contact);
703 assign(normal, _normal);
704 assign(linearVel, _linearVel);
705 assign(angularVel, _angularVel);
710 FGInterface::get_agl_ft(double t, const double pt[3], double max_altoff,
711 double contact[3], double normal[3],
712 double linearVel[3], double angularVel[3],
713 SGMaterial const*& material, simgear::BVHNode::Id& id)
715 // Convert units and do the real work.
716 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
717 pt_m *= SG_FEET_TO_METER;
718 SGVec3d _contact, _normal, _linearVel, _angularVel;
720 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
721 _angularVel, id, material);
722 // correct the linear velocity, since the line intersector delivers
723 // values for the start point and the get_agl function should
724 // traditionally deliver for the contact point
725 _linearVel += cross(_angularVel, _contact - pt_m);
727 // Convert units back ...
728 assign( contact, SG_METER_TO_FEET*_contact );
729 assign( normal, _normal );
730 assign( linearVel, SG_METER_TO_FEET*_linearVel );
731 assign( angularVel, _angularVel );
736 FGInterface::get_nearest_m(double t, const double pt[3], double maxDist,
737 double contact[3], double normal[3],
738 double linearVel[3], double angularVel[3],
739 SGMaterial const*& material,
740 simgear::BVHNode::Id& id)
742 SGVec3d _contact, _linearVel, _angularVel;
743 if (!ground_cache.get_nearest(t, SGVec3d(pt), maxDist, _contact, _linearVel,
744 _angularVel, id, material))
747 assign(contact, _contact);
748 assign(linearVel, _linearVel);
749 assign(angularVel, _angularVel);
754 FGInterface::get_nearest_ft(double t, const double pt[3], double maxDist,
755 double contact[3], double normal[3],
756 double linearVel[3], double angularVel[3],
757 SGMaterial const*& material,
758 simgear::BVHNode::Id& id)
760 SGVec3d _contact, _linearVel, _angularVel;
761 if (!ground_cache.get_nearest(t, SG_FEET_TO_METER*SGVec3d(pt),
762 SG_FEET_TO_METER*maxDist, _contact, _linearVel,
763 _angularVel, id, material))
766 assign(contact, SG_METER_TO_FEET*_contact);
767 assign(linearVel, SG_METER_TO_FEET*_linearVel);
768 assign(angularVel, _angularVel);
773 FGInterface::get_groundlevel_m(double lat, double lon, double alt)
775 return get_groundlevel_m(SGGeod::fromRadM(lon, lat, alt));
779 FGInterface::get_groundlevel_m(const SGGeod& geod)
781 // Compute the cartesian position of the given lat/lon/alt.
782 SGVec3d pos = SGVec3d::fromGeod(geod);
784 // FIXME: how to handle t - ref_time differences ???
786 double ref_time = 0, radius;
787 // Prepare the ground cache for that position.
788 if (!is_valid_m(&ref_time, cpos.data(), &radius)) {
789 double startTime = ref_time;
790 double endTime = startTime + 1;
791 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), 10);
792 /// This is most likely the case when the given altitude is
793 /// too low, try with a new altitude of 10000m, that should be
794 /// sufficient to find a ground level below everywhere on our planet
796 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
797 /// If there is still no ground, return sea level radius
798 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), 10))
801 } else if (radius*radius <= distSqr(pos, cpos)) {
802 double startTime = ref_time;
803 double endTime = startTime + 1;
805 /// We reuse the old radius value, but only if it is at least 10 Meters ..
806 if (!(10 < radius)) // Well this strange compare is nan safe
809 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), radius);
810 /// This is most likely the case when the given altitude is
811 /// too low, try with a new altitude of 10000m, that should be
812 /// sufficient to find a ground level below everywhere on our planet
814 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
815 /// If there is still no ground, return sea level radius
816 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), radius))
821 double contact[3], normal[3], vel[3], angvel[3];
822 const SGMaterial* material;
823 simgear::BVHNode::Id id;
824 // Ignore the return value here, since it just tells us if
825 // the returns stem from the groundcache or from the coarse
826 // computations below the groundcache. The contact point is still something
827 // valid, the normals and the other returns just contain some defaults.
828 get_agl_m(ref_time, pos.data(), 2.0, contact, normal, vel, angvel,
830 return SGGeod::fromCart(SGVec3d(contact)).getElevationM();
834 FGInterface::caught_wire_m(double t, const double pt[4][3])
837 for (int i=0; i<4; ++i)
838 pt_m[i] = SGVec3d(pt[i]);
840 return ground_cache.caught_wire(t, pt_m);
844 FGInterface::caught_wire_ft(double t, const double pt[4][3])
846 // Convert units and do the real work.
848 for (int i=0; i<4; ++i)
849 pt_m[i] = SG_FEET_TO_METER*SGVec3d(pt[i]);
851 return ground_cache.caught_wire(t, pt_m);
855 FGInterface::get_wire_ends_m(double t, double end[2][3], double vel[2][3])
857 SGVec3d _end[2], _vel[2];
858 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
859 for (int k=0; k<2; ++k) {
860 assign( end[k], _end[k] );
861 assign( vel[k], _vel[k] );
867 FGInterface::get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
869 // Convert units and do the real work.
870 SGVec3d _end[2], _vel[2];
871 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
872 for (int k=0; k<2; ++k) {
873 assign( end[k], SG_METER_TO_FEET*_end[k] );
874 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
880 FGInterface::release_wire(void)
882 ground_cache.release_wire();