ocra-recipes
Doxygen documentation for the ocra-recipes repository
Model.cpp
Go to the documentation of this file.
1 #include "ocra/control/Model.h"
2 
3 
4 namespace ocra
5 {
6  Model::Model(const std::string& name, int ndofs, bool freeRoot, const std::string& jointTorqueVariableName,
7  const std::string& forceVariableName, const std::string& configurationVariableName,
8  const std::string& internalDofsSuffix, const std::string& externalDofsSuffix)
9  : NamedInstance(name)
10  , _dofs(ndofs)
11  , _fixedRoot(!freeRoot)
12  {
13  _modelContacts = new ModelContacts(*this);
14  if (freeRoot)
15  {
16  int n_int = _dofs-FREE_DOFS;
17  _tau = new BaseVariable(jointTorqueVariableName, n_int);
18  BaseVariable* q_root = new BaseVariable(configurationVariableName+externalDofsSuffix, FREE_DOFS);
19  BaseVariable* q_int = new BaseVariable(configurationVariableName+internalDofsSuffix, n_int);
20  _q = new CompositeVariable(configurationVariableName, *q_root, *q_int);
21  }
22  else
23  {
24  _tau = new BaseVariable(jointTorqueVariableName, _dofs);
25  _q = new BaseVariable(configurationVariableName, _dofs);
26  }
27  _q_dot = &_q->getTimeDerivative();
28  _q_ddot = &_q_dot->getTimeDerivative();
29  _jointDamping = VectorXd::Zero(_tau->getSize());
30 
31 
32  _q->connect<EVT_CHANGE_VALUE>(*this, &Model::invalidate);
33  _q_dot->connect<EVT_CHANGE_VALUE>(*this, &Model::invalidate);
34 
35  }
36 
38  {
39  _q->disconnect<EVT_CHANGE_VALUE>(*this, &Model::invalidate);
40  _q_dot->disconnect<EVT_CHANGE_VALUE>(*this, &Model::invalidate);
41 
42  if (!_fixedRoot)
43  {
44  delete &(*static_cast<CompositeVariable*>(_q))(1);
45  delete &(*static_cast<CompositeVariable*>(_q))(0);
46  }
47 
48  delete _q;
49  delete _tau;
50  delete _modelContacts;
51  }
52 
53 
54  int Model::nbDofs() const
55  {
56  return _dofs;
57  }
58 
60  {
61  if (!_fixedRoot)
62  return _dofs-FREE_DOFS;
63  else
64  return _dofs;
65  }
66 
67  bool Model::hasFixedRoot() const
68  {
69  return _fixedRoot;
70  }
71 
72  void Model::setJointPositions(const VectorXd& q)
73  {
74  this->modelMutex.wait();
77  this->modelMutex.post();
78  }
79 
80  void Model::setJointVelocities(const VectorXd& q_dot)
81  {
82  this->modelMutex.wait();
84  doSetJointVelocities(q_dot);
85  this->modelMutex.post();
86  }
87 
88  void Model::setFreeFlyerPosition(const Eigen::Displacementd& H_root)
89  {
90  assert(!_fixedRoot);
91  doSetFreeFlyerPosition(H_root);
92  //we do not set H_root in q_root on purpose: H_root is in SE3 while q_root is in R^6
93  propagate<EVT_CHANGE_VALUE>();
94  }
95 
96  void Model::setFreeFlyerVelocity(const Eigen::Twistd& T_root)
97  {
98  assert(!_fixedRoot);
99  doSetFreeFlyerVelocity(T_root);
100  getRootVelocityVariable().setValue(T_root.get());
101  propagate<EVT_CHANGE_VALUE>();
102  }
103 
104  void Model::setState(const VectorXd& q, const VectorXd& q_dot)
105  {
107  setJointVelocities(q_dot);
108  doSetState(q, q_dot);
109  }
110 
111  void Model::setState(const Eigen::Displacementd& H_root, const VectorXd& q, const Eigen::Twistd& T_root, const VectorXd& q_dot)
112  {
114  setJointVelocities(q_dot);
115  if(!_fixedRoot)
116  {
117  setFreeFlyerPosition(H_root);
118  setFreeFlyerVelocity(T_root);
119  }
120  doSetState(H_root, q, T_root, q_dot);
121  }
122 
124  {
125  return *_q;
126  }
127 
129  {
130  return *_q_dot;
131  }
132 
134  {
135  return *_q_ddot;
136  }
137 
139  {
140  return *_tau;
141  }
142 
144  {
145  assert(!_fixedRoot);
146  return (*static_cast<CompositeVariable*>(_q))(0);
147  }
148 
150  {
151  if (_fixedRoot)
152  return *_q;
153  else
154  return (*static_cast<CompositeVariable*>(_q))(1);
155  }
156 
158  {
159  assert(!_fixedRoot);
160  return (*static_cast<CompositeVariable*>(_q_dot))(0);
161  }
162 
164  {
165  if (_fixedRoot)
166  return *_q_dot;
167  else
168  return (*static_cast<CompositeVariable*>(_q_dot))(1);
169  }
170 
172  {
173  assert(!_fixedRoot);
174  return (*static_cast<CompositeVariable*>(_q_ddot))(0);
175  }
176 
178  {
179  if (_fixedRoot)
180  return *_q_ddot;
181  else
182  return (*static_cast<CompositeVariable*>(_q_ddot))(1);
183  }
184 
185 
187  {
188  return* _modelContacts;
189  }
190 
191 
192  int Model::getSegmentIndex(const std::string& name) const
193  {
194  return doGetSegmentIndex(name);
195  }
196 
197  const std::string& Model::getSegmentName(int index) const
198  {
199  assert(index>=0 && index <nbSegments());
200  return doGetSegmentName(index);
201  }
202 
203  void Model::setJointDamping(const VectorXd& damping)
204  {
205  _jointDamping = damping;
206  }
207 
208  const VectorXd& Model::getJointDamping() const
209  {
210  return _jointDamping;
211  }
212 
213  int Model::doGetSegmentIndex(const std::string& name) const
214  {
215  throw std::runtime_error("[Model::doGetSegmentIndex] This function was not overriden for a specific model");
216  }
217 
218  const std::string& Model::doGetSegmentName(int index) const
219  {
220  throw std::runtime_error("[Model::doGetSegmentName] This function was not overriden for a specific model");
221  }
222 
223  void Model::invalidate(int timestamp)
224  {
225  doInvalidate();
228  if (!_fixedRoot)
229  doSetFreeFlyerVelocity(Twistd(getRootVelocityVariable().getValue()));
230  }
231 
232  //TODO: Clean this shit up...
233  //-----------------------------BEGINING OF SHIT-------------------------------//
234  int Model::getDofIndex(const std::string& name) const
235  {
236  return doGetDofIndex(name);
237  }
238 
239  const std::string& Model::getDofName(int index) const
240  {
241  assert(index>=0 && index < nbDofs());
242  return doGetDofName(index);
243  }
244 
245  const std::string Model::SegmentName(const std::string& name) const
246  {
247  return doSegmentName(name);
248  }
249 
250  const std::string Model::DofName(const std::string& name) const
251  {
252  return doDofName(name);
253  }
254 
255  int Model::doGetDofIndex(const std::string& name) const
256  {
257  throw std::runtime_error("[Model::doGetDofIndex] This function was not overriden for a specific model");
258  }
259 
260  const std::string& Model::doGetDofName(int index) const
261  {
262  throw std::runtime_error("[Model::doGetDofName] This function was not overriden for a specific model");
263  }
264 
265  // Translates segment name to the format of the model (for example orcXdeModel should be robot.name)
266  const std::string Model::doSegmentName(const std::string& name) const
267  {
268  throw std::runtime_error("[Model::doSegmentName] This function was not overriden for a specific model");
269  }
270 
271  // Translates DOF name to the format of the model (for example orcXdeModel should be robot.name)
272  const std::string Model::doDofName(const std::string& name) const
273  {
274  throw std::runtime_error("[Model::doDofName] This function was not overriden for a specific model");
275  }
276  //-----------------------------END OF SHIT------------------------------------//
277 
278 
279 }
280 
281 // cmake:sourcegroup=Api
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
virtual Variable & getTimeDerivative()
Get the time derivative/primitive of the variable.
Definition: Variable.cpp:106
int nbInternalDofs() const
Definition: Model.cpp:59
virtual const std::string doDofName(const std::string &name) const
Definition: Model.cpp:272
void setJointVelocities(const Eigen::VectorXd &q_dot)
Definition: Model.cpp:80
void setValue(const VectorXd &value)
Definition: Variable.cpp:99
ModelContacts class.
Definition: ModelContacts.h:52
Declaration file of the Model class.
const Eigen::VectorXd & getJointDamping() const
Definition: Model.cpp:208
virtual const std::string & doGetSegmentName(int index) const =0
Definition: Model.cpp:218
Variable & getRootAccelerationVariable() const
Definition: Model.cpp:171
virtual void doSetState(const Eigen::VectorXd &q, const Eigen::VectorXd &q_dot)
Definition: Model.h:184
virtual void doSetFreeFlyerVelocity(const Eigen::Twistd &T_root)=0
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
void setJointPositions(const Eigen::VectorXd &q)
Definition: Model.cpp:72
yarp::os::Semaphore modelMutex
Definition: Model.h:157
Variable & getConfigurationVariable() const
Definition: Model.cpp:123
virtual void doSetJointVelocities(const Eigen::VectorXd &q_dot)=0
Variable & getInternalAccelerationVariable() const
Definition: Model.cpp:177
int getSegmentIndex(const std::string &name) const
Definition: Model.cpp:192
int getSize() const
Definition: Variable.cpp:81
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
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
const std::string & getSegmentName(int index) const
Definition: Model.cpp:197
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
bool hasFixedRoot() const
Definition: Model.cpp:67
A concatenation of base variables and other composite variables.
Definition: Variable.h:357
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 doSetJointPositions(const Eigen::VectorXd &q)=0
void disconnect(Derived &object, void(Base::*callbackToErase)(int)) const
Disconnect non-static method.
void connect(Derived &object, void(Base::*newCallback)(int)) const
Call this method to register a non-static method as a callback.
virtual void doSetFreeFlyerPosition(const Eigen::Displacementd &H_root)=0
ModelContacts & getModelContacts() const
Definition: Model.cpp:186
Implements a basic variable.
Definition: Variable.h:304
virtual int doGetSegmentIndex(const std::string &name) const =0
Definition: Model.cpp:213
const std::string & getDofName(int index) const
Definition: Model.cpp:239
virtual int nbSegments() const =0
Variable & getVelocityVariable() const
Definition: Model.cpp:128