Controller.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_control/api.h>
31 #include <mc_control/fsm/Controller.h>
32 #include <mc_control/mc_controller.h>
33 #include <mc_planning/Pendulum.h>
34 #include <mc_rtc/logging.h>
35 #include <mc_tasks/SurfaceTransformTask.h>
36 #include <mc_tasks/lipm_stabilizer/StabilizerTask.h>
37 
38 #include <lipm_walking/Contact.h>
43 #include <lipm_walking/Sole.h>
45 
49 namespace lipm_walking
50 {
51 
56 
60 struct MC_CONTROL_DLLAPI Controller : public mc_control::fsm::Controller
61 {
62  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
63 
73  Controller(mc_rbdyn::RobotModulePtr robot,
74  double dt,
75  const mc_rtc::Configuration & config,
76  mc_control::ControllerParameters params = {});
77 
83  void reset(const mc_control::ControllerResetData & data) override;
84 
90  void addGUIElements(std::shared_ptr<mc_rtc::gui::StateBuilder> gui);
91 
97  void addGUIMarkers(std::shared_ptr<mc_rtc::gui::StateBuilder> gui);
98 
104  void addLogEntries(mc_rtc::Logger & logger);
105 
111  void leftFootRatio(double ratio);
112 
118  void loadFootstepPlan(std::string name);
119 
120  void updatePlan(const std::string & name);
121 
127  void pauseWalkingCallback(bool verbose = false);
128 
132  virtual bool run() override;
133 
139  void startLogSegment(const std::string & label);
140 
144  void stopLogSegment();
145 
149  bool updatePreview();
150 
154  void warnIfRobotIsInTheAir();
155 
159  mc_rbdyn::Robot & controlRobot()
160  {
161  return mc_control::fsm::Controller::robot();
162  }
163 
168  {
169  double duration;
170  if(doubleSupportDurationOverride_ > 0.)
171  {
172  duration = doubleSupportDurationOverride_;
173  doubleSupportDurationOverride_ = -1.;
174  }
175  else
176  {
177  duration = plan.doubleSupportDuration();
178  }
179  return duration;
180  }
181 
185  bool isLastDSP()
186  {
187  return (supportContact().id > targetContact().id);
188  }
189 
193  bool isLastSSP()
194  {
195  return (targetContact().id > nextContact().id);
196  }
197 
201  double leftFootRatio()
202  {
203  return leftFootRatio_;
204  }
205 
210  {
211  double leftFootPressure = realRobot().forceSensor("LeftFootForceSensor").force().z();
212  double rightFootPressure = realRobot().forceSensor("RightFootForceSensor").force().z();
213  leftFootPressure = std::max(0., leftFootPressure);
214  rightFootPressure = std::max(0., rightFootPressure);
215  return leftFootPressure / (leftFootPressure + rightFootPressure);
216  }
217 
222  {
223  return mpc_;
224  }
225 
229  const Contact & nextContact() const
230  {
231  return plan.nextContact();
232  }
233 
239  void nextDoubleSupportDuration(double duration)
240  {
241  doubleSupportDurationOverride_ = duration;
242  }
243 
248  {
249  return pendulum_;
250  }
251 
255  const Contact & prevContact() const
256  {
257  return plan.prevContact();
258  }
259 
264  {
265  return plan.singleSupportDuration();
266  }
267 
271  const Sole & sole() const
272  {
273  return sole_;
274  }
275 
279  std::shared_ptr<mc_tasks::lipm_stabilizer::StabilizerTask> stabilizer()
280  {
281  return stabilizer_;
282  }
283 
284  /* Set contacts to the stabilizer and QP */
285  void setContacts(const std::vector<std::pair<mc_tasks::lipm_stabilizer::ContactState, sva::PTransformd>> & contacts,
286  bool fullDoF = false);
287 
292  {
293  return plan.supportContact();
294  }
295 
300  {
301  return plan.targetContact();
302  }
303 
305  bool isInOpenLoopTicker() const;
306 
307 public: /* visible to FSM states */
313  bool emergencyStop = false;
314  bool startWalking = false;
315  bool pauseWalking = false;
316  bool pauseWalkingRequested = false;
317  std::shared_ptr<Preview> preview;
318  std::vector<std::vector<double>>
321  std::shared_ptr<mc_tasks::SurfaceTransformTask> swingFootTaskLeft_;
322  std::shared_ptr<mc_tasks::SurfaceTransformTask> swingFootTaskRight_;
323  bool isWalking = false;
324  unsigned nbMPCFailures_ = 0;
326 private: /* hidden from FSM states */
327  std::shared_ptr<mc_tasks::lipm_stabilizer::StabilizerTask> stabilizer_;
328  mc_rbdyn::lipm_stabilizer::StabilizerConfiguration
329  defaultStabilizerConfig_;
331  mc_rtc::Configuration mpcConfig_;
333  pendulum_;
334  Sole sole_;
335  double ctlTime_ = 0.;
336  double doubleSupportDurationOverride_ = -1.; // [s]
337  double leftFootRatio_ = 0.5;
338  std::string segmentName_ = "";
339  unsigned nbLogSegments_ = 100;
340  std::string observerPipelineName_ = "LIPMWalkingObserverPipeline";
342 };
343 
344 } // namespace lipm_walking
mc_planning::Pendulum Pendulum
Definition: Preview.cpp:37
constexpr double PREVIEW_UPDATE_PERIOD
Definition: Controller.h:55
const Contact & prevContact() const
Definition: Controller.h:255
ModelPredictiveControl & mpc()
Definition: Controller.h:221
void nextDoubleSupportDuration(double duration)
Definition: Controller.h:239
mc_rbdyn::Robot & controlRobot()
Definition: Controller.h:159
double doubleSupportDuration()
Definition: Controller.h:167
double measuredLeftFootRatio()
Definition: Controller.h:209
const Contact & targetContact()
Definition: Controller.h:299
std::shared_ptr< mc_tasks::lipm_stabilizer::StabilizerTask > stabilizer()
Definition: Controller.h:279
const Contact & nextContact() const
Definition: Controller.h:229
const Contact & supportContact()
Definition: Controller.h:291
std::vector< std::vector< double > > halfSitPose
Definition: Controller.h:319
std::shared_ptr< mc_tasks::SurfaceTransformTask > swingFootTaskLeft_
Definition: Controller.h:321
const Sole & sole() const
Definition: Controller.h:271
std::shared_ptr< Preview > preview
Definition: Controller.h:317
double singleSupportDuration()
Definition: Controller.h:263
void addGUIMarkers(std::shared_ptr< mc_rtc::gui::StateBuilder > gui)
mc_planning::Pendulum & pendulum()
Definition: Controller.h:247
PlanInterpolator planInterpolator
Definition: Controller.h:310
std::shared_ptr< mc_tasks::SurfaceTransformTask > swingFootTaskRight_
Definition: Controller.h:322
ExternalPlanner externalFootstepPlanner
Handle requesting/receiving plans from an external planner.
Definition: Controller.h:312
Handle requesting/receiving a footstep plan from an external planner.
static constexpr EIGEN_MAKE_ALIGNED_OPERATOR_NEW double SAMPLING_PERIOD