JSBSim Flight Dynamics Model  1.0 (02 March 2017)
An Open Source Flight Dynamics and Control Software Library in C++
FGExternalForce.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 
3  Source: FGExternalForce.cpp
4  Author: Jon Berndt, Dave Culp
5  Date started: 9/21/07
6 
7  ------------- Copyright (C) 2007 Jon S. Berndt (jon@jsbsim.org) -------------
8 
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
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 Lesser General Public License for more
17  details.
18 
19  You should have received a copy of the GNU Lesser 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.
22 
23  Further information about the GNU Lesser General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25 
26  HISTORY
27 --------------------------------------------------------------------------------
28 9/21/07 JB Created
29 
30 <external_reactions>
31 
32  <!-- Interface properties, a.k.a. property declarations -->
33  <property> ... </property>
34 
35  <force name="name" frame="BODY|LOCAL|WIND">
36 
37  <function> ... </function>
38 
39  <location unit="units"> <!-- location -->
40  <x> value </x>
41  <y> value </y>
42  <z> value </z>
43  </location>
44  <direction> <!-- optional for initial direction vector -->
45  <x> value </x>
46  <y> value </y>
47  <z> value </z>
48  </direction>
49  </force>
50 
51 </external_reactions>
52 
53 */
54 
55 #include "FGExternalForce.h"
56 #include "input_output/FGXMLElement.h"
57 #include <iostream>
58 
59 using namespace std;
60 
61 namespace JSBSim {
62 
63 IDENT(IdSrc,"$Id: FGExternalForce.cpp,v 1.16 2014/12/18 09:56:05 andgi Exp $");
64 IDENT(IdHdr,ID_EXTERNALFORCE);
65 
66 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 
68 FGExternalForce::FGExternalForce(FGFDMExec *FDMExec, Element *el, int index)
69  : FGForce(FDMExec)
70 {
71  Element* location_element=0;
72  Element* direction_element=0;
73  Element* function_element=0;
74  string sFrame;
75  string BasePropertyName;
76  FGColumnVector3 location;
77  Magnitude_Function = 0;
78  magnitude = 0.0;
79  azimuth = 0.0;
80 
81  FGPropertyManager* PropertyManager = fdmex->GetPropertyManager();
82  Name = el->GetAttributeValue("name");
83  BasePropertyName = "external_reactions/" + Name;
84 
85  // The value sent to the sim through the external_forces/{force name}/magnitude
86  // property will be multiplied against the unit vector, which can come in
87  // initially in the direction vector. The frame in which the vector is defined
88  // is specified with the frame attribute. The vector is normalized to magnitude 1.
89 
90  function_element = el->FindElement("function");
91  if (function_element) {
92  Magnitude_Function = new FGFunction(PropertyManager, function_element);
93  } else {
94  PropertyManager->Tie( BasePropertyName + "/magnitude",(FGExternalForce*)this, &FGExternalForce::GetMagnitude, &FGExternalForce::SetMagnitude);
95  }
96 
97 
98  // Set frame (from FGForce).
99  sFrame = el->GetAttributeValue("frame");
100  if (sFrame.empty()) {
101  cerr << "No frame specified for external force, \"" << Name << "\"." << endl;
102  cerr << "Frame set to Body" << endl;
103  ttype = tNone;
104  } else if (sFrame == "BODY") {
105  ttype = tNone;
106  } else if (sFrame == "LOCAL") {
107  ttype = tLocalBody;
108  PropertyManager->Tie( BasePropertyName + "/azimuth", (FGExternalForce*)this, &FGExternalForce::GetAzimuth, &FGExternalForce::SetAzimuth);
109  } else if (sFrame == "WIND") {
110  ttype = tWindBody;
111  } else {
112  cerr << "Invalid frame specified for external force, \"" << Name << "\"." << endl;
113  cerr << "Frame set to Body" << endl;
114  ttype = tNone;
115  }
116  PropertyManager->Tie( BasePropertyName + "/x", (FGExternalForce*)this, &FGExternalForce::GetX, &FGExternalForce::SetX);
117  PropertyManager->Tie( BasePropertyName + "/y", (FGExternalForce*)this, &FGExternalForce::GetY, &FGExternalForce::SetY);
118  PropertyManager->Tie( BasePropertyName + "/z", (FGExternalForce*)this, &FGExternalForce::GetZ, &FGExternalForce::SetZ);
119 
120  location_element = el->FindElement("location");
121  if (!location_element) {
122  cerr << "No location element specified in force object." << endl;
123  } else {
124  location = location_element->FindElementTripletConvertTo("IN");
125  SetLocation(location);
126  }
127  PropertyManager->Tie( BasePropertyName + "/location-x-in", (FGExternalForce*)this, &FGExternalForce::GetLocX, &FGExternalForce::SetLocX);
128  PropertyManager->Tie( BasePropertyName + "/location-y-in", (FGExternalForce*)this, &FGExternalForce::GetLocY, &FGExternalForce::SetLocY);
129  PropertyManager->Tie( BasePropertyName + "/location-z-in", (FGExternalForce*)this, &FGExternalForce::GetLocZ, &FGExternalForce::SetLocZ);
130 
131  direction_element = el->FindElement("direction");
132  if (!direction_element) {
133  cerr << "No direction element specified in force object. Default is (0,0,0)." << endl;
134  } else {
135  vDirection = direction_element->FindElementTripletConvertTo("IN");
136  vDirection.Normalize();
137  }
138 
139  Debug(0);
140 }
141 
142 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 // Copy constructor
144 
146 {
147  magnitude = extForce.magnitude;
148  Frame = extForce.Frame;
149  vDirection = extForce.vDirection;
150  Name = extForce.Name;
151 }
152 
153 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 
156 {
157  delete Magnitude_Function;
158  Debug(1);
159 }
160 
161 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162 
163 void FGExternalForce::SetMagnitude(double mag)
164 {
165  magnitude = mag;
166  vFn = vDirection*mag;
167 }
168 
169 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 
171 const FGColumnVector3& FGExternalForce::GetBodyForces(void)
172 {
173  if (Magnitude_Function) {
174  double mag = Magnitude_Function->GetValue();
175  SetMagnitude(mag);
176  }
177 
178  return FGForce::GetBodyForces();
179 }
180 
181 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182 // The bitmasked value choices are as follows:
183 // unset: In this case (the default) JSBSim would only print
184 // out the normally expected messages, essentially echoing
185 // the config files as they are read. If the environment
186 // variable is not set, debug_lvl is set to 1 internally
187 // 0: This requests JSBSim not to output any messages
188 // whatsoever.
189 // 1: This value explicity requests the normal JSBSim
190 // startup messages
191 // 2: This value asks for a message to be printed out when
192 // a class is instantiated
193 // 4: When this value is set, a message is displayed when a
194 // FGModel object executes its Run() method
195 // 8: When this value is set, various runtime state variables
196 // are printed out periodically
197 // 16: When set various parameters are sanity checked and
198 // a message is printed out when they go out of bounds
199 
200 void FGExternalForce::Debug(int from)
201 {
202  if (debug_lvl <= 0) return;
203 
204  if (debug_lvl & 1) { // Standard console startup message output
205  if (from == 0) { // Constructor
206  cout << " " << Name << endl;
207  cout << " Frame: " << Frame << endl;
208  cout << " Location: (" << vXYZn(eX) << ", " << vXYZn(eY) << ", " << vXYZn(eZ) << ")" << endl;
209  }
210  }
211  if (debug_lvl & 2 ) { // Instantiation/Destruction notification
212  if (from == 0) cout << "Instantiated: FGExternalForce" << endl;
213  if (from == 1) cout << "Destroyed: FGExternalForce" << endl;
214  }
215  if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
216  }
217  if (debug_lvl & 8 ) { // Runtime state variables
218  }
219  if (debug_lvl & 16) { // Sanity checking
220  }
221  if (debug_lvl & 64) {
222  if (from == 0) { // Constructor
223  cout << IdSrc << endl;
224  cout << IdHdr << endl;
225  }
226  }
227 }
228 }
std::string GetAttributeValue(const std::string &key)
Retrieves an attribute.
Encapsulates code that models an individual arbitrary force.
STL namespace.
FGColumnVector3 & Normalize(void)
Normalize.
Element * FindElement(const std::string &el="")
Searches for a specified element.
double GetValue(void) const
Retrieves the value of the function object.
Definition: FGFunction.cpp:364
FGPropertyManager * GetPropertyManager(void)
Returns a pointer to the property manager object.
Definition: FGFDMExec.cpp:1099
void Tie(const std::string &name, bool *pointer, bool useDefault=true)
Tie a property to an external bool variable.
Represents a mathematical function.
Definition: FGFunction.h:699
This class implements a 3 element column vector.
FGExternalForce(FGFDMExec *FDMExec)
Constructor.
Encapsulates the JSBSim simulation executive.
Definition: FGFDMExec.h:189
Utility class that aids in the conversion of forces between coordinate systems and calculation of mom...
Definition: FGForce.h:225
FGColumnVector3 FindElementTripletConvertTo(const std::string &target_units)
Composes a 3-element column vector for the supplied location or orientation.