Skip to content

Commit

Permalink
Moved the gravity calculations from FGAccelerations to FGInertial.
Browse files Browse the repository at this point in the history
There is now only one place where the gravity parameters are defined and where all the related calculations take place. This ensures that all classes use the same value of g.

This commit is related to the discussion from the issue #184.
  • Loading branch information
bcoconni committed Jul 14, 2019
1 parent 2814c73 commit 2c44e8d
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 143 deletions.
24 changes: 10 additions & 14 deletions src/FGFDMExec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ CLASS IMPLEMENTATION
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Constructor

FGFDMExec::FGFDMExec(FGPropertyManager* root, unsigned int* fdmctr) : Root(root), FDMctr(fdmctr)
FGFDMExec::FGFDMExec(FGPropertyManager* root, unsigned int* fdmctr)
: Root(root), FDMctr(fdmctr)
{
Frame = 0;
Error = 0;
IC = 0;
Trim = 0;
Script = 0;
IC = nullptr;
Trim = nullptr;
Script = nullptr;
disperse = 0;

RootDir = "";
Expand Down Expand Up @@ -291,8 +291,6 @@ bool FGFDMExec::DeAllocate(void)
delete IC;
delete Trim;

Error = 0;

modelLoaded = false;
return modelLoaded;
}
Expand Down Expand Up @@ -345,8 +343,7 @@ void FGFDMExec::LoadInputs(unsigned int idx)
case eInput:
break;
case eInertial:
Inertial->in.Radius = Propagate->GetRadius();
Inertial->in.Latitude = Propagate->GetLatitude();
Inertial->in.Position = Propagate->GetLocation();
break;
case eAtmosphere:
Atmosphere->in.altitudeASL = Propagate->GetAltitudeASL();
Expand Down Expand Up @@ -457,7 +454,7 @@ void FGFDMExec::LoadInputs(unsigned int idx)
BuoyantForces->in.Density = Atmosphere->GetDensity();
BuoyantForces->in.Pressure = Atmosphere->GetPressure();
BuoyantForces->in.Temperature = Atmosphere->GetTemperature();
BuoyantForces->in.gravity = Inertial->gravity();
BuoyantForces->in.gravity = Inertial->GetGravity().Magnitude();
break;
case eMassBalance:
MassBalance->in.GasInertia = BuoyantForces->GetGasMassInertia();
Expand Down Expand Up @@ -490,8 +487,7 @@ void FGFDMExec::LoadInputs(unsigned int idx)
Accelerations->in.GroundMoment = GroundReactions->GetMoments();
Accelerations->in.Force = Aircraft->GetForces();
Accelerations->in.GroundForce = GroundReactions->GetForces();
Accelerations->in.GAccel = Inertial->GetGAccel(Propagate->GetRadius());
Accelerations->in.J2Grav = Inertial->GetGravityJ2(Propagate->GetLocation());
Accelerations->in.vGravAccel = Inertial->GetGravity();
Accelerations->in.vPQRi = Propagate->GetPQRi();
Accelerations->in.vPQR = Propagate->GetPQR();
Accelerations->in.vUVW = Propagate->GetUVW();
Expand Down Expand Up @@ -672,8 +668,8 @@ bool FGFDMExec::LoadModel(const string& model, bool addModelToPath)
modelName = model; // Set the class modelName attribute

if( AircraftPath.isNull() || EnginePath.isNull() || SystemsPath.isNull()) {
cerr << "Error: attempted to load aircraft with undefined ";
cerr << "aircraft, engine, and system paths" << endl;
cerr << "Error: attempted to load aircraft with undefined "
<< "aircraft, engine, and system paths" << endl;
return false;
}

Expand Down
1 change: 0 additions & 1 deletion src/FGFDMExec.h
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,6 @@ class FGFDMExec : public FGJSBBase
}

private:
int Error;
unsigned int Frame;
unsigned int IdFDM;
int disperse;
Expand Down
8 changes: 4 additions & 4 deletions src/initialization/FGTrim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ bool FGTrim::checkLimits(FGTrimAxis& axis)

