ocra-recipes
Doxygen documentation for the ocra-recipes repository
GHCJTTask.cpp
Go to the documentation of this file.
1 
9 
10 // OCRA INCLUDES
11 #include "ocra/control/Model.h"
17 
18 // GOCRA INCLUDES
19 #include "gocra/gOcraDebug.h"
20 
21 
22 namespace gocra
23 {
24 
25 
27 {
28  const std::string& name;
29  const Model& innerModel;
33  boost::shared_ptr<LinearFunction> taskSEConstraintFunction;
34  bool useGSHC;
36 
37 
38  double weight;
39  MatrixXd alpha;
40  MatrixXd projector;
41  MatrixXd taskiProjector;
42  const Feature& feature;
44  int indexEnd;
45 
47  boost::shared_ptr<LinearizedCoulombFunction> frictionFunction;
49 
53 
54 
58 
59 
63 
64  LinearFunction* regulationObjectiveFunction; //objective function for GHC
66 
67  Pimpl(const std::string& name, const Model& m, const Feature& s)
68  : name(name)
69  , innerModel(m)
70  , isFreeFloating(m.nbDofs() - m.nbInternalDofs()>0)
71  , solver(0x0)
72  , useGSHC(false)
73  , fcVar(name+".var", s.getDimension())
74  , taskSEConstraintFunction()
75  , frictionConstraint(NULL)
76  , frictionFunction()
77  , weight(1.)
78  , alpha(MatrixXd::Zero(s.getDimension(),s.getDimension()))
79  , projector(MatrixXd::Identity(m.nbDofs(),m.nbDofs()))
80  , taskiProjector(MatrixXd::Identity(m.nbDofs(),m.nbDofs()))
81  , feature(s)
82  , taskHasBeenInitialized(false)
83  , contactForceConstraintHasBeenSavedInSolver(false)
84  , contactPointHasBeenSavedInModel(false)
85  , frictionConstraintIsRegisteredInConstraint(false)
86  , isRegisteredAsObjective(false)
87  , isRegisteredAsConstraint(false)
88  , isPointContactTask(false)
89  , innerObjectiveFunction(NULL)
90  , innerTaskAsObjective(NULL)
91  , regulationObjectiveFunction(NULL)
92  , regulationObjective(NULL)
93  {
94  innerTaskAsConstraint.set(NULL);
95 
96  if(fcVar.getSize() == 3)// candidate for contact mode
97  {
98  frictionFunction.reset( new LinearizedCoulombFunction(fcVar, 1., 6, 0.) );
99  const int outDim = frictionFunction->getDimension();
100  const int inDim = 3;
101  frictionConstraint.set( reinterpret_cast<LinearizedCoulombFunction *>(new LinearFunction(fcVar, MatrixXd::Identity(outDim, inDim), VectorXd::Zero(outDim))) );
102  }
103  if (isFreeFloating)
104  {
105  taskSEConstraintFunction.reset(new LinearFunction( fcVar, MatrixXd::Zero(m.nbDofs() - m.nbInternalDofs(), fcVar.getSize()), VectorXd::Zero(m.nbDofs() - m.nbInternalDofs()) ));
106  }
107 
108  }
109 
110 
111 
113  {
114  if (innerTaskAsObjective)
115  {
116  delete &innerTaskAsObjective->getFunction();
117  delete innerTaskAsObjective;
118  }
119  if (regulationObjective)
120  {
121  std::cout<<"----"<<std::endl;
122 
123  delete &regulationObjective->getFunction();
124  delete regulationObjective;
125  }
126  }
127 
129  {
130  innerTaskAsObjective = new Objective<SquaredLinearFunction>(new SquaredLinearFunction(innerObjectiveFunction), weight); // Here, it will manage the SquaredLinearFunction build on a pointer of the function.
131  innerTaskAsConstraint.set(innerObjectiveFunction); // As as ConstraintPtr, it will manage the new created function innerObjectiveFunction
132  regulationObjective = new Objective<SquaredLinearFunction>(new SquaredLinearFunction(regulationObjectiveFunction), weight*0.01);
133  }
134 };
135 
136 
144 GHCJTTask::GHCJTTask(const std::string& taskName, const Model& innerModel, Feature::Ptr feature, Feature::Ptr featureDes)
145  : Task(taskName, innerModel, feature, featureDes)
146  , pimpl(new Pimpl(taskName, innerModel, feature))
147 {
148 
149 }
150 
157 GHCJTTask::GHCJTTask(const std::string& taskName, const Model& innerModel, Feature::Ptr feature)
158  : Task(taskName, innerModel, feature)
159  , pimpl(new Pimpl(taskName, innerModel, feature))
160 {
161 
162 }
163 
164 
166 {
167 
168 }
169 
170 
172 {
173  pimpl->solver = &solver;
174  pimpl->seConstraint = &seConstraint;
175 
176 
177  int featn = pimpl->feature->getDimension();
178  pimpl->innerObjectiveFunction = new LinearFunction(pimpl->fcVar, Eigen::MatrixXd::Identity(featn, featn), Eigen::VectorXd::Zero(featn));
179  pimpl->regulationObjectiveFunction = new LinearFunction(pimpl->fcVar, Eigen::MatrixXd::Identity(featn, featn),Eigen::VectorXd::Zero(featn));
180 
181  pimpl->connectFunctionnWithObjectiveAndConstraint();
182 }
183 
184 
186 {
187  if (pimpl->isRegisteredAsObjective)
188  {
189  pimpl->solver->removeObjective(*pimpl->innerTaskAsObjective);
190 
191  if (!isPointContactTask())
192  {
193  pimpl->solver->removeObjective(*pimpl->regulationObjective);
194  }
195  }
196  if (pimpl->isRegisteredAsConstraint)
197  {
198  pimpl->solver->removeConstraint(pimpl->innerTaskAsConstraint);
199  }
200 
201  if (pimpl->frictionConstraintIsRegisteredInConstraint)
202  {
203  pimpl->solver->removeConstraint(pimpl->frictionConstraint.getConstraint());
204  }
205 
206 
207 }
208 
209 
210 const Eigen::VectorXd& GHCJTTask::getComputedForce() const
211 {
212  return pimpl->fcVar.getValue();
213 }
214 
215 
216 void GHCJTTask::doGetOutput(Eigen::VectorXd& output) const
217 {
218  output = pimpl->fcVar.getValue();
219 }
220 
221 
222 
224 {
225  //THIS SHOULD BE DONE ONLY ONCE!!!
226  if ( ! pimpl->contactPointHasBeenSavedInModel )
227  {
228  pimpl->innerModel.getModelContacts().addContactPoint(pimpl->fcVar, getFeature());
229  pimpl->contactPointHasBeenSavedInModel = true;
230  }
231 
232 
233 }
234 
236 {
237  // if ( pimpl->contactPointHasBeenSavedInModel )
238 // {
239 // pimpl->model.getModelContacts().removeContactPoint(pimpl->fcVar);
240 // pimpl->contactPointHasBeenSavedInModel = false;
241 // }
242 // if ( ! pimpl->contactForceConstraintHasBeenSavedInSolver )
243 // {
244 // pimpl->solver->addConstraint(pimpl->ContactForceConstraint);
245 // pimpl->contactForceConstraintHasBeenSavedInSolver = true;
246 // }
247 }
248 
249 
250 
257 {
259 
261 
262  // add friction cone in constraint
263  pimpl->solver->addConstraint(pimpl->frictionConstraint.getConstraint());
264 
265  pimpl->frictionConstraintIsRegisteredInConstraint = true;
266 }
267 
268 
275 {
277 
279 
280  // remove friction cone from constraint set
281  pimpl->solver->removeConstraint(pimpl->frictionConstraint.getConstraint());
282  pimpl->frictionConstraintIsRegisteredInConstraint = false;
283 }
284 
285 
291 {
292  pimpl->frictionFunction->setFrictionCoeff(getFrictionCoeff());
293 }
294 
300 {
301  pimpl->frictionFunction->setMargin(getMargin());
302 }
303 
304 
310 {
312  pimpl->solver->addObjective(*pimpl->innerTaskAsObjective);
313  pimpl->solver->addObjective(*pimpl->regulationObjective);
314  if (pimpl->isFreeFloating)
315  pimpl->seConstraint->addFunction(*pimpl->taskSEConstraintFunction);
316 
317  pimpl->isRegisteredAsObjective = true;
318 
319 
320 
321 }
322 
328 {
330  pimpl->solver->removeObjective(*pimpl->innerTaskAsObjective);
331  pimpl->solver->removeObjective(*pimpl->regulationObjective);
332  if (pimpl->isFreeFloating)
333  pimpl->seConstraint->removeFunction(*pimpl->taskSEConstraintFunction);
334  pimpl->isRegisteredAsObjective = false;
335 
336 
337 
338 }
339 
346 {
348  pimpl->solver->addConstraint(pimpl->innerTaskAsConstraint);
349  pimpl->solver->addObjective(*pimpl->regulationObjective);
350  if (pimpl->isFreeFloating)
351  pimpl->seConstraint->addFunction(*pimpl->taskSEConstraintFunction);
352 
353  pimpl->isRegisteredAsConstraint = true;
354 
355 
356 }
357 
363 {
365  pimpl->solver->removeObjective(*pimpl->regulationObjective);
366  pimpl->solver->removeConstraint(pimpl->innerTaskAsConstraint);
367  if (pimpl->isFreeFloating)
368  pimpl->seConstraint->removeFunction(*pimpl->taskSEConstraintFunction);
369  pimpl->isRegisteredAsConstraint = false;
370 
371 
372 }
373 
374 
375 
384 {
385 
386  if (pimpl->innerTaskAsObjective)
387  {
388 
389  pimpl->innerTaskAsObjective->getFunction().changeWeight(getWeight());
390  }
391 
392 }
393 
394 
395 //--------------------------------------------------------------------------------------------------------------------//
397 {
398 
400  return;
401 
402  if (pimpl->isFreeFloating)
403  {
404  const MatrixXd Jt = getJacobian().transpose();
405  const MatrixXd PJt = getProjector()*Jt;
406  if (isPointContactTask())
407  pimpl->taskSEConstraintFunction->changeA( Jt.topRows(pimpl->taskSEConstraintFunction->getDimension()) );
408  else
409  pimpl->taskSEConstraintFunction->changeA( PJt.topRows(pimpl->taskSEConstraintFunction->getDimension()) );
410  }
411 
412  const MatrixXd& Kp = getStiffness();
413  const MatrixXd& Kd = getDamping();
414 // const VectorXd e = pimpl->feature->computeError(*getFeatureDes());
415 // const VectorXd edot = pimpl->feature->computeErrorDot(*getFeatureDes());
416 // const VectorXd& e = getFeatureDes() ? getFeature().computeError(*getFeatureDes()) : getFeature().computeError();
417 // const VectorXd& edot = getFeatureDes() ? getFeature().computeErrorDot(*getFeatureDes()) : getFeature().computeErrorDot();
418 // const VectorXd& fd = getFeatureDes() ? getFeature().computeEffort(*getFeatureDes()) : getFeature().computeEffort();
419 // const VectorXd f = -Kp * e - Kd * edot - fd;
420 
421  const VectorXd f = -Kp * getError() - Kd * getErrorDot() - getEffort();
422 
423 // std::cout<<"f="<<f.transpose()<<std::endl;
424 // const VectorXd f = getEffort();
425 
426 // pimpl->innerObjectiveFunction->changeReference(f);
427  pimpl->innerObjectiveFunction->changeb(f);
428 
429  if(isPointContactTask())
430  {
431  setWeight(0.01);
432  const MatrixXd& A = pimpl->frictionFunction->getA();
433  const VectorXd b = pimpl->frictionFunction->getb() + A * getFrictionConstraintOffset();
434  pimpl->frictionConstraint.getFunction().changeA(A);
435  pimpl->frictionConstraint.getFunction().changeb(b);
436  }
437 }
438 
439 
440 
442 {
443  if (!pimpl->solver)
444  {
445  std::string errmsg = std::string("[GHCJTTask::doActivateAsObjective]: task '") + getName() + std::string("' not connected to any solver; Call prior that 'GHCJTController::addTask' to connect to the solver inside the controller.\n"); //
446  throw std::runtime_error(std::string(errmsg));
447  }
448 }
449 
450 
451 const MatrixXd& GHCJTTask::getPriority() const
452 {
453  return pimpl->alpha;
454 
455 }
456 
457 void GHCJTTask::setPriority(Eigen::MatrixXd& alpha)
458 {
459  pimpl->alpha = alpha;
460 
461 }
462 
464 {
465  return pimpl->feature->getDimension();
466 }
467 
468 const std::string& GHCJTTask::getTaskName() const
469 {
470  return pimpl->name;
471 }
472 
474 {
475  pimpl->indexBegin = index;
476 }
477 
479 {
480  return pimpl->indexBegin;
481 }
482 
483 void GHCJTTask::setIndexEnd(int index)
484 {
485  pimpl->indexEnd = index;
486 }
487 
489 {
490  return pimpl->indexEnd;
491 }
492 
493 void GHCJTTask::setProjector(MatrixXd& proj)
494 {
495  pimpl->projector = proj;
496 }
497 
498 const MatrixXd& GHCJTTask::getProjector() const
499 {
500  return pimpl->projector;
501 }
502 
503 void GHCJTTask::setTaskiProjector(MatrixXd& proj)
504 {
505  pimpl->taskiProjector = proj;
506 }
507 
508 const MatrixXd& GHCJTTask::getTaskiProjector() const
509 {
510  return pimpl->taskiProjector;
511 }
512 
514 {
515  return pimpl->innerObjectiveFunction;
516 }
517 
519 {
520  return pimpl->fcVar;
521 }
522 
523 } // end namespace gocra
virtual void doSetMargin()
Definition: GHCJTTask.cpp:299
void setIndexBegin(int index)
Definition: GHCJTTask.cpp:473
Define task class for GHCJT controller. It inherits from the task class defined in the ocra framework...
const Eigen::MatrixXd & getJacobian() const
Definition: Task.cpp:600
double getMargin() const
Definition: Task.cpp:527
int nbInternalDofs() const
Definition: Model.cpp:59
void setProjector(Eigen::MatrixXd &proj)
Definition: GHCJTTask.cpp:493
void addContactPointInModel()
Definition: GHCJTTask.cpp:223
bool contactPointHasBeenSavedInModel
Definition: GHCJTTask.cpp:51
GHCJTTask(const std::string &taskName, const Model &innerModel, Feature::Ptr feature, Feature::Ptr featureDes)
Definition: GHCJTTask.cpp:144
LinearFunction * getInnerObjectiveFunction() const
Definition: GHCJTTask.cpp:513
void connectToController(ocra::OneLevelSolver &_solver, SumOfLinearFunctions &seConstraint)
Definition: GHCJTTask.cpp:171
const Eigen::VectorXd & getErrorDot() const
Definition: Task.cpp:582
const std::string & name
Definition: GHCJTTask.cpp:28
void setTaskiProjector(Eigen::MatrixXd &proj)
Definition: GHCJTTask.cpp:503
virtual void doDeactivateAsConstraint()
Definition: GHCJTTask.cpp:362
virtual void doSetWeight()
Definition: GHCJTTask.cpp:383
const Eigen::MatrixXd & getPriority() const
Definition: GHCJTTask.cpp:451
LinearFunction * regulationObjectiveFunction
Definition: GHCJTTask.cpp:64
boost::shared_ptr< LinearFunction > taskSEConstraintFunction
Definition: GHCJTTask.cpp:33
Declaration file of the Model class.
virtual void doActivateAsConstraint()
Definition: GHCJTTask.cpp:345
const Eigen::MatrixXd & getDamping() const
Definition: Task.cpp:466
boost::shared_ptr< LinearizedCoulombFunction > frictionFunction
Definition: GHCJTTask.cpp:47
const std::string & getName() const
const Variable & getVariable() const
Definition: GHCJTTask.cpp:518
const Eigen::VectorXd & getError() const
Definition: Task.cpp:576
int getDimension() const
Definition: Task.cpp:565
LinearizedCoulombFunction class.
const Eigen::Vector3d & getFrictionConstraintOffset() const
Definition: Task.cpp:532
Model class.
Definition: Model.h:38
int getTaskDimension() const
Definition: GHCJTTask.cpp:463
LinearFunction class.
Feature interface, used by tasks to compute errors and jacobians.
Definition: Feature.h:55
int getIndexEnd() const
Definition: GHCJTTask.cpp:488
Objective< SquaredLinearFunction > * innerTaskAsObjective
Definition: GHCJTTask.cpp:61
bool isBodyContactConstraint() const
Definition: Task.cpp:512
virtual void doDeactivateContactMode()
Definition: GHCJTTask.cpp:274
const Eigen::VectorXd & getComputedForce() const
Definition: GHCJTTask.cpp:210
void disconnectFromController()
Definition: GHCJTTask.cpp:185
Feature::Ptr getFeature() const
Definition: Task.cpp:606
Declaration file of the LinearFunction class.
bool contactForceConstraintHasBeenSavedInSolver
Definition: GHCJTTask.cpp:50
virtual void doDeactivateAsObjective()
Definition: GHCJTTask.cpp:327
virtual T & getFunction(void)
Definition: Objective.h:64
int getSize() const
Definition: Variable.cpp:81
int nbDofs() const
Definition: Model.cpp:54
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
const Eigen::MatrixXd & getProjector() const
Definition: GHCJTTask.cpp:498
EqualZeroConstraintPtr< LinearFunction > innerTaskAsConstraint
Definition: GHCJTTask.cpp:62
Declaration file of the WeightedSquareDistanceFunction class.
const Eigen::MatrixXd & getTaskiProjector() const
Definition: GHCJTTask.cpp:508
virtual void doSetFrictionCoeff()
Definition: GHCJTTask.cpp:290
void setIndexEnd(int index)
Definition: GHCJTTask.cpp:483
int getIndexBegin() const
Definition: GHCJTTask.cpp:478
SquaredLinearFunction class.
Declaration file of the LinearizedCoulombFunction class.
ocra::BaseVariable fcVar
Definition: GHCJTTask.cpp:35
Objective< SquaredLinearFunction > * regulationObjective
Definition: GHCJTTask.cpp:65
A generic abstract class the solvers that can be used in the wOcra Controller.
virtual void doGetOutput(Eigen::VectorXd &output) const
Definition: GHCJTTask.cpp:216
double getFrictionCoeff() const
Definition: Task.cpp:522
void setWeight(double weight)
Definition: Task.cpp:501
void checkIfConnectedToController() const
Definition: GHCJTTask.cpp:441
bool isPointContactTask() const
Definition: Task.cpp:517
const Eigen::VectorXd & getEffort() const
Definition: Task.cpp:594
Debug trace.
ocra::OneLevelSolver * solver
Definition: GHCJTTask.cpp:31
virtual ~GHCJTTask()
Definition: GHCJTTask.cpp:165
const Eigen::VectorXd & getWeight() const
Definition: Task.cpp:560
SumOfLinearFunctions * seConstraint
Definition: GHCJTTask.cpp:32
void setPriority(Eigen::MatrixXd &alpha)
Definition: GHCJTTask.cpp:457
virtual void doActivateAsObjective()
Definition: GHCJTTask.cpp:309
const Model & innerModel
Definition: GHCJTTask.cpp:29
bool frictionConstraintIsRegisteredInConstraint
Definition: GHCJTTask.cpp:52
Pimpl(const std::string &name, const Model &m, const Feature &s)
Definition: GHCJTTask.cpp:67
virtual void doActivateContactMode()
Definition: GHCJTTask.cpp:256
Implements a basic variable.
Definition: Variable.h:304
void connectFunctionnWithObjectiveAndConstraint()
Definition: GHCJTTask.cpp:128
LinearFunction * innerObjectiveFunction
Definition: GHCJTTask.cpp:60
LessThanZeroConstraintPtr< LinearizedCoulombFunction > frictionConstraint
Definition: GHCJTTask.cpp:48
const Eigen::MatrixXd & getStiffness() const
Definition: Task.cpp:471
void removeContactPointInModel()
Definition: GHCJTTask.cpp:235
const Feature & feature
Definition: GHCJTTask.cpp:42
const std::string & getTaskName() const
Definition: GHCJTTask.cpp:468