Debian Codegraph @

src/FDM/JSBSim/initialization/FGInitialCondition.cpp

raw symbols SCIP
1 /*******************************************************************************
2
3 Header: FGInitialCondition.cpp
4 Author: Tony Peden, Bertrand Coconnier
5 Date started: 7/1/99
6
7 --------- Copyright (C) 1999 Anthony K. Peden ([email protected]) ---------
8
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 2 of the License, or (at your option) any
12 later version.
13
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17 details.
18
19 You should have received a copy of the GNU Lesser General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc., 59
21 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23 Further information about the GNU Lesser General Public License can also be
24 found on the world wide web at http://www.gnu.org.
25
26
27 HISTORY
28 --------------------------------------------------------------------------------
29 7/1/99 TP Created
30 11/25/10 BC Complete revision - Use minimal set of variables to prevent
31 inconsistent states. Wind is correctly handled.
32
33
34 FUNCTIONAL DESCRIPTION
35 --------------------------------------------------------------------------------
36
37 The purpose of this class is to take a set of initial conditions and provide a
38 kinematically consistent set of body axis velocity components, euler angles, and
39 altitude. This class does not attempt to trim the model i.e. the sim will most
40 likely start in a very dynamic state (unless, of course, you have chosen your
41 IC's wisely) even after setting it up with this class.
42
43 ********************************************************************************
44 INCLUDES
45 *******************************************************************************/
46
47 #include "FGInitialCondition.h"
48 #include "models/FGInertial.h"
49 #include "models/FGAtmosphere.h"
50 #include "models/FGAccelerations.h"
51 #include "models/FGAuxiliary.h"
52 #include "input_output/FGXMLFileRead.h"
53 #include "FGTrim.h"
54
55 using namespace std;
56
57 namespace JSBSim {
58
59 //******************************************************************************
60
61 FGInitialCondition::FGInitialCondition(FGFDMExec *FDMExec) : fdmex(FDMExec)
62 {
63 InitializeIC();
64
65 if(FDMExec) {
66 Aircraft=fdmex->GetAircraft();
67 Auxiliary=fdmex->GetAuxiliary();
68 } else {
69 cout << "FGInitialCondition: This class requires a pointer to a valid FGFDMExec object" << endl;
70 }
71
72 Debug(0);
73 }
74
75 //******************************************************************************
76
77 FGInitialCondition::~FGInitialCondition()
78 {
79 Debug(1);
80 }
81
82 //******************************************************************************
83
84 void FGInitialCondition::ResetIC(double u0, double v0, double w0,
85 double p0, double q0, double r0,
86 double alpha0, double beta0,
87 double phi0, double theta0, double psi0,
88 double latRad0, double lonRad0, double altAGLFt0,
89 double gamma0)
90 {
91 double calpha = cos(alpha0), cbeta = cos(beta0);
92 double salpha = sin(alpha0), sbeta = sin(beta0);
93
94 InitializeIC();
95
96 vPQR_body = {p0, q0, r0};
97 alpha = alpha0; beta = beta0;
98
99 position.SetLongitude(lonRad0);
100 position.SetLatitude(latRad0);
101 fdmex->GetInertial()->SetAltitudeAGL(position, altAGLFt0);
102 lastLatitudeSet = setgeoc;
103 lastAltitudeSet = setagl;
104
105 orientation = FGQuaternion(phi0, theta0, psi0);
106 const FGMatrix33& Tb2l = orientation.GetTInv();
107
108 vUVW_NED = Tb2l * FGColumnVector3(u0, v0, w0);
109 vt = vUVW_NED.Magnitude();
110 lastSpeedSet = setuvw;
111
112 Tw2b = { calpha*cbeta, -calpha*sbeta, -salpha,
113 sbeta, cbeta, 0.0,
114 salpha*cbeta, -salpha*sbeta, calpha };
115 Tb2w = Tw2b.Transposed();
116
117 SetFlightPathAngleRadIC(gamma0);
118 }
119
120 //******************************************************************************
121
122 void FGInitialCondition::InitializeIC(void)
123 {
124 alpha = beta = 0.0;
125 epa = 0.0;
126
127 double a = fdmex->GetInertial()->GetSemimajor();
128 double b = fdmex->GetInertial()->GetSemiminor();
129
130 position.SetEllipse(a, b);
131
132 position.SetPositionGeodetic(0.0, 0.0, 0.0);
133
134 orientation = FGQuaternion(0.0, 0.0, 0.0);
135 vUVW_NED.InitMatrix();
136 vPQR_body.InitMatrix();
137 vt=0;
138
139 targetNlfIC = 1.0;
140
141 Tw2b = { 1., 0., 0., 0., 1., 0., 0., 0., 1. };
142 Tb2w = { 1., 0., 0., 0., 1., 0., 0., 0., 1. };
143
144 lastSpeedSet = setvt;
145 lastAltitudeSet = setasl;
146 lastLatitudeSet = setgeoc;
147 enginesRunning = 0;
148 trimRequested = TrimMode::tNone;
149 }
150
151 //******************************************************************************
152
153 void FGInitialCondition::SetVequivalentKtsIC(double ve)
154 {
155 const auto Atmosphere = fdmex->GetAtmosphere();
156 double altitudeASL = GetAltitudeASLFtIC();
157 double rho = Atmosphere->GetDensity(altitudeASL);
158 SetVtrueFpsIC(ve*ktstofps*sqrt(FGAtmosphere::StdDaySLdensity/rho));
159 lastSpeedSet = setve;
160 }
161
162 //******************************************************************************
163
164 void FGInitialCondition::SetMachIC(double mach)
165 {
166 const auto Atmosphere = fdmex->GetAtmosphere();
167 double altitudeASL = GetAltitudeASLFtIC();
168 double soundSpeed = Atmosphere->GetSoundSpeed(altitudeASL);
169 SetVtrueFpsIC(mach*soundSpeed);
170 lastSpeedSet = setmach;
171 }
172
173 //******************************************************************************
174
175 void FGInitialCondition::SetVcalibratedKtsIC(double vcas)
176 {
177 const auto Atmosphere = fdmex->GetAtmosphere();
178 double altitudeASL = GetAltitudeASLFtIC();
179 double pressure = Atmosphere->GetPressure(altitudeASL);
180 double mach = Auxiliary->MachFromVcalibrated(fabs(vcas)*ktstofps, pressure);
181 double soundSpeed = Atmosphere->GetSoundSpeed(altitudeASL);
182
183 SetVtrueFpsIC(mach * soundSpeed);
184 lastSpeedSet = setvc;
185 }
186
187 //******************************************************************************
188 // Updates alpha and beta according to the aircraft true airspeed in the local
189 // NED frame.
190
191 void FGInitialCondition::calcAeroAngles(const FGColumnVector3& _vt_NED)
192 {
193 const FGMatrix33& Tl2b = orientation.GetT();
194 FGColumnVector3 _vt_BODY = Tl2b * _vt_NED;
195 double ua = _vt_BODY(eX);
196 double va = _vt_BODY(eY);
197 double wa = _vt_BODY(eZ);
198 double uwa = sqrt(ua*ua + wa*wa);
199 double calpha, cbeta;
200 double salpha, sbeta;
201
202 alpha = beta = 0.0;
203 calpha = cbeta = 1.0;
204 salpha = sbeta = 0.0;
205
206 if( wa != 0 )
207 alpha = atan2( wa, ua );
208
209 // alpha cannot be constrained without updating other informations like the
210 // true speed or the Euler angles. Otherwise we might end up with an
211 // inconsistent state of the aircraft.
212 /*alpha = Constrain(fdmex->GetAerodynamics()->GetAlphaCLMin(), alpha,
213 fdmex->GetAerodynamics()->GetAlphaCLMax());*/
214
215 if( va != 0 )
216 beta = atan2( va, uwa );
217
218 if (uwa != 0) {
219 calpha = ua / uwa;
220 salpha = wa / uwa;
221 }
222
223 if (vt != 0) {
224 cbeta = uwa / vt;
225 sbeta = va / vt;
226 }
227
228 Tw2b = { calpha*cbeta, -calpha*sbeta, -salpha,
229 sbeta, cbeta, 0.0,
230 salpha*cbeta, -salpha*sbeta, calpha };
231 Tb2w = Tw2b.Transposed();
232 }
233
234 //******************************************************************************
235 // Set the ground velocity. Caution it sets the vertical velocity to zero to
236 // keep backward compatibility.
237
238 void FGInitialCondition::SetVgroundFpsIC(double vg)
239 {
240 const FGMatrix33& Tb2l = orientation.GetTInv();
241 FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
242 FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
243
244 vUVW_NED(eU) = vg * orientation.GetCosEuler(ePsi);
245 vUVW_NED(eV) = vg * orientation.GetSinEuler(ePsi);
246 vUVW_NED(eW) = 0.;
247 _vt_NED = vUVW_NED + _vWIND_NED;
248 vt = _vt_NED.Magnitude();
249
250 calcAeroAngles(_vt_NED);
251
252 lastSpeedSet = setvg;
253 }
254
255 //******************************************************************************
256 // Sets the true airspeed. The amplitude of the airspeed is modified but its
257 // direction is kept unchanged. If there is no wind, the same is true for the
258 // ground velocity. If there is some wind, the airspeed direction is unchanged
259 // but this may result in the ground velocity direction being altered. This is
260 // for backward compatibility.
261
262 void FGInitialCondition::SetVtrueFpsIC(double vtrue)
263 {
264 const FGMatrix33& Tb2l = orientation.GetTInv();
265 FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
266 FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
267
268 if (vt > 0.1)
269 _vt_NED *= vtrue / vt;
270 else
271 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vtrue, 0., 0.);
272
273 vt = vtrue;
274 vUVW_NED = _vt_NED - _vWIND_NED;
275
276 calcAeroAngles(_vt_NED);
277
278 lastSpeedSet = setvt;
279 }
280
281 //******************************************************************************
282 // When the climb rate is modified, we need to update the angles theta and beta
283 // to keep the true airspeed amplitude, the AoA and the heading unchanged.
284 // Beta will be modified if the aircraft roll angle is not null.
285
286 void FGInitialCondition::SetClimbRateFpsIC(double hdot)
287 {
288 if (fabs(hdot) > vt) {
289 cerr << "The climb rate cannot be higher than the true speed." << endl;
290 return;
291 }
292
293 const FGMatrix33& Tb2l = orientation.GetTInv();
294 FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
295 FGColumnVector3 _WIND_NED = _vt_NED - vUVW_NED;
296 double hdot0 = -_vt_NED(eW);
297
298 if (fabs(hdot0) < vt) { // Is this check really needed ?
299 double scale = sqrt((vt*vt-hdot*hdot)/(vt*vt-hdot0*hdot0));
300 _vt_NED(eU) *= scale;
301 _vt_NED(eV) *= scale;
302 }
303 _vt_NED(eW) = -hdot;
304 vUVW_NED = _vt_NED - _WIND_NED;
305
306 // Updating the angles theta and beta to keep the true airspeed amplitude
307 calcThetaBeta(alpha, _vt_NED);
308 }
309
310 //******************************************************************************
311 // When the AoA is modified, we need to update the angles theta and beta to
312 // keep the true airspeed amplitude, the climb rate and the heading unchanged.
313 // Beta will be modified if the aircraft roll angle is not null.
314
315 void FGInitialCondition::SetAlphaRadIC(double alfa)
316 {
317 const FGMatrix33& Tb2l = orientation.GetTInv();
318 FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
319 calcThetaBeta(alfa, _vt_NED);
320 }
321
322 //******************************************************************************
323 // When the AoA is modified, we need to update the angles theta and beta to
324 // keep the true airspeed amplitude, the climb rate and the heading unchanged.
325 // Beta will be modified if the aircraft roll angle is not null.
326
327 void FGInitialCondition::calcThetaBeta(double alfa, const FGColumnVector3& _vt_NED)
328 {
329 FGColumnVector3 vOrient = orientation.GetEuler();
330 double calpha = cos(alfa), salpha = sin(alfa);
331 double cpsi = orientation.GetCosEuler(ePsi), spsi = orientation.GetSinEuler(ePsi);
332 double cphi = orientation.GetCosEuler(ePhi), sphi = orientation.GetSinEuler(ePhi);
333 FGMatrix33 Tpsi( cpsi, spsi, 0.,
334 -spsi, cpsi, 0.,
335 0., 0., 1.);
336 FGMatrix33 Tphi(1., 0., 0.,
337 0., cphi, sphi,
338 0.,-sphi, cphi);
339 FGMatrix33 Talpha( calpha, 0., salpha,
340 0., 1., 0.,
341 -salpha, 0., calpha);
342
343 FGColumnVector3 v0 = Tpsi * _vt_NED;
344 FGColumnVector3 n = (Talpha * Tphi).Transposed() * FGColumnVector3(0., 0., 1.);
345 FGColumnVector3 y = {0., 1., 0.};
346 FGColumnVector3 u = y - DotProduct(y, n) * n;
347 FGColumnVector3 p = y * n;
348
349 if (DotProduct(p, v0) < 0) p *= -1.0;
350 p.Normalize();
351
352 u *= DotProduct(v0, y) / DotProduct(u, y);
353
354 // There are situations where the desired alpha angle cannot be obtained. This
355 // is not a limitation of the algorithm but is due to the mathematical problem
356 // not having a solution. This can only be cured by limiting the alpha angle
357 // or by modifying an additional angle (psi ?). Since this is anticipated to
358 // be a pathological case (mainly when a high roll angle is required) this
359 // situation is not addressed below. However if there are complaints about the
360 // following error being raised too often, we might need to reconsider this
361 // position.
362 if (DotProduct(v0, v0) < DotProduct(u, u)) {
363 cerr << "Cannot modify angle 'alpha' from " << alpha << " to " << alfa << endl;
364 return;
365 }
366
367 FGColumnVector3 v1 = u + sqrt(DotProduct(v0, v0) - DotProduct(u, u))*p;
368
369 FGColumnVector3 v0xz(v0(eU), 0., v0(eW));
370 FGColumnVector3 v1xz(v1(eU), 0., v1(eW));
371 v0xz.Normalize();
372 v1xz.Normalize();
373 double sinTheta = (v1xz * v0xz)(eY);
374 vOrient(eTht) = asin(sinTheta);
375
376 orientation = FGQuaternion(vOrient);
377
378 const FGMatrix33& Tl2b = orientation.GetT();
379 FGColumnVector3 v2 = Talpha * Tl2b * _vt_NED;
380
381 alpha = alfa;
382 beta = atan2(v2(eV), v2(eU));
383 double cbeta=1.0, sbeta=0.0;
384 if (vt != 0.0) {
385 cbeta = v2(eU) / vt;
386 sbeta = v2(eV) / vt;
387 }
388 Tw2b = { calpha*cbeta, -calpha*sbeta, -salpha,
389 sbeta, cbeta, 0.0,
390 salpha*cbeta, -salpha*sbeta, calpha };
391 Tb2w = Tw2b.Transposed();
392 }
393
394 //******************************************************************************
395 // When the beta angle is modified, we need to update the angles theta and psi
396 // to keep the true airspeed (amplitude and direction - including the climb rate)
397 // and the alpha angle unchanged. This may result in the aircraft heading (psi)
398 // being altered especially if there is cross wind.
399
400 void FGInitialCondition::SetBetaRadIC(double bta)
401 {
402 const FGMatrix33& Tb2l = orientation.GetTInv();
403 FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
404 FGColumnVector3 vOrient = orientation.GetEuler();
405
406 beta = bta;
407 double calpha = cos(alpha), salpha = sin(alpha);
408 double cbeta = cos(beta), sbeta = sin(beta);
409 double cphi = orientation.GetCosEuler(ePhi), sphi = orientation.GetSinEuler(ePhi);
410 FGMatrix33 TphiInv(1., 0., 0.,
411 0., cphi,-sphi,
412 0., sphi, cphi);
413
414 Tw2b = { calpha*cbeta, -calpha*sbeta, -salpha,
415 sbeta, cbeta, 0.0,
416 salpha*cbeta, -salpha*sbeta, calpha };
417 Tb2w = Tw2b.Transposed();
418
419 FGColumnVector3 vf = TphiInv * Tw2b * FGColumnVector3(vt, 0., 0.);
420 FGColumnVector3 v0xy(_vt_NED(eX), _vt_NED(eY), 0.);
421 FGColumnVector3 v1xy(sqrt(v0xy(eX)*v0xy(eX)+v0xy(eY)*v0xy(eY)-vf(eY)*vf(eY)),vf(eY),0.);
422 v0xy.Normalize();
423 v1xy.Normalize();
424
425 if (vf(eX) < 0.) v0xy(eX) *= -1.0;
426
427 double sinPsi = (v1xy * v0xy)(eZ);
428 double cosPsi = DotProduct(v0xy, v1xy);
429 vOrient(ePsi) = atan2(sinPsi, cosPsi);
430 FGMatrix33 Tpsi( cosPsi, sinPsi, 0.,
431 -sinPsi, cosPsi, 0.,
432 0., 0., 1.);
433
434 FGColumnVector3 v2xz = Tpsi * _vt_NED;
435 FGColumnVector3 vfxz = vf;
436 v2xz(eV) = vfxz(eV) = 0.0;
437 v2xz.Normalize();
438 vfxz.Normalize();
439 double sinTheta = (v2xz * vfxz)(eY);
440 vOrient(eTht) = -asin(sinTheta);
441
442 orientation = FGQuaternion(vOrient);
443 }
444
445 //******************************************************************************
446 // Modifies the body frame orientation.
447
448 void FGInitialCondition::SetEulerAngleRadIC(int idx, double angle)
449 {
450 const FGMatrix33& Tb2l = orientation.GetTInv();
451 const FGMatrix33& Tl2b = orientation.GetT();
452 FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
453 FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
454 FGColumnVector3 _vUVW_BODY = Tl2b * vUVW_NED;
455 FGColumnVector3 vOrient = orientation.GetEuler();
456
457 vOrient(idx) = angle;
458 orientation = FGQuaternion(vOrient);
459
460 if ((lastSpeedSet != setned) && (lastSpeedSet != setvg)) {
461 const FGMatrix33& newTb2l = orientation.GetTInv();
462 vUVW_NED = newTb2l * _vUVW_BODY;
463 _vt_NED = vUVW_NED + _vWIND_NED;
464 vt = _vt_NED.Magnitude();
465 }
466
467 calcAeroAngles(_vt_NED);
468 }
469
470 //******************************************************************************
471 // Modifies an aircraft velocity component (eU, eV or eW) in the body frame. The
472 // true airspeed is modified accordingly. If there is some wind, the airspeed
473 // direction modification may differ from the body velocity modification.
474
475 void FGInitialCondition::SetBodyVelFpsIC(int idx, double vel)
476 {
477 const FGMatrix33& Tb2l = orientation.GetTInv();
478 const FGMatrix33& Tl2b = orientation.GetT();
479 FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
480 FGColumnVector3 _vUVW_BODY = Tl2b * vUVW_NED;
481 FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
482
483 _vUVW_BODY(idx) = vel;
484 vUVW_NED = Tb2l * _vUVW_BODY;
485 _vt_NED = vUVW_NED + _vWIND_NED;
486 vt = _vt_NED.Magnitude();
487
488 calcAeroAngles(_vt_NED);
489
490 lastSpeedSet = setuvw;
491 }
492
493 //******************************************************************************
494 // Modifies an aircraft velocity component (eX, eY or eZ) in the local NED frame.
495 // The true airspeed is modified accordingly. If there is some wind, the airspeed
496 // direction modification may differ from the local velocity modification.
497
498 void FGInitialCondition::SetNEDVelFpsIC(int idx, double vel)
499 {
500 const FGMatrix33& Tb2l = orientation.GetTInv();
501 FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
502 FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
503
504 vUVW_NED(idx) = vel;
505 _vt_NED = vUVW_NED + _vWIND_NED;
506 vt = _vt_NED.Magnitude();
507
508 calcAeroAngles(_vt_NED);
509
510 lastSpeedSet = setned;
511 }
512
513 //******************************************************************************
514 // Set wind amplitude and direction in the local NED frame. The aircraft velocity
515 // with respect to the ground is not changed but the true airspeed is.
516
517 void FGInitialCondition::SetWindNEDFpsIC(double wN, double wE, double wD )
518 {
519 FGColumnVector3 _vt_NED = vUVW_NED + FGColumnVector3(wN, wE, wD);
520 vt = _vt_NED.Magnitude();
521
522 calcAeroAngles(_vt_NED);
523 }
524
525 //******************************************************************************
526 // Set the cross wind velocity (in knots). Here, 'cross wind' means perpendicular
527 // to the aircraft heading and parallel to the ground. The aircraft velocity
528 // with respect to the ground is not changed but the true airspeed is.
529
530 void FGInitialCondition::SetCrossWindKtsIC(double cross)
531 {
532 const FGMatrix33& Tb2l = orientation.GetTInv();
533 FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
534 FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
535 FGColumnVector3 _vCROSS(-orientation.GetSinEuler(ePsi), orientation.GetCosEuler(ePsi), 0.);
536
537 // Gram-Schmidt process is used to remove the existing cross wind component
538 _vWIND_NED -= DotProduct(_vWIND_NED, _vCROSS) * _vCROSS;
539 // Which is now replaced by the new value. The input cross wind is expected
540 // in knots, so first convert to fps, which is the internal unit used.
541 _vWIND_NED += (cross * ktstofps) * _vCROSS;
542 _vt_NED = vUVW_NED + _vWIND_NED;
543 vt = _vt_NED.Magnitude();
544
545 calcAeroAngles(_vt_NED);
546 }
547
548 //******************************************************************************
549 // Set the head wind velocity (in knots). Here, 'head wind' means parallel
550 // to the aircraft heading and to the ground. The aircraft velocity
551 // with respect to the ground is not changed but the true airspeed is.
552
553 void FGInitialCondition::SetHeadWindKtsIC(double head)
554 {
555 const FGMatrix33& Tb2l = orientation.GetTInv();
556 FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
557 FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
558 // This is a head wind, so the direction vector for the wind
559 // needs to be set opposite to the heading the aircraft
560 // is taking. So, the cos and sin of the heading (psi)
561 // are negated in the line below.
562 FGColumnVector3 _vHEAD(-orientation.GetCosEuler(ePsi), -orientation.GetSinEuler(ePsi), 0.);
563
564 // Gram-Schmidt process is used to remove the existing head wind component
565 _vWIND_NED -= DotProduct(_vWIND_NED, _vHEAD) * _vHEAD;
566 // Which is now replaced by the new value. The input head wind is expected
567 // in knots, so first convert to fps, which is the internal unit used.
568 _vWIND_NED += (head * ktstofps) * _vHEAD;
569 _vt_NED = vUVW_NED + _vWIND_NED;
570
571 vt = _vt_NED.Magnitude();
572
573 calcAeroAngles(_vt_NED);
574 }
575
576 //******************************************************************************
577 // Set the vertical wind velocity (in knots). The 'vertical' is taken in the
578 // local NED frame. The aircraft velocity with respect to the ground is not
579 // changed but the true airspeed is.
580
581 void FGInitialCondition::SetWindDownKtsIC(double wD)
582 {
583 const FGMatrix33& Tb2l = orientation.GetTInv();
584 FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
585
586 _vt_NED(eW) = vUVW_NED(eW) + wD * ktstofps;
587 vt = _vt_NED.Magnitude();
588
589 calcAeroAngles(_vt_NED);
590 }
591
592 //******************************************************************************
593 // Modifies the wind velocity (in knots) while keeping its direction unchanged.
594 // The vertical component (in local NED frame) is unmodified. The aircraft
595 // velocity with respect to the ground is not changed but the true airspeed is.
596
597 void FGInitialCondition::SetWindMagKtsIC(double mag)
598 {
599 const FGMatrix33& Tb2l = orientation.GetTInv();
600 FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
601 FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
602 FGColumnVector3 _vHEAD(_vWIND_NED(eU), _vWIND_NED(eV), 0.);
603 double windMag = _vHEAD.Magnitude();
604
605 if (windMag > 0.001)
606 _vHEAD *= (mag*ktstofps) / windMag;
607 else
608 _vHEAD = {mag*ktstofps, 0., 0.};
609
610 _vWIND_NED(eU) = _vHEAD(eU);
611 _vWIND_NED(eV) = _vHEAD(eV);
612 _vt_NED = vUVW_NED + _vWIND_NED;
613 vt = _vt_NED.Magnitude();
614
615 calcAeroAngles(_vt_NED);
616 }
617
618 //******************************************************************************
619 // Modifies the wind direction while keeping its velocity unchanged. The vertical
620 // component (in local NED frame) is unmodified. The aircraft velocity with
621 // respect to the ground is not changed but the true airspeed is.
622
623 void FGInitialCondition::SetWindDirDegIC(double dir)
624 {
625 const FGMatrix33& Tb2l = orientation.GetTInv();
626 FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
627 FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
628 double mag = _vWIND_NED.Magnitude(eU, eV);
629 FGColumnVector3 _vHEAD(mag*cos(dir*degtorad), mag*sin(dir*degtorad), 0.);
630
631 _vWIND_NED(eU) = _vHEAD(eU);
632 _vWIND_NED(eV) = _vHEAD(eV);
633 _vt_NED = vUVW_NED + _vWIND_NED;
634 vt = _vt_NED.Magnitude();
635
636 calcAeroAngles(_vt_NED);
637 }
638
639 //******************************************************************************
640
641 void FGInitialCondition::SetTerrainElevationFtIC(double elev)
642 {
643 double agl = GetAltitudeAGLFtIC();
644 fdmex->GetInertial()->SetTerrainElevation(elev);
645
646 if (lastAltitudeSet == setagl)
647 SetAltitudeAGLFtIC(agl);
648 }
649
650 //******************************************************************************
651
652 double FGInitialCondition::GetAltitudeASLFtIC(void) const
653 {
654 return position.GetRadius() - position.GetSeaLevelRadius();
655 }
656
657 //******************************************************************************
658
659 double FGInitialCondition::GetAltitudeAGLFtIC(void) const
660 {
661 return fdmex->GetInertial()->GetAltitudeAGL(position);
662 }
663
664 //******************************************************************************
665
666 double FGInitialCondition::GetTerrainElevationFtIC(void) const
667 {
668 FGColumnVector3 normal, v, w;
669 FGLocation contact;
670 double a = fdmex->GetInertial()->GetSemimajor();
671 double b = fdmex->GetInertial()->GetSemiminor();
672 contact.SetEllipse(a, b);
673 fdmex->GetInertial()->GetContactPoint(position, contact, normal, v, w);
674 return contact.GetGeodAltitude();
675 }
676
677 //******************************************************************************
678
679 void FGInitialCondition::SetAltitudeAGLFtIC(double agl)
680 {
681 const auto Atmosphere = fdmex->GetAtmosphere();
682 double altitudeASL = GetAltitudeASLFtIC();
683 double pressure = Atmosphere->GetPressure(altitudeASL);
684 double soundSpeed = Atmosphere->GetSoundSpeed(altitudeASL);
685 double rho = Atmosphere->GetDensity(altitudeASL);
686
687 double mach0 = vt / soundSpeed;
688 double vc0 = Auxiliary->VcalibratedFromMach(mach0, pressure);
689 double ve0 = vt * sqrt(rho/FGAtmosphere::StdDaySLdensity);
690
691 switch(lastLatitudeSet) {
692 case setgeod:
693 fdmex->GetInertial()->SetAltitudeAGL(position, agl);
694 break;
695 case setgeoc:
696 {
697 double a = fdmex->GetInertial()->GetSemimajor();
698 double b = fdmex->GetInertial()->GetSemiminor();
699 double e2 = 1.0-b*b/(a*a);
700 double tanlat = tan(position.GetLatitude());
701 double n = e2;
702 double prev_n = 1.0;
703 int iter = 0;
704 double longitude = position.GetLongitude();
705 double alt = position.GetGeodAltitude();
706 double h = -2.0*max(a,b);
707 double geodLat;
708 while ((fabs(n-prev_n) > 1E-15 || fabs(h-agl) > 1E-10) && iter < 10) {
709 geodLat = atan(tanlat/(1-n));
710 position.SetPositionGeodetic(longitude, geodLat, alt);
711 h = GetAltitudeAGLFtIC();
712 alt += agl-h;
713 double sinGeodLat = sin(geodLat);
714 double N = a/sqrt(1-e2*sinGeodLat*sinGeodLat);
715 prev_n = n;
716 n = e2*N/(N+alt);
717 iter++;
718 }
719 }
720 break;
721 }
722
723 altitudeASL = GetAltitudeASLFtIC();
724 soundSpeed = Atmosphere->GetSoundSpeed(altitudeASL);
725 rho = Atmosphere->GetDensity(altitudeASL);
726 pressure = Atmosphere->GetPressure(altitudeASL);
727
728 switch(lastSpeedSet) {
729 case setvc:
730 mach0 = Auxiliary->MachFromVcalibrated(vc0, pressure);
731 SetVtrueFpsIC(mach0 * soundSpeed);
732 break;
733 case setmach:
734 SetVtrueFpsIC(mach0 * soundSpeed);
735 break;
736 case setve:
737 SetVtrueFpsIC(ve0 * sqrt(FGAtmosphere::StdDaySLdensity/rho));
738 break;
739 default: // Make the compiler stop complaining about missing enums
740 break;
741 }
742
743 lastAltitudeSet = setagl;
744 }
745
746 //******************************************************************************
747 // Set the altitude SL. If the airspeed has been previously set with parameters
748 // that are atmosphere dependent (Mach, VCAS, VEAS) then the true airspeed is
749 // modified to keep the last set speed to its previous value.
750
751 void FGInitialCondition::SetAltitudeASLFtIC(double alt)
752 {
753 const auto Atmosphere = fdmex->GetAtmosphere();
754 double altitudeASL = GetAltitudeASLFtIC();
755 double pressure = Atmosphere->GetPressure(altitudeASL);
756 double soundSpeed = Atmosphere->GetSoundSpeed(altitudeASL);
757 double rho = Atmosphere->GetDensity(altitudeASL);
758
759 double mach0 = vt / soundSpeed;
760 double vc0 = Auxiliary->VcalibratedFromMach(mach0, pressure);
761 double ve0 = vt * sqrt(rho/FGAtmosphere::StdDaySLdensity);
762
763 switch(lastLatitudeSet) {
764 case setgeod:
765 {
766 // Given an altitude above the mean sea level (or a position radius which
767 // is the same) and a geodetic latitude, compute the geodetic altitude.
768 double a = fdmex->GetInertial()->GetSemimajor();
769 double b = fdmex->GetInertial()->GetSemiminor();
770 double e2 = 1.0-b*b/(a*a);
771 double geodLatitude = position.GetGeodLatitudeRad();
772 double cosGeodLat = cos(geodLatitude);
773 double sinGeodLat = sin(geodLatitude);
774 double N = a/sqrt(1-e2*sinGeodLat*sinGeodLat);
775 double geodAlt = 0.0;
776 double n = e2;
777 double prev_n = 1.0;
778 int iter = 0;
779 // Use tan or cotan to solve the geodetic altitude to avoid floating point
780 // exceptions.
781 if (cosGeodLat > fabs(sinGeodLat)) { // tan() can safely be used.
782 double tanGeodLat = sinGeodLat/cosGeodLat;
783 double x0 = N*e2*cosGeodLat;
784 double x = 0.0;
785 while (fabs(n-prev_n) > 1E-15 && iter < 10) {
786 double tanLat = (1-n)*tanGeodLat; // See Stevens & Lewis 1.6-14
787 double cos2Lat = 1./(1.+tanLat*tanLat);
788 double slr = b/sqrt(1.-e2*cos2Lat);
789 double R = slr + alt;
790 x = R*sqrt(cos2Lat); // OK, cos(latitude) is always positive.
791 prev_n = n;
792 n = x0/x;
793 iter++;
794 }
795 geodAlt = x/cosGeodLat-N;
796 }
797 else { // better use cotan (i.e. 1./tan())
798 double cotanGeodLat = cosGeodLat/sinGeodLat;
799 double z0 = N*e2*sinGeodLat;
800 double z = 0.0;
801 while (fabs(n-prev_n) > 1E-15 && iter < 10) {
802 double cotanLat = cotanGeodLat/(1-n);
803 double sin2Lat = 1./(1.+cotanLat*cotanLat);
804 double cos2Lat = 1.-sin2Lat;
805 double slr = b/sqrt(1.-e2*cos2Lat);
806 double R = slr + alt;
807 z = R*sign(cotanLat)*sqrt(sin2Lat);
808 prev_n = n;
809 n = z0/(z0+z);
810 iter++;
811 }
812 geodAlt = z/sinGeodLat-N*(1-e2);
813 }
814
815 double longitude = position.GetLongitude();
816 position.SetPositionGeodetic(longitude, geodLatitude, geodAlt);
817 }
818 break;
819 case setgeoc:
820 {
821 double slr = position.GetSeaLevelRadius();
822 position.SetRadius(slr+alt);
823 }
824 break;
825 }
826
827 altitudeASL = position.GetGeodAltitude();
828 soundSpeed = Atmosphere->GetSoundSpeed(altitudeASL);
829 rho = Atmosphere->GetDensity(altitudeASL);
830 pressure = Atmosphere->GetPressure(altitudeASL);
831
832 switch(lastSpeedSet) {
833 case setvc:
834 mach0 = Auxiliary->MachFromVcalibrated(vc0, pressure);
835 SetVtrueFpsIC(mach0 * soundSpeed);
836 break;
837 case setmach:
838 SetVtrueFpsIC(mach0 * soundSpeed);
839 break;
840 case setve:
841 SetVtrueFpsIC(ve0 * sqrt(FGAtmosphere::StdDaySLdensity/rho));
842 break;
843 default: // Make the compiler stop complaining about missing enums
844 break;
845 }
846
847 lastAltitudeSet = setasl;
848 }
849
850 //******************************************************************************
851
852 void FGInitialCondition::SetGeodLatitudeRadIC(double geodLatitude)
853 {
854 double lon = position.GetLongitude();
855 lastLatitudeSet = setgeod;
856
857 switch (lastAltitudeSet)
858 {
859 case setagl:
860 {
861 double agl = GetAltitudeAGLFtIC();
862 position.SetPositionGeodetic(lon, geodLatitude, 0.);
863 fdmex->GetInertial()->SetAltitudeAGL(position, agl);
864 }
865 break;
866 case setasl:
867 {
868 double asl = GetAltitudeASLFtIC();
869 position.SetPositionGeodetic(lon, geodLatitude, 0.);
870 SetAltitudeASLFtIC(asl);
871 }
872 break;
873 }
874 }
875
876 //******************************************************************************
877
878 void FGInitialCondition::SetLatitudeRadIC(double lat)
879 {
880 double altitude;
881
882 lastLatitudeSet = setgeoc;
883
884 switch(lastAltitudeSet) {
885 case setagl:
886 altitude = GetAltitudeAGLFtIC();
887 position.SetLatitude(lat);
888 SetAltitudeAGLFtIC(altitude);
889 break;
890 default:
891 altitude = GetAltitudeASLFtIC();
892 position.SetLatitude(lat);
893 SetAltitudeASLFtIC(altitude);
894 break;
895 }
896 }
897
898 //******************************************************************************
899
900 void FGInitialCondition::SetLongitudeRadIC(double lon)
901 {
902 double altitude;
903
904 switch(lastAltitudeSet) {
905 case setagl:
906 altitude = GetAltitudeAGLFtIC();
907 position.SetLongitude(lon);
908 SetAltitudeAGLFtIC(altitude);
909 break;
910 default:
911 position.SetLongitude(lon);
912 break;
913 }
914 }
915
916 //******************************************************************************
917
918 double FGInitialCondition::GetWindDirDegIC(void) const
919 {
920 const FGMatrix33& Tb2l = orientation.GetTInv();
921 FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
922 FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
923
924 return _vWIND_NED.Magnitude(eU, eV) == 0.0 ? 0.0
925 : atan2(_vWIND_NED(eV), _vWIND_NED(eU))*radtodeg;
926 }
927
928 //******************************************************************************
929
930 FGColumnVector3 FGInitialCondition::GetWindNEDFpsIC(void) const {
931 const FGMatrix33& Tb2l = orientation.GetTInv();
932 FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
933 return _vt_NED - vUVW_NED;
934 }
935
936 //******************************************************************************
937
938 double FGInitialCondition::GetWindFpsIC(void) const
939 {
940 const FGMatrix33& Tb2l = orientation.GetTInv();
941 FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
942 FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
943
944 return _vWIND_NED.Magnitude(eU, eV);
945 }
946
947 //******************************************************************************
948
949 double FGInitialCondition::GetBodyWindFpsIC(int idx) const
950 {
951 const FGMatrix33& Tl2b = orientation.GetT();
952 FGColumnVector3 _vt_BODY = Tw2b * FGColumnVector3(vt, 0., 0.);
953 FGColumnVector3 _vUVW_BODY = Tl2b * vUVW_NED;
954 FGColumnVector3 _vWIND_BODY = _vt_BODY - _vUVW_BODY;
955
956 return _vWIND_BODY(idx);
957 }
958
959 //******************************************************************************
960
961 double FGInitialCondition::GetVcalibratedKtsIC(void) const
962 {
963 const auto Atmosphere = fdmex->GetAtmosphere();
964 double altitudeASL = GetAltitudeASLFtIC();
965 double pressure = Atmosphere->GetPressure(altitudeASL);
966 double soundSpeed = Atmosphere->GetSoundSpeed(altitudeASL);
967 double mach = vt / soundSpeed;
968
969 return fpstokts * Auxiliary->VcalibratedFromMach(mach, pressure);
970 }
971
972 //******************************************************************************
973
974 double FGInitialCondition::GetVequivalentKtsIC(void) const
975 {
976 const auto Atmosphere = fdmex->GetAtmosphere();
977 double altitudeASL = GetAltitudeASLFtIC();
978 double rho = Atmosphere->GetDensity(altitudeASL);
979 return fpstokts * vt * sqrt(rho/FGAtmosphere::StdDaySLdensity);
980 }
981
982 //******************************************************************************
983
984 double FGInitialCondition::GetMachIC(void) const
985 {
986 const auto Atmosphere = fdmex->GetAtmosphere();
987 double altitudeASL = GetAltitudeASLFtIC();
988 double soundSpeed = Atmosphere->GetSoundSpeed(altitudeASL);
989 return vt / soundSpeed;
990 }
991
992 //******************************************************************************
993
994 double FGInitialCondition::GetBodyVelFpsIC(int idx) const
995 {
996 const FGMatrix33& Tl2b = orientation.GetT();
997 FGColumnVector3 _vUVW_BODY = Tl2b * vUVW_NED;
998
999 return _vUVW_BODY(idx);
1000 }
1001
1002 //******************************************************************************
1003
1004 bool FGInitialCondition::Load(const SGPath& rstfile, bool useAircraftPath)
1005 {
1006 SGPath init_file_name;
1007 if(useAircraftPath && rstfile.isRelative()) {
1008 init_file_name = fdmex->GetFullAircraftPath()/rstfile.utf8Str();
1009 } else {
1010 init_file_name = rstfile;
1011 }
1012
1013 FGXMLFileRead XMLFileRead;
1014 Element* document = XMLFileRead.LoadXMLDocument(init_file_name);
1015
1016 // Make sure that the document is valid
1017 if (!document) {
1018 stringstream s;
1019 s << "File: " << init_file_name << " could not be read.";
1020 cerr << s.str() << endl;
1021 throw BaseException(s.str());
1022 }
1023
1024 if (document->GetName() != "initialize") {
1025 stringstream s;
1026 s << "File: " << init_file_name << " is not a reset file.";
1027 cerr << s.str() << endl;
1028 throw BaseException(s.str());
1029 }
1030
1031 bool result = false;
1032
1033 // If doc has an version, check it. Otherwise fall back to legacy.
1034 if (document->HasAttribute("version")) {
1035 double version = document->GetAttributeValueAsNumber("version");
1036
1037 if (version >= 3.0) {
1038 const string s("Only initialization file formats 1 and 2 are currently supported");
1039 cerr << document->ReadFrom() << endl << s << endl;
1040 throw BaseException(s);
1041 } else if (version >= 2.0) {
1042 result = Load_v2(document);
1043 } else if (version >= 1.0) {
1044 result = Load_v1(document);
1045 }
1046
1047 } else {
1048 result = Load_v1(document);
1049 }
1050
1051 // Check to see if any engines are specified to be initialized in a running state
1052 Element* running_elements = document->FindElement("running");
1053 while (running_elements) {
1054 int engineNumber = int(running_elements->GetDataAsNumber());
1055 enginesRunning |= engineNumber == -1 ? engineNumber : 1 << engineNumber;
1056 running_elements = document->FindNextElement("running");
1057 }
1058
1059 return result;
1060 }
1061
1062 //******************************************************************************
1063
1064 bool FGInitialCondition::LoadLatitude(Element* position_el)
1065 {
1066 Element* latitude_el = position_el->FindElement("latitude");
1067
1068 if (latitude_el) {
1069 double latitude = position_el->FindElementValueAsNumberConvertTo("latitude", "RAD");
1070
1071 if (fabs(latitude) > 0.5*M_PI) {
1072 string unit_type = latitude_el->GetAttributeValue("unit");
1073 if (unit_type.empty()) unit_type="RAD";
1074
1075 cerr << latitude_el->ReadFrom() << "The latitude value "
1076 << latitude_el->GetDataAsNumber() << " " << unit_type
1077 << " is outside the range [";
1078 if (unit_type == "DEG")
1079 cerr << "-90 DEG ; +90 DEG]" << endl;
1080 else
1081 cerr << "-PI/2 RAD; +PI/2 RAD]" << endl;
1082
1083 return false;
1084 }
1085
1086 string lat_type = latitude_el->GetAttributeValue("type");
1087
1088 if (lat_type == "geod" || lat_type == "geodetic") {
1089 SetGeodLatitudeRadIC(latitude);
1090 lastLatitudeSet = setgeod;
1091 }
1092 else {
1093 SetLatitudeRadIC(latitude);
1094 lastLatitudeSet = setgeoc;
1095 }
1096 }
1097
1098 return true;
1099 }
1100
1101 //******************************************************************************
1102
1103 void FGInitialCondition::SetTrimRequest(std::string trim)
1104 {
1105 std::string& trimOption = to_lower(trim);
1106 if (trimOption == "1")
1107 trimRequested = TrimMode::tGround; // For backwards compatabiity
1108 else if (trimOption == "longitudinal")
1109 trimRequested = TrimMode::tLongitudinal;
1110 else if (trimOption == "full")
1111 trimRequested = TrimMode::tFull;
1112 else if (trimOption == "ground")
1113 trimRequested = TrimMode::tGround;
1114 else if (trimOption == "pullup")
1115 trimRequested = TrimMode::tPullup;
1116 else if (trimOption == "custom")
1117 trimRequested = TrimMode::tCustom;
1118 else if (trimOption == "turn")
1119 trimRequested = TrimMode::tTurn;
1120 }
1121
1122 //******************************************************************************
1123
1124 bool FGInitialCondition::Load_v1(Element* document)
1125 {
1126 bool result = true;
1127
1128 if (document->FindElement("longitude"))
1129 SetLongitudeRadIC(document->FindElementValueAsNumberConvertTo("longitude", "RAD"));
1130 if (document->FindElement("elevation"))
1131 SetTerrainElevationFtIC(document->FindElementValueAsNumberConvertTo("elevation", "FT"));
1132
1133 if (document->FindElement("altitude")) // This is feet above ground level
1134 SetAltitudeAGLFtIC(document->FindElementValueAsNumberConvertTo("altitude", "FT"));
1135 else if (document->FindElement("altitudeAGL")) // This is feet above ground level
1136 SetAltitudeAGLFtIC(document->FindElementValueAsNumberConvertTo("altitudeAGL", "FT"));
1137 else if (document->FindElement("altitudeMSL")) // This is feet above sea level
1138 SetAltitudeASLFtIC(document->FindElementValueAsNumberConvertTo("altitudeMSL", "FT"));
1139
1140 result = LoadLatitude(document);
1141
1142 FGColumnVector3 vOrient = orientation.GetEuler();
1143
1144 if (document->FindElement("phi"))
1145 vOrient(ePhi) = document->FindElementValueAsNumberConvertTo("phi", "RAD");
1146 if (document->FindElement("theta"))
1147 vOrient(eTht) = document->FindElementValueAsNumberConvertTo("theta", "RAD");
1148 if (document->FindElement("psi"))
1149 vOrient(ePsi) = document->FindElementValueAsNumberConvertTo("psi", "RAD");
1150
1151 orientation = FGQuaternion(vOrient);
1152
1153 if (document->FindElement("ubody"))
1154 SetUBodyFpsIC(document->FindElementValueAsNumberConvertTo("ubody", "FT/SEC"));
1155 if (document->FindElement("vbody"))
1156 SetVBodyFpsIC(document->FindElementValueAsNumberConvertTo("vbody", "FT/SEC"));
1157 if (document->FindElement("wbody"))
1158 SetWBodyFpsIC(document->FindElementValueAsNumberConvertTo("wbody", "FT/SEC"));
1159 if (document->FindElement("vnorth"))
1160 SetVNorthFpsIC(document->FindElementValueAsNumberConvertTo("vnorth", "FT/SEC"));
1161 if (document->FindElement("veast"))
1162 SetVEastFpsIC(document->FindElementValueAsNumberConvertTo("veast", "FT/SEC"));
1163 if (document->FindElement("vdown"))
1164 SetVDownFpsIC(document->FindElementValueAsNumberConvertTo("vdown", "FT/SEC"));
1165 if (document->FindElement("vc"))
1166 SetVcalibratedKtsIC(document->FindElementValueAsNumberConvertTo("vc", "KTS"));
1167 if (document->FindElement("vt"))
1168 SetVtrueKtsIC(document->FindElementValueAsNumberConvertTo("vt", "KTS"));
1169 if (document->FindElement("mach"))
1170 SetMachIC(document->FindElementValueAsNumber("mach"));
1171 if (document->FindElement("gamma"))
1172 SetFlightPathAngleDegIC(document->FindElementValueAsNumberConvertTo("gamma", "DEG"));
1173 if (document->FindElement("roc"))
1174 SetClimbRateFpsIC(document->FindElementValueAsNumberConvertTo("roc", "FT/SEC"));
1175 if (document->FindElement("vground"))
1176 SetVgroundKtsIC(document->FindElementValueAsNumberConvertTo("vground", "KTS"));
1177 if (document->FindElement("alpha"))
1178 SetAlphaDegIC(document->FindElementValueAsNumberConvertTo("alpha", "DEG"));
1179 if (document->FindElement("beta"))
1180 SetBetaDegIC(document->FindElementValueAsNumberConvertTo("beta", "DEG"));
1181 if (document->FindElement("vwind"))
1182 SetWindMagKtsIC(document->FindElementValueAsNumberConvertTo("vwind", "KTS"));
1183 if (document->FindElement("winddir"))
1184 SetWindDirDegIC(document->FindElementValueAsNumberConvertTo("winddir", "DEG"));
1185 if (document->FindElement("hwind"))
1186 SetHeadWindKtsIC(document->FindElementValueAsNumberConvertTo("hwind", "KTS"));
1187 if (document->FindElement("xwind"))
1188 SetCrossWindKtsIC(document->FindElementValueAsNumberConvertTo("xwind", "KTS"));
1189 if (document->FindElement("targetNlf"))
1190 SetTargetNlfIC(document->FindElementValueAsNumber("targetNlf"));
1191 if (document->FindElement("trim"))
1192 SetTrimRequest(document->FindElementValue("trim"));
1193
1194 vPQR_body.InitMatrix();
1195
1196 return result;
1197 }
1198
1199 //******************************************************************************
1200
1201 bool FGInitialCondition::Load_v2(Element* document)
1202 {
1203 FGColumnVector3 vOrient;
1204 bool result = true;
1205
1206 // support both earth_position_angle and planet_position_angle, for now.
1207 if (document->FindElement("earth_position_angle"))
1208 epa = document->FindElementValueAsNumberConvertTo("earth_position_angle", "RAD");
1209 if (document->FindElement("planet_position_angle"))
1210 epa = document->FindElementValueAsNumberConvertTo("planet_position_angle", "RAD");
1211
1212 // Calculate the inertial to ECEF matrices
1213 FGMatrix33 Ti2ec(cos(epa), sin(epa), 0.0,
1214 -sin(epa), cos(epa), 0.0,
1215 0.0, 0.0, 1.0);
1216 FGMatrix33 Tec2i = Ti2ec.Transposed();
1217
1218 if (document->FindElement("planet_rotation_rate")) {
1219 fdmex->GetInertial()->SetOmegaPlanet(document->FindElementValueAsNumberConvertTo("planet_rotation_rate", "RAD"));
1220 fdmex->GetPropagate()->in.vOmegaPlanet = fdmex->GetInertial()->GetOmegaPlanet();
1221 fdmex->GetAccelerations()->in.vOmegaPlanet = fdmex->GetInertial()->GetOmegaPlanet();
1222 }
1223 FGColumnVector3 vOmegaEarth = fdmex->GetInertial()->GetOmegaPlanet();
1224
1225 if (document->FindElement("elevation")) {
1226 fdmex->GetInertial()->SetTerrainElevation(document->FindElementValueAsNumberConvertTo("elevation", "FT"));
1227 }
1228
1229 // Initialize vehicle position
1230 //
1231 // Allowable frames:
1232 // - ECI (Earth Centered Inertial)
1233 // - ECEF (Earth Centered, Earth Fixed)
1234
1235 Element* position_el = document->FindElement("position");
1236 if (position_el) {
1237 string frame = position_el->GetAttributeValue("frame");
1238 frame = to_lower(frame);
1239 if (frame == "eci") { // Need to transform vLoc to ECEF for storage and use in FGLocation.
1240 position = Ti2ec * position_el->FindElementTripletConvertTo("FT");
1241 } else if (frame == "ecef") {
1242 if (!position_el->FindElement("x") && !position_el->FindElement("y") && !position_el->FindElement("z")) {
1243 if (position_el->FindElement("longitude")) {
1244 SetLongitudeRadIC(position_el->FindElementValueAsNumberConvertTo("longitude", "RAD"));
1245 }
1246 if (position_el->FindElement("radius")) {
1247 position.SetRadius(position_el->FindElementValueAsNumberConvertTo("radius", "FT"));
1248 } else if (position_el->FindElement("altitudeAGL")) {
1249 SetAltitudeAGLFtIC(position_el->FindElementValueAsNumberConvertTo("altitudeAGL", "FT"));
1250 } else if (position_el->FindElement("altitudeMSL")) {
1251 SetAltitudeASLFtIC(position_el->FindElementValueAsNumberConvertTo("altitudeMSL", "FT"));
1252 } else {
1253 cerr << endl << " No altitude or radius initial condition is given." << endl;
1254 result = false;
1255 }
1256
1257 if (result)
1258 result = LoadLatitude(position_el);
1259
1260 } else {
1261 position = position_el->FindElementTripletConvertTo("FT");
1262 }
1263 } else {
1264 cerr << endl << " Neither ECI nor ECEF frame is specified for initial position." << endl;
1265 result = false;
1266 }
1267 } else {
1268 cerr << endl << " Initial position not specified in this initialization file." << endl;
1269 result = false;
1270 }
1271
1272 // End of position initialization
1273
1274 // Initialize vehicle orientation
1275 // Allowable frames
1276 // - ECI (Earth Centered Inertial)
1277 // - ECEF (Earth Centered, Earth Fixed)
1278 // - Local
1279 //
1280 // Need to convert the provided orientation to a local orientation, using
1281 // the given orientation and knowledge of the Earth position angle.
1282 // This could be done using matrices (where in the subscript "b/a",
1283 // it is meant "b with respect to a", and where b=body frame,
1284 // i=inertial frame, l=local NED frame and e=ecef frame) as:
1285 //
1286 // C_b/l = C_b/e * C_e/l
1287 //
1288 // Using quaternions (note reverse ordering compared to matrix representation):
1289 //
1290 // Q_b/l = Q_e/l * Q_b/e
1291 //
1292 // Use the specific matrices as needed. The above example of course is for the whole
1293 // body to local orientation.
1294 // The new orientation angles can be extracted from the matrix or the quaternion.
1295 // ToDo: Do we need to deal with normalization of the quaternions here?
1296
1297 Element* orientation_el = document->FindElement("orientation");
1298 if (orientation_el) {
1299 string frame = orientation_el->GetAttributeValue("frame");
1300 frame = to_lower(frame);
1301 vOrient = orientation_el->FindElementTripletConvertTo("RAD");
1302 if (frame == "eci") {
1303
1304 // In this case, we are supplying the Euler angles for the vehicle with
1305 // respect to the inertial system, represented by the C_b/i Matrix.
1306 // We want the body orientation with respect to the local (NED frame):
1307 //
1308 // C_b/l = C_b/i * C_i/l
1309 //
1310 // Or, using quaternions (note reverse ordering compared to matrix representation):
1311 //
1312 // Q_b/l = Q_i/l * Q_b/i
1313
1314 FGQuaternion QuatI2Body = FGQuaternion(vOrient);
1315 QuatI2Body.Normalize();
1316 FGQuaternion QuatLocal2I = Tec2i * position.GetTl2ec();
1317 QuatLocal2I.Normalize();
1318 orientation = QuatLocal2I * QuatI2Body;
1319
1320 } else if (frame == "ecef") {
1321
1322 // In this case we are given the Euler angles representing the orientation of
1323 // the body with respect to the ECEF system, represented by the C_b/e Matrix.
1324 // We want the body orientation with respect to the local (NED frame):
1325 //
1326 // C_b/l = C_b/e * C_e/l
1327 //
1328 // Using quaternions (note reverse ordering compared to matrix representation):
1329 //
1330 // Q_b/l = Q_e/l * Q_b/e
1331
1332 FGQuaternion QuatEC2Body(vOrient); // Store relationship of Body frame wrt ECEF frame, Q_b/e
1333 QuatEC2Body.Normalize();
1334 FGQuaternion QuatLocal2EC = position.GetTl2ec(); // Get Q_e/l from matrix
1335 QuatLocal2EC.Normalize();
1336 orientation = QuatLocal2EC * QuatEC2Body; // Q_b/l = Q_e/l * Q_b/e
1337
1338 } else if (frame == "local") {
1339
1340 orientation = FGQuaternion(vOrient);
1341
1342 } else {
1343
1344 cerr << endl << fgred << " Orientation frame type: \"" << frame
1345 << "\" is not supported!" << reset << endl << endl;
1346 result = false;
1347
1348 }
1349 }
1350
1351 // Initialize vehicle velocity
1352 // Allowable frames
1353 // - ECI (Earth Centered Inertial)
1354 // - ECEF (Earth Centered, Earth Fixed)
1355 // - Local
1356 // - Body
1357 // The vehicle will be defaulted to (0,0,0) in the Body frame if nothing is provided.
1358
1359 Element* velocity_el = document->FindElement("velocity");
1360 FGMatrix33 mTec2l = position.GetTec2l();
1361 const FGMatrix33& Tb2l = orientation.GetTInv();
1362
1363 if (velocity_el) {
1364
1365 string frame = velocity_el->GetAttributeValue("frame");
1366 frame = to_lower(frame);
1367 FGColumnVector3 vInitVelocity = velocity_el->FindElementTripletConvertTo("FT/SEC");
1368
1369 if (frame == "eci") {
1370 FGColumnVector3 omega_cross_r = vOmegaEarth * (Tec2i * position);
1371 vUVW_NED = mTec2l * (vInitVelocity - omega_cross_r);
1372 lastSpeedSet = setned;
1373 } else if (frame == "ecef") {
1374 vUVW_NED = mTec2l * vInitVelocity;
1375 lastSpeedSet = setned;
1376 } else if (frame == "local") {
1377 vUVW_NED = vInitVelocity;
1378 lastSpeedSet = setned;
1379 } else if (frame == "body") {
1380 vUVW_NED = Tb2l * vInitVelocity;
1381 lastSpeedSet = setuvw;
1382 } else {
1383
1384 cerr << endl << fgred << " Velocity frame type: \"" << frame
1385 << "\" is not supported!" << reset << endl << endl;
1386 result = false;
1387
1388 }
1389
1390 } else {
1391
1392 vUVW_NED.InitMatrix();
1393
1394 }
1395
1396 vt = vUVW_NED.Magnitude();
1397
1398 calcAeroAngles(vUVW_NED);
1399
1400 // Initialize vehicle body rates
1401 // Allowable frames
1402 // - ECI (Earth Centered Inertial)
1403 // - ECEF (Earth Centered, Earth Fixed)
1404 // - Body
1405
1406 Element* attrate_el = document->FindElement("attitude_rate");
1407
1408 if (attrate_el) {
1409
1410 string frame = attrate_el->GetAttributeValue("frame");
1411 frame = to_lower(frame);
1412 const FGMatrix33& Tl2b = orientation.GetT();
1413 FGColumnVector3 vAttRate = attrate_el->FindElementTripletConvertTo("RAD/SEC");
1414
1415 if (frame == "eci") {
1416 FGMatrix33 Ti2l = position.GetTec2l() * Ti2ec;
1417 vPQR_body = Tl2b * Ti2l * (vAttRate - vOmegaEarth);
1418 } else if (frame == "ecef") {
1419 vPQR_body = Tl2b * position.GetTec2l() * vAttRate;
1420 } else if (frame == "local") {
1421 // Refer to Stevens and Lewis, 1.5-14a, pg. 49.
1422 // This is the rotation rate of the "Local" frame, expressed in the local frame.
1423 double radInv = 1.0 / position.GetRadius();
1424 FGColumnVector3 vOmegaLocal = {radInv*vUVW_NED(eEast),
1425 -radInv*vUVW_NED(eNorth),
1426 -radInv*vUVW_NED(eEast)*tan(position.GetLatitude())};
1427 vPQR_body = Tl2b * (vAttRate + vOmegaLocal);
1428 } else if (frame == "body") {
1429 vPQR_body = vAttRate;
1430 } else if (!frame.empty()) { // misspelling of frame
1431
1432 cerr << endl << fgred << " Attitude rate frame type: \"" << frame
1433 << "\" is not supported!" << reset << endl << endl;
1434 result = false;
1435
1436 } else if (frame.empty()) {
1437 vPQR_body.InitMatrix();
1438 }
1439
1440 } else { // Body frame attitude rate assumed 0 relative to local.
1441 vPQR_body.InitMatrix();
1442 }
1443
1444 return result;
1445 }
1446
1447 //******************************************************************************
1448
1449 void FGInitialCondition::bind(FGPropertyManager* PropertyManager)
1450 {
1451 PropertyManager->Tie("ic/vc-kts", this,
1452 &FGInitialCondition::GetVcalibratedKtsIC,
1453 &FGInitialCondition::SetVcalibratedKtsIC);
1454 PropertyManager->Tie("ic/ve-kts", this,
1455 &FGInitialCondition::GetVequivalentKtsIC,
1456 &FGInitialCondition::SetVequivalentKtsIC);
1457 PropertyManager->Tie("ic/vg-kts", this,
1458 &FGInitialCondition::GetVgroundKtsIC,
1459 &FGInitialCondition::SetVgroundKtsIC);
1460 PropertyManager->Tie("ic/vt-kts", this,
1461 &FGInitialCondition::GetVtrueKtsIC,
1462 &FGInitialCondition::SetVtrueKtsIC);
1463 PropertyManager->Tie("ic/mach", this,
1464 &FGInitialCondition::GetMachIC,
1465 &FGInitialCondition::SetMachIC);
1466 PropertyManager->Tie("ic/roc-fpm", this,
1467 &FGInitialCondition::GetClimbRateFpmIC,
1468 &FGInitialCondition::SetClimbRateFpmIC);
1469 PropertyManager->Tie("ic/gamma-deg", this,
1470 &FGInitialCondition::GetFlightPathAngleDegIC,
1471 &FGInitialCondition::SetFlightPathAngleDegIC);
1472 PropertyManager->Tie("ic/alpha-deg", this,
1473 &FGInitialCondition::GetAlphaDegIC,
1474 &FGInitialCondition::SetAlphaDegIC);
1475 PropertyManager->Tie("ic/beta-deg", this,
1476 &FGInitialCondition::GetBetaDegIC,
1477 &FGInitialCondition::SetBetaDegIC);
1478 PropertyManager->Tie("ic/theta-deg", this,
1479 &FGInitialCondition::GetThetaDegIC,
1480 &FGInitialCondition::SetThetaDegIC);
1481 PropertyManager->Tie("ic/phi-deg", this,
1482 &FGInitialCondition::GetPhiDegIC,
1483 &FGInitialCondition::SetPhiDegIC);
1484 PropertyManager->Tie("ic/psi-true-deg", this,
1485 &FGInitialCondition::GetPsiDegIC,
1486 &FGInitialCondition::SetPsiDegIC);
1487 PropertyManager->Tie("ic/lat-gc-deg", this,
1488 &FGInitialCondition::GetLatitudeDegIC,
1489 &FGInitialCondition::SetLatitudeDegIC);
1490 PropertyManager->Tie("ic/long-gc-deg", this,
1491 &FGInitialCondition::GetLongitudeDegIC,
1492 &FGInitialCondition::SetLongitudeDegIC);
1493 PropertyManager->Tie("ic/h-sl-ft", this,
1494 &FGInitialCondition::GetAltitudeASLFtIC,
1495 &FGInitialCondition::SetAltitudeASLFtIC);
1496 PropertyManager->Tie("ic/h-agl-ft", this,
1497 &FGInitialCondition::GetAltitudeAGLFtIC,
1498 &FGInitialCondition::SetAltitudeAGLFtIC);
1499 PropertyManager->Tie("ic/terrain-elevation-ft", this,
1500 &FGInitialCondition::GetTerrainElevationFtIC,
1501 &FGInitialCondition::SetTerrainElevationFtIC);
1502 PropertyManager->Tie("ic/vg-fps", this,
1503 &FGInitialCondition::GetVgroundFpsIC,
1504 &FGInitialCondition::SetVgroundFpsIC);
1505 PropertyManager->Tie("ic/vt-fps", this,
1506 &FGInitialCondition::GetVtrueFpsIC,
1507 &FGInitialCondition::SetVtrueFpsIC);
1508 PropertyManager->Tie("ic/vw-bx-fps", this,
1509 &FGInitialCondition::GetWindUFpsIC);
1510 PropertyManager->Tie("ic/vw-by-fps", this,
1511 &FGInitialCondition::GetWindVFpsIC);
1512 PropertyManager->Tie("ic/vw-bz-fps", this,
1513 &FGInitialCondition::GetWindWFpsIC);
1514 PropertyManager->Tie("ic/vw-north-fps", this,
1515 &FGInitialCondition::GetWindNFpsIC);
1516 PropertyManager->Tie("ic/vw-east-fps", this,
1517 &FGInitialCondition::GetWindEFpsIC);
1518 PropertyManager->Tie("ic/vw-down-fps", this,
1519 &FGInitialCondition::GetWindDFpsIC);
1520 PropertyManager->Tie("ic/vw-mag-fps", this,
1521 &FGInitialCondition::GetWindFpsIC);
1522 PropertyManager->Tie("ic/vw-dir-deg", this,
1523 &FGInitialCondition::GetWindDirDegIC,
1524 &FGInitialCondition::SetWindDirDegIC);
1525
1526 PropertyManager->Tie("ic/roc-fps", this,
1527 &FGInitialCondition::GetClimbRateFpsIC,
1528 &FGInitialCondition::SetClimbRateFpsIC);
1529 PropertyManager->Tie("ic/u-fps", this,
1530 &FGInitialCondition::GetUBodyFpsIC,
1531 &FGInitialCondition::SetUBodyFpsIC);
1532 PropertyManager->Tie("ic/v-fps", this,
1533 &FGInitialCondition::GetVBodyFpsIC,
1534 &FGInitialCondition::SetVBodyFpsIC);
1535 PropertyManager->Tie("ic/w-fps", this,
1536 &FGInitialCondition::GetWBodyFpsIC,
1537 &FGInitialCondition::SetWBodyFpsIC);
1538 PropertyManager->Tie("ic/vn-fps", this,
1539 &FGInitialCondition::GetVNorthFpsIC,
1540 &FGInitialCondition::SetVNorthFpsIC);
1541 PropertyManager->Tie("ic/ve-fps", this,
1542 &FGInitialCondition::GetVEastFpsIC,
1543 &FGInitialCondition::SetVEastFpsIC);
1544 PropertyManager->Tie("ic/vd-fps", this,
1545 &FGInitialCondition::GetVDownFpsIC,
1546 &FGInitialCondition::SetVDownFpsIC);
1547 PropertyManager->Tie("ic/gamma-rad", this,
1548 &FGInitialCondition::GetFlightPathAngleRadIC,
1549 &FGInitialCondition::SetFlightPathAngleRadIC);
1550 PropertyManager->Tie("ic/alpha-rad", this,
1551 &FGInitialCondition::GetAlphaRadIC,
1552 &FGInitialCondition::SetAlphaRadIC);
1553 PropertyManager->Tie("ic/theta-rad", this,
1554 &FGInitialCondition::GetThetaRadIC,
1555 &FGInitialCondition::SetThetaRadIC);
1556 PropertyManager->Tie("ic/beta-rad", this,
1557 &FGInitialCondition::GetBetaRadIC,
1558 &FGInitialCondition::SetBetaRadIC);
1559 PropertyManager->Tie("ic/phi-rad", this,
1560 &FGInitialCondition::GetPhiRadIC,
1561 &FGInitialCondition::SetPhiRadIC);
1562 PropertyManager->Tie("ic/psi-true-rad", this,
1563 &FGInitialCondition::GetPsiRadIC,
1564 &FGInitialCondition::SetPsiRadIC);
1565 PropertyManager->Tie("ic/lat-gc-rad", this,
1566 &FGInitialCondition::GetLatitudeRadIC,
1567 &FGInitialCondition::SetLatitudeRadIC);
1568 PropertyManager->Tie("ic/long-gc-rad", this,
1569 &FGInitialCondition::GetLongitudeRadIC,
1570 &FGInitialCondition::SetLongitudeRadIC);
1571 PropertyManager->Tie("ic/p-rad_sec", this,
1572 &FGInitialCondition::GetPRadpsIC,
1573 &FGInitialCondition::SetPRadpsIC);
1574 PropertyManager->Tie("ic/q-rad_sec", this,
1575 &FGInitialCondition::GetQRadpsIC,
1576 &FGInitialCondition::SetQRadpsIC);
1577 PropertyManager->Tie("ic/r-rad_sec", this,
1578 &FGInitialCondition::GetRRadpsIC,
1579 &FGInitialCondition::SetRRadpsIC);
1580 PropertyManager->Tie("ic/lat-geod-rad", this,
1581 &FGInitialCondition::GetGeodLatitudeRadIC,
1582 &FGInitialCondition::SetGeodLatitudeRadIC);
1583 PropertyManager->Tie("ic/lat-geod-deg", this,
1584 &FGInitialCondition::GetGeodLatitudeDegIC,
1585 &FGInitialCondition::SetGeodLatitudeDegIC);
1586 PropertyManager->Tie("ic/geod-alt-ft", &position,
1587 &FGLocation::GetGeodAltitude);
1588
1589 PropertyManager->Tie("ic/targetNlf", this,
1590 &FGInitialCondition::GetTargetNlfIC,
1591 &FGInitialCondition::SetTargetNlfIC);
1592 }
1593
1594 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1595 // The bitmasked value choices are as follows:
1596 // unset: In this case (the default) JSBSim would only print
1597 // out the normally expected messages, essentially echoing
1598 // the config files as they are read. If the environment
1599 // variable is not set, debug_lvl is set to 1 internally
1600 // 0: This requests JSBSim not to output any messages
1601 // whatsoever.
1602 // 1: This value explicity requests the normal JSBSim
1603 // startup messages
1604 // 2: This value asks for a message to be printed out when
1605 // a class is instantiated
1606 // 4: When this value is set, a message is displayed when a
1607 // FGModel object executes its Run() method
1608 // 8: When this value is set, various runtime state variables
1609 // are printed out periodically
1610 // 16: When set various parameters are sanity checked and
1611 // a message is printed out when they go out of bounds
1612
1613 void FGInitialCondition::Debug(int from)
1614 {
1615 if (debug_lvl <= 0) return;
1616
1617 if (debug_lvl & 1) { // Standard console startup message output
1618 }
1619 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
1620 if (from == 0) cout << "Instantiated: FGInitialCondition" << endl;
1621 if (from == 1) cout << "Destroyed: FGInitialCondition" << endl;
1622 }
1623 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
1624 }
1625 if (debug_lvl & 8 ) { // Runtime state variables
1626 }
1627 if (debug_lvl & 16) { // Sanity checking
1628 }
1629 if (debug_lvl & 64) {
1630 if (from == 0) { // Constructor
1631 }
1632 }
1633 }
1634 }