ocra-recipes
Doxygen documentation for the ocra-recipes repository
ModelContacts.cpp
Go to the documentation of this file.
2 #include "ocra/control/Model.h"
3 #include "ocra/control/Feature.h"
4 #include "ocra/optim/Variable.h"
6 #include <algorithm>
7 #include <vector>
8 #include <utility>
9 
10 namespace ocra
11 {
13  {
16  MatrixXd Jct;
18  std::vector<const Feature*> contactFeatures;
19  std::vector<Variable*> forceVariables;
20 
22  : model(m)
23  , contactForcesVariable(m.getName()+"_fc")
24  , Jct_isUpToDate(false)
25  {}
26  };
27 
29  : pimpl(new Pimpl(model))
30  {
31  pimpl->model.connect<EVT_CHANGE_VALUE>(*this, &ModelContacts::invalidate);
32  }
33 
35  {
36  pimpl->model.disconnect<EVT_CHANGE_VALUE>(*this, &ModelContacts::invalidate);
37  }
38 
40  {
41  ocra_assert(std::find(pimpl->contactFeatures.begin(), pimpl->contactFeatures.end(), &contactFeature) == pimpl->contactFeatures.end()
42  && "This contact point was already added");
43  ocra_assert(std::find(pimpl->forceVariables.begin(), pimpl->forceVariables.end(), &f) == pimpl->forceVariables.end()
44  && "This force variable was already added");
45  ocra_assert(contactFeature.getDimension()==3 && "Only 3D features can be used for contact points");
46 
47  // add the variable
48  pimpl->contactForcesVariable.add(f);
49  pimpl->forceVariables.push_back(&f);
50 
51  // add contact in vector
52  pimpl->contactFeatures.push_back(&contactFeature);
53 
54  // invalidate jacobian
55  invalidate();
56 
57  return *this;
58  }
59 
61  {
62  std::vector<Variable*>::iterator it = std::find(pimpl->forceVariables.begin(), pimpl->forceVariables.end(), &f);
63 
64  if (it != pimpl->forceVariables.end())
65  {
66  size_t i = std::distance(pimpl->forceVariables.begin(), it);
67  pimpl->forceVariables.erase(it);
68  pimpl->contactForcesVariable.remove(f);
69  pimpl->contactFeatures.erase(pimpl->contactFeatures.begin()+i);
70  invalidate();
71  }
72 
73  return *this;
74  }
75 
77  {
78  while (pimpl->forceVariables.size()>0)
79  {
80  pimpl->contactFeatures.pop_back();
81  pimpl->contactForcesVariable.remove(*pimpl->forceVariables.back());
82  pimpl->forceVariables.pop_back();
83  }
84 
85  invalidate();
86  }
87 
89  {
90  return pimpl->contactForcesVariable;
91  }
92 
93  const MatrixXd& ModelContacts::getJct() const
94  {
95  if(!pimpl->Jct_isUpToDate)
96  {
97  pimpl->Jct.resize(pimpl->model.nbDofs(), 3*nbContactPoints());
98  for(int i = 0; i < nbContactPoints(); ++i)
99  pimpl->Jct.block(0,3*i,pimpl->Jct.rows(),3) = getContactFeature(i).computeJacobian().transpose();
100  pimpl->Jct_isUpToDate = true;
101  }
102 
103  return pimpl->Jct;
104  }
105 
107  {
108  return static_cast<int>(pimpl->contactFeatures.size());
109  }
110 
111  std::pair<const Variable*, const Feature*> ModelContacts::getContactPoint(int index) const
112  {
113  return std::make_pair(pimpl->forceVariables[index], pimpl->contactFeatures[index]);
114  }
115 
117  {
118  return *pimpl->forceVariables[index];
119  }
120 
122  {
123  return *pimpl->contactFeatures[index];
124  }
125 
127  {
128  return pimpl->model;
129  }
130 
131  void ModelContacts::invalidate(int timestamp)
132  {
133  pimpl->Jct_isUpToDate = false;
134  }
135 }
136 
137 // cmake:sourcegroup=Api
std::pair< const Variable *, const Feature * > getContactPoint(int index) const
virtual const Eigen::MatrixXd & computeJacobian(const Feature &featureDes) const =0
Declaration file of the ModelContacts class.
std::vector< Variable * > forceVariables
ModelContacts class.
Definition: ModelContacts.h:52
const Feature & getContactFeature(int index) const
Declaration file of the Model class.
A class hierarchy to compute task errors based on control frames.
Model class.
Definition: Model.h:38
std::vector< const Feature * > contactFeatures
Feature interface, used by tasks to compute errors and jacobians.
Definition: Feature.h:55
Variable & getContactForcesVariable() const
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
ModelContacts(Model &model)
const Variable & getContactForceVariable(int index) const
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
ModelContacts & addContactPoint(Variable &f, const Feature &contactFeature)
const Eigen::MatrixXd & getJct() const
ModelContacts & removeContactPoint(Variable &f)
A concatenation of base variables and other composite variables.
Definition: Variable.h:357
virtual int getDimension() const =0
int nbContactPoints() const
#define ocra_assert(ocra_expression)
Definition: ocra_assert.h:45
Declaration file of the Variable class.
double distance(double a, double b)
Definition: QuadProg++.cpp:714
CompositeVariable contactForcesVariable
const Model & getModel() const