MetaTask.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 
7 #include <mc_tasks/api.h>
8 
9 #include <mc_rtc/Configuration.h>
11 #include <mc_rtc/log/Logger.h>
12 
13 #include <mc_solver/QPSolver.h>
14 #include <mc_solver/api.h>
15 
16 #include <cmath>
17 
18 namespace mc_control
19 {
20 struct CompletionCriteria;
21 }
22 
23 namespace mc_solver
24 {
25 
26 struct TasksQPSolver;
27 struct TVMQPSolver;
28 
29 } // namespace mc_solver
30 
31 namespace mc_tasks
32 {
33 
40 {
41  friend struct mc_solver::QPSolver;
42  friend struct mc_solver::TasksQPSolver;
43  friend struct mc_solver::TVMQPSolver;
45 
48 
49 public:
50  inline MetaTask() : backend_(mc_solver::QPSolver::context_backend()) {}
51 
52  virtual ~MetaTask();
53 
55  const std::string & type() const { return type_; }
56 
64  virtual inline void name(const std::string & name) { name_ = name; }
65 
67  inline const std::string & name() const { return name_; }
68 
70  virtual void reset() = 0;
71 
80  virtual void dimWeight(const Eigen::VectorXd & dimW) = 0;
81 
83  virtual Eigen::VectorXd dimWeight() const = 0;
84 
100  virtual void selectActiveJoints(mc_solver::QPSolver & solver,
101  const std::vector<std::string> & activeJointsName,
102  const std::map<std::string, std::vector<std::array<int, 2>>> & activeDofs = {}) = 0;
103 
119  virtual void selectUnactiveJoints(
120  mc_solver::QPSolver & solver,
121  const std::vector<std::string> & unactiveJointsName,
122  const std::map<std::string, std::vector<std::array<int, 2>>> & unactiveDofs = {}) = 0;
123 
129  virtual void resetJointsSelector(mc_solver::QPSolver & solver) = 0;
130 
136  virtual Eigen::VectorXd eval() const = 0;
137 
143  virtual Eigen::VectorXd speed() const = 0;
144 
146  virtual void load(mc_solver::QPSolver & solver, const mc_rtc::Configuration & config);
147 
149  inline size_t iterInSolver() const noexcept { return iterInSolver_; }
150 
152  inline void resetIterInSolver() noexcept { iterInSolver_ = 0; }
153 
155  inline void incrementIterInSolver() noexcept { iterInSolver_++; }
156 
157  inline Backend backend() const noexcept { return backend_; }
158 
159 protected:
165  virtual void addToSolver(mc_solver::QPSolver & solver) = 0;
166 
169  static inline void addToSolver(MetaTask & t, mc_solver::QPSolver & solver) { t.addToSolver(solver); }
170 
176  virtual void removeFromSolver(mc_solver::QPSolver & solver) = 0;
177 
180  static inline void removeFromSolver(MetaTask & t, mc_solver::QPSolver & solver) { t.removeFromSolver(solver); }
181 
189  virtual void update(mc_solver::QPSolver & solver) = 0;
190 
192  static inline void update(MetaTask & t, mc_solver::QPSolver & solver) { t.update(solver); }
193 
201  virtual void addToLogger(mc_rtc::Logger &) {}
202 
205  static inline void addToLogger(MetaTask & t, mc_rtc::Logger & logger) { t.addToLogger(logger); }
206 
214  virtual void removeFromLogger(mc_rtc::Logger &);
215 
218  static inline void removeFromLogger(MetaTask & t, mc_rtc::Logger & logger) { t.removeFromLogger(logger); }
219 
228  virtual void addToGUI(mc_rtc::gui::StateBuilder &);
229 
232  static inline void addToGUI(MetaTask & t, mc_rtc::gui::StateBuilder & gui) { t.addToGUI(gui); }
233 
241  virtual void removeFromGUI(mc_rtc::gui::StateBuilder &);
242 
245  static inline void removeFromGUI(MetaTask & t, mc_rtc::gui::StateBuilder & gui) { t.removeFromGUI(gui); }
246 
247  inline static void ensureHasJoints(const mc_rbdyn::Robot & robot,
248  const std::vector<std::string> & joints,
249  const std::string & prefix)
250  {
251  for(const auto & jName : joints)
252  {
253  if(!robot.hasJoint(jName))
254  {
255  mc_rtc::log::error_and_throw("{} No joint named {} in robot {}", prefix, jName, robot.name());
256  }
257  }
258  }
259 
278  virtual std::function<bool(const mc_tasks::MetaTask & task, std::string &)> buildCompletionCriteria(
279  double dt,
280  const mc_rtc::Configuration & config) const;
281 
284 
285  std::string type_;
286  std::string name_;
287 
288  size_t iterInSolver_ = 0;
289 };
290 
291 using MetaTaskPtr = std::shared_ptr<MetaTask>;
292 
293 } // namespace mc_tasks
mc_rtc::Configuration
Simplify access to values hold within a JSON file.
Definition: Configuration.h:165
mc_solver::TasksQPSolver
Definition: TasksQPSolver.h:19
mc_rbdyn::Robot::hasJoint
bool hasJoint(const std::string &name) const
mc_tasks::MetaTask::backend
Backend backend() const noexcept
Definition: MetaTask.h:157
mc_tasks::MetaTask
Represents a generic task.
Definition: MetaTask.h:39
api.h
mc_tasks::MetaTask::removeFromLogger
virtual void removeFromLogger(mc_rtc::Logger &)
mc_tasks::MetaTask::addToLogger
static void addToLogger(MetaTask &t, mc_rtc::Logger &logger)
Definition: MetaTask.h:205
mc_tasks::MetaTask::removeFromGUI
virtual void removeFromGUI(mc_rtc::gui::StateBuilder &)
mc_tasks::MetaTask::name_
std::string name_
Definition: MetaTask.h:286
mc_solver::QPSolver
Definition: QPSolver.h:85
api.h
mc_tasks::MetaTask::addToLogger
virtual void addToLogger(mc_rtc::Logger &)
Definition: MetaTask.h:201
mc_rbdyn::Robot
Definition: Robot.h:62
mc_tasks::MetaTask::backend_
Backend backend_
Definition: MetaTask.h:283
mc_tasks::MetaTask::removeFromSolver
static void removeFromSolver(MetaTask &t, mc_solver::QPSolver &solver)
Definition: MetaTask.h:180
mc_tasks::MetaTask::iterInSolver
size_t iterInSolver() const noexcept
Get the number of iterations since the task was added to the solver.
Definition: MetaTask.h:149
mc_tasks::MetaTask::MetaTask
MetaTask()
Definition: MetaTask.h:50
mc_tasks::MetaTask::update
virtual void update(mc_solver::QPSolver &solver)=0
Update the task.
mc_rtc::Logger
Logs controller data to disk.
Definition: Logger.h:29
StateBuilder.h
mc_solver::TVMQPSolver
Definition: TVMQPSolver.h:23
mc_solver::QPSolver::Backend
Backend
Definition: QPSolver.h:90
Logger.h
mc_tasks::MetaTask::addToSolver
static void addToSolver(MetaTask &t, mc_solver::QPSolver &solver)
Definition: MetaTask.h:169
mc_tasks::MetaTaskPtr
std::shared_ptr< MetaTask > MetaTaskPtr
Definition: MetaTask.h:291
mc_rtc::gui::StateBuilder
Definition: StateBuilder.h:27
mc_tasks::MetaTask::update
static void update(MetaTask &t, mc_solver::QPSolver &solver)
Definition: MetaTask.h:192
mc_tasks::MetaTask::ensureHasJoints
static void ensureHasJoints(const mc_rbdyn::Robot &robot, const std::vector< std::string > &joints, const std::string &prefix)
Definition: MetaTask.h:247
mc_rtc::log::error_and_throw
void error_and_throw(Args &&... args)
Definition: logging.h:47
mc_tasks::MetaTask::name
virtual void name(const std::string &name)
Definition: MetaTask.h:64
mc_tasks::MetaTask::resetIterInSolver
void resetIterInSolver() noexcept
Set the number of iterations since the task was added to the solver to zero.
Definition: MetaTask.h:152
mc_tasks::MetaTask::removeFromGUI
static void removeFromGUI(MetaTask &t, mc_rtc::gui::StateBuilder &gui)
Definition: MetaTask.h:245
mc_rbdyn::Robot::name
const std::string & name() const
mc_tasks::MetaTask::addToGUI
static void addToGUI(MetaTask &t, mc_rtc::gui::StateBuilder &gui)
Definition: MetaTask.h:232
mc_tasks::MetaTask::type_
std::string type_
Definition: MetaTask.h:285
mc_tasks::MetaTask::name
const std::string & name() const
Definition: MetaTask.h:67
Configuration.h
mc_tasks::MetaTask::incrementIterInSolver
void incrementIterInSolver() noexcept
Increment the number of iterations since the task was added to the solver.
Definition: MetaTask.h:155
mc_solver
Definition: Contact.h:17
mc_control
Definition: CompletionCriteria.h:10
mc_tasks::MetaTask::removeFromLogger
static void removeFromLogger(MetaTask &t, mc_rtc::Logger &logger)
Definition: MetaTask.h:218
MC_SOLVER_DLLAPI
#define MC_SOLVER_DLLAPI
Definition: api.h:50
mc_tasks
Definition: StabilizerStandingState.h:11
mc_tasks::MetaTask::removeFromSolver
virtual void removeFromSolver(mc_solver::QPSolver &solver)=0
Remove the task from a solver.
mc_tasks::MetaTask::addToGUI
virtual void addToGUI(mc_rtc::gui::StateBuilder &)
mc_tasks::MetaTask::type
const std::string & type() const
Definition: MetaTask.h:55
mc_control::CompletionCriteria
Definition: CompletionCriteria.h:53
mc_tasks::MetaTask::addToSolver
virtual void addToSolver(mc_solver::QPSolver &solver)=0
Add the task to a solver.
QPSolver.h