]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAerodynamics.cpp
JSBSim updates. This update changes the file format, so an update of the base
[flightgear.git] / src / FDM / JSBSim / FGAerodynamics.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGAerodynamics.cpp
4  Author:       Jon S. Berndt
5  Date started: 09/13/00
6  Purpose:      Encapsulates the aerodynamic forces (gear and collision)
7
8  ------------- Copyright (C) 2000  Jon S. Berndt (jsb@hal-pc.org) -------------
9
10  This program is free software; you can redistribute it and/or modify it under
11  the terms of the GNU General Public License as published by the Free Software
12  Foundation; either version 2 of the License, or (at your option) any later
13  version.
14
15  This program is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  details.
19
20  You should have received a copy of the GNU General Public License along with
21  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  Place - Suite 330, Boston, MA  02111-1307, USA.
23
24  Further information about the GNU General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29
30 HISTORY
31 --------------------------------------------------------------------------------
32 09/13/00   JSB   Created
33 04/22/01   JSB   Moved code into here from FGAircraft
34
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 INCLUDES
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38
39 #include "FGAerodynamics.h"
40 #include "FGFactorGroup.h"
41 #include "FGCoefficient.h"
42 #include "FGPropertyManager.h"
43
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_AERODYNAMICS;
46
47 const unsigned NAxes=6;                           
48 const char* AxisNames[] = { "drag", "side-force", "lift", "rolling-moment",
49                             "pitching-moment","yawing-moment" }; 
50
51 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 CLASS IMPLEMENTATION
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54
55
56 FGAerodynamics::FGAerodynamics(FGFDMExec* FDMExec) : FGModel(FDMExec)
57 {
58   Name = "FGAerodynamics";
59
60   AxisIdx["DRAG"]  = 0;
61   AxisIdx["SIDE"]  = 1;
62   AxisIdx["LIFT"]  = 2;
63   AxisIdx["ROLL"]  = 3;
64   AxisIdx["PITCH"] = 4;
65   AxisIdx["YAW"]   = 5;
66
67   Coeff = new CoeffArray[6];
68   
69   impending_stall = stall_hyst = 0.0;
70   alphaclmin = alphaclmax = 0.0;
71   alphahystmin = alphahystmax = 0.0;
72   clsq = lod = 0.0;
73   alphaw = 0.0;  
74   bi2vel = ci2vel = 0.0;
75   bind();
76
77   Debug(0);
78 }
79
80 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81
82 FGAerodynamics::~FGAerodynamics()
83 {
84   unsigned int i,j;
85   
86   unbind();
87   
88   for (i=0; i<6; i++) {
89     for (j=0; j<Coeff[i].size(); j++) {
90       delete Coeff[i][j];
91     }
92   }
93   delete[] Coeff;
94   
95   Debug(1);
96 }
97
98 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99
100 bool FGAerodynamics::Run(void)
101 {
102   unsigned int axis_ctr,ctr;
103   double alpha, twovel;
104
105   if (!FGModel::Run()) {
106
107     twovel = 2*Translation->GetVt();
108     if (twovel > 0) {
109       bi2vel = Aircraft->GetWingSpan() / twovel;
110       ci2vel = Aircraft->Getcbar() / twovel;
111     }  
112     
113     alphaw = Translation->Getalpha() + Aircraft->GetWingIncidence();
114     
115     alpha = Translation->Getalpha();
116     
117     if (alphaclmax != 0) {
118       if (alpha > 0.85*alphaclmax) {
119         impending_stall = 10*(alpha/alphaclmax - 0.85);
120       } else {
121         impending_stall = 0;
122       }
123     }
124    
125     if (alphahystmax != 0.0 && alphahystmin != 0.0) {
126       if (alpha > alphahystmax) {
127          stall_hyst = 1;
128       } else if (alpha < alphahystmin) {
129          stall_hyst = 0;
130       }    
131     }
132  
133     vLastFs = vFs;
134     vFs.InitMatrix();
135
136     for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
137       for (ctr=0; ctr < Coeff[axis_ctr].size(); ctr++) {
138         vFs(axis_ctr+1) += Coeff[axis_ctr][ctr]->TotalValue();
139       }
140     }
141
142     //correct signs of drag and lift to wind axes convention
143     //positive forward, right, down
144     if ( Translation->Getqbar() > 0) {
145       clsq = vFs(eLift) / (Aircraft->GetWingArea()*Translation->Getqbar());
146       clsq *= clsq;
147     }
148     if ( vFs(eDrag)  > 0) {
149       lod = vFs(eLift) / vFs(eDrag);
150     }  
151         
152     //correct signs of drag and lift to wind axes convention
153     //positive forward, right, down
154     vFs(eDrag)*=-1; vFs(eLift)*=-1;
155
156     vForces = State->GetTs2b()*vFs;
157
158     vDXYZcg(eX) = -(Aircraft->GetXYZrp(eX) 
159                       - MassBalance->GetXYZcg(eX))*inchtoft;
160     vDXYZcg(eY) =  (Aircraft->GetXYZrp(eY) 
161                       - MassBalance->GetXYZcg(eY))*inchtoft;
162     vDXYZcg(eZ) = -(Aircraft->GetXYZrp(eZ) 
163                       - MassBalance->GetXYZcg(eZ))*inchtoft;
164
165     vMoments = vDXYZcg*vForces; // M = r X F
166
167     for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
168       for (ctr = 0; ctr < Coeff[axis_ctr+3].size(); ctr++) {
169         vMoments(axis_ctr+1) += Coeff[axis_ctr+3][ctr]->TotalValue();
170       }
171     }
172
173     return false;
174   } else {
175     return true;
176   }
177 }
178
179 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180
181 bool FGAerodynamics::Load(FGConfigFile* AC_cfg)
182 {
183   string parameter, axis, scratch;
184
185   AC_cfg->GetNextConfigLine();
186
187   while ((parameter = AC_cfg->GetValue()) != string("/AERODYNAMICS")) {
188     if (parameter == "AXIS") {
189       CoeffArray ca;
190       axis = AC_cfg->GetValue("NAME");
191       AC_cfg->GetNextConfigLine();
192       while ((parameter = AC_cfg->GetValue()) != string("/AXIS")) {
193         if ( parameter == "COEFFICIENT" ) {
194           ca.push_back( new FGCoefficient(FDMExec) );
195           ca.back()->Load(AC_cfg);
196         } else if ( parameter == "GROUP" ) {
197           ca.push_back( new FGFactorGroup(FDMExec) );
198           ca.back()->Load(AC_cfg);
199         }
200       }
201       Coeff[AxisIdx[axis]] = ca;
202       AC_cfg->GetNextConfigLine();
203     } else if (parameter == "AC_ALPHALIMITS") {
204       *AC_cfg >> scratch >> alphaclmin >> alphaclmax;
205       if (debug_lvl > 0) cout << "    Maximum Alpha: " << alphaclmax
206                               << "    Minimum Alpha: " << alphaclmin
207                               << endl;
208     } else if (parameter == "AC_HYSTLIMITS") {
209       *AC_cfg >> scratch >> alphahystmin >> alphahystmax;
210       if (debug_lvl > 0) cout << "    Hysteresis Start: " << alphahystmax
211                               << "    Hysteresis End: " << alphahystmin
212                               << endl;
213     }
214   }
215
216   bindModel();
217   
218   return true;
219 }
220
221 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222
223 string FGAerodynamics::GetCoefficientStrings(void)
224 {
225   string CoeffStrings = "";
226   bool firstime = true;
227   unsigned int axis, sd;
228
229   for (axis = 0; axis < 6; axis++) {
230     for (sd = 0; sd < Coeff[axis].size(); sd++) {
231       if (firstime) {
232         firstime = false;
233       } else {
234         CoeffStrings += ", ";
235       }
236       CoeffStrings += Coeff[axis][sd]->GetCoefficientName();
237     }
238   }
239   return CoeffStrings;
240 }
241
242 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243
244 string FGAerodynamics::GetCoefficientValues(void)
245 {
246   string SDValues = "";
247   bool firstime = true;
248
249   for (unsigned int axis = 0; axis < 6; axis++) {
250     for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
251       if (firstime) {
252         firstime = false;
253       } else {
254         SDValues += ", ";
255       }
256       SDValues += Coeff[axis][sd]->GetSDstring();
257     }
258   }
259
260   return SDValues;
261 }
262
263 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264
265 void FGAerodynamics::bind(void)
266 {
267   typedef double (FGAerodynamics::*PMF)(int) const;
268
269   PropertyManager->Tie("forces/fbx-aero-lbs", this,1,
270                        (PMF)&FGAerodynamics::GetForces);
271   PropertyManager->Tie("forces/fby-aero-lbs", this,2,
272                        (PMF)&FGAerodynamics::GetForces);
273   PropertyManager->Tie("forces/fbz-aero-lbs", this,3,
274                        (PMF)&FGAerodynamics::GetForces);
275   PropertyManager->Tie("moments/l-aero-lbsft", this,1,
276                        (PMF)&FGAerodynamics::GetMoments);
277   PropertyManager->Tie("moments/m-aero-lbsft", this,2,
278                        (PMF)&FGAerodynamics::GetMoments);
279   PropertyManager->Tie("moments/n-aero-lbsft", this,3,
280                        (PMF)&FGAerodynamics::GetMoments);
281   PropertyManager->Tie("forces/fwx-aero-lbs", this,1,
282                        (PMF)&FGAerodynamics::GetvFs);
283   PropertyManager->Tie("forces/fwy-aero-lbs", this,2,
284                        (PMF)&FGAerodynamics::GetvFs);
285   PropertyManager->Tie("forces/fwz-aero-lbs", this,3,
286                        (PMF)&FGAerodynamics::GetvFs);
287   PropertyManager->Tie("forces/lod-norm", this,
288                        &FGAerodynamics::GetLoD);
289   PropertyManager->Tie("aero/cl-squared-norm", this,
290                        &FGAerodynamics::GetClSquared); 
291   PropertyManager->Tie("aero/alpha-max-deg", this,
292                        &FGAerodynamics::GetAlphaCLMax,
293                        &FGAerodynamics::SetAlphaCLMax,
294                        true);
295   PropertyManager->Tie("aero/alpha-min-deg", this,
296                        &FGAerodynamics::GetAlphaCLMin,
297                        &FGAerodynamics::SetAlphaCLMin,
298                        true);
299   PropertyManager->Tie("aero/bi2vel", this,
300                        &FGAerodynamics::GetBI2Vel);
301   PropertyManager->Tie("aero/ci2vel", this,
302                        &FGAerodynamics::GetCI2Vel);
303   PropertyManager->Tie("aero/alpha-wing-rad", this,
304                        &FGAerodynamics::GetAlphaW);
305   PropertyManager->Tie("systems/stall-warn-norm", this,
306                         &FGAerodynamics::GetStallWarn);
307   PropertyManager->Tie("aero/stall-hyst-norm", this,
308                         &FGAerodynamics::GetHysteresisParm);
309 }
310
311 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312
313 void FGAerodynamics::bindModel(void)
314
315   unsigned i,j;
316   FGPropertyManager* node;
317   string axis_node_name;
318   node = PropertyManager->GetNode("aero/buildup",true);
319   for (i=0;i<NAxes;i++) {
320      node = node->GetNode( string(AxisNames[i]),true );
321      for (j=0; j < Coeff[i].size(); j++) {
322        Coeff[i][j]->bind(node);
323      } 
324      node = (FGPropertyManager*)node->getParent();                                         
325   }
326 }
327
328 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329
330 void FGAerodynamics::unbind(void)
331 {
332   unsigned i,j;
333
334   PropertyManager->Untie("forces/fbx-aero-lbs");
335   PropertyManager->Untie("forces/fby-aero-lbs");
336   PropertyManager->Untie("forces/fbz-aero-lbs");
337   PropertyManager->Untie("moments/l-aero-lbsft");
338   PropertyManager->Untie("moments/m-aero-lbsft");
339   PropertyManager->Untie("moments/n-aero-lbsft");
340   PropertyManager->Untie("forces/fwx-aero-lbs");
341   PropertyManager->Untie("forces/fwy-aero-lbs");
342   PropertyManager->Untie("forces/fwz-aero-lbs");
343   PropertyManager->Untie("forces/lod-norm");
344   PropertyManager->Untie("aero/cl-squared-norm");  
345   PropertyManager->Untie("aero/alpha-max-deg");
346   PropertyManager->Untie("aero/alpha-min-deg");
347   PropertyManager->Untie("aero/bi2vel");
348   PropertyManager->Untie("aero/ci2vel");
349   PropertyManager->Untie("aero/alpha-wing-rad");
350   PropertyManager->Untie("systems/stall-warn-norm");
351
352   for ( i=0; i<NAxes; i++ ) {
353      for ( j=0; j < Coeff[i].size(); j++ ) {
354        Coeff[i][j]->unbind();
355        
356      }
357   }
358 }
359
360 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361 //    The bitmasked value choices are as follows:
362 //    unset: In this case (the default) JSBSim would only print
363 //       out the normally expected messages, essentially echoing
364 //       the config files as they are read. If the environment
365 //       variable is not set, debug_lvl is set to 1 internally
366 //    0: This requests JSBSim not to output any messages
367 //       whatsoever.
368 //    1: This value explicity requests the normal JSBSim
369 //       startup messages
370 //    2: This value asks for a message to be printed out when
371 //       a class is instantiated
372 //    4: When this value is set, a message is displayed when a
373 //       FGModel object executes its Run() method
374 //    8: When this value is set, various runtime state variables
375 //       are printed out periodically
376 //    16: When set various parameters are sanity checked and
377 //       a message is printed out when they go out of bounds
378
379 void FGAerodynamics::Debug(int from)
380 {
381   if (debug_lvl <= 0) return;
382
383   if (debug_lvl & 1) { // Standard console startup message output
384     if (from == 0) { // Constructor
385
386     }
387   }
388   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
389     if (from == 0) cout << "Instantiated: FGAerodynamics" << endl;
390     if (from == 1) cout << "Destroyed:    FGAerodynamics" << endl;
391   }
392   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
393   }
394   if (debug_lvl & 8 ) { // Runtime state variables
395   }
396   if (debug_lvl & 16) { // Sanity checking
397   }
398   if (debug_lvl & 64) {
399     if (from == 0) { // Constructor
400       cout << IdSrc << endl;
401       cout << IdHdr << endl;
402     }
403   }
404 }
405