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.
31 #include "FDM/LaRCsimIC.hxx"
32 #include <FDM/LaRCsim/ls_cockpit.h>
33 #include <FDM/LaRCsim/ls_generic.h>
34 #include <FDM/LaRCsim/ls_interface.h>
35 #include <FDM/LaRCsim/atmos_62.h>
36 #include <FDM/LaRCsim/ls_constants.h>
37 #include <FDM/LaRCsim/ls_geodesy.h>
39 LaRCsimIC::LaRCsimIC(void) {
44 altitude=runway_altitude=hdot=alt_agl=0;
45 latitude_gd=latitude_gc=longitude=0;
48 vnorthwind=veastwind=vdownwind=0;
52 ls_geod_to_geoc( latitude_gd,altitude,&sea_level_radius,&latitude_gc);
57 LaRCsimIC::~LaRCsimIC(void) {}
59 void LaRCsimIC::get_atmosphere(void) {
60 Altitude=altitude; //set LaRCsim variable
61 ls_atmos(Altitude, &density_ratio, &soundspeed, &T, &p);
62 rho=density_ratio*SEA_LEVEL_DENSITY;
65 void LaRCsimIC::SetVcalibratedKtsIC( SCALAR tt) {
68 vt=sqrt(1/density_ratio*vc*vc);
71 void LaRCsimIC::SetVtrueFpsIC( SCALAR tt) {
76 void LaRCsimIC::SetMachIC( SCALAR tt) {
79 lastSpeedSet=lssetmach;
82 void LaRCsimIC::SetVequivalentKtsIC(SCALAR tt) {
85 vt=sqrt(SEA_LEVEL_DENSITY/rho)*ve;
88 void LaRCsimIC::SetClimbRateFpmIC( SCALAR tt) {
89 SetClimbRateFpsIC(tt/60.0);
92 void LaRCsimIC::SetClimbRateFpsIC( SCALAR tt) {
94 cout << "vtg: " << vtg << endl;
96 hdot = tt - vdownwind;
100 cout << "hdot: " << hdot << endl;
101 cout << "gamma: " << gamma*RAD_TO_DEG << endl;
102 cout << "vdown: " << vdown << endl;
106 void LaRCsimIC::SetFlightPathAngleRadIC( SCALAR tt) {
111 cout << "hdot: " << hdot << endl;
115 void LaRCsimIC::SetPitchAngleRadIC(SCALAR tt) {
116 if(alt_agl < (DEFAULT_AGL_ALT + 0.1) || vt < 10 )
117 theta=DEFAULT_PITCH_ON_GROUND;
123 void LaRCsimIC::SetUVWFpsIC(SCALAR vu, SCALAR vv, SCALAR vw) {
125 vt=sqrt(u*u+v*v+w*w);
126 lastSpeedSet=lssetuvw;
130 void LaRCsimIC::SetVNEDAirmassFpsIC(SCALAR north, SCALAR east, SCALAR down ) {
131 vnorthwind=north; veastwind=east; vdownwind=down;
132 vw=sqrt(vnorthwind*vnorthwind + veastwind*veastwind + vdownwind*vdownwind);
134 SetClimbRateFpsIC(-1*(vdown+vdownwind));
137 void LaRCsimIC::SetAltitudeFtIC( SCALAR tt) {
138 if(tt > (runway_altitude + DEFAULT_AGL_ALT)) {
141 altitude=runway_altitude + DEFAULT_AGL_ALT;
142 alt_agl=DEFAULT_AGL_ALT;
143 theta=DEFAULT_PITCH_ON_GROUND;
147 //lets try to make sure the user gets what they intended
149 switch(lastSpeedSet) {
158 SetVcalibratedKtsIC(vc*V_TO_KNOTS);
161 SetVequivalentKtsIC(ve*V_TO_KNOTS);
169 void LaRCsimIC::SetAltitudeAGLFtIC( SCALAR tt) {
172 altitude=runway_altitude + alt_agl;
175 void LaRCsimIC::SetRunwayAltitudeFtIC( SCALAR tt) {
177 if(lastAltSet == lssetagl)
178 altitude = runway_altitude + alt_agl;
181 void LaRCsimIC::calcVtfromNED(void) {
182 //take the earth's rotation out of veast first
183 //float veastner = veast- OMEGA_EARTH*sea_level_radius*cos( latitude_gd );
184 float veastner=veast;
185 u = (vnorth - vnorthwind)*cos(theta)*cos(psi) +
186 (veastner - veastwind)*cos(theta)*sin(psi) -
187 (vdown - vdownwind)*sin(theta);
188 v = (vnorth - vnorthwind)*(sin(phi)*sin(theta)*cos(psi) - cos(phi)*sin(psi)) +
189 (veastner - veastwind)*(sin(phi)*sin(theta)*sin(psi) + cos(phi)*cos(psi)) +
190 (vdown - vdownwind)*sin(phi)*cos(theta);
191 w = (vnorth - vnorthwind)*(cos(phi)*sin(theta)*cos(psi) + sin(phi)*sin(psi)) +
192 (veastner - veastwind)*(cos(phi)*sin(theta)*sin(psi) - sin(phi)*cos(psi)) +
193 (vdown - vdownwind)*cos(phi)*cos(theta);
194 vt = sqrt(u*u + v*v + w*w);
197 void LaRCsimIC::calcNEDfromVt(void) {
200 //get the body components of vt
205 //transform them to local axes and add on the wind components
206 vnorth = u*cos(theta)*cos(psi) +
207 v*(sin(phi)*sin(theta)*cos(psi) - cos(phi)*sin(psi)) +
208 w*(cos(phi)*sin(theta)*cos(psi) + sin(phi)*sin(psi)) +
211 //need to account for the earth's rotation here
212 veastner = u*cos(theta)*sin(psi) +
213 v*(sin(phi)*sin(theta)*sin(psi) + cos(phi)*cos(psi)) +
214 w*(cos(phi)*sin(theta)*sin(psi) - sin(phi)*cos(psi)) +
217 //veast = veastner + OMEGA_EARTH*sea_level_radius*cos( latitude_gd );
219 vdown = u*sin(theta) +
220 v*sin(phi)*cos(theta) +
221 w*cos(phi)*cos(theta) +
225 void LaRCsimIC::SetVNEDFpsIC( SCALAR north, SCALAR east, SCALAR down) {
229 SetClimbRateFpsIC(-1*vdown);
230 lastSpeedSet=lssetned;
234 void LaRCsimIC::SetLatitudeGDRadIC(SCALAR tt) {
236 ls_geod_to_geoc(latitude_gd,altitude,&sea_level_radius, &latitude_gc);
239 bool LaRCsimIC::getAlpha(void) {
241 float guess=theta-gamma;
245 sfunc=&LaRCsimIC::GammaEqOfAlpha;
246 if(findInterval(0,guess)){
255 bool LaRCsimIC::getTheta(void) {
257 float guess=alpha+gamma;
260 sfunc=&LaRCsimIC::GammaEqOfTheta;
261 if(findInterval(0,guess)){
271 SCALAR LaRCsimIC::GammaEqOfTheta( SCALAR theta_arg) {
274 a=cos(alpha)*cos(beta)*sin(theta_arg);
275 b=sin(beta)*sin(phi);
276 c=sin(alpha)*cos(beta)*cos(phi);
277 return sin(gamma)-a+(b+c)*cos(theta_arg);
280 SCALAR LaRCsimIC::GammaEqOfAlpha( SCALAR alpha_arg) {
283 a=cos(alpha_arg)*cos(beta)*sin(theta);
284 b=sin(beta)*sin(phi);
285 c=sin(alpha_arg)*cos(beta)*cos(phi);
286 return sin(gamma)-a+(b+c)*cos(theta);
294 bool LaRCsimIC::findInterval( SCALAR x,SCALAR guess) {
295 //void find_interval(inter_params &ip,eqfunc f,float y,float constant, int &flag){
299 float flo,fhi,fguess;
302 fguess=(this->*sfunc)(guess)-x;
308 if(lo < xmin) lo=xmin;
309 if(hi > xmax) hi=xmax;
311 flo=(this->*sfunc)(lo)-x;
312 fhi=(this->*sfunc)(hi)-x;
313 if(flo*fhi <=0) { //found interval with root
315 if(flo*fguess <= 0) { //narrow interval down a bit
316 hi=lo+step; //to pass solver interval that is as
319 else if(fhi*fguess <= 0) {
323 //cout << "findInterval: i=" << i << " Lo= " << lo << " Hi= " << hi << endl;
325 while((found == 0) && (i <= 100));
334 bool LaRCsimIC::solve( SCALAR *y,SCALAR x) {
335 float x1,x2,x3,f1,f2,f3,d,d0;
337 float const relax =0.9;
345 f1=(this->*sfunc)(x1)-x;
346 f3=(this->*sfunc)(x3)-x;
351 while ((fabs(d) > eps) && (i < 100)) {
353 x2=x1-d*d0*f1/(f3-f1);
355 f2=(this->*sfunc)(x2)-x;
356 //cout << "solve x1,x2,x3: " << x1 << "," << x2 << "," << x3 << endl;
357 //cout << " " << f1 << "," << f2 << "," << f3 << endl;
359 if(fabs(f2) <= 0.001) {
361 } else if(f1*f2 <= 0.0) {
365 } else if(f2*f3 <= 0) {
378 //cout << "Success= " << success << " Vcas: " << vcas*V_TO_KNOTS << " Mach: " << x2 << endl;