ocra-recipes
Doxygen documentation for the ocra-recipes repository
Task.cpp
Go to the documentation of this file.
1 // #pragma warning(disable: 4244) // XXX Eigen 3 JacobiSVD
2 #include "ocra/control/Task.h"
3 
4 
5 // #pragma warning(default: 4244)
6 
7 using namespace Eigen;
8 
9 namespace
10 {
11  enum
12  {
13  TASK_DEACTIVATED,
16  };
17 
18  struct GainsWorkspace
19  {
20  JacobiSVD<MatrixXd> svd;
21  VectorXd s;
22  VectorXd kps;
23  VectorXd kds;
24 
25  GainsWorkspace(int n) : svd(n, n), s(n), kps(n), kds(n) {}
26  };
27 }
28 
29 namespace ocra
30 {
31  struct Task::Pimpl
32  {
33  Feature::Ptr feature;
34  Feature::Ptr featureDes;
35  int mode;
36  GainsWorkspace gainsWorkspace;
37  MatrixXd M;
38  MatrixXd M_inverse;
39  MatrixXd B;
40  MatrixXd K;
41  VectorXd weight;
42  VectorXd output;
43  VectorXd error;
44  VectorXd errorDot;
45  VectorXd errorDdot;
46  VectorXd effort;
47  MatrixXd jacobian;
48  Vector3d frictionOffset;
49  double frictionCoeff;
50  double margin;
56 
57  std::shared_ptr<Model> innerModel;
58  std::shared_ptr<OneLevelSolver> solver;
68 
71 
72 
76 
77  Pimpl(const std::string& name, std::shared_ptr<Model> m, Feature::Ptr s, Feature::Ptr sdes)
78  : feature(s)
79  , featureDes(sdes)
80  , mode(TASK_DEACTIVATED)
81  , gainsWorkspace(s->getDimension())
82  , M( MatrixXd::Identity(s->getDimension(), s->getDimension()) )
83  , M_inverse( MatrixXd::Identity(s->getDimension(), s->getDimension()) )
84  , B( MatrixXd::Zero(s->getDimension(), s->getDimension()) )
85  , K( MatrixXd::Zero(s->getDimension(), s->getDimension()) )
86  , weight( VectorXd::Ones(s->getDimension()) )
87  , frictionOffset(Vector3d::Zero())
88  , frictionCoeff(1.)
89  , margin(0.)
90  , useActualMass(true)
91  , contactActive(false)
92  , innerTaskType(UNKNOWNTASK)
93  , innerMetaTaskType(UNKNOWN)
94  , hierarchyLevel(-1)
95  , innerModel(m)
96  , solver(0x0)
97  , dynamicEquation(0x0)
98  , useReducedProblem(false)
99  , fcVar(name+".var", s->getDimension())
100 // , weight(1.)
101  , taskHasBeenInitialized(false)
102  , contactForceConstraintHasBeenSavedInSolver(false)
103  , contactPointHasBeenSavedInModel(false)
104  , frictionConstraintIsRegisteredInConstraint(false)
105  , isRegisteredAsObjective(false)
106  , isRegisteredAsConstraint(false)
107  , innerObjectiveFunction(NULL)
108  , innerTaskAsObjective(NULL)
109  {
110  innerTaskAsConstraint.set(NULL);
111 
112  if(fcVar.getSize() == 3)
113  {
114 // std::cout<<"CAN BE A CONTACT POINT!!! register friction and contact constraints\n";
115 // registerFrictionConstraint = true;
116  frictionConstraint.set( new LinearizedCoulombFunction(fcVar, 1., 6, 0.) );
117  ContactForceConstraint.set( new LinearFunction( fcVar, Eigen::MatrixXd::Identity(3,3), VectorXd::Zero(3) ) );
118  }
119  else
120  {
121 // registerFrictionConstraint = false;
122  frictionConstraint.set(NULL);
123  ContactForceConstraint.set(NULL);
124  }
125  }
127  {
128  if (innerTaskAsObjective)
129  {
130  delete &innerTaskAsObjective->getFunction();
131  delete innerTaskAsObjective;
132  }
133  }
135  {
136 
137  int featn = feature->getDimension();
138  if (useReducedProblem)
139  {
140  innerObjectiveFunction = new VariableChiFunction(dynamicEquation->getActionVariable(), featn);
141  }
142  else
143  {
144  innerObjectiveFunction = new LinearFunction (innerModel->getAccelerationVariable(), Eigen::MatrixXd::Zero(featn, innerModel->nbDofs()), Eigen::VectorXd::Zero(featn));
145  }
146  connectFunctionnWithObjectiveAndConstraint();
147  }
148 
150  {
151  int featn = feature->getDimension();
152  innerObjectiveFunction = new LinearFunction(innerModel->getJointTorqueVariable(), Eigen::MatrixXd::Zero(featn, innerModel->nbInternalDofs()), Eigen::VectorXd::Zero(featn));
153  connectFunctionnWithObjectiveAndConstraint();
154  }
155 
157  {
158  int featn = feature->getDimension();
159  innerObjectiveFunction = new LinearFunction(fcVar, Eigen::MatrixXd::Identity(featn, featn), Eigen::VectorXd::Zero(featn));
160  connectFunctionnWithObjectiveAndConstraint();
161  }
162 
164  {
165  innerTaskAsObjective = new Objective<SquaredLinearFunction>(new SquaredLinearFunction(innerObjectiveFunction));//, weight); // Here, it will manage the SquaredLinearFunction build on a pointer of the function.
166  innerTaskAsConstraint.set(innerObjectiveFunction); // As as ConstraintPtr, it will manage the new created function innerObjectiveFunction
167  }
168 
169  };
170 
171  Task::Task(const std::string& name, std::shared_ptr<Model> model, Feature::Ptr feature, Feature::Ptr featureDes)
172  : NamedInstance(name)
173  , pimpl(new Pimpl(name, model, feature, featureDes))
174  {
175  }
176 
177  Task::Task(const std::string& name, std::shared_ptr<Model> model, Feature::Ptr feature)
178  : NamedInstance(name)
179  , pimpl(new Pimpl(name, model, feature, 0x0))
180  {
181  }
182 
184  {
185  }
186 
188  {
189  if (getTaskType()==UNKNOWNTASK) {
190  pimpl->innerTaskType = newTaskType;
191  }else{
192  std::cout << "[warning] can't change a task's type once it has been set." << std::endl;
193  }
194  }
196  {
197  return pimpl->hierarchyLevel;
198  }
199  void Task::setHierarchyLevel(int level)
200  {
201  std::cout << "\033[1;31m["<<getName()<<"]\033[0m Setting hierarchy level to "<<level<< std::endl;
202  if( level < 0 )
203  {
204  std::cout << "[warning] Level should be >0 , but you provided "<<level << std::endl;
205  return;
206  }
207  if(getHierarchyLevel() == -1) // TODO: find out why this crashes
208  {
209  pimpl->hierarchyLevel = level;
210  }else{
211  std::cout << "[warning] Can't change a task's level once it has been set." << std::endl;
212  }
213  return;
214  }
215 
217  {
218  return pimpl->innerTaskType;
219  }
220 
222  {
223  if (getMetaTaskType()==UNKNOWN) {
224  pimpl->innerMetaTaskType = newMetaTaskType;
225  }else{
226  std::cout << "[warning] can't change a task's type once it has been set." << std::endl;
227  }
228  }
229 
231  {
232  return pimpl->innerMetaTaskType;
233  }
234 
236  {
237  Task::META_TASK_TYPE metaType = this->getMetaTaskType();
238  std::string metaTypeString;
239 
240  switch (metaType) {
241  case Task::UNKNOWN: {metaTypeString = "UNKNOWN";}break;
242  case Task::POSITION: {metaTypeString = "POSITION";}break;
243  case Task::ORIENTATION: {metaTypeString = "ORIENTATION";}break;
244  case Task::POSE: {metaTypeString = "POSE";}break;
245  case Task::FORCE: {metaTypeString = "FORCE";}break;
246  case Task::COM: {metaTypeString = "COM";}break;
247  case Task::COM_MOMENTUM: {metaTypeString = "COM_MOMENTUM";}break;
248  case Task::PARTIAL_POSTURE: {metaTypeString = "PARTIAL_POSTURE";}break;
249  case Task::FULL_POSTURE: {metaTypeString = "FULL_POSTURE";}break;
250  case Task::PARTIAL_TORQUE: {metaTypeString = "PARTIAL_TORQUE";}break;
251  case Task::FULL_TORQUE: {metaTypeString = "FULL_TORQUE";}break;
252  case Task::POINT_CONTACT: {metaTypeString = "POINT_CONTACT";}break;
253  default: {metaTypeString = "UNKNOWN";}break;
254  }
255 
256  return util::convertToLowerCase(metaTypeString);
257  }
258 
260  {
261  switch(getTaskType())
262  {
263 
264  case(ACCELERATIONTASK):
265  {
267  break;
268  }
269  case(TORQUETASK):
270  {
272  break;
273  }
274  case(FORCETASK):
275  {
276  updateForceTask();
277  break;
278  }
279  case(COMMOMENTUMTASK):
280  {
282  break;
283  }
284  case(UNKNOWNTASK):
285  {
286  throw std::runtime_error(std::string("[Task::update]: The task type has not been set during creation."));
287  break;
288  }
289  default:
290  {
291  throw std::runtime_error(std::string("[Task::update]: Unhandle case of TYPETASK."));
292  break;
293  }
294  }
295  }
296 
297 
299  {
300  if(pimpl->mode == TASK_AS_OBJECTIVE)
301  return;
302  else if(pimpl->mode == TASK_AS_CONSTRAINT)
303  deactivate();
304 
306  if(pimpl->contactActive)
308 
309  pimpl->mode = TASK_AS_OBJECTIVE;
310  }
311 
313  {
314  if(pimpl->mode == TASK_AS_CONSTRAINT)
315  return;
316  else if(pimpl->mode == TASK_AS_OBJECTIVE)
317  deactivate();
318 
319 // doActivateAsConstraint();
320  if(pimpl->contactActive)
322 
323  pimpl->mode = TASK_AS_CONSTRAINT;
324  }
325 
327  {
328  if(pimpl->mode == TASK_DEACTIVATED)
329  return;
330 
331  if(pimpl->mode == TASK_AS_OBJECTIVE)
333  else if(pimpl->mode == TASK_AS_CONSTRAINT) {
334  OCRA_WARNING("Deactivating foot contacts");
335 // doDeactivateAsConstraint();
336  if(pimpl->contactActive)
338  }
339 
340  pimpl->mode = TASK_DEACTIVATED;
341  }
342 
344  {
345  return (pimpl->mode == TASK_AS_OBJECTIVE);
346  }
347 
349  {
350  return (pimpl->mode == TASK_AS_CONSTRAINT);
351  }
352 
354  {
355  pimpl->useActualMass = true;
356  }
357 
358  void Task::setDesiredMass(double Md)
359  {
360  pimpl->useActualMass = false;
361  pimpl->M = MatrixXd::Zero(pimpl->feature->getDimension(), pimpl->feature->getDimension());
362  pimpl->M.diagonal().setConstant(Md);
363  }
364 
365  void Task::setDesiredMass(const VectorXd& Md)
366  {
367  pimpl->useActualMass = false;
368  pimpl->M = MatrixXd::Zero(pimpl->feature->getDimension(), pimpl->feature->getDimension());
369  pimpl->M.diagonal() = Md;
370  }
371 
372  void Task::setDesiredMass(const MatrixXd& Md)
373  {
374  pimpl->useActualMass = false;
375  pimpl->M = Md;
376  }
377 
378  void Task::setDamping(double B)
379  {
380  pimpl->B = MatrixXd::Zero(pimpl->feature->getDimension(), pimpl->feature->getDimension());
381  pimpl->B.diagonal().setConstant(B);
382  }
383 
384  void Task::setDamping(const VectorXd& B)
385  {
386  pimpl->B = MatrixXd::Zero(pimpl->feature->getDimension(), pimpl->feature->getDimension());
387  pimpl->B.diagonal() = B;
388  }
389 
390  void Task::setDamping(const MatrixXd& B)
391  {
392  pimpl->B = B;
393  }
394 
395  void Task::setStiffness(double K)
396  {
397  pimpl->K = MatrixXd::Zero(pimpl->feature->getDimension(), pimpl->feature->getDimension());
398  pimpl->K.diagonal().setConstant(K);
399  }
400 
401  void Task::setStiffness(const VectorXd& K)
402  {
403  pimpl->K = MatrixXd::Zero(pimpl->feature->getDimension(), pimpl->feature->getDimension());
404  pimpl->K.diagonal() = K;
405  }
406 
407  void Task::setStiffness(const MatrixXd& K)
408  {
409  pimpl->K = K;
410  }
411 
412  void Task::setAutoGains(double freq)
413  {
414  setAutoGains(freq, 100.);
415  }
416 
417  void Task::setAutoGains(double freq, double massSaturation)
418  {
419  JacobiSVD<MatrixXd>& svd = pimpl->gainsWorkspace.svd;
420  svd.compute(getDesiredMass(), ComputeFullU | ComputeFullV);
421 
422  const VectorXd& s = svd.singularValues();
423  pimpl->gainsWorkspace.s = (s.array() < massSaturation).select(s, massSaturation);
424 
425  const double w = 2. * M_PI * freq;
426  VectorXd& kps = pimpl->gainsWorkspace.kps;
427  VectorXd& kds = pimpl->gainsWorkspace.kds;
428 
429  kps = s * w*w; // kp[i] = s[i] * w^2
430  kds = (kps.array()*s.array()).sqrt() * 2.; // kd[i] = 2 * sqrt(kp[i]*s[i])
431 
432  pimpl->K = svd.matrixU() * kps.asDiagonal() * svd.matrixV().transpose();
433  pimpl->B = svd.matrixU() * kds.asDiagonal() * svd.matrixV().transpose();
434  }
435 
437  {
438  return pimpl->useActualMass;
439  }
440 
441  const MatrixXd& Task::getDesiredMass() const
442  {
443  if(pimpl->useActualMass)
444  {
445  if(pimpl->featureDes)
446  return pimpl->feature->computeProjectedMass(*pimpl->featureDes);
447  else
448  return pimpl->feature->computeProjectedMass();
449  }
450  return pimpl->M;
451  }
452 
453  const MatrixXd& Task::getDesiredMassInverse() const
454  {
455  if(pimpl->useActualMass)
456  {
457  if(pimpl->featureDes)
458  return pimpl->feature->computeProjectedMassInverse(*pimpl->featureDes);
459  else
460  return pimpl->feature->computeProjectedMassInverse();
461  }
462  pimpl->M_inverse = pimpl->M.inverse();
463  return pimpl->M_inverse;
464  }
465 
466  const MatrixXd& Task::getDamping() const
467  {
468  return pimpl->B;
469  }
470 
471  const MatrixXd& Task::getStiffness() const
472  {
473  return pimpl->K;
474  }
475 
477  {
478  if(pimpl->contactActive)
479  return;
480 
481  if((pimpl->feature->getDimension() != 3) && (pimpl->feature->getDimension() != 6))
482  throw std::runtime_error("[Task::activateContactMode] Contact mode is available only for features with dimension 3 or 6");
483 
486 
487  pimpl->contactActive = true;
488  }
489 
491  {
492  if(!pimpl->contactActive)
493  return;
494 
497 
498  pimpl->contactActive = false;
499  }
500 
501  void Task::setWeight(double weight)
502  {
503  pimpl->weight.setConstant(weight);
504  doSetWeight();
505  }
506 
508  {
509  return pimpl->contactActive;
510  }
511 
513  {
514  return isContactModeActive() && getDimension() != 3;
515  }
516 
518  {
519  return isContactModeActive() && getDimension() == 3;
520  }
521 
522  double Task::getFrictionCoeff() const
523  {
524  return pimpl->frictionCoeff;
525  }
526 
527  double Task::getMargin() const
528  {
529  return pimpl->margin;
530  }
531 
532  const Vector3d& Task::getFrictionConstraintOffset() const
533  {
534  return pimpl->frictionOffset;
535  }
536 
537  void Task::setFrictionCoeff(double coeff)
538  {
539  pimpl->frictionCoeff = coeff;
541  }
542 
543  void Task::setMargin(double margin)
544  {
545  pimpl->margin = margin;
546  doSetMargin();
547  }
548 
549  void Task::setWeight(const VectorXd& weight)
550  {
551  pimpl->weight = weight;
552  doSetWeight();
553  }
554 
555  void Task::setFrictionConstraintOffset(const Vector3d& offset)
556  {
557  pimpl->frictionOffset = offset;
558  }
559 
560  const VectorXd& Task::getWeight() const
561  {
562  return pimpl->weight;
563  }
564 
565  int Task::getDimension() const
566  {
567  return getFeature()->getDimension();
568  }
569 
570  const VectorXd& Task::getOutput() const
571  {
572  doGetOutput(pimpl->output);
573  return pimpl->output;
574  }
575 
576  const VectorXd& Task::getError() const
577  {
578  pimpl->error = pimpl->featureDes ? pimpl->feature->computeError(*pimpl->featureDes) : pimpl->feature->computeError();
579  return pimpl->error;
580  }
581 
582  const VectorXd& Task::getErrorDot() const
583  {
584  pimpl->errorDot = pimpl->featureDes ? pimpl->feature->computeErrorDot(*pimpl->featureDes) : pimpl->feature->computeErrorDot();
585  return pimpl->errorDot;
586  }
587 
588  const VectorXd& Task::getErrorDdot() const
589  {
590  pimpl->errorDdot = pimpl->featureDes ? pimpl->feature->computeAcceleration(*pimpl->featureDes) : pimpl->feature->computeAcceleration();
591  return pimpl->errorDdot;
592  }
593 
594  const VectorXd& Task::getEffort() const
595  {
596  pimpl->effort = pimpl->featureDes ? pimpl->feature->computeEffort(*pimpl->featureDes) : pimpl->feature->computeEffort();
597  return pimpl->effort;
598  }
599 
600  const MatrixXd& Task::getJacobian() const
601  {
602  pimpl->jacobian = pimpl->featureDes ? pimpl->feature->computeJacobian(*pimpl->featureDes) : pimpl->feature->computeJacobian();
603  return pimpl->jacobian;
604  }
605 
606  Feature::Ptr Task::getFeature() const
607  {
608  return pimpl->feature;
609  }
610 
611  Feature::Ptr Task::getFeatureDes() const
612  {
613  return pimpl->featureDes;
614  }
615 
624 
625 
626 
633 void Task::doGetOutput(Eigen::VectorXd& output) const
634 {
635  output = Eigen::VectorXd::Zero(getDimension());
636 }
637 
638 
639 const Eigen::VectorXd& Task::getComputedForce() const
640 {
641  return pimpl->fcVar.getValue();
642 }
643 
645 {
646  if (pimpl->isRegisteredAsObjective)
647  {
648  if(pimpl->innerTaskAsObjective != NULL){
649  pimpl->solver->removeObjective(*pimpl->innerTaskAsObjective);
650  }
651  }
652  if (pimpl->isRegisteredAsConstraint)
653  {
654  pimpl->solver->removeConstraint(pimpl->innerTaskAsConstraint);
655  }
656 
657  if (pimpl->frictionConstraintIsRegisteredInConstraint)
658  {
659  pimpl->solver->removeConstraint(pimpl->frictionConstraint);
660  }
661  if (pimpl->contactForceConstraintHasBeenSavedInSolver)
662  {
663  pimpl->solver->removeConstraint(pimpl->ContactForceConstraint);
664  }
665 
666 }
667 
668 void Task::connectToController(std::shared_ptr<OneLevelSolver> solver, const FullDynamicEquationFunction& dynamicEquation, bool useReducedProblem)
669 {
670  pimpl->solver = solver;
671  pimpl->dynamicEquation = &dynamicEquation;
672  pimpl->useReducedProblem = useReducedProblem;
673 
674  switch(getTaskType())
675  {
676 
677  case(ACCELERATIONTASK):
678  {
679  pimpl->setAsAccelerationTask();
680 
681  break;
682  }
683  case(TORQUETASK):
684  {
685  pimpl->setAsTorqueTask();
686  break;
687  }
688  case(FORCETASK):
689  {
690  pimpl->setAsForceTask();
691  break;
692  }
693  case(COMMOMENTUMTASK):
694  {
695  pimpl->setAsAccelerationTask();
696  break;
697  }
698  case(UNKNOWNTASK):
699  {
700  std::string errmsg = std::string("[Task::connectToController]: The task type of '") + getName() + std::string("' has not been set during creation.\nCall prior that 'initAsAccelerationTask', 'initAsTorqueTask' or 'initAsForceTask'\n"); //
701  throw std::runtime_error(std::string(errmsg));
702  break;
703  }
704  default:
705  {
706  throw std::runtime_error(std::string("[Task::connectToController]: Unhandle case of TYPETASK for task ")+getName() );
707  break;
708  }
709  }
710 }
711 
712 
714 {
715  //THIS SHOULD BE DONE ONLY ONCE!!!
716  if ( ! pimpl->contactPointHasBeenSavedInModel )
717  {
718  pimpl->innerModel->getModelContacts().addContactPoint(pimpl->fcVar, *getFeature());
719  pimpl->contactPointHasBeenSavedInModel = true;
720  }
721 
722  if ( pimpl->contactForceConstraintHasBeenSavedInSolver )
723  {
724  pimpl->solver->removeConstraint(pimpl->ContactForceConstraint);
725  pimpl->contactForceConstraintHasBeenSavedInSolver = false;
726  }
727 }
728 
730 {
731  // if ( pimpl->contactPointHasBeenSavedInModel )
732 // {
733 // pimpl->model.getModelContacts().removeContactPoint(pimpl->fcVar);
734 // pimpl->contactPointHasBeenSavedInModel = false;
735 // }
736  if ( ! pimpl->contactForceConstraintHasBeenSavedInSolver )
737  {
738  pimpl->solver->addConstraint(pimpl->ContactForceConstraint);
739  pimpl->contactForceConstraintHasBeenSavedInSolver = true;
740  }
741 }
742 
743 
744 
767 {
768  const MatrixXd& J = getJacobian();
769  const MatrixXd& Kp = getStiffness();
770  const MatrixXd& Kd = getDamping();
771 
772  const VectorXd accDes = - ( getErrorDdot() + Kp * getError() + Kd * getErrorDot() );
773 
774  // std::cout << "\n----\nname = " << getName() << std::endl;
775  // std::cout << "getError() = " << getError().transpose() << std::endl;
776  // std::cout << "getErrorDot() = " << getErrorDot().transpose() << std::endl;
777  // std::cout << "getErrorDdot() = " << getErrorDdot().transpose() << std::endl;
778 
779  if (pimpl->useReducedProblem)
780  {
781  const Eigen::MatrixXd E2 = - J * pimpl->dynamicEquation->getInertiaMatrixInverseJchiT();
782  const Eigen::VectorXd f2 = accDes + J * pimpl->dynamicEquation->getInertiaMatrixInverseLinNonLinGrav();
783 
784  pimpl->innerObjectiveFunction->changeA(E2);
785  pimpl->innerObjectiveFunction->changeb(f2);
786  }
787  else
788  {
789  pimpl->innerObjectiveFunction->changeA(J);
790  pimpl->innerObjectiveFunction->changeb(accDes);
791  }
792 }
793 
794 
795 
796 
798 {
799  const MatrixXd& J = getJacobian();
800  const VectorXd eff = - getEffort();
801 
802  pimpl->innerObjectiveFunction->changeA(J);
803  pimpl->innerObjectiveFunction->changeb(eff);
804 }
805 
806 
808 {
809  //innerObjectiveFunction->changeA(); //already set in initForceTask
810 
811  const VectorXd eff = - getEffort();
812 
813  pimpl->innerObjectiveFunction->changeb(eff);
814 
815 }
816 
818 {
819  const MatrixXd& J = pimpl->innerModel->getCoMAngularJacobian();
820  const MatrixXd& Kd = getDamping();
821 
822  const VectorXd accDes = - Kd * pimpl->innerModel->getCoMAngularVelocity();
823 
824 
825  if (pimpl->useReducedProblem)
826  {
827  const Eigen::MatrixXd E2 = - J * pimpl->dynamicEquation->getInertiaMatrixInverseJchiT();
828  const Eigen::VectorXd f2 = accDes + J * pimpl->dynamicEquation->getInertiaMatrixInverseLinNonLinGrav();
829 
830  pimpl->innerObjectiveFunction->changeA(E2);
831  pimpl->innerObjectiveFunction->changeb(f2);
832  }
833  else
834  {
835  pimpl->innerObjectiveFunction->changeA(J);
836  pimpl->innerObjectiveFunction->changeb(accDes);
837  }
838 }
839 
840 
842 {
843  if (!pimpl->solver)
844  {
845  std::string errmsg = std::string("[Task::doActivateAsObjective]: task '") + getName() + std::string("' not connected to any solver; Call prior that 'Controller::addTask' to connect to the solver inside the controller.\n"); //
846  throw std::runtime_error(std::string(errmsg));
847  }
848 }
849 
850 
851 
858 {
860 
862 
863  // add friction cone in constraint
864  pimpl->solver->addConstraint(pimpl->frictionConstraint);
865  pimpl->frictionConstraintIsRegisteredInConstraint = true;
866 }
867 
868 
875 {
877 
879 
880  // remove friction cone from constraint set
881  pimpl->solver->removeConstraint(pimpl->frictionConstraint);
882  pimpl->frictionConstraintIsRegisteredInConstraint = false;
883 }
884 
885 
891 {
892  pimpl->frictionConstraint.getFunction().setFrictionCoeff(getFrictionCoeff());
893 }
894 
900 {
901  pimpl->frictionConstraint.getFunction().setMargin(getMargin());
902 }
903 
904 
905 
906 
907 
908 
909 
910 
911 
917 {
919  if(!pimpl->isRegisteredAsObjective)
920  {
921  pimpl->solver->addObjective(*pimpl->innerTaskAsObjective);
922  pimpl->isRegisteredAsObjective = true;
923  }
924 
925  if (getTaskType() == FORCETASK)
926  {
928  }
929 }
930 
936 {
938  if(pimpl->isRegisteredAsObjective)
939  {
940  pimpl->solver->removeObjective(*pimpl->innerTaskAsObjective);
941  pimpl->isRegisteredAsObjective = false;
942  }
943  if (getTaskType() == FORCETASK)
944  {
946  }
947 }
948 
955 {
957  if(!pimpl->isRegisteredAsConstraint)
958  {
959  pimpl->solver->addConstraint(pimpl->innerTaskAsConstraint);
960  pimpl->isRegisteredAsConstraint = true;
961  }
962  if (getTaskType() == FORCETASK)
963  {
965  }
966 }
967 
973 {
975  if(pimpl->isRegisteredAsConstraint)
976  {
977  pimpl->solver->removeConstraint(pimpl->innerTaskAsConstraint);
978  pimpl->isRegisteredAsConstraint = false;
979  }
980  if (getTaskType() == FORCETASK)
981  {
983  }
984 }
985 
986 
987 
994 {
995  if (pimpl->innerTaskAsObjective)
996  {
997  pimpl->innerTaskAsObjective->getFunction().changeWeight(getWeight());
998  }
999 
1000 
1001 }
1002 
1003 
1005 {
1006  return getFeature()->getState();
1007 }
1008 
1010 {
1011  return pimpl->featureDes ? getFeatureDes()->getState() : getFeature()->getState();
1012 }
1013 
1014 void Task::setDesiredTaskState(const TaskState& newDesiredTaskState)
1015 {
1016  // Implement Trajectory thread here.
1017 }
1018 
1019 void Task::setDesiredTaskStateDirect(const TaskState& newDesiredTaskState)
1020 {
1021  if (pimpl->featureDes) {
1022  getFeatureDes()->setState(newDesiredTaskState);
1023  }
1024 }
1025 
1026 
1027 
1028 
1029 
1030 }
1031 
1032 
1033 
1034 
1035 
1036 
1037 
1038 
1039 
1040 
1041 
1042 
1043 
1044 
1045 
1046 // cmake:sourcegroup=Api
void doSetFrictionCoeff()
Definition: Task.cpp:890
const Eigen::MatrixXd & getJacobian() const
Definition: Task.cpp:600
EqualZeroConstraintPtr< LinearFunction > innerTaskAsConstraint
Definition: Task.cpp:75
void doDeactivateAsObjective()
Definition: Task.cpp:935
bool useReducedProblem
Definition: Task.cpp:60
double getMargin() const
Definition: Task.cpp:527
VectorXd errorDdot
Definition: Task.cpp:45
bool contactActive
Definition: Task.cpp:52
#define M_PI
Definition: MathTypes.h:43
void setDamping(double B)
Definition: Task.cpp:378
void setDesiredTaskState(const TaskState &newDesiredTaskState)
Definition: Task.cpp:1014
Pimpl(const std::string &name, std::shared_ptr< Model > m, Feature::Ptr s, Feature::Ptr sdes)
Definition: Task.cpp:77
MatrixXd K
Definition: Task.cpp:40
MatrixXd jacobian
Definition: Task.cpp:47
void doSetMargin()
Definition: Task.cpp:899
VectorXd output
Definition: Task.cpp:42
const Eigen::VectorXd & getErrorDot() const
Definition: Task.cpp:582
VectorXd effort
Definition: Task.cpp:46
bool taskHasBeenInitialized
Definition: Task.cpp:62
Vector3d frictionOffset
Definition: Task.cpp:48
MatrixXd M
Definition: Task.cpp:37
bool frictionConstraintIsRegisteredInConstraint
Definition: Task.cpp:66
bool isActiveAsObjective() const
Definition: Task.cpp:343
const Eigen::MatrixXd & getDamping() const
Definition: Task.cpp:466
const std::string & getName() const
void activateAsConstraint()
Definition: Task.cpp:312
const Eigen::VectorXd & getError() const
Definition: Task.cpp:576
int getDimension() const
Definition: Task.cpp:565
void update()
Definition: Task.cpp:259
int hierarchyLevel
Definition: Task.cpp:55
LinearizedCoulombFunction class.
void doActivateAsConstraint()
Definition: Task.cpp:954
Objective< SquaredLinearFunction > * innerTaskAsObjective
Definition: Task.cpp:74
void setFrictionConstraintOffset(const Eigen::Vector3d &offset)
Definition: Task.cpp:555
void doActivateContactMode()
Definition: Task.cpp:857
const Eigen::Vector3d & getFrictionConstraintOffset() const
Definition: Task.cpp:532
void setTaskType(Task::TYPETASK newTaskType)
Definition: Task.cpp:187
TYPETASK
Definition: Task.h:59
TaskState getTaskState()
Definition: Task.cpp:1004
void updateAccelerationTask()
Definition: Task.cpp:766
const Eigen::MatrixXd & getDesiredMassInverse() const
Definition: Task.cpp:453
void disconnectFromController()
Definition: Task.cpp:644
LinearFunction class.
GainsWorkspace gainsWorkspace
Definition: Task.cpp:36
Feature::Ptr feature
Definition: Task.cpp:33
const Eigen::VectorXd & getErrorDdot() const
Definition: Task.cpp:588
BaseVariable fcVar
Definition: Task.cpp:61
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
Task::META_TASK_TYPE getMetaTaskType()
Definition: Task.cpp:230
bool isBodyContactConstraint() const
Definition: Task.cpp:512
Feature::Ptr featureDes
Definition: Task.cpp:34
void updateForceTask()
Definition: Task.cpp:807
bool contactForceConstraintHasBeenSavedInSolver
Definition: Task.cpp:64
void connectFunctionnWithObjectiveAndConstraint()
Definition: Task.cpp:163
Feature::Ptr getFeature() const
Definition: Task.cpp:606
bool contactPointHasBeenSavedInModel
Definition: Task.cpp:65
void addContactPointInModel()
Definition: Task.cpp:713
void deactivate()
Definition: Task.cpp:326
void setAsAccelerationTask()
Definition: Task.cpp:134
void updateCoMMomentumTask()
Definition: Task.cpp:817
bool isRegisteredAsObjective
Definition: Task.cpp:69
void doSetWeight()
Definition: Task.cpp:993
EqualZeroConstraintPtr< LinearFunction > ContactForceConstraint
Definition: Task.cpp:67
Task(const std::string &name, std::shared_ptr< Model > model, Feature::Ptr feature, Feature::Ptr featureDes)
Definition: Task.cpp:171
virtual T & getFunction(void)
Definition: Objective.h:64
TYPETASK innerTaskType
Definition: Task.cpp:53
int getSize() const
Definition: Variable.cpp:81
virtual ~Task()
Definition: Task.cpp:183
void doDeactivateAsConstraint()
Definition: Task.cpp:972
MatrixXd B
Definition: Task.cpp:39
std::string getMetaTaskTypeAsString()
Definition: Task.cpp:235
META_TASK_TYPE
Definition: Task.h:44
void setAutoGains(double freq)
Definition: Task.cpp:412
void checkIfConnectedToController() const
Definition: Task.cpp:841
void setDesiredTaskStateDirect(const TaskState &newDesiredTaskState)
Definition: Task.cpp:1019
Create a linear function that represents the dynamic equation of motion.
SquaredLinearFunction class.
bool isActiveAsConstraint() const
Definition: Task.cpp:348
VectorXd error
Definition: Task.cpp:43
bool isDesiredMassTheActualOne() const
Definition: Task.cpp:436
double frictionCoeff
Definition: Task.cpp:49
Task::TYPETASK getTaskType()
Definition: Task.cpp:216
void setHierarchyLevel(int level)
Definition: Task.cpp:199
void setMetaTaskType(Task::META_TASK_TYPE newMetaTaskType)
Definition: Task.cpp:221
MatrixXd M_inverse
Definition: Task.cpp:38
const Eigen::VectorXd & getComputedForce() const
Definition: Task.cpp:639
double getFrictionCoeff() const
Definition: Task.cpp:522
void setMargin(double margin)
Definition: Task.cpp:543
void setWeight(double weight)
Definition: Task.cpp:501
void deactivateContactMode()
Definition: Task.cpp:490
void setStiffness(double K)
Definition: Task.cpp:395
LinearFunction * innerObjectiveFunction
Definition: Task.cpp:73
bool isPointContactTask() const
Definition: Task.cpp:517
const FullDynamicEquationFunction * dynamicEquation
Definition: Task.cpp:59
void setDesiredMass(double Md)
Definition: Task.cpp:358
const Eigen::VectorXd & getEffort() const
Definition: Task.cpp:594
void doActivateAsObjective()
Definition: Task.cpp:916
std::shared_ptr< Model > innerModel
Definition: Task.cpp:57
bool isRegisteredAsConstraint
Definition: Task.cpp:70
void setAsForceTask()
Definition: Task.cpp:156
bool useActualMass
Definition: Task.cpp:51
Feature::Ptr getFeatureDes() const
Definition: Task.cpp:611
bool isContactModeActive() const
Definition: Task.cpp:507
double margin
Definition: Task.cpp:50
void setAsTorqueTask()
Definition: Task.cpp:149
void setDesiredMassToActualOne()
Definition: Task.cpp:353
void doDeactivateContactMode()
Definition: Task.cpp:874
void updateTorqueTask()
Definition: Task.cpp:797
void activateAsObjective()
Definition: Task.cpp:298
const Eigen::VectorXd & getOutput() const
Definition: Task.cpp:570
const Eigen::VectorXd & getWeight() const
Definition: Task.cpp:560
void connectToController(std::shared_ptr< OneLevelSolver > _solver, const FullDynamicEquationFunction &dynamicEquation, bool useReducedProblem)
Definition: Task.cpp:668
void removeContactPointInModel()
Definition: Task.cpp:729
VectorXd errorDot
Definition: Task.cpp:44
META_TASK_TYPE innerMetaTaskType
Definition: Task.cpp:54
int getHierarchyLevel()
Definition: Task.cpp:195
LessThanZeroConstraintPtr< LinearizedCoulombFunction > frictionConstraint
Definition: Task.cpp:63
Implements a basic variable.
Definition: Variable.h:304
void doGetOutput(Eigen::VectorXd &output) const
Definition: Task.cpp:633
VectorXd weight
Definition: Task.cpp:41
TaskState getDesiredTaskState()
Definition: Task.cpp:1009
const Eigen::MatrixXd & getStiffness() const
Definition: Task.cpp:471
std::shared_ptr< OneLevelSolver > solver
Definition: Task.cpp:58
std::string convertToLowerCase(const std::string &originalString)
void activateContactMode()
Definition: Task.cpp:476
const Eigen::MatrixXd & getDesiredMass() const
Definition: Task.cpp:441
void setFrictionCoeff(double coeff)
Definition: Task.cpp:537