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.
31 #include <simgear/constants.h>
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/math/sg_geodesy.hxx>
34 #include <simgear/scene/model/placement.hxx>
35 #include <simgear/scene/material/mat.hxx>
36 #include <simgear/timing/timestamp.hxx>
38 #include <Scenery/scenery.hxx>
39 #include <Main/globals.hxx>
40 #include <Main/fg_props.hxx>
41 #include <FDM/groundcache.hxx>
46 // base_fdm_state is the internal state that is updated in integer
47 // multiples of "dt". This leads to "jitter" with respect to the real
48 // world time, so we introduce cur_fdm_state which is extrapolated by
49 // the difference between sim time and real world time
51 FGInterface *cur_fdm_state = 0;
52 FGInterface base_fdm_state;
54 inline void init_vec(FG_VECTOR_3 vec) {
55 vec[0] = 0.0; vec[1] = 0.0; vec[2] = 0.0;
59 FGInterface::FGInterface()
65 FGInterface::FGInterface( double dt )
72 FGInterface::~FGInterface() {
73 // unbind(); // FIXME: should be called explicitly
78 FGInterface::_calc_multiloop (double dt)
80 int hz = fgGetInt("/sim/model-hz");
81 int speedup = fgGetInt("/sim/speed-up");
86 // Avoid roundoff problems by adding the roundoff itself.
87 // ... ok, two times the roundoff to have enough room.
88 int multiloop = int(floor(ml * (1.0 + 2.0*DBL_EPSILON)));
89 remainder = (ml - multiloop) / hz;
91 // If we artificially inflate ml above by a tiny amount to get the
92 // closest integer, then subtract the integer from the original
93 // slightly smaller value, we can get a negative remainder.
94 // Logically this should never happen, and we definitely don't want
95 // to carry a negative remainder over to the next iteration, so
96 // never let the remainder go below zero.
98 // Note: this fixes a problem where we run 1, 3, 1, 3, 1, 3... loops
99 // of the FDM when in fact we want to run 2, 2, 2, 2, 2...
100 if ( remainder < 0 ) { remainder = 0; }
102 return (multiloop * speedup);
107 * Set default values for the state of the FDM.
109 * This method is invoked by the constructors.
112 FGInterface::_setup ()
117 init_vec( d_pilot_rp_body_v );
118 init_vec( d_cg_rp_body_v );
119 init_vec( f_body_total_v );
120 init_vec( f_local_total_v );
121 init_vec( f_aero_v );
122 init_vec( f_engine_v );
123 init_vec( f_gear_v );
124 init_vec( m_total_rp_v );
125 init_vec( m_total_cg_v );
126 init_vec( m_aero_v );
127 init_vec( m_engine_v );
128 init_vec( m_gear_v );
129 init_vec( v_dot_local_v );
130 init_vec( v_dot_body_v );
131 init_vec( a_cg_body_v );
132 init_vec( a_pilot_body_v );
133 init_vec( n_cg_body_v );
134 init_vec( n_pilot_body_v );
135 init_vec( omega_dot_body_v );
136 init_vec( v_local_v );
137 init_vec( v_local_rel_ground_v );
138 init_vec( v_local_airmass_v );
139 init_vec( v_local_rel_airmass_v );
140 init_vec( v_local_gust_v );
141 init_vec( v_wind_body_v );
142 init_vec( omega_body_v );
143 init_vec( omega_local_v );
144 init_vec( omega_total_v );
145 init_vec( euler_rates_v );
146 init_vec( geocentric_rates_v );
147 init_vec( geocentric_position_v );
148 init_vec( geodetic_position_v );
149 init_vec( euler_angles_v );
150 init_vec( d_cg_rwy_local_v );
151 init_vec( d_cg_rwy_rwy_v );
152 init_vec( d_pilot_rwy_local_v );
153 init_vec( d_pilot_rwy_rwy_v );
154 init_vec( t_local_to_body_m[0] );
155 init_vec( t_local_to_body_m[1] );
156 init_vec( t_local_to_body_m[2] );
158 mass=i_xx=i_yy=i_zz=i_xz=0;
160 v_rel_wind=v_true_kts=v_rel_ground=v_inertial=0;
161 v_ground_speed=v_equiv=v_equiv_kts=0;
162 v_calibrated=v_calibrated_kts=0;
164 centrifugal_relief=0;
165 alpha=beta=alpha_dot=beta_dot=0;
166 cos_alpha=sin_alpha=cos_beta=sin_beta=0;
167 cos_phi=sin_phi=cos_theta=sin_theta=cos_psi=sin_psi=0;
168 gamma_vert_rad=gamma_horiz_rad=0;
169 sigma=density=v_sound=mach_number=0;
170 static_pressure=total_pressure=impact_pressure=0;
172 static_temperature=total_temperature=0;
173 sea_level_radius=earth_position_angle=0;
174 runway_altitude=runway_latitude=runway_longitude=0;
178 sin_lat_geocentric=cos_lat_geocentric=0;
179 sin_latitude=cos_latitude=0;
180 sin_longitude=cos_longitude=0;
185 FGInterface::init () {}
188 * Initialize the state of the FDM.
190 * Subclasses of FGInterface may do their own, additional initialization,
191 * but there is some that is common to all. Normally, they should call
192 * this before they begin their own init to make sure the basic structures
193 * are set up properly.
196 FGInterface::common_init ()
198 SG_LOG( SG_FLIGHT, SG_INFO, "Start common FDM init" );
203 // set_remainder( 0 );
205 // Set initial position
206 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing position..." );
207 double lon = fgGetDouble("/sim/presets/longitude-deg")
208 * SGD_DEGREES_TO_RADIANS;
209 double lat = fgGetDouble("/sim/presets/latitude-deg")
210 * SGD_DEGREES_TO_RADIANS;
211 double alt_ft = fgGetDouble("/sim/presets/altitude-ft");
212 double alt_m = alt_ft * SG_FEET_TO_METER;
213 set_Longitude( lon );
215 SG_LOG( SG_FLIGHT, SG_INFO, "Checking for lon = "
216 << lon*SGD_RADIANS_TO_DEGREES << "deg, lat = "
217 << lat*SGD_RADIANS_TO_DEGREES << "deg, alt = "
220 double ground_elev_m = get_groundlevel_m(lat, lon, alt_m);
221 double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET;
222 _set_Runway_altitude ( ground_elev_ft );
223 if ( fgGetBool("/sim/presets/onground") || alt_ft < ground_elev_ft ) {
224 fgSetDouble("/position/altitude-ft", ground_elev_ft + 0.1);
225 set_Altitude( ground_elev_ft + 0.1);
227 set_Altitude( alt_ft );
230 // Set ground elevation
231 SG_LOG( SG_FLIGHT, SG_INFO,
232 "...initializing ground elevation to " << ground_elev_ft
235 // Set sea-level radius
236 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing sea-level radius..." );
237 SG_LOG( SG_FLIGHT, SG_INFO, " lat = "
238 << fgGetDouble("/sim/presets/latitude-deg")
239 << " alt = " << get_Altitude() );
240 double sea_level_radius_meters;
242 sgGeodToGeoc( fgGetDouble("/sim/presets/latitude-deg")
243 * SGD_DEGREES_TO_RADIANS,
244 get_Altitude() * SG_FEET_TO_METER,
245 &sea_level_radius_meters, &lat_geoc );
246 _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
248 // Set initial velocities
249 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing velocities..." );
250 if ( !fgHasNode("/sim/presets/speed-set") ) {
251 set_V_calibrated_kts(0.0);
253 const string speedset = fgGetString("/sim/presets/speed-set");
254 if ( speedset == "knots" || speedset == "KNOTS" ) {
255 set_V_calibrated_kts( fgGetDouble("/sim/presets/airspeed-kt") );
256 } else if ( speedset == "mach" || speedset == "MACH" ) {
257 set_Mach_number( fgGetDouble("/sim/presets/mach") );
258 } else if ( speedset == "UVW" || speedset == "uvw" ) {
259 set_Velocities_Wind_Body(
260 fgGetDouble("/sim/presets/uBody-fps"),
261 fgGetDouble("/sim/presets/vBody-fps"),
262 fgGetDouble("/sim/presets/wBody-fps") );
263 } else if ( speedset == "NED" || speedset == "ned" ) {
264 set_Velocities_Local(
265 fgGetDouble("/sim/presets/speed-north-fps"),
266 fgGetDouble("/sim/presets/speed-east-fps"),
267 fgGetDouble("/sim/presets/speed-down-fps") );
269 SG_LOG( SG_FLIGHT, SG_ALERT,
270 "Unrecognized value for /sim/presets/speed-set: "
272 set_V_calibrated_kts( 0.0 );
276 // Set initial Euler angles
277 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing Euler angles..." );
278 set_Euler_Angles( fgGetDouble("/sim/presets/roll-deg")
279 * SGD_DEGREES_TO_RADIANS,
280 fgGetDouble("/sim/presets/pitch-deg")
281 * SGD_DEGREES_TO_RADIANS,
282 fgGetDouble("/sim/presets/heading-deg")
283 * SGD_DEGREES_TO_RADIANS );
285 SG_LOG( SG_FLIGHT, SG_INFO, "End common FDM init" );
290 * Bind getters and setters to properties.
292 * The bind() method will be invoked after init(). Note that unlike
293 * the usual implementations of FGSubsystem::bind(), this method does
294 * not automatically pick up existing values for the properties at
295 * bind time; instead, all values are set explicitly in the init()
303 // Time management (read-only)
304 // fgTie("/fdm/time/delta_t", this,
305 // &FGInterface::get_delta_t); // read-only
306 // fgTie("/fdm/time/elapsed", this,
307 // &FGInterface::get_elapsed); // read-only
308 // fgTie("/fdm/time/remainder", this,
309 // &FGInterface::get_remainder); // read-only
310 // fgTie("/fdm/time/multi_loop", this,
311 // &FGInterface::get_multi_loop); // read-only
314 fgTie("/position/latitude-deg", this,
315 &FGInterface::get_Latitude_deg,
316 &FGInterface::set_Latitude_deg,
318 fgSetArchivable("/position/latitude-deg");
319 fgTie("/position/longitude-deg", this,
320 &FGInterface::get_Longitude_deg,
321 &FGInterface::set_Longitude_deg,
323 fgSetArchivable("/position/longitude-deg");
324 fgTie("/position/altitude-ft", this,
325 &FGInterface::get_Altitude,
326 &FGInterface::set_Altitude,
328 fgSetArchivable("/position/altitude-ft");
329 fgTie("/position/altitude-agl-ft", this,
330 &FGInterface::get_Altitude_AGL); // read-only
331 fgSetArchivable("/position/ground-elev-ft");
332 fgTie("/position/ground-elev-ft", this,
333 &FGInterface::get_Runway_altitude); // read-only
334 fgSetArchivable("/position/ground-elev-m");
335 fgTie("/position/ground-elev-m", this,
336 &FGInterface::get_Runway_altitude_m); // read-only
337 fgTie("/environment/ground-elevation-m", this,
338 &FGInterface::get_Runway_altitude_m); // read-only
339 fgSetArchivable("/position/sea-level-radius-ft");
340 fgTie("/position/sea-level-radius-ft", this,
341 &FGInterface::get_Sea_level_radius); // read-only
344 fgTie("/orientation/roll-deg", this,
345 &FGInterface::get_Phi_deg,
346 &FGInterface::set_Phi_deg);
347 fgSetArchivable("/orientation/roll-deg");
348 fgTie("/orientation/pitch-deg", this,
349 &FGInterface::get_Theta_deg,
350 &FGInterface::set_Theta_deg);
351 fgSetArchivable("/orientation/pitch-deg");
352 fgTie("/orientation/heading-deg", this,
353 &FGInterface::get_Psi_deg,
354 &FGInterface::set_Psi_deg);
355 fgSetArchivable("/orientation/heading-deg");
357 // Body-axis "euler rates" (rotation speed, but in a funny
359 fgTie("/orientation/roll-rate-degps", this,
360 &FGInterface::get_Phi_dot_degps);
361 fgTie("/orientation/pitch-rate-degps", this,
362 &FGInterface::get_Theta_dot_degps);
363 fgTie("/orientation/yaw-rate-degps", this,
364 &FGInterface::get_Psi_dot_degps);
366 // Calibrated airspeed
367 fgTie("/velocities/airspeed-kt", this,
368 &FGInterface::get_V_calibrated_kts,
369 &FGInterface::set_V_calibrated_kts,
373 fgTie("/velocities/mach", this,
374 &FGInterface::get_Mach_number,
375 &FGInterface::set_Mach_number,
379 // fgTie("/velocities/speed-north-fps", this,
380 // &FGInterface::get_V_north,
381 // &FGInterface::set_V_north);
382 // fgSetArchivable("/velocities/speed-north-fps");
383 // fgTie("/velocities/speed-east-fps", this,
384 // &FGInterface::get_V_east,
385 // &FGInterface::set_V_east);
386 // fgSetArchivable("/velocities/speed-east-fps");
387 // fgTie("/velocities/speed-down-fps", this,
388 // &FGInterface::get_V_down,
389 // &FGInterface::set_V_down);
390 // fgSetArchivable("/velocities/speed-down-fps");
391 // FIXME: Temporarily read-only, until the
392 // incompatibilities between JSBSim and
393 // LaRCSim are fixed (LaRCSim adds the
394 // earth's rotation to the east velocity).
395 fgTie("/velocities/speed-north-fps", this,
396 &FGInterface::get_V_north);
397 fgTie("/velocities/speed-east-fps", this,
398 &FGInterface::get_V_east);
399 fgTie("/velocities/speed-down-fps", this,
400 &FGInterface::get_V_down);
403 // FIXME: temporarily archivable, until
404 // the NED problem is fixed.
405 fgTie("/velocities/uBody-fps", this,
406 &FGInterface::get_uBody,
407 &FGInterface::set_uBody,
409 fgSetArchivable("/velocities/uBody-fps");
410 fgTie("/velocities/vBody-fps", this,
411 &FGInterface::get_vBody,
412 &FGInterface::set_vBody,
414 fgSetArchivable("/velocities/vBody-fps");
415 fgTie("/velocities/wBody-fps", this,
416 &FGInterface::get_wBody,
417 &FGInterface::set_wBody,
419 fgSetArchivable("/velocities/wBody-fps");
421 // Climb and slip (read-only)
422 fgTie("/velocities/vertical-speed-fps", this,
423 &FGInterface::get_Climb_Rate,
424 &FGInterface::set_Climb_Rate );
425 fgTie("/velocities/glideslope", this,
426 &FGInterface::get_Gamma_vert_rad,
427 &FGInterface::set_Gamma_vert_rad );
428 fgTie("/orientation/side-slip-rad", this,
429 &FGInterface::get_Beta); // read-only
430 fgTie("/orientation/side-slip-deg", this,
431 &FGInterface::get_Beta_deg); // read-only
432 fgTie("/orientation/alpha-deg", this,
433 &FGInterface::get_Alpha_deg); // read-only
434 fgTie("/accelerations/nlf", this,
435 &FGInterface::get_Nlf); // read-only
438 fgTie("/accelerations/ned/north-accel-fps_sec",
439 this, &FGInterface::get_V_dot_north);
440 fgTie("/accelerations/ned/east-accel-fps_sec",
441 this, &FGInterface::get_V_dot_east);
442 fgTie("/accelerations/ned/down-accel-fps_sec",
443 this, &FGInterface::get_V_dot_down);
445 // Pilot accelerations
446 fgTie("/accelerations/pilot/x-accel-fps_sec",
447 this, &FGInterface::get_A_X_pilot);
448 fgTie("/accelerations/pilot/y-accel-fps_sec",
449 this, &FGInterface::get_A_Y_pilot);
450 fgTie("/accelerations/pilot/z-accel-fps_sec",
451 this, &FGInterface::get_A_Z_pilot);
457 * Unbind any properties bound to this FDM.
459 * This method allows the FDM to release properties so that a new
460 * FDM can bind them instead.
463 FGInterface::unbind ()
467 // fgUntie("/fdm/time/delta_t");
468 // fgUntie("/fdm/time/elapsed");
469 // fgUntie("/fdm/time/remainder");
470 // fgUntie("/fdm/time/multi_loop");
471 fgUntie("/position/latitude-deg");
472 fgUntie("/position/longitude-deg");
473 fgUntie("/position/altitude-ft");
474 fgUntie("/position/altitude-agl-ft");
475 fgUntie("/position/ground-elev-ft");
476 fgUntie("/position/ground-elev-m");
477 fgUntie("/environment/ground-elevation-m");
478 fgUntie("/position/sea-level-radius-ft");
479 fgUntie("/orientation/roll-deg");
480 fgUntie("/orientation/pitch-deg");
481 fgUntie("/orientation/heading-deg");
482 fgUntie("/orientation/roll-rate-degps");
483 fgUntie("/orientation/pitch-rate-degps");
484 fgUntie("/orientation/yaw-rate-degps");
485 fgUntie("/orientation/side-slip-rad");
486 fgUntie("/orientation/side-slip-deg");
487 fgUntie("/orientation/alpha-deg");
488 fgUntie("/velocities/airspeed-kt");
489 fgUntie("/velocities/mach");
490 fgUntie("/velocities/speed-north-fps");
491 fgUntie("/velocities/speed-east-fps");
492 fgUntie("/velocities/speed-down-fps");
493 fgUntie("/velocities/uBody-fps");
494 fgUntie("/velocities/vBody-fps");
495 fgUntie("/velocities/wBody-fps");
496 fgUntie("/velocities/vertical-speed-fps");
497 fgUntie("/velocities/glideslope");
498 fgUntie("/accelerations/nlf");
499 fgUntie("/accelerations/pilot/x-accel-fps_sec");
500 fgUntie("/accelerations/pilot/y-accel-fps_sec");
501 fgUntie("/accelerations/pilot/z-accel-fps_sec");
502 fgUntie("/accelerations/ned/north-accel-fps_sec");
503 fgUntie("/accelerations/ned/east-accel-fps_sec");
504 fgUntie("/accelerations/ned/down-accel-fps_sec");
508 * Update the state of the FDM (i.e. run the equations of motion).
511 FGInterface::update (double dt)
513 SG_LOG(SG_FLIGHT, SG_ALERT, "dummy update() ... SHOULDN'T BE CALLED!");
517 void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
519 double lat_geoc, sl_radius;
521 // cout << "starting sea level rad = " << get_Sea_level_radius() << endl;
523 sgGeodToGeoc( lat, alt * SG_FEET_TO_METER, &sl_radius, &lat_geoc );
525 SG_LOG( SG_FLIGHT, SG_DEBUG, "lon = " << lon
526 << " lat_geod = " << lat
527 << " lat_geoc = " << lat_geoc
529 << " sl_radius = " << sl_radius * SG_METER_TO_FEET
530 << " Equator = " << SG_EQUATORIAL_RADIUS_FT );
532 _set_Geocentric_Position( lat_geoc, lon,
533 sl_radius * SG_METER_TO_FEET + alt );
535 _set_Geodetic_Position( lat, lon, alt );
537 _set_Sea_level_radius( sl_radius * SG_METER_TO_FEET );
538 _update_ground_elev_at_pos();
540 _set_sin_lat_geocentric( lat_geoc );
541 _set_cos_lat_geocentric( lat_geoc );
543 _set_sin_cos_longitude( lon );
545 _set_sin_cos_latitude( lat );
549 void FGInterface::_updateGeocentricPosition( double lat_geoc, double lon,
552 double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
554 // cout << "starting sea level rad = " << get_Sea_level_radius() << endl;
556 sgGeocToGeod( lat_geoc, ( get_Sea_level_radius() + alt ) * SG_FEET_TO_METER,
557 &lat_geod, &tmp_alt, &sl_radius1 );
558 sgGeodToGeoc( lat_geod, alt * SG_FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
560 SG_LOG( SG_FLIGHT, SG_DEBUG, "lon = " << lon
561 << " lat_geod = " << lat_geod
562 << " lat_geoc = " << lat_geoc
564 << " tmp_alt = " << tmp_alt * SG_METER_TO_FEET
565 << " sl_radius1 = " << sl_radius1 * SG_METER_TO_FEET
566 << " sl_radius2 = " << sl_radius2 * SG_METER_TO_FEET
567 << " Equator = " << SG_EQUATORIAL_RADIUS_FT );
569 _set_Geocentric_Position( lat_geoc, lon,
570 sl_radius2 * SG_METER_TO_FEET + alt );
572 _set_Geodetic_Position( lat_geod, lon, alt );
574 _set_Sea_level_radius( sl_radius2 * SG_METER_TO_FEET );
575 _update_ground_elev_at_pos();
577 _set_sin_lat_geocentric( lat_geoc );
578 _set_cos_lat_geocentric( lat_geoc );
580 _set_sin_cos_longitude( lon );
582 _set_sin_cos_latitude( lat_geod );
585 void FGInterface::_update_ground_elev_at_pos( void ) {
586 double lat = get_Latitude();
587 double lon = get_Longitude();
588 double alt_m = get_Altitude()*SG_FEET_TO_METER;
589 double groundlevel_m = get_groundlevel_m(lat, lon, alt_m);
590 _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
593 // Extrapolate fdm based on time_offset (in usec)
594 void FGInterface::extrapolate( int time_offset ) {
595 double dt = time_offset / 1000000.0;
597 // -dw- metrowerks complains about ambiguous access, not critical
600 SG_LOG(SG_FLIGHT, SG_INFO, "extrapolating FDM by dt = " << dt);
603 double lat = geodetic_position_v[0] + geocentric_rates_v[0] * dt;
604 double lat_geoc = geocentric_position_v[0] + geocentric_rates_v[0] * dt;
606 double lon = geodetic_position_v[1] + geocentric_rates_v[1] * dt;
607 double lon_geoc = geocentric_position_v[1] + geocentric_rates_v[1] * dt;
609 double alt = geodetic_position_v[2] + geocentric_rates_v[2] * dt;
610 double radius = geocentric_position_v[2] + geocentric_rates_v[2] * dt;
612 geodetic_position_v[0] = lat;
613 geocentric_position_v[0] = lat_geoc;
615 geodetic_position_v[1] = lon;
616 geocentric_position_v[1] = lon_geoc;
618 geodetic_position_v[2] = alt;
619 geocentric_position_v[2] = radius;
623 void FGInterface::set_Latitude(double lat) {
624 geodetic_position_v[0] = lat;
627 void FGInterface::set_Longitude(double lon) {
628 geodetic_position_v[1] = lon;
631 void FGInterface::set_Altitude(double alt) {
632 geodetic_position_v[2] = alt;
635 void FGInterface::set_AltitudeAGL(double altagl) {
640 void FGInterface::set_V_calibrated_kts(double vc) {
641 v_calibrated_kts = vc;
644 void FGInterface::set_Mach_number(double mach) {
648 void FGInterface::set_Velocities_Local( double north,
651 v_local_v[0] = north;
656 void FGInterface::set_Velocities_Wind_Body( double u,
659 v_wind_body_v[0] = u;
660 v_wind_body_v[1] = v;
661 v_wind_body_v[2] = w;
665 void FGInterface::set_Euler_Angles( double phi,
668 euler_angles_v[0] = phi;
669 euler_angles_v[1] = theta;
670 euler_angles_v[2] = psi;
674 void FGInterface::set_Climb_Rate( double roc) {
678 void FGInterface::set_Gamma_vert_rad( double gamma) {
679 gamma_vert_rad = gamma;
682 void FGInterface::set_Static_pressure(double p) { static_pressure = p; }
683 void FGInterface::set_Static_temperature(double T) { static_temperature = T; }
684 void FGInterface::set_Density(double rho) { density = rho; }
686 void FGInterface::set_Velocities_Local_Airmass (double wnorth,
689 v_local_airmass_v[0] = wnorth;
690 v_local_airmass_v[1] = weast;
691 v_local_airmass_v[2] = wdown;
695 void FGInterface::_busdump(void) {
697 SG_LOG(SG_FLIGHT,SG_INFO,"d_pilot_rp_body_v[3]: " << d_pilot_rp_body_v[0] << ", " << d_pilot_rp_body_v[1] << ", " << d_pilot_rp_body_v[2]);
698 SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rp_body_v[3]: " << d_cg_rp_body_v[0] << ", " << d_cg_rp_body_v[1] << ", " << d_cg_rp_body_v[2]);
699 SG_LOG(SG_FLIGHT,SG_INFO,"f_body_total_v[3]: " << f_body_total_v[0] << ", " << f_body_total_v[1] << ", " << f_body_total_v[2]);
700 SG_LOG(SG_FLIGHT,SG_INFO,"f_local_total_v[3]: " << f_local_total_v[0] << ", " << f_local_total_v[1] << ", " << f_local_total_v[2]);
701 SG_LOG(SG_FLIGHT,SG_INFO,"f_aero_v[3]: " << f_aero_v[0] << ", " << f_aero_v[1] << ", " << f_aero_v[2]);
702 SG_LOG(SG_FLIGHT,SG_INFO,"f_engine_v[3]: " << f_engine_v[0] << ", " << f_engine_v[1] << ", " << f_engine_v[2]);
703 SG_LOG(SG_FLIGHT,SG_INFO,"f_gear_v[3]: " << f_gear_v[0] << ", " << f_gear_v[1] << ", " << f_gear_v[2]);
704 SG_LOG(SG_FLIGHT,SG_INFO,"m_total_rp_v[3]: " << m_total_rp_v[0] << ", " << m_total_rp_v[1] << ", " << m_total_rp_v[2]);
705 SG_LOG(SG_FLIGHT,SG_INFO,"m_total_cg_v[3]: " << m_total_cg_v[0] << ", " << m_total_cg_v[1] << ", " << m_total_cg_v[2]);
706 SG_LOG(SG_FLIGHT,SG_INFO,"m_aero_v[3]: " << m_aero_v[0] << ", " << m_aero_v[1] << ", " << m_aero_v[2]);
707 SG_LOG(SG_FLIGHT,SG_INFO,"m_engine_v[3]: " << m_engine_v[0] << ", " << m_engine_v[1] << ", " << m_engine_v[2]);
708 SG_LOG(SG_FLIGHT,SG_INFO,"m_gear_v[3]: " << m_gear_v[0] << ", " << m_gear_v[1] << ", " << m_gear_v[2]);
709 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_local_v[3]: " << v_dot_local_v[0] << ", " << v_dot_local_v[1] << ", " << v_dot_local_v[2]);
710 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_body_v[3]: " << v_dot_body_v[0] << ", " << v_dot_body_v[1] << ", " << v_dot_body_v[2]);
711 SG_LOG(SG_FLIGHT,SG_INFO,"a_cg_body_v[3]: " << a_cg_body_v[0] << ", " << a_cg_body_v[1] << ", " << a_cg_body_v[2]);
712 SG_LOG(SG_FLIGHT,SG_INFO,"a_pilot_body_v[3]: " << a_pilot_body_v[0] << ", " << a_pilot_body_v[1] << ", " << a_pilot_body_v[2]);
713 SG_LOG(SG_FLIGHT,SG_INFO,"n_cg_body_v[3]: " << n_cg_body_v[0] << ", " << n_cg_body_v[1] << ", " << n_cg_body_v[2]);
714 SG_LOG(SG_FLIGHT,SG_INFO,"n_pilot_body_v[3]: " << n_pilot_body_v[0] << ", " << n_pilot_body_v[1] << ", " << n_pilot_body_v[2]);
715 SG_LOG(SG_FLIGHT,SG_INFO,"omega_dot_body_v[3]: " << omega_dot_body_v[0] << ", " << omega_dot_body_v[1] << ", " << omega_dot_body_v[2]);
716 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v[3]: " << v_local_v[0] << ", " << v_local_v[1] << ", " << v_local_v[2]);
717 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_rel_ground_v[3]: " << v_local_rel_ground_v[0] << ", " << v_local_rel_ground_v[1] << ", " << v_local_rel_ground_v[2]);
718 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_airmass_v[3]: " << v_local_airmass_v[0] << ", " << v_local_airmass_v[1] << ", " << v_local_airmass_v[2]);
719 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_rel_airmass_v[3]: " << v_local_rel_airmass_v[0] << ", " << v_local_rel_airmass_v[1] << ", " << v_local_rel_airmass_v[2]);
720 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_gust_v[3]: " << v_local_gust_v[0] << ", " << v_local_gust_v[1] << ", " << v_local_gust_v[2]);
721 SG_LOG(SG_FLIGHT,SG_INFO,"v_wind_body_v[3]: " << v_wind_body_v[0] << ", " << v_wind_body_v[1] << ", " << v_wind_body_v[2]);
722 SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v[3]: " << omega_body_v[0] << ", " << omega_body_v[1] << ", " << omega_body_v[2]);
723 SG_LOG(SG_FLIGHT,SG_INFO,"omega_local_v[3]: " << omega_local_v[0] << ", " << omega_local_v[1] << ", " << omega_local_v[2]);
724 SG_LOG(SG_FLIGHT,SG_INFO,"omega_total_v[3]: " << omega_total_v[0] << ", " << omega_total_v[1] << ", " << omega_total_v[2]);
725 SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v[3]: " << euler_rates_v[0] << ", " << euler_rates_v[1] << ", " << euler_rates_v[2]);
726 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v[3]: " << geocentric_rates_v[0] << ", " << geocentric_rates_v[1] << ", " << geocentric_rates_v[2]);
727 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v[3]: " << geocentric_position_v[0] << ", " << geocentric_position_v[1] << ", " << geocentric_position_v[2]);
728 SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v[3]: " << geodetic_position_v[0] << ", " << geodetic_position_v[1] << ", " << geodetic_position_v[2]);
729 SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v[3]: " << euler_angles_v[0] << ", " << euler_angles_v[1] << ", " << euler_angles_v[2]);
730 SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rwy_local_v[3]: " << d_cg_rwy_local_v[0] << ", " << d_cg_rwy_local_v[1] << ", " << d_cg_rwy_local_v[2]);
731 SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rwy_rwy_v[3]: " << d_cg_rwy_rwy_v[0] << ", " << d_cg_rwy_rwy_v[1] << ", " << d_cg_rwy_rwy_v[2]);
732 SG_LOG(SG_FLIGHT,SG_INFO,"d_pilot_rwy_local_v[3]: " << d_pilot_rwy_local_v[0] << ", " << d_pilot_rwy_local_v[1] << ", " << d_pilot_rwy_local_v[2]);
733 SG_LOG(SG_FLIGHT,SG_INFO,"d_pilot_rwy_rwy_v[3]: " << d_pilot_rwy_rwy_v[0] << ", " << d_pilot_rwy_rwy_v[1] << ", " << d_pilot_rwy_rwy_v[2]);
735 SG_LOG(SG_FLIGHT,SG_INFO,"t_local_to_body_m[0][3]: " << t_local_to_body_m[0][0] << ", " << t_local_to_body_m[0][1] << ", " << t_local_to_body_m[0][2]);
736 SG_LOG(SG_FLIGHT,SG_INFO,"t_local_to_body_m[1][3]: " << t_local_to_body_m[1][0] << ", " << t_local_to_body_m[1][1] << ", " << t_local_to_body_m[1][2]);
737 SG_LOG(SG_FLIGHT,SG_INFO,"t_local_to_body_m[2][3]: " << t_local_to_body_m[2][0] << ", " << t_local_to_body_m[2][1] << ", " << t_local_to_body_m[2][2]);
739 SG_LOG(SG_FLIGHT,SG_INFO,"mass: " << mass );
740 SG_LOG(SG_FLIGHT,SG_INFO,"i_xx: " << i_xx );
741 SG_LOG(SG_FLIGHT,SG_INFO,"i_yy: " << i_yy );
742 SG_LOG(SG_FLIGHT,SG_INFO,"i_zz: " << i_zz );
743 SG_LOG(SG_FLIGHT,SG_INFO,"i_xz: " << i_xz );
744 SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << nlf );
745 SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << v_rel_wind );
746 SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << v_true_kts );
747 SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_ground: " << v_rel_ground );
748 SG_LOG(SG_FLIGHT,SG_INFO,"v_inertial: " << v_inertial );
749 SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << v_ground_speed );
750 SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv: " << v_equiv );
751 SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << v_equiv_kts );
752 SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated: " << v_calibrated );
753 SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << v_calibrated_kts );
754 SG_LOG(SG_FLIGHT,SG_INFO,"gravity: " << gravity );
755 SG_LOG(SG_FLIGHT,SG_INFO,"centrifugal_relief: " << centrifugal_relief );
756 SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << alpha );
757 SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << beta );
758 SG_LOG(SG_FLIGHT,SG_INFO,"alpha_dot: " << alpha_dot );
759 SG_LOG(SG_FLIGHT,SG_INFO,"beta_dot: " << beta_dot );
760 SG_LOG(SG_FLIGHT,SG_INFO,"cos_alpha: " << cos_alpha );
761 SG_LOG(SG_FLIGHT,SG_INFO,"sin_alpha: " << sin_alpha );
762 SG_LOG(SG_FLIGHT,SG_INFO,"cos_beta: " << cos_beta );
763 SG_LOG(SG_FLIGHT,SG_INFO,"sin_beta: " << sin_beta );
764 SG_LOG(SG_FLIGHT,SG_INFO,"cos_phi: " << cos_phi );
765 SG_LOG(SG_FLIGHT,SG_INFO,"sin_phi: " << sin_phi );
766 SG_LOG(SG_FLIGHT,SG_INFO,"cos_theta: " << cos_theta );
767 SG_LOG(SG_FLIGHT,SG_INFO,"sin_theta: " << sin_theta );
768 SG_LOG(SG_FLIGHT,SG_INFO,"cos_psi: " << cos_psi );
769 SG_LOG(SG_FLIGHT,SG_INFO,"sin_psi: " << sin_psi );
770 SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << gamma_vert_rad );
771 SG_LOG(SG_FLIGHT,SG_INFO,"gamma_horiz_rad: " << gamma_horiz_rad );
772 SG_LOG(SG_FLIGHT,SG_INFO,"sigma: " << sigma );
773 SG_LOG(SG_FLIGHT,SG_INFO,"density: " << density );
774 SG_LOG(SG_FLIGHT,SG_INFO,"v_sound: " << v_sound );
775 SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << mach_number );
776 SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << static_pressure );
777 SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << total_pressure );
778 SG_LOG(SG_FLIGHT,SG_INFO,"impact_pressure: " << impact_pressure );
779 SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << dynamic_pressure );
780 SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << static_temperature );
781 SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << total_temperature );
782 SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << sea_level_radius );
783 SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << earth_position_angle );
784 SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << runway_altitude );
785 SG_LOG(SG_FLIGHT,SG_INFO,"runway_latitude: " << runway_latitude );
786 SG_LOG(SG_FLIGHT,SG_INFO,"runway_longitude: " << runway_longitude );
787 SG_LOG(SG_FLIGHT,SG_INFO,"runway_heading: " << runway_heading );
788 SG_LOG(SG_FLIGHT,SG_INFO,"radius_to_rwy: " << radius_to_rwy );
789 SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << climb_rate );
790 SG_LOG(SG_FLIGHT,SG_INFO,"sin_lat_geocentric: " << sin_lat_geocentric );
791 SG_LOG(SG_FLIGHT,SG_INFO,"cos_lat_geocentric: " << cos_lat_geocentric );
792 SG_LOG(SG_FLIGHT,SG_INFO,"sin_longitude: " << sin_longitude );
793 SG_LOG(SG_FLIGHT,SG_INFO,"cos_longitude: " << cos_longitude );
794 SG_LOG(SG_FLIGHT,SG_INFO,"sin_latitude: " << sin_latitude );
795 SG_LOG(SG_FLIGHT,SG_INFO,"cos_latitude: " << cos_latitude );
796 SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << altitude_agl );
800 FGInterface::prepare_ground_cache_m(double ref_time, const double pt[3],
803 return ground_cache.prepare_ground_cache(ref_time, SGVec3d(pt), rad);
806 bool FGInterface::prepare_ground_cache_ft(double ref_time, const double pt[3],
809 // Convert units and do the real work.
810 SGVec3d pt_ft = SG_FEET_TO_METER*SGVec3d(pt);
811 return ground_cache.prepare_ground_cache(ref_time, pt_ft, rad*SG_FEET_TO_METER);
815 FGInterface::is_valid_m(double *ref_time, double pt[3], double *rad)
818 bool valid = ground_cache.is_valid(*ref_time, _pt, *rad);
819 sgdCopyVec3(pt, _pt.data());
823 bool FGInterface::is_valid_ft(double *ref_time, double pt[3], double *rad)
825 // Convert units and do the real work.
827 bool found_ground = ground_cache.is_valid(*ref_time, _pt, *rad);
828 sgdScaleVec3(pt, _pt.data(), SG_METER_TO_FEET);
829 *rad *= SG_METER_TO_FEET;
834 FGInterface::get_cat_m(double t, const double pt[3],
835 double end[2][3], double vel[2][3])
837 SGVec3d _end[2], _vel[2];
838 double dist = ground_cache.get_cat(t, SGVec3d(pt), _end, _vel);
839 for (int k=0; k<2; ++k) {
840 sgdCopyVec3( end[k], _end[k].data() );
841 sgdCopyVec3( vel[k], _vel[k].data() );
847 FGInterface::get_cat_ft(double t, const double pt[3],
848 double end[2][3], double vel[2][3])
850 // Convert units and do the real work.
851 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
852 SGVec3d _end[2], _vel[2];
853 double dist = ground_cache.get_cat(t, pt_m, _end, _vel);
854 for (int k=0; k<2; ++k) {
855 sgdScaleVec3( end[k], _end[k].data(), SG_METER_TO_FEET );
856 sgdScaleVec3( vel[k], _vel[k].data(), SG_METER_TO_FEET );
858 return dist*SG_METER_TO_FEET;
861 // Legacy interface just kept because of JSBSim
863 FGInterface::get_agl_m(double t, const double pt[3],
864 double contact[3], double normal[3], double vel[3],
865 int *type, double *loadCapacity,
866 double *frictionFactor, double *agl)
868 const SGMaterial* material;
869 SGVec3d _contact, _normal, _vel;
870 bool ret = ground_cache.get_agl(t, SGVec3d(pt), 2.0, _contact, _normal,
871 _vel, type, &material, agl);
872 sgdCopyVec3(contact, _contact.data());
873 sgdCopyVec3(normal, _normal.data());
874 sgdCopyVec3(vel, _vel.data());
876 *loadCapacity = material->get_load_resistence();
877 *frictionFactor = material->get_friction_factor();
880 *loadCapacity = DBL_MAX;
881 *frictionFactor = 1.0;
886 // Legacy interface just kept because of JSBSim
888 FGInterface::get_agl_ft(double t, const double pt[3],
889 double contact[3], double normal[3], double vel[3],
890 int *type, double *loadCapacity,
891 double *frictionFactor, double *agl)
893 // Convert units and do the real work.
894 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
896 const SGMaterial* material;
897 SGVec3d _contact, _normal, _vel;
898 bool ret = ground_cache.get_agl(t, pt_m, 2.0, _contact, _normal, _vel,
899 type, &material, agl);
900 // Convert units back ...
901 sgdScaleVec3( contact, _contact.data(), SG_METER_TO_FEET );
902 sgdScaleVec3( vel, _vel.data(), SG_METER_TO_FEET );
903 sgdCopyVec3( normal, _normal.data() );
904 *agl *= SG_METER_TO_FEET;
906 // return material properties if available
908 // FIXME: convert units?? now pascal to lbf/ft^2
909 *loadCapacity = 0.020885434*material->get_load_resistence();
910 *frictionFactor = material->get_friction_factor();
912 *loadCapacity = DBL_MAX;
913 *frictionFactor = 1.0;
919 FGInterface::get_agl_m(double t, const double pt[3], double max_altoff,
920 double contact[3], double normal[3], double vel[3],
921 int *type, const SGMaterial** material, double *agl)
923 SGVec3d _contact, _normal, _vel;
924 bool found = ground_cache.get_agl(t, SGVec3d(pt), max_altoff, _contact,
925 _normal, _vel, type, material, agl);
926 sgdCopyVec3(contact, _contact.data());
927 sgdCopyVec3(normal, _normal.data());
928 sgdCopyVec3(vel, _vel.data());
933 FGInterface::get_agl_ft(double t, const double pt[3], double max_altoff,
934 double contact[3], double normal[3], double vel[3],
935 int *type, const SGMaterial** material, double *agl)
937 // Convert units and do the real work.
938 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
939 SGVec3d _contact, _normal, _vel;
940 bool ret = ground_cache.get_agl(t, pt_m, SG_FEET_TO_METER * max_altoff,
941 _contact, _normal, _vel,
942 type, material, agl);
943 // Convert units back ...
944 sgdScaleVec3( contact, _contact.data(), SG_METER_TO_FEET );
945 sgdScaleVec3( vel, _vel.data(), SG_METER_TO_FEET );
946 sgdCopyVec3( normal, _normal.data() );
947 *agl *= SG_METER_TO_FEET;
953 FGInterface::get_groundlevel_m(double lat, double lon, double alt)
955 // Compute the cartesian position of the given lat/lon/alt.
956 SGVec3d pos = SGVec3d::fromGeod(SGGeod::fromRadM(lon, lat, alt));
958 // FIXME: how to handle t - ref_time differences ???
960 double ref_time, radius;
961 // Prepare the ground cache for that position.
962 if (!is_valid_m(&ref_time, cpos.data(), &radius)) {
963 bool ok = prepare_ground_cache_m(ref_time, pos.data(), 10);
964 /// This is most likely the case when the given altitude is
965 /// too low, try with a new altitude of 10000m, that should be
966 /// sufficient to find a ground level below everywhere on our planet
968 pos = SGVec3d::fromGeod(SGGeod::fromRadM(lon, lat, 10000));
969 /// If there is still no ground, return sea level radius
970 if (!prepare_ground_cache_m(ref_time, pos.data(), 10))
973 } else if (radius*radius <= distSqr(pos, cpos)) {
974 /// We reuse the old radius value, but only if it is at least 10 Meters ..
975 if (!(10 < radius)) // Well this strange compare is nan safe
978 bool ok = prepare_ground_cache_m(ref_time, pos.data(), radius);
979 /// This is most likely the case when the given altitude is
980 /// too low, try with a new altitude of 10000m, that should be
981 /// sufficient to find a ground level below everywhere on our planet
983 pos = SGVec3d::fromGeod(SGGeod::fromRadM(lon, lat, 10000));
984 /// If there is still no ground, return sea level radius
985 if (!prepare_ground_cache_m(ref_time, pos.data(), radius))
990 double contact[3], normal[3], vel[3], agl;
992 // Ignore the return value here, since it just tells us if
993 // the returns stem from the groundcache or from the coarse
994 // computations below the groundcache. The contact point is still something
995 // valid, the normals and the other returns just contain some defaults.
996 get_agl_m(ref_time, pos.data(), 2.0, contact, normal, vel, &type, 0, &agl);
997 SGGeod geod = SGGeod::fromCart(SGVec3d(contact));
998 return geod.getElevationM();
1002 FGInterface::caught_wire_m(double t, const double pt[4][3])
1005 for (int i=0; i<4; ++i)
1006 sgdCopyVec3(pt_m[i].data(), pt[i]);
1008 return ground_cache.caught_wire(t, pt_m);
1012 FGInterface::caught_wire_ft(double t, const double pt[4][3])
1014 // Convert units and do the real work.
1016 for (int i=0; i<4; ++i)
1017 sgdScaleVec3(pt_m[i].data(), pt[i], SG_FEET_TO_METER);
1019 return ground_cache.caught_wire(t, pt_m);
1023 FGInterface::get_wire_ends_m(double t, double end[2][3], double vel[2][3])
1025 SGVec3d _end[2], _vel[2];
1026 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
1027 for (int k=0; k<2; ++k) {
1028 sgdCopyVec3( end[k], _end[k].data() );
1029 sgdCopyVec3( vel[k], _vel[k].data() );
1035 FGInterface::get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
1037 // Convert units and do the real work.
1038 SGVec3d _end[2], _vel[2];
1039 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
1040 for (int k=0; k<2; ++k) {
1041 sgdScaleVec3( end[k], _end[k].data(), SG_METER_TO_FEET );
1042 sgdScaleVec3( vel[k], _vel[k].data(), SG_METER_TO_FEET );
1048 FGInterface::release_wire(void)
1050 ground_cache.release_wire();
1053 void fgToggleFDMdataLogging(void) {
1054 cur_fdm_state->ToggleDataLogging();