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