Controller.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 
10 #include <mc_tasks/PostureTask.h>
11 
12 #include <functional>
13 
14 namespace mc_control
15 {
16 
17 namespace fsm
18 {
19 
20 struct Controller;
21 
23 
24 } // namespace fsm
25 
26 } // namespace mc_control
27 
28 namespace mc_control
29 {
30 
31 namespace fsm
32 {
33 
35 
50 {
51  friend struct Executor;
52 
54  double dt,
55  const mc_rtc::Configuration & config,
56  ControllerParameters params = {});
57 
58  ~Controller() override;
59 
60  bool run() override;
61 
67  bool run(mc_solver::FeedbackType fType);
68 
69  void reset(const ControllerResetData & data) override;
70 
79  virtual void interrupt() { executor_.interrupt(); }
80 
82  bool running() { return executor_.running(); }
83 
93  bool resume(const std::string & state);
94 
101  std::shared_ptr<mc_tasks::PostureTask> getPostureTask(const std::string & robot);
102 
104  mc_solver::ContactConstraint & contactConstraint() { return *contact_constraint_; }
105 
107 #ifndef MC_RTC_BUILD_STATIC
108  StateFactory & factory() { return factory_; }
109 #else
110  static StateFactory & factory()
111  {
112  if(!factory_ptr_) { factory_ptr_.reset(new StateFactory({}, {}, false)); }
113  return *factory_ptr_;
114  }
115 #endif
116 
117 private:
119  void resetPostures();
120 
122  void startIdleState();
123 
125  void teardownIdleState();
126 
127 protected:
130  std::map<std::string, std::shared_ptr<mc_tasks::PostureTask>> posture_tasks_;
131  std::map<std::string, double> saved_posture_weights_;
132 
134  std::map<std::string, std::shared_ptr<mc_tasks::EndEffectorTask>> ff_tasks_;
135 
137 #ifndef MC_RTC_BUILD_STATIC
139 #else
140  static std::unique_ptr<StateFactory> factory_ptr_;
141  StateFactory & factory_;
142 #endif
143 
144  bool idle_keep_state_ = false;
146  bool running_ = false;
148  bool first_reset_ = true;
151 };
152 
153 namespace details
154 {
155 
162 template<MCController::Backend backend, typename SolverT>
164 {
166  double dt,
168  ControllerParameters params = {})
169  : Controller(rm, dt, config, params.backend(backend))
170  {
171  }
172 
173  const SolverT & solver() const noexcept { return SolverT::from_solver(MCController::solver()); }
174 
175  SolverT & solver() noexcept { return SolverT::from_solver(MCController::solver()); }
176 };
177 
178 } // namespace details
179 
180 } // namespace fsm
181 
182 } // namespace mc_control
mc_rtc::Configuration
Simplify access to values hold within a JSON file.
Definition: Configuration.h:165
mc_control::MCController::solver
const mc_solver::QPSolver & solver() const noexcept
Definition: MCController.h:337
PostureTask.h
mc_solver::ContactConstraint
Definition: ContactConstraint.h:20
mc_control::fsm::Controller::factory_
StateFactory factory_
Definition: Controller.h:138
mc_control::fsm::Controller::contactConstraint
mc_solver::ContactConstraint & contactConstraint()
Definition: Controller.h:104
MC_CONTROL_FSM_DLLAPI
#define MC_CONTROL_FSM_DLLAPI
Definition: api.h:50
mc_control::fsm::Controller::Controller
Controller(mc_rbdyn::RobotModulePtr rm, double dt, const mc_rtc::Configuration &config, ControllerParameters params={})
mc_control::ContactSet
std::unordered_set< Contact, std::hash< Contact >, std::equal_to< Contact >, Eigen::aligned_allocator< Contact > > ContactSet
Definition: Contact.h:62
mc_control::fsm::details::BackendSpecificController::solver
SolverT & solver() noexcept
Definition: Controller.h:175
EndEffectorTask.h
mc_control::fsm::Controller::saved_posture_weights_
std::map< std::string, double > saved_posture_weights_
Definition: Controller.h:131
mc_control::fsm::Controller::interrupt
virtual void interrupt()
Definition: Controller.h:79
mc_control::fsm::Controller::ff_tasks_
std::map< std::string, std::shared_ptr< mc_tasks::EndEffectorTask > > ff_tasks_
Definition: Controller.h:134
mc_control::ControllerParameters
Extra parameters that influence the creator construction.
Definition: MCController.h:52
mc_control::fsm::StateFactory
Definition: StateFactory.h:32
mc_control::fsm::Controller
struct MC_CONTROL_FSM_DLLAPI Controller
Definition: State.h:23
mc_control::Contact
Definition: Contact.h:25
mc_control::fsm::Controller::executor_
Executor executor_
Definition: Controller.h:150
mc_control::fsm::ContactSet
mc_control::ContactSet ContactSet
Definition: Controller.h:34
mc_control::fsm::Controller::posture_tasks_
std::map< std::string, std::shared_ptr< mc_tasks::PostureTask > > posture_tasks_
Definition: Controller.h:130
mc_control::fsm::details::BackendSpecificController::solver
const SolverT & solver() const noexcept
Definition: Controller.h:173
Executor.h
mc_control::fsm::Executor
Definition: Executor.h:32
mc_control::fsm::Controller::factory
StateFactory & factory()
Definition: Controller.h:108
mc_control::fsm::Controller::running
bool running()
Definition: Controller.h:82
MCController.h
mc_control::ControllerResetData
Contains information allowing the controller to start smoothly from the current state of the robot.
Definition: MCController.h:45
mc_control::fsm::details::BackendSpecificController::BackendSpecificController
BackendSpecificController(mc_rbdyn::RobotModulePtr rm, double dt, const mc_rtc::Configuration &config, ControllerParameters params={})
Definition: Controller.h:165
mc_control::fsm::Contact
mc_control::Contact Contact
Definition: Controller.h:22
mc_rbdyn::RobotModulePtr
std::shared_ptr< RobotModule > RobotModulePtr
Definition: RobotModule.h:629
mc_control
Definition: CompletionCriteria.h:10
mc_control::MCController
MCController is the base class to implement all controllers. It assumes that at least two robots are ...
Definition: MCController.h:98
mc_control::MCController::config
mc_rtc::Configuration & config()
Definition: MCController.h:482
mc_control::fsm::details::BackendSpecificController
Definition: Controller.h:163
mc_control::fsm::Controller
Definition: Controller.h:49