]> git.mxchange.org Git - flightgear.git/blob - src/FDM/LaRCsimIC.cxx
2bc6caa05ce98b96d6c184638c19680a65f42b09
[flightgear.git] / src / FDM / LaRCsimIC.cxx
1 /*******************************************************************************
2  
3  Header:       LaRCsimIC.cxx
4  Author:       Tony Peden
5  Date started: 10/9/99
6  
7  ------------- Copyright (C) 1999  Anthony K. Peden (apeden@earthlink.net) -------------
8  
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  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 General Public License for more
17  details.
18  
19  You should have received a copy of the GNU General Public License along with
20  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21  Place - Suite 330, Boston, MA  02111-1307, USA.
22  
23  Further information about the GNU General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25 */ 
26  
27
28 #include <simgear/compiler.h>
29
30 #include <math.h>
31 #include STL_IOSTREAM
32
33 #include "FDM/LaRCsimIC.hxx"
34 #include <FDM/LaRCsim/ls_cockpit.h>
35 #include <FDM/LaRCsim/ls_generic.h>
36 #include <FDM/LaRCsim/ls_interface.h>
37 #include <FDM/LaRCsim/atmos_62.h>
38 #include <FDM/LaRCsim/ls_constants.h>
39 #include <FDM/LaRCsim/ls_geodesy.h>
40
41 SG_USING_STD(cout);
42 SG_USING_STD(endl);
43
44
45 LaRCsimIC::LaRCsimIC(void) {
46   vt=vtg=vw=vc=ve=0;
47   mach=0;
48   alpha=beta=gamma=0;
49   theta=phi=psi=0;
50   altitude=runway_altitude=hdot=alt_agl=0;
51   latitude_gd=latitude_gc=longitude=0;
52   u=v=w=0;  
53   vnorth=veast=vdown=0;
54   vnorthwind=veastwind=vdownwind=0;
55   lastSpeedSet=lssetvt;
56   lastAltSet=lssetasl;
57   get_atmosphere();
58   ls_geod_to_geoc( latitude_gd,altitude,&sea_level_radius,&latitude_gc); 
59   
60 }
61
62
63 LaRCsimIC::~LaRCsimIC(void) {}
64
65 void LaRCsimIC::get_atmosphere(void) {
66   Altitude=altitude; //set LaRCsim variable
67   ls_atmos(Altitude, &density_ratio, &soundspeed, &T, &p);
68   rho=density_ratio*SEA_LEVEL_DENSITY;
69 }  
70
71 void LaRCsimIC::SetVcalibratedKtsIC( SCALAR tt) {
72     vc=tt*KTS_TO_FPS;
73     lastSpeedSet=lssetvc;
74     vt=sqrt(1/density_ratio*vc*vc);
75 }
76
77 void LaRCsimIC::SetVtrueFpsIC( SCALAR tt) {
78   vt=tt;
79   lastSpeedSet=lssetvt;
80 }
81
82 void LaRCsimIC::SetMachIC( SCALAR tt) {
83   mach=tt;
84   vt=mach*soundspeed;
85   lastSpeedSet=lssetmach;
86 }
87
88 void LaRCsimIC::SetVequivalentKtsIC(SCALAR tt) {
89   ve=tt*KTS_TO_FPS;
90   lastSpeedSet=lssetve;
91   vt=sqrt(SEA_LEVEL_DENSITY/rho)*ve;
92 }  
93
94 void LaRCsimIC::SetClimbRateFpmIC( SCALAR tt) {
95   SetClimbRateFpsIC(tt/60.0);
96 }
97
98 void LaRCsimIC::SetClimbRateFpsIC( SCALAR tt) {
99   vtg=vt+vw;
100   if(vtg > 0.1) {
101     hdot = tt - vdownwind;
102     gamma=asin(hdot/vtg);
103     getTheta();
104     vdown=-1*hdot;
105   }
106 }
107
108 void LaRCsimIC::SetFlightPathAngleRadIC( SCALAR tt) {
109   gamma=tt;
110   getTheta();
111   vtg=vt+vw;
112   hdot=vtg*sin(tt);
113   vdown=-1*hdot;
114 }
115
116 void LaRCsimIC::SetPitchAngleRadIC(SCALAR tt) { 
117   if(alt_agl < (DEFAULT_AGL_ALT + 0.1) || vt < 10 ) 
118     theta=DEFAULT_PITCH_ON_GROUND; 
119   else
120     theta=tt;  
121   getAlpha();
122 }
123
124 void LaRCsimIC::SetUVWFpsIC(SCALAR vu, SCALAR vv, SCALAR vw) {
125   u=vu; v=vv; w=vw;
126   vt=sqrt(u*u+v*v+w*w);
127   lastSpeedSet=lssetuvw;
128 }
129
130   
131 void LaRCsimIC::SetVNEDAirmassFpsIC(SCALAR north, SCALAR east, SCALAR down ) {
132   vnorthwind=north; veastwind=east; vdownwind=down;
133   vw=sqrt(vnorthwind*vnorthwind + veastwind*veastwind + vdownwind*vdownwind);
134   vtg=vt+vw;
135   SetClimbRateFpsIC(-1*(vdown+vdownwind));
136 }  
137
138 void LaRCsimIC::SetAltitudeFtIC( SCALAR tt) {
139   if(tt > (runway_altitude + DEFAULT_AGL_ALT)) {
140     altitude=tt;
141   } else {
142     altitude=runway_altitude + DEFAULT_AGL_ALT;
143     alt_agl=DEFAULT_AGL_ALT;
144     theta=DEFAULT_PITCH_ON_GROUND; 
145   }  
146   lastAltSet=lssetasl;
147   get_atmosphere();
148   //lets try to make sure the user gets what they intended
149
150   switch(lastSpeedSet) {
151   case lssetned:
152     calcVtfromNED();
153     break;  
154   case lssetuvw:
155   case lssetvt:
156     SetVtrueFpsIC(vt);
157     break;
158   case lssetvc:
159     SetVcalibratedKtsIC(vc*V_TO_KNOTS);
160     break;
161   case lssetve:
162     SetVequivalentKtsIC(ve*V_TO_KNOTS);
163     break;
164   case lssetmach:
165     SetMachIC(mach);
166     break;
167   }
168 }
169
170 void LaRCsimIC::SetAltitudeAGLFtIC( SCALAR tt) {
171   alt_agl=tt;
172   lastAltSet=lssetagl;
173   altitude=runway_altitude + alt_agl;
174 }
175
176 void LaRCsimIC::SetRunwayAltitudeFtIC( SCALAR tt) {
177   runway_altitude=tt;
178   if(lastAltSet == lssetagl) 
179       altitude = runway_altitude + alt_agl;
180 }  
181         
182 void LaRCsimIC::calcVtfromNED(void) {
183   //take the earth's rotation out of veast first
184   //float veastner = veast- OMEGA_EARTH*sea_level_radius*cos( latitude_gd );
185   float veastner=veast;
186   u = (vnorth - vnorthwind)*cos(theta)*cos(psi) + 
187       (veastner - veastwind)*cos(theta)*sin(psi) - 
188       (vdown - vdownwind)*sin(theta);
189   v = (vnorth - vnorthwind)*(sin(phi)*sin(theta)*cos(psi) - cos(phi)*sin(psi)) +
190       (veastner - veastwind)*(sin(phi)*sin(theta)*sin(psi) + cos(phi)*cos(psi)) +
191       (vdown - vdownwind)*sin(phi)*cos(theta);
192   w = (vnorth - vnorthwind)*(cos(phi)*sin(theta)*cos(psi) + sin(phi)*sin(psi)) +
193       (veastner - veastwind)*(cos(phi)*sin(theta)*sin(psi) - sin(phi)*cos(psi)) +
194       (vdown - vdownwind)*cos(phi)*cos(theta);
195   vt = sqrt(u*u + v*v + w*w);
196
197
198 void LaRCsimIC::calcNEDfromVt(void) {
199   float veastner;
200   
201   //get the body components of vt
202   u = GetUBodyFpsIC();
203   v = GetVBodyFpsIC();   
204   w = GetWBodyFpsIC();
205   
206   //transform them to local axes and add on the wind components
207   vnorth = u*cos(theta)*cos(psi) +
208            v*(sin(phi)*sin(theta)*cos(psi) - cos(phi)*sin(psi)) +
209            w*(cos(phi)*sin(theta)*cos(psi) + sin(phi)*sin(psi)) +
210            vnorthwind;
211   
212   //need to account for the earth's rotation here
213   veastner = u*cos(theta)*sin(psi) +
214              v*(sin(phi)*sin(theta)*sin(psi) + cos(phi)*cos(psi)) +
215              w*(cos(phi)*sin(theta)*sin(psi) - sin(phi)*cos(psi)) +
216              veastwind;
217   veast = veastner;
218   //veast = veastner + OMEGA_EARTH*sea_level_radius*cos( latitude_gd );  
219   
220   vdown =  u*sin(theta) +
221            v*sin(phi)*cos(theta) +
222            w*cos(phi)*cos(theta) +
223            vdownwind;
224 }           
225
226 void LaRCsimIC::SetVNEDFpsIC( SCALAR north, SCALAR east, SCALAR down) {
227   vnorth=north;
228   veast=east;
229   vdown=down;
230   SetClimbRateFpsIC(-1*vdown);
231   lastSpeedSet=lssetned;
232   calcVtfromNED();
233 }        
234   
235 void LaRCsimIC::SetLatitudeGDRadIC(SCALAR tt) {
236   latitude_gd=tt;
237   ls_geod_to_geoc(latitude_gd,altitude,&sea_level_radius, &latitude_gc);
238 }
239   
240 bool LaRCsimIC::getAlpha(void) {
241   bool result=false;
242   float guess=theta-gamma;
243   xlo=xhi=0;
244   xmin=-89;
245   xmax=89;
246   sfunc=&LaRCsimIC::GammaEqOfAlpha;
247   if(findInterval(0,guess)){
248     if(solve(&alpha,0)){
249       result=true;
250     }
251   }
252   return result;
253 }      
254
255       
256 bool LaRCsimIC::getTheta(void) {
257   bool result=false;
258   float guess=alpha+gamma;
259   xlo=xhi=0;
260   xmin=-89;xmax=89;
261   sfunc=&LaRCsimIC::GammaEqOfTheta;
262   if(findInterval(0,guess)){
263     if(solve(&theta,0)){
264       result=true;
265     }
266   }
267   return result;
268 }      
269   
270
271
272 SCALAR LaRCsimIC::GammaEqOfTheta( SCALAR theta_arg) {
273   SCALAR a,b,c;
274   
275   a=cos(alpha)*cos(beta)*sin(theta_arg);
276   b=sin(beta)*sin(phi);
277   c=sin(alpha)*cos(beta)*cos(phi);
278   return sin(gamma)-a+(b+c)*cos(theta_arg);
279 }
280
281 SCALAR LaRCsimIC::GammaEqOfAlpha( SCALAR alpha_arg) {
282   float a,b,c;
283   
284   a=cos(alpha_arg)*cos(beta)*sin(theta);
285   b=sin(beta)*sin(phi);
286   c=sin(alpha_arg)*cos(beta)*cos(phi);
287   return sin(gamma)-a+(b+c)*cos(theta);
288 }
289
290   
291
292
293
294
295 bool LaRCsimIC::findInterval( SCALAR x,SCALAR guess) {
296   //void find_interval(inter_params &ip,eqfunc f,float y,float constant, int &flag){
297
298   int i=0;
299   bool found=false;
300   float flo,fhi,fguess;
301   float lo,hi,step;
302   step=0.1;
303   fguess=(this->*sfunc)(guess)-x;
304   lo=hi=guess;
305   do {
306     step=2*step;
307     lo-=step;
308     hi+=step;
309     if(lo < xmin) lo=xmin;
310     if(hi > xmax) hi=xmax;
311     i++;
312     flo=(this->*sfunc)(lo)-x;
313     fhi=(this->*sfunc)(hi)-x;
314     if(flo*fhi <=0) {  //found interval with root
315       found=true;
316       if(flo*fguess <= 0) {  //narrow interval down a bit
317         hi=lo+step;    //to pass solver interval that is as
318         //small as possible
319       }
320       else if(fhi*fguess <= 0) {
321         lo=hi-step;
322       }
323     }
324     //cout << "findInterval: i=" << i << " Lo= " << lo << " Hi= " << hi << endl;
325   }
326   while((found == 0) && (i <= 100));
327   xlo=lo;
328   xhi=hi;
329   return found;
330 }
331
332
333
334
335 bool LaRCsimIC::solve( SCALAR *y,SCALAR x) {  
336   float x1,x2,x3,f1,f2,f3,d,d0;
337   float eps=1E-5;
338   float const relax =0.9;
339   int i;
340   bool success=false;
341   
342    //initializations
343   d=1;
344   
345     x1=xlo;x3=xhi;
346     f1=(this->*sfunc)(x1)-x;
347     f3=(this->*sfunc)(x3)-x;
348     d0=fabs(x3-x1);
349   
350     //iterations
351     i=0;
352     while ((fabs(d) > eps) && (i < 100)) {
353       d=(x3-x1)/d0;
354       x2=x1-d*d0*f1/(f3-f1);
355       
356       f2=(this->*sfunc)(x2)-x;
357       //cout << "solve x1,x2,x3: " << x1 << "," << x2 << "," << x3 << endl;
358       //cout << "                " << f1 << "," << f2 << "," << f3 << endl;
359
360       if(fabs(f2) <= 0.001) {
361         x1=x3=x2;
362       } else if(f1*f2 <= 0.0) {
363         x3=x2;
364         f3=f2;
365         f1=relax*f1;
366       } else if(f2*f3 <= 0) {
367         x1=x2;
368         f1=f2;
369         f3=relax*f3;
370       }
371       //cout << i << endl;
372       i++;
373     }//end while
374     if(i < 100) {
375       success=true;
376       *y=x2;
377     }
378
379   //cout << "Success= " << success << " Vcas: " << vcas*V_TO_KNOTS << " Mach: " << x2 << endl;
380   return success;
381 }