MBSim

Project Page
frame.h
00001 /* Copyright (C) 2004-2013 MBSim Development Team
00002  *
00003  * This library is free software; you can redistribute it and/or 
00004  * modify it under the terms of the GNU Lesser General Public 
00005  * License as published by the Free Software Foundation; either 
00006  * version 2.1 of the License, or (at your option) any later version. 
00007  *  
00008  * This library is distributed in the hope that it will be useful, 
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
00011  * Lesser General Public License for more details. 
00012  *  
00013  * You should have received a copy of the GNU Lesser General Public 
00014  * License along with this library; if not, write to the Free Software 
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
00016  *
00017  * Contact: martin.o.foerg@googlemail.com
00018  */
00019 
00020 #ifndef _FRAME_H__
00021 #define _FRAME_H__
00022 
00023 #include "mbsim/element.h"
00024 
00025 #ifdef HAVE_OPENMBVCPPINTERFACE
00026 namespace OpenMBV {
00027   class Frame;
00028   class Arrow;
00029 }
00030 #endif
00031 
00032 namespace MBSim {
00033 
00037   enum FrameFeature {
00038     position, firstTangent, normal, secondTangent, cosy, position_cosy, velocity, angularVelocity, velocity_cosy, velocities, velocities_cosy, all
00039   };
00040 
00048   class Frame : public Element {
00049     public:
00054       Frame(const std::string &name = "dummy");
00055 
00059       virtual ~Frame() {}
00060 
00061       /* INHERITED INTERFACE ELEMENT */
00062       std::string getType() const { return "Frame"; }
00063       virtual void plot(double t, double dt = 1); 
00064       virtual void closePlot(); 
00065       /***************************************************/
00066 
00067       /* INTERFACE FOR DERIVED CLASSES */
00068       virtual int gethSize(int i=0) const { return hSize[i]; }
00069       virtual int gethInd(int i=0) const { return hInd[i]; }
00070       //virtual ObjectInterface* getParent() { return parent; }
00071       //virtual void setParent(ObjectInterface* parent_) { parent = parent_; }
00072       virtual const fmatvec::Vec3& getPosition() const { return WrOP; }
00073       virtual const fmatvec::SqrMat3& getOrientation() const { return AWP; }
00074       virtual fmatvec::Vec3& getPosition() { return WrOP; }
00075       virtual fmatvec::SqrMat3& getOrientation() { return AWP; }
00076       virtual void setPosition(const fmatvec::Vec3 &v) { WrOP = v; }
00077       virtual void setOrientation(const fmatvec::SqrMat3 &AWP_) { AWP = AWP_; }
00078       virtual const fmatvec::Vec3& getVelocity() const { return WvP; } 
00079       virtual const fmatvec::Vec3& getAngularVelocity() const { return WomegaP; }
00080       virtual const fmatvec::Mat3xV& getJacobianOfTranslation(int j=0) const { return WJP[j]; }
00081       virtual const fmatvec::Mat3xV& getJacobianOfRotation(int j=0) const { return WJR[j]; }
00082       virtual const fmatvec::Vec3& getGyroscopicAccelerationOfTranslation(int j=0) const { return WjP[j]; }
00083       virtual const fmatvec::Vec3& getGyroscopicAccelerationOfRotation(int j=0) const { return WjR[j]; }
00084       virtual const fmatvec::Vec3& getAcceleration() const { return WaP; } 
00085       virtual const fmatvec::Vec3& getAngularAcceleration() const { return WpsiP; }
00086       virtual void init(InitStage stage);
00087 #ifdef HAVE_OPENMBVCPPINTERFACE
00088       virtual void enableOpenMBV(double size=1, double offset=1);
00089       void setOpenMBVFrame(OpenMBV::Frame* frame) { openMBVFrame = frame; }
00090       OpenMBV::Frame* getOpenMBVFrame() {return openMBVFrame; }
00091 #endif
00092       /***************************************************/
00093       
00094       /* GETTER / SETTER */
00095       void sethSize(int size, int i=0) { hSize[i] = size; }
00096       void sethInd(int ind, int i=0) { hInd[i] = ind; }
00097 
00098       fmatvec::Vec3& getVelocity() { return WvP; } 
00099       fmatvec::Vec3& getAngularVelocity() { return WomegaP; }
00100       void setVelocity(const fmatvec::Vec3 &v) { WvP = v; } 
00101       void setAngularVelocity(const fmatvec::Vec3 &omega) { WomegaP = omega; }
00102 
00103       void setJacobianOfTranslation(const fmatvec::Mat3xV &WJP_, int j=0) { WJP[j]=WJP_; }
00104       void setGyroscopicAccelerationOfTranslation(const fmatvec::Vec3 &WjP_, int j=0) { WjP[j]=WjP_; }
00105       void setJacobianOfRotation(const fmatvec::Mat3xV &WJR_, int j=0) { WJR[j]=WJR_; }
00106       void setGyroscopicAccelerationOfRotation(const fmatvec::Vec3 &WjR_, int j=0) { WjR[j]=WjR_; }
00107       fmatvec::Mat3xV& getJacobianOfTranslation(int j=0) { return WJP[j]; }
00108       fmatvec::Mat3xV& getJacobianOfRotation(int j=0) { return WJR[j]; }
00109       fmatvec::Vec3& getGyroscopicAccelerationOfTranslation(int j=0) { return WjP[j]; }
00110       fmatvec::Vec3& getGyroscopicAccelerationOfRotation(int j=0) { return WjR[j]; }
00111       fmatvec::Vec3& getAcceleration() { return WaP; } 
00112       fmatvec::Vec3& getAngularAcceleration() { return WpsiP; }
00113       void setAcceleration(const fmatvec::Vec3 &a) { WaP = a; } 
00114       void setAngularAcceleration(const fmatvec::Vec3 &psi) { WpsiP = psi; }
00115 
00116       virtual void initializeUsingXML(MBXMLUtils::TiXmlElement *element);
00117       virtual MBXMLUtils::TiXmlElement* writeXMLFile(MBXMLUtils::TiXmlNode *element);
00118       /***************************************************/
00119 
00120       virtual Element * getByPathSearch(std::string path);
00121 
00122     protected:
00124       // * \brief parent object for plot invocation
00125       // */
00126       //ObjectInterface* parent;
00127 
00131       int hSize[2], hInd[2];
00132 
00136       fmatvec::Vec3 WrOP;
00137 
00141       fmatvec::SqrMat3 AWP;
00142 
00146       fmatvec::Vec3 WvP, WomegaP;
00147 
00151       fmatvec::Mat3xV WJP[2], WJR[2];
00152 
00156       fmatvec::Vec3 WjP[2], WjR[2];
00157 
00161       fmatvec::Vec3 WaP, WpsiP;
00162 
00163 #ifdef HAVE_OPENMBVCPPINTERFACE
00164       OpenMBV::Frame* openMBVFrame;
00165 #endif
00166   };
00167 
00172   class FixedRelativeFrame : public Frame {
00173 
00174     public:
00175       FixedRelativeFrame(const std::string &name = "dummy", const fmatvec::Vec3 &r=fmatvec::Vec3(), const fmatvec::SqrMat3 &A=fmatvec::SqrMat3(fmatvec::EYE), const Frame *refFrame=0) : Frame(name), R(refFrame), RrRP(r), ARP(A) {}
00176 
00177       std::string getType() const { return "FixedRelativeFrame"; }
00178 
00179       virtual void init(InitStage stage);
00180 
00181       void setRelativePosition(const fmatvec::Vec3 &r) { RrRP = r; }
00182       void setRelativeOrientation(const fmatvec::SqrMat3 &A) { ARP = A; }
00183       void setFrameOfReference(const Frame *frame) { R = frame; }
00184       void setFrameOfReference(const std::string &frame) { saved_frameOfReference = frame; }
00185 
00186       const fmatvec::Vec3& getRelativePosition() const { return RrRP; }
00187       const fmatvec::SqrMat3& getRelativeOrientation() const { return ARP; }
00188       const Frame* getFrameOfReference() const { return R; }
00189       const fmatvec::Vec3& getWrRP() const { return WrRP; }
00190 
00191       void updateRelativePosition() { WrRP = R->getOrientation()*RrRP; }
00192       void updatePosition() { updateRelativePosition(); setPosition(R->getPosition() + WrRP); }
00193       void updateOrientation() { setOrientation(R->getOrientation()*ARP); }
00194       void updateVelocity() { setVelocity(R->getVelocity() + crossProduct(R->getAngularVelocity(), WrRP)); } 
00195       void updateAngularVelocity() { setAngularVelocity(R->getAngularVelocity()); }
00196       void updateStateDependentVariables() {
00197         updatePosition();
00198         updateOrientation();
00199         updateVelocity();
00200         updateAngularVelocity();
00201       }
00202       void updateJacobians(int j=0) {
00203         fmatvec::SqrMat3 tWrRP = tilde(WrRP);
00204         setJacobianOfTranslation(R->getJacobianOfTranslation(j) - tWrRP*R->getJacobianOfRotation(j),j);
00205         setJacobianOfRotation(R->getJacobianOfRotation(j),j);
00206         setGyroscopicAccelerationOfTranslation(R->getGyroscopicAccelerationOfTranslation(j) - tWrRP*R->getGyroscopicAccelerationOfRotation(j) + crossProduct(R->getAngularVelocity(),crossProduct(R->getAngularVelocity(),WrRP)),j);
00207         setGyroscopicAccelerationOfRotation(R->getGyroscopicAccelerationOfRotation(j),j);
00208       }
00209       void updateStateDerivativeDependentVariables(const fmatvec::Vec &ud) { 
00210         setAcceleration(getJacobianOfTranslation()*ud + getGyroscopicAccelerationOfTranslation()); 
00211         setAngularAcceleration(getJacobianOfRotation()*ud + getGyroscopicAccelerationOfRotation());
00212       }
00213 
00214       virtual void initializeUsingXML(MBXMLUtils::TiXmlElement *element);
00215       virtual MBXMLUtils::TiXmlElement* writeXMLFile(MBXMLUtils::TiXmlNode *element);
00216 
00217     protected:
00218       const Frame *R;
00219       fmatvec::Vec3 RrRP, WrRP;
00220       fmatvec::SqrMat3 ARP;
00221       std::string saved_frameOfReference;
00222   };
00223 
00224 }
00225 
00226 #endif /* _FRAME_H_ */
00227