void FGTrim::setupPullup() {
double g,q,cgamma;
g=fdmex->GetInertial()->gravity();
g=fdmex->GetInertial()->GetGravity().Magnitude();
cgamma=cos(fgic.GetFlightPathAngleRadIC());
cout << "setPitchRateInPullup(): " << g << ", " << cgamma << ", "
<< fgic.GetVtrueFpsIC() << endl;
Expand All @@ -744,7 +744,7 @@ void FGTrim::setupTurn(void){
phi = fgic.GetPhiRadIC();
if( fabs(phi) > 0.001 && fabs(phi) < 1.56 ) {
targetNlf = 1 / cos(phi);
g = fdmex->GetInertial()->gravity();
g = fdmex->GetInertial()->GetGravity().Magnitude();
psidot = g*tan(phi) / fgic.GetUBodyFpsIC();
cout << targetNlf << ", " << psidot << endl;
}
Expand All @@ -756,7 +756,7 @@ void FGTrim::setupTurn(void){
void FGTrim::updateRates(void){
if( mode == tTurn ) {
double phi = fgic.GetPhiRadIC();
double g = fdmex->GetInertial()->gravity();
double g = fdmex->GetInertial()->GetGravity().Magnitude();
double p,q,r,theta;
if(fabs(phi) > 0.001 && fabs(phi) < 1.56 ) {
theta=fgic.GetThetaRadIC();
Expand All @@ -773,7 +773,7 @@ void FGTrim::updateRates(void){
fgic.SetRRadpsIC(r);
} else if( mode == tPullup && fabs(targetNlf-1) > 0.01) {
double g,q,cgamma;
g=fdmex->GetInertial()->gravity();
g=fdmex->GetInertial()->GetGravity().Magnitude();
cgamma=cos(fgic.GetFlightPathAngleRadIC());
q=g*(targetNlf-cgamma)/fgic.GetVtrueFpsIC();
fgic.SetQRadpsIC(q);
Expand Down
43 changes: 13 additions & 30 deletions src/models/FGAccelerations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,29 @@
------------- Copyright (C) 2011 Jon S. Berndt ([email protected]) -------------
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Further information about the GNU Lesser General Public License can also be found on
the world wide web at http://www.gnu.org.
Further information about the GNU Lesser General Public License can also be
found on the world wide web at http://www.gnu.org.
FUNCTIONAL DESCRIPTION
--------------------------------------------------------------------------------
This class encapsulates the calculation of the derivatives of the state vectors
UVW and PQR - the translational and rotational rates relative to the planet
fixed frame. The derivatives relative to the inertial frame are also calculated
as a side effect. Also, the derivative of the attitude quaterion is also calculated.
as a side effect. Also, the derivative of the attitude quaterion is also
calculated.
HISTORY
--------------------------------------------------------------------------------
Expand All @@ -54,7 +55,6 @@ INCLUDES

#include "FGAccelerations.h"
#include "FGFDMExec.h"
#include "input_output/FGPropertyManager.h"

using namespace std;

Expand All @@ -69,13 +69,11 @@ FGAccelerations::FGAccelerations(FGFDMExec* fdmex)
{
Debug(0);
Name = "FGAccelerations";
gravType = gtWGS84;
gravTorque = false;

vPQRidot.InitMatrix();
vUVWidot.InitMatrix();
vUVWdot.InitMatrix();
vGravAccel.InitMatrix();
vBodyAccel.InitMatrix();

bind();
Expand All @@ -98,7 +96,6 @@ bool FGAccelerations::InitModel(void)
vPQRidot.InitMatrix();
vUVWidot.InitMatrix();
vUVWdot.InitMatrix();
vGravAccel.InitMatrix();
vBodyAccel.InitMatrix();

return true;
Expand Down Expand Up @@ -147,7 +144,7 @@ void FGAccelerations::CalculatePQRdot(void)
FGColumnVector3 R = in.Ti2b * in.vInertialPosition;
double invRadius = 1.0 / R.Magnitude();
R *= invRadius;
in.Moment += (3.0 * in.GAccel * invRadius) * (R * (in.J * R));
in.Moment += (3.0 * in.vGravAccel.Magnitude() * invRadius) * (R * (in.J * R));
}

// Compute body frame rotational accelerations based on the current body
Expand Down Expand Up @@ -195,28 +192,15 @@ void FGAccelerations::CalculateUVWdot(void)
// Include Centripetal acceleration.
vUVWdot -= in.Ti2b * (in.vOmegaPlanet * (in.vOmegaPlanet * in.vInertialPosition));

// Include Gravitation accel
switch (gravType) {
case gtStandard:
{
double radius = in.vInertialPosition.Magnitude();
vGravAccel = -(in.GAccel / radius) * in.vInertialPosition;
}
break;
case gtWGS84:
vGravAccel = in.Tec2i * in.J2Grav;
break;
}

if (FDMExec->GetHoldDown()) {
// The acceleration in ECI is calculated so that the acceleration is zero
// in the body frame.
vUVWidot = in.vOmegaPlanet * (in.vOmegaPlanet * in.vInertialPosition);
vUVWdot.InitMatrix();
}
else {
vUVWdot += in.Ti2b * vGravAccel;
vUVWidot = in.Tb2i * vBodyAccel + vGravAccel;
vUVWdot += in.Tec2b * in.vGravAccel;
vUVWidot = in.Tb2i * vBodyAccel + in.Tec2i * in.vGravAccel;
}
}

Expand Down Expand Up @@ -370,7 +354,6 @@ void FGAccelerations::bind(void)
PropertyManager->Tie("accelerations/wdot-ft_sec2", this, eW, (PMF)&FGAccelerations::GetUVWdot);

PropertyManager->Tie("accelerations/gravity-ft_sec2", this, &FGAccelerations::GetGravAccelMagnitude);
PropertyManager->Tie("simulation/gravity-model", &gravType);
PropertyManager->Tie("simulation/gravitational-torque", &gravTorque);
PropertyManager->Tie("forces/fbx-weight-lbs", this, eX, (PMF)&FGAccelerations::GetWeight);
PropertyManager->Tie("forces/fby-weight-lbs", this, eY, (PMF)&FGAccelerations::GetWeight);
Expand Down
48 changes: 16 additions & 32 deletions src/models/FGAccelerations.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
------------- Copyright (C) 2011 Jon S. Berndt ([email protected]) -------------
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Further information about the GNU Lesser General Public License can also be found on
the world wide web at http://www.gnu.org.
Further information about the GNU Lesser General Public License can also be
found on the world wide web at http://www.gnu.org.
HISTORY
--------------------------------------------------------------------------------
Expand All @@ -38,8 +38,6 @@ SENTRY
INCLUDES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

#include <vector>

#include "models/FGModel.h"
#include "math/FGColumnVector3.h"
#include "math/LagrangeMultiplier.h"
Expand Down Expand Up @@ -103,18 +101,10 @@ class FGAccelerations : public FGModel {
/// Destructor
~FGAccelerations();

/// These define the indices use to select the gravitation models.
enum eGravType {
/// Evaluate gravity using Newton's classical formula assuming the Earth is spherical
gtStandard,
/// Evaluate gravity using WGS84 formulas that take the Earth oblateness into account
gtWGS84
};

/** Initializes the FGAccelerations class after instantiation and prior to first execution.
The base class FGModel::InitModel is called first, initializing pointers to the
other FGModel objects (and others). */
bool InitModel(void);
bool InitModel(void) override;

/** Runs the state propagation model; called by the Executive
Can pass in a value indicating if the executive is directing the simulation to Hold.
Expand All @@ -123,7 +113,7 @@ class FGAccelerations : public FGModel {
model, which may need to be active to listen on a socket for the
"Resume" command to be given.
@return false if no error */
bool Run(bool Holding);
bool Run(bool Holding) override;

/** Retrieves the body axis acceleration.
Retrieves the computed body axis accelerations based on the
Expand Down Expand Up @@ -212,9 +202,7 @@ class FGAccelerations : public FGModel {
*/
const FGColumnVector3& GetBodyAccel(void) const { return vBodyAccel; }

const FGColumnVector3& GetGravAccel(void) const {return vGravAccel; }

double GetGravAccelMagnitude(void) const { return vGravAccel.Magnitude(); }
double GetGravAccelMagnitude(void) const { return in.vGravAccel.Magnitude(); }

/** Retrieves a component of the acceleration resulting from the applied forces.
Retrieves a component of the ratio between the sum of all forces applied
Expand Down Expand Up @@ -312,8 +300,8 @@ class FGAccelerations : public FGModel {
@param idx the index of the forces component desired (1-based).
@return The ground forces applied on the body.
*/
double GetWeight(int idx) const { return in.Mass * (in.Ti2b * vGravAccel)(idx); }
FGColumnVector3 GetWeight(void) const { return in.Mass * in.Ti2b * vGravAccel; }
double GetWeight(int idx) const { return in.Mass * (in.Tec2b * in.vGravAccel)(idx); }
FGColumnVector3 GetWeight(void) const { return in.Mass * in.Tec2b * in.vGravAccel; }

/** Initializes the FGAccelerations class prior to a new execution.
Initializes the class prior to a new execution when the input data stored
Expand Down Expand Up @@ -348,8 +336,8 @@ class FGAccelerations : public FGModel {
FGColumnVector3 Force;
/// Forces generated by the ground normal reactions expressed in the body frame. Does not account for friction.
FGColumnVector3 GroundForce;
/// Gravity intensity vector using WGS84 formulas (expressed in the ECEF frame).
FGColumnVector3 J2Grav;
/// Gravity intensity vector (expressed in the ECEF frame).
FGColumnVector3 vGravAccel;
/// Angular velocities of the body with respect to the ECI frame (expressed in the body frame).
FGColumnVector3 vPQRi;
/// Angular velocities of the body with respect to the local frame (expressed in the body frame).
Expand All @@ -368,8 +356,6 @@ class FGAccelerations : public FGModel {
double DeltaT;
/// Body mass
double Mass;
/// Gravity intensity assuming the Earth is spherical
double GAccel;
/// List of Lagrange multipliers set by FGLGear for friction forces calculations.
std::vector<LagrangeMultiplier*> *MultipliersList;
} in;
Expand All @@ -379,11 +365,9 @@ class FGAccelerations : public FGModel {
FGColumnVector3 vPQRdot, vPQRidot;
FGColumnVector3 vUVWdot, vUVWidot;
FGColumnVector3 vBodyAccel;
FGColumnVector3 vGravAccel;
FGColumnVector3 vFrictionForces;
FGColumnVector3 vFrictionMoments;

int gravType;
bool gravTorque;

void CalculatePQRdot(void);
Expand All @@ -392,7 +376,7 @@ class FGAccelerations : public FGModel {
void CalculateFrictionForces(double dt);

void bind(void);
void Debug(int from);
void Debug(int from) override;
};
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down
Loading

0 comments on commit 2c44e8d

Please sign in to comment.