1 /*******************************************************************************
7 ------------- Copyright (C) 1999 Anthony K. Peden (apeden@earthlink.net) -------------
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
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
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.
23 Further information about the GNU General Public License can also be found on
24 the world wide web at http://www.gnu.org.
28 #include <simgear/compiler.h>
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>
41 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
47 LaRCsimIC::LaRCsimIC(void) {
52 altitude=runway_altitude=hdot=alt_agl=0;
53 latitude_gd=latitude_gc=longitude=0;
56 vnorthwind=veastwind=vdownwind=0;
60 ls_geod_to_geoc( latitude_gd,altitude,&sea_level_radius,&latitude_gc);
65 LaRCsimIC::~LaRCsimIC(void) {}
67 void LaRCsimIC::get_atmosphere(void) {
68 Altitude=altitude; //set LaRCsim variable
69 ls_atmos(Altitude, &density_ratio, &soundspeed, &T, &p);
70 rho=density_ratio*SEA_LEVEL_DENSITY;
73 void LaRCsimIC::SetVcalibratedKtsIC( SCALAR tt) {
76 vt=sqrt(1/density_ratio*vc*vc);
79 void LaRCsimIC::SetVtrueFpsIC( SCALAR tt) {
84 void LaRCsimIC::SetMachIC( SCALAR tt) {
87 lastSpeedSet=lssetmach;
90 void LaRCsimIC::SetVequivalentKtsIC(SCALAR tt) {
93 vt=sqrt(SEA_LEVEL_DENSITY/rho)*ve;
96 void LaRCsimIC::SetClimbRateFpmIC( SCALAR tt) {
97 SetClimbRateFpsIC(tt/60.0);
100 void LaRCsimIC::SetClimbRateFpsIC( SCALAR tt) {
102 cout << "vtg: " << vtg << endl;
104 hdot = tt - vdownwind;
105 gamma=asin(hdot/vtg);
108 cout << "hdot: " << hdot << endl;
109 cout << "gamma: " << gamma*SGD_RADIANS_TO_DEGREES << endl;
110 cout << "vdown: " << vdown << endl;
114 void LaRCsimIC::SetFlightPathAngleRadIC( SCALAR tt) {
119 cout << "hdot: " << hdot << endl;
123 void LaRCsimIC::SetPitchAngleRadIC(SCALAR tt) {
124 if(alt_agl < (DEFAULT_AGL_ALT + 0.1) || vt < 10 )
125 theta=DEFAULT_PITCH_ON_GROUND;
131 void LaRCsimIC::SetUVWFpsIC(SCALAR vu, SCALAR vv, SCALAR vw) {
133 vt=sqrt(u*u+v*v+w*w);
134 lastSpeedSet=lssetuvw;
138 void LaRCsimIC::SetVNEDAirmassFpsIC(SCALAR north, SCALAR east, SCALAR down ) {
139 vnorthwind=north; veastwind=east; vdownwind=down;
140 vw=sqrt(vnorthwind*vnorthwind + veastwind*veastwind + vdownwind*vdownwind);
142 SetClimbRateFpsIC(-1*(vdown+vdownwind));
145 void LaRCsimIC::SetAltitudeFtIC( SCALAR tt) {
146 if(tt > (runway_altitude + DEFAULT_AGL_ALT)) {
149 altitude=runway_altitude + DEFAULT_AGL_ALT;
150 alt_agl=DEFAULT_AGL_ALT;
151 theta=DEFAULT_PITCH_ON_GROUND;
155 //lets try to make sure the user gets what they intended
157 switch(lastSpeedSet) {
166 SetVcalibratedKtsIC(vc*V_TO_KNOTS);
169 SetVequivalentKtsIC(ve*V_TO_KNOTS);
177 void LaRCsimIC::SetAltitudeAGLFtIC( SCALAR tt) {
180 altitude=runway_altitude + alt_agl;
183 void LaRCsimIC::SetRunwayAltitudeFtIC( SCALAR tt) {
185 if(lastAltSet == lssetagl)
186 altitude = runway_altitude + alt_agl;
189 void LaRCsimIC::calcVtfromNED(void) {
190 //take the earth's rotation out of veast first
191 //float veastner = veast- OMEGA_EARTH*sea_level_radius*cos( latitude_gd );
192 float veastner=veast;
193 u = (vnorth - vnorthwind)*cos(theta)*cos(psi) +
194 (veastner - veastwind)*cos(theta)*sin(psi) -
195 (vdown - vdownwind)*sin(theta);
196 v = (vnorth - vnorthwind)*(sin(phi)*sin(theta)*cos(psi) - cos(phi)*sin(psi)) +
197 (veastner - veastwind)*(sin(phi)*sin(theta)*sin(psi) + cos(phi)*cos(psi)) +
198 (vdown - vdownwind)*sin(phi)*cos(theta);
199 w = (vnorth - vnorthwind)*(cos(phi)*sin(theta)*cos(psi) + sin(phi)*sin(psi)) +
200 (veastner - veastwind)*(cos(phi)*sin(theta)*sin(psi) - sin(phi)*cos(psi)) +
201 (vdown - vdownwind)*cos(phi)*cos(theta);
202 vt = sqrt(u*u + v*v + w*w);
205 void LaRCsimIC::calcNEDfromVt(void) {
208 //get the body components of vt
213 //transform them to local axes and add on the wind components
214 vnorth = u*cos(theta)*cos(psi) +
215 v*(sin(phi)*sin(theta)*cos(psi) - cos(phi)*sin(psi)) +
216 w*(cos(phi)*sin(theta)*cos(psi) + sin(phi)*sin(psi)) +
219 //need to account for the earth's rotation here
220 veastner = u*cos(theta)*sin(psi) +
221 v*(sin(phi)*sin(theta)*sin(psi) + cos(phi)*cos(psi)) +
222 w*(cos(phi)*sin(theta)*sin(psi) - sin(phi)*cos(psi)) +
225 //veast = veastner + OMEGA_EARTH*sea_level_radius*cos( latitude_gd );
227 vdown = u*sin(theta) +
228 v*sin(phi)*cos(theta) +
229 w*cos(phi)*cos(theta) +
233 void LaRCsimIC::SetVNEDFpsIC( SCALAR north, SCALAR east, SCALAR down) {
237 SetClimbRateFpsIC(-1*vdown);
238 lastSpeedSet=lssetned;
242 void LaRCsimIC::SetLatitudeGDRadIC(SCALAR tt) {
244 ls_geod_to_geoc(latitude_gd,altitude,&sea_level_radius, &latitude_gc);
247 bool LaRCsimIC::getAlpha(void) {
249 float guess=theta-gamma;
253 sfunc=&LaRCsimIC::GammaEqOfAlpha;
254 if(findInterval(0,guess)){
263 bool LaRCsimIC::getTheta(void) {
265 float guess=alpha+gamma;
268 sfunc=&LaRCsimIC::GammaEqOfTheta;
269 if(findInterval(0,guess)){
279 SCALAR LaRCsimIC::GammaEqOfTheta( SCALAR theta_arg) {
282 a=cos(alpha)*cos(beta)*sin(theta_arg);
283 b=sin(beta)*sin(phi);
284 c=sin(alpha)*cos(beta)*cos(phi);
285 return sin(gamma)-a+(b+c)*cos(theta_arg);
288 SCALAR LaRCsimIC::GammaEqOfAlpha( SCALAR alpha_arg) {
291 a=cos(alpha_arg)*cos(beta)*sin(theta);
292 b=sin(beta)*sin(phi);
293 c=sin(alpha_arg)*cos(beta)*cos(phi);
294 return sin(gamma)-a+(b+c)*cos(theta);
302 bool LaRCsimIC::findInterval( SCALAR x,SCALAR guess) {
303 //void find_interval(inter_params &ip,eqfunc f,float y,float constant, int &flag){
307 float flo,fhi,fguess;
310 fguess=(this->*sfunc)(guess)-x;
316 if(lo < xmin) lo=xmin;
317 if(hi > xmax) hi=xmax;
319 flo=(this->*sfunc)(lo)-x;
320 fhi=(this->*sfunc)(hi)-x;
321 if(flo*fhi <=0) { //found interval with root
323 if(flo*fguess <= 0) { //narrow interval down a bit
324 hi=lo+step; //to pass solver interval that is as
327 else if(fhi*fguess <= 0) {
331 //cout << "findInterval: i=" << i << " Lo= " << lo << " Hi= " << hi << endl;
333 while((found == 0) && (i <= 100));
342 bool LaRCsimIC::solve( SCALAR *y,SCALAR x) {
343 float x1,x2,x3,f1,f2,f3,d,d0;
345 float const relax =0.9;
353 f1=(this->*sfunc)(x1)-x;
354 f3=(this->*sfunc)(x3)-x;
359 while ((fabs(d) > eps) && (i < 100)) {
361 x2=x1-d*d0*f1/(f3-f1);
363 f2=(this->*sfunc)(x2)-x;
364 //cout << "solve x1,x2,x3: " << x1 << "," << x2 << "," << x3 << endl;
365 //cout << " " << f1 << "," << f2 << "," << f3 << endl;
367 if(fabs(f2) <= 0.001) {
369 } else if(f1*f2 <= 0.0) {
373 } else if(f2*f3 <= 0) {
386 //cout << "Success= " << success << " Vcas: " << vcas*V_TO_KNOTS << " Mach: " << x2 << endl;