MCController.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2022 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 
8 #include <mc_control/Contact.h>
9 
11 
13 #include <mc_rbdyn/Robots.h>
14 
15 #include <mc_rtc/DataStore.h>
16 #include <mc_rtc/gui.h>
17 #include <mc_rtc/log/Logger.h>
18 #include <mc_rtc/unique_ptr.h>
19 
25 #include <mc_solver/QPSolver.h>
26 
27 #include <mc_tasks/PostureTask.h>
28 
29 namespace mc_rbdyn
30 {
31 struct Contact;
32 }
33 
34 #include <mc_control/api.h>
35 
36 namespace mc_control
37 {
38 
46 {
48  const std::vector<std::vector<double>> q;
49 };
50 
53 {
54  inline ControllerParameters() = default;
55  inline ControllerParameters(const ControllerParameters &) = default;
59 
60 #define ADD_PARAMETER(TYPE, NAME, DEFAULT) \
61  TYPE NAME##_ = DEFAULT; \
62  ControllerParameters & NAME(TYPE value) \
63  { \
64  NAME##_ = value; \
65  return *this; \
66  }
70  ADD_PARAMETER(bool, load_robot_config, true)
73  ADD_PARAMETER(bool, overwrite_config, false)
75  ADD_PARAMETER(bool, load_robot_config_with_module_name, true)
77  ADD_PARAMETER(std::vector<std::string>, load_robot_config_into, {"robots"})
84  ADD_PARAMETER(std::vector<std::string>, extra_configurations, {})
85 
87  inline ControllerParameters(mc_solver::QPSolver::Backend backend) : backend_(backend) {}
88 };
89 
90 struct MCGlobalController;
91 
99 {
100  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
101  friend struct MCGlobalController;
102 
103 public:
106 
107  virtual ~MCController();
120  virtual bool run();
121 
130  virtual void createObserverPipelines(const mc_rtc::Configuration & config);
131 
152  virtual bool runObserverPipelines();
153 
160  virtual bool resetObserverPipelines();
161 
166  bool hasObserverPipeline(const std::string & name) const;
167 
169  bool hasObserverPipeline() const;
170 
175  const std::vector<mc_observers::ObserverPipeline> & observerPipelines() const;
177  std::vector<mc_observers::ObserverPipeline> & observerPipelines();
178 
184  const mc_observers::ObserverPipeline & observerPipeline(const std::string & name) const;
187 
196 
203  bool run(mc_solver::FeedbackType fType);
204 
212  virtual void stop();
213 
229  virtual void reset(const ControllerResetData & reset_data);
230 
236  void addCollisions(const std::string & r1,
237  const std::string & r2,
238  const std::vector<mc_rbdyn::Collision> & collisions);
239 
241  bool hasCollision(const std::string & r1, const std::string & r2, const mc_rbdyn::Collision & col) const noexcept;
242 
244  bool hasCollision(const std::string & r1,
245  const std::string & r2,
246  const std::string & c1,
247  const std::string & c2) const noexcept;
248 
254  void removeCollisions(const std::string & r1,
255  const std::string & r2,
256  const std::vector<mc_rbdyn::Collision> & collisions);
257 
263  void removeCollisions(const std::string & r1, const std::string & r2);
264 
270  void addContact(const Contact & c);
271 
277  void removeContact(const Contact & c);
278 
281 
283  const ContactSet & contacts() const;
284 
286  bool hasContact(const Contact & c) const;
287 
289  inline bool hasRobot(const std::string & robot) const noexcept { return robots().hasRobot(robot); }
290 
294  inline const mc_rbdyn::Robots & robots() const noexcept { return solver().robots(); }
295 
297  inline mc_rbdyn::Robots & robots() noexcept { return solver().robots(); }
298 
306  inline const mc_rbdyn::Robot & robot() const noexcept { return robots().robot(); }
307 
309  inline mc_rbdyn::Robot & robot() noexcept { return robots().robot(); }
310 
316  inline const mc_rbdyn::Robot & robot(const std::string & name) const { return robots().robot(name); }
317 
319  inline mc_rbdyn::Robot & robot(const std::string & name) { return robots().robot(name); }
320 
328  inline const mc_rbdyn::Robot & env() const noexcept { return robots().env(); }
329 
331  inline mc_rbdyn::Robot & env() noexcept { return robots().env(); }
337  inline const mc_solver::QPSolver & solver() const noexcept { return *qpsolver; }
338 
340  inline mc_solver::QPSolver & solver() noexcept { return *qpsolver; }
341 
343  inline mc_rtc::Logger & logger() noexcept { return *logger_; }
344 
346  inline std::shared_ptr<mc_rtc::gui::StateBuilder> gui() const noexcept { return gui_; }
347 
349  inline mc_rtc::DataStore & datastore() noexcept { return datastore_; }
350 
352  const mc_rtc::DataStore & datastore() const noexcept { return datastore_; }
353 
361  inline const mc_rbdyn::Robots & realRobots() const noexcept { return solver().realRobots(); }
363  inline mc_rbdyn::Robots & realRobots() noexcept { return solver().realRobots(); }
364 
368  inline const mc_rbdyn::Robot & realRobot() const noexcept { return realRobots().robot(); }
370  inline mc_rbdyn::Robot & realRobot() noexcept { return realRobots().robot(); }
371 
377  inline const mc_rbdyn::Robot & realRobot(const std::string & name) const { return realRobots().robot(name); }
378 
380  inline mc_rbdyn::Robot & realRobot(const std::string & name) { return realRobots().robot(name); }
393  inline const mc_rbdyn::Robots & outputRobots() const noexcept { return *outputRobots_; }
395  inline mc_rbdyn::Robots & outputRobots() noexcept { return *outputRobots_; }
396 
400  inline const mc_rbdyn::Robot & outputRobot() const noexcept { return outputRobots_->robot(); }
402  inline mc_rbdyn::Robot & outputRobot() noexcept { return outputRobots_->robot(); }
403 
409  inline const mc_rbdyn::Robot & outputRobot(const std::string & name) const { return outputRobots_->robot(name); }
410 
412  inline mc_rbdyn::Robot & outputRobot(const std::string & name) { return outputRobots_->robot(name); }
426  inline const mc_rbdyn::Robots & outputRealRobots() const noexcept { return *outputRealRobots_; }
428  inline mc_rbdyn::Robots & outputRealRobots() noexcept { return *outputRealRobots_; }
429 
433  inline const mc_rbdyn::Robot & outputRealRobot() const noexcept { return outputRealRobots_->robot(); }
435  inline mc_rbdyn::Robot & outputRealRobot() noexcept { return outputRealRobots_->robot(); }
436 
442  inline const mc_rbdyn::Robot & outputRealRobot(const std::string & name) const
443  {
444  return outputRealRobots_->robot(name);
445  }
446 
448  inline mc_rbdyn::Robot & outputRealRobot(const std::string & name) { return outputRealRobots_->robot(name); }
461  virtual void supported_robots(std::vector<std::string> & out) const;
462 
473  mc_rbdyn::Robot & loadRobot(mc_rbdyn::RobotModulePtr rm, const std::string & name);
474 
479  void removeRobot(const std::string & name);
480 
482  mc_rtc::Configuration & config() { return config_; }
483 
485  const mc_rtc::Configuration & config() const { return config_; }
486 
495  Gripper & gripper(const std::string & robot, const std::string & gripper);
496 
500  template<typename T>
501  struct DeprecatedAnchorFrame : public std::false_type
502  {
503  };
504 
509  template<typename T = void> // void for shorter error message
510  inline void anchorFrame(const sva::PTransformd &)
511  {
512  static_assert(DeprecatedAnchorFrame<T>::value,
513  "[MC_RTC_DEPRECATED] The anchorFrame and anchorFrameReal accessors are no longer supported, please "
514  "remove calls to these functions from your code and replace it with a datastore callback. For "
515  "further information please refer to the observer's tutorial: "
516  "https://jrl-umi3218.github.io/mc_rtc/tutorials/recipes/observers.html");
517  }
518 
523  template<typename T = void> // void for shorter error message
524  inline void anchorFrameReal(const sva::PTransformd &)
525  {
526  static_assert(DeprecatedAnchorFrame<T>::value,
527  "[MC_RTC_DEPRECATED] The anchorFrame and anchorFrameReal accessors are no longer supported, please "
528  "remove calls to these functions from your code and replace it with a datastore callback. For "
529  "further information please refer to the observer's tutorial: "
530  "https://jrl-umi3218.github.io/mc_rtc/tutorials/recipes/observers.html");
531  }
536  template<typename T = void> // void for shorter error message
537  inline const sva::PTransformd & anchorFrame() const
538  {
539  static_assert(DeprecatedAnchorFrame<T>::value,
540  "[MC_RTC_DEPRECATED] The anchorFrame and anchorFrameReal accessors are no longer supported, please "
541  "remove calls to these functions from your code and replace it with a datastore callback. For "
542  "further information please refer to the observer's tutorial: "
543  "https://jrl-umi3218.github.io/mc_rtc/tutorials/recipes/observers.html");
544  return robot().posW();
545  }
546 
551  template<typename T = void> // void for shorter error message
552  inline const sva::PTransformd & anchorFrameReal() const
553  {
554  static_assert(DeprecatedAnchorFrame<T>::value,
555  "[MC_RTC_DEPRECATED] The anchorFrame and anchorFrameReal accessors are no longer supported, please "
556  "remove calls to these functions from your code and replace it with a datastore callback. For "
557  "further information please refer to the observer's tutorial: "
558  "https://jrl-umi3218.github.io/mc_rtc/tutorials/recipes/observers.html");
559  return robot().posW();
560  }
561 
562 protected:
571  MCController(std::shared_ptr<mc_rbdyn::RobotModule> robot, double dt, ControllerParameters params = {});
572 
583  MCController(std::shared_ptr<mc_rbdyn::RobotModule> robot,
584  double dt,
585  const mc_rtc::Configuration & config,
586  ControllerParameters params = {});
587 
596  MCController(const std::vector<std::shared_ptr<mc_rbdyn::RobotModule>> & robot_modules,
597  double dt,
598  ControllerParameters params = {});
599 
610  MCController(const std::vector<std::shared_ptr<mc_rbdyn::RobotModule>> & robot_modules,
611  double dt,
612  const mc_rtc::Configuration & config,
613  ControllerParameters params = {});
614 
626  const std::string & name,
627  mc_rbdyn::Robots & robots,
628  const mc_rbdyn::LoadRobotParameters & params);
629 
631  void addRobotToLog(const mc_rbdyn::Robot & robot);
632 
634  void addRobotToGUI(const mc_rbdyn::Robot & robot);
635 
638 
639 protected:
641  std::shared_ptr<mc_solver::QPSolver> qpsolver;
642 
651  std::vector<mc_rbdyn::RobotConverter> converters_;
652 
654  std::vector<mc_observers::ObserverPipeline> observerPipelines_;
655 
657  std::shared_ptr<mc_rtc::Logger> logger_;
659  std::shared_ptr<mc_rtc::gui::StateBuilder> gui_;
660 
663 
667 
670  std::vector<mc_solver::ConstraintSetPtr> constraints_;
671 
673  std::shared_ptr<mc_solver::ContactConstraint> contact_constraint_ = nullptr;
674 
677  std::map<std::pair<std::string, std::string>, std::shared_ptr<mc_solver::CollisionsConstraint>> collision_constraints_;
678 
684  using ContactTableDataT = std::tuple<std::string, std::string, std::string, std::string, std::string, double>;
686  std::vector<ContactTableDataT> contacts_table_;
687 
688  using duration_ms = std::chrono::duration<double, std::milli>;
690  duration_ms updateContacts_dt_{0};
691 
692 public:
694  const double timeStep;
706  std::shared_ptr<mc_tasks::PostureTask> postureTask;
707  /* Controller's name */
708  const std::string name_;
710  const std::string loading_location_;
711 
719  mc_rtc::Configuration robot_config(const std::string & robot_name) const;
720 
723 
730  static void set_loading_location(std::string_view location);
731 
736  static void set_name(std::string_view name);
737 };
738 
739 namespace details
740 {
741 
748 template<MCController::Backend backend, typename SolverT>
750 {
752  : MCController(robot, dt, config, backend)
753  {
754  }
755 
756  BackendSpecificController(const std::vector<mc_rbdyn::RobotModulePtr> & robots,
757  double dt,
758  const mc_rtc::Configuration & config = {},
759  ControllerParameters params = {})
760  : MCController(robots, dt, config, params.backend(backend))
761  {
762  }
763 
764  const SolverT & solver() const noexcept { return SolverT::from_solver(MCController::solver()); }
765 
766  SolverT & solver() noexcept { return SolverT::from_solver(MCController::solver()); }
767 };
768 
769 } // namespace details
770 
771 } // namespace mc_control
772 
773 #ifdef WIN32
774 # define CONTROLLER_MODULE_API __declspec(dllexport)
775 #else
776 # if __GNUC__ >= 4
777 # define CONTROLLER_MODULE_API __attribute__((visibility("default")))
778 # else
779 # define CONTROLLER_MODULE_API
780 # endif
781 #endif
#define ADD_PARAMETER(TYPE, NAME, DEFAULT)
Definition: MCController.h:60
#define MC_CONTROL_DLLAPI
Definition: api.h:50
mc_control::Contact Contact
Definition: Controller.h:22
Definition: CompletionCriteria.h:11
std::unordered_set< Contact, std::hash< Contact >, std::equal_to< Contact >, Eigen::aligned_allocator< Contact > > ContactSet
Definition: Contact.h:62
Definition: generic_gripper.h:15
std::shared_ptr< Robots > RobotsPtr
Definition: fwd.h:17
std::shared_ptr< RobotModule > RobotModulePtr
Definition: RobotModule.h:629
Definition: Contact.h:67
Definition: Contact.h:26
Extra parameters that influence the creator construction.
Definition: MCController.h:53
ControllerParameters(ControllerParameters &&)=default
ControllerParameters & operator=(const ControllerParameters &)=default
ControllerParameters & operator=(ControllerParameters &&)=default
ControllerParameters(const ControllerParameters &)=default
ControllerParameters(mc_solver::QPSolver::Backend backend)
Definition: MCController.h:87
Contains information allowing the controller to start smoothly from the current state of the robot.
Definition: MCController.h:46
const std::vector< std::vector< double > > q
Definition: MCController.h:48
A robot's gripper reprensentation.
Definition: generic_gripper.h:35
MCController is the base class to implement all controllers. It assumes that at least two robots are ...
Definition: MCController.h:99
virtual void createObserverPipelines(const mc_rtc::Configuration &config)
const mc_rbdyn::Robots & realRobots() const noexcept
Definition: MCController.h:361
void removeCollisions(const std::string &r1, const std::string &r2)
bool hasContact(const Contact &c) const
const mc_rbdyn::Robot & outputRobot(const std::string &name) const
Definition: MCController.h:409
std::tuple< std::string, std::string, std::string, std::string, std::string, double > ContactTableDataT
Definition: MCController.h:684
const std::string name_
Definition: MCController.h:708
mc_rtc::unique_ptr< mc_solver::ContactConstraint > contactConstraint
Definition: MCController.h:696
const mc_rbdyn::Robot & realRobot(const std::string &name) const
Definition: MCController.h:377
const mc_observers::ObserverPipeline & observerPipeline() const
mc_rbdyn::Robot & outputRealRobot(const std::string &name)
Definition: MCController.h:448
void addContact(const Contact &c)
mc_rtc::unique_ptr< mc_solver::DynamicsConstraint > dynamicsConstraint
Definition: MCController.h:698
const mc_rbdyn::Robot & outputRealRobot(const std::string &name) const
Definition: MCController.h:442
mc_rbdyn::Robot & outputRealRobot() noexcept
Definition: MCController.h:435
std::vector< mc_solver::ConstraintSetPtr > constraints_
Definition: MCController.h:670
const mc_rbdyn::Robot & outputRealRobot() const noexcept
Definition: MCController.h:433
mc_rbdyn::Robots & outputRealRobots() noexcept
Definition: MCController.h:428
mc_rbdyn::Robot & realRobot() noexcept
Definition: MCController.h:370
MCController(const std::vector< std::shared_ptr< mc_rbdyn::RobotModule >> &robot_modules, double dt, ControllerParameters params={})
MCController(std::shared_ptr< mc_rbdyn::RobotModule > robot, double dt, const mc_rtc::Configuration &config, ControllerParameters params={})
const mc_solver::QPSolver & solver() const noexcept
Definition: MCController.h:337
mc_rbdyn::Robot & outputRobot(const std::string &name)
Definition: MCController.h:412
mc_rtc::unique_ptr< mc_solver::CompoundJointConstraint > compoundJointConstraint
Definition: MCController.h:704
void anchorFrame(const sva::PTransformd &)
Definition: MCController.h:510
mc_rbdyn::Robot & loadRobot(mc_rbdyn::RobotModulePtr rm, const std::string &name)
bool hasCollision(const std::string &r1, const std::string &r2, const mc_rbdyn::Collision &col) const noexcept
const sva::PTransformd & anchorFrame() const
Definition: MCController.h:537
const std::string loading_location_
Definition: MCController.h:710
mc_rbdyn::RobotsPtr outputRealRobots_
Definition: MCController.h:649
const std::vector< mc_observers::ObserverPipeline > & observerPipelines() const
std::vector< ContactTableDataT > contacts_table_
Definition: MCController.h:686
virtual void reset(const ControllerResetData &reset_data)
std::shared_ptr< mc_tasks::PostureTask > postureTask
Definition: MCController.h:706
mc_rtc::unique_ptr< mc_solver::CollisionsConstraint > selfCollisionConstraint
Definition: MCController.h:702
mc_rtc::DataStore datastore_
Definition: MCController.h:666
const mc_rbdyn::Robot & realRobot() const noexcept
Definition: MCController.h:368
std::shared_ptr< mc_solver::QPSolver > qpsolver
Definition: MCController.h:641
virtual bool runObserverPipelines()
mc_observers::ObserverPipeline & observerPipeline(const std::string &name)
const ContactSet & contacts() const
const mc_rbdyn::Robots & outputRealRobots() const noexcept
Definition: MCController.h:426
bool contacts_changed_
Definition: MCController.h:682
std::shared_ptr< mc_rtc::gui::StateBuilder > gui_
Definition: MCController.h:659
mc_rtc::Configuration robot_config(const std::string &robot_name) const
void addCollisions(const std::string &r1, const std::string &r2, const std::vector< mc_rbdyn::Collision > &collisions)
std::chrono::duration< double, std::milli > duration_ms
Definition: MCController.h:688
std::shared_ptr< mc_rtc::Logger > logger_
Definition: MCController.h:657
MCController(std::shared_ptr< mc_rbdyn::RobotModule > robot, double dt, ControllerParameters params={})
mc_rbdyn::Robots & robots() noexcept
Definition: MCController.h:297
Gripper & gripper(const std::string &robot, const std::string &gripper)
const mc_rbdyn::Robot & robot(const std::string &name) const
Definition: MCController.h:316
static void set_name(std::string_view name)
void removeCollisions(const std::string &r1, const std::string &r2, const std::vector< mc_rbdyn::Collision > &collisions)
mc_rbdyn::Robots & realRobots() noexcept
Definition: MCController.h:363
static void set_loading_location(std::string_view location)
const mc_rtc::Configuration & config() const
Definition: MCController.h:485
mc_rtc::Configuration robot_config(const mc_rbdyn::Robot &robot) const
bool run(mc_solver::FeedbackType fType)
mc_rbdyn::Robot & robot() noexcept
Definition: MCController.h:309
mc_rbdyn::Robots & outputRobots() noexcept
Definition: MCController.h:395
const mc_observers::ObserverPipeline & observerPipeline(const std::string &name) const
void addRobotToGUI(const mc_rbdyn::Robot &robot)
bool hasCollision(const std::string &r1, const std::string &r2, const std::string &c1, const std::string &c2) const noexcept
mc_rbdyn::RobotsPtr outputRobots_
Definition: MCController.h:648
mc_rbdyn::Robot & env() noexcept
Definition: MCController.h:331
virtual void supported_robots(std::vector< std::string > &out) const
mc_rtc::Logger & logger() noexcept
Definition: MCController.h:343
void addRobotToLog(const mc_rbdyn::Robot &robot)
std::vector< mc_observers::ObserverPipeline > observerPipelines_
Definition: MCController.h:654
mc_rbdyn::Robot & outputRobot() noexcept
Definition: MCController.h:402
const mc_rtc::DataStore & datastore() const noexcept
Definition: MCController.h:352
void anchorFrameReal(const sva::PTransformd &)
Definition: MCController.h:524
mc_solver::QPSolver & solver() noexcept
Definition: MCController.h:340
bool hasObserverPipeline() const
mc_rtc::Configuration & config()
Definition: MCController.h:482
const mc_rbdyn::Robot & robot() const noexcept
Definition: MCController.h:306
mc_rtc::unique_ptr< mc_solver::KinematicsConstraint > kinematicsConstraint
Definition: MCController.h:700
const mc_rbdyn::Robots & outputRobots() const noexcept
Definition: MCController.h:393
const mc_rbdyn::Robot & outputRobot() const noexcept
Definition: MCController.h:400
const mc_rbdyn::Robot & env() const noexcept
Definition: MCController.h:328
mc_rtc::Configuration config_
Definition: MCController.h:662
std::vector< mc_observers::ObserverPipeline > & observerPipelines()
mc_observers::ObserverPipeline & observerPipeline()
std::shared_ptr< mc_rtc::gui::StateBuilder > gui() const noexcept
Definition: MCController.h:346
std::map< std::pair< std::string, std::string >, std::shared_ptr< mc_solver::CollisionsConstraint > > collision_constraints_
Definition: MCController.h:677
std::vector< mc_rbdyn::RobotConverter > converters_
Definition: MCController.h:651
const mc_rbdyn::Robots & robots() const noexcept
Definition: MCController.h:294
virtual bool resetObserverPipelines()
Reset the observers.
mc_rbdyn::Robot & robot(const std::string &name)
Definition: MCController.h:319
void removeContact(const Contact &c)
const double timeStep
Definition: MCController.h:694
const sva::PTransformd & anchorFrameReal() const
Definition: MCController.h:552
void removeRobot(const std::string &name)
mc_rbdyn::Robot & realRobot(const std::string &name)
Definition: MCController.h:380
MCController(const std::vector< std::shared_ptr< mc_rbdyn::RobotModule >> &robot_modules, double dt, const mc_rtc::Configuration &config, ControllerParameters params={})
bool hasObserverPipeline(const std::string &name) const
mc_rbdyn::Robot & loadRobot(mc_rbdyn::RobotModulePtr rm, const std::string &name, mc_rbdyn::Robots &robots, const mc_rbdyn::LoadRobotParameters &params)
ContactSet contacts_
Definition: MCController.h:680
mc_rtc::DataStore & datastore() noexcept
Definition: MCController.h:349
bool hasRobot(const std::string &robot) const noexcept
Definition: MCController.h:289
Definition: mc_global_controller.h:23
const SolverT & solver() const noexcept
Definition: MCController.h:764
SolverT & solver() noexcept
Definition: MCController.h:766
BackendSpecificController(mc_rbdyn::RobotModulePtr robot, double dt, const mc_rtc::Configuration &config={})
Definition: MCController.h:751
BackendSpecificController(const std::vector< mc_rbdyn::RobotModulePtr > &robots, double dt, const mc_rtc::Configuration &config={}, ControllerParameters params={})
Definition: MCController.h:756
State observation pipeline.
Definition: ObserverPipeline.h:38
Definition: Collision.h:21
Definition: Robot.h:33
Definition: Robot.h:63
Definition: Robots.h:16
mc_rbdyn::Robots & robots() noexcept
Definition: Robots.h:51
Simplify access to values hold within a JSON file.
Definition: Configuration.h:166
Generic data store.
Definition: DataStore.h:133
Logs controller data to disk.
Definition: Logger.h:30
Definition: QPSolver.h:86
Backend
Definition: QPSolver.h:91