ocra-recipes
Doxygen documentation for the ocra-recipes repository
Model.h
Go to the documentation of this file.
1 
10 #ifndef _OCRACONTROL_MODEL_H_
11 #define _OCRACONTROL_MODEL_H_
12 
13 // includes
14 #include <ocra/util/MathTypes.h>
16 #include "ocra/optim/Variable.h"
19 #include <ocra/util/Macros.h>
20 #include <Eigen/Lgsm>
21 
22 #include <string>
23 #include <iostream>
24 
25 #include <yarp/os/Semaphore.h>
26 
27 namespace ocra
28 {
38  class Model : public ObserverSubject, public NamedInstance
39  {
41  public:
42  Model(const std::string& name, int ndofs, bool freeRoot,
43  const std::string& jointTorqueVariableName = "tau",
44  const std::string& forceVariableName = "f",
45  const std::string& configurationVariableName = "q",
46  const std::string& internalDofsSuffix = "_int",
47  const std::string& externalDofsSuffix = "_root");
48  public:
49  virtual ~Model();
50 
51  // ------------------------ public interface --------------------------------
52  public:
53  //set/get configuration
54  void setJointPositions(const Eigen::VectorXd& q);
55  void setJointVelocities(const Eigen::VectorXd& q_dot);
56  void setFreeFlyerPosition(const Eigen::Displacementd& H_root);
57  void setFreeFlyerVelocity(const Eigen::Twistd& T_root);
58  void setState(const Eigen::VectorXd& q, const Eigen::VectorXd& q_dot);
59  void setState(const Eigen::Displacementd& H_root, const Eigen::VectorXd& q, const Eigen::Twistd& T_root, const Eigen::VectorXd& q_dot);
60  virtual const Eigen::VectorXd& getJointPositions() const = 0;
61  virtual const Eigen::VectorXd& getJointVelocities() const = 0;
62  virtual const Eigen::VectorXd& getJointAccelerations() const { std::cout << "getJointAccelerations() has not been implemented" << std::endl; };
63  virtual const Eigen::VectorXd& getJointTorques() const = 0;
64  virtual const Eigen::Displacementd& getFreeFlyerPosition() const = 0;
65  virtual const Eigen::Twistd& getFreeFlyerVelocity() const = 0;
66 
67  //get whole body data
68  //dofs
69  int nbDofs() const;
70  int nbInternalDofs() const;
71  bool hasFixedRoot() const;
72  virtual int nbSegments() const = 0;
73  virtual const Eigen::VectorXd& getActuatedDofs() const = 0;
74  virtual const Eigen::VectorXd& getJointLowerLimits() const = 0;
75  virtual const Eigen::VectorXd& getJointUpperLimits() const = 0;
76  //CoM
77  virtual double getMass() const = 0;
78  virtual const Eigen::Vector3d& getCoMPosition() const = 0;
79  virtual const Eigen::Vector3d& getCoMVelocity() const = 0;
80  virtual const Eigen::Vector3d& getCoMAcceleration() const { std::cout << "getCoMAcceleration() Not implemented" << std::endl; };
81  virtual const Eigen::Vector3d& getCoMAngularVelocity() const{ std::cout << "getCoMAngularVelocity() Not implemented" << std::endl; }
82  virtual const Eigen::Vector3d& getCoMJdotQdot() const = 0;
83  virtual const Eigen::Matrix<double,3,Eigen::Dynamic>& getCoMJacobian() const = 0;
84  virtual const Eigen::Matrix<double,3,Eigen::Dynamic>& getCoMAngularJacobian() const{ std::cout << "getCoMAngularVelocity() Not implemented" << std::endl; }
85  virtual const Eigen::Matrix<double,3,Eigen::Dynamic>& getCoMJacobianDot() const = 0;
86  //dynamic/static equation terms
87  virtual const Eigen::MatrixXd& getInertiaMatrix() const = 0;
88  virtual const Eigen::MatrixXd& getInertiaMatrixInverse() const = 0;
89  virtual const Eigen::MatrixXd& getDampingMatrix() const = 0;
90  virtual const Eigen::VectorXd& getNonLinearTerms() const = 0;
91  virtual const Eigen::VectorXd& getLinearTerms() const = 0;
92  virtual const Eigen::VectorXd& getGravityTerms() const = 0;
93 
94  //segment data
95  virtual const Eigen::Displacementd& getSegmentPosition(int index) const = 0;
96  virtual const Eigen::Twistd& getSegmentVelocity(int index) const = 0;
97  virtual const Eigen::Matrix<double,6,Eigen::Dynamic>& getSegmentJacobian(int index) const = 0;
98  virtual const Eigen::Matrix<double,6,Eigen::Dynamic>& getSegmentJdot(int index) const = 0;
99  virtual const Eigen::Twistd& getSegmentJdotQdot(int index) const = 0;
100  virtual const Eigen::Matrix<double,6,Eigen::Dynamic>& getJointJacobian(int index) const = 0;
101  virtual double getSegmentMass(int index) const = 0;
102  virtual const Eigen::Vector3d& getSegmentCoM(int index) const = 0;
103  virtual const Eigen::Matrix<double, 6, 6>& getSegmentMassMatrix(int index) const = 0;
104  virtual const Eigen::Vector3d& getSegmentMomentsOfInertia(int index) const = 0;
105  virtual const Eigen::Rotation3d& getSegmentInertiaAxes(int index) const = 0;
106 
107  //segment data
108  const Eigen::Displacementd& getSegmentPosition(const std::string& segName) const {
109  return getSegmentPosition(getSegmentIndex(segName));
110  }
111  const Eigen::Twistd& getSegmentVelocity(const std::string& segName) const {
112  return getSegmentVelocity(getSegmentIndex(segName));
113  }
114  const Eigen::Matrix<double,6,Eigen::Dynamic>& getSegmentJacobian(const std::string& segName) const {
115  return getSegmentJacobian(getSegmentIndex(segName));
116  }
117  const Eigen::Matrix<double,6,Eigen::Dynamic>& getSegmentJdot(const std::string& segName) const {
118  return getSegmentJdot(getSegmentIndex(segName));
119  }
120  const Eigen::Twistd& getSegmentJdotQdot(const std::string& segName) const {
121  return getSegmentJdotQdot(getSegmentIndex(segName));
122  }
123  const Eigen::Matrix<double,6,Eigen::Dynamic>& getJointJacobian(const std::string& segName) const {
124  return getJointJacobian(getSegmentIndex(segName));
125  }
126  double getSegmentMass(const std::string& segName) const {
127  return getSegmentMass(getSegmentIndex(segName));
128  }
129  const Eigen::Vector3d& getSegmentCoM(const std::string& segName) const {
130  return getSegmentCoM(getSegmentIndex(segName));
131  }
132  const Eigen::Matrix<double, 6, 6>& getSegmentMassMatrix(const std::string& segName) const {
133  return getSegmentMassMatrix(getSegmentIndex(segName));
134  }
135  const Eigen::Vector3d& getSegmentMomentsOfInertia(const std::string& segName) const {
137  }
138  const Eigen::Rotation3d& getSegmentInertiaAxes(const std::string& segName) const {
139  return getSegmentInertiaAxes(getSegmentIndex(segName));
140  }
141 
142  void setJointDamping(const Eigen::VectorXd& damping);
143  const Eigen::VectorXd& getJointDamping() const;
144 
145  //variables
147  Variable& getVelocityVariable() const;
150 
157  yarp::os::Semaphore modelMutex;
158 
159  //subModels
161 
162  //utils
163  int getSegmentIndex(const std::string& name) const;
164  const std::string& getSegmentName(int index) const;
165 
166  // ------------------------ public methods ----------------------------------
167  public:
168  //TODO: Clean this shit up...
169  //-----------------------------BEGINING OF SHIT-------------------------------//
170  int getDofIndex(const std::string& name) const;
171  const std::string& getDofName(int index) const;
172  const std::string DofName(const std::string& name) const;
173  const std::string SegmentName(const std::string& name) const;
174  virtual const std::string& getJointName (int index) const = 0;
175 
176  //-----------------------------END OF SHIT------------------------------------//
177 
178 
179  // ------------------------ public static methods ---------------------------
180  public:
181 
182  // ------------------------ protected methods -------------------------------
183  protected:
184  virtual void doSetState(const Eigen::VectorXd& q, const Eigen::VectorXd& q_dot){};
185  virtual void doSetState(const Eigen::Displacementd& H_root, const Eigen::VectorXd& q, const Eigen::Twistd& T_root, const Eigen::VectorXd& q_dot){};
186 
187  virtual void doSetJointPositions(const Eigen::VectorXd& q) = 0;
188  virtual void doSetJointVelocities(const Eigen::VectorXd& q_dot) = 0;
189  virtual void doSetFreeFlyerPosition(const Eigen::Displacementd& H_root) = 0;
190  virtual void doSetFreeFlyerVelocity(const Eigen::Twistd& T_root) = 0;
191 
192  virtual int doGetSegmentIndex(const std::string& name) const = 0;
193  virtual const std::string& doGetSegmentName(int index) const = 0;
194 
195  virtual void doInvalidate() {}
196 
197  //TODO: Clean this shit up...
198  //-----------------------------BEGINING OF SHIT-------------------------------//
199  virtual int doGetDofIndex (const std::string& name) const;
200  virtual const std::string& doGetDofName (int index) const;
201  virtual const std::string doSegmentName (const std::string& name) const;
202  virtual const std::string doDofName (const std::string& name) const;
203  //-----------------------------END OF SHIT------------------------------------//
204 
205 
206  // ------------------------ protected static methods ------------------------
207  protected:
208 
209  // ------------------------ private methods ---------------------------------
210  private:
211  void invalidate(int timestamp);
212 
213  // ------------------------ private static methods --------------------------
214  private:
215 
216  // ------------------------ protected members -------------------------------
217  protected:
218 
219  // ------------------------ protected static members ------------------------
220  protected:
221 
222  // ------------------------ private members ---------------------------------
223  private:
224  int _dofs;
225  bool _fixedRoot;
226  VectorXd _jointDamping;
227  Variable* _tau;
228  Variable* _q;
229  Variable* _q_dot;
230  Variable* _q_ddot;
231 
232  ModelContacts* _modelContacts;
233  // ------------------------ private static members --------------------------
234  private:
235  static const int FREE_DOFS = 6;
236 
237  // ------------------------ friendship declarations -------------------------
238  };
239 }
240 
241 
242 #endif //_OCRACONTROL_MODEL_H_
243 
244 // cmake:sourcegroup=Api
const Eigen::Matrix< double, 6, Eigen::Dynamic > & getSegmentJacobian(const std::string &segName) const
Definition: Model.h:114
virtual const Eigen::Twistd & getFreeFlyerVelocity() const =0
virtual int doGetDofIndex(const std::string &name) const
Definition: Model.cpp:255
Variable & getRootVelocityVariable() const
Definition: Model.cpp:157
Variable & getInternalVelocityVariable() const
Definition: Model.cpp:163
virtual ~Model()
Definition: Model.cpp:37
const Eigen::Matrix< double, 6, Eigen::Dynamic > & getJointJacobian(const std::string &segName) const
Definition: Model.h:123
int nbInternalDofs() const
Definition: Model.cpp:59
const Eigen::Displacementd & getSegmentPosition(const std::string &segName) const
Definition: Model.h:108
virtual const Eigen::Matrix< double, 3, Eigen::Dynamic > & getCoMAngularJacobian() const
Definition: Model.h:84
virtual const std::string doDofName(const std::string &name) const
Definition: Model.cpp:272
const Eigen::Vector3d & getSegmentCoM(const std::string &segName) const
Definition: Model.h:129
void setJointVelocities(const Eigen::VectorXd &q_dot)
Definition: Model.cpp:80
Declaration file of the ModelContacts class.
virtual const Eigen::VectorXd & getJointUpperLimits() const =0
double getSegmentMass(const std::string &segName) const
Definition: Model.h:126
virtual const Eigen::Displacementd & getFreeFlyerPosition() const =0
virtual const Eigen::VectorXd & getJointPositions() const =0
ModelContacts class.
Definition: ModelContacts.h:52
virtual const Eigen::VectorXd & getGravityTerms() const =0
virtual const Eigen::Vector3d & getCoMAcceleration() const
Definition: Model.h:80
const Eigen::VectorXd & getJointDamping() const
Definition: Model.cpp:208
virtual const Eigen::VectorXd & getJointTorques() const =0
virtual const std::string & doGetSegmentName(int index) const =0
Definition: Model.cpp:218
const Eigen::Rotation3d & getSegmentInertiaAxes(const std::string &segName) const
Definition: Model.h:138
virtual const Eigen::VectorXd & getActuatedDofs() const =0
Variable & getRootAccelerationVariable() const
Definition: Model.cpp:171
virtual const Eigen::Vector3d & getCoMJdotQdot() const =0
virtual const Eigen::VectorXd & getJointVelocities() const =0
virtual void doSetState(const Eigen::VectorXd &q, const Eigen::VectorXd &q_dot)
Definition: Model.h:184
Model class.
Definition: Model.h:38
virtual const Eigen::VectorXd & getNonLinearTerms() const =0
#define DEFINE_CLASS_POINTER_TYPEDEFS(Class)
Definition: Macros.h:8
virtual void doSetFreeFlyerVelocity(const Eigen::Twistd &T_root)=0
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
virtual const Eigen::Twistd & getSegmentJdotQdot(int index) const =0
virtual const Eigen::Vector3d & getCoMPosition() const =0
void setJointPositions(const Eigen::VectorXd &q)
Definition: Model.cpp:72
yarp::os::Semaphore modelMutex
Definition: Model.h:157
virtual const Eigen::Matrix< double, 6, 6 > & getSegmentMassMatrix(int index) const =0
Variable & getConfigurationVariable() const
Definition: Model.cpp:123
virtual const std::string & getJointName(int index) const =0
virtual void doSetJointVelocities(const Eigen::VectorXd &q_dot)=0
virtual const Eigen::Vector3d & getCoMVelocity() const =0
Variable & getInternalAccelerationVariable() const
Definition: Model.cpp:177
virtual const Eigen::Twistd & getSegmentVelocity(int index) const =0
const Eigen::Matrix< double, 6, 6 > & getSegmentMassMatrix(const std::string &segName) const
Definition: Model.h:132
virtual double getMass() const =0
int getSegmentIndex(const std::string &name) const
Definition: Model.cpp:192
void setState(const Eigen::VectorXd &q, const Eigen::VectorXd &q_dot)
Variable & getRootConfigurationVariable() const
Definition: Model.cpp:143
virtual const std::string doSegmentName(const std::string &name) const
Definition: Model.cpp:266
virtual const Eigen::Matrix< double, 3, Eigen::Dynamic > & getCoMJacobian() const =0
Variable & getInternalConfigurationVariable() const
Definition: Model.cpp:149
int nbDofs() const
Definition: Model.cpp:54
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
void setJointDamping(const Eigen::VectorXd &damping)
Definition: Model.cpp:203
virtual const std::string & doGetDofName(int index) const
Definition: Model.cpp:260
virtual void doInvalidate()
Definition: Model.h:195
int getDofIndex(const std::string &name) const
Definition: Model.cpp:234
void setFreeFlyerVelocity(const Eigen::Twistd &T_root)
Definition: Model.cpp:96
void setFreeFlyerPosition(const Eigen::Displacementd &H_root)
Definition: Model.cpp:88
virtual const Eigen::Vector3d & getSegmentCoM(int index) const =0
const Eigen::Twistd & getSegmentVelocity(const std::string &segName) const
Definition: Model.h:111
const std::string & getSegmentName(int index) const
Definition: Model.cpp:197
virtual const Eigen::Rotation3d & getSegmentInertiaAxes(int index) const =0
virtual const Eigen::Vector3d & getSegmentMomentsOfInertia(int index) const =0
virtual const Eigen::VectorXd & getJointAccelerations() const
Definition: Model.h:62
Variable & getJointTorqueVariable() const
Definition: Model.cpp:138
Variable & getAccelerationVariable() const
Definition: Model.cpp:133
const std::string SegmentName(const std::string &name) const
Definition: Model.cpp:245
const std::string DofName(const std::string &name) const
Definition: Model.cpp:250
virtual const Eigen::Matrix< double, 6, Eigen::Dynamic > & getJointJacobian(int index) const =0
virtual const Eigen::MatrixXd & getInertiaMatrix() const =0
bool hasFixedRoot() const
Definition: Model.cpp:67
virtual const Eigen::VectorXd & getLinearTerms() const =0
virtual const Eigen::MatrixXd & getDampingMatrix() const =0
virtual const Eigen::Matrix< double, 6, Eigen::Dynamic > & getSegmentJdot(int index) const =0
virtual const Eigen::MatrixXd & getInertiaMatrixInverse() const =0
virtual const Eigen::Displacementd & getSegmentPosition(int index) const =0
virtual const Eigen::VectorXd & getJointLowerLimits() const =0
Model(const std::string &name, int ndofs, bool freeRoot, const std::string &jointTorqueVariableName="tau", const std::string &forceVariableName="f", const std::string &configurationVariableName="q", const std::string &internalDofsSuffix="_int", const std::string &externalDofsSuffix="_root")
Definition: Model.cpp:6
virtual void doSetState(const Eigen::Displacementd &H_root, const Eigen::VectorXd &q, const Eigen::Twistd &T_root, const Eigen::VectorXd &q_dot)
Definition: Model.h:185
Declaration file of the Variable class.
virtual void doSetJointPositions(const Eigen::VectorXd &q)=0
Declaration file of the Observer, Subject and ObserverSubject classes.
const Eigen::Matrix< double, 6, Eigen::Dynamic > & getSegmentJdot(const std::string &segName) const
Definition: Model.h:117
Declaration file of the OptimizationVariable class.
virtual const Eigen::Matrix< double, 6, Eigen::Dynamic > & getSegmentJacobian(int index) const =0
virtual void doSetFreeFlyerPosition(const Eigen::Displacementd &H_root)=0
const Eigen::Vector3d & getSegmentMomentsOfInertia(const std::string &segName) const
Definition: Model.h:135
ModelContacts & getModelContacts() const
Definition: Model.cpp:186
virtual const Eigen::Vector3d & getCoMAngularVelocity() const
Definition: Model.h:81
virtual int doGetSegmentIndex(const std::string &name) const =0
Definition: Model.cpp:213
const Eigen::Twistd & getSegmentJdotQdot(const std::string &segName) const
Definition: Model.h:120
const std::string & getDofName(int index) const
Definition: Model.cpp:239
virtual const Eigen::Matrix< double, 3, Eigen::Dynamic > & getCoMJacobianDot() const =0
virtual double getSegmentMass(int index) const =0
virtual int nbSegments() const =0
Variable & getVelocityVariable() const
Definition: Model.cpp:128