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) {
103 hdot = tt - vdownwind;
104 gamma=asin(hdot/vtg);
110 void LaRCsimIC::SetFlightPathAngleRadIC( SCALAR tt) {
118 void LaRCsimIC::SetPitchAngleRadIC(SCALAR tt) {
119 if(alt_agl < (DEFAULT_AGL_ALT + 0.1) || vt < 10 )
120 theta=DEFAULT_PITCH_ON_GROUND;
126 void LaRCsimIC::SetUVWFpsIC(SCALAR vu, SCALAR vv, SCALAR vw) {
128 vt=sqrt(u*u+v*v+w*w);
129 lastSpeedSet=lssetuvw;
133 void LaRCsimIC::SetVNEDAirmassFpsIC(SCALAR north, SCALAR east, SCALAR down ) {
134 vnorthwind=north; veastwind=east; vdownwind=down;
135 vw=sqrt(vnorthwind*vnorthwind + veastwind*veastwind + vdownwind*vdownwind);
137 SetClimbRateFpsIC(-1*(vdown+vdownwind));
140 void LaRCsimIC::SetAltitudeFtIC( SCALAR tt) {
141 if(tt > (runway_altitude + DEFAULT_AGL_ALT)) {
144 altitude=runway_altitude + DEFAULT_AGL_ALT;
145 alt_agl=DEFAULT_AGL_ALT;
146 theta=DEFAULT_PITCH_ON_GROUND;
150 //lets try to make sure the user gets what they intended
152 switch(lastSpeedSet) {
161 SetVcalibratedKtsIC(vc*V_TO_KNOTS);
164 SetVequivalentKtsIC(ve*V_TO_KNOTS);
172 void LaRCsimIC::SetAltitudeAGLFtIC( SCALAR tt) {
175 altitude=runway_altitude + alt_agl;
178 void LaRCsimIC::SetRunwayAltitudeFtIC( SCALAR tt) {
180 if(lastAltSet == lssetagl)
181 altitude = runway_altitude + alt_agl;
184 void LaRCsimIC::calcVtfromNED(void) {
185 //take the earth's rotation out of veast first
186 //float veastner = veast- OMEGA_EARTH*sea_level_radius*cos( latitude_gd );
187 float veastner=veast;
188 u = (vnorth - vnorthwind)*cos(theta)*cos(psi) +
189 (veastner - veastwind)*cos(theta)*sin(psi) -
190 (vdown - vdownwind)*sin(theta);
191 v = (vnorth - vnorthwind)*(sin(phi)*sin(theta)*cos(psi) - cos(phi)*sin(psi)) +
192 (veastner - veastwind)*(sin(phi)*sin(theta)*sin(psi) + cos(phi)*cos(psi)) +
193 (vdown - vdownwind)*sin(phi)*cos(theta);
194 w = (vnorth - vnorthwind)*(cos(phi)*sin(theta)*cos(psi) + sin(phi)*sin(psi)) +
195 (veastner - veastwind)*(cos(phi)*sin(theta)*sin(psi) - sin(phi)*cos(psi)) +
196 (vdown - vdownwind)*cos(phi)*cos(theta);
197 vt = sqrt(u*u + v*v + w*w);
200 void LaRCsimIC::calcNEDfromVt(void) {
203 //get the body components of vt
208 //transform them to local axes and add on the wind components
209 vnorth = u*cos(theta)*cos(psi) +
210 v*(sin(phi)*sin(theta)*cos(psi) - cos(phi)*sin(psi)) +
211 w*(cos(phi)*sin(theta)*cos(psi) + sin(phi)*sin(psi)) +
214 //need to account for the earth's rotation here
215 veastner = u*cos(theta)*sin(psi) +
216 v*(sin(phi)*sin(theta)*sin(psi) + cos(phi)*cos(psi)) +
217 w*(cos(phi)*sin(theta)*sin(psi) - sin(phi)*cos(psi)) +
220 //veast = veastner + OMEGA_EARTH*sea_level_radius*cos( latitude_gd );
222 vdown = u*sin(theta) +
223 v*sin(phi)*cos(theta) +
224 w*cos(phi)*cos(theta) +
228 void LaRCsimIC::SetVNEDFpsIC( SCALAR north, SCALAR east, SCALAR down) {
232 SetClimbRateFpsIC(-1*vdown);
233 lastSpeedSet=lssetned;
237 void LaRCsimIC::SetLatitudeGDRadIC(SCALAR tt) {
239 ls_geod_to_geoc(latitude_gd,altitude,&sea_level_radius, &latitude_gc);
242 bool LaRCsimIC::getAlpha(void) {
244 float guess=theta-gamma;
248 sfunc=&LaRCsimIC::GammaEqOfAlpha;
249 if(findInterval(0,guess)){
258 bool LaRCsimIC::getTheta(void) {
260 float guess=alpha+gamma;
263 sfunc=&LaRCsimIC::GammaEqOfTheta;
264 if(findInterval(0,guess)){
274 SCALAR LaRCsimIC::GammaEqOfTheta( SCALAR theta_arg) {
277 a=cos(alpha)*cos(beta)*sin(theta_arg);
278 b=sin(beta)*sin(phi);
279 c=sin(alpha)*cos(beta)*cos(phi);
280 return sin(gamma)-a+(b+c)*cos(theta_arg);
283 SCALAR LaRCsimIC::GammaEqOfAlpha( SCALAR alpha_arg) {
286 a=cos(alpha_arg)*cos(beta)*sin(theta);
287 b=sin(beta)*sin(phi);
288 c=sin(alpha_arg)*cos(beta)*cos(phi);
289 return sin(gamma)-a+(b+c)*cos(theta);
297 bool LaRCsimIC::findInterval( SCALAR x,SCALAR guess) {
298 //void find_interval(inter_params &ip,eqfunc f,float y,float constant, int &flag){
302 float flo,fhi,fguess;
305 fguess=(this->*sfunc)(guess)-x;
311 if(lo < xmin) lo=xmin;
312 if(hi > xmax) hi=xmax;
314 flo=(this->*sfunc)(lo)-x;
315 fhi=(this->*sfunc)(hi)-x;
316 if(flo*fhi <=0) { //found interval with root
318 if(flo*fguess <= 0) { //narrow interval down a bit
319 hi=lo+step; //to pass solver interval that is as
322 else if(fhi*fguess <= 0) {
326 //cout << "findInterval: i=" << i << " Lo= " << lo << " Hi= " << hi << endl;
328 while((found == 0) && (i <= 100));
337 bool LaRCsimIC::solve( SCALAR *y,SCALAR x) {
338 float x1,x2,x3,f1,f2,f3,d,d0;
340 float const relax =0.9;
348 f1=(this->*sfunc)(x1)-x;
349 f3=(this->*sfunc)(x3)-x;
354 while ((fabs(d) > eps) && (i < 100)) {
356 x2=x1-d*d0*f1/(f3-f1);
358 f2=(this->*sfunc)(x2)-x;
359 //cout << "solve x1,x2,x3: " << x1 << "," << x2 << "," << x3 << endl;
360 //cout << " " << f1 << "," << f2 << "," << f3 << endl;
362 if(fabs(f2) <= 0.001) {
364 } else if(f1*f2 <= 0.0) {
368 } else if(f2*f3 <= 0) {
381 //cout << "Success= " << success << " Vcas: " << vcas*V_TO_KNOTS << " Mach: " << x2 << endl;