Initial.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 "Initial.h"
29 
30 namespace lipm_walking
31 {
32 
34 {
35  auto & ctl = controller();
36  ctl.walkingState = WalkingState::Standby;
37 
38  mc_rtc::log::info("initial config: {}", config_.dump(true, true));
39  config_("resetFloatingBaseToPlan", resetFloatingBaseToPlan_);
40  config_("updateContactFramesToCurrentSurface", updateContactFramesToCurrentSurface_);
41  config_("resetPosture", resetPosture_);
42  config_("resetPendulumHeight", resetPendulumHeight_);
43  postureTaskIsActive_ = true;
44  postureTaskWasActive_ = true;
45  startStandingButton_ = false;
46  if(auto autoplayState = config_.find("autoplay"))
47  {
48  startStanding_ = *autoplayState;
49  }
50  else
51  {
52  startStanding_ = ctl.config()("autoplay", false);
53  }
54  mc_rtc::log::info("Start standing is {}", startStanding_);
55 
56  internalReset();
57 
58  logger().addLogEntry("walking_phase", []() { return -2.; });
59 
60  if(gui())
61  {
62  gui()->removeElement({"Walking", "Main"}, "Pause walking");
63  }
64 
65  runState(); // don't wait till next cycle to update reference and tasks
66 }
67 
69 {
70  logger().removeLogEntry("walking_phase");
71 
72  if(gui())
73  {
74  hideStartStandingButton();
75  }
76 }
77 
79 {
80  auto & ctl = controller();
81  if(resetPosture_)
82  {
83  postureTaskIsActive_ = (ctl.postureTask->speed().norm() > 1e-2);
84  if(postureTaskIsActive_)
85  {
86  hideStartStandingButton();
87  postureTaskWasActive_ = true;
88  }
89  else if(postureTaskWasActive_)
90  {
91  showStartStandingButton();
92  postureTaskWasActive_ = false;
93  }
94  }
95  else
96  {
97  showStartStandingButton();
98  postureTaskIsActive_ = false;
99  startStanding_ = true;
100  }
101 }
102 
104 {
105  if(startStanding_ && !postureTaskIsActive_)
106  {
107  output("Standing");
108  return true;
109  }
110  return false;
111 }
112 
114 {
115  if(!startStandingButton_ && gui())
116  {
117  using namespace mc_rtc::gui;
118  gui()->addElement({"Walking", "Main"}, Button("Start standing", [this]() { startStanding_ = true; }));
119  startStandingButton_ = true;
120  }
121 }
122 
124 {
125  if(startStandingButton_ && gui())
126  {
127  gui()->removeElement({"Walking", "Main"}, "Start standing");
128  startStandingButton_ = false;
129  }
130 }
131 
133 {
134  auto & ctl = controller();
135  // FIXME:
136  // - resets does not respect the foot plan position when pausing walking (feet come back align
137  // dragging on the floor)
138  // - Do we still need the posture tricks?
139 
140  // (1) update floating-base transforms of both robot mbc's
141  if(resetFloatingBaseToPlan_)
142  {
143  auto X_0_fb = ctl.supportContact().robotTransform(ctl.controlRobot());
144  ctl.controlRobot().posW(X_0_fb);
145  ctl.realRobot().posW(X_0_fb);
146  if(ctl.controlRobot().mb().joint(0).dof() == 6)
147  {
148  ctl.controlRobot().velW(sva::MotionVecd::Zero());
149  ctl.realRobot().velW(sva::MotionVecd::Zero());
150  }
151  }
152 
153  // (2) update contact frames to coincide with surface ones
154  ctl.loadFootstepPlan(ctl.plan.name);
155 
156  // (3) reset solver tasks
157  if(resetPosture_)
158  {
159  ctl.postureTask->posture(ctl.halfSitPose);
160  }
161 
162  // (4) reset controller attributes
163  ctl.leftFootRatio(0.5);
164  ctl.nbMPCFailures_ = 0;
165  ctl.pauseWalking = false;
166  ctl.pauseWalkingRequested = false;
167 
168  if(resetPendulumHeight_)
169  {
170  // XXX Default height is the same as that defined hidden in Stephane's
171  // Pendulum::reset() function. This should probably be ready from config
172  // or use the robot height above ground instead.
173  constexpr double DEFAULT_HEIGHT = 0.8; // [m]
174  double lambda = mc_rtc::constants::GRAVITY / DEFAULT_HEIGHT;
175  ctl.pendulum().reset(lambda, ctl.controlRobot().com());
176  }
177 
178  ctl.stopLogSegment();
179 }
180 
181 } // namespace lipm_walking
182 
183 EXPORT_SINGLE_STATE("LIPMWalking::Initial", lipm_walking::states::Initial)
mc_rtc::Logger & logger()
Definition: State.h:67
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Controller & controller()
Definition: State.h:47
std::shared_ptr< mc_rtc::gui::StateBuilder > gui()
Definition: State.h:57
void teardown() override
Definition: Initial.cpp:68
bool checkTransitions() override
Definition: Initial.cpp:103
void runState() override
Definition: Initial.cpp:78