Debian Codegraph @

src/FDM/flight.cxx

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