ModelPredictiveControl.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019, CNRS-UM LIRMM
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 
30 #include <mc_planning/Pendulum.h>
31 #include <mc_rtc/constants.h>
32 #include <mc_rtc/gui.h>
33 #include <mc_rtc/log/Logger.h>
34 
35 #include <copra/LMPC.h>
36 #include <copra/PreviewSystem.h>
37 #include <copra/constraints.h>
38 #include <copra/costFunctions.h>
39 #include <lipm_walking/Contact.h>
40 #include <lipm_walking/Preview.h>
41 #include <lipm_walking/Sole.h>
42 
43 namespace lipm_walking
44 {
45 
54 {
55  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
56 
57  static constexpr double SAMPLING_PERIOD = 0.1;
58  static constexpr unsigned INPUT_SIZE = 2;
59  static constexpr unsigned NB_STEPS = 16;
60  static constexpr unsigned STATE_SIZE =
61  6;
67 
73  void addGUIElements(std::shared_ptr<mc_rtc::gui::StateBuilder> gui);
74 
80  void addLogEntries(mc_rtc::Logger & logger);
81 
85  void configure(const mc_rtc::Configuration &);
86 
106  void phaseDurations(double initSupportDuration, double doubleSupportDuration, double targetSupportDuration);
107 
113  bool buildAndSolve();
114 
120  void comHeight(double height)
121  {
122  double zeta = height / mc_rtc::constants::GRAVITY;
123  double omegaInv = std::sqrt(zeta);
124  // clang-format off
125  dcmFromState_ <<
126  1, 0, omegaInv, 0, 0, 0,
127  0, 1, 0, omegaInv, 0, 0;
128  zmpFromState_ <<
129  1, 0, 0, 0, -zeta, 0,
130  0, 1, 0, 0, 0, -zeta;
131  // clang-format on
132  }
133 
144  {
145  initContact_ = initContact;
146  nextContact_ = nextContact;
147  targetContact_ = targetContact;
148  }
149 
155  void initState(const mc_planning::Pendulum & pendulum)
156  {
157  initState_ = Eigen::VectorXd(STATE_SIZE);
158  initState_ << pendulum.com().head<2>(), pendulum.comd().head<2>(), pendulum.comdd().head<2>();
159  }
160 
171  unsigned indexToHrep(unsigned i) const
172  {
173  return indexToHrep_[i];
174  }
175 
179  const Contact & initContact() const
180  {
181  return initContact_;
182  }
183 
188  unsigned nbInitSupportSteps() const
189  {
190  return nbInitSupportSteps_;
191  }
192 
198  unsigned nbDoubleSupportSteps() const
199  {
200  return nbDoubleSupportSteps_;
201  }
202 
206  const Contact & nextContact() const
207  {
208  return nextContact_;
209  }
210 
216  void sole(const Sole & sole)
217  {
218  sole_ = sole;
219  }
220 
224  const std::shared_ptr<Preview> solution() const
225  {
226  return solution_;
227  }
228 
232  const Contact & targetContact() const
233  {
234  return targetContact_;
235  }
236 
237 private:
238  void computeZMPRef();
239 
240  void updateTerminalConstraint();
241 
242  void updateZMPConstraint();
243 
244  void updateJerkCost();
245 
246  void updateVelCost();
247 
248  void updateZMPCost();
249 
250 public:
251  Eigen::Vector2d velWeights = {10., 10.};
252  double jerkWeight = 1.;
253  double zmpWeight = 1000.;
255 private:
256  using RefVector = Eigen::Matrix<double, 2 * (NB_STEPS + 1), 1>;
257  using StateMatrix = Eigen::Matrix<double, 2, STATE_SIZE>;
258 
259  Contact initContact_;
260  Contact nextContact_;
261  Contact targetContact_;
262  RefVector velRef_ = RefVector::Zero();
263  RefVector zmpRef_ = RefVector::Zero();
264  Eigen::Matrix<double, 2 * (NB_STEPS + 1), STATE_SIZE *(NB_STEPS + 1)> velCostMat_;
265  StateMatrix dcmFromState_ =
266  StateMatrix::Zero();
267  StateMatrix zmpFromState_ =
268  StateMatrix::Zero();
269  Eigen::VectorXd initState_;
270  HrepXd hreps_[4];
271  copra::SolverFlag solver_ = copra::SolverFlag::QLD;
272  double buildAndSolveTime_ = 0.;
273  double solveTime_ = 0.;
274  std::shared_ptr<Preview> solution_ = nullptr;
275  std::shared_ptr<copra::ControlCost> jerkCost_ = nullptr;
276  std::shared_ptr<copra::PreviewSystem> previewSystem_ =
277  nullptr;
278  std::shared_ptr<copra::TrajectoryConstraint> termDCMCons_ = nullptr;
279  std::shared_ptr<copra::TrajectoryConstraint> termZMPCons_ = nullptr;
280  std::shared_ptr<copra::TrajectoryConstraint> zmpCons_ = nullptr;
281  std::shared_ptr<copra::TrajectoryCost> velCost_ = nullptr;
282  std::shared_ptr<copra::TrajectoryCost> zmpCost_ = nullptr;
283  unsigned indexToHrep_[NB_STEPS + 1];
284  unsigned nbDoubleSupportSteps_ = 0;
285  Sole sole_;
286  unsigned nbInitSupportSteps_ =
287  0;
288  unsigned nbNextDoubleSupportSteps_ =
289  0;
290  unsigned nbTargetSupportSteps_ =
291  0;
292 };
293 
294 } // namespace lipm_walking
std::pair< Eigen::MatrixXd, Eigen::VectorXd > HrepXd
Definition: Contact.h:44
mc_planning::Pendulum Pendulum
Definition: Preview.cpp:37
void contacts(Contact initContact, Contact targetContact, Contact nextContact)
void addLogEntries(mc_rtc::Logger &logger)
void addGUIElements(std::shared_ptr< mc_rtc::gui::StateBuilder > gui)
const std::shared_ptr< Preview > solution() const
void initState(const mc_planning::Pendulum &pendulum)
static constexpr EIGEN_MAKE_ALIGNED_OPERATOR_NEW double SAMPLING_PERIOD
void configure(const mc_rtc::Configuration &)
void phaseDurations(double initSupportDuration, double doubleSupportDuration, double targetSupportDuration)