]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAerodynamics.cpp
Expose the strut compression.
[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 "FGPropagate.h"
41 #include "FGAircraft.h"
42 #include "FGState.h"
43 #include "FGMassBalance.h"
44 #include "FGFactorGroup.h"
45 #include "FGPropertyManager.h"
46
47 namespace JSBSim {
48
49 static const char *IdSrc = "$Id$";
50 static const char *IdHdr = ID_AERODYNAMICS;
51
52 const unsigned NAxes=6;
53 const char* AxisNames[] = { "drag", "side", "lift", "roll",
54                             "pitch","yaw" };
55 const char* AxisNamesUpper[] = { "DRAG", "SIDE", "LIFT", "ROLL",
56                                  "PITCH","YAW" };
57
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 CLASS IMPLEMENTATION
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
61
62
63 FGAerodynamics::FGAerodynamics(FGFDMExec* FDMExec) : FGModel(FDMExec)
64 {
65   Name = "FGAerodynamics";
66
67   AxisIdx["DRAG"]  = 0;
68   AxisIdx["SIDE"]  = 1;
69   AxisIdx["LIFT"]  = 2;
70   AxisIdx["ROLL"]  = 3;
71   AxisIdx["PITCH"] = 4;
72   AxisIdx["YAW"]   = 5;
73
74   Coeff = new CoeffArray[6];
75
76   impending_stall = stall_hyst = 0.0;
77   alphaclmin = alphaclmax = 0.0;
78   alphahystmin = alphahystmax = 0.0;
79   clsq = lod = 0.0;
80   alphaw = 0.0;
81   bi2vel = ci2vel = 0.0;
82   bind();
83
84   Debug(0);
85 }
86
87 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88
89 FGAerodynamics::~FGAerodynamics()
90 {
91   unsigned int i,j;
92
93   unbind();
94
95   for (i=0; i<6; i++) {
96     for (j=0; j<Coeff[i].size(); j++) {
97       delete Coeff[i][j];
98     }
99   }
100   delete[] Coeff;
101
102   Debug(1);
103 }
104
105 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106
107 bool FGAerodynamics::Run(void)
108 {
109   unsigned int axis_ctr,ctr;
110   double alpha, twovel;
111
112   if (!FGModel::Run()) {
113
114     twovel = 2*Auxiliary->GetVt();
115     if (twovel != 0) {
116       bi2vel = Aircraft->GetWingSpan() / twovel;
117       ci2vel = Aircraft->Getcbar() / twovel;
118     }
119
120     alphaw = Auxiliary->Getalpha() + Aircraft->GetWingIncidence();
121
122     alpha = Auxiliary->Getalpha();
123
124     if (alphaclmax != 0) {
125       if (alpha > 0.85*alphaclmax) {
126         impending_stall = 10*(alpha/alphaclmax - 0.85);
127       } else {
128         impending_stall = 0;
129       }
130     }
131
132     if (alphahystmax != 0.0 && alphahystmin != 0.0) {
133       if (alpha > alphahystmax) {
134          stall_hyst = 1;
135       } else if (alpha < alphahystmin) {
136          stall_hyst = 0;
137       }
138     }
139
140     vLastFs = vFs;
141     vFs.InitMatrix();
142
143     for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
144       for (ctr=0; ctr < Coeff[axis_ctr].size(); ctr++) {
145         vFs(axis_ctr+1) += Coeff[axis_ctr][ctr]->TotalValue();
146       }
147     }
148
149     //correct signs of drag and lift to wind axes convention
150     //positive forward, right, down
151     if ( Auxiliary->Getqbar() > 0) {
152       clsq = vFs(eLift) / (Aircraft->GetWingArea()*Auxiliary->Getqbar());
153       clsq *= clsq;
154     }
155     if ( vFs(eDrag)  > 0) {
156       lod = vFs(eLift) / vFs(eDrag);
157     }
158
159     //correct signs of drag and lift to wind axes convention
160     //positive forward, right, down
161     vFs(eDrag)*=-1; vFs(eLift)*=-1;
162
163     vForces = State->GetTs2b()*vFs;
164
165     vDXYZcg = MassBalance->StructuralToBody(Aircraft->GetXYZrp());
166
167     vMoments = vDXYZcg*vForces; // M = r X F
168
169     for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
170       for (ctr = 0; ctr < Coeff[axis_ctr+3].size(); ctr++) {
171         vMoments(axis_ctr+1) += Coeff[axis_ctr+3][ctr]->TotalValue();
172       }
173     }
174
175     return false;
176   } else {
177     return true;
178   }
179 }
180
181 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182
183 bool FGAerodynamics::Load(FGConfigFile* AC_cfg)
184 {
185   string parameter, axis, scratch;
186
187   AC_cfg->GetNextConfigLine();
188
189   while ((parameter = AC_cfg->GetValue()) != string("/AERODYNAMICS")) {
190     if (parameter == "AXIS") {
191       CoeffArray ca;
192       axis = AC_cfg->GetValue("NAME");
193       AC_cfg->GetNextConfigLine();
194       while ((parameter = AC_cfg->GetValue()) != string("/AXIS")) {
195         if ( parameter == "COEFFICIENT" ) {
196           ca.push_back( new FGCoefficient(FDMExec) );
197           ca.back()->Load(AC_cfg);
198         } else if ( parameter == "GROUP" ) {
199           ca.push_back( new FGFactorGroup(FDMExec) );
200           ca.back()->Load(AC_cfg);
201         }
202       }
203       Coeff[AxisIdx[axis]] = ca;
204       AC_cfg->GetNextConfigLine();
205     } else if (parameter == "AC_ALPHALIMITS") {
206       *AC_cfg >> scratch >> alphaclmin >> alphaclmax;
207       if (debug_lvl > 0) cout << "    Maximum Alpha: " << alphaclmax
208                               << "    Minimum Alpha: " << alphaclmin
209                               << endl;
210     } else if (parameter == "AC_HYSTLIMITS") {
211       *AC_cfg >> scratch >> alphahystmin >> alphahystmax;
212       if (debug_lvl > 0) cout << "    Hysteresis Start: " << alphahystmax
213                               << "    Hysteresis End: " << alphahystmin
214                               << endl;
215     }
216   }
217
218   bindModel();
219
220   return true;
221 }
222
223 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224
225 string FGAerodynamics::GetCoefficientStrings(string delimeter)
226 {
227   string CoeffStrings = "";
228   bool firstime = true;
229   unsigned int axis, sd;
230
231   for (axis = 0; axis < 6; axis++) {
232     for (sd = 0; sd < Coeff[axis].size(); sd++) {
233       if (firstime) {
234         firstime = false;
235       } else {
236         CoeffStrings += delimeter;
237       }
238       CoeffStrings += Coeff[axis][sd]->GetCoefficientName();
239     }
240   }
241   return CoeffStrings;
242 }
243
244 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245
246 string FGAerodynamics::GetCoefficientValues(string delimeter)
247 {
248   string SDValues = "";
249   bool firstime = true;
250
251   for (unsigned int axis = 0; axis < 6; axis++) {
252     for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
253       if (firstime) {
254         firstime = false;
255       } else {
256         SDValues += delimeter;
257       }
258       SDValues += Coeff[axis][sd]->GetSDstring();
259     }
260   }
261
262   return SDValues;
263 }
264
265 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266
267 void FGAerodynamics::bind(void)
268 {
269   typedef double (FGAerodynamics::*PMF)(int) const;
270
271   PropertyManager->Tie("forces/fbx-aero-lbs", this,1,
272                        (PMF)&FGAerodynamics::GetForces);
273   PropertyManager->Tie("forces/fby-aero-lbs", this,2,
274                        (PMF)&FGAerodynamics::GetForces);
275   PropertyManager->Tie("forces/fbz-aero-lbs", this,3,
276                        (PMF)&FGAerodynamics::GetForces);
277   PropertyManager->Tie("moments/l-aero-lbsft", this,1,
278                        (PMF)&FGAerodynamics::GetMoments);
279   PropertyManager->Tie("moments/m-aero-lbsft", this,2,
280                        (PMF)&FGAerodynamics::GetMoments);
281   PropertyManager->Tie("moments/n-aero-lbsft", this,3,
282                        (PMF)&FGAerodynamics::GetMoments);
283   PropertyManager->Tie("forces/fwx-aero-lbs", this,1,
284                        (PMF)&FGAerodynamics::GetvFs);
285   PropertyManager->Tie("forces/fwy-aero-lbs", this,2,
286                        (PMF)&FGAerodynamics::GetvFs);
287   PropertyManager->Tie("forces/fwz-aero-lbs", this,3,
288                        (PMF)&FGAerodynamics::GetvFs);
289   PropertyManager->Tie("forces/lod-norm", this,
290                        &FGAerodynamics::GetLoD);
291   PropertyManager->Tie("aero/cl-squared-norm", this,
292                        &FGAerodynamics::GetClSquared);
293   PropertyManager->Tie("aero/alpha-max-deg", this,
294                        &FGAerodynamics::GetAlphaCLMax,
295                        &FGAerodynamics::SetAlphaCLMax,
296                        true);
297   PropertyManager->Tie("aero/alpha-min-deg", this,
298                        &FGAerodynamics::GetAlphaCLMin,
299                        &FGAerodynamics::SetAlphaCLMin,
300                        true);
301   PropertyManager->Tie("aero/bi2vel", this,
302                        &FGAerodynamics::GetBI2Vel);
303   PropertyManager->Tie("aero/ci2vel", this,
304                        &FGAerodynamics::GetCI2Vel);
305   PropertyManager->Tie("aero/alpha-wing-rad", this,
306                        &FGAerodynamics::GetAlphaW);
307   PropertyManager->Tie("systems/stall-warn-norm", this,
308                         &FGAerodynamics::GetStallWarn);
309   PropertyManager->Tie("aero/stall-hyst-norm", this,
310                         &FGAerodynamics::GetHysteresisParm);
311 }
312
313 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314
315 void FGAerodynamics::bindModel(void)
316 {
317   unsigned i,j;
318   FGPropertyManager* node;
319   string axis_node_name;
320   node = PropertyManager->GetNode("aero/buildup",true);
321   for (i=0;i<NAxes;i++) {
322      node = node->GetNode( string(AxisNames[i]),true );
323      for (j=0; j < Coeff[i].size(); j++) {
324        Coeff[i][j]->bind(node);
325      }
326      node = (FGPropertyManager*)node->getParent();
327   }
328 }
329
330 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
331
332 void FGAerodynamics::unbind(void)
333 {
334   unsigned i,j;
335
336   PropertyManager->Untie("forces/fbx-aero-lbs");
337   PropertyManager->Untie("forces/fby-aero-lbs");
338   PropertyManager->Untie("forces/fbz-aero-lbs");
339   PropertyManager->Untie("moments/l-aero-lbsft");
340   PropertyManager->Untie("moments/m-aero-lbsft");
341   PropertyManager->Untie("moments/n-aero-lbsft");
342   PropertyManager->Untie("forces/fwx-aero-lbs");
343   PropertyManager->Untie("forces/fwy-aero-lbs");
344   PropertyManager->Untie("forces/fwz-aero-lbs");
345   PropertyManager->Untie("forces/lod-norm");
346   PropertyManager->Untie("aero/cl-squared-norm");
347   PropertyManager->Untie("aero/alpha-max-deg");
348   PropertyManager->Untie("aero/alpha-min-deg");
349   PropertyManager->Untie("aero/bi2vel");
350   PropertyManager->Untie("aero/ci2vel");
351   PropertyManager->Untie("aero/alpha-wing-rad");
352   PropertyManager->Untie("aero/stall-hyst-norm");
353   PropertyManager->Untie("systems/stall-warn-norm");
354
355   for ( i=0; i<NAxes; i++ ) {
356      for ( j=0; j < Coeff[i].size(); j++ ) {
357        Coeff[i][j]->unbind();
358
359      }
360   }
361 }
362
363 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364
365 void FGAerodynamics::convert(void)
366 {
367
368   for (int axis=0; axis<6; axis++) {
369     if (Coeff[axis].size()>0) {
370       cout << endl << "        <axis name=\"" << AxisNamesUpper[axis] << "\">" << endl;
371       for (int c=0; c<Coeff[axis].size(); c++) {
372         Coeff[axis][c]->convert();
373       }
374       cout << "        </axis>" << endl;
375     }
376   }
377
378 }
379
380 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
381 //    The bitmasked value choices are as follows:
382 //    unset: In this case (the default) JSBSim would only print
383 //       out the normally expected messages, essentially echoing
384 //       the config files as they are read. If the environment
385 //       variable is not set, debug_lvl is set to 1 internally
386 //    0: This requests JSBSim not to output any messages
387 //       whatsoever.
388 //    1: This value explicity requests the normal JSBSim
389 //       startup messages
390 //    2: This value asks for a message to be printed out when
391 //       a class is instantiated
392 //    4: When this value is set, a message is displayed when a
393 //       FGModel object executes its Run() method
394 //    8: When this value is set, various runtime state variables
395 //       are printed out periodically
396 //    16: When set various parameters are sanity checked and
397 //       a message is printed out when they go out of bounds
398
399 void FGAerodynamics::Debug(int from)
400 {
401   if (debug_lvl <= 0) return;
402
403   if (debug_lvl & 1) { // Standard console startup message output
404     if (from == 0) { // Constructor
405
406     }
407   }
408   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
409     if (from == 0) cout << "Instantiated: FGAerodynamics" << endl;
410     if (from == 1) cout << "Destroyed:    FGAerodynamics" << endl;
411   }
412   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
413   }
414   if (debug_lvl & 8 ) { // Runtime state variables
415   }
416   if (debug_lvl & 16) { // Sanity checking
417   }
418   if (debug_lvl & 64) {
419     if (from == 0) { // Constructor
420       cout << IdSrc << endl;
421       cout << IdHdr << endl;
422     }
423   }
424 }
425
426 } // namespace JSBSim