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>
45 LaRCsimIC::LaRCsimIC(void) {
50 altitude=runway_altitude=hdot=alt_agl=0;
51 latitude_gd=latitude_gc=longitude=0;
54 vnorthwind=veastwind=vdownwind=0;
58 ls_geod_to_geoc( latitude_gd,altitude,&sea_level_radius,&latitude_gc);
63 LaRCsimIC::~LaRCsimIC(void) {}
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;
71 void LaRCsimIC::SetVcalibratedKtsIC( SCALAR tt) {
74 vt=sqrt(1/density_ratio*vc*vc);
77 void LaRCsimIC::SetVtrueFpsIC( SCALAR tt) {
82 void LaRCsimIC::SetMachIC( SCALAR tt) {
85 lastSpeedSet=lssetmach;
88 void LaRCsimIC::SetVequivalentKtsIC(SCALAR tt) {
91 vt=sqrt(SEA_LEVEL_DENSITY/rho)*ve;
94 void LaRCsimIC::SetClimbRateFpmIC( SCALAR tt) {
95 SetClimbRateFpsIC(tt/60.0);
98 void LaRCsimIC::SetClimbRateFpsIC( SCALAR tt) {
101 hdot = tt - vdownwind;
102 gamma=asin(hdot/vtg);
108 void LaRCsimIC::SetFlightPathAngleRadIC( SCALAR tt) {
116 void LaRCsimIC::SetPitchAngleRadIC(SCALAR tt) {
117 if(alt_agl < (DEFAULT_AGL_ALT + 0.1) || vt < 10 )
118 theta=DEFAULT_PITCH_ON_GROUND;
124 void LaRCsimIC::SetUVWFpsIC(SCALAR vu, SCALAR vv, SCALAR vw) {
126 vt=sqrt(u*u+v*v+w*w);
127 lastSpeedSet=lssetuvw;
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);
135 SetClimbRateFpsIC(-1*(vdown+vdownwind));
138 void LaRCsimIC::SetAltitudeFtIC( SCALAR tt) {
139 if(tt > (runway_altitude + DEFAULT_AGL_ALT)) {
142 altitude=runway_altitude + DEFAULT_AGL_ALT;
143 alt_agl=DEFAULT_AGL_ALT;
144 theta=DEFAULT_PITCH_ON_GROUND;
148 //lets try to make sure the user gets what they intended
150 switch(lastSpeedSet) {
159 SetVcalibratedKtsIC(vc*V_TO_KNOTS);
162 SetVequivalentKtsIC(ve*V_TO_KNOTS);
170 void LaRCsimIC::SetAltitudeAGLFtIC( SCALAR tt) {
173 altitude=runway_altitude + alt_agl;
176 void LaRCsimIC::SetRunwayAltitudeFtIC( SCALAR tt) {
178 if(lastAltSet == lssetagl)
179 altitude = runway_altitude + alt_agl;
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);
198 void LaRCsimIC::calcNEDfromVt(void) {
201 //get the body components of vt
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)) +
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)) +
218 //veast = veastner + OMEGA_EARTH*sea_level_radius*cos( latitude_gd );
220 vdown = u*sin(theta) +
221 v*sin(phi)*cos(theta) +
222 w*cos(phi)*cos(theta) +
226 void LaRCsimIC::SetVNEDFpsIC( SCALAR north, SCALAR east, SCALAR down) {
230 SetClimbRateFpsIC(-1*vdown);
231 lastSpeedSet=lssetned;
235 void LaRCsimIC::SetLatitudeGDRadIC(SCALAR tt) {
237 ls_geod_to_geoc(latitude_gd,altitude,&sea_level_radius, &latitude_gc);
240 bool LaRCsimIC::getAlpha(void) {
242 float guess=theta-gamma;
246 sfunc=&LaRCsimIC::GammaEqOfAlpha;
247 if(findInterval(0,guess)){
256 bool LaRCsimIC::getTheta(void) {
258 float guess=alpha+gamma;
261 sfunc=&LaRCsimIC::GammaEqOfTheta;
262 if(findInterval(0,guess)){
272 SCALAR LaRCsimIC::GammaEqOfTheta( SCALAR theta_arg) {
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);
281 SCALAR LaRCsimIC::GammaEqOfAlpha( SCALAR alpha_arg) {
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);
295 bool LaRCsimIC::findInterval( SCALAR x,SCALAR guess) {
296 //void find_interval(inter_params &ip,eqfunc f,float y,float constant, int &flag){
300 float flo,fhi,fguess;
303 fguess=(this->*sfunc)(guess)-x;
309 if(lo < xmin) lo=xmin;
310 if(hi > xmax) hi=xmax;
312 flo=(this->*sfunc)(lo)-x;
313 fhi=(this->*sfunc)(hi)-x;
314 if(flo*fhi <=0) { //found interval with root
316 if(flo*fguess <= 0) { //narrow interval down a bit
317 hi=lo+step; //to pass solver interval that is as
320 else if(fhi*fguess <= 0) {
324 //cout << "findInterval: i=" << i << " Lo= " << lo << " Hi= " << hi << endl;
326 while((found == 0) && (i <= 100));
335 bool LaRCsimIC::solve( SCALAR *y,SCALAR x) {
336 float x1,x2,x3,f1,f2,f3,d,d0;
338 float const relax =0.9;
346 f1=(this->*sfunc)(x1)-x;
347 f3=(this->*sfunc)(x3)-x;
352 while ((fabs(d) > eps) && (i < 100)) {
354 x2=x1-d*d0*f1/(f3-f1);
356 f2=(this->*sfunc)(x2)-x;
357 //cout << "solve x1,x2,x3: " << x1 << "," << x2 << "," << x3 << endl;
358 //cout << " " << f1 << "," << f2 << "," << f3 << endl;
360 if(fabs(f2) <= 0.001) {
362 } else if(f1*f2 <= 0.0) {
366 } else if(f2*f3 <= 0) {
379 //cout << "Success= " << success << " Vcas: " << vcas*V_TO_KNOTS << " Mach: " << x2 << endl;