Marine systems simulation
CMass.cpp

This source file shows how to implement a mass object. This is only a mass point, with three degrees of freedom. It has six states, position and velocity along global x-, y- and z-axis. Its output is basically the same as its states. Most "work" is done in the OdeFcn. See also the class documentation.

// Includes
#include "CMass.h"
//==================================================================================================
// FUNCTION NAME: CMass constructor
//==================================================================================================
//
CMass::CMass(std::string sSimObjectName, ISimObjectCreator* pCreator)
:SimObject(sSimObjectName)
{
//Input ports.
pCreator->AddInport("Force", 3, &m_pInForce);
// Output ports.
pCreator->AddOutport ("Pos" , 3, PORT_FUNCTION(CMass::Position));
pCreator->AddOutport ("Vel" , 3, PORT_FUNCTION(CMass::Velocity));
// States
m_IStatePos = pCreator->AddState("Pos", 3);
m_IStateVel = pCreator->AddState("Vel", 3);
// Parameters
pCreator->GetDoubleParam("Mass", &m_dMass);
pCreator->GetDoubleParam("g", &m_dg,0.0);
if(m_dMass <= 0)
pCreator->ReportParameterError("Mass", "Must be a real number greater than zero.");
#ifdef FH_VISUALIZATION
pCreator->GetStringParam("Material", m_sMaterial, "Simple/Black");
pCreator->GetStringParam("Mesh", m_sMeshName, "fhSphere.mesh");
pCreator->GetDoubleParam("Scale",&m_dScale, 1.0);
#endif
}
//==================================================================================================
// FUNCTION NAME: OdeFcn
//==================================================================================================
//
void CMass::OdeFcn(const double dT, const double* const adX,
double* const adXDot, const bool bIsMajorTimeStep)
{
const double* const adForceIn = m_pInForce->GetPortValue(dT,adX);
for (int i = 0; i<3 ; i++)
{
adXDot[m_IStatePos + i] = adX[m_IStateVel + i];
adXDot[m_IStateVel + i] = adForceIn[i] / m_dMass ;
if (i==2)
adXDot[m_IStateVel + i] += -m_dg;
}
}
//==================================================================================================
// FUNCTION NAME: Position
//==================================================================================================
//
const double* CMass::Position( const double dT, const double* const adX )
{
return adX + m_IStatePos;
}
//==================================================================================================
// FUNCTION NAME: Velocity
//==================================================================================================
//
const double* CMass::Velocity( const double dT, const double* const adX )
{
return adX + m_IStateVel;
}
#ifdef FH_VISUALIZATION
//==================================================================================================
// FUNCTION NAME: RenderInit
//==================================================================================================
//
void CMass::RenderInit(Ogre::Root* const pOgreRoot, ISimObjectCreator* const pCreator)
{
m_pSceneMgr = pOgreRoot->getSceneManager("main");
m_pRenderNode = m_pSceneMgr->getRootSceneNode()->createChildSceneNode( m_SimObjectName + "FollowNode" );
m_pRenderEntity = m_pSceneMgr->createEntity( m_SimObjectName + "Entity", m_sMeshName);
m_pRenderEntity->setMaterialName(m_sMaterial);
m_pRenderNode->attachObject( m_pRenderEntity );
m_pRenderNode->scale(m_dScale,m_dScale,m_dScale);
}
//==================================================================================================
// FUNCTION NAME: RenderUpdate
//==================================================================================================
//
void CMass::RenderUpdate(const double T, const double* const X)
{
m_pRenderNode->setPosition(
Ogre::Vector3(X[m_IStatePos + 0],X[m_IStatePos + 1],X[m_IStatePos + 2]));
}
#endif
double m_dg
The acceleration of gravity.
Definition: CMass.h:41
int m_IStateVel
The index of the velocity state.
Definition: CMass.h:44
double m_dMass
The mass of the object.
Definition: CMass.h:40
virtual void OdeFcn(const double dT, const double *const adX, double *const adXDot, const bool bIsMajorTimeStep)
Calculates the state derivatives.
CMass(std::string sSimObjectName, ISimObjectCreator *pCreator)
The constructor sets the pointer to the output object and the parser object.
int m_IStatePos
The index of the position state.
Definition: CMass.h:43
ISignalPort * m_pInForce
A pointer to the input force.
Definition: CMass.h:42