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 "ls_cockpit.h"
34 #include "ls_generic.h"
35 #include "ls_interface.h"
37 #include "ls_constants.h"
38 #include "ls_geodesy.h"
40 #include "LaRCsimIC.hxx"
46 LaRCsimIC::LaRCsimIC(void) {
51 altitude=runway_altitude=hdot=alt_agl=0;
52 latitude_gd=latitude_gc=longitude=0;
55 vnorthwind=veastwind=vdownwind=0;
59 ls_geod_to_geoc( latitude_gd,altitude,&sea_level_radius,&latitude_gc);
64 LaRCsimIC::~LaRCsimIC(void) {}
66 void LaRCsimIC::get_atmosphere(void) {
67 Altitude=altitude; //set LaRCsim variable
68 ls_atmos(Altitude, &density_ratio, &soundspeed, &T, &p);
69 rho=density_ratio*SEA_LEVEL_DENSITY;
72 void LaRCsimIC::SetVcalibratedKtsIC( SCALAR tt) {
75 vt=sqrt(1/density_ratio*vc*vc);
78 void LaRCsimIC::SetVtrueFpsIC( SCALAR tt) {
83 void LaRCsimIC::SetMachIC( SCALAR tt) {
86 lastSpeedSet=lssetmach;
89 void LaRCsimIC::SetVequivalentKtsIC(SCALAR tt) {
92 vt=sqrt(SEA_LEVEL_DENSITY/rho)*ve;
95 void LaRCsimIC::SetClimbRateFpmIC( SCALAR tt) {
96 SetClimbRateFpsIC(tt/60.0);
99 void LaRCsimIC::SetClimbRateFpsIC( SCALAR tt) {
102 hdot = tt - vdownwind;
103 gamma=asin(hdot/vtg);
109 void LaRCsimIC::SetFlightPathAngleRadIC( SCALAR tt) {
117 void LaRCsimIC::SetPitchAngleRadIC(SCALAR tt) {
118 if(alt_agl < (DEFAULT_AGL_ALT + 0.1) || vt < 10 )
119 theta=DEFAULT_PITCH_ON_GROUND;
125 void LaRCsimIC::SetUVWFpsIC(SCALAR vu, SCALAR vv, SCALAR vw) {
127 vt=sqrt(u*u+v*v+w*w);
128 lastSpeedSet=lssetuvw;
132 void LaRCsimIC::SetVNEDAirmassFpsIC(SCALAR north, SCALAR east, SCALAR down ) {
133 vnorthwind=north; veastwind=east; vdownwind=down;
134 vw=sqrt(vnorthwind*vnorthwind + veastwind*veastwind + vdownwind*vdownwind);
136 SetClimbRateFpsIC(-1*(vdown+vdownwind));
139 void LaRCsimIC::SetAltitudeFtIC( SCALAR tt) {
140 if(tt > (runway_altitude + DEFAULT_AGL_ALT)) {
143 altitude=runway_altitude + DEFAULT_AGL_ALT;
144 alt_agl=DEFAULT_AGL_ALT;
145 theta=DEFAULT_PITCH_ON_GROUND;
149 //lets try to make sure the user gets what they intended
151 switch(lastSpeedSet) {
160 SetVcalibratedKtsIC(vc*V_TO_KNOTS);
163 SetVequivalentKtsIC(ve*V_TO_KNOTS);
171 void LaRCsimIC::SetAltitudeAGLFtIC( SCALAR tt) {
174 altitude=runway_altitude + alt_agl;
177 void LaRCsimIC::SetRunwayAltitudeFtIC( SCALAR tt) {
179 if(lastAltSet == lssetagl)
180 altitude = runway_altitude + alt_agl;
183 void LaRCsimIC::calcVtfromNED(void) {
184 //take the earth's rotation out of veast first
185 //float veastner = veast- OMEGA_EARTH*sea_level_radius*cos( latitude_gd );
186 float veastner=veast;
187 u = (vnorth - vnorthwind)*cos(theta)*cos(psi) +
188 (veastner - veastwind)*cos(theta)*sin(psi) -
189 (vdown - vdownwind)*sin(theta);
190 v = (vnorth - vnorthwind)*(sin(phi)*sin(theta)*cos(psi) - cos(phi)*sin(psi)) +
191 (veastner - veastwind)*(sin(phi)*sin(theta)*sin(psi) + cos(phi)*cos(psi)) +
192 (vdown - vdownwind)*sin(phi)*cos(theta);
193 w = (vnorth - vnorthwind)*(cos(phi)*sin(theta)*cos(psi) + sin(phi)*sin(psi)) +
194 (veastner - veastwind)*(cos(phi)*sin(theta)*sin(psi) - sin(phi)*cos(psi)) +
195 (vdown - vdownwind)*cos(phi)*cos(theta);
196 vt = sqrt(u*u + v*v + w*w);
199 void LaRCsimIC::calcNEDfromVt(void) {
202 //get the body components of vt
207 //transform them to local axes and add on the wind components
208 vnorth = u*cos(theta)*cos(psi) +
209 v*(sin(phi)*sin(theta)*cos(psi) - cos(phi)*sin(psi)) +
210 w*(cos(phi)*sin(theta)*cos(psi) + sin(phi)*sin(psi)) +
213 //need to account for the earth's rotation here
214 veastner = u*cos(theta)*sin(psi) +
215 v*(sin(phi)*sin(theta)*sin(psi) + cos(phi)*cos(psi)) +
216 w*(cos(phi)*sin(theta)*sin(psi) - sin(phi)*cos(psi)) +
219 //veast = veastner + OMEGA_EARTH*sea_level_radius*cos( latitude_gd );
221 vdown = u*sin(theta) +
222 v*sin(phi)*cos(theta) +
223 w*cos(phi)*cos(theta) +
227 void LaRCsimIC::SetVNEDFpsIC( SCALAR north, SCALAR east, SCALAR down) {
231 SetClimbRateFpsIC(-1*vdown);
232 lastSpeedSet=lssetned;
236 void LaRCsimIC::SetLatitudeGDRadIC(SCALAR tt) {
238 ls_geod_to_geoc(latitude_gd,altitude,&sea_level_radius, &latitude_gc);
241 bool LaRCsimIC::getAlpha(void) {
243 float guess=theta-gamma;
247 sfunc=&LaRCsimIC::GammaEqOfAlpha;
248 if(findInterval(0,guess)){
257 bool LaRCsimIC::getTheta(void) {
259 float guess=alpha+gamma;
262 sfunc=&LaRCsimIC::GammaEqOfTheta;
263 if(findInterval(0,guess)){
273 SCALAR LaRCsimIC::GammaEqOfTheta( SCALAR theta_arg) {
276 a=cos(alpha)*cos(beta)*sin(theta_arg);
277 b=sin(beta)*sin(phi);
278 c=sin(alpha)*cos(beta)*cos(phi);
279 return sin(gamma)-a+(b+c)*cos(theta_arg);
282 SCALAR LaRCsimIC::GammaEqOfAlpha( SCALAR alpha_arg) {
285 a=cos(alpha_arg)*cos(beta)*sin(theta);
286 b=sin(beta)*sin(phi);
287 c=sin(alpha_arg)*cos(beta)*cos(phi);
288 return sin(gamma)-a+(b+c)*cos(theta);
296 bool LaRCsimIC::findInterval( SCALAR x,SCALAR guess) {
297 //void find_interval(inter_params &ip,eqfunc f,float y,float constant, int &flag){
301 float flo,fhi,fguess;
304 fguess=(this->*sfunc)(guess)-x;
310 if(lo < xmin) lo=xmin;
311 if(hi > xmax) hi=xmax;
313 flo=(this->*sfunc)(lo)-x;
314 fhi=(this->*sfunc)(hi)-x;
315 if(flo*fhi <=0) { //found interval with root
317 if(flo*fguess <= 0) { //narrow interval down a bit
318 hi=lo+step; //to pass solver interval that is as
321 else if(fhi*fguess <= 0) {
325 //cout << "findInterval: i=" << i << " Lo= " << lo << " Hi= " << hi << endl;
327 while((found == 0) && (i <= 100));
336 bool LaRCsimIC::solve( SCALAR *y,SCALAR x) {
337 float x1,x2,x3,f1,f2,f3,d,d0;
339 float const relax =0.9;
347 f1=(this->*sfunc)(x1)-x;
348 f3=(this->*sfunc)(x3)-x;
353 while ((fabs(d) > eps) && (i < 100)) {
355 x2=x1-d*d0*f1/(f3-f1);
357 f2=(this->*sfunc)(x2)-x;
358 //cout << "solve x1,x2,x3: " << x1 << "," << x2 << "," << x3 << endl;
359 //cout << " " << f1 << "," << f2 << "," << f3 << endl;
361 if(fabs(f2) <= 0.001) {
363 } else if(f1*f2 <= 0.0) {
367 } else if(f2*f3 <= 0) {
380 //cout << "Success= " << success << " Vcas: " << vcas*V_TO_KNOTS << " Mach: " << x2 << endl;