PlanInterpolator.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_rbdyn/rpy_utils.h>
31 
35 
36 namespace mc_rtc
37 {
38 namespace gui
39 {
40 struct StateBuilder;
41 } // namespace gui
42 } // namespace mc_rtc
43 
44 namespace lipm_walking
45 {
46 
51 {
52  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
53 
55 
56  static constexpr double DEFAULT_EXTRA_STEP_WIDTH = 0.; // [m]
57  static constexpr double IN_PLACE_EXTRA_STEP_WIDTH = 0.02; // [m]
58  static constexpr double IN_PLACE_MAX_STEP_ANGLE = 45.; // [deg]
59  static constexpr double MAX_EXTRA_STEP_WIDTH = 0.05; // [m]
60  static constexpr double MAX_LATERAL_STEP_LENGTH = 0.2; // [m]
61  static constexpr double MAX_SAGITTAL_STEP_LENGTH = 0.4; // [m]
62  static constexpr double MIN_STEP_LENGTH = 0.001; // [m]
63 
72  enum class Gait
73  {
74  Walk,
75  Shuffle,
76  Turn
77  };
78 
84  PlanInterpolator(std::shared_ptr<mc_rtc::gui::StateBuilder> gui) : gui_(gui) {}
85 
89  void addGUIElements();
90 
96  void configure(const mc_rtc::Configuration & config);
97 
101  void removeGUIElements();
102 
106  void run();
107 
111  void suggestGait();
112 
120  void updateSupportPath(const sva::PTransformd & X_0_lf, const sva::PTransformd & X_0_rf);
121 
127  std::vector<std::string> availablePlans() const
128  {
129  return plans_.keys();
130  }
131 
137  const std::string & customPlanName() const
138  {
139  return customPlan_.name;
140  }
141 
147  std::string gait() const noexcept
148  {
149  if(gait_ == Gait::Shuffle)
150  {
151  return "Shuffle";
152  }
153  else if(gait_ == Gait::Turn)
154  {
155  return "Turn";
156  }
157  else // (gait_ == Gait::Walk)
158  {
159  return "Walk";
160  }
161  }
162 
168  void gait(const std::string & dir)
169  {
170  if(dir == "Shuffle")
171  {
172  extraStepWidth_ = DEFAULT_EXTRA_STEP_WIDTH;
173  gait_ = Gait::Shuffle;
174  }
175  else if(dir == "Turn")
176  {
177  extraStepWidth_ = IN_PLACE_EXTRA_STEP_WIDTH;
178  gait_ = Gait::Turn;
179  targetPose_.x = 0.;
180  targetPose_.y = 0.;
181  }
182  else // (dir == "Walk")
183  {
184  extraStepWidth_ = DEFAULT_EXTRA_STEP_WIDTH;
185  gait_ = Gait::Walk;
186  }
187  run();
188  }
189 
195  FootstepPlan getPlan(std::string name)
196  {
197  // Always compute custom plans
198  // This will generate a plan based on targetPose_
199  if(name.rfind("custom_", 0) == 0)
200  {
201  run();
202  return customPlan_;
203  }
204 
205  if(name == customPlan_.name)
206  {
207  return customPlan_;
208  }
209  else // pre-defined plans
210  {
211  return plans_(name);
212  }
213  }
214 
219  {
220  gait_ = Gait::Walk;
221  targetPose_ = lastBackwardTarget_;
222  run();
223  }
224 
229  {
230  gait_ = Gait::Walk;
231  targetPose_ = lastForwardTarget_;
232  run();
233  }
234 
239  {
240  gait_ = Gait::Shuffle;
241  targetPose_ = lastLateralTarget_;
242  run();
243  }
244 
250  double stepWidth() const
251  {
252  return stepWidth_;
253  }
254 
264  void stepWidth(double stepWidth)
265  {
266  stepWidth_ = stepWidth;
267  }
268 
272  const sva::PTransformd & worldReference() const
273  {
274  return worldReference_;
275  }
276 
282  void worldReference(const sva::PTransformd & worldReference)
283  {
284  worldReference_ = worldReference;
285  }
286 
295  {
296  if(hasRun_)
297  {
298  hasRun_ = false;
299  return true;
300  }
301  return false;
302  }
303 
310  void updateLocalTarget_(const SE2d & target);
311 
318  void updateWorldTarget_(const Eigen::Vector3d & desired);
319 
320 private:
324  void restoreDefaults();
325 
331  void runShuffling_();
332 
338  void runTurning_();
339 
345  void runWalking_();
346 
347 private:
348  FootstepPlan customPlan_;
349  Gait gait_ = Gait::Walk;
351  SE2d initPose_ = {0., 0., 0.};
352  SE2d lastBackwardTarget_ = {-0.5, 0., 0.};
353  SE2d lastForwardTarget_ = {0.5, 0., 0.};
354  SE2d lastLateralTarget_ = {0.0, 0.3, 0.};
355  SE2d targetPose_ = {0.5, 0., 0.};
356  bool startWithRightFootstep_ = true;
357  double desiredStepAngle_ = 10. * M_PI / 180.; // [rad]
358  double desiredStepLength_ = 0.2; // [m]
359  double extraStepWidth_ = 0.; // [m]
360  double outputVel_ = 0.25; // [m] / [s]
361  double stepAngle_ = 0.0; // [deg]
362  double stepLength_ = 0.2;
363  double stepWidth_ = 0.18; // [m], default value is for HRP-4
364  mc_rtc::Configuration plans_;
365  std::shared_ptr<mc_rtc::gui::StateBuilder> gui_;
366  std::vector<Eigen::Vector3d> supportPathDisplay_;
367  sva::PTransformd worldReference_;
368  unsigned nbFootsteps_ = 0;
369  bool hasRun_ = false;
370 };
371 
372 } // namespace lipm_walking
utils::SE2d SE2d
bool checkPlanUpdated()
Checks whether the plan has been recomputed (run() was called)
void configure(const mc_rtc::Configuration &config)
void stepWidth(double stepWidth)
static constexpr double DEFAULT_EXTRA_STEP_WIDTH
void updateLocalTarget_(const SE2d &target)
static constexpr double IN_PLACE_MAX_STEP_ANGLE
void gait(const std::string &dir)
static constexpr double MIN_STEP_LENGTH
std::string gait() const noexcept
const sva::PTransformd & worldReference() const
static constexpr double MAX_SAGITTAL_STEP_LENGTH
FootstepPlan getPlan(std::string name)
std::vector< std::string > availablePlans() const
static constexpr double MAX_LATERAL_STEP_LENGTH
PlanInterpolator(std::shared_ptr< mc_rtc::gui::StateBuilder > gui)
static constexpr double MAX_EXTRA_STEP_WIDTH
static constexpr double IN_PLACE_EXTRA_STEP_WIDTH
void updateSupportPath(const sva::PTransformd &X_0_lf, const sva::PTransformd &X_0_rf)
const std::string & customPlanName() const
void worldReference(const sva::PTransformd &worldReference)
void updateWorldTarget_(const Eigen::Vector3d &desired)
lipm_walking::utils::SE2d SE2d