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) {
100 cout << "vtg: " << vtg << endl;
102 hdot = tt - vdownwind;
103 gamma=asin(hdot/vtg);
106 cout << "hdot: " << hdot << endl;
107 cout << "gamma: " << gamma*RAD_TO_DEG << endl;
108 cout << "vdown: " << vdown << endl;
112 void LaRCsimIC::SetFlightPathAngleRadIC( SCALAR tt) {
117 cout << "hdot: " << hdot << endl;
121 void LaRCsimIC::SetPitchAngleRadIC(SCALAR tt) {
122 if(alt_agl < (DEFAULT_AGL_ALT + 0.1) || vt < 10 )
123 theta=DEFAULT_PITCH_ON_GROUND;
129 void LaRCsimIC::SetUVWFpsIC(SCALAR vu, SCALAR vv, SCALAR vw) {
131 vt=sqrt(u*u+v*v+w*w);
132 lastSpeedSet=lssetuvw;
136 void LaRCsimIC::SetVNEDAirmassFpsIC(SCALAR north, SCALAR east, SCALAR down ) {
137 vnorthwind=north; veastwind=east; vdownwind=down;
138 vw=sqrt(vnorthwind*vnorthwind + veastwind*veastwind + vdownwind*vdownwind);
140 SetClimbRateFpsIC(-1*(vdown+vdownwind));
143 void LaRCsimIC::SetAltitudeFtIC( SCALAR tt) {
144 if(tt > (runway_altitude + DEFAULT_AGL_ALT)) {
147 altitude=runway_altitude + DEFAULT_AGL_ALT;
148 alt_agl=DEFAULT_AGL_ALT;
149 theta=DEFAULT_PITCH_ON_GROUND;
153 //lets try to make sure the user gets what they intended
155 switch(lastSpeedSet) {
164 SetVcalibratedKtsIC(vc*V_TO_KNOTS);
167 SetVequivalentKtsIC(ve*V_TO_KNOTS);
175 void LaRCsimIC::SetAltitudeAGLFtIC( SCALAR tt) {
178 altitude=runway_altitude + alt_agl;
181 void LaRCsimIC::SetRunwayAltitudeFtIC( SCALAR tt) {
183 if(lastAltSet == lssetagl)
184 altitude = runway_altitude + alt_agl;
187 void LaRCsimIC::calcVtfromNED(void) {
188 //take the earth's rotation out of veast first
189 //float veastner = veast- OMEGA_EARTH*sea_level_radius*cos( latitude_gd );
190 float veastner=veast;
191 u = (vnorth - vnorthwind)*cos(theta)*cos(psi) +
192 (veastner - veastwind)*cos(theta)*sin(psi) -
193 (vdown - vdownwind)*sin(theta);
194 v = (vnorth - vnorthwind)*(sin(phi)*sin(theta)*cos(psi) - cos(phi)*sin(psi)) +
195 (veastner - veastwind)*(sin(phi)*sin(theta)*sin(psi) + cos(phi)*cos(psi)) +
196 (vdown - vdownwind)*sin(phi)*cos(theta);
197 w = (vnorth - vnorthwind)*(cos(phi)*sin(theta)*cos(psi) + sin(phi)*sin(psi)) +
198 (veastner - veastwind)*(cos(phi)*sin(theta)*sin(psi) - sin(phi)*cos(psi)) +
199 (vdown - vdownwind)*cos(phi)*cos(theta);
200 vt = sqrt(u*u + v*v + w*w);
203 void LaRCsimIC::calcNEDfromVt(void) {
206 //get the body components of vt
211 //transform them to local axes and add on the wind components
212 vnorth = u*cos(theta)*cos(psi) +
213 v*(sin(phi)*sin(theta)*cos(psi) - cos(phi)*sin(psi)) +
214 w*(cos(phi)*sin(theta)*cos(psi) + sin(phi)*sin(psi)) +
217 //need to account for the earth's rotation here
218 veastner = u*cos(theta)*sin(psi) +
219 v*(sin(phi)*sin(theta)*sin(psi) + cos(phi)*cos(psi)) +
220 w*(cos(phi)*sin(theta)*sin(psi) - sin(phi)*cos(psi)) +
223 //veast = veastner + OMEGA_EARTH*sea_level_radius*cos( latitude_gd );
225 vdown = u*sin(theta) +
226 v*sin(phi)*cos(theta) +
227 w*cos(phi)*cos(theta) +
231 void LaRCsimIC::SetVNEDFpsIC( SCALAR north, SCALAR east, SCALAR down) {
235 SetClimbRateFpsIC(-1*vdown);
236 lastSpeedSet=lssetned;
240 void LaRCsimIC::SetLatitudeGDRadIC(SCALAR tt) {
242 ls_geod_to_geoc(latitude_gd,altitude,&sea_level_radius, &latitude_gc);
245 bool LaRCsimIC::getAlpha(void) {
247 float guess=theta-gamma;
251 sfunc=&LaRCsimIC::GammaEqOfAlpha;
252 if(findInterval(0,guess)){
261 bool LaRCsimIC::getTheta(void) {
263 float guess=alpha+gamma;
266 sfunc=&LaRCsimIC::GammaEqOfTheta;
267 if(findInterval(0,guess)){
277 SCALAR LaRCsimIC::GammaEqOfTheta( SCALAR theta_arg) {
280 a=cos(alpha)*cos(beta)*sin(theta_arg);
281 b=sin(beta)*sin(phi);
282 c=sin(alpha)*cos(beta)*cos(phi);
283 return sin(gamma)-a+(b+c)*cos(theta_arg);
286 SCALAR LaRCsimIC::GammaEqOfAlpha( SCALAR alpha_arg) {
289 a=cos(alpha_arg)*cos(beta)*sin(theta);
290 b=sin(beta)*sin(phi);
291 c=sin(alpha_arg)*cos(beta)*cos(phi);
292 return sin(gamma)-a+(b+c)*cos(theta);
300 bool LaRCsimIC::findInterval( SCALAR x,SCALAR guess) {
301 //void find_interval(inter_params &ip,eqfunc f,float y,float constant, int &flag){
305 float flo,fhi,fguess;
308 fguess=(this->*sfunc)(guess)-x;
314 if(lo < xmin) lo=xmin;
315 if(hi > xmax) hi=xmax;
317 flo=(this->*sfunc)(lo)-x;
318 fhi=(this->*sfunc)(hi)-x;
319 if(flo*fhi <=0) { //found interval with root
321 if(flo*fguess <= 0) { //narrow interval down a bit
322 hi=lo+step; //to pass solver interval that is as
325 else if(fhi*fguess <= 0) {
329 //cout << "findInterval: i=" << i << " Lo= " << lo << " Hi= " << hi << endl;
331 while((found == 0) && (i <= 100));
340 bool LaRCsimIC::solve( SCALAR *y,SCALAR x) {
341 float x1,x2,x3,f1,f2,f3,d,d0;
343 float const relax =0.9;
351 f1=(this->*sfunc)(x1)-x;
352 f3=(this->*sfunc)(x3)-x;
357 while ((fabs(d) > eps) && (i < 100)) {
359 x2=x1-d*d0*f1/(f3-f1);
361 f2=(this->*sfunc)(x2)-x;
362 //cout << "solve x1,x2,x3: " << x1 << "," << x2 << "," << x3 << endl;
363 //cout << " " << f1 << "," << f2 << "," << f3 << endl;
365 if(fabs(f2) <= 0.001) {
367 } else if(f1*f2 <= 0.0) {
371 } else if(f2*f3 <= 0) {
384 //cout << "Success= " << success << " Vcas: " << vcas*V_TO_KNOTS << " Mach: " << x2 << endl;