ocra-recipes
Doxygen documentation for the ocra-recipes repository
ContactAvoidanceConstraint.cpp
Go to the documentation of this file.
1 
9 
10 
11 using namespace ocra;
12 
13 
14 
18 
31  : NamedInstance("wOcra Contact Avoidance Function")
32  , CoupledInputOutputSize(false)
33  , LinearFunction(var, 0)
34 // , _model(model)
35 // , _q_ddot(model.getAccelerationVariable())
36  , hpos(.5)
37  , margin(.05)
38 {
41 }
42 
47 {
48 
49 }
50 
56 {
57  return hpos;
58 }
59 
65 {
66  hpos = newHpos;
67 }
68 
74 {
75  return margin;
76 }
77 
83 {
84  margin = newMargin;
85 }
86 
94 void ContactAvoidanceFunction::updateContactInformation(const Eigen::MatrixXd& _JObst, const Eigen::VectorXd& _dJdqObst, const Eigen::VectorXd& _distObst, const Eigen::VectorXd& _velObst)
95 {
96  JObst = _JObst;
97  dJdqObst = _dJdqObst;
98  distObst = _distObst;
99  velObst = _velObst;
100 
101  changeFunctionDimension(_distObst.size());
102 }
103 
109 void ContactAvoidanceFunction::computeFullJacobian(const Eigen::MatrixXd& _JObst, Eigen::MatrixXd& fullJacobian) const
110 {
111  fullJacobian = - _JObst;
112 }
113 
121 void ContactAvoidanceFunction::computeFullb(const Eigen::VectorXd& _dJdqObst, const Eigen::VectorXd& _distObst, const Eigen::VectorXd& _velObst, Eigen::VectorXd& fullb) const
122 {
123  fullb = - _dJdqObst + ( (_distObst - Eigen::VectorXd::Constant(_distObst.size(), margin)) - _velObst * hpos ) * 2/(hpos*hpos);
124 
125 
126  for (int i=0; i<_distObst.size(); i++)
127  {
128  double tmax = 2*(_distObst(i) - margin) / _velObst(i);
129  if ((0 <= tmax) && (tmax <= hpos))
130  {
131  double ddqlim = - ( _velObst(i)*_velObst(i) ) / ( 2.*(_distObst(i) - margin) );
132  if (fullb(i) >= ddqlim)
133  {
134  fullb(i) = ddqlim;
135  }
136  }
137  }
138 }
139 
147 {
148  //TODO change _jacobian
149 }
150 
158 {
159  //TODO change _b
160 }
161 
168 {
169 
170 }
171 
178 {
179 
180 }
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
193 
198  : NamedInstance("Full Contact Avoidance Function")
200  , CoupledInputOutputSize(false)
201  , ContactAvoidanceFunction(model, model.getAccelerationVariable())
202  //, pimpl(new Pimpl(model))
203 {
204 
205 }
206 
211 {
212 
213 }
214 
220 {
222 }
223 
229 {
231 }
232 
233 
234 
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
247 {
248  const Model& _model;
250 
251  Eigen::MatrixXd _fullJacobian;
252  Eigen::VectorXd _fullb;
253 
254  Pimpl(const Model& model, const FullDynamicEquationFunction& dynamicEquation)
255  : _model(model)
256  , _dynamicEquation(dynamicEquation)
257  {
258 
259  }
260 };
261 
268  : NamedInstance("Reduced Contact Avoidance Function")
270  , CoupledInputOutputSize(false)
271  , ContactAvoidanceFunction(model, dynamicEquation.getActionVariable())
272  , pimpl(new Pimpl(model, dynamicEquation))
273 {
274  pimpl->_fullJacobian.setZero(_jacobian.rows(), _jacobian.cols());
275  pimpl->_fullb.setZero(_b.size());
276 }
277 
282 {
283 
284 }
285 
286 
294 {
295  computeFullJacobian(JObst, pimpl->_fullJacobian);
296  computeFullb(dJdqObst, distObst, velObst, pimpl->_fullb);
297 
298  _jacobian = - pimpl->_fullJacobian * pimpl->_dynamicEquation.getInertiaMatrixInverseJchiT();
299  _b = pimpl->_fullb - pimpl->_fullJacobian * pimpl->_dynamicEquation.getInertiaMatrixInverseLinNonLinGrav();
300 }
301 
302 
308 void ReducedContactAvoidanceFunction::doUpdateInputSizeBegin()
309 {
310  //do nothing : this overload allows to resize
311 }
312 
317 void ReducedContactAvoidanceFunction::doUpdateInputSizeEnd()
318 {
319  pimpl->_fullJacobian.setZero(_jacobian.rows(), _jacobian.cols());
320  pimpl->_fullb.setZero(_b.size());
321 }
322 
323 
324 
325 
326 
327 
328 
330  : _model(model)
331  , _is_connected(false)
333 {
335  setMargin(margin);
336 }
337 
339 {
340  return _hpos;
341 }
342 
344 {
345  _hpos = newHpos;
346  if (_is_connected)
347  _contactAvoidanceFunction->setHorizonOfPrediction(newHpos);
348 }
349 
350 
352 {
353  return _margin;
354 }
355 
357 {
358  _margin = newMargin;
359  if (_is_connected)
360  _contactAvoidanceFunction->setMargin(newMargin);
361 }
362 
363 
364 void ContactAvoidanceConstraint::updateContactInformation(const Eigen::MatrixXd& _JObst, const Eigen::VectorXd& _dJdqOst, const Eigen::VectorXd& _distObst, const Eigen::VectorXd& _velObst)
365 {
366  if (_is_connected)
367  _contactAvoidanceFunction->updateContactInformation(_JObst, _dJdqOst, _distObst, _velObst);
368  else
369  throw std::runtime_error("[ContactAvoidanceConstraint::updateContactInformation] Constraint is not connected to the controller!");
370 }
371 
372 
373 void ContactAvoidanceConstraint::connectToController(const FullDynamicEquationFunction& dynamicEquation, bool useReducedProblem)
374 {
375  LinearFunction* f = NULL;
376  if (useReducedProblem)
377  f = createReducedContactAvoidanceFunction(_model, dynamicEquation);
378  else
379  f = createFullContactAvoidanceFunction(_model);
380 
381  _constraint.reset(new LinearConstraint(f));
382  _is_connected = true;
383 
384  setHorizonOfPrediction(_hpos);
385  setMargin(_margin);
386 }
387 
389 {
390  _is_connected = false;
391 }
392 
393 ContactAvoidanceFunction* ContactAvoidanceConstraint::createFullContactAvoidanceFunction(const Model& model)
394 {
395  _contactAvoidanceFunction = new FullContactAvoidanceFunction(model);
396  return _contactAvoidanceFunction;
397 }
398 
399 ContactAvoidanceFunction* ContactAvoidanceConstraint::createReducedContactAvoidanceFunction(const Model& model, const FullDynamicEquationFunction& dynamicEquation)
400 {
401  _contactAvoidanceFunction = new ReducedContactAvoidanceFunction(model, dynamicEquation);
402  return _contactAvoidanceFunction;
403 }
void updateContactInformation(const Eigen::MatrixXd &_JObst, const Eigen::VectorXd &_dJdqOst, const Eigen::VectorXd &_distObst, const Eigen::VectorXd &_velObst)
ContactAvoidanceConstraint(const Model &model, double hpos, double margin)
virtual void doUpdateDimensionBegin(int newDimension)
void updateContactInformation(const Eigen::MatrixXd &_JObst, const Eigen::VectorXd &_dJdqOst, const Eigen::VectorXd &_distObst, const Eigen::VectorXd &_velObst)
ContactAvoidanceFunction(const Model &m, Variable &var)
virtual void connectToController(const FullDynamicEquationFunction &dynamicEquation, bool useReducedProblem)
Create a linear function that represents the contact avoidance function for the full formalism...
Constraint< LinearFunction > LinearConstraint
Definition: Constraint.h:673
Model class.
Definition: Model.h:38
LinearFunction class.
ReducedContactAvoidanceFunction(const Model &model, const FullDynamicEquationFunction &dynamicEquation)
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
boost::shared_ptr< Constraint< LinearFunction > > _constraint
MatrixXd & _jacobian
Definition: Function.h:313
void computeFullJacobian(const Eigen::MatrixXd &_JObst, Eigen::MatrixXd &fullJacobian) const
Pimpl(const Model &model, const FullDynamicEquationFunction &dynamicEquation)
void invalidateb(int timestamp)
Create a linear function that represents the joint limit function.
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
Create a linear function that represents the dynamic equation of motion.
void changeFunctionDimension(int newDimension)
Definition: Function.cpp:56
void invalidateAll()
Definition: Function.h:330
virtual void doUpdateDimensionEnd(int oldDimension)
Define contact avoidance constraint for wOcra controller.
const FullDynamicEquationFunction & _dynamicEquation
void computeFullb(const Eigen::VectorXd &_dJdqObst, const Eigen::VectorXd &_distObst, const Eigen::VectorXd &_velObst, Eigen::VectorXd &fullb) const
Create a linear function that represents the contact avoidance function for the reduced formalism...
void connect(Derived &object, void(Base::*newCallback)(int)) const
Call this method to register a non-static method as a callback.