-
Notifications
You must be signed in to change notification settings - Fork 431
/
FGInitialCondition.cpp
1676 lines (1402 loc) · 59.5 KB
/
FGInitialCondition.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
Header: FGInitialCondition.cpp
Author: Tony Peden, Bertrand Coconnier
Date started: 7/1/99
--------- Copyright (C) 1999 Anthony K. Peden (apeden@earthlink.net) ---------
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Further information about the GNU Lesser General Public License can also be
found on the world wide web at http://www.gnu.org.
HISTORY
--------------------------------------------------------------------------------
7/1/99 TP Created
11/25/10 BC Complete revision - Use minimal set of variables to prevent
inconsistent states. Wind is correctly handled.
FUNCTIONAL DESCRIPTION
--------------------------------------------------------------------------------
The purpose of this class is to take a set of initial conditions and provide a
kinematically consistent set of body axis velocity components, euler angles, and
altitude. This class does not attempt to trim the model i.e. the sim will most
likely start in a very dynamic state (unless, of course, you have chosen your
IC's wisely) even after setting it up with this class.
********************************************************************************
INCLUDES
*******************************************************************************/
#include "FGInitialCondition.h"
#include "models/FGInertial.h"
#include "models/FGAtmosphere.h"
#include "models/FGAccelerations.h"
#include "input_output/FGXMLFileRead.h"
#include "FGTrim.h"
#include "FGFDMExec.h"
using namespace std;
namespace JSBSim {
//******************************************************************************
FGInitialCondition::FGInitialCondition(FGFDMExec *FDMExec) : fdmex(FDMExec)
{
InitializeIC();
if(FDMExec) {
Atmosphere=fdmex->GetAtmosphere();
Aircraft=fdmex->GetAircraft();
} else {
cout << "FGInitialCondition: This class requires a pointer to a valid FGFDMExec object" << endl;
}
Debug(0);
}
//******************************************************************************
FGInitialCondition::~FGInitialCondition()
{
Debug(1);
}
//******************************************************************************
void FGInitialCondition::ResetIC(double u0, double v0, double w0,
double p0, double q0, double r0,
double alpha0, double beta0,
double phi0, double theta0, double psi0,
double latRad0, double lonRad0, double altAGLFt0,
double gamma0)
{
double calpha = cos(alpha0), cbeta = cos(beta0);
double salpha = sin(alpha0), sbeta = sin(beta0);
InitializeIC();
vPQR_body = {p0, q0, r0};
alpha = alpha0; beta = beta0;
position.SetLongitude(lonRad0);
position.SetLatitude(latRad0);
fdmex->GetInertial()->SetAltitudeAGL(position, altAGLFt0);
lastLatitudeSet = setgeoc;
lastAltitudeSet = setagl;
orientation = FGQuaternion(phi0, theta0, psi0);
const FGMatrix33& Tb2l = orientation.GetTInv();
vUVW_NED = Tb2l * FGColumnVector3(u0, v0, w0);
vt = vUVW_NED.Magnitude();
lastSpeedSet = setuvw;
Tw2b = { calpha*cbeta, -calpha*sbeta, -salpha,
sbeta, cbeta, 0.0,
salpha*cbeta, -salpha*sbeta, calpha };
Tb2w = Tw2b.Transposed();
SetFlightPathAngleRadIC(gamma0);
}
//******************************************************************************
void FGInitialCondition::InitializeIC(void)
{
alpha = beta = 0.0;
epa = 0.0;
double a = fdmex->GetInertial()->GetSemimajor();
double b = fdmex->GetInertial()->GetSemiminor();
position.SetEllipse(a, b);
position.SetPositionGeodetic(0.0, 0.0, 0.0);
orientation = FGQuaternion(0.0, 0.0, 0.0);
vUVW_NED.InitMatrix();
vPQR_body.InitMatrix();
vt=0;
targetNlfIC = 1.0;
Tw2b = { 1., 0., 0., 0., 1., 0., 0., 0., 1. };
Tb2w = { 1., 0., 0., 0., 1., 0., 0., 0., 1. };
lastSpeedSet = setvt;
lastAltitudeSet = setasl;
lastLatitudeSet = setgeoc;
enginesRunning = 0;
trimRequested = TrimMode::tNone;
}
//******************************************************************************
void FGInitialCondition::SetVequivalentKtsIC(double ve)
{
double altitudeASL = GetAltitudeASLFtIC();
double rho = Atmosphere->GetDensity(altitudeASL);
double rhoSL = Atmosphere->GetDensitySL();
SetVtrueFpsIC(ve*ktstofps*sqrt(rhoSL/rho));
lastSpeedSet = setve;
}
//******************************************************************************
void FGInitialCondition::SetMachIC(double mach)
{
double altitudeASL = GetAltitudeASLFtIC();
double soundSpeed = Atmosphere->GetSoundSpeed(altitudeASL);
SetVtrueFpsIC(mach*soundSpeed);
lastSpeedSet = setmach;
}
//******************************************************************************
void FGInitialCondition::SetVcalibratedKtsIC(double vcas)
{
double altitudeASL = GetAltitudeASLFtIC();
double pressure = Atmosphere->GetPressure(altitudeASL);
double mach = MachFromVcalibrated(fabs(vcas)*ktstofps, pressure);
double soundSpeed = Atmosphere->GetSoundSpeed(altitudeASL);
SetVtrueFpsIC(mach * soundSpeed);
lastSpeedSet = setvc;
}
//******************************************************************************
// Updates alpha and beta according to the aircraft true airspeed in the local
// NED frame.
void FGInitialCondition::calcAeroAngles(const FGColumnVector3& _vt_NED)
{
const FGMatrix33& Tl2b = orientation.GetT();
FGColumnVector3 _vt_BODY = Tl2b * _vt_NED;
double ua = _vt_BODY(eX);
double va = _vt_BODY(eY);
double wa = _vt_BODY(eZ);
double uwa = sqrt(ua*ua + wa*wa);
double calpha, cbeta;
double salpha, sbeta;
alpha = beta = 0.0;
calpha = cbeta = 1.0;
salpha = sbeta = 0.0;
if( wa != 0 )
alpha = atan2( wa, ua );
// alpha cannot be constrained without updating other informations like the
// true speed or the Euler angles. Otherwise we might end up with an
// inconsistent state of the aircraft.
/*alpha = Constrain(fdmex->GetAerodynamics()->GetAlphaCLMin(), alpha,
fdmex->GetAerodynamics()->GetAlphaCLMax());*/
if( va != 0 )
beta = atan2( va, uwa );
if (uwa != 0) {
calpha = ua / uwa;
salpha = wa / uwa;
}
if (vt != 0) {
cbeta = uwa / vt;
sbeta = va / vt;
}
Tw2b = { calpha*cbeta, -calpha*sbeta, -salpha,
sbeta, cbeta, 0.0,
salpha*cbeta, -salpha*sbeta, calpha };
Tb2w = Tw2b.Transposed();
}
//******************************************************************************
// Set the ground velocity. Caution it sets the vertical velocity to zero to
// keep backward compatibility.
void FGInitialCondition::SetVgroundFpsIC(double vg)
{
const FGMatrix33& Tb2l = orientation.GetTInv();
FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
vUVW_NED(eU) = vg * orientation.GetCosEuler(ePsi);
vUVW_NED(eV) = vg * orientation.GetSinEuler(ePsi);
vUVW_NED(eW) = 0.;
_vt_NED = vUVW_NED + _vWIND_NED;
vt = _vt_NED.Magnitude();
calcAeroAngles(_vt_NED);
lastSpeedSet = setvg;
}
//******************************************************************************
// Sets the true airspeed. The amplitude of the airspeed is modified but its
// direction is kept unchanged. If there is no wind, the same is true for the
// ground velocity. If there is some wind, the airspeed direction is unchanged
// but this may result in the ground velocity direction being altered. This is
// for backward compatibility.
void FGInitialCondition::SetVtrueFpsIC(double vtrue)
{
const FGMatrix33& Tb2l = orientation.GetTInv();
FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
if (vt > 0.1)
_vt_NED *= vtrue / vt;
else
_vt_NED = Tb2l * Tw2b * FGColumnVector3(vtrue, 0., 0.);
vt = vtrue;
vUVW_NED = _vt_NED - _vWIND_NED;
calcAeroAngles(_vt_NED);
lastSpeedSet = setvt;
}
//******************************************************************************
// When the climb rate is modified, we need to update the angles theta and beta
// to keep the true airspeed amplitude, the AoA and the heading unchanged.
// Beta will be modified if the aircraft roll angle is not null.
void FGInitialCondition::SetClimbRateFpsIC(double hdot)
{
if (fabs(hdot) > vt) {
cerr << "The climb rate cannot be higher than the true speed." << endl;
return;
}
const FGMatrix33& Tb2l = orientation.GetTInv();
FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
FGColumnVector3 _WIND_NED = _vt_NED - vUVW_NED;
double hdot0 = -_vt_NED(eW);
if (fabs(hdot0) < vt) { // Is this check really needed ?
double scale = sqrt((vt*vt-hdot*hdot)/(vt*vt-hdot0*hdot0));
_vt_NED(eU) *= scale;
_vt_NED(eV) *= scale;
}
_vt_NED(eW) = -hdot;
vUVW_NED = _vt_NED - _WIND_NED;
// Updating the angles theta and beta to keep the true airspeed amplitude
calcThetaBeta(alpha, _vt_NED);
}
//******************************************************************************
// When the AoA is modified, we need to update the angles theta and beta to
// keep the true airspeed amplitude, the climb rate and the heading unchanged.
// Beta will be modified if the aircraft roll angle is not null.
void FGInitialCondition::SetAlphaRadIC(double alfa)
{
const FGMatrix33& Tb2l = orientation.GetTInv();
FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
calcThetaBeta(alfa, _vt_NED);
}
//******************************************************************************
// When the AoA is modified, we need to update the angles theta and beta to
// keep the true airspeed amplitude, the climb rate and the heading unchanged.
// Beta will be modified if the aircraft roll angle is not null.
void FGInitialCondition::calcThetaBeta(double alfa, const FGColumnVector3& _vt_NED)
{
FGColumnVector3 vOrient = orientation.GetEuler();
double calpha = cos(alfa), salpha = sin(alfa);
double cpsi = orientation.GetCosEuler(ePsi), spsi = orientation.GetSinEuler(ePsi);
double cphi = orientation.GetCosEuler(ePhi), sphi = orientation.GetSinEuler(ePhi);
FGMatrix33 Tpsi( cpsi, spsi, 0.,
-spsi, cpsi, 0.,
0., 0., 1.);
FGMatrix33 Tphi(1., 0., 0.,
0., cphi, sphi,
0.,-sphi, cphi);
FGMatrix33 Talpha( calpha, 0., salpha,
0., 1., 0.,
-salpha, 0., calpha);
FGColumnVector3 v0 = Tpsi * _vt_NED;
FGColumnVector3 n = (Talpha * Tphi).Transposed() * FGColumnVector3(0., 0., 1.);
FGColumnVector3 y = {0., 1., 0.};
FGColumnVector3 u = y - DotProduct(y, n) * n;
FGColumnVector3 p = y * n;
if (DotProduct(p, v0) < 0) p *= -1.0;
p.Normalize();
u *= DotProduct(v0, y) / DotProduct(u, y);
// There are situations where the desired alpha angle cannot be obtained. This
// is not a limitation of the algorithm but is due to the mathematical problem
// not having a solution. This can only be cured by limiting the alpha angle
// or by modifying an additional angle (psi ?). Since this is anticipated to
// be a pathological case (mainly when a high roll angle is required) this
// situation is not addressed below. However if there are complaints about the
// following error being raised too often, we might need to reconsider this
// position.
if (DotProduct(v0, v0) < DotProduct(u, u)) {
cerr << "Cannot modify angle 'alpha' from " << alpha << " to " << alfa << endl;
return;
}
FGColumnVector3 v1 = u + sqrt(DotProduct(v0, v0) - DotProduct(u, u))*p;
FGColumnVector3 v0xz(v0(eU), 0., v0(eW));
FGColumnVector3 v1xz(v1(eU), 0., v1(eW));
v0xz.Normalize();
v1xz.Normalize();
double sinTheta = (v1xz * v0xz)(eY);
vOrient(eTht) = asin(sinTheta);
orientation = FGQuaternion(vOrient);
const FGMatrix33& Tl2b = orientation.GetT();
FGColumnVector3 v2 = Talpha * Tl2b * _vt_NED;
alpha = alfa;
beta = atan2(v2(eV), v2(eU));
double cbeta=1.0, sbeta=0.0;
if (vt != 0.0) {
cbeta = v2(eU) / vt;
sbeta = v2(eV) / vt;
}
Tw2b = { calpha*cbeta, -calpha*sbeta, -salpha,
sbeta, cbeta, 0.0,
salpha*cbeta, -salpha*sbeta, calpha };
Tb2w = Tw2b.Transposed();
}
//******************************************************************************
// When the beta angle is modified, we need to update the angles theta and psi
// to keep the true airspeed (amplitude and direction - including the climb rate)
// and the alpha angle unchanged. This may result in the aircraft heading (psi)
// being altered especially if there is cross wind.
void FGInitialCondition::SetBetaRadIC(double bta)
{
const FGMatrix33& Tb2l = orientation.GetTInv();
FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
FGColumnVector3 vOrient = orientation.GetEuler();
beta = bta;
double calpha = cos(alpha), salpha = sin(alpha);
double cbeta = cos(beta), sbeta = sin(beta);
double cphi = orientation.GetCosEuler(ePhi), sphi = orientation.GetSinEuler(ePhi);
FGMatrix33 TphiInv(1., 0., 0.,
0., cphi,-sphi,
0., sphi, cphi);
Tw2b = { calpha*cbeta, -calpha*sbeta, -salpha,
sbeta, cbeta, 0.0,
salpha*cbeta, -salpha*sbeta, calpha };
Tb2w = Tw2b.Transposed();
FGColumnVector3 vf = TphiInv * Tw2b * FGColumnVector3(vt, 0., 0.);
FGColumnVector3 v0xy(_vt_NED(eX), _vt_NED(eY), 0.);
FGColumnVector3 v1xy(sqrt(v0xy(eX)*v0xy(eX)+v0xy(eY)*v0xy(eY)-vf(eY)*vf(eY)),vf(eY),0.);
v0xy.Normalize();
v1xy.Normalize();
if (vf(eX) < 0.) v0xy(eX) *= -1.0;
double sinPsi = (v1xy * v0xy)(eZ);
double cosPsi = DotProduct(v0xy, v1xy);
vOrient(ePsi) = atan2(sinPsi, cosPsi);
FGMatrix33 Tpsi( cosPsi, sinPsi, 0.,
-sinPsi, cosPsi, 0.,
0., 0., 1.);
FGColumnVector3 v2xz = Tpsi * _vt_NED;
FGColumnVector3 vfxz = vf;
v2xz(eV) = vfxz(eV) = 0.0;
v2xz.Normalize();
vfxz.Normalize();
double sinTheta = (v2xz * vfxz)(eY);
vOrient(eTht) = -asin(sinTheta);
orientation = FGQuaternion(vOrient);
}
//******************************************************************************
// Modifies the body frame orientation.
void FGInitialCondition::SetEulerAngleRadIC(int idx, double angle)
{
const FGMatrix33& Tb2l = orientation.GetTInv();
const FGMatrix33& Tl2b = orientation.GetT();
FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
FGColumnVector3 _vUVW_BODY = Tl2b * vUVW_NED;
FGColumnVector3 vOrient = orientation.GetEuler();
vOrient(idx) = angle;
orientation = FGQuaternion(vOrient);
if ((lastSpeedSet != setned) && (lastSpeedSet != setvg)) {
const FGMatrix33& newTb2l = orientation.GetTInv();
vUVW_NED = newTb2l * _vUVW_BODY;
_vt_NED = vUVW_NED + _vWIND_NED;
vt = _vt_NED.Magnitude();
}
calcAeroAngles(_vt_NED);
}
//******************************************************************************
// Modifies an aircraft velocity component (eU, eV or eW) in the body frame. The
// true airspeed is modified accordingly. If there is some wind, the airspeed
// direction modification may differ from the body velocity modification.
void FGInitialCondition::SetBodyVelFpsIC(int idx, double vel)
{
const FGMatrix33& Tb2l = orientation.GetTInv();
const FGMatrix33& Tl2b = orientation.GetT();
FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
FGColumnVector3 _vUVW_BODY = Tl2b * vUVW_NED;
FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
_vUVW_BODY(idx) = vel;
vUVW_NED = Tb2l * _vUVW_BODY;
_vt_NED = vUVW_NED + _vWIND_NED;
vt = _vt_NED.Magnitude();
calcAeroAngles(_vt_NED);
lastSpeedSet = setuvw;
}
//******************************************************************************
// Modifies an aircraft velocity component (eX, eY or eZ) in the local NED frame.
// The true airspeed is modified accordingly. If there is some wind, the airspeed
// direction modification may differ from the local velocity modification.
void FGInitialCondition::SetNEDVelFpsIC(int idx, double vel)
{
const FGMatrix33& Tb2l = orientation.GetTInv();
FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
vUVW_NED(idx) = vel;
_vt_NED = vUVW_NED + _vWIND_NED;
vt = _vt_NED.Magnitude();
calcAeroAngles(_vt_NED);
lastSpeedSet = setned;
}
//******************************************************************************
// Set wind amplitude and direction in the local NED frame. The aircraft velocity
// with respect to the ground is not changed but the true airspeed is.
void FGInitialCondition::SetWindNEDFpsIC(double wN, double wE, double wD )
{
FGColumnVector3 _vt_NED = vUVW_NED + FGColumnVector3(wN, wE, wD);
vt = _vt_NED.Magnitude();
calcAeroAngles(_vt_NED);
}
//******************************************************************************
// Set the cross wind velocity (in knots). Here, 'cross wind' means perpendicular
// to the aircraft heading and parallel to the ground. The aircraft velocity
// with respect to the ground is not changed but the true airspeed is.
void FGInitialCondition::SetCrossWindKtsIC(double cross)
{
const FGMatrix33& Tb2l = orientation.GetTInv();
FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
FGColumnVector3 _vCROSS(-orientation.GetSinEuler(ePsi), orientation.GetCosEuler(ePsi), 0.);
// Gram-Schmidt process is used to remove the existing cross wind component
_vWIND_NED -= DotProduct(_vWIND_NED, _vCROSS) * _vCROSS;
// Which is now replaced by the new value. The input cross wind is expected
// in knots, so first convert to fps, which is the internal unit used.
_vWIND_NED += (cross * ktstofps) * _vCROSS;
_vt_NED = vUVW_NED + _vWIND_NED;
vt = _vt_NED.Magnitude();
calcAeroAngles(_vt_NED);
}
//******************************************************************************
// Set the head wind velocity (in knots). Here, 'head wind' means parallel
// to the aircraft heading and to the ground. The aircraft velocity
// with respect to the ground is not changed but the true airspeed is.
void FGInitialCondition::SetHeadWindKtsIC(double head)
{
const FGMatrix33& Tb2l = orientation.GetTInv();
FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
// This is a head wind, so the direction vector for the wind
// needs to be set opposite to the heading the aircraft
// is taking. So, the cos and sin of the heading (psi)
// are negated in the line below.
FGColumnVector3 _vHEAD(-orientation.GetCosEuler(ePsi), -orientation.GetSinEuler(ePsi), 0.);
// Gram-Schmidt process is used to remove the existing head wind component
_vWIND_NED -= DotProduct(_vWIND_NED, _vHEAD) * _vHEAD;
// Which is now replaced by the new value. The input head wind is expected
// in knots, so first convert to fps, which is the internal unit used.
_vWIND_NED += (head * ktstofps) * _vHEAD;
_vt_NED = vUVW_NED + _vWIND_NED;
vt = _vt_NED.Magnitude();
calcAeroAngles(_vt_NED);
}
//******************************************************************************
// Set the vertical wind velocity (in knots). The 'vertical' is taken in the
// local NED frame. The aircraft velocity with respect to the ground is not
// changed but the true airspeed is.
void FGInitialCondition::SetWindDownKtsIC(double wD)
{
const FGMatrix33& Tb2l = orientation.GetTInv();
FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
_vt_NED(eW) = vUVW_NED(eW) + wD;
vt = _vt_NED.Magnitude();
calcAeroAngles(_vt_NED);
}
//******************************************************************************
// Modifies the wind velocity (in knots) while keeping its direction unchanged.
// The vertical component (in local NED frame) is unmodified. The aircraft
// velocity with respect to the ground is not changed but the true airspeed is.
void FGInitialCondition::SetWindMagKtsIC(double mag)
{
const FGMatrix33& Tb2l = orientation.GetTInv();
FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
FGColumnVector3 _vHEAD(_vWIND_NED(eU), _vWIND_NED(eV), 0.);
double windMag = _vHEAD.Magnitude();
if (windMag > 0.001)
_vHEAD *= (mag*ktstofps) / windMag;
else
_vHEAD = {mag*ktstofps, 0., 0.};
_vWIND_NED(eU) = _vHEAD(eU);
_vWIND_NED(eV) = _vHEAD(eV);
_vt_NED = vUVW_NED + _vWIND_NED;
vt = _vt_NED.Magnitude();
calcAeroAngles(_vt_NED);
}
//******************************************************************************
// Modifies the wind direction while keeping its velocity unchanged. The vertical
// component (in local NED frame) is unmodified. The aircraft velocity with
// respect to the ground is not changed but the true airspeed is.
void FGInitialCondition::SetWindDirDegIC(double dir)
{
const FGMatrix33& Tb2l = orientation.GetTInv();
FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
double mag = _vWIND_NED.Magnitude(eU, eV);
FGColumnVector3 _vHEAD(mag*cos(dir*degtorad), mag*sin(dir*degtorad), 0.);
_vWIND_NED(eU) = _vHEAD(eU);
_vWIND_NED(eV) = _vHEAD(eV);
_vt_NED = vUVW_NED + _vWIND_NED;
vt = _vt_NED.Magnitude();
calcAeroAngles(_vt_NED);
}
//******************************************************************************
void FGInitialCondition::SetTerrainElevationFtIC(double elev)
{
double agl = GetAltitudeAGLFtIC();
fdmex->GetInertial()->SetTerrainElevation(elev);
if (lastAltitudeSet == setagl)
SetAltitudeAGLFtIC(agl);
}
//******************************************************************************
double FGInitialCondition::GetAltitudeASLFtIC(void) const
{
return position.GetRadius() - position.GetSeaLevelRadius();
}
//******************************************************************************
double FGInitialCondition::GetAltitudeAGLFtIC(void) const
{
return fdmex->GetInertial()->GetAltitudeAGL(position);
}
//******************************************************************************
double FGInitialCondition::GetTerrainElevationFtIC(void) const
{
FGColumnVector3 normal, v, w;
FGLocation contact;
double a = fdmex->GetInertial()->GetSemimajor();
double b = fdmex->GetInertial()->GetSemiminor();
contact.SetEllipse(a, b);
fdmex->GetInertial()->GetContactPoint(position, contact, normal, v, w);
return contact.GetGeodAltitude();
}
//******************************************************************************
void FGInitialCondition::SetAltitudeAGLFtIC(double agl)
{
double altitudeASL = GetAltitudeASLFtIC();
double pressure = Atmosphere->GetPressure(altitudeASL);
double soundSpeed = Atmosphere->GetSoundSpeed(altitudeASL);
double rho = Atmosphere->GetDensity(altitudeASL);
double rhoSL = Atmosphere->GetDensitySL();
double mach0 = vt / soundSpeed;
double vc0 = VcalibratedFromMach(mach0, pressure);
double ve0 = vt * sqrt(rho/rhoSL);
switch(lastLatitudeSet) {
case setgeod:
fdmex->GetInertial()->SetAltitudeAGL(position, agl);
break;
case setgeoc:
{
double a = fdmex->GetInertial()->GetSemimajor();
double b = fdmex->GetInertial()->GetSemiminor();
double e2 = 1.0-b*b/(a*a);
double tanlat = tan(position.GetLatitude());
double n = e2;
double prev_n = 1.0;
int iter = 0;
double longitude = position.GetLongitude();
double alt = position.GetGeodAltitude();
double h = -2.0*max(a,b);
double geodLat;
while ((fabs(n-prev_n) > 1E-15 || fabs(h-agl) > 1E-10) && iter < 10) {
geodLat = atan(tanlat/(1-n));
position.SetPositionGeodetic(longitude, geodLat, alt);
h = GetAltitudeAGLFtIC();
alt += agl-h;
double sinGeodLat = sin(geodLat);
double N = a/sqrt(1-e2*sinGeodLat*sinGeodLat);
prev_n = n;
n = e2*N/(N+alt);
iter++;
}
}
break;
}
altitudeASL = GetAltitudeASLFtIC();
soundSpeed = Atmosphere->GetSoundSpeed(altitudeASL);
rho = Atmosphere->GetDensity(altitudeASL);
pressure = Atmosphere->GetPressure(altitudeASL);
switch(lastSpeedSet) {
case setvc:
mach0 = MachFromVcalibrated(vc0, pressure);
SetVtrueFpsIC(mach0 * soundSpeed);
break;
case setmach:
SetVtrueFpsIC(mach0 * soundSpeed);
break;
case setve:
SetVtrueFpsIC(ve0 * sqrt(rhoSL/rho));
break;
default: // Make the compiler stop complaining about missing enums
break;
}
lastAltitudeSet = setagl;
}
//******************************************************************************
// Set the altitude SL. If the airspeed has been previously set with parameters
// that are atmosphere dependent (Mach, VCAS, VEAS) then the true airspeed is
// modified to keep the last set speed to its previous value.
void FGInitialCondition::SetAltitudeASLFtIC(double alt)
{
double altitudeASL = GetAltitudeASLFtIC();
double pressure = Atmosphere->GetPressure(altitudeASL);
double soundSpeed = Atmosphere->GetSoundSpeed(altitudeASL);
double rho = Atmosphere->GetDensity(altitudeASL);
double rhoSL = Atmosphere->GetDensitySL();
double mach0 = vt / soundSpeed;
double vc0 = VcalibratedFromMach(mach0, pressure);
double ve0 = vt * sqrt(rho/rhoSL);
switch(lastLatitudeSet) {
case setgeod:
{
// Given an altitude above the mean sea level (or a position radius which
// is the same) and a geodetic latitude, compute the geodetic altitude.
double a = fdmex->GetInertial()->GetSemimajor();
double b = fdmex->GetInertial()->GetSemiminor();
double e2 = 1.0-b*b/(a*a);
double geodLatitude = position.GetGeodLatitudeRad();
double cosGeodLat = cos(geodLatitude);
double sinGeodLat = sin(geodLatitude);
double N = a/sqrt(1-e2*sinGeodLat*sinGeodLat);
double geodAlt = 0.0;
double n = e2;
double prev_n = 1.0;
int iter = 0;
// Use tan or cotan to solve the geodetic altitude to avoid floating point
// exceptions.
if (cosGeodLat > fabs(sinGeodLat)) { // tan() can safely be used.
double tanGeodLat = sinGeodLat/cosGeodLat;
double x0 = N*e2*cosGeodLat;
double x = 0.0;
while (fabs(n-prev_n) > 1E-15 && iter < 10) {
double tanLat = (1-n)*tanGeodLat; // See Stevens & Lewis 1.6-14
double cos2Lat = 1./(1.+tanLat*tanLat);
double slr = b/sqrt(1.-e2*cos2Lat);
double R = slr + alt;
x = R*sqrt(cos2Lat); // OK, cos(latitude) is always positive.
prev_n = n;
n = x0/x;
iter++;
}
geodAlt = x/cosGeodLat-N;
}
else { // better use cotan (i.e. 1./tan())
double cotanGeodLat = cosGeodLat/sinGeodLat;
double z0 = N*e2*sinGeodLat;
double z = 0.0;
while (fabs(n-prev_n) > 1E-15 && iter < 10) {
double cotanLat = cotanGeodLat/(1-n);
double sin2Lat = 1./(1.+cotanLat*cotanLat);
double cos2Lat = 1.-sin2Lat;
double slr = b/sqrt(1.-e2*cos2Lat);
double R = slr + alt;
z = R*sign(cotanLat)*sqrt(sin2Lat);
prev_n = n;
n = z0/(z0+z);
iter++;
}
geodAlt = z/sinGeodLat-N*(1-e2);
}
double longitude = position.GetLongitude();
position.SetPositionGeodetic(longitude, geodLatitude, geodAlt);
}
break;
case setgeoc:
{
double slr = position.GetSeaLevelRadius();
position.SetRadius(slr+alt);
}
break;
}
altitudeASL = position.GetGeodAltitude();
soundSpeed = Atmosphere->GetSoundSpeed(altitudeASL);
rho = Atmosphere->GetDensity(altitudeASL);
pressure = Atmosphere->GetPressure(altitudeASL);
switch(lastSpeedSet) {
case setvc:
mach0 = MachFromVcalibrated(vc0, pressure);
SetVtrueFpsIC(mach0 * soundSpeed);
break;
case setmach:
SetVtrueFpsIC(mach0 * soundSpeed);
break;
case setve:
SetVtrueFpsIC(ve0 * sqrt(rhoSL/rho));
break;
default: // Make the compiler stop complaining about missing enums
break;
}
lastAltitudeSet = setasl;
}
//******************************************************************************
void FGInitialCondition::SetGeodLatitudeRadIC(double geodLatitude)
{
double lon = position.GetLongitude();
lastLatitudeSet = setgeod;
switch (lastAltitudeSet)
{
case setagl:
{
double agl = GetAltitudeAGLFtIC();
position.SetPositionGeodetic(lon, geodLatitude, 0.);
fdmex->GetInertial()->SetAltitudeAGL(position, agl);
}
break;
case setasl:
{
double asl = GetAltitudeASLFtIC();
position.SetPositionGeodetic(lon, geodLatitude, 0.);
SetAltitudeASLFtIC(asl);
}
break;
}
}
//******************************************************************************
void FGInitialCondition::SetLatitudeRadIC(double lat)
{
double altitude;
lastLatitudeSet = setgeoc;
switch(lastAltitudeSet) {
case setagl:
altitude = GetAltitudeAGLFtIC();
position.SetLatitude(lat);
SetAltitudeAGLFtIC(altitude);
break;
default:
altitude = GetAltitudeASLFtIC();
position.SetLatitude(lat);
SetAltitudeASLFtIC(altitude);
break;
}
}
//******************************************************************************
void FGInitialCondition::SetLongitudeRadIC(double lon)
{
double altitude;
switch(lastAltitudeSet) {
case setagl:
altitude = GetAltitudeAGLFtIC();
position.SetLongitude(lon);
SetAltitudeAGLFtIC(altitude);
break;
default:
position.SetLongitude(lon);
break;
}
}
//******************************************************************************
double FGInitialCondition::GetWindDirDegIC(void) const
{
const FGMatrix33& Tb2l = orientation.GetTInv();
FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
return _vWIND_NED(eV) == 0.0 ? 0.0
: atan2(_vWIND_NED(eV), _vWIND_NED(eU))*radtodeg;
}
//******************************************************************************
double FGInitialCondition::GetNEDWindFpsIC(int idx) const
{
const FGMatrix33& Tb2l = orientation.GetTInv();
FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
return _vWIND_NED(idx);
}
//******************************************************************************
double FGInitialCondition::GetWindFpsIC(void) const
{
const FGMatrix33& Tb2l = orientation.GetTInv();
FGColumnVector3 _vt_NED = Tb2l * Tw2b * FGColumnVector3(vt, 0., 0.);
FGColumnVector3 _vWIND_NED = _vt_NED - vUVW_NED;
return _vWIND_NED.Magnitude(eU, eV);
}
//******************************************************************************
double FGInitialCondition::GetBodyWindFpsIC(int idx) const
{
const FGMatrix33& Tl2b = orientation.GetT();
FGColumnVector3 _vt_BODY = Tw2b * FGColumnVector3(vt, 0., 0.);
FGColumnVector3 _vUVW_BODY = Tl2b * vUVW_NED;
FGColumnVector3 _vWIND_BODY = _vt_BODY - _vUVW_BODY;
return _vWIND_BODY(idx);
}
//******************************************************************************
double FGInitialCondition::GetVcalibratedKtsIC(void) const
{
double altitudeASL = GetAltitudeASLFtIC();
double pressure = Atmosphere->GetPressure(altitudeASL);
double soundSpeed = Atmosphere->GetSoundSpeed(altitudeASL);
double mach = vt / soundSpeed;
return fpstokts * VcalibratedFromMach(mach, pressure);
}
//******************************************************************************
double FGInitialCondition::GetVequivalentKtsIC(void) const
{
double altitudeASL = GetAltitudeASLFtIC();
double rho = Atmosphere->GetDensity(altitudeASL);
double rhoSL = Atmosphere->GetDensitySL();
return fpstokts * vt * sqrt(rho/rhoSL);
}
//******************************************************************************
double FGInitialCondition::GetMachIC(void) const
{
double altitudeASL = GetAltitudeASLFtIC();
double soundSpeed = Atmosphere->GetSoundSpeed(altitudeASL);
return vt / soundSpeed;
}
//******************************************************************************
double FGInitialCondition::GetBodyVelFpsIC(int idx) const
{
const FGMatrix33& Tl2b = orientation.GetT();
FGColumnVector3 _vUVW_BODY = Tl2b * vUVW_NED;
return _vUVW_BODY(idx);
}