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;
127 FGInterface::init () {}
130 * Initialize the state of the FDM.
132 * Subclasses of FGInterface may do their own, additional initialization,
133 * but there is some that is common to all. Normally, they should call
134 * this before they begin their own init to make sure the basic structures
135 * are set up properly.
138 FGInterface::common_init ()
140 SG_LOG( SG_FLIGHT, SG_INFO, "Start common FDM init" );
144 ground_cache.set_cache_time_offset(globals->get_sim_time_sec());
146 // Set initial position
147 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing position..." );
148 double lon = fgGetDouble("/sim/presets/longitude-deg")
149 * SGD_DEGREES_TO_RADIANS;
150 double lat = fgGetDouble("/sim/presets/latitude-deg")
151 * SGD_DEGREES_TO_RADIANS;
152 double alt_ft = fgGetDouble("/sim/presets/altitude-ft");
153 double alt_m = alt_ft * SG_FEET_TO_METER;
154 set_Longitude( lon );
156 SG_LOG( SG_FLIGHT, SG_INFO, "Checking for lon = "
157 << lon*SGD_RADIANS_TO_DEGREES << "deg, lat = "
158 << lat*SGD_RADIANS_TO_DEGREES << "deg, alt = "
161 double ground_elev_m = get_groundlevel_m(lat, lon, alt_m);
162 double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET;
163 _set_Runway_altitude ( ground_elev_ft );
164 if ( fgGetBool("/sim/presets/onground") || alt_ft < ground_elev_ft ) {
165 fgSetDouble("/position/altitude-ft", ground_elev_ft + 0.1);
166 set_Altitude( ground_elev_ft + 0.1);
168 set_Altitude( alt_ft );
171 // Set ground elevation
172 SG_LOG( SG_FLIGHT, SG_INFO,
173 "...initializing ground elevation to " << ground_elev_ft
176 // Set sea-level radius
177 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing sea-level radius..." );
178 SG_LOG( SG_FLIGHT, SG_INFO, " lat = "
179 << fgGetDouble("/sim/presets/latitude-deg")
180 << " alt = " << get_Altitude() );
181 double slr = SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v);
182 _set_Sea_level_radius( slr * SG_METER_TO_FEET );
184 // Set initial velocities
185 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing velocities..." );
186 if ( !fgHasNode("/sim/presets/speed-set") ) {
187 set_V_calibrated_kts(0.0);
189 const string speedset = fgGetString("/sim/presets/speed-set");
190 if ( speedset == "knots" || speedset == "KNOTS" ) {
191 set_V_calibrated_kts( fgGetDouble("/sim/presets/airspeed-kt") );
192 } else if ( speedset == "mach" || speedset == "MACH" ) {
193 set_Mach_number( fgGetDouble("/sim/presets/mach") );
194 } else if ( speedset == "UVW" || speedset == "uvw" ) {
195 set_Velocities_Wind_Body(
196 fgGetDouble("/sim/presets/uBody-fps"),
197 fgGetDouble("/sim/presets/vBody-fps"),
198 fgGetDouble("/sim/presets/wBody-fps") );
199 } else if ( speedset == "NED" || speedset == "ned" ) {
200 set_Velocities_Local(
201 fgGetDouble("/sim/presets/speed-north-fps"),
202 fgGetDouble("/sim/presets/speed-east-fps"),
203 fgGetDouble("/sim/presets/speed-down-fps") );
205 SG_LOG( SG_FLIGHT, SG_ALERT,
206 "Unrecognized value for /sim/presets/speed-set: "
208 set_V_calibrated_kts( 0.0 );
212 // Set initial Euler angles
213 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing Euler angles..." );
214 set_Euler_Angles( fgGetDouble("/sim/presets/roll-deg")
215 * SGD_DEGREES_TO_RADIANS,
216 fgGetDouble("/sim/presets/pitch-deg")
217 * SGD_DEGREES_TO_RADIANS,
218 fgGetDouble("/sim/presets/heading-deg")
219 * SGD_DEGREES_TO_RADIANS );
221 SG_LOG( SG_FLIGHT, SG_INFO, "End common FDM init" );
226 * Bind getters and setters to properties.
228 * The bind() method will be invoked after init(). Note that unlike
229 * the usual implementations of FGSubsystem::bind(), this method does
230 * not automatically pick up existing values for the properties at
231 * bind time; instead, all values are set explicitly in the init()
240 fgTie("/position/latitude-deg", this,
241 &FGInterface::get_Latitude_deg,
242 &FGInterface::set_Latitude_deg,
244 fgSetArchivable("/position/latitude-deg");
245 fgTie("/position/longitude-deg", this,
246 &FGInterface::get_Longitude_deg,
247 &FGInterface::set_Longitude_deg,
249 fgSetArchivable("/position/longitude-deg");
250 fgTie("/position/altitude-ft", this,
251 &FGInterface::get_Altitude,
252 &FGInterface::set_Altitude,
254 fgSetArchivable("/position/altitude-ft");
255 fgTie("/position/altitude-agl-ft", this,
256 &FGInterface::get_Altitude_AGL); // read-only
257 fgSetArchivable("/position/ground-elev-ft");
258 fgTie("/position/ground-elev-ft", this,
259 &FGInterface::get_Runway_altitude); // read-only
260 fgSetArchivable("/position/ground-elev-m");
261 fgTie("/position/ground-elev-m", this,
262 &FGInterface::get_Runway_altitude_m); // read-only
263 fgTie("/environment/ground-elevation-m", this,
264 &FGInterface::get_Runway_altitude_m); // read-only
265 fgSetArchivable("/position/sea-level-radius-ft");
266 fgTie("/position/sea-level-radius-ft", this,
267 &FGInterface::get_Sea_level_radius); // read-only
270 fgTie("/orientation/roll-deg", this,
271 &FGInterface::get_Phi_deg,
272 &FGInterface::set_Phi_deg);
273 fgSetArchivable("/orientation/roll-deg");
274 fgTie("/orientation/pitch-deg", this,
275 &FGInterface::get_Theta_deg,
276 &FGInterface::set_Theta_deg);
277 fgSetArchivable("/orientation/pitch-deg");
278 fgTie("/orientation/heading-deg", this,
279 &FGInterface::get_Psi_deg,
280 &FGInterface::set_Psi_deg);
281 fgSetArchivable("/orientation/heading-deg");
282 fgTie("/orientation/track-deg", this,
283 &FGInterface::get_Track);
285 // Body-axis "euler rates" (rotation speed, but in a funny
287 fgTie("/orientation/roll-rate-degps", this,
288 &FGInterface::get_Phi_dot_degps);
289 fgTie("/orientation/pitch-rate-degps", this,
290 &FGInterface::get_Theta_dot_degps);
291 fgTie("/orientation/yaw-rate-degps", this,
292 &FGInterface::get_Psi_dot_degps);
294 // Ground speed knots
295 fgTie("/velocities/groundspeed-kt", this,
296 &FGInterface::get_V_ground_speed_kt);
298 // Calibrated airspeed
299 fgTie("/velocities/airspeed-kt", this,
300 &FGInterface::get_V_calibrated_kts,
301 &FGInterface::set_V_calibrated_kts,
305 fgTie("/velocities/mach", this,
306 &FGInterface::get_Mach_number,
307 &FGInterface::set_Mach_number,
311 // fgTie("/velocities/speed-north-fps", this,
312 // &FGInterface::get_V_north,
313 // &FGInterface::set_V_north);
314 // fgSetArchivable("/velocities/speed-north-fps");
315 // fgTie("/velocities/speed-east-fps", this,
316 // &FGInterface::get_V_east,
317 // &FGInterface::set_V_east);
318 // fgSetArchivable("/velocities/speed-east-fps");
319 // fgTie("/velocities/speed-down-fps", this,
320 // &FGInterface::get_V_down,
321 // &FGInterface::set_V_down);
322 // fgSetArchivable("/velocities/speed-down-fps");
323 // FIXME: Temporarily read-only, until the
324 // incompatibilities between JSBSim and
325 // LaRCSim are fixed (LaRCSim adds the
326 // earth's rotation to the east velocity).
327 fgTie("/velocities/speed-north-fps", this,
328 &FGInterface::get_V_north);
329 fgTie("/velocities/speed-east-fps", this,
330 &FGInterface::get_V_east);
331 fgTie("/velocities/speed-down-fps", this,
332 &FGInterface::get_V_down);
335 // FIXME: temporarily archivable, until
336 // the NED problem is fixed.
337 fgTie("/velocities/uBody-fps", this,
338 &FGInterface::get_uBody,
339 &FGInterface::set_uBody,
341 fgSetArchivable("/velocities/uBody-fps");
342 fgTie("/velocities/vBody-fps", this,
343 &FGInterface::get_vBody,
344 &FGInterface::set_vBody,
346 fgSetArchivable("/velocities/vBody-fps");
347 fgTie("/velocities/wBody-fps", this,
348 &FGInterface::get_wBody,
349 &FGInterface::set_wBody,
351 fgSetArchivable("/velocities/wBody-fps");
353 // Climb and slip (read-only)
354 fgTie("/velocities/vertical-speed-fps", this,
355 &FGInterface::get_Climb_Rate,
356 &FGInterface::set_Climb_Rate );
357 fgTie("/velocities/glideslope", this,
358 &FGInterface::get_Gamma_vert_rad,
359 &FGInterface::set_Gamma_vert_rad );
360 fgTie("/orientation/side-slip-rad", this,
361 &FGInterface::get_Beta); // read-only
362 fgTie("/orientation/side-slip-deg", this,
363 &FGInterface::get_Beta_deg); // read-only
364 fgTie("/orientation/alpha-deg", this,
365 &FGInterface::get_Alpha_deg); // read-only
366 fgTie("/accelerations/nlf", this,
367 &FGInterface::get_Nlf); // read-only
370 fgTie("/accelerations/ned/north-accel-fps_sec",
371 this, &FGInterface::get_V_dot_north);
372 fgTie("/accelerations/ned/east-accel-fps_sec",
373 this, &FGInterface::get_V_dot_east);
374 fgTie("/accelerations/ned/down-accel-fps_sec",
375 this, &FGInterface::get_V_dot_down);
377 // Pilot accelerations
378 fgTie("/accelerations/pilot/x-accel-fps_sec",
379 this, &FGInterface::get_A_X_pilot);
380 fgTie("/accelerations/pilot/y-accel-fps_sec",
381 this, &FGInterface::get_A_Y_pilot);
382 fgTie("/accelerations/pilot/z-accel-fps_sec",
383 this, &FGInterface::get_A_Z_pilot);
389 * Unbind any properties bound to this FDM.
391 * This method allows the FDM to release properties so that a new
392 * FDM can bind them instead.
395 FGInterface::unbind ()
399 fgUntie("/position/latitude-deg");
400 fgUntie("/position/longitude-deg");
401 fgUntie("/position/altitude-ft");
402 fgUntie("/position/altitude-agl-ft");
403 fgUntie("/position/ground-elev-ft");
404 fgUntie("/position/ground-elev-m");
405 fgUntie("/environment/ground-elevation-m");
406 fgUntie("/position/sea-level-radius-ft");
407 fgUntie("/orientation/roll-deg");
408 fgUntie("/orientation/pitch-deg");
409 fgUntie("/orientation/heading-deg");
410 fgUntie("/orientation/track-deg");
411 fgUntie("/orientation/roll-rate-degps");
412 fgUntie("/orientation/pitch-rate-degps");
413 fgUntie("/orientation/yaw-rate-degps");
414 fgUntie("/orientation/side-slip-rad");
415 fgUntie("/orientation/side-slip-deg");
416 fgUntie("/orientation/alpha-deg");
417 fgUntie("/velocities/airspeed-kt");
418 fgUntie("/velocities/groundspeed-kt");
419 fgUntie("/velocities/mach");
420 fgUntie("/velocities/speed-north-fps");
421 fgUntie("/velocities/speed-east-fps");
422 fgUntie("/velocities/speed-down-fps");
423 fgUntie("/velocities/uBody-fps");
424 fgUntie("/velocities/vBody-fps");
425 fgUntie("/velocities/wBody-fps");
426 fgUntie("/velocities/vertical-speed-fps");
427 fgUntie("/velocities/glideslope");
428 fgUntie("/accelerations/nlf");
429 fgUntie("/accelerations/pilot/x-accel-fps_sec");
430 fgUntie("/accelerations/pilot/y-accel-fps_sec");
431 fgUntie("/accelerations/pilot/z-accel-fps_sec");
432 fgUntie("/accelerations/ned/north-accel-fps_sec");
433 fgUntie("/accelerations/ned/east-accel-fps_sec");
434 fgUntie("/accelerations/ned/down-accel-fps_sec");
438 * Update the state of the FDM (i.e. run the equations of motion).
441 FGInterface::update (double dt)
443 SG_LOG(SG_FLIGHT, SG_ALERT, "dummy update() ... SHOULDN'T BE CALLED!");
447 void FGInterface::_updatePositionM(const SGVec3d& cartPos)
449 TrackComputer tracker( track, geodetic_position_v );
450 cartesian_position_v = cartPos;
451 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
452 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
453 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
454 _update_ground_elev_at_pos();
458 void FGInterface::_updatePosition(const SGGeod& geod)
460 TrackComputer tracker( track, geodetic_position_v );
461 geodetic_position_v = geod;
462 cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
463 geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
465 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
466 _update_ground_elev_at_pos();
470 void FGInterface::_updatePosition(const SGGeoc& geoc)
472 TrackComputer tracker( track, geodetic_position_v );
473 geocentric_position_v = geoc;
474 cartesian_position_v = SGVec3d::fromGeoc(geocentric_position_v);
475 geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
477 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
478 _update_ground_elev_at_pos();
482 void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
484 _updatePosition(SGGeod::fromRadFt(lon, lat, alt));
488 void FGInterface::_updateGeocentricPosition( double lat, double lon,
491 _updatePosition(SGGeoc::fromRadFt(lon, lat, get_Sea_level_radius() + alt));
494 void FGInterface::_update_ground_elev_at_pos( void ) {
495 double groundlevel_m = get_groundlevel_m(geodetic_position_v);
496 _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
500 void FGInterface::set_Latitude(double lat) {
501 geodetic_position_v.setLatitudeRad(lat);
504 void FGInterface::set_Longitude(double lon) {
505 geodetic_position_v.setLongitudeRad(lon);
508 void FGInterface::set_Altitude(double alt) {
509 geodetic_position_v.setElevationFt(alt);
512 void FGInterface::set_AltitudeAGL(double altagl) {
517 void FGInterface::set_V_calibrated_kts(double vc) {
518 v_calibrated_kts = vc;
521 void FGInterface::set_Mach_number(double mach) {
525 void FGInterface::set_Velocities_Local( double north,
528 v_local_v[0] = north;
533 void FGInterface::set_Velocities_Wind_Body( double u,
536 v_wind_body_v[0] = u;
537 v_wind_body_v[1] = v;
538 v_wind_body_v[2] = w;
542 void FGInterface::set_Euler_Angles( double phi,
545 euler_angles_v[0] = phi;
546 euler_angles_v[1] = theta;
547 euler_angles_v[2] = psi;
551 void FGInterface::set_Climb_Rate( double roc) {
555 void FGInterface::set_Gamma_vert_rad( double gamma) {
556 gamma_vert_rad = gamma;
559 void FGInterface::set_Static_pressure(double p) { static_pressure = p; }
560 void FGInterface::set_Static_temperature(double T) { static_temperature = T; }
561 void FGInterface::set_Density(double rho) { density = rho; }
563 void FGInterface::set_Velocities_Local_Airmass (double wnorth,
566 v_local_airmass_v[0] = wnorth;
567 v_local_airmass_v[1] = weast;
568 v_local_airmass_v[2] = wdown;
572 void FGInterface::_busdump(void) {
574 SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rp_body_v: " << d_cg_rp_body_v);
575 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_local_v: " << v_dot_local_v);
576 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_body_v: " << v_dot_body_v);
577 SG_LOG(SG_FLIGHT,SG_INFO,"a_cg_body_v: " << a_cg_body_v);
578 SG_LOG(SG_FLIGHT,SG_INFO,"a_pilot_body_v: " << a_pilot_body_v);
579 SG_LOG(SG_FLIGHT,SG_INFO,"n_cg_body_v: " << n_cg_body_v);
580 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v: " << v_local_v);
581 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_rel_ground_v: " << v_local_rel_ground_v);
582 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_airmass_v: " << v_local_airmass_v);
583 SG_LOG(SG_FLIGHT,SG_INFO,"v_wind_body_v: " << v_wind_body_v);
584 SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v: " << omega_body_v);
585 SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v: " << euler_rates_v);
586 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v: " << geocentric_rates_v);
587 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v: " << geocentric_position_v);
588 SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v: " << geodetic_position_v);
589 SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v: " << euler_angles_v);
591 SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << nlf );
592 SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << v_rel_wind );
593 SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << v_true_kts );
594 SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << v_ground_speed );
595 SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << v_equiv_kts );
596 SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << v_calibrated_kts );
597 SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << alpha );
598 SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << beta );
599 SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << gamma_vert_rad );
600 SG_LOG(SG_FLIGHT,SG_INFO,"density: " << density );
601 SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << mach_number );
602 SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << static_pressure );
603 SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << total_pressure );
604 SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << dynamic_pressure );
605 SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << static_temperature );
606 SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << total_temperature );
607 SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << sea_level_radius );
608 SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << earth_position_angle );
609 SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << runway_altitude );
610 SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << climb_rate );
611 SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << altitude_agl );
615 FGInterface::prepare_ground_cache_m(double startSimTime, double endSimTime,
616 const double pt[3], double rad)
618 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
623 FGInterface::prepare_ground_cache_ft(double startSimTime, double endSimTime,
624 const double pt[3], double rad)
626 // Convert units and do the real work.
627 SGVec3d pt_ft = SG_FEET_TO_METER*SGVec3d(pt);
628 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
629 pt_ft, rad*SG_FEET_TO_METER);
633 FGInterface::is_valid_m(double *ref_time, double pt[3], double *rad)
636 bool valid = ground_cache.is_valid(*ref_time, _pt, *rad);
641 bool FGInterface::is_valid_ft(double *ref_time, double pt[3], double *rad)
643 // Convert units and do the real work.
645 bool found_ground = ground_cache.is_valid(*ref_time, _pt, *rad);
646 assign(pt, SG_METER_TO_FEET*_pt);
647 *rad *= SG_METER_TO_FEET;
652 FGInterface::get_cat_m(double t, const double pt[3],
653 double end[2][3], double vel[2][3])
655 SGVec3d _end[2], _vel[2];
656 double dist = ground_cache.get_cat(t, SGVec3d(pt), _end, _vel);
657 for (int k=0; k<2; ++k) {
658 assign( end[k], _end[k] );
659 assign( vel[k], _vel[k] );
665 FGInterface::get_cat_ft(double t, const double pt[3],
666 double end[2][3], double vel[2][3])
668 // Convert units and do the real work.
669 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
670 SGVec3d _end[2], _vel[2];
671 double dist = ground_cache.get_cat(t, pt_m, _end, _vel);
672 for (int k=0; k<2; ++k) {
673 assign( end[k], SG_METER_TO_FEET*_end[k] );
674 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
676 return dist*SG_METER_TO_FEET;
680 FGInterface::get_body_m(double t, simgear::BVHNode::Id id,
681 double bodyToWorld[16], double linearVel[3],
682 double angularVel[3])
684 SGMatrixd _bodyToWorld;
685 SGVec3d _linearVel, _angularVel;
686 if (!ground_cache.get_body(t, _bodyToWorld, _linearVel, _angularVel, id))
689 assign(linearVel, _linearVel);
690 assign(angularVel, _angularVel);
691 for (unsigned i = 0; i < 16; ++i)
692 bodyToWorld[i] = _bodyToWorld.data()[i];
698 FGInterface::get_agl_m(double t, const double pt[3], double max_altoff,
699 double contact[3], double normal[3],
700 double linearVel[3], double angularVel[3],
701 SGMaterial const*& material, simgear::BVHNode::Id& id)
703 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
704 SGVec3d _contact, _normal, _linearVel, _angularVel;
706 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
707 _angularVel, id, material);
708 // correct the linear velocity, since the line intersector delivers
709 // values for the start point and the get_agl function should
710 // traditionally deliver for the contact point
711 _linearVel += cross(_angularVel, _contact - pt_m);
713 assign(contact, _contact);
714 assign(normal, _normal);
715 assign(linearVel, _linearVel);
716 assign(angularVel, _angularVel);
721 FGInterface::get_agl_ft(double t, const double pt[3], double max_altoff,
722 double contact[3], double normal[3],
723 double linearVel[3], double angularVel[3],
724 SGMaterial const*& material, simgear::BVHNode::Id& id)
726 // Convert units and do the real work.
727 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
728 pt_m *= SG_FEET_TO_METER;
729 SGVec3d _contact, _normal, _linearVel, _angularVel;
731 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
732 _angularVel, id, material);
733 // correct the linear velocity, since the line intersector delivers
734 // values for the start point and the get_agl function should
735 // traditionally deliver for the contact point
736 _linearVel += cross(_angularVel, _contact - pt_m);
738 // Convert units back ...
739 assign( contact, SG_METER_TO_FEET*_contact );
740 assign( normal, _normal );
741 assign( linearVel, SG_METER_TO_FEET*_linearVel );
742 assign( angularVel, _angularVel );
747 FGInterface::get_nearest_m(double t, const double pt[3], double maxDist,
748 double contact[3], double normal[3],
749 double linearVel[3], double angularVel[3],
750 SGMaterial const*& material,
751 simgear::BVHNode::Id& id)
753 SGVec3d _contact, _linearVel, _angularVel;
754 if (!ground_cache.get_nearest(t, SGVec3d(pt), maxDist, _contact, _linearVel,
755 _angularVel, id, material))
758 assign(contact, _contact);
759 assign(linearVel, _linearVel);
760 assign(angularVel, _angularVel);
765 FGInterface::get_nearest_ft(double t, const double pt[3], double maxDist,
766 double contact[3], double normal[3],
767 double linearVel[3], double angularVel[3],
768 SGMaterial const*& material,
769 simgear::BVHNode::Id& id)
771 SGVec3d _contact, _linearVel, _angularVel;
772 if (!ground_cache.get_nearest(t, SG_FEET_TO_METER*SGVec3d(pt),
773 SG_FEET_TO_METER*maxDist, _contact, _linearVel,
774 _angularVel, id, material))
777 assign(contact, SG_METER_TO_FEET*_contact);
778 assign(linearVel, SG_METER_TO_FEET*_linearVel);
779 assign(angularVel, _angularVel);
784 FGInterface::get_groundlevel_m(double lat, double lon, double alt)
786 return get_groundlevel_m(SGGeod::fromRadM(lon, lat, alt));
790 FGInterface::get_groundlevel_m(const SGGeod& geod)
792 // Compute the cartesian position of the given lat/lon/alt.
793 SGVec3d pos = SGVec3d::fromGeod(geod);
795 // FIXME: how to handle t - ref_time differences ???
797 double ref_time = 0, radius;
798 // Prepare the ground cache for that position.
799 if (!is_valid_m(&ref_time, cpos.data(), &radius)) {
800 double startTime = ref_time;
801 double endTime = startTime + 1;
802 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), 10);
803 /// This is most likely the case when the given altitude is
804 /// too low, try with a new altitude of 10000m, that should be
805 /// sufficient to find a ground level below everywhere on our planet
807 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
808 /// If there is still no ground, return sea level radius
809 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), 10))
812 } else if (radius*radius <= distSqr(pos, cpos)) {
813 double startTime = ref_time;
814 double endTime = startTime + 1;
816 /// We reuse the old radius value, but only if it is at least 10 Meters ..
817 if (!(10 < radius)) // Well this strange compare is nan safe
820 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), radius);
821 /// This is most likely the case when the given altitude is
822 /// too low, try with a new altitude of 10000m, that should be
823 /// sufficient to find a ground level below everywhere on our planet
825 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
826 /// If there is still no ground, return sea level radius
827 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), radius))
832 double contact[3], normal[3], vel[3], angvel[3];
833 const SGMaterial* material;
834 simgear::BVHNode::Id id;
835 // Ignore the return value here, since it just tells us if
836 // the returns stem from the groundcache or from the coarse
837 // computations below the groundcache. The contact point is still something
838 // valid, the normals and the other returns just contain some defaults.
839 get_agl_m(ref_time, pos.data(), 2.0, contact, normal, vel, angvel,
841 return SGGeod::fromCart(SGVec3d(contact)).getElevationM();
845 FGInterface::caught_wire_m(double t, const double pt[4][3])
848 for (int i=0; i<4; ++i)
849 pt_m[i] = SGVec3d(pt[i]);
851 return ground_cache.caught_wire(t, pt_m);
855 FGInterface::caught_wire_ft(double t, const double pt[4][3])
857 // Convert units and do the real work.
859 for (int i=0; i<4; ++i)
860 pt_m[i] = SG_FEET_TO_METER*SGVec3d(pt[i]);
862 return ground_cache.caught_wire(t, pt_m);
866 FGInterface::get_wire_ends_m(double t, double end[2][3], double vel[2][3])
868 SGVec3d _end[2], _vel[2];
869 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
870 for (int k=0; k<2; ++k) {
871 assign( end[k], _end[k] );
872 assign( vel[k], _vel[k] );
878 FGInterface::get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
880 // Convert units and do the real work.
881 SGVec3d _end[2], _vel[2];
882 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
883 for (int k=0; k<2; ++k) {
884 assign( end[k], SG_METER_TO_FEET*_end[k] );
885 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
891 FGInterface::release_wire(void)
893 ground_cache.release_wire();
896 void fgToggleFDMdataLogging(void) {
897 cur_fdm_state->ToggleDataLogging();