| 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 ([email protected]) ------------- |
| 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 |
| 11 | Software Foundation; either version 2 of the License, or (at your option) any |
| 12 | later 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 |
| 20 | with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 21 | Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | |
| 23 | Further information about the GNU Lesser General Public License can also be |
| 24 | found on 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 | <moment name="name" frame="BODY|LOCAL|WIND"> |
| 52 | |
| 53 | <function> ... </function> |
| 54 | |
| 55 | <direction> <!-- optional for initial direction vector --> |
| 56 | <x> value </x> |
| 57 | <y> value </y> |
| 58 | <z> value </z> |
| 59 | </direction> |
| 60 | </force> |
| 61 | |
| 62 | </external_reactions> |
| 63 | |
| 64 | */ |
| 65 | |
| 66 | #include "FGFDMExec.h" |
| 67 | #include "FGExternalForce.h" |
| 68 | #include "input_output/FGXMLElement.h" |
| 69 | |
| 70 | using namespace std; |
| 71 | |
| 72 | namespace JSBSim { |
| 73 | |
| 74 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 75 | |
| 76 | FGPropertyVector3::FGPropertyVector3(FGPropertyManager* pm, |
| 77 | const std::string& baseName, |
| 78 | const std::string& xcmp, |
| 79 | const std::string& ycmp, |
| 80 | const std::string& zcmp) |
| 81 | { |
| 82 | data[0] = pm->CreatePropertyObject<double>(baseName + "/" + xcmp); |
| 83 | data[1] = pm->CreatePropertyObject<double>(baseName + "/" + ycmp); |
| 84 | data[2] = pm->CreatePropertyObject<double>(baseName + "/" + zcmp); |
| 85 | } |
| 86 | |
| 87 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 88 | |
| 89 | FGParameter* FGExternalForce::bind(Element *el, const string& magName, |
| 90 | FGPropertyVector3& v) |
| 91 | { |
| 92 | // Set frame (from FGForce). |
| 93 | string sFrame = el->GetAttributeValue("frame"); |
| 94 | if (sFrame.empty()) { |
| 95 | cerr << el->ReadFrom() |
| 96 | << "No frame specified for external " << el->GetName() << ", \"" |
| 97 | << Name << "\"." << endl |
| 98 | << "Frame set to Body" << endl; |
| 99 | ttype = tNone; |
| 100 | } else if (sFrame == "BODY") { |
| 101 | ttype = tNone; |
| 102 | } else if (sFrame == "LOCAL") { |
| 103 | ttype = tLocalBody; |
| 104 | } else if (sFrame == "WIND") { |
| 105 | ttype = tWindBody; |
| 106 | } else if (sFrame == "INERTIAL") { |
| 107 | ttype = tInertialBody; |
| 108 | } else { |
| 109 | cerr << el->ReadFrom() |
| 110 | << "Invalid frame specified for external " << el->GetName() << ", \"" |
| 111 | << Name << "\"." << endl |
| 112 | << "Frame set to Body" << endl; |
| 113 | ttype = tNone; |
| 114 | } |
| 115 | |
| 116 | Element* direction_element = el->FindElement("direction"); |
| 117 | if (!direction_element) { |
| 118 | cerr << el->ReadFrom() |
| 119 | << "No direction element specified in " << el->GetName() |
| 120 | << " object. Default is (0,0,0)." << endl; |
| 121 | } else { |
| 122 | FGColumnVector3 direction = direction_element->FindElementTripletConvertTo("IN"); |
| 123 | direction.Normalize(); |
| 124 | v = direction; |
| 125 | } |
| 126 | |
| 127 | // The value sent to the sim through the external_reactions/{force |
| 128 | // name}/magnitude property will be multiplied against the unit vector, which |
| 129 | // can come in initially in the direction vector. The frame in which the |
| 130 | // vector is defined is specified with the frame attribute. The vector is |
| 131 | // normalized to magnitude 1. |
| 132 | |
| 133 | Element* function_element = el->FindElement("function"); |
| 134 | if (function_element) { |
| 135 | return new FGFunction(fdmex, function_element); |
| 136 | } else { |
| 137 | FGPropertyManager* pm = fdmex->GetPropertyManager(); |
| 138 | FGPropertyNode* node = pm->GetNode(magName, true); |
| 139 | return new FGPropertyValue(node); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 144 | |
| 145 | void FGExternalForce::setForce(Element *el) |
| 146 | { |
| 147 | FGPropertyManager* PropertyManager = fdmex->GetPropertyManager(); |
| 148 | Name = el->GetAttributeValue("name"); |
| 149 | string BasePropertyName = "external_reactions/" + Name; |
| 150 | |
| 151 | forceDirection = FGPropertyVector3(PropertyManager, BasePropertyName, |
| 152 | "x", "y", "z"); |
| 153 | forceMagnitude = bind(el, BasePropertyName + "/magnitude", forceDirection); |
| 154 | |
| 155 | Element* location_element = el->FindElement("location"); |
| 156 | if (!location_element) { |
| 157 | cerr << el->ReadFrom() |
| 158 | << "No location element specified in force object." << endl; |
| 159 | } else { |
| 160 | FGColumnVector3 location = location_element->FindElementTripletConvertTo("IN"); |
| 161 | SetLocation(location); |
| 162 | } |
| 163 | PropertyManager->Tie( BasePropertyName + "/location-x-in", (FGForce*)this, |
| 164 | &FGForce::GetLocationX, &FGForce::SetLocationX); |
| 165 | PropertyManager->Tie( BasePropertyName + "/location-y-in", (FGForce*)this, |
| 166 | &FGForce::GetLocationY, &FGForce::SetLocationY); |
| 167 | PropertyManager->Tie( BasePropertyName + "/location-z-in", (FGForce*)this, |
| 168 | &FGForce::GetLocationZ, &FGForce::SetLocationZ); |
| 169 | } |
| 170 | |
| 171 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 172 | |
| 173 | void FGExternalForce::setMoment(Element *el) |
| 174 | { |
| 175 | FGPropertyManager* PropertyManager = fdmex->GetPropertyManager(); |
| 176 | Name = el->GetAttributeValue("name"); |
| 177 | string BasePropertyName = "external_reactions/" + Name; |
| 178 | |
| 179 | momentDirection = FGPropertyVector3(PropertyManager, BasePropertyName, |
| 180 | "l", "m", "n"); |
| 181 | momentMagnitude = bind(el, BasePropertyName + "/magnitude-lbsft", |
| 182 | momentDirection); |
| 183 | } |
| 184 | |
| 185 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 186 | |
| 187 | FGExternalForce::~FGExternalForce() |
| 188 | { |
| 189 | delete forceMagnitude; |
| 190 | delete momentMagnitude; |
| 191 | Debug(1); |
| 192 | } |
| 193 | |
| 194 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 195 | |
| 196 | const FGColumnVector3& FGExternalForce::GetBodyForces(void) |
| 197 | { |
| 198 | if (forceMagnitude) |
| 199 | vFn = forceMagnitude->GetValue() * forceDirection; |
| 200 | |
| 201 | if (momentMagnitude) |
| 202 | vMn = Transform() * (momentMagnitude->GetValue() * momentDirection); |
| 203 | |
| 204 | return FGForce::GetBodyForces(); |
| 205 | } |
| 206 | |
| 207 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 208 | // The bitmasked value choices are as follows: |
| 209 | // unset: In this case (the default) JSBSim would only print |
| 210 | // out the normally expected messages, essentially echoing |
| 211 | // the config files as they are read. If the environment |
| 212 | // variable is not set, debug_lvl is set to 1 internally |
| 213 | // 0: This requests JSBSim not to output any messages |
| 214 | // whatsoever. |
| 215 | // 1: This value explicity requests the normal JSBSim |
| 216 | // startup messages |
| 217 | // 2: This value asks for a message to be printed out when |
| 218 | // a class is instantiated |
| 219 | // 4: When this value is set, a message is displayed when a |
| 220 | // FGModel object executes its Run() method |
| 221 | // 8: When this value is set, various runtime state variables |
| 222 | // are printed out periodically |
| 223 | // 16: When set various parameters are sanity checked and |
| 224 | // a message is printed out when they go out of bounds |
| 225 | |
| 226 | void FGExternalForce::Debug(int from) |
| 227 | { |
| 228 | if (debug_lvl <= 0) return; |
| 229 | |
| 230 | if (debug_lvl & 1) { // Standard console startup message output |
| 231 | if (from == 0) { // Constructor |
| 232 | cout << " " << Name << endl; |
| 233 | cout << " Frame: "; |
| 234 | switch(ttype) { |
| 235 | case tNone: |
| 236 | cout << "BODY"; |
| 237 | break; |
| 238 | case tLocalBody: |
| 239 | cout << "LOCAL"; |
| 240 | break; |
| 241 | case tWindBody: |
| 242 | cout << "WIND"; |
| 243 | break; |
| 244 | case tInertialBody: |
| 245 | cout << "INERTIAL"; |
| 246 | break; |
| 247 | default: |
| 248 | cout << "ERROR/UNKNOWN"; |
| 249 | } |
| 250 | cout << endl << " Location: (" << vXYZn(eX) << ", " << vXYZn(eY) << ", " << vXYZn(eZ) << ")" << endl; |
| 251 | } |
| 252 | } |
| 253 | if (debug_lvl & 2 ) { // Instantiation/Destruction notification |
| 254 | if (from == 0) cout << "Instantiated: FGExternalForce" << endl; |
| 255 | if (from == 1) cout << "Destroyed: FGExternalForce" << endl; |
| 256 | } |
| 257 | if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects |
| 258 | } |
| 259 | if (debug_lvl & 8 ) { // Runtime state variables |
| 260 | } |
| 261 | if (debug_lvl & 16) { // Sanity checking |
| 262 | } |
| 263 | if (debug_lvl & 64) { |
| 264 | if (from == 0) { // Constructor |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } |