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>
32 #include <simgear/scene/material/mat.hxx>
33 #include <simgear/io/iochannel.hxx>
35 #include <Scenery/scenery.hxx>
36 #include <Main/globals.hxx>
37 #include <Main/fg_props.hxx>
38 #include <FDM/groundcache.hxx>
41 static inline void assign(double* ptr, const SGVec3d& vec)
49 FGInterface::FGInterface()
54 FGInterface::FGInterface( double dt )
60 FGInterface::~FGInterface() {
61 // unbind(); // FIXME: should be called explicitly
65 FGInterface::_calc_multiloop (double dt)
67 // Since some time the simulation time increments we get here are
68 // already a multiple of the basic update frequency.
69 // So, there is no need to do our own multiloop rounding with all bad
70 // roundoff problems when we already have nearly accurate values.
71 // Only the speedup thing must be still handled here
72 int hz = fgGetInt("/sim/model-hz");
73 double speedup = fgGetDouble("/sim/speed-up");
74 double loops = dt * hz * speedup + delta_loops;
75 int iloops = SGMiscd::roundToInt(loops);
76 delta_loops = loops-iloops; // delta_loops required for speed-ups < 1 (to do one iteration every n-th step)
82 * Set default values for the state of the FDM.
84 * This method is invoked by the constructors.
87 FGInterface::_setup ()
92 _state.d_cg_rp_body_v = SGVec3d::zeros();
93 _state.v_dot_local_v = SGVec3d::zeros();
94 _state.v_dot_body_v = SGVec3d::zeros();
95 _state.a_cg_body_v = SGVec3d::zeros();
96 _state.a_pilot_body_v = SGVec3d::zeros();
97 _state.n_cg_body_v = SGVec3d::zeros();
98 _state.v_local_v = SGVec3d::zeros();
99 _state.v_local_rel_ground_v = SGVec3d::zeros();
100 _state.v_local_airmass_v = SGVec3d::zeros();
101 _state.v_body_v = SGVec3d::zeros();
102 _state.omega_body_v = SGVec3d::zeros();
103 _state.euler_rates_v = SGVec3d::zeros();
104 _state.geocentric_rates_v = SGVec3d::zeros();
105 _state.geodetic_position_v = SGGeod::fromRadM(0, 0, 0);
106 _state.cartesian_position_v = SGVec3d::fromGeod(_state.geodetic_position_v);
107 _state.geocentric_position_v = SGGeoc::fromCart(_state.cartesian_position_v);
108 _state.euler_angles_v = SGVec3d::zeros();
111 _state.v_rel_wind=_state.v_true_kts=0;
112 _state.v_ground_speed=_state.v_equiv_kts=0;
113 _state.v_calibrated_kts=0;
114 _state.alpha=_state.beta=0;
115 _state.gamma_vert_rad=0;
116 _state.density=_state.mach_number=0;
117 _state.static_pressure=_state.total_pressure=0;
118 _state.dynamic_pressure=0;
119 _state.static_temperature=_state.total_temperature=0;
120 _state.sea_level_radius=_state.earth_position_angle=0;
121 _state.runway_altitude=0;
123 _state.altitude_agl=0;
130 FGInterface::init () {}
133 * Initialize the state of the FDM.
135 * Subclasses of FGInterface may do their own, additional initialization,
136 * but there is some that is common to all. Normally, they should call
137 * this before they begin their own init to make sure the basic structures
138 * are set up properly.
141 FGInterface::common_init ()
143 SG_LOG( SG_FLIGHT, SG_INFO, "Start common FDM init" );
147 ground_cache.set_cache_time_offset(globals->get_sim_time_sec());
149 // Set initial position
150 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing position..." );
151 double lon = fgGetDouble("/sim/presets/longitude-deg")
152 * SGD_DEGREES_TO_RADIANS;
153 double lat = fgGetDouble("/sim/presets/latitude-deg")
154 * SGD_DEGREES_TO_RADIANS;
155 double alt_ft = fgGetDouble("/sim/presets/altitude-ft");
156 double alt_m = alt_ft * SG_FEET_TO_METER;
157 set_Longitude( lon );
159 SG_LOG( SG_FLIGHT, SG_INFO, "Checking for lon = "
160 << lon*SGD_RADIANS_TO_DEGREES << "deg, lat = "
161 << lat*SGD_RADIANS_TO_DEGREES << "deg, alt = "
164 double ground_elev_m = get_groundlevel_m(lat, lon, alt_m);
165 double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET;
166 _set_Runway_altitude ( ground_elev_ft );
168 // Set aircraft altitude
169 if ( fgGetBool("/sim/presets/onground") || alt_ft < ground_elev_ft ) {
170 fgSetDouble("/position/altitude-ft", ground_elev_ft + 0.1);
171 set_Altitude( ground_elev_ft + 0.1);
173 set_Altitude( alt_ft );
176 // Set ground elevation
177 SG_LOG( SG_FLIGHT, SG_INFO,
178 "...initializing ground elevation to " << ground_elev_ft
181 // Set sea-level radius
182 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing sea-level radius..." );
183 SG_LOG( SG_FLIGHT, SG_INFO, " lat = "
184 << fgGetDouble("/sim/presets/latitude-deg")
185 << " alt = " << get_Altitude() );
186 double slr = SGGeodesy::SGGeodToSeaLevelRadius(_state.geodetic_position_v);
187 _set_Sea_level_radius( slr * SG_METER_TO_FEET );
189 // Set initial Euler angles
190 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing Euler angles..." );
191 set_Euler_Angles( fgGetDouble("/sim/presets/roll-deg")
192 * SGD_DEGREES_TO_RADIANS,
193 fgGetDouble("/sim/presets/pitch-deg")
194 * SGD_DEGREES_TO_RADIANS,
195 fgGetDouble("/sim/presets/heading-deg")
196 * SGD_DEGREES_TO_RADIANS );
198 // Set initial velocities
199 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing velocities..." );
200 if ( !fgHasNode("/sim/presets/speed-set") ) {
201 set_V_calibrated_kts(0.0);
203 const std::string speedset = fgGetString("/sim/presets/speed-set");
204 if ( speedset == "knots" || speedset == "KNOTS" ) {
205 set_V_calibrated_kts( fgGetDouble("/sim/presets/airspeed-kt") );
206 } else if ( speedset == "mach" || speedset == "MACH" ) {
207 set_Mach_number( fgGetDouble("/sim/presets/mach") );
208 } else if ( speedset == "UVW" || speedset == "uvw" ) {
210 fgGetDouble("/sim/presets/uBody-fps"),
211 fgGetDouble("/sim/presets/vBody-fps"),
212 fgGetDouble("/sim/presets/wBody-fps") );
213 } else if ( speedset == "NED" || speedset == "ned" ) {
214 set_Velocities_Local(
215 fgGetDouble("/sim/presets/speed-north-fps"),
216 fgGetDouble("/sim/presets/speed-east-fps"),
217 fgGetDouble("/sim/presets/speed-down-fps") );
219 SG_LOG( SG_FLIGHT, SG_ALERT,
220 "Unrecognized value for /sim/presets/speed-set: "
222 set_V_calibrated_kts( 0.0 );
226 if ( fgHasNode("/sim/presets/glideslope-deg") )
227 set_Gamma_vert_rad( fgGetDouble("/sim/presets/glideslope-deg")
228 * SGD_DEGREES_TO_RADIANS );
229 else if ( fgHasNode("/sim/presets/speed-set") &&
230 fgHasNode( "/sim/presets/vertical-speed-fps") )
232 set_Climb_Rate( fgGetDouble("/sim/presets/vertical-speed-fps") );
235 SG_LOG( SG_FLIGHT, SG_INFO, "End common FDM init" );
240 * Bind getters and setters to properties.
242 * The bind() method will be invoked after init(). Note that unlike
243 * the usual implementations of FGSubsystem::bind(), this method does
244 * not automatically pick up existing values for the properties at
245 * bind time; instead, all values are set explicitly in the init()
253 _tiedProperties.setRoot(globals->get_props());
255 _tiedProperties.Tie("/position/latitude-deg", this,
256 &FGInterface::get_Latitude_deg,
257 &FGInterface::set_Latitude_deg,
259 fgSetArchivable("/position/latitude-deg");
260 _tiedProperties.Tie("/position/longitude-deg", this,
261 &FGInterface::get_Longitude_deg,
262 &FGInterface::set_Longitude_deg,
264 fgSetArchivable("/position/longitude-deg");
265 _tiedProperties.Tie("/position/altitude-ft", this,
266 &FGInterface::get_Altitude,
267 &FGInterface::set_Altitude,
269 fgSetArchivable("/position/altitude-ft");
270 _tiedProperties.Tie("/position/altitude-agl-ft", this,
271 &FGInterface::get_Altitude_AGL, &FGInterface::set_AltitudeAGL, false);
272 fgSetArchivable("/position/ground-elev-ft");
273 _tiedProperties.Tie("/position/ground-elev-ft", this,
274 &FGInterface::get_Runway_altitude); // read-only
275 fgSetArchivable("/position/ground-elev-m");
276 _tiedProperties.Tie("/position/ground-elev-m", this,
277 &FGInterface::get_Runway_altitude_m); // read-only
278 _tiedProperties.Tie("/environment/ground-elevation-m", this,
279 &FGInterface::get_Runway_altitude_m); // read-only
280 fgSetArchivable("/position/sea-level-radius-ft");
281 _tiedProperties.Tie("/position/sea-level-radius-ft", this,
282 &FGInterface::get_Sea_level_radius,
283 &FGInterface::_set_Sea_level_radius, false);
286 _tiedProperties.Tie("/orientation/roll-deg", this,
287 &FGInterface::get_Phi_deg,
288 &FGInterface::set_Phi_deg, false);
289 fgSetArchivable("/orientation/roll-deg");
290 _tiedProperties.Tie("/orientation/pitch-deg", this,
291 &FGInterface::get_Theta_deg,
292 &FGInterface::set_Theta_deg, false);
293 fgSetArchivable("/orientation/pitch-deg");
294 _tiedProperties.Tie("/orientation/heading-deg", this,
295 &FGInterface::get_Psi_deg,
296 &FGInterface::set_Psi_deg, false);
297 fgSetArchivable("/orientation/heading-deg");
298 _tiedProperties.Tie("/orientation/track-deg", this,
299 &FGInterface::get_Track); // read-only
301 // Body-axis "euler rates" (rotation speed, but in a funny
303 _tiedProperties.Tie("/orientation/roll-rate-degps", this,
304 &FGInterface::get_Phi_dot_degps,
305 &FGInterface::set_Phi_dot_degps, false);
306 _tiedProperties.Tie("/orientation/pitch-rate-degps", this,
307 &FGInterface::get_Theta_dot_degps,
308 &FGInterface::set_Theta_dot_degps, false);
309 _tiedProperties.Tie("/orientation/yaw-rate-degps", this,
310 &FGInterface::get_Psi_dot_degps,
311 &FGInterface::set_Psi_dot_degps, false);
313 _tiedProperties.Tie("/orientation/p-body", this, &FGInterface::get_P_body); // read-only
314 _tiedProperties.Tie("/orientation/q-body", this, &FGInterface::get_Q_body); // read-only
315 _tiedProperties.Tie("/orientation/r-body", this, &FGInterface::get_R_body); // read-only
317 // Ground speed knots
318 _tiedProperties.Tie("/velocities/groundspeed-kt", this,
319 &FGInterface::get_V_ground_speed_kt,
320 &FGInterface::set_V_ground_speed_kt); // read-only
322 // Calibrated airspeed
323 _tiedProperties.Tie("/velocities/airspeed-kt", this,
324 &FGInterface::get_V_calibrated_kts,
325 &FGInterface::set_V_calibrated_kts,
328 _tiedProperties.Tie("/velocities/equivalent-kt", this,
329 &FGInterface::get_V_equiv_kts); // read-only
332 _tiedProperties.Tie("/velocities/mach", this,
333 &FGInterface::get_Mach_number,
334 &FGInterface::set_Mach_number,
338 // _tiedProperties.Tie("/velocities/speed-north-fps", this,
339 // &FGInterface::get_V_north,
340 // &FGInterface::set_V_north);
341 // fgSetArchivable("/velocities/speed-north-fps");
342 // _tiedProperties.Tie("/velocities/speed-east-fps", this,
343 // &FGInterface::get_V_east,
344 // &FGInterface::set_V_east);
345 // fgSetArchivable("/velocities/speed-east-fps");
346 // _tiedProperties.Tie("/velocities/speed-down-fps", this,
347 // &FGInterface::get_V_down,
348 // &FGInterface::set_V_down);
349 // fgSetArchivable("/velocities/speed-down-fps");
351 // FIXME: Temporarily read-only, until the
352 // incompatibilities between JSBSim and
353 // LaRCSim are fixed (LaRCSim adds the
354 // earth's rotation to the east velocity).
355 _tiedProperties.Tie("/velocities/speed-north-fps", this,
356 &FGInterface::get_V_north, &FGInterface::set_V_north, false);
357 _tiedProperties.Tie("/velocities/speed-east-fps", this,
358 &FGInterface::get_V_east, &FGInterface::set_V_east, false);
359 _tiedProperties.Tie("/velocities/speed-down-fps", this,
360 &FGInterface::get_V_down, &FGInterface::set_V_down, false);
362 _tiedProperties.Tie("/velocities/north-relground-fps", this,
363 &FGInterface::get_V_north_rel_ground); // read-only
364 _tiedProperties.Tie("/velocities/east-relground-fps", this,
365 &FGInterface::get_V_east_rel_ground); // read-only
366 _tiedProperties.Tie("/velocities/down-relground-fps", this,
367 &FGInterface::get_V_down_rel_ground); // read-only
369 // ECEF velocity in body axis
370 // FIXME: temporarily archivable, until the NED problem is fixed.
371 _tiedProperties.Tie("/velocities/uBody-fps", this,
372 &FGInterface::get_uBody,
373 &FGInterface::set_uBody,
375 fgSetArchivable("/velocities/uBody-fps");
376 _tiedProperties.Tie("/velocities/vBody-fps", this,
377 &FGInterface::get_vBody,
378 &FGInterface::set_vBody,
380 fgSetArchivable("/velocities/vBody-fps");
381 _tiedProperties.Tie("/velocities/wBody-fps", this,
382 &FGInterface::get_wBody,
383 &FGInterface::set_wBody,
385 fgSetArchivable("/velocities/wBody-fps");
387 // Climb and slip (read-only)
388 _tiedProperties.Tie("/velocities/vertical-speed-fps", this,
389 &FGInterface::get_Climb_Rate,
390 &FGInterface::set_Climb_Rate, false );
391 _tiedProperties.Tie("/velocities/glideslope", this,
392 &FGInterface::get_Gamma_vert_rad,
393 &FGInterface::set_Gamma_vert_rad, false );
394 _tiedProperties.Tie("/orientation/side-slip-rad", this,
395 &FGInterface::get_Beta, &FGInterface::_set_Beta, false);
396 _tiedProperties.Tie("/orientation/side-slip-deg", this,
397 &FGInterface::get_Beta_deg); // read-only
398 _tiedProperties.Tie("/orientation/alpha-deg", this,
399 &FGInterface::get_Alpha_deg, &FGInterface::set_Alpha_deg, false);
400 _tiedProperties.Tie("/accelerations/nlf", this,
401 &FGInterface::get_Nlf); // read-only
404 _tiedProperties.Tie("/accelerations/ned/north-accel-fps_sec",
405 this, &FGInterface::get_V_dot_north); // read-only
406 _tiedProperties.Tie("/accelerations/ned/east-accel-fps_sec",
407 this, &FGInterface::get_V_dot_east); // read-only
408 _tiedProperties.Tie("/accelerations/ned/down-accel-fps_sec",
409 this, &FGInterface::get_V_dot_down); // read-only
411 // Pilot accelerations
412 _tiedProperties.Tie("/accelerations/pilot/x-accel-fps_sec",
413 this, &FGInterface::get_A_X_pilot, &FGInterface::set_A_X_pilot, false);
414 _tiedProperties.Tie("/accelerations/pilot/y-accel-fps_sec",
415 this, &FGInterface::get_A_Y_pilot, &FGInterface::set_A_Y_pilot, false);
416 _tiedProperties.Tie("/accelerations/pilot/z-accel-fps_sec",
417 this, &FGInterface::get_A_Z_pilot, &FGInterface::set_A_Z_pilot, false);
419 _tiedProperties.Tie("/accelerations/n-z-cg-fps_sec",
420 this, &FGInterface::get_N_Z_cg); // read-only
425 * Unbind any properties bound to this FDM.
427 * This method allows the FDM to release properties so that a new
428 * FDM can bind them instead.
431 FGInterface::unbind ()
433 _tiedProperties.Untie();
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!");
446 bool FGInterface::readState(SGIOChannel* io)
449 int length = sizeof(FlightState);
451 if ( io->get_type() == sgFileType ) {
452 if ( io->read( (char *)(& buf), length ) == length ) {
453 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
458 while ( io->read( (char *)(& buf), length ) == length ) {
459 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
463 _state = buf; // copy the read state over
467 bool FGInterface::writeState(SGIOChannel* io)
469 if (!bound || !inited) {
473 int length = sizeof(FlightState);
474 if ( ! io->write( (char *)(& _state), length ) ) {
475 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
482 void FGInterface::_updatePositionM(const SGVec3d& cartPos)
484 TrackComputer tracker( _state.track, _state.geodetic_position_v );
485 _state.cartesian_position_v = cartPos;
486 _state.geodetic_position_v = SGGeod::fromCart(_state.cartesian_position_v);
487 _state.geocentric_position_v = SGGeoc::fromCart(_state.cartesian_position_v);
488 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(_state.geodetic_position_v)*SG_METER_TO_FEET );
489 _update_ground_elev_at_pos();
493 void FGInterface::_updatePosition(const SGGeod& geod)
495 TrackComputer tracker( _state.track, _state.geodetic_position_v );
496 _state.geodetic_position_v = geod;
497 _state.cartesian_position_v = SGVec3d::fromGeod(_state.geodetic_position_v);
498 _state.geocentric_position_v = SGGeoc::fromCart(_state.cartesian_position_v);
500 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(_state.geodetic_position_v)*SG_METER_TO_FEET );
501 _update_ground_elev_at_pos();
505 void FGInterface::_updatePosition(const SGGeoc& geoc)
507 TrackComputer tracker( _state.track, _state.geodetic_position_v );
508 _state.geocentric_position_v = geoc;
509 _state.cartesian_position_v = SGVec3d::fromGeoc(_state.geocentric_position_v);
510 _state.geodetic_position_v = SGGeod::fromCart(_state.cartesian_position_v);
512 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(_state.geodetic_position_v)*SG_METER_TO_FEET );
513 _update_ground_elev_at_pos();
517 void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
519 _updatePosition(SGGeod::fromRadFt(lon, lat, alt));
523 void FGInterface::_updateGeocentricPosition( double lat, double lon,
526 _updatePosition(SGGeoc::fromRadFt(lon, lat, get_Sea_level_radius() + alt));
529 void FGInterface::_update_ground_elev_at_pos( void ) {
530 double groundlevel_m = get_groundlevel_m(_state.geodetic_position_v);
531 _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
535 void FGInterface::set_Latitude(double lat) {
536 _state.geodetic_position_v.setLatitudeRad(lat);
539 void FGInterface::set_Longitude(double lon) {
540 _state.geodetic_position_v.setLongitudeRad(lon);
543 void FGInterface::set_Altitude(double alt) {
544 _state.geodetic_position_v.setElevationFt(alt);
547 void FGInterface::set_AltitudeAGL(double altagl) {
548 _state.altitude_agl=altagl;
552 void FGInterface::set_V_calibrated_kts(double vc) {
553 _state.v_calibrated_kts = vc;
556 void FGInterface::set_Mach_number(double mach) {
557 _state.mach_number = mach;
560 void FGInterface::set_Velocities_Local( double north,
563 _state.v_local_v[0] = north;
564 _state.v_local_v[1] = east;
565 _state.v_local_v[2] = down;
568 void FGInterface::set_Velocities_Body( double u,
571 _state.v_body_v[0] = u;
572 _state.v_body_v[1] = v;
573 _state.v_body_v[2] = w;
577 void FGInterface::set_Euler_Angles( double phi,
580 _state.euler_angles_v[0] = phi;
581 _state.euler_angles_v[1] = theta;
582 _state.euler_angles_v[2] = psi;
586 void FGInterface::set_Climb_Rate( double roc) {
587 _state.climb_rate = roc;
590 void FGInterface::set_Gamma_vert_rad( double gamma) {
591 _state.gamma_vert_rad = gamma;
594 void FGInterface::set_Static_pressure(double p) { _state.static_pressure = p; }
595 void FGInterface::set_Static_temperature(double T) { _state.static_temperature = T; }
596 void FGInterface::set_Density(double rho) { _state.density = rho; }
598 void FGInterface::set_Velocities_Local_Airmass (double wnorth,
601 _state.v_local_airmass_v[0] = wnorth;
602 _state.v_local_airmass_v[1] = weast;
603 _state.v_local_airmass_v[2] = wdown;
607 void FGInterface::_busdump(void)
609 SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rp_body_v: " << _state.d_cg_rp_body_v);
610 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_local_v: " << _state.v_dot_local_v);
611 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_body_v: " << _state.v_dot_body_v);
612 SG_LOG(SG_FLIGHT,SG_INFO,"a_cg_body_v: " << _state.a_cg_body_v);
613 SG_LOG(SG_FLIGHT,SG_INFO,"a_pilot_body_v: " << _state.a_pilot_body_v);
614 SG_LOG(SG_FLIGHT,SG_INFO,"n_cg_body_v: " << _state.n_cg_body_v);
615 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v: " << _state.v_local_v);
616 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_rel_ground_v: " << _state.v_local_rel_ground_v);
617 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_airmass_v: " << _state.v_local_airmass_v);
618 SG_LOG(SG_FLIGHT,SG_INFO,"v_body_v: " << _state.v_body_v);
619 SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v: " << _state.omega_body_v);
620 SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v: " << _state.euler_rates_v);
621 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v: " << _state.geocentric_rates_v);
622 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v: " << _state.geocentric_position_v);
623 SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v: " << _state.geodetic_position_v);
624 SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v: " << _state.euler_angles_v);
626 SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << _state.nlf );
627 SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << _state.v_rel_wind );
628 SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << _state.v_true_kts );
629 SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << _state.v_ground_speed );
630 SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << _state.v_equiv_kts );
631 SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << _state.v_calibrated_kts );
632 SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << _state.alpha );
633 SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << _state.beta );
634 SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << _state.gamma_vert_rad );
635 SG_LOG(SG_FLIGHT,SG_INFO,"density: " << _state.density );
636 SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << _state.mach_number );
637 SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << _state.static_pressure );
638 SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << _state.total_pressure );
639 SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << _state.dynamic_pressure );
640 SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << _state.static_temperature );
641 SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << _state.total_temperature );
642 SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << _state.sea_level_radius );
643 SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << _state.earth_position_angle );
644 SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << _state.runway_altitude );
645 SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << _state.climb_rate );
646 SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << _state.altitude_agl );
650 FGInterface::prepare_ground_cache_m(double startSimTime, double endSimTime,
651 const double pt[3], double rad)
653 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
658 FGInterface::prepare_ground_cache_ft(double startSimTime, double endSimTime,
659 const double pt[3], double rad)
661 // Convert units and do the real work.
662 SGVec3d pt_ft = SG_FEET_TO_METER*SGVec3d(pt);
663 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
664 pt_ft, rad*SG_FEET_TO_METER);
668 FGInterface::is_valid_m(double *ref_time, double pt[3], double *rad)
671 bool valid = ground_cache.is_valid(*ref_time, _pt, *rad);
676 bool FGInterface::is_valid_ft(double *ref_time, double pt[3], double *rad)
678 // Convert units and do the real work.
680 bool found_ground = ground_cache.is_valid(*ref_time, _pt, *rad);
681 assign(pt, SG_METER_TO_FEET*_pt);
682 *rad *= SG_METER_TO_FEET;
687 FGInterface::get_cat_m(double t, const double pt[3],
688 double end[2][3], double vel[2][3])
690 SGVec3d _end[2], _vel[2];
691 double dist = ground_cache.get_cat(t, SGVec3d(pt), _end, _vel);
692 for (int k=0; k<2; ++k) {
693 assign( end[k], _end[k] );
694 assign( vel[k], _vel[k] );
700 FGInterface::get_cat_ft(double t, const double pt[3],
701 double end[2][3], double vel[2][3])
703 // Convert units and do the real work.
704 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
705 SGVec3d _end[2], _vel[2];
706 double dist = ground_cache.get_cat(t, pt_m, _end, _vel);
707 for (int k=0; k<2; ++k) {
708 assign( end[k], SG_METER_TO_FEET*_end[k] );
709 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
711 return dist*SG_METER_TO_FEET;
715 FGInterface::get_body_m(double t, simgear::BVHNode::Id id,
716 double bodyToWorld[16], double linearVel[3],
717 double angularVel[3])
719 SGMatrixd _bodyToWorld;
720 SGVec3d _linearVel, _angularVel;
721 if (!ground_cache.get_body(t, _bodyToWorld, _linearVel, _angularVel, id))
724 assign(linearVel, _linearVel);
725 assign(angularVel, _angularVel);
726 for (unsigned i = 0; i < 16; ++i)
727 bodyToWorld[i] = _bodyToWorld.data()[i];
733 FGInterface::get_agl_m(double t, const double pt[3], double max_altoff,
734 double contact[3], double normal[3],
735 double linearVel[3], double angularVel[3],
736 simgear::BVHMaterial const*& material, simgear::BVHNode::Id& id)
738 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
739 SGVec3d _contact, _normal, _linearVel, _angularVel;
741 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
742 _angularVel, id, material);
743 // correct the linear velocity, since the line intersector delivers
744 // values for the start point and the get_agl function should
745 // traditionally deliver for the contact point
746 _linearVel += cross(_angularVel, _contact - pt_m);
748 assign(contact, _contact);
749 assign(normal, _normal);
750 assign(linearVel, _linearVel);
751 assign(angularVel, _angularVel);
756 FGInterface::get_agl_ft(double t, const double pt[3], double max_altoff,
757 double contact[3], double normal[3],
758 double linearVel[3], double angularVel[3],
759 simgear::BVHMaterial const*& material, simgear::BVHNode::Id& id)
761 // Convert units and do the real work.
762 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
763 pt_m *= SG_FEET_TO_METER;
764 SGVec3d _contact, _normal, _linearVel, _angularVel;
766 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
767 _angularVel, id, material);
768 // correct the linear velocity, since the line intersector delivers
769 // values for the start point and the get_agl function should
770 // traditionally deliver for the contact point
771 _linearVel += cross(_angularVel, _contact - pt_m);
773 // Convert units back ...
774 assign( contact, SG_METER_TO_FEET*_contact );
775 assign( normal, _normal );
776 assign( linearVel, SG_METER_TO_FEET*_linearVel );
777 assign( angularVel, _angularVel );
782 FGInterface::get_nearest_m(double t, const double pt[3], double maxDist,
783 double contact[3], double normal[3],
784 double linearVel[3], double angularVel[3],
785 simgear::BVHMaterial const*& material,
786 simgear::BVHNode::Id& id)
788 SGVec3d _contact, _linearVel, _angularVel;
790 if (!ground_cache.get_nearest(t, SGVec3d(pt), maxDist, _contact, _linearVel,
791 _angularVel, id, material))
794 assign(contact, _contact);
795 assign(linearVel, _linearVel);
796 assign(angularVel, _angularVel);
801 FGInterface::get_nearest_ft(double t, const double pt[3], double maxDist,
802 double contact[3], double normal[3],
803 double linearVel[3], double angularVel[3],
804 simgear::BVHMaterial const*& material,
805 simgear::BVHNode::Id& id)
807 SGVec3d _contact, _linearVel, _angularVel;
809 if (!ground_cache.get_nearest(t, SG_FEET_TO_METER*SGVec3d(pt),
810 SG_FEET_TO_METER*maxDist, _contact, _linearVel,
811 _angularVel, id, material))
814 assign(contact, SG_METER_TO_FEET*_contact);
815 assign(linearVel, SG_METER_TO_FEET*_linearVel);
816 assign(angularVel, _angularVel);
821 FGInterface::get_groundlevel_m(double lat, double lon, double alt)
823 return get_groundlevel_m(SGGeod::fromRadM(lon, lat, alt));
827 FGInterface::get_groundlevel_m(const SGGeod& geod)
829 // Compute the cartesian position of the given lat/lon/alt.
830 SGVec3d pos = SGVec3d::fromGeod(geod);
832 // FIXME: how to handle t - ref_time differences ???
834 double ref_time = 0, radius;
835 // Prepare the ground cache for that position.
836 if (!is_valid_m(&ref_time, cpos.data(), &radius)) {
837 double startTime = ref_time;
838 double endTime = startTime + 1;
839 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), 10);
840 /// This is most likely the case when the given altitude is
841 /// too low, try with a new altitude of 10000m, that should be
842 /// sufficient to find a ground level below everywhere on our planet
844 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
845 /// If there is still no ground, return sea level radius
846 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), 10))
849 } else if (radius*radius <= distSqr(pos, cpos)) {
850 double startTime = ref_time;
851 double endTime = startTime + 1;
853 /// We reuse the old radius value, but only if it is at least 10 Meters ..
854 if (!(10 < radius)) // Well this strange compare is nan safe
857 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), radius);
858 /// This is most likely the case when the given altitude is
859 /// too low, try with a new altitude of 10000m, that should be
860 /// sufficient to find a ground level below everywhere on our planet
862 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
863 /// If there is still no ground, return sea level radius
864 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), radius))
869 double contact[3], normal[3], vel[3], angvel[3];
870 const simgear::BVHMaterial* material;
871 simgear::BVHNode::Id id;
872 // Ignore the return value here, since it just tells us if
873 // the returns stem from the groundcache or from the coarse
874 // computations below the groundcache. The contact point is still something
875 // valid, the normals and the other returns just contain some defaults.
876 get_agl_m(ref_time, pos.data(), 2.0, contact, normal, vel, angvel,
878 return SGGeod::fromCart(SGVec3d(contact)).getElevationM();
882 FGInterface::caught_wire_m(double t, const double pt[4][3])
885 for (int i=0; i<4; ++i)
886 pt_m[i] = SGVec3d(pt[i]);
888 return ground_cache.caught_wire(t, pt_m);
892 FGInterface::caught_wire_ft(double t, const double pt[4][3])
894 // Convert units and do the real work.
896 for (int i=0; i<4; ++i)
897 pt_m[i] = SG_FEET_TO_METER*SGVec3d(pt[i]);
899 return ground_cache.caught_wire(t, pt_m);
903 FGInterface::get_wire_ends_m(double t, double end[2][3], double vel[2][3])
905 SGVec3d _end[2], _vel[2];
906 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
907 for (int k=0; k<2; ++k) {
908 assign( end[k], _end[k] );
909 assign( vel[k], _vel[k] );
915 FGInterface::get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
917 // Convert units and do the real work.
918 SGVec3d _end[2], _vel[2];
919 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
920 for (int k=0; k<2; ++k) {
921 assign( end[k], SG_METER_TO_FEET*_end[k] );
922 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
928 FGInterface::release_wire(void)
930 ground_cache.release_wire();