Preview.cpp
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 #include <iomanip>
30 #include <lipm_walking/Preview.h>
33 
34 namespace lipm_walking
35 {
36 
38 
39 namespace
40 {
41 
42 constexpr double SAMPLING_PERIOD = ModelPredictiveControl::SAMPLING_PERIOD;
43 constexpr unsigned INPUT_SIZE = ModelPredictiveControl::INPUT_SIZE;
44 constexpr unsigned NB_STEPS = ModelPredictiveControl::NB_STEPS;
45 constexpr unsigned STATE_SIZE = ModelPredictiveControl::STATE_SIZE;
46 
47 } // namespace
48 
50 {
51  inputTraj_ = Eigen::VectorXd::Zero(NB_STEPS * INPUT_SIZE);
52  stateTraj_ = Eigen::VectorXd::Zero((NB_STEPS + 1) * STATE_SIZE);
53 }
54 
55 Preview::Preview(const Eigen::VectorXd & stateTraj, const Eigen::VectorXd & inputTraj)
56 {
57  if(stateTraj.size() / STATE_SIZE != 1 + inputTraj.size() / INPUT_SIZE)
58  {
59  mc_rtc::log::error("Invalid state/input sizes, respectively {} and {}", stateTraj.size(), inputTraj.size());
60  }
61  stateTraj_ = stateTraj;
62  inputTraj_ = inputTraj;
63 }
64 
65 void Preview::integrate(Pendulum & pendulum, double dt)
66 {
67  if(playbackStep_ < NB_STEPS)
68  {
69  integratePlayback(pendulum, dt);
70  }
71  else // (playbackStep_ >= NB_STEPS)
72  {
73  integratePostPlayback(pendulum, dt);
74  }
75 }
76 
77 void Preview::integratePlayback(Pendulum & pendulum, double dt)
78 {
79  Eigen::Vector3d comddd;
80  comddd.head<INPUT_SIZE>() = inputTraj_.segment<INPUT_SIZE>(INPUT_SIZE * playbackStep_);
81  comddd.z() = 0.;
82  playbackTime_ += dt;
83  if(playbackTime_ >= (playbackStep_ + 1) * SAMPLING_PERIOD)
84  {
85  playbackStep_++;
86  }
87  pendulum.integrateCoMJerk(comddd, dt);
88 }
89 
90 void Preview::integratePostPlayback(Pendulum & pendulum, double dt)
91 {
92  Eigen::Vector3d comddd;
93  Eigen::VectorXd lastState = stateTraj_.segment<STATE_SIZE>(STATE_SIZE * NB_STEPS);
94  Eigen::Vector2d comd_f = lastState.segment<2>(2);
95  Eigen::Vector2d comdd_f = lastState.segment<2>(4);
96  if(std::abs(comd_f.x() * comdd_f.y() - comd_f.y() * comdd_f.x()) > 1e-4)
97  {
98  mc_rtc::log::warning("MPC terminal condition is not properly fulfilled");
99  }
100  double omega_f = -comd_f.dot(comdd_f) / comd_f.dot(comd_f);
101  double lambda_f = std::pow(omega_f, 2);
102  comddd = -omega_f * pendulum.comdd() - lambda_f * pendulum.comd();
103  comddd.z() = 0.;
104  pendulum.integrateCoMJerk(comddd, dt);
105 }
106 
107 } // namespace lipm_walking
mc_planning::Pendulum Pendulum
Definition: Preview.cpp:37
static constexpr EIGEN_MAKE_ALIGNED_OPERATOR_NEW double SAMPLING_PERIOD
void integrate(mc_planning::Pendulum &state, double dt)
Definition: Preview.cpp:65
void integratePostPlayback(mc_planning::Pendulum &state, double dt)
Definition: Preview.cpp:90
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Preview()
Definition: Preview.cpp:49
void integratePlayback(mc_planning::Pendulum &state, double dt)
Definition: Preview.cpp:77