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 
37 namespace
38 {
39 
40 constexpr double SAMPLING_PERIOD = ModelPredictiveControl::SAMPLING_PERIOD;
41 constexpr unsigned INPUT_SIZE = ModelPredictiveControl::INPUT_SIZE;
42 constexpr unsigned NB_STEPS = ModelPredictiveControl::NB_STEPS;
43 constexpr unsigned STATE_SIZE = ModelPredictiveControl::STATE_SIZE;
44 
45 } // namespace
46 
48 {
49  inputTraj_ = Eigen::VectorXd::Zero(NB_STEPS * INPUT_SIZE);
50  stateTraj_ = Eigen::VectorXd::Zero((NB_STEPS + 1) * STATE_SIZE);
51 }
52 
53 Preview::Preview(const Eigen::VectorXd & stateTraj, const Eigen::VectorXd & inputTraj)
54 {
55  if(stateTraj.size() / STATE_SIZE != 1 + inputTraj.size() / INPUT_SIZE)
56  {
57  LOG_ERROR("Invalid state/input sizes, respectively " << stateTraj.size() << " and " << inputTraj.size());
58  }
59  stateTraj_ = stateTraj;
60  inputTraj_ = inputTraj;
61 }
62 
63 void Preview::integrate(Pendulum & pendulum, double dt)
64 {
65  if(playbackStep_ < NB_STEPS)
66  {
67  integratePlayback(pendulum, dt);
68  }
69  else // (playbackStep_ >= NB_STEPS)
70  {
71  integratePostPlayback(pendulum, dt);
72  }
73 }
74 
75 void Preview::integratePlayback(Pendulum & pendulum, double dt)
76 {
77  Eigen::Vector3d comddd;
78  comddd.head<INPUT_SIZE>() = inputTraj_.segment<INPUT_SIZE>(INPUT_SIZE * playbackStep_);
79  comddd.z() = 0.;
80  playbackTime_ += dt;
81  if(playbackTime_ >= (playbackStep_ + 1) * SAMPLING_PERIOD)
82  {
83  playbackStep_++;
84  }
85  pendulum.integrateCoMJerk(comddd, dt);
86 }
87 
88 void Preview::integratePostPlayback(Pendulum & pendulum, double dt)
89 {
90  Eigen::Vector3d comddd;
91  Eigen::VectorXd lastState = stateTraj_.segment<STATE_SIZE>(STATE_SIZE * NB_STEPS);
92  Eigen::Vector2d comd_f = lastState.segment<2>(2);
93  Eigen::Vector2d comdd_f = lastState.segment<2>(4);
94  if(std::abs(comd_f.x() * comdd_f.y() - comd_f.y() * comdd_f.x()) > 1e-4)
95  {
96  LOG_WARNING("MPC terminal condition is not properly fulfilled");
97  }
98  double omega_f = -comd_f.dot(comdd_f) / comd_f.dot(comd_f);
99  double lambda_f = std::pow(omega_f, 2);
100  comddd = -omega_f * pendulum.comdd() - lambda_f * pendulum.comd();
101  comddd.z() = 0.;
102  pendulum.integrateCoMJerk(comddd, dt);
103 }
104 
105 } // namespace lipm_walking
static constexpr unsigned INPUT_SIZE
Input is the 2D horizontal CoM jerk.
const Eigen::Vector3d & comdd() const
CoM acceleration in the world frame.
Definition: Pendulum.h:128
void integratePostPlayback(Pendulum &state, double dt)
Post-playback integration of state reference.
Definition: Preview.cpp:88
State of the inverted pendulum model.
Definition: Pendulum.h:40
const Eigen::Vector3d & comd() const
CoM velocity in the world frame.
Definition: Pendulum.h:120
static constexpr unsigned NB_STEPS
Number of sampling steps.
static EIGEN_MAKE_ALIGNED_OPERATOR_NEW constexpr double SAMPLING_PERIOD
Duration of each sampling step in [s].
void integrateCoMJerk(const Eigen::Vector3d &comddd, double dt)
Integrate constant CoM jerk for a given duration.
Definition: Pendulum.cpp:71
static constexpr unsigned STATE_SIZE
State is the 6D stacked vector of CoM positions, velocities and accelerations.
void integratePlayback(Pendulum &state, double dt)
Playback integration of state reference.
Definition: Preview.cpp:75
Main controller namespace.
Definition: build.dox:1
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Preview()
Initialize with zero state and input trajectories.
Definition: Preview.cpp:47
void integrate(Pendulum &state, double dt)
Integrate playback on reference.
Definition: Preview.cpp:63