| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Header: FGMassBalance.h |
| 4 | Author: Jon S. Berndt |
| 5 | Date started: 09/12/2000 |
| 6 | |
| 7 | ------------- Copyright (C) 2000 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 | 09/12/2000 JSB Created |
| 29 | |
| 30 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 31 | SENTRY |
| 32 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 33 | |
| 34 | #ifndef FGMASSBALANCE_H |
| 35 | #define FGMASSBALANCE_H |
| 36 | |
| 37 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 38 | INCLUDES |
| 39 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 40 | |
| 41 | #include "FGModel.h" |
| 42 | #include "math/FGMatrix33.h" |
| 43 | |
| 44 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 45 | FORWARD DECLARATIONSS |
| 46 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 47 | |
| 48 | namespace JSBSim { |
| 49 | |
| 50 | class FGPropagate; |
| 51 | class FGGroundReactions; |
| 52 | |
| 53 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 54 | CLASS DOCUMENTATION |
| 55 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 56 | |
| 57 | /** Models weight, balance and moment of inertia information. |
| 58 | |
| 59 | Maintains a vector of point masses. Sums the contribution of all, and |
| 60 | provides this to FGPropagate. Loads the <mass_balance> section of the |
| 61 | aircraft configuration file. There can be any number of <pointmasses>. Each |
| 62 | can also have a shape which - if present - causes an associated moment of |
| 63 | inertia to be calculated based on the shape. Note that a cylinder is solid, |
| 64 | a tube is hollow, a ball is solid and a sphere is hollow. |
| 65 | |
| 66 | The inertia tensor must be specified in the structural frame (x axis |
| 67 | positive aft, y axis positive out of the right wing and z axis upward). The |
| 68 | sign of the inertia cross products are optional by JSBSim. |
| 69 | if negated_crossproduct_inertia == "true", then define: |
| 70 | ixy = -integral( x * y * dm ), |
| 71 | ixz = -integral( x * z * dm ), |
| 72 | iyz = -integral( y * z * dm ). |
| 73 | else if negated_crossproduct_inertia == "false", then define: |
| 74 | ixy = integral( x * y * dm ), |
| 75 | ixz = integral( x * z * dm ), |
| 76 | iyz = integral( y * z * dm ). |
| 77 | default is negated_crossproduct_inertia = "true". |
| 78 | We strongly recommend defining negated_crossproduct_inertia = "false", |
| 79 | which is consistent with the specifications in the field of flight dynamics. |
| 80 | |
| 81 | <h3>Configuration File Format for \<mass_balance> Section:</h3> |
| 82 | @code{.xml} |
| 83 | <mass_balance negated_crossproduct_inertia="true|false"> |
| 84 | <ixx unit="{SLUG*FT2 | KG*M2}"> {number} </ixx> |
| 85 | <iyy unit="{SLUG*FT2 | KG*M2}"> {number} </iyy> |
| 86 | <izz unit="{SLUG*FT2 | KG*M2}"> {number} </izz> |
| 87 | <ixy unit="{SLUG*FT2 | KG*M2}"> {number} </ixy> |
| 88 | <ixz unit="{SLUG*FT2 | KG*M2}"> {number} </ixz> |
| 89 | <iyz unit="{SLUG*FT2 | KG*M2}"> {number} </iyz> |
| 90 | <emptywt unit="{LBS | KG"> {number} </emptywt> |
| 91 | <location name="CG" unit="{IN | FT | M}"> |
| 92 | <x> {number} </x> |
| 93 | <y> {number} </y> |
| 94 | <z> {number} </z> |
| 95 | </location> |
| 96 | [<pointmass name="{string}"> |
| 97 | <form shape="{tube | cylinder | sphere | ball}"> |
| 98 | <radius unit="{IN | FT | M}"> {number} </radius> |
| 99 | <length unit="{IN | FT | M}"> {number} </length> |
| 100 | </form> |
| 101 | <weight unit="{LBS | KG}"> {number} </weight> |
| 102 | <location name="{string}" unit="{IN | FT | M}"> |
| 103 | <x> {number} </x> |
| 104 | <y> {number} </y> |
| 105 | <z> {number} </z> |
| 106 | </location> |
| 107 | </pointmass> |
| 108 | ... other point masses ...] |
| 109 | </mass_balance> |
| 110 | @endcode |
| 111 | |
| 112 | @see Stevens and Lewis, "Flight Control & Simulation" |
| 113 | @see Bernard Etkin, " Dynamics Of Atmosferic Flight" |
| 114 | @see https://en.wikipedia.org/wiki/Moment_of_inertia#Inertia_tensor |
| 115 | @see https://www.mathworks.com/help/physmod/sm/ug/specify-custom-inertia.html |
| 116 | */ |
| 117 | |
| 118 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 119 | CLASS DECLARATION |
| 120 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 121 | |
| 122 | class JSBSIM_API FGMassBalance : public FGModel |
| 123 | { |
| 124 | |
| 125 | public: |
| 126 | explicit FGMassBalance(FGFDMExec*); |
| 127 | ~FGMassBalance(); |
| 128 | |
| 129 | bool Load(Element* el) override; |
| 130 | bool InitModel(void) override; |
| 131 | /** Runs the Mass Balance model; called by the Executive |
| 132 | Can pass in a value indicating if the executive is directing the |
| 133 | simulation to Hold. |
| 134 | @param Holding if true, the executive has been directed to hold the sim |
| 135 | from advancing time. Some models may ignore this flag, such |
| 136 | as the Input model, which may need to be active to listen |
| 137 | on a socket for the "Resume" command to be given. @return |
| 138 | false if no error */ |
| 139 | bool Run(bool Holding) override; |
| 140 | |
| 141 | double GetMass(void) const {return Mass;} |
| 142 | double GetWeight(void) const {return Weight;} |
| 143 | double GetEmptyWeight(void) const {return EmptyWeight;} |
| 144 | /** Returns the coordinates of the center of gravity expressed in the |
| 145 | structural frame. */ |
| 146 | const FGColumnVector3& GetXYZcg(void) const {return vXYZcg;} |
| 147 | double GetXYZcg(int axis) const {return vXYZcg(axis);} |
| 148 | const FGColumnVector3& GetDeltaXYZcg(void) const {return vDeltaXYZcg;} |
| 149 | double GetDeltaXYZcg(int axis) const {return vDeltaXYZcg(axis);} |
| 150 | |
| 151 | /** Computes the inertia contribution of a pointmass. |
| 152 | Computes and returns the inertia matrix of a pointmass of mass |
| 153 | slugs at the given vector r in the structural frame. The units |
| 154 | should be for the mass in slug and the vector in the structural |
| 155 | frame as usual in inches. |
| 156 | @param mass_sl the mass of this single pointmass given in slugs |
| 157 | @param r the location of this single pointmass in the structural frame |
| 158 | */ |
| 159 | FGMatrix33 GetPointmassInertia(double mass_sl, const FGColumnVector3& r) const |
| 160 | { |
| 161 | FGColumnVector3 v = StructuralToBody( r ); |
| 162 | FGColumnVector3 sv = mass_sl*v; |
| 163 | double xx = sv(1)*v(1); |
| 164 | double yy = sv(2)*v(2); |
| 165 | double zz = sv(3)*v(3); |
| 166 | double xy = -sv(1)*v(2); |
| 167 | double xz = -sv(1)*v(3); |
| 168 | double yz = -sv(2)*v(3); |
| 169 | return FGMatrix33( yy+zz, xy, xz, |
| 170 | xy, xx+zz, yz, |
| 171 | xz, yz, xx+yy ); |
| 172 | } |
| 173 | |
| 174 | /** Conversion from the structural frame to the body frame. |
| 175 | Converts the location given in the structural frame |
| 176 | coordinate system to the body frame. The units of the structural |
| 177 | frame are assumed to be in inches. The unit of the result is in |
| 178 | ft. |
| 179 | @param r vector coordinate in the structural reference frame (X positive |
| 180 | aft, measurements in inches). |
| 181 | @return vector coordinate in the body frame, in feet. |
| 182 | */ |
| 183 | FGColumnVector3 StructuralToBody(const FGColumnVector3& r) const; |
| 184 | |
| 185 | void SetEmptyWeight(double EW) { EmptyWeight = EW;} |
| 186 | void SetBaseCG(const FGColumnVector3& CG) {vbaseXYZcg = vXYZcg = CG;} |
| 187 | |
| 188 | void AddPointMass(Element* el); |
| 189 | double GetTotalPointMassWeight(void) const; |
| 190 | |
| 191 | const FGColumnVector3& GetPointMassMoment(void); |
| 192 | /// Returns the inertia matrix expressed in the body frame. |
| 193 | const FGMatrix33& GetJ(void) const {return mJ;} |
| 194 | /// Returns the inverse of the inertia matrix expressed in the body frame. |
| 195 | const FGMatrix33& GetJinv(void) const {return mJinv;} |
| 196 | void SetAircraftBaseInertias(const FGMatrix33& BaseJ) {baseJ = BaseJ;} |
| 197 | void GetMassPropertiesReport(int i); |
| 198 | |
| 199 | struct Inputs { |
| 200 | double GasMass; |
| 201 | double TanksWeight; |
| 202 | FGColumnVector3 GasMoment; |
| 203 | FGMatrix33 GasInertia; |
| 204 | FGColumnVector3 TanksMoment; |
| 205 | FGMatrix33 TankInertia; |
| 206 | bool WOW; |
| 207 | } in; |
| 208 | |
| 209 | private: |
| 210 | FGPropagate* Propagate; |
| 211 | double Weight; |
| 212 | double EmptyWeight; |
| 213 | double Mass; |
| 214 | FGMatrix33 mJ; |
| 215 | FGMatrix33 mJinv; |
| 216 | FGMatrix33 pmJ; |
| 217 | FGMatrix33 baseJ; |
| 218 | FGColumnVector3 vXYZcg; |
| 219 | FGColumnVector3 vLastXYZcg; |
| 220 | FGColumnVector3 vDeltaXYZcg; |
| 221 | FGColumnVector3 vDeltaXYZcgBody; |
| 222 | FGColumnVector3 vXYZtank; |
| 223 | FGColumnVector3 vbaseXYZcg; |
| 224 | FGColumnVector3 vPMxyz; |
| 225 | FGColumnVector3 PointMassCG; |
| 226 | const FGMatrix33& CalculatePMInertias(void); |
| 227 | double GetIxx(void) const { return mJ(1,1); } |
| 228 | double GetIyy(void) const { return mJ(2,2); } |
| 229 | double GetIzz(void) const { return mJ(3,3); } |
| 230 | double GetIxy(void) const { return -mJ(1,2); } |
| 231 | double GetIxz(void) const { return mJ(1,3); } |
| 232 | double GetIyz(void) const { return -mJ(2,3); } |
| 233 | |
| 234 | /** The PointMass structure encapsulates a point mass object, moments of inertia |
| 235 | mass, location, etc. */ |
| 236 | struct PointMass { |
| 237 | PointMass(double w, FGColumnVector3& vXYZ) : |
| 238 | eShapeType(esUnspecified), Location(vXYZ), Weight(w), Radius(0.0), |
| 239 | Length(0.0) {} |
| 240 | |
| 241 | void CalculateShapeInertia(void) { |
| 242 | switch(eShapeType) { |
| 243 | case esTube: |
| 244 | mPMInertia(1,1) = (Weight/(slugtolb))*Radius*Radius; // mr^2 |
| 245 | mPMInertia(2,2) = (Weight/(slugtolb*12))*(6*Radius*Radius + Length*Length); |
| 246 | mPMInertia(3,3) = mPMInertia(2,2); |
| 247 | break; |
| 248 | case esCylinder: |
| 249 | mPMInertia(1,1) = (Weight/(slugtolb*2))*Radius*Radius; // 0.5*mr^2 |
| 250 | mPMInertia(2,2) = (Weight/(slugtolb*12))*(3*Radius*Radius + Length*Length); |
| 251 | mPMInertia(3,3) = mPMInertia(2,2); |
| 252 | break; |
| 253 | case esSphere: |
| 254 | mPMInertia(1,1) = (Weight/(slugtolb*3))*Radius*Radius*2; // (2mr^2)/3 |
| 255 | mPMInertia(2,2) = mPMInertia(1,1); |
| 256 | mPMInertia(3,3) = mPMInertia(1,1); |
| 257 | break; |
| 258 | case esBall: |
| 259 | mPMInertia(1,1) = (Weight/(slugtolb*5))*Radius*Radius*2; // (2mr^2)/5 |
| 260 | mPMInertia(2,2) = mPMInertia(1,1); |
| 261 | mPMInertia(3,3) = mPMInertia(1,1); |
| 262 | break; |
| 263 | default: |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | enum esShape {esUnspecified, esTube, esCylinder, esSphere, esBall} eShapeType; |
| 269 | FGColumnVector3 Location; |
| 270 | double Weight; /// Weight in pounds. |
| 271 | double Radius; /// Radius in feet. |
| 272 | double Length; /// Length in feet. |
| 273 | std::string Name; |
| 274 | FGMatrix33 mPMInertia; |
| 275 | |
| 276 | double GetPointMassLocation(int axis) const {return Location(axis);} |
| 277 | double GetPointMassWeight(void) const {return Weight;} |
| 278 | esShape GetShapeType(void) {return eShapeType;} |
| 279 | const FGColumnVector3& GetLocation(void) {return Location;} |
| 280 | const FGMatrix33& GetPointMassInertia(void) {return mPMInertia;} |
| 281 | const std::string& GetName(void) {return Name;} |
| 282 | |
| 283 | void SetPointMassLocation(int axis, double value) {Location(axis) = value;} |
| 284 | void SetPointMassWeight(double wt) { |
| 285 | Weight = wt; |
| 286 | CalculateShapeInertia(); |
| 287 | } |
| 288 | void SetPointMassShapeType(esShape st) {eShapeType = st;} |
| 289 | void SetRadius(double r) {Radius = r;} |
| 290 | void SetLength(double l) {Length = l;} |
| 291 | void SetName(const std::string& name) {Name = name;} |
| 292 | void SetPointMassMoI(const FGMatrix33& MoI) { mPMInertia = MoI; } |
| 293 | double GetPointMassMoI(int r, int c) {return mPMInertia(r,c);} |
| 294 | |
| 295 | void bind(FGPropertyManager* PropertyManager, unsigned int num); |
| 296 | }; |
| 297 | |
| 298 | std::vector <struct PointMass*> PointMasses; |
| 299 | |
| 300 | void bind(void); |
| 301 | void Debug(int from) override; |
| 302 | }; |
| 303 | } |
| 304 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 305 | #endif |