FlightGear next
flight.cxx
Go to the documentation of this file.
1// flight.cxx -- a general interface to the various flight models
2//
3// Written by Curtis Olson, started May 1997.
4//
5// Copyright (C) 1997 Curtis L. Olson - http://www.flightgear.org/~curt
6//
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.
11//
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.
16//
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.
20//
21// $Id$
22
23#ifdef HAVE_CONFIG_H
24# include "config.h"
25#endif
26
27#include "flight.hxx"
28
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>
34
35#include <Scenery/scenery.hxx>
36#include <Main/globals.hxx>
37#include <Main/fg_props.hxx>
38#include <FDM/groundcache.hxx>
39
40
41static inline void assign(double* ptr, const SGVec3d& vec)
42{
43 ptr[0] = vec[0];
44 ptr[1] = vec[1];
45 ptr[2] = vec[2];
46}
47
48// Constructor
53
55{
56 _setup();
57}
58
59// Destructor
61 // unbind(); // FIXME: should be called explicitly
62}
63
64int
66{
67 if (dt == 0.0) {
68 return 0; // paused
69 }
70
71 // this method is now obsolete - multiloop is handled by
72 // SGSubsystemGroup; the FDM group operates with a fixed time interval
73 // (defined by /sim/model-hz), so at this level we always want to run
74 // exactly one FDM iteration
75 return 1;
76}
77
78
84void
86{
87 inited = false;
88 bound = false;
89
90 _state.d_cg_rp_body_v = SGVec3d::zeros();
91 _state.v_dot_local_v = SGVec3d::zeros();
92 _state.v_dot_body_v = SGVec3d::zeros();
93 _state.a_cg_body_v = SGVec3d::zeros();
94 _state.a_pilot_body_v = SGVec3d::zeros();
95 _state.n_cg_body_v = SGVec3d::zeros();
96 _state.v_local_v = SGVec3d::zeros();
97 _state.v_local_rel_ground_v = SGVec3d::zeros();
98 _state.v_local_airmass_v = SGVec3d::zeros();
99 _state.v_body_v = SGVec3d::zeros();
100 _state.omega_body_v = SGVec3d::zeros();
101 _state.euler_rates_v = SGVec3d::zeros();
102 _state.geocentric_rates_v = SGVec3d::zeros();
103 _state.geodetic_position_v = SGGeod::fromRadM(0, 0, 0);
104 _state.cartesian_position_v = SGVec3d::fromGeod(_state.geodetic_position_v);
105 _state.geocentric_position_v = SGGeoc::fromCart(_state.cartesian_position_v);
106 _state.euler_angles_v = SGVec3d::zeros();
107
108 _state.nlf=0;
109 _state.v_rel_wind=_state.v_true_kts=0;
110 _state.v_ground_speed=_state.v_equiv_kts=0;
111 _state.v_calibrated_kts=0;
112 _state.alpha=_state.beta=0;
113 _state.gamma_vert_rad=0;
114 _state.density=_state.mach_number=0;
115 _state.static_pressure=_state.total_pressure=0;
116 _state.dynamic_pressure=0;
117 _state.static_temperature=_state.total_temperature=0;
118 _state.sea_level_radius=_state.earth_position_angle=0;
119 _state.runway_altitude=0;
120 _state.climb_rate=0;
121 _state.altitude_agl=0;
122 _state.track=0;
123}
124
125void
127
136void
138{
139 SG_LOG( SG_FLIGHT, SG_INFO, "Start common FDM init" );
140
141 set_inited( true );
142
143 ground_cache.set_cache_time_offset(globals->get_sim_time_sec());
144
145 // Set initial position
146 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing position..." );
147 double lon = fgGetDouble("/sim/presets/longitude-deg")
148 * SGD_DEGREES_TO_RADIANS;
149 double lat = fgGetDouble("/sim/presets/latitude-deg")
150 * SGD_DEGREES_TO_RADIANS;
151 double alt_ft = fgGetDouble("/sim/presets/altitude-ft");
152 double alt_m = alt_ft * SG_FEET_TO_METER;
153 set_Longitude( lon );
154 set_Latitude( lat );
155 SG_LOG( SG_FLIGHT, SG_INFO, "Checking for lon = "
156 << lon*SGD_RADIANS_TO_DEGREES << "deg, lat = "
157 << lat*SGD_RADIANS_TO_DEGREES << "deg, alt = "
158 << alt_ft << "ft");
159
160 double ground_elev_m = get_groundlevel_m(lat, lon, alt_m);
161 double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET;
162 _set_Runway_altitude ( ground_elev_ft );
163
164 // Set aircraft altitude
165 if ( fgGetBool("/sim/presets/onground") || alt_ft < ground_elev_ft ) {
166 fgSetDouble("/position/altitude-ft", ground_elev_ft + 0.1);
167 set_Altitude( ground_elev_ft + 0.1);
168 } else {
169 set_Altitude( alt_ft );
170 }
171
172 // Set ground elevation
173 SG_LOG( SG_FLIGHT, SG_INFO,
174 "...initializing ground elevation to " << ground_elev_ft
175 << "ft..." );
176
177 // Set sea-level radius
178 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing sea-level radius..." );
179 SG_LOG( SG_FLIGHT, SG_INFO, " lat = "
180 << fgGetDouble("/sim/presets/latitude-deg")
181 << " alt = " << get_Altitude() );
182 double slr = SGGeodesy::SGGeodToSeaLevelRadius(_state.geodetic_position_v);
183 _set_Sea_level_radius( slr * SG_METER_TO_FEET );
184
185 // Set initial Euler angles
186 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing Euler angles..." );
187 set_Euler_Angles( fgGetDouble("/sim/presets/roll-deg")
188 * SGD_DEGREES_TO_RADIANS,
189 fgGetDouble("/sim/presets/pitch-deg")
190 * SGD_DEGREES_TO_RADIANS,
191 fgGetDouble("/sim/presets/heading-deg")
192 * SGD_DEGREES_TO_RADIANS );
193
194 // Set initial velocities
195 SG_LOG( SG_FLIGHT, SG_INFO, "...initializing velocities..." );
196 if ( !fgHasNode("/sim/presets/speed-set") ) {
198 } else {
199 const std::string speedset = fgGetString("/sim/presets/speed-set");
200 if ( speedset == "knots" || speedset == "KNOTS" ) {
201 set_V_calibrated_kts( fgGetDouble("/sim/presets/airspeed-kt") );
202 } else if ( speedset == "mach" || speedset == "MACH" ) {
203 set_Mach_number( fgGetDouble("/sim/presets/mach") );
204 } else if ( speedset == "UVW" || speedset == "uvw" ) {
206 fgGetDouble("/sim/presets/uBody-fps"),
207 fgGetDouble("/sim/presets/vBody-fps"),
208 fgGetDouble("/sim/presets/wBody-fps") );
209 } else if ( speedset == "NED" || speedset == "ned" ) {
211 fgGetDouble("/sim/presets/speed-north-fps"),
212 fgGetDouble("/sim/presets/speed-east-fps"),
213 fgGetDouble("/sim/presets/speed-down-fps") );
214 } else {
215 SG_LOG( SG_FLIGHT, SG_ALERT,
216 "Unrecognized value for /sim/presets/speed-set: "
217 << speedset);
219 }
220 }
221
222 if ( fgHasNode("/sim/presets/glideslope-deg") )
223 set_Gamma_vert_rad( fgGetDouble("/sim/presets/glideslope-deg")
224 * SGD_DEGREES_TO_RADIANS );
225 else if ( fgHasNode("/sim/presets/speed-set") &&
226 fgHasNode( "/sim/presets/vertical-speed-fps") )
227 {
228 set_Climb_Rate( fgGetDouble("/sim/presets/vertical-speed-fps") );
229 }
230
232
233 SG_LOG( SG_FLIGHT, SG_INFO, "End common FDM init" );
234}
235
237 std::function<void(const std::string& from, const std::string& to)> fn
238 )
239{
240 /* Do nothing by default. */
241}
242
243
253void
255{
256 bound = true;
257
258 _tiedProperties.setRoot(globals->get_props());
259 // Aircraft position
260 _tiedProperties.Tie("/position/latitude-deg", this,
263 false);
264 fgSetArchivable("/position/latitude-deg");
265 _tiedProperties.Tie("/position/longitude-deg", this,
268 false);
269 fgSetArchivable("/position/longitude-deg");
270 _tiedProperties.Tie("/position/altitude-ft", this,
273 false);
274 fgSetArchivable("/position/altitude-ft");
275 _tiedProperties.Tie("/position/altitude-agl-ft", this,
277 fgSetArchivable("/position/ground-elev-ft");
278 _tiedProperties.Tie("/position/ground-elev-ft", this,
280 fgSetArchivable("/position/ground-elev-m");
281 _tiedProperties.Tie("/position/ground-elev-m", this,
283 _tiedProperties.Tie("/environment/ground-elevation-m", this,
285 fgSetArchivable("/position/sea-level-radius-ft");
286 _tiedProperties.Tie("/position/sea-level-radius-ft", this,
289
290 // Orientation
291 _tiedProperties.Tie("/orientation/roll-deg", this,
294 fgSetArchivable("/orientation/roll-deg");
295 _tiedProperties.Tie("/orientation/pitch-deg", this,
298 fgSetArchivable("/orientation/pitch-deg");
299 _tiedProperties.Tie("/orientation/heading-deg", this,
302 fgSetArchivable("/orientation/heading-deg");
303 _tiedProperties.Tie("/orientation/true-heading-deg", this,
306 fgSetArchivable("/orientation/true-heading-deg");
307 _tiedProperties.Tie("/orientation/track-deg", this,
308 &FGInterface::get_Track); // read-only
309 _tiedProperties.Tie("/orientation/path-deg", this,
310 &FGInterface::get_Path); // read-only
311
312 // Body-axis "euler rates" (rotation speed, but in a funny
313 // representation).
314 _tiedProperties.Tie("/orientation/roll-rate-degps", this,
317 _tiedProperties.Tie("/orientation/pitch-rate-degps", this,
320 _tiedProperties.Tie("/orientation/yaw-rate-degps", this,
323
324 _tiedProperties.Tie("/orientation/p-body", this, &FGInterface::get_P_body); // read-only
325 _tiedProperties.Tie("/orientation/q-body", this, &FGInterface::get_Q_body); // read-only
326 _tiedProperties.Tie("/orientation/r-body", this, &FGInterface::get_R_body); // read-only
327
328 // Ground speed knots
329 _tiedProperties.Tie("/velocities/groundspeed-kt", this,
332
333 // Calibrated airspeed
334 _tiedProperties.Tie("/velocities/airspeed-kt", this,
337 false);
338
339 _tiedProperties.Tie("/velocities/equivalent-kt", this,
340 &FGInterface::get_V_equiv_kts); // read-only
341
342 // Mach number
343 _tiedProperties.Tie("/velocities/mach", this,
346 false);
347
348 // Local velocities
349// _tiedProperties.Tie("/velocities/speed-north-fps", this,
350// &FGInterface::get_V_north,
351// &FGInterface::set_V_north);
352// fgSetArchivable("/velocities/speed-north-fps");
353// _tiedProperties.Tie("/velocities/speed-east-fps", this,
354// &FGInterface::get_V_east,
355// &FGInterface::set_V_east);
356// fgSetArchivable("/velocities/speed-east-fps");
357// _tiedProperties.Tie("/velocities/speed-down-fps", this,
358// &FGInterface::get_V_down,
359// &FGInterface::set_V_down);
360// fgSetArchivable("/velocities/speed-down-fps");
361
362 // FIXME: Temporarily read-only, until the
363 // incompatibilities between JSBSim and
364 // LaRCSim are fixed (LaRCSim adds the
365 // earth's rotation to the east velocity).
366 _tiedProperties.Tie("/velocities/speed-north-fps", this,
368 _tiedProperties.Tie("/velocities/speed-east-fps", this,
370 _tiedProperties.Tie("/velocities/speed-down-fps", this,
372
373 _tiedProperties.Tie("/velocities/north-relground-fps", this,
375 _tiedProperties.Tie("/velocities/east-relground-fps", this,
377 _tiedProperties.Tie("/velocities/down-relground-fps", this,
379
380 // ECEF velocity in body axis
381 // FIXME: temporarily archivable, until the NED problem is fixed.
382 _tiedProperties.Tie("/velocities/uBody-fps", this,
385 false);
386 fgSetArchivable("/velocities/uBody-fps");
387 _tiedProperties.Tie("/velocities/vBody-fps", this,
390 false);
391 fgSetArchivable("/velocities/vBody-fps");
392 _tiedProperties.Tie("/velocities/wBody-fps", this,
395 false);
396 fgSetArchivable("/velocities/wBody-fps");
397
398 // Climb and slip (read-only)
399 _tiedProperties.Tie("/velocities/vertical-speed-fps", this,
402 _tiedProperties.Tie("/velocities/glideslope", this,
405 _tiedProperties.Tie("/orientation/side-slip-rad", this,
407 _tiedProperties.Tie("/orientation/side-slip-deg", this,
408 &FGInterface::get_Beta_deg); // read-only
409 _tiedProperties.Tie("/orientation/alpha-deg", this,
411 _tiedProperties.Tie("/accelerations/nlf", this,
412 &FGInterface::get_Nlf); // read-only
413
414 // NED accelerations
415 _tiedProperties.Tie("/accelerations/ned/north-accel-fps_sec",
416 this, &FGInterface::get_V_dot_north); // read-only
417 _tiedProperties.Tie("/accelerations/ned/east-accel-fps_sec",
418 this, &FGInterface::get_V_dot_east); // read-only
419 _tiedProperties.Tie("/accelerations/ned/down-accel-fps_sec",
420 this, &FGInterface::get_V_dot_down); // read-only
421
422 // Pilot accelerations
423 _tiedProperties.Tie("/accelerations/pilot/x-accel-fps_sec",
424 this, &FGInterface::get_A_X_pilot, &FGInterface::set_A_X_pilot, false);
425 _tiedProperties.Tie("/accelerations/pilot/y-accel-fps_sec",
426 this, &FGInterface::get_A_Y_pilot, &FGInterface::set_A_Y_pilot, false);
427 _tiedProperties.Tie("/accelerations/pilot/z-accel-fps_sec",
428 this, &FGInterface::get_A_Z_pilot, &FGInterface::set_A_Z_pilot, false);
429
430 _tiedProperties.Tie("/accelerations/n-z-cg-fps_sec",
431 this, &FGInterface::get_N_Z_cg); // read-only
432}
433
434
441void
443{
444 _tiedProperties.Untie();
445 bound = false;
446}
447
451void
453{
454 SG_LOG(SG_FLIGHT, SG_ALERT, "dummy update() ... SHOULDN'T BE CALLED!");
455}
456
457bool FGInterface::readState(SGIOChannel* io)
458{
459 FlightState buf;
460 int length = sizeof(FlightState);
461
462 if ( io->get_type() == sgFileType ) {
463 if ( io->read( (char *)(& buf), length ) == length ) {
464 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
465 } else {
466 return false;
467 }
468 } else {
469 while ( io->read( (char *)(& buf), length ) == length ) {
470 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
471 }
472 }
473
474 _state = buf; // copy the read state over
475 return true;
476}
477
478bool FGInterface::writeState(SGIOChannel* io)
479{
480 if (!bound || !inited) {
481 return false;
482 }
483
484 int length = sizeof(FlightState);
485 if ( ! io->write( (char *)(& _state), length ) ) {
486 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
487 return false;
488 }
489
490 return true;
491}
492
493void FGInterface::_updatePositionM(const SGVec3d& cartPos)
494{
495 TrackComputer tracker( _state.track, _state.path, _state.geodetic_position_v );
496 _state.cartesian_position_v = cartPos;
497 _state.geodetic_position_v = SGGeod::fromCart(_state.cartesian_position_v);
498 _state.geocentric_position_v = SGGeoc::fromCart(_state.cartesian_position_v);
499 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(_state.geodetic_position_v)*SG_METER_TO_FEET );
501}
502
503
504void FGInterface::_updatePosition(const SGGeod& geod)
505{
506 TrackComputer tracker( _state.track, _state.path, _state.geodetic_position_v );
507 _state.geodetic_position_v = geod;
508 _state.cartesian_position_v = SGVec3d::fromGeod(_state.geodetic_position_v);
509 _state.geocentric_position_v = SGGeoc::fromCart(_state.cartesian_position_v);
510
511 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(_state.geodetic_position_v)*SG_METER_TO_FEET );
513}
514
515
516void FGInterface::_updatePosition(const SGGeoc& geoc)
517{
518 TrackComputer tracker( _state.track, _state.path, _state.geodetic_position_v );
519 _state.geocentric_position_v = geoc;
520 _state.cartesian_position_v = SGVec3d::fromGeoc(_state.geocentric_position_v);
521 _state.geodetic_position_v = SGGeod::fromCart(_state.cartesian_position_v);
522
523 _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(_state.geodetic_position_v)*SG_METER_TO_FEET );
525}
526
527
528void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
529{
530 _updatePosition(SGGeod::fromRadFt(lon, lat, alt));
531}
532
533
534void FGInterface::_updateGeocentricPosition( double lat, double lon,
535 double alt )
536{
537 _updatePosition(SGGeoc::fromRadFt(lon, lat, get_Sea_level_radius() + alt));
538}
539
541 double groundlevel_m = get_groundlevel_m(_state.geodetic_position_v);
542 _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
543}
544
545// Positions
547 _state.geodetic_position_v.setLatitudeRad(lat);
548}
549
551 _state.geodetic_position_v.setLongitudeRad(lon);
552}
553
555 _state.geodetic_position_v.setElevationFt(alt);
556}
557
558void FGInterface::set_AltitudeAGL(double altagl) {
559 _state.altitude_agl=altagl;
560}
561
562// Velocities
564 _state.v_calibrated_kts = vc;
565}
566
568 _state.mach_number = mach;
569}
570
572 double east,
573 double down ){
574 _state.v_local_v[0] = north;
575 _state.v_local_v[1] = east;
576 _state.v_local_v[2] = down;
577}
578
580 double v,
581 double w){
582 _state.v_body_v[0] = u;
583 _state.v_body_v[1] = v;
584 _state.v_body_v[2] = w;
585}
586
587// Euler angles
589 double theta,
590 double psi ) {
591 _state.euler_angles_v[0] = phi;
592 _state.euler_angles_v[1] = theta;
593 _state.euler_angles_v[2] = psi;
594}
595
596// Flight Path
598 _state.climb_rate = roc;
599}
600
602 _state.gamma_vert_rad = gamma;
603}
604
605void FGInterface::set_Static_pressure(double p) { _state.static_pressure = p; }
606void FGInterface::set_Static_temperature(double T) { _state.static_temperature = T; }
607void FGInterface::set_Density(double rho) { _state.density = rho; }
608
610 double weast,
611 double wdown ) {
612 _state.v_local_airmass_v[0] = wnorth;
613 _state.v_local_airmass_v[1] = weast;
614 _state.v_local_airmass_v[2] = wdown;
615}
616
617
619{
620 SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rp_body_v: " << _state.d_cg_rp_body_v);
621 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_local_v: " << _state.v_dot_local_v);
622 SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_body_v: " << _state.v_dot_body_v);
623 SG_LOG(SG_FLIGHT,SG_INFO,"a_cg_body_v: " << _state.a_cg_body_v);
624 SG_LOG(SG_FLIGHT,SG_INFO,"a_pilot_body_v: " << _state.a_pilot_body_v);
625 SG_LOG(SG_FLIGHT,SG_INFO,"n_cg_body_v: " << _state.n_cg_body_v);
626 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v: " << _state.v_local_v);
627 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_rel_ground_v: " << _state.v_local_rel_ground_v);
628 SG_LOG(SG_FLIGHT,SG_INFO,"v_local_airmass_v: " << _state.v_local_airmass_v);
629 SG_LOG(SG_FLIGHT,SG_INFO,"v_body_v: " << _state.v_body_v);
630 SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v: " << _state.omega_body_v);
631 SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v: " << _state.euler_rates_v);
632 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v: " << _state.geocentric_rates_v);
633 SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v: " << _state.geocentric_position_v);
634 SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v: " << _state.geodetic_position_v);
635 SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v: " << _state.euler_angles_v);
636
637 SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << _state.nlf );
638 SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << _state.v_rel_wind );
639 SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << _state.v_true_kts );
640 SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << _state.v_ground_speed );
641 SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << _state.v_equiv_kts );
642 SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << _state.v_calibrated_kts );
643 SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << _state.alpha );
644 SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << _state.beta );
645 SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << _state.gamma_vert_rad );
646 SG_LOG(SG_FLIGHT,SG_INFO,"density: " << _state.density );
647 SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << _state.mach_number );
648 SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << _state.static_pressure );
649 SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << _state.total_pressure );
650 SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << _state.dynamic_pressure );
651 SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << _state.static_temperature );
652 SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << _state.total_temperature );
653 SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << _state.sea_level_radius );
654 SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << _state.earth_position_angle );
655 SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << _state.runway_altitude );
656 SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << _state.climb_rate );
657 SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << _state.altitude_agl );
658}
659
660bool
661FGInterface::prepare_ground_cache_m(double startSimTime, double endSimTime,
662 const double pt[3], double rad)
663{
664 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
665 SGVec3d(pt), rad);
666}
667
668bool
669FGInterface::prepare_ground_cache_ft(double startSimTime, double endSimTime,
670 const double pt[3], double rad)
671{
672 // Convert units and do the real work.
673 SGVec3d pt_ft = SG_FEET_TO_METER*SGVec3d(pt);
674 return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
675 pt_ft, rad*SG_FEET_TO_METER);
676}
677
678bool
679FGInterface::is_valid_m(double *ref_time, double pt[3], double *rad)
680{
681 SGVec3d _pt;
682 bool valid = ground_cache.is_valid(*ref_time, _pt, *rad);
683 assign(pt, _pt);
684 return valid;
685}
686
687bool FGInterface::is_valid_ft(double *ref_time, double pt[3], double *rad)
688{
689 // Convert units and do the real work.
690 SGVec3d _pt;
691 bool found_ground = ground_cache.is_valid(*ref_time, _pt, *rad);
692 assign(pt, SG_METER_TO_FEET*_pt);
693 *rad *= SG_METER_TO_FEET;
694 return found_ground;
695}
696
697double
698FGInterface::get_cat_m(double t, const double pt[3],
699 double end[2][3], double vel[2][3])
700{
701 SGVec3d _end[2], _vel[2];
702 double dist = ground_cache.get_cat(t, SGVec3d(pt), _end, _vel);
703 for (int k=0; k<2; ++k) {
704 assign( end[k], _end[k] );
705 assign( vel[k], _vel[k] );
706 }
707 return dist;
708}
709
710double
711FGInterface::get_cat_ft(double t, const double pt[3],
712 double end[2][3], double vel[2][3])
713{
714 // Convert units and do the real work.
715 SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
716 SGVec3d _end[2], _vel[2];
717 double dist = ground_cache.get_cat(t, pt_m, _end, _vel);
718 for (int k=0; k<2; ++k) {
719 assign( end[k], SG_METER_TO_FEET*_end[k] );
720 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
721 }
722 return dist*SG_METER_TO_FEET;
723}
724
725bool
726FGInterface::get_body_m(double t, simgear::BVHNode::Id id,
727 double bodyToWorld[16], double linearVel[3],
728 double angularVel[3])
729{
730 SGMatrixd _bodyToWorld;
731 SGVec3d _linearVel, _angularVel;
732 bool _foundID = ground_cache.get_body(t, _bodyToWorld, _linearVel, _angularVel, id);
733
734 assign(linearVel, _linearVel);
735 assign(angularVel, _angularVel);
736 for (unsigned int i = 0; i < 16; ++i)
737 bodyToWorld[i] = _bodyToWorld.data()[i];
738
739 if (!_foundID)
740 return false;
741
742 return true;
743}
744
745bool
746FGInterface::get_agl_m(double t, const double pt[3], double max_altoff,
747 double contact[3], double normal[3],
748 double linearVel[3], double angularVel[3],
749 simgear::BVHMaterial const*& material, simgear::BVHNode::Id& id)
750{
751 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
752 SGVec3d _contact, _normal, _linearVel, _angularVel;
753 material = 0;
754 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
755 _angularVel, id, material);
756 // correct the linear velocity, since the line intersector delivers
757 // values for the start point and the get_agl function should
758 // traditionally deliver for the contact point
759 _linearVel += cross(_angularVel, _contact - pt_m);
760
761 assign(contact, _contact);
762 assign(normal, _normal);
763 assign(linearVel, _linearVel);
764 assign(angularVel, _angularVel);
765 return ret;
766}
767
768bool
769FGInterface::get_agl_ft(double t, const double pt[3], double max_altoff,
770 double contact[3], double normal[3],
771 double linearVel[3], double angularVel[3],
772 simgear::BVHMaterial const*& material, simgear::BVHNode::Id& id)
773{
774 // Convert units and do the real work.
775 SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
776 pt_m *= SG_FEET_TO_METER;
777 SGVec3d _contact, _normal, _linearVel, _angularVel;
778 material = 0;
779 bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
780 _angularVel, id, material);
781 // correct the linear velocity, since the line intersector delivers
782 // values for the start point and the get_agl function should
783 // traditionally deliver for the contact point
784 _linearVel += cross(_angularVel, _contact - pt_m);
785
786 // Convert units back ...
787 assign( contact, SG_METER_TO_FEET*_contact );
788 assign( normal, _normal );
789 assign( linearVel, SG_METER_TO_FEET*_linearVel );
790 assign( angularVel, _angularVel );
791 return ret;
792}
793
794bool
795FGInterface::get_nearest_m(double t, const double pt[3], double maxDist,
796 double contact[3], double normal[3],
797 double linearVel[3], double angularVel[3],
798 simgear::BVHMaterial const*& material,
799 simgear::BVHNode::Id& id)
800{
801 SGVec3d _contact, _linearVel, _angularVel;
802 material = 0;
803 if (!ground_cache.get_nearest(t, SGVec3d(pt), maxDist, _contact, _linearVel,
804 _angularVel, id, material))
805 return false;
806
807 assign(contact, _contact);
808 assign(linearVel, _linearVel);
809 assign(angularVel, _angularVel);
810 return true;
811}
812
813bool
814FGInterface::get_nearest_ft(double t, const double pt[3], double maxDist,
815 double contact[3], double normal[3],
816 double linearVel[3], double angularVel[3],
817 simgear::BVHMaterial const*& material,
818 simgear::BVHNode::Id& id)
819{
820 SGVec3d _contact, _linearVel, _angularVel;
821 material = 0;
822 if (!ground_cache.get_nearest(t, SG_FEET_TO_METER*SGVec3d(pt),
823 SG_FEET_TO_METER*maxDist, _contact, _linearVel,
824 _angularVel, id, material))
825 return false;
826
827 assign(contact, SG_METER_TO_FEET*_contact);
828 assign(linearVel, SG_METER_TO_FEET*_linearVel);
829 assign(angularVel, _angularVel);
830 return true;
831}
832
833double
834FGInterface::get_groundlevel_m(double lat, double lon, double alt)
835{
836 return get_groundlevel_m(SGGeod::fromRadM(lon, lat, alt));
837}
838
839double
841{
842 // Compute the cartesian position of the given lat/lon/alt.
843 SGVec3d pos = SGVec3d::fromGeod(geod);
844
845 // FIXME: how to handle t - ref_time differences ???
846 SGVec3d cpos;
847 double ref_time = 0, radius;
848 // Prepare the ground cache for that position.
849 if (!is_valid_m(&ref_time, cpos.data(), &radius)) {
850 double startTime = ref_time;
851 double endTime = startTime + 1;
852 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), 10);
856 if (!ok) {
857 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
859 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), 10))
860 return 0;
861 }
862 } else if (radius*radius <= distSqr(pos, cpos)) {
863 double startTime = ref_time;
864 double endTime = startTime + 1;
865
867 if (!(10 < radius)) // Well this strange compare is nan safe
868 radius = 10;
869
870 bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), radius);
874 if (!ok) {
875 pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
877 if (!prepare_ground_cache_m(startTime, endTime, pos.data(), radius))
878 return 0;
879 }
880 }
881
882 double contact[3], normal[3], vel[3], angvel[3];
883 const simgear::BVHMaterial* material;
884 simgear::BVHNode::Id id;
885 // Ignore the return value here, since it just tells us if
886 // the returns stem from the groundcache or from the coarse
887 // computations below the groundcache. The contact point is still something
888 // valid, the normals and the other returns just contain some defaults.
889 get_agl_m(ref_time, pos.data(), 2.0, contact, normal, vel, angvel,
890 material, id);
891 return SGGeod::fromCart(SGVec3d(contact)).getElevationM();
892}
893
894bool
895FGInterface::caught_wire_m(double t, const double pt[4][3])
896{
897 SGVec3d pt_m[4];
898 for (int i=0; i<4; ++i)
899 pt_m[i] = SGVec3d(pt[i]);
900
901 return ground_cache.caught_wire(t, pt_m);
902}
903
904bool
905FGInterface::caught_wire_ft(double t, const double pt[4][3])
906{
907 // Convert units and do the real work.
908 SGVec3d pt_m[4];
909 for (int i=0; i<4; ++i)
910 pt_m[i] = SG_FEET_TO_METER*SGVec3d(pt[i]);
911
912 return ground_cache.caught_wire(t, pt_m);
913}
914
915bool
916FGInterface::get_wire_ends_m(double t, double end[2][3], double vel[2][3])
917{
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], _end[k] );
922 assign( vel[k], _vel[k] );
923 }
924 return ret;
925}
926
927bool
928FGInterface::get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
929{
930 // Convert units and do the real work.
931 SGVec3d _end[2], _vel[2];
932 bool ret = ground_cache.get_wire_ends(t, _end, _vel);
933 for (int k=0; k<2; ++k) {
934 assign( end[k], SG_METER_TO_FEET*_end[k] );
935 assign( vel[k], SG_METER_TO_FEET*_vel[k] );
936 }
937 return ret;
938}
939
940void
942{
943 ground_cache.release_wire();
944}
#define p(x)
#define _normal
#define i(x)
void init() override
Definition flight.cxx:126
void _updatePosition(const SGGeod &geod)
Definition flight.cxx:504
double get_V_dot_east() const
Definition flight.hxx:534
void set_Theta_dot_degps(double x)
Definition flight.hxx:323
void set_V_ground_speed_kt(double ground_speed)
Definition flight.hxx:591
virtual void set_Static_temperature(double T)
Definition flight.cxx:606
double get_V_east() const
Definition flight.hxx:558
double get_vBody() const
Definition flight.hxx:561
void _update_ground_elev_at_pos(void)
Definition flight.cxx:540
virtual ~FGInterface()
Definition flight.cxx:60
bool get_nearest_m(double t, const double pt[3], double maxDist, double contact[3], double normal[3], double linearVel[3], double angularVel[3], simgear::BVHMaterial const *&material, simgear::BVHNode::Id &id)
Definition flight.cxx:795
double get_V_north_rel_ground() const
Definition flight.hxx:566
bool writeState(SGIOChannel *io)
Definition flight.cxx:478
void set_V_east(double east)
Definition flight.hxx:472
double get_V_down() const
Definition flight.hxx:559
virtual void set_Latitude_deg(double lat)
Definition flight.hxx:458
virtual void set_Theta_deg(double theta)
Definition flight.hxx:503
virtual void set_Velocities_Body(double u, double v, double w)
Definition flight.cxx:579
bool caught_wire_m(double t, const double pt[4][3])
Definition flight.cxx:895
bool get_nearest_ft(double t, const double pt[3], double maxDist, double contact[3], double normal[3], double linearVel[3], double angularVel[3], simgear::BVHMaterial const *&material, simgear::BVHNode::Id &id)
Definition flight.cxx:814
virtual void set_vBody(double vBody)
Definition flight.hxx:482
double get_V_calibrated_kts() const
Definition flight.hxx:595
void unbind() override
Unbind any properties bound to this FDM.
Definition flight.cxx:442
void set_inited(bool value)
Definition flight.hxx:439
double get_Psi_deg() const
Definition flight.hxx:653
bool get_agl_ft(double t, const double pt[3], double max_altoff, double contact[3], double normal[3], double linearVel[3], double angularVel[3], simgear::BVHMaterial const *&material, simgear::BVHNode::Id &id)
Definition flight.cxx:769
virtual void set_wBody(double wBody)
Definition flight.hxx:485
double get_V_equiv_kts() const
Definition flight.hxx:593
bool get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
Definition flight.cxx:928
bool is_valid_ft(double *ref_time, double pt[3], double *rad)
Definition flight.cxx:687
double get_V_down_rel_ground() const
Definition flight.hxx:572
virtual void set_Phi_deg(double phi)
Definition flight.hxx:500
double get_cat_m(double t, const double pt[3], double end[2][3], double vel[2][3])
Definition flight.cxx:698
virtual void property_associations(std::function< void(const std::string &from, const std::string &to)> fn)
Definition flight.cxx:236
virtual void set_Latitude(double lat)
Definition flight.cxx:546
virtual void set_Climb_Rate(double roc)
Definition flight.cxx:597
double get_P_body() const
Definition flight.hxx:597
void bind() override
Bind getters and setters to properties.
Definition flight.cxx:254
double get_Gamma_vert_rad() const
Definition flight.hxx:662
void set_Phi_dot_degps(double x)
Definition flight.hxx:318
void common_init()
Initialize the state of the FDM.
Definition flight.cxx:137
bool readState(SGIOChannel *io)
Definition flight.cxx:457
bool prepare_ground_cache_ft(double startSimTime, double endSimTime, const double pt[3], double rad)
Definition flight.cxx:669
virtual void set_Mach_number(double mach)
Definition flight.cxx:567
void _set_Runway_altitude(double alt)
Definition flight.hxx:388
double get_cat_ft(double t, const double pt[3], double end[2][3], double vel[2][3])
Definition flight.cxx:711
double get_Track(void) const
Definition flight.hxx:638
virtual void set_Gamma_vert_rad(double gamma)
Definition flight.cxx:601
void set_V_down(double down)
Definition flight.hxx:475
double get_Beta_deg() const
Definition flight.hxx:661
void set_Alpha_deg(double a)
Definition flight.hxx:378
double get_Mach_number() const
Definition flight.hxx:665
double get_A_Z_pilot() const
Definition flight.hxx:547
void release_wire(void)
Definition flight.cxx:941
virtual void set_V_calibrated_kts(double vc)
Definition flight.cxx:563
bool prepare_ground_cache_m(double startSimTime, double endSimTime, const double pt[3], double rad)
Definition flight.cxx:661
void _set_Beta(double b)
Definition flight.hxx:376
double get_Runway_altitude_m() const
Definition flight.hxx:680
double get_Beta() const
Definition flight.hxx:660
virtual void set_Altitude(double alt)
Definition flight.cxx:554
double get_Runway_altitude() const
Definition flight.hxx:679
bool get_agl_m(double t, const double pt[3], double max_altoff, double contact[3], double normal[3], double linearVel[3], double angularVel[3], simgear::BVHMaterial const *&material, simgear::BVHNode::Id &id)
Definition flight.cxx:746
double get_V_dot_down() const
Definition flight.hxx:535
virtual void set_Longitude_deg(double lon)
Definition flight.hxx:461
double get_uBody() const
Definition flight.hxx:560
virtual void set_Density(double rho)
Definition flight.cxx:607
double get_Nlf(void) const
Definition flight.hxx:553
double get_Longitude_deg() const
Definition flight.hxx:644
double get_N_Z_cg() const
Definition flight.hxx:551
bool is_valid_m(double *ref_time, double pt[3], double *rad)
Definition flight.cxx:679
double get_Path(void) const
Definition flight.hxx:639
double get_Latitude_deg() const
Definition flight.hxx:641
double get_V_north() const
Definition flight.hxx:557
void _setup()
Set default values for the state of the FDM.
Definition flight.cxx:85
bool get_wire_ends_m(double t, double end[2][3], double vel[2][3])
Definition flight.cxx:916
double get_A_X_pilot() const
Definition flight.hxx:545
void _set_Sea_level_radius(double r)
Definition flight.hxx:386
void _updateGeocentricPosition(double lat_geoc, double lon, double alt)
Definition flight.cxx:534
double get_V_dot_north() const
Definition flight.hxx:533
double get_A_Y_pilot() const
Definition flight.hxx:546
double get_Altitude() const
Definition flight.hxx:634
void set_V_north(double north)
Definition flight.hxx:469
virtual void set_Static_pressure(double p)
Definition flight.cxx:605
bool caught_wire_ft(double t, const double pt[4][3])
Definition flight.cxx:905
virtual void set_Psi_deg(double psi)
Definition flight.hxx:506
virtual void set_Euler_Angles(double phi, double theta, double psi)
Definition flight.cxx:588
void update(double dt) override
Update the state of the FDM (i.e.
Definition flight.cxx:452
bool get_body_m(double t, simgear::BVHNode::Id id, double bodyToWorld[16], double linearVel[3], double angularVel[3])
Definition flight.cxx:726
void _updateGeodeticPosition(double lat, double lon, double alt)
Definition flight.cxx:528
double get_Psi_dot_degps() const
Definition flight.hxx:606
double get_V_east_rel_ground() const
Definition flight.hxx:569
double get_Phi_dot_degps() const
Definition flight.hxx:604
void _updatePositionM(const SGVec3d &cartPos)
Definition flight.cxx:493
int _calc_multiloop(double dt)
Definition flight.cxx:65
double get_Sea_level_radius() const
Definition flight.hxx:674
void reset_wake_group(void)
Definition flight.hxx:791
double get_Alpha_deg() const
Definition flight.hxx:659
virtual void set_uBody(double uBody)
Definition flight.hxx:479
virtual void set_Longitude(double lon)
Definition flight.cxx:550
double get_groundlevel_m(double lat, double lon, double alt)
Definition flight.cxx:834
double get_V_ground_speed_kt() const
Definition flight.hxx:590
double get_Climb_Rate() const
Definition flight.hxx:682
void _busdump(void)
Definition flight.cxx:618
virtual void set_Velocities_Local(double north, double east, double down)
Definition flight.cxx:571
double get_Altitude_AGL(void) const
Definition flight.hxx:637
double get_Q_body() const
Definition flight.hxx:598
virtual void set_Velocities_Local_Airmass(double wnorth, double weast, double wdown)
Definition flight.cxx:609
double get_Theta_dot_degps() const
Definition flight.hxx:605
virtual void set_AltitudeAGL(double altagl)
Definition flight.cxx:558
double get_Phi_deg() const
Definition flight.hxx:651
double get_wBody() const
Definition flight.hxx:562
double get_R_body() const
Definition flight.hxx:599
void set_Psi_dot_degps(double x)
Definition flight.hxx:328
double get_Theta_deg() const
Definition flight.hxx:652
A little helper class to update the track if the position has changed.
Definition flight.hxx:105
bool fgHasNode(const char *path)
Test whether a given node exists.
Definition fg_props.cxx:507
std::string fgGetString(const char *name, const char *defaultValue)
Get a string value for a property.
Definition fg_props.cxx:556
void fgSetArchivable(const char *name, bool state)
Set the state of the archive attribute for a property.
Definition fg_props.cxx:598
static void assign(double *ptr, const SGVec3d &vec)
Definition flight.cxx:41
FGGlobals * globals
Definition globals.cxx:142
bool fgSetDouble(const char *name, double defaultValue)
Set a double value for a property.
Definition proptest.cpp:31
bool fgGetBool(char const *name, bool def)
Get a bool value for a property.
Definition proptest.cpp:25
double fgGetDouble(const char *name, double defaultValue)
Get a double value for a property.
Definition proptest.cpp:30