1 // flight.cxx -- a general interface to the various flight models
3 // Written by Curtis Olson, started May 1997.
5 // Copyright (C) 1997 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <simgear/constants.h>
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/math/SGMath.hxx>
32 #include <simgear/timing/timestamp.hxx>
34 #include <Scenery/scenery.hxx>
35 #include <Main/globals.hxx>
36 #include <Main/fg_props.hxx>
37 #include <FDM/groundcache.hxx>
40 static inline void assign(double* ptr, const SGVec3d& vec)
47 FGInterface *cur_fdm_state = 0;
50 FGInterface::FGInterface()
55 FGInterface::FGInterface( double dt )
61 FGInterface::~FGInterface() {
62 // unbind(); // FIXME: should be called explicitly
66 FGInterface::_calc_multiloop (double dt)
68 // Since some time the simulation time increments we get here are
69 // already a multiple of the basic update freqency.
70 // So, there is no need to do our own multiloop rounding with all bad
71 // roundoff problems when we already have nearly accurate values.
72 // Only the speedup thing must be still handled here
73 int hz = fgGetInt("/sim/model-hz");
74 int multiloop = SGMiscd::roundToInt(dt*hz);
75 int speedup = fgGetInt("/sim/speed-up");
76 return multiloop * speedup;
81 * Set default values for the state of the FDM.
83 * This method is invoked by the constructors.
86 FGInterface::_setup ()
91 d_cg_rp_body_v = SGVec3d::zeros();
92 v_dot_local_v = SGVec3d::zeros();
93 v_dot_body_v = SGVec3d::zeros();
94 a_cg_body_v = SGVec3d::zeros();
95 a_pilot_body_v = SGVec3d::zeros();
96 n_cg_body_v = SGVec3d::zeros();
97 v_local_v = SGVec3d::zeros();
98 v_local_rel_ground_v = SGVec3d::zeros();
99 v_local_airmass_v = SGVec3d::zeros();
100 v_wind_body_v = SGVec3d::zeros();
101 omega_body_v = SGVec3d::zeros();
102 euler_rates_v = SGVec3d::zeros();
103 geocentric_rates_v = SGVec3d::zeros();
104 geodetic_position_v = SGGeod::fromRadM(0, 0, 0);
105 cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
106 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
107 euler_angles_v = SGVec3d::zeros();
110 v_rel_wind=v_true_kts=0;
111 v_ground_speed=v_equiv_kts=0;
115 density=mach_number=0;
116 static_pressure=total_pressure=0;
118 static_temperature=total_temperature=0;
119 sea_level_radius=earth_position_angle=0;
126 FGInterface::init () {}
129 * Initialize the state of the FDM.
131 * Subclasses of FGInterface may do their own, additional initialization,
132 * but there is some that is common to all. Normally, they should call
133 * this before they begin their own init to make sure the basic structures
134 * are set up properly.
137 FGInterface::common_init ()
139 SG_LOG( SG_FLIGHT, SG_INFO, "Start common FDM init" );
143 ground_cache.set_cache_time_offset(globals->get_sim_time_sec());
145 // Set initial position
146 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing position..." );
147 double lon = fgGetDouble("/sim/presets/longitude-deg")
148 * SGD_DEGREES_TO_RADIANS;
149 double lat = fgGetDouble("/sim/presets/latitude-deg")
150 * SGD_DEGREES_TO_RADIANS;
151 double alt_ft = fgGetDouble("/sim/presets/altitude-ft");
152 double alt_m = alt_ft * SG_FEET_TO_METER;
153 set_Longitude( lon );
155 SG_LOG( SG_FLIGHT, SG_INFO, "Checking for lon = "
156 << lon*SGD_RADIANS_TO_DEGREES << "deg, lat = "
157 << lat*SGD_RADIANS_TO_DEGREES << "deg, alt = "
160 double ground_elev_m = get_groundlevel_m(lat, lon, alt_m);
161 double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET;
162 _set_Runway_altitude ( ground_elev_ft );
163 if ( fgGetBool("/sim/presets/onground") || alt_ft < ground_elev_ft ) {
164 fgSetDouble("/position/altitude-ft", ground_elev_ft + 0.1);
165 set_Altitude( ground_elev_ft + 0.1);
167 set_Altitude( alt_ft );
170 // Set ground elevation
171 SG_LOG( SG_FLIGHT, SG_INFO,
172 "...initializing ground elevation to " << ground_elev_ft
175 // Set sea-level radius
176 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing sea-level radius..." );
177 SG_LOG( SG_FLIGHT, SG_INFO, " lat = "
178 << fgGetDouble("/sim/presets/latitude-deg")
179 << " alt = " << get_Altitude() );
180 double slr = SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v);
181 _set_Sea_level_radius( slr * SG_METER_TO_FEET );
183 // Set initial velocities
184 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing velocities..." );
185 if ( !fgHasNode("/sim/presets/speed-set") ) {
186 set_V_calibrated_kts(0.0);
188 const string speedset = fgGetString("/sim/presets/speed-set");
189 if ( speedset == "knots" || speedset == "KNOTS" ) {
190 set_V_calibrated_kts( fgGetDouble("/sim/presets/airspeed-kt") );
191 } else if ( speedset == "mach" || speedset == "MACH" ) {
192 set_Mach_number( fgGetDouble("/sim/presets/mach") );
193 } else if ( speedset == "UVW" || speedset == "uvw" ) {
194 set_Velocities_Wind_Body(
195 fgGetDouble("/sim/presets/uBody-fps"),
196 fgGetDouble("/sim/presets/vBody-fps"),
197 fgGetDouble("/sim/presets/wBody-fps") );
198 } else if ( speedset == "NED" || speedset == "ned" ) {
199 set_Velocities_Local(
200 fgGetDouble("/sim/presets/speed-north-fps"),
201 fgGetDouble("/sim/presets/speed-east-fps"),
202 fgGetDouble("/sim/presets/speed-down-fps") );
204 SG_LOG( SG_FLIGHT, SG_ALERT,
205 "Unrecognized value for /sim/presets/speed-set: "
207 set_V_calibrated_kts( 0.0 );
211 // Set initial Euler angles
212 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing Euler angles..." );
213 set_Euler_Angles( fgGetDouble("/sim/presets/roll-deg")
214 * SGD_DEGREES_TO_RADIANS,
215 fgGetDouble("/sim/presets/pitch-deg")
216 * SGD_DEGREES_TO_RADIANS,
217 fgGetDouble("/sim/presets/heading-deg")
218 * SGD_DEGREES_TO_RADIANS );
220 SG_LOG( SG_FLIGHT, SG_INFO, "End common FDM init" );
225 * Bind getters and setters to properties.
227 * The bind() method will be invoked after init(). Note that unlike
228 * the usual implementations of FGSubsystem::bind(), this method does
229 * not automatically pick up existing values for the properties at
230 * bind time; instead, all values are set explicitly in the init()
239 fgTie("/position/latitude-deg", this,
240 &FGInterface::get_Latitude_deg,
241 &FGInterface::set_Latitude_deg,
243 fgSetArchivable("/position/latitude-deg");
244 fgTie("/position/longitude-deg", this,
245 &FGInterface::get_Longitude_deg,
246 &FGInterface::set_Longitude_deg,
248 fgSetArchivable("/position/longitude-deg");
249 fgTie("/position/altitude-ft", this,
250 &FGInterface::get_Altitude,
251 &FGInterface::set_Altitude,
253 fgSetArchivable("/position/altitude-ft");
254 fgTie("/position/altitude-agl-ft", this,
255 &FGInterface::get_Altitude_AGL); // read-only
256 fgSetArchivable("/position/ground-elev-ft");
257 fgTie("/position/ground-elev-ft", this,
258 &FGInterface::get_Runway_altitude); // read-only
259 fgSetArchivable("/position/ground-elev-m");
260 fgTie("/position/ground-elev-m", this,
261 &FGInterface::get_Runway_altitude_m); // read-only
262 fgTie("/environment/ground-elevation-m", this,
263 &FGInterface::get_Runway_altitude_m); // read-only
264 fgSetArchivable("/position/sea-level-radius-ft");
265 fgTie("/position/sea-level-radius-ft", this,
266 &FGInterface::get_Sea_level_radius); // read-only
269 fgTie("/orientation/roll-deg", this,
270 &FGInterface::get_Phi_deg,
271 &FGInterface::set_Phi_deg);
272 fgSetArchivable("/orientation/roll-deg");
273 fgTie("/orientation/pitch-deg", this,
274 &FGInterface::get_Theta_deg,
275 &FGInterface::set_Theta_deg);
276 fgSetArchivable("/orientation/pitch-deg");
277 fgTie("/orientation/heading-deg", this,
278 &FGInterface::get_Psi_deg,
279 &FGInterface::set_Psi_deg);
280 fgSetArchivable("/orientation/heading-deg");
282 // Body-axis "euler rates" (rotation speed, but in a funny
284 fgTie("/orientation/roll-rate-degps", this,
285 &FGInterface::get_Phi_dot_degps);
286 fgTie("/orientation/pitch-rate-degps", this,
287 &FGInterface::get_Theta_dot_degps);
288 fgTie("/orientation/yaw-rate-degps", this,
289 &FGInterface::get_Psi_dot_degps);
291 // Ground speed knots
292 fgTie("/velocities/groundspeed-kt", this,
293 &FGInterface::get_V_ground_speed_kt);
295 // Calibrated airspeed
296 fgTie("/velocities/airspeed-kt", this,
297 &FGInterface::get_V_calibrated_kts,
298 &FGInterface::set_V_calibrated_kts,
302 fgTie("/velocities/mach", this,
303 &FGInterface::get_Mach_number,
304 &FGInterface::set_Mach_number,
308 // fgTie("/velocities/speed-north-fps", this,
309 // &FGInterface::get_V_north,
310 // &FGInterface::set_V_north);
311 // fgSetArchivable("/velocities/speed-north-fps");
312 // fgTie("/velocities/speed-east-fps", this,
313 // &FGInterface::get_V_east,
314 // &FGInterface::set_V_east);
315 // fgSetArchivable("/velocities/speed-east-fps");
316 // fgTie("/velocities/speed-down-fps", this,
317 // &FGInterface::get_V_down,
318 // &FGInterface::set_V_down);
319 // fgSetArchivable("/velocities/speed-down-fps");
320 // FIXME: Temporarily read-only, until the
321 // incompatibilities between JSBSim and
322 // LaRCSim are fixed (LaRCSim adds the
323 // earth's rotation to the east velocity).
324 fgTie("/velocities/speed-north-fps", this,
325 &FGInterface::get_V_north);
326 fgTie("/velocities/speed-east-fps", this,
327 &FGInterface::get_V_east);
328 fgTie("/velocities/speed-down-fps", this,
329 &FGInterface::get_V_down);
332 // FIXME: temporarily archivable, until
333 // the NED problem is fixed.
334 fgTie("/velocities/uBody-fps", this,
335 &FGInterface::get_uBody,
336 &FGInterface::set_uBody,
338 fgSetArchivable("/velocities/uBody-fps");
339 fgTie("/velocities/vBody-fps", this,
340 &FGInterface::get_vBody,
341 &FGInterface::set_vBody,
343 fgSetArchivable("/velocities/vBody-fps");
344 fgTie("/velocities/wBody-fps", this,
345 &FGInterface::get_wBody,
346 &FGInterface::set_wBody,
348 fgSetArchivable("/velocities/wBody-fps");
350 // Climb and slip (read-only)
351 fgTie("/velocities/vertical-speed-fps", this,
352 &FGInterface::get_Climb_Rate,
353 &FGInterface::set_Climb_Rate );
354 fgTie("/velocities/glideslope", this,
355 &FGInterface::get_Gamma_vert_rad,
356 &FGInterface::set_Gamma_vert_rad );
357 fgTie("/orientation/side-slip-rad", this,
358 &FGInterface::get_Beta); // read-only
359 fgTie("/orientation/side-slip-deg", this,
360 &FGInterface::get_Beta_deg); // read-only
361 fgTie("/orientation/alpha-deg", this,
362 &FGInterface::get_Alpha_deg); // read-only
363 fgTie("/accelerations/nlf", this,
364 &FGInterface::get_Nlf); // read-only
367 fgTie("/accelerations/ned/north-accel-fps_sec",
368 this, &FGInterface::get_V_dot_north);
369 fgTie("/accelerations/ned/east-accel-fps_sec",
370 this, &FGInterface::get_V_dot_east);
371 fgTie("/accelerations/ned/down-accel-fps_sec",
372 this, &FGInterface::get_V_dot_down);
374 // Pilot accelerations
375 fgTie("/accelerations/pilot/x-accel-fps_sec",
376 this, &FGInterface::get_A_X_pilot);
377 fgTie("/accelerations/pilot/y-accel-fps_sec",
378 this, &FGInterface::get_A_Y_pilot);
379 fgTie("/accelerations/pilot/z-accel-fps_sec",
380 this, &FGInterface::get_A_Z_pilot);
386 * Unbind any properties bound to this FDM.
388 * This method allows the FDM to release properties so that a new
389 * FDM can bind them instead.
392 FGInterface::unbind ()
396 fgUntie("/position/latitude-deg");
397 fgUntie("/position/longitude-deg");
398 fgUntie("/position/altitude-ft");
399 fgUntie("/position/altitude-agl-ft");
400 fgUntie("/position/ground-elev-ft");
401 fgUntie("/position/ground-elev-m");
402 fgUntie("/environment/ground-elevation-m");
403 fgUntie("/position/sea-level-radius-ft");
404 fgUntie("/orientation/roll-deg");
405 fgUntie("/orientation/pitch-deg");
406 fgUntie("/orientation/heading-deg");
407 fgUntie("/orientation/roll-rate-degps");
408 fgUntie("/orientation/pitch-rate-degps");
409 fgUntie("/orientation/yaw-rate-degps");
410 fgUntie("/orientation/side-slip-rad");
411 fgUntie("/orientation/side-slip-deg");
412 fgUntie("/orientation/alpha-deg");
413 fgUntie("/velocities/airspeed-kt");
414 fgUntie("/velocities/groundspeed-kt");
415 fgUntie("/velocities/mach");
416 fgUntie("/velocities/speed-north-fps");
417 fgUntie("/velocities/speed-east-fps");
418 fgUntie("/velocities/speed-down-fps");
419 fgUntie("/velocities/uBody-fps");
420 fgUntie("/velocities/vBody-fps");
421 fgUntie("/velocities/wBody-fps");
422 fgUntie("/velocities/vertical-speed-fps");
423 fgUntie("/velocities/glideslope");
424 fgUntie("/accelerations/nlf");
425 fgUntie("/accelerations/pilot/x-accel-fps_sec");
426 fgUntie("/accelerations/pilot/y-accel-fps_sec");
427 fgUntie("/accelerations/pilot/z-accel-fps_sec");
428 fgUntie("/accelerations/ned/north-accel-fps_sec");
429 fgUntie("/accelerations/ned/east-accel-fps_sec");
430 fgUntie("/accelerations/ned/down-accel-fps_sec");
434 * Update the state of the FDM (i.e. run the equations of motion).
437 FGInterface::update (double dt)
439 SG_LOG(SG_FLIGHT, SG_ALERT, "dummy update() ... SHOULDN'T BE CALLED!");
443 void FGInterface::_updatePositionM(const SGVec3d& cartPos)
445 cartesian_position_v = cartPos;
446 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
447 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
448 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
449 _update_ground_elev_at_pos();
453 void FGInterface::_updatePosition(const SGGeod& geod)
455 geodetic_position_v = geod;
456 cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
457 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
459 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
460 _update_ground_elev_at_pos();
464 void FGInterface::_updatePosition(const SGGeoc& geoc)
466 geocentric_position_v = geoc;
467 cartesian_position_v = SGVec3d::fromGeoc(geocentric_position_v);
468 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
470 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
471 _update_ground_elev_at_pos();
475 void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
477 _updatePosition(SGGeod::fromRadFt(lon, lat, alt));
481 void FGInterface::_updateGeocentricPosition( double lat, double lon,
484 _updatePosition(SGGeoc::fromRadFt(lon, lat, get_Sea_level_radius() + alt));
487 void FGInterface::_update_ground_elev_at_pos( void ) {
488 double groundlevel_m = get_groundlevel_m(geodetic_position_v);
489 _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
493 void FGInterface::set_Latitude(double lat) {
494 geodetic_position_v.setLatitudeRad(lat);
497 void FGInterface::set_Longitude(double lon) {
498 geodetic_position_v.setLongitudeRad(lon);
501 void FGInterface::set_Altitude(double alt) {
502 geodetic_position_v.setElevationFt(alt);
505 void FGInterface::set_AltitudeAGL(double altagl) {
510 void FGInterface::set_V_calibrated_kts(double vc) {
511 v_calibrated_kts = vc;
514 void FGInterface::set_Mach_number(double mach) {
518 void FGInterface::set_Velocities_Local( double north,
521 v_local_v[0] = north;
526 void FGInterface::set_Velocities_Wind_Body( double u,
529 v_wind_body_v[0] = u;
530 v_wind_body_v[1] = v;
531 v_wind_body_v[2] = w;
535 void FGInterface::set_Euler_Angles( double phi,
538 euler_angles_v[0] = phi;
539 euler_angles_v[1] = theta;
540 euler_angles_v[2] = psi;
544 void FGInterface::set_Climb_Rate( double roc) {
548 void FGInterface::set_Gamma_vert_rad( double gamma) {
549 gamma_vert_rad = gamma;
552 void FGInterface::set_Static_pressure(double p) { static_pressure = p; }
553 void FGInterface::set_Static_temperature(double T) { static_temperature = T; }
554 void FGInterface::set_Density(double rho) { density = rho; }
556 void FGInterface::set_Velocities_Local_Airmass (double wnorth,
559 v_local_airmass_v[0] = wnorth;
560 v_local_airmass_v[1] = weast;
561 v_local_airmass_v[2] = wdown;
565 void FGInterface::_busdump(void) {
567 SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rp_body_v: " << d_cg_rp_body_v);
568 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_local_v: " << v_dot_local_v);
569 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_body_v: " << v_dot_body_v);
570 SG_LOG(SG_FLIGHT,SG_INFO,"a_cg_body_v: " << a_cg_body_v);
571 SG_LOG(SG_FLIGHT,SG_INFO,"a_pilot_body_v: " << a_pilot_body_v);
572 SG_LOG(SG_FLIGHT,SG_INFO,"n_cg_body_v: " << n_cg_body_v);
573 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v: " << v_local_v);
574 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_rel_ground_v: " << v_local_rel_ground_v);
575 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_airmass_v: " << v_local_airmass_v);
576 SG_LOG(SG_FLIGHT,SG_INFO,"v_wind_body_v: " << v_wind_body_v);
577 SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v: " << omega_body_v);
578 SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v: " << euler_rates_v);
579 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v: " << geocentric_rates_v);
580 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v: " << geocentric_position_v);
581 SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v: " << geodetic_position_v);
582 SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v: " << euler_angles_v);
584 SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << nlf );
585 SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << v_rel_wind );
586 SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << v_true_kts );
587 SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << v_ground_speed );
588 SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << v_equiv_kts );
589 SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << v_calibrated_kts );
590 SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << alpha );
591 SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << beta );
592 SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << gamma_vert_rad );
593 SG_LOG(SG_FLIGHT,SG_INFO,"density: " << density );
594 SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << mach_number );
595 SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << static_pressure );
596 SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << total_pressure );
597 SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << dynamic_pressure );
598 SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << static_temperature );
599 SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << total_temperature );
600 SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << sea_level_radius );
601 SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << earth_position_angle );
602 SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << runway_altitude );
603 SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << climb_rate );
604 SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << altitude_agl );
608 FGInterface::prepare_ground_cache_m(double startSimTime, double endSimTime,
609 const double pt[3], double rad)
611 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
616 FGInterface::prepare_ground_cache_ft(double startSimTime, double endSimTime,
617 const double pt[3], double rad)
619 // Convert units and do the real work.
620 SGVec3d pt_ft = SG_FEET_TO_METER*SGVec3d(pt);
621 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
622 pt_ft, rad*SG_FEET_TO_METER);
626 FGInterface::is_valid_m(double *ref_time, double pt[3], double *rad)
629 bool valid = ground_cache.is_valid(*ref_time, _pt, *rad);
634 bool FGInterface::is_valid_ft(double *ref_time, double pt[3], double *rad)
636 // Convert units and do the real work.
638 bool found_ground = ground_cache.is_valid(*ref_time, _pt, *rad);
639 assign(pt, SG_METER_TO_FEET*_pt);
640 *rad *= SG_METER_TO_FEET;
645 FGInterface::get_cat_m(double t, const double pt[3],
646 double end[2][3], double vel[2][3])
648 SGVec3d _end[2], _vel[2];
649 double dist = ground_cache.get_cat(t, SGVec3d(pt), _end, _vel);
650 for (int k=0; k<2; ++k) {
651 assign( end[k], _end[k] );
652 assign( vel[k], _vel[k] );
658 FGInterface::get_cat_ft(double t, const double pt[3],
659 double end[2][3], double vel[2][3])
661 // Convert units and do the real work.
662 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
663 SGVec3d _end[2], _vel[2];
664 double dist = ground_cache.get_cat(t, pt_m, _end, _vel);
665 for (int k=0; k<2; ++k) {
666 assign( end[k], SG_METER_TO_FEET*_end[k] );
667 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
669 return dist*SG_METER_TO_FEET;
673 FGInterface::get_body_m(double t, simgear::BVHNode::Id id,
674 double bodyToWorld[16], double linearVel[3],
675 double angularVel[3])
677 SGMatrixd _bodyToWorld;
678 SGVec3d _linearVel, _angularVel;
679 if (!ground_cache.get_body(t, _bodyToWorld, _linearVel, _angularVel, id))
682 assign(linearVel, _linearVel);
683 assign(angularVel, _angularVel);
684 for (unsigned i = 0; i < 16; ++i)
685 bodyToWorld[i] = _bodyToWorld.data()[i];
691 FGInterface::get_agl_m(double t, const double pt[3], double max_altoff,
692 double contact[3], double normal[3],
693 double linearVel[3], double angularVel[3],
694 SGMaterial const*& material, simgear::BVHNode::Id& id)
696 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
697 SGVec3d _contact, _normal, _linearVel, _angularVel;
699 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
700 _angularVel, id, material);
701 // correct the linear velocity, since the line intersector delivers
702 // values for the start point and the get_agl function should
703 // traditionally deliver for the contact point
704 _linearVel += cross(_angularVel, _contact - pt_m);
706 assign(contact, _contact);
707 assign(normal, _normal);
708 assign(linearVel, _linearVel);
709 assign(angularVel, _angularVel);
714 FGInterface::get_agl_ft(double t, const double pt[3], double max_altoff,
715 double contact[3], double normal[3],
716 double linearVel[3], double angularVel[3],
717 SGMaterial const*& material, simgear::BVHNode::Id& id)
719 // Convert units and do the real work.
720 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
721 pt_m *= SG_FEET_TO_METER;
722 SGVec3d _contact, _normal, _linearVel, _angularVel;
724 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
725 _angularVel, id, material);
726 // correct the linear velocity, since the line intersector delivers
727 // values for the start point and the get_agl function should
728 // traditionally deliver for the contact point
729 _linearVel += cross(_angularVel, _contact - pt_m);
731 // Convert units back ...
732 assign( contact, SG_METER_TO_FEET*_contact );
733 assign( normal, _normal );
734 assign( linearVel, SG_METER_TO_FEET*_linearVel );
735 assign( angularVel, _angularVel );
740 FGInterface::get_nearest_m(double t, const double pt[3], double maxDist,
741 double contact[3], double normal[3],
742 double linearVel[3], double angularVel[3],
743 SGMaterial const*& material,
744 simgear::BVHNode::Id& id)
746 SGVec3d _contact, _linearVel, _angularVel;
747 if (!ground_cache.get_nearest(t, SGVec3d(pt), maxDist, _contact, _linearVel,
748 _angularVel, id, material))
751 assign(contact, _contact);
752 assign(linearVel, _linearVel);
753 assign(angularVel, _angularVel);
758 FGInterface::get_nearest_ft(double t, const double pt[3], double maxDist,
759 double contact[3], double normal[3],
760 double linearVel[3], double angularVel[3],
761 SGMaterial const*& material,
762 simgear::BVHNode::Id& id)
764 SGVec3d _contact, _linearVel, _angularVel;
765 if (!ground_cache.get_nearest(t, SG_FEET_TO_METER*SGVec3d(pt),
766 SG_FEET_TO_METER*maxDist, _contact, _linearVel,
767 _angularVel, id, material))
770 assign(contact, SG_METER_TO_FEET*_contact);
771 assign(linearVel, SG_METER_TO_FEET*_linearVel);
772 assign(angularVel, _angularVel);
777 FGInterface::get_groundlevel_m(double lat, double lon, double alt)
779 return get_groundlevel_m(SGGeod::fromRadM(lon, lat, alt));
783 FGInterface::get_groundlevel_m(const SGGeod& geod)
785 // Compute the cartesian position of the given lat/lon/alt.
786 SGVec3d pos = SGVec3d::fromGeod(geod);
788 // FIXME: how to handle t - ref_time differences ???
790 double ref_time = 0, radius;
791 // Prepare the ground cache for that position.
792 if (!is_valid_m(&ref_time, cpos.data(), &radius)) {
793 double startTime = ref_time;
794 double endTime = startTime + 1;
795 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), 10);
796 /// This is most likely the case when the given altitude is
797 /// too low, try with a new altitude of 10000m, that should be
798 /// sufficient to find a ground level below everywhere on our planet
800 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
801 /// If there is still no ground, return sea level radius
802 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), 10))
805 } else if (radius*radius <= distSqr(pos, cpos)) {
806 double startTime = ref_time;
807 double endTime = startTime + 1;
809 /// We reuse the old radius value, but only if it is at least 10 Meters ..
810 if (!(10 < radius)) // Well this strange compare is nan safe
813 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), radius);
814 /// This is most likely the case when the given altitude is
815 /// too low, try with a new altitude of 10000m, that should be
816 /// sufficient to find a ground level below everywhere on our planet
818 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
819 /// If there is still no ground, return sea level radius
820 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), radius))
825 double contact[3], normal[3], vel[3], angvel[3];
826 const SGMaterial* material;
827 simgear::BVHNode::Id id;
828 // Ignore the return value here, since it just tells us if
829 // the returns stem from the groundcache or from the coarse
830 // computations below the groundcache. The contact point is still something
831 // valid, the normals and the other returns just contain some defaults.
832 get_agl_m(ref_time, pos.data(), 2.0, contact, normal, vel, angvel,
834 return SGGeod::fromCart(SGVec3d(contact)).getElevationM();
838 FGInterface::caught_wire_m(double t, const double pt[4][3])
841 for (int i=0; i<4; ++i)
842 pt_m[i] = SGVec3d(pt[i]);
844 return ground_cache.caught_wire(t, pt_m);
848 FGInterface::caught_wire_ft(double t, const double pt[4][3])
850 // Convert units and do the real work.
852 for (int i=0; i<4; ++i)
853 pt_m[i] = SG_FEET_TO_METER*SGVec3d(pt[i]);
855 return ground_cache.caught_wire(t, pt_m);
859 FGInterface::get_wire_ends_m(double t, double end[2][3], double vel[2][3])
861 SGVec3d _end[2], _vel[2];
862 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
863 for (int k=0; k<2; ++k) {
864 assign( end[k], _end[k] );
865 assign( vel[k], _vel[k] );
871 FGInterface::get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
873 // Convert units and do the real work.
874 SGVec3d _end[2], _vel[2];
875 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
876 for (int k=0; k<2; ++k) {
877 assign( end[k], SG_METER_TO_FEET*_end[k] );
878 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
884 FGInterface::release_wire(void)
886 ground_cache.release_wire();
889 void fgToggleFDMdataLogging(void) {
890 cur_fdm_state->ToggleDataLogging();