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");
281 fgTie("/orientation/track-deg", this,
282 &FGInterface::get_Track);
284 // Body-axis "euler rates" (rotation speed, but in a funny
286 fgTie("/orientation/roll-rate-degps", this,
287 &FGInterface::get_Phi_dot_degps);
288 fgTie("/orientation/pitch-rate-degps", this,
289 &FGInterface::get_Theta_dot_degps);
290 fgTie("/orientation/yaw-rate-degps", this,
291 &FGInterface::get_Psi_dot_degps);
293 // Ground speed knots
294 fgTie("/velocities/groundspeed-kt", this,
295 &FGInterface::get_V_ground_speed_kt);
297 // Calibrated airspeed
298 fgTie("/velocities/airspeed-kt", this,
299 &FGInterface::get_V_calibrated_kts,
300 &FGInterface::set_V_calibrated_kts,
304 fgTie("/velocities/mach", this,
305 &FGInterface::get_Mach_number,
306 &FGInterface::set_Mach_number,
310 // fgTie("/velocities/speed-north-fps", this,
311 // &FGInterface::get_V_north,
312 // &FGInterface::set_V_north);
313 // fgSetArchivable("/velocities/speed-north-fps");
314 // fgTie("/velocities/speed-east-fps", this,
315 // &FGInterface::get_V_east,
316 // &FGInterface::set_V_east);
317 // fgSetArchivable("/velocities/speed-east-fps");
318 // fgTie("/velocities/speed-down-fps", this,
319 // &FGInterface::get_V_down,
320 // &FGInterface::set_V_down);
321 // fgSetArchivable("/velocities/speed-down-fps");
322 // FIXME: Temporarily read-only, until the
323 // incompatibilities between JSBSim and
324 // LaRCSim are fixed (LaRCSim adds the
325 // earth's rotation to the east velocity).
326 fgTie("/velocities/speed-north-fps", this,
327 &FGInterface::get_V_north);
328 fgTie("/velocities/speed-east-fps", this,
329 &FGInterface::get_V_east);
330 fgTie("/velocities/speed-down-fps", this,
331 &FGInterface::get_V_down);
334 // FIXME: temporarily archivable, until
335 // the NED problem is fixed.
336 fgTie("/velocities/uBody-fps", this,
337 &FGInterface::get_uBody,
338 &FGInterface::set_uBody,
340 fgSetArchivable("/velocities/uBody-fps");
341 fgTie("/velocities/vBody-fps", this,
342 &FGInterface::get_vBody,
343 &FGInterface::set_vBody,
345 fgSetArchivable("/velocities/vBody-fps");
346 fgTie("/velocities/wBody-fps", this,
347 &FGInterface::get_wBody,
348 &FGInterface::set_wBody,
350 fgSetArchivable("/velocities/wBody-fps");
352 // Climb and slip (read-only)
353 fgTie("/velocities/vertical-speed-fps", this,
354 &FGInterface::get_Climb_Rate,
355 &FGInterface::set_Climb_Rate );
356 fgTie("/velocities/glideslope", this,
357 &FGInterface::get_Gamma_vert_rad,
358 &FGInterface::set_Gamma_vert_rad );
359 fgTie("/orientation/side-slip-rad", this,
360 &FGInterface::get_Beta); // read-only
361 fgTie("/orientation/side-slip-deg", this,
362 &FGInterface::get_Beta_deg); // read-only
363 fgTie("/orientation/alpha-deg", this,
364 &FGInterface::get_Alpha_deg); // read-only
365 fgTie("/accelerations/nlf", this,
366 &FGInterface::get_Nlf); // read-only
369 fgTie("/accelerations/ned/north-accel-fps_sec",
370 this, &FGInterface::get_V_dot_north);
371 fgTie("/accelerations/ned/east-accel-fps_sec",
372 this, &FGInterface::get_V_dot_east);
373 fgTie("/accelerations/ned/down-accel-fps_sec",
374 this, &FGInterface::get_V_dot_down);
376 // Pilot accelerations
377 fgTie("/accelerations/pilot/x-accel-fps_sec",
378 this, &FGInterface::get_A_X_pilot);
379 fgTie("/accelerations/pilot/y-accel-fps_sec",
380 this, &FGInterface::get_A_Y_pilot);
381 fgTie("/accelerations/pilot/z-accel-fps_sec",
382 this, &FGInterface::get_A_Z_pilot);
388 * Unbind any properties bound to this FDM.
390 * This method allows the FDM to release properties so that a new
391 * FDM can bind them instead.
394 FGInterface::unbind ()
398 fgUntie("/position/latitude-deg");
399 fgUntie("/position/longitude-deg");
400 fgUntie("/position/altitude-ft");
401 fgUntie("/position/altitude-agl-ft");
402 fgUntie("/position/ground-elev-ft");
403 fgUntie("/position/ground-elev-m");
404 fgUntie("/environment/ground-elevation-m");
405 fgUntie("/position/sea-level-radius-ft");
406 fgUntie("/orientation/roll-deg");
407 fgUntie("/orientation/pitch-deg");
408 fgUntie("/orientation/heading-deg");
409 fgUntie("/orientation/track-deg");
410 fgUntie("/orientation/roll-rate-degps");
411 fgUntie("/orientation/pitch-rate-degps");
412 fgUntie("/orientation/yaw-rate-degps");
413 fgUntie("/orientation/side-slip-rad");
414 fgUntie("/orientation/side-slip-deg");
415 fgUntie("/orientation/alpha-deg");
416 fgUntie("/velocities/airspeed-kt");
417 fgUntie("/velocities/groundspeed-kt");
418 fgUntie("/velocities/mach");
419 fgUntie("/velocities/speed-north-fps");
420 fgUntie("/velocities/speed-east-fps");
421 fgUntie("/velocities/speed-down-fps");
422 fgUntie("/velocities/uBody-fps");
423 fgUntie("/velocities/vBody-fps");
424 fgUntie("/velocities/wBody-fps");
425 fgUntie("/velocities/vertical-speed-fps");
426 fgUntie("/velocities/glideslope");
427 fgUntie("/accelerations/nlf");
428 fgUntie("/accelerations/pilot/x-accel-fps_sec");
429 fgUntie("/accelerations/pilot/y-accel-fps_sec");
430 fgUntie("/accelerations/pilot/z-accel-fps_sec");
431 fgUntie("/accelerations/ned/north-accel-fps_sec");
432 fgUntie("/accelerations/ned/east-accel-fps_sec");
433 fgUntie("/accelerations/ned/down-accel-fps_sec");
437 * Update the state of the FDM (i.e. run the equations of motion).
440 FGInterface::update (double dt)
442 SG_LOG(SG_FLIGHT, SG_ALERT, "dummy update() ... SHOULDN'T BE CALLED!");
446 void FGInterface::_updatePositionM(const SGVec3d& cartPos)
448 TrackComputer tracker( track, geodetic_position_v );
449 cartesian_position_v = cartPos;
450 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
451 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
452 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
453 _update_ground_elev_at_pos();
457 void FGInterface::_updatePosition(const SGGeod& geod)
459 TrackComputer tracker( track, geodetic_position_v );
460 geodetic_position_v = geod;
461 cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
462 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
464 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
465 _update_ground_elev_at_pos();
469 void FGInterface::_updatePosition(const SGGeoc& geoc)
471 TrackComputer tracker( track, geodetic_position_v );
472 geocentric_position_v = geoc;
473 cartesian_position_v = SGVec3d::fromGeoc(geocentric_position_v);
474 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
476 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
477 _update_ground_elev_at_pos();
481 void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
483 _updatePosition(SGGeod::fromRadFt(lon, lat, alt));
487 void FGInterface::_updateGeocentricPosition( double lat, double lon,
490 _updatePosition(SGGeoc::fromRadFt(lon, lat, get_Sea_level_radius() + alt));
493 void FGInterface::_update_ground_elev_at_pos( void ) {
494 double groundlevel_m = get_groundlevel_m(geodetic_position_v);
495 _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
499 void FGInterface::set_Latitude(double lat) {
500 geodetic_position_v.setLatitudeRad(lat);
503 void FGInterface::set_Longitude(double lon) {
504 geodetic_position_v.setLongitudeRad(lon);
507 void FGInterface::set_Altitude(double alt) {
508 geodetic_position_v.setElevationFt(alt);
511 void FGInterface::set_AltitudeAGL(double altagl) {
516 void FGInterface::set_V_calibrated_kts(double vc) {
517 v_calibrated_kts = vc;
520 void FGInterface::set_Mach_number(double mach) {
524 void FGInterface::set_Velocities_Local( double north,
527 v_local_v[0] = north;
532 void FGInterface::set_Velocities_Wind_Body( double u,
535 v_wind_body_v[0] = u;
536 v_wind_body_v[1] = v;
537 v_wind_body_v[2] = w;
541 void FGInterface::set_Euler_Angles( double phi,
544 euler_angles_v[0] = phi;
545 euler_angles_v[1] = theta;
546 euler_angles_v[2] = psi;
550 void FGInterface::set_Climb_Rate( double roc) {
554 void FGInterface::set_Gamma_vert_rad( double gamma) {
555 gamma_vert_rad = gamma;
558 void FGInterface::set_Static_pressure(double p) { static_pressure = p; }
559 void FGInterface::set_Static_temperature(double T) { static_temperature = T; }
560 void FGInterface::set_Density(double rho) { density = rho; }
562 void FGInterface::set_Velocities_Local_Airmass (double wnorth,
565 v_local_airmass_v[0] = wnorth;
566 v_local_airmass_v[1] = weast;
567 v_local_airmass_v[2] = wdown;
571 void FGInterface::_busdump(void) {
573 SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rp_body_v: " << d_cg_rp_body_v);
574 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_local_v: " << v_dot_local_v);
575 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_body_v: " << v_dot_body_v);
576 SG_LOG(SG_FLIGHT,SG_INFO,"a_cg_body_v: " << a_cg_body_v);
577 SG_LOG(SG_FLIGHT,SG_INFO,"a_pilot_body_v: " << a_pilot_body_v);
578 SG_LOG(SG_FLIGHT,SG_INFO,"n_cg_body_v: " << n_cg_body_v);
579 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v: " << v_local_v);
580 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_rel_ground_v: " << v_local_rel_ground_v);
581 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_airmass_v: " << v_local_airmass_v);
582 SG_LOG(SG_FLIGHT,SG_INFO,"v_wind_body_v: " << v_wind_body_v);
583 SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v: " << omega_body_v);
584 SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v: " << euler_rates_v);
585 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v: " << geocentric_rates_v);
586 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v: " << geocentric_position_v);
587 SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v: " << geodetic_position_v);
588 SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v: " << euler_angles_v);
590 SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << nlf );
591 SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << v_rel_wind );
592 SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << v_true_kts );
593 SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << v_ground_speed );
594 SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << v_equiv_kts );
595 SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << v_calibrated_kts );
596 SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << alpha );
597 SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << beta );
598 SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << gamma_vert_rad );
599 SG_LOG(SG_FLIGHT,SG_INFO,"density: " << density );
600 SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << mach_number );
601 SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << static_pressure );
602 SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << total_pressure );
603 SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << dynamic_pressure );
604 SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << static_temperature );
605 SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << total_temperature );
606 SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << sea_level_radius );
607 SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << earth_position_angle );
608 SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << runway_altitude );
609 SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << climb_rate );
610 SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << altitude_agl );
614 FGInterface::prepare_ground_cache_m(double startSimTime, double endSimTime,
615 const double pt[3], double rad)
617 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
622 FGInterface::prepare_ground_cache_ft(double startSimTime, double endSimTime,
623 const double pt[3], double rad)
625 // Convert units and do the real work.
626 SGVec3d pt_ft = SG_FEET_TO_METER*SGVec3d(pt);
627 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
628 pt_ft, rad*SG_FEET_TO_METER);
632 FGInterface::is_valid_m(double *ref_time, double pt[3], double *rad)
635 bool valid = ground_cache.is_valid(*ref_time, _pt, *rad);
640 bool FGInterface::is_valid_ft(double *ref_time, double pt[3], double *rad)
642 // Convert units and do the real work.
644 bool found_ground = ground_cache.is_valid(*ref_time, _pt, *rad);
645 assign(pt, SG_METER_TO_FEET*_pt);
646 *rad *= SG_METER_TO_FEET;
651 FGInterface::get_cat_m(double t, const double pt[3],
652 double end[2][3], double vel[2][3])
654 SGVec3d _end[2], _vel[2];
655 double dist = ground_cache.get_cat(t, SGVec3d(pt), _end, _vel);
656 for (int k=0; k<2; ++k) {
657 assign( end[k], _end[k] );
658 assign( vel[k], _vel[k] );
664 FGInterface::get_cat_ft(double t, const double pt[3],
665 double end[2][3], double vel[2][3])
667 // Convert units and do the real work.
668 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
669 SGVec3d _end[2], _vel[2];
670 double dist = ground_cache.get_cat(t, pt_m, _end, _vel);
671 for (int k=0; k<2; ++k) {
672 assign( end[k], SG_METER_TO_FEET*_end[k] );
673 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
675 return dist*SG_METER_TO_FEET;
679 FGInterface::get_body_m(double t, simgear::BVHNode::Id id,
680 double bodyToWorld[16], double linearVel[3],
681 double angularVel[3])
683 SGMatrixd _bodyToWorld;
684 SGVec3d _linearVel, _angularVel;
685 if (!ground_cache.get_body(t, _bodyToWorld, _linearVel, _angularVel, id))
688 assign(linearVel, _linearVel);
689 assign(angularVel, _angularVel);
690 for (unsigned i = 0; i < 16; ++i)
691 bodyToWorld[i] = _bodyToWorld.data()[i];
697 FGInterface::get_agl_m(double t, const double pt[3], double max_altoff,
698 double contact[3], double normal[3],
699 double linearVel[3], double angularVel[3],
700 SGMaterial const*& material, simgear::BVHNode::Id& id)
702 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
703 SGVec3d _contact, _normal, _linearVel, _angularVel;
705 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
706 _angularVel, id, material);
707 // correct the linear velocity, since the line intersector delivers
708 // values for the start point and the get_agl function should
709 // traditionally deliver for the contact point
710 _linearVel += cross(_angularVel, _contact - pt_m);
712 assign(contact, _contact);
713 assign(normal, _normal);
714 assign(linearVel, _linearVel);
715 assign(angularVel, _angularVel);
720 FGInterface::get_agl_ft(double t, const double pt[3], double max_altoff,
721 double contact[3], double normal[3],
722 double linearVel[3], double angularVel[3],
723 SGMaterial const*& material, simgear::BVHNode::Id& id)
725 // Convert units and do the real work.
726 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
727 pt_m *= SG_FEET_TO_METER;
728 SGVec3d _contact, _normal, _linearVel, _angularVel;
730 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
731 _angularVel, id, material);
732 // correct the linear velocity, since the line intersector delivers
733 // values for the start point and the get_agl function should
734 // traditionally deliver for the contact point
735 _linearVel += cross(_angularVel, _contact - pt_m);
737 // Convert units back ...
738 assign( contact, SG_METER_TO_FEET*_contact );
739 assign( normal, _normal );
740 assign( linearVel, SG_METER_TO_FEET*_linearVel );
741 assign( angularVel, _angularVel );
746 FGInterface::get_nearest_m(double t, const double pt[3], double maxDist,
747 double contact[3], double normal[3],
748 double linearVel[3], double angularVel[3],
749 SGMaterial const*& material,
750 simgear::BVHNode::Id& id)
752 SGVec3d _contact, _linearVel, _angularVel;
753 if (!ground_cache.get_nearest(t, SGVec3d(pt), maxDist, _contact, _linearVel,
754 _angularVel, id, material))
757 assign(contact, _contact);
758 assign(linearVel, _linearVel);
759 assign(angularVel, _angularVel);
764 FGInterface::get_nearest_ft(double t, const double pt[3], double maxDist,
765 double contact[3], double normal[3],
766 double linearVel[3], double angularVel[3],
767 SGMaterial const*& material,
768 simgear::BVHNode::Id& id)
770 SGVec3d _contact, _linearVel, _angularVel;
771 if (!ground_cache.get_nearest(t, SG_FEET_TO_METER*SGVec3d(pt),
772 SG_FEET_TO_METER*maxDist, _contact, _linearVel,
773 _angularVel, id, material))
776 assign(contact, SG_METER_TO_FEET*_contact);
777 assign(linearVel, SG_METER_TO_FEET*_linearVel);
778 assign(angularVel, _angularVel);
783 FGInterface::get_groundlevel_m(double lat, double lon, double alt)
785 return get_groundlevel_m(SGGeod::fromRadM(lon, lat, alt));
789 FGInterface::get_groundlevel_m(const SGGeod& geod)
791 // Compute the cartesian position of the given lat/lon/alt.
792 SGVec3d pos = SGVec3d::fromGeod(geod);
794 // FIXME: how to handle t - ref_time differences ???
796 double ref_time = 0, radius;
797 // Prepare the ground cache for that position.
798 if (!is_valid_m(&ref_time, cpos.data(), &radius)) {
799 double startTime = ref_time;
800 double endTime = startTime + 1;
801 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), 10);
802 /// This is most likely the case when the given altitude is
803 /// too low, try with a new altitude of 10000m, that should be
804 /// sufficient to find a ground level below everywhere on our planet
806 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
807 /// If there is still no ground, return sea level radius
808 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), 10))
811 } else if (radius*radius <= distSqr(pos, cpos)) {
812 double startTime = ref_time;
813 double endTime = startTime + 1;
815 /// We reuse the old radius value, but only if it is at least 10 Meters ..
816 if (!(10 < radius)) // Well this strange compare is nan safe
819 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), radius);
820 /// This is most likely the case when the given altitude is
821 /// too low, try with a new altitude of 10000m, that should be
822 /// sufficient to find a ground level below everywhere on our planet
824 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
825 /// If there is still no ground, return sea level radius
826 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), radius))
831 double contact[3], normal[3], vel[3], angvel[3];
832 const SGMaterial* material;
833 simgear::BVHNode::Id id;
834 // Ignore the return value here, since it just tells us if
835 // the returns stem from the groundcache or from the coarse
836 // computations below the groundcache. The contact point is still something
837 // valid, the normals and the other returns just contain some defaults.
838 get_agl_m(ref_time, pos.data(), 2.0, contact, normal, vel, angvel,
840 return SGGeod::fromCart(SGVec3d(contact)).getElevationM();
844 FGInterface::caught_wire_m(double t, const double pt[4][3])
847 for (int i=0; i<4; ++i)
848 pt_m[i] = SGVec3d(pt[i]);
850 return ground_cache.caught_wire(t, pt_m);
854 FGInterface::caught_wire_ft(double t, const double pt[4][3])
856 // Convert units and do the real work.
858 for (int i=0; i<4; ++i)
859 pt_m[i] = SG_FEET_TO_METER*SGVec3d(pt[i]);
861 return ground_cache.caught_wire(t, pt_m);
865 FGInterface::get_wire_ends_m(double t, double end[2][3], double vel[2][3])
867 SGVec3d _end[2], _vel[2];
868 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
869 for (int k=0; k<2; ++k) {
870 assign( end[k], _end[k] );
871 assign( vel[k], _vel[k] );
877 FGInterface::get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
879 // Convert units and do the real work.
880 SGVec3d _end[2], _vel[2];
881 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
882 for (int k=0; k<2; ++k) {
883 assign( end[k], SG_METER_TO_FEET*_end[k] );
884 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
890 FGInterface::release_wire(void)
892 ground_cache.release_wire();
895 void fgToggleFDMdataLogging(void) {
896 cur_fdm_state->ToggleDataLogging();