TVMQPSolver.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_solver/QPSolver.h>
8 
9 #include <mc_rtc/clock.h>
10 
11 #include <tvm/ControlProblem.h>
12 #include <tvm/LinearizedControlProblem.h>
13 #include <tvm/scheme/WeightedLeastSquares.h>
14 
15 namespace mc_solver
16 {
17 
23 struct MC_SOLVER_DLLAPI TVMQPSolver final : public QPSolver
24 {
25  TVMQPSolver(mc_rbdyn::RobotsPtr robots, double timeStep);
26 
27  TVMQPSolver(double timeStep);
28 
29  ~TVMQPSolver() final = default;
30 
31  void setContacts(ControllerToken, const std::vector<mc_rbdyn::Contact> & contacts) final;
32 
33  const sva::ForceVecd desiredContactForce(const mc_rbdyn::Contact & id) const final;
34 
35  double solveTime() final;
36 
37  double solveAndBuildTime() final;
38 
40  inline tvm::LinearizedControlProblem & problem() noexcept { return problem_; }
41 
43  inline const tvm::LinearizedControlProblem & problem() const noexcept { return problem_; }
44 
51  static inline TVMQPSolver & from_solver(QPSolver & solver) noexcept
52  {
53  assert(solver.backend() == QPSolver::Backend::TVM);
54  return static_cast<TVMQPSolver &>(solver);
55  }
56 
63  static inline const TVMQPSolver & from_solver(const QPSolver & solver) noexcept
64  {
65  assert(solver.backend() == QPSolver::Backend::TVM);
66  return static_cast<const TVMQPSolver &>(solver);
67  }
68 
69 private:
71  tvm::LinearizedControlProblem problem_;
73  tvm::scheme::WeightedLeastSquares solver_;
75  struct ContactData
76  {
78  tvm::TaskWithRequirementsPtr contactConstraint_;
80  tvm::VariableVector f1_;
82  std::vector<tvm::TaskWithRequirementsPtr> f1Constraints_;
84  tvm::VariableVector f2_;
86  std::vector<tvm::TaskWithRequirementsPtr> f2Constraints_;
87  };
89  std::vector<ContactData> contactsData_;
91  mc_rtc::duration_ms solve_dt_{0};
92 
94  bool runCommon();
96  bool runOpenLoop();
98  bool runJointsFeedback(bool wVelocity);
99 
120  bool runClosedLoop(bool integrateControlState);
121 
123  void updateRobot(mc_rbdyn::Robot & robot);
124 
126  std::vector<std::vector<double>> prev_encoders_{};
127  std::vector<std::vector<double>> encoders_alpha_{};
128  std::vector<std::vector<std::vector<double>>> control_q_{};
129  std::vector<std::vector<std::vector<double>>> control_alpha_{};
130 
132  std::unordered_map<std::string, DynamicsConstraint *> dynamics_;
133 
134  bool run_impl(FeedbackType fType = FeedbackType::None) final;
135 
136  void addDynamicsConstraint(mc_solver::DynamicsConstraint * dynamics) final;
137 
138  void removeDynamicsConstraint(mc_solver::ConstraintSet * maybe_dynamics) final;
139  void removeDynamicsConstraint(mc_solver::DynamicsConstraint * dyn);
140 
141  size_t getContactIdx(const mc_rbdyn::Contact & contact);
142  void addContact(const mc_rbdyn::Contact & contact);
143  using ContactIterator = std::vector<mc_rbdyn::Contact>::iterator;
144  ContactIterator removeContact(size_t idx);
145  std::tuple<size_t, bool> addVirtualContactImpl(const mc_rbdyn::Contact & contact);
146  void addContactToDynamics(const std::string & robot,
147  const mc_rbdyn::RobotFrame & frame,
148  const std::vector<sva::PTransformd> & points,
149  tvm::VariableVector & forces,
150  std::vector<tvm::TaskWithRequirementsPtr> & constraints,
151  const Eigen::MatrixXd & frictionCone,
152  double dir);
153 };
154 
161 inline TVMQPSolver & tvm_solver(QPSolver & solver) noexcept
162 {
163  return TVMQPSolver::from_solver(solver);
164 }
165 
166 /* const version */
167 inline const TVMQPSolver & tvm_solver(const QPSolver & solver) noexcept
168 {
169  return TVMQPSolver::from_solver(solver);
170 }
171 
172 } // namespace mc_solver
mc_solver::DynamicsConstraint
Definition: DynamicsConstraint.h:20
mc_solver::QPSolver::ControllerToken
Definition: QPSolver.h:101
mc_solver::QPSolver
Definition: QPSolver.h:85
mc_solver::TVMQPSolver::problem
const tvm::LinearizedControlProblem & problem() const noexcept
Definition: TVMQPSolver.h:43
mc_rbdyn::Robot
Definition: Robot.h:62
mc_solver::TVMQPSolver::from_solver
static TVMQPSolver & from_solver(QPSolver &solver) noexcept
Definition: TVMQPSolver.h:51
mc_rbdyn::RobotsPtr
std::shared_ptr< Robots > RobotsPtr
Definition: fwd.h:17
mc_solver::ConstraintSet
This class is a basis to wrap Constraint functions from Tasks. The aim of such wrappers should be two...
Definition: ConstraintSet.h:20
clock.h
mc_solver::QPSolver::Backend::TVM
@ TVM
mc_solver::TVMQPSolver::problem
tvm::LinearizedControlProblem & problem() noexcept
Definition: TVMQPSolver.h:40
mc_rbdyn::RobotFrame
Definition: RobotFrame.h:21
mc_solver::TVMQPSolver
Definition: TVMQPSolver.h:23
mc_rtc::duration_ms
std::chrono::duration< double, std::milli > duration_ms
Definition: clock.h:13
mc_rbdyn::Contact
Definition: Contact.h:48
mc_solver
Definition: Contact.h:17
mc_solver::TVMQPSolver::from_solver
static const TVMQPSolver & from_solver(const QPSolver &solver) noexcept
Definition: TVMQPSolver.h:63
MC_SOLVER_DLLAPI
#define MC_SOLVER_DLLAPI
Definition: api.h:50
mc_solver::tvm_solver
TVMQPSolver & tvm_solver(QPSolver &solver) noexcept
Definition: TVMQPSolver.h:161
QPSolver.h