sot-talos-balance  1.7.0
dcm-com-controller.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018, Gepetto team, LAAS-CNRS
3  *
4  * This file is part of sot-talos-balance.
5  * sot-talos-balance is free software: you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation, either version 3 of
8  * the License, or (at your option) any later version.
9  * sot-talos-balance is distributed in the hope that it will be
10  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
11  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details. You should
13  * have received a copy of the GNU Lesser General Public License along
14  * with sot-talos-balance. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
18 
19 #include <sot/core/debug.hh>
20 #include <dynamic-graph/factory.h>
21 #include <dynamic-graph/command-bind.h>
22 
23 #include <dynamic-graph/all-commands.h>
24 #include <sot/core/stop-watch.hh>
25 
26 namespace dynamicgraph
27 {
28  namespace sot
29  {
30  namespace talos_balance
31  {
32  namespace dg = ::dynamicgraph;
33  using namespace dg;
34  using namespace dg::command;
35 
36 //Size to be aligned "-------------------------------------------------------"
37 #define PROFILE_DCMCOMCONTROLLER_DDCOMREF_COMPUTATION "DcmComController: ddcomRef computation "
38 #define PROFILE_DCMCOMCONTROLLER_ZMPREF_COMPUTATION "DcmComController: zmpRef computation "
39 #define PROFILE_DCMCOMCONTROLLER_WRENCHREF_COMPUTATION "DcmComController: wrenchRef computation "
40 
41 #define INPUT_SIGNALS m_KpSIN << m_KiSIN << m_decayFactorSIN << m_omegaSIN << m_massSIN << m_dcmSIN << m_dcmDesSIN << m_comDesSIN << m_ddcomDesSIN
42 
43 #define OUTPUT_SIGNALS m_ddcomRefSOUT << m_zmpRefSOUT << m_wrenchRefSOUT
44 
47  typedef DcmComController EntityClassName;
48 
49  /* --- DG FACTORY ---------------------------------------------------- */
50  DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(DcmComController,
51  "DcmComController");
52 
53  /* ------------------------------------------------------------------- */
54  /* --- CONSTRUCTION -------------------------------------------------- */
55  /* ------------------------------------------------------------------- */
56  DcmComController::DcmComController(const std::string& name)
57  : Entity(name)
58  , CONSTRUCT_SIGNAL_IN(Kp, dynamicgraph::Vector)
59  , CONSTRUCT_SIGNAL_IN(Ki, dynamicgraph::Vector)
60  , CONSTRUCT_SIGNAL_IN(decayFactor, double)
61  , CONSTRUCT_SIGNAL_IN(omega, double)
62  , CONSTRUCT_SIGNAL_IN(mass, double)
63  , CONSTRUCT_SIGNAL_IN(dcm, dynamicgraph::Vector)
64  , CONSTRUCT_SIGNAL_IN(dcmDes, dynamicgraph::Vector)
65  , CONSTRUCT_SIGNAL_IN(comDes, dynamicgraph::Vector)
66  , CONSTRUCT_SIGNAL_IN(ddcomDes, dynamicgraph::Vector)
67  , CONSTRUCT_SIGNAL_OUT(ddcomRef, dynamicgraph::Vector, INPUT_SIGNALS)
68  , CONSTRUCT_SIGNAL_OUT(zmpRef, dynamicgraph::Vector, m_ddcomRefSOUT << m_comDesSIN << m_omegaSIN )
69  , CONSTRUCT_SIGNAL_OUT(wrenchRef, dynamicgraph::Vector, m_ddcomRefSOUT << m_comDesSIN << m_massSIN )
70  , m_initSucceeded(false)
71  {
72  Entity::signalRegistration( INPUT_SIGNALS << OUTPUT_SIGNALS );
73 
74  /* Commands. */
75  addCommand("init", makeCommandVoid1(*this, &DcmComController::init, docCommandVoid1("Initialize the entity.","time step")));
76  addCommand("resetDcmIntegralError", makeCommandVoid0(*this, &DcmComController::resetDcmIntegralError, docCommandVoid0("Set dcm integral error to zero.")));
77  }
78 
79  void DcmComController::init(const double & dt)
80  {
81  if(!m_KpSIN.isPlugged())
82  return SEND_MSG("Init failed: signal Kp is not plugged", MSG_TYPE_ERROR);
83  if(!m_KiSIN.isPlugged())
84  return SEND_MSG("Init failed: signal Ki is not plugged", MSG_TYPE_ERROR);
85  if(!m_decayFactorSIN.isPlugged())
86  return SEND_MSG("Init failed: signal decayFactor is not plugged", MSG_TYPE_ERROR);
87  if(!m_omegaSIN.isPlugged())
88  return SEND_MSG("Init failed: signal omega is not plugged", MSG_TYPE_ERROR);
89  if(!m_massSIN.isPlugged())
90  return SEND_MSG("Init failed: signal mass is not plugged", MSG_TYPE_ERROR);
91  if(!m_dcmSIN.isPlugged())
92  return SEND_MSG("Init failed: signal dcm is not plugged", MSG_TYPE_ERROR);
93  if(!m_dcmDesSIN.isPlugged())
94  return SEND_MSG("Init failed: signal dcmDes is not plugged", MSG_TYPE_ERROR);
95  if(!m_comDesSIN.isPlugged())
96  return SEND_MSG("Init failed: signal comDes is not plugged", MSG_TYPE_ERROR);
97  if(!m_ddcomDesSIN.isPlugged())
98  return SEND_MSG("Init failed: signal ddcomDes is not plugged", MSG_TYPE_ERROR);
99 
100  m_dt = dt;
102  m_initSucceeded = true;
103  }
104 
106  {
107  m_dcmIntegralError.setZero(3);
108  }
109 
110  /* ------------------------------------------------------------------- */
111  /* --- SIGNALS ------------------------------------------------------- */
112  /* ------------------------------------------------------------------- */
113 
115  {
116  if(!m_initSucceeded)
117  {
118  SEND_WARNING_STREAM_MSG("Cannot compute signal ddcomRef before initialization!");
119  return s;
120  }
121  if(s.size()!=3)
122  s.resize(3);
123 
125 
126 
127  const Vector & Kp = m_KpSIN(iter);
128  const Vector & Ki = m_KiSIN(iter);
129  const double & decayFactor = m_decayFactorSIN(iter);
130  const double & omega = m_omegaSIN(iter);
131  const Vector & dcm = m_dcmSIN(iter);
132  const Vector & dcmDes = m_dcmDesSIN(iter);
133  const Vector & ddcomDes = m_ddcomDesSIN(iter);
134 
135  assert(Kp.size()==3 && "Unexpected size of signal Kp");
136  assert(Ki.size()==3 && "Unexpected size of signal Ki");
137  assert(dcm.size()==3 && "Unexpected size of signal dcm");
138  assert(dcmDes.size()==3 && "Unexpected size of signal dcmDes");
139  assert(ddcomDes.size()==3 && "Unexpected size of signal ddcomDes");
140 
141  const Eigen::Vector3d dcmError = dcmDes - dcm;
142 
143  const Eigen::Vector3d ddcomRef = ddcomDes + omega * Kp.cwiseProduct(dcmError) + omega * Ki.cwiseProduct(m_dcmIntegralError);
144 
145  // update the integrator (AFTER using its value)
146  m_dcmIntegralError += ( dcmError - decayFactor*m_dcmIntegralError ) * m_dt;
147 
148  s = ddcomRef;
149 
151 
152  return s;
153  }
154 
156  {
157  if(!m_initSucceeded)
158  {
159  SEND_WARNING_STREAM_MSG("Cannot compute signal zmpRef before initialization!");
160  return s;
161  }
162  if(s.size()!=3)
163  s.resize(3);
164 
165  getProfiler().start(PROFILE_DCMCOMCONTROLLER_ZMPREF_COMPUTATION);
166 
167  const double & omega = m_omegaSIN(iter);
168  const Vector & comDes = m_comDesSIN(iter);
169 
170  const Vector & ddcomRef = m_ddcomRefSOUT(iter);
171 
172  assert(comDes.size()==3 && "Unexpected size of signal comDes");
173 
174  Eigen::Vector3d zmpRef = comDes - ddcomRef/(omega*omega);
175  zmpRef[2] = 0.0; // maybe needs better way
176 
177  s = zmpRef;
178 
180 
181  return s;
182  }
183 
185  {
186  if(!m_initSucceeded)
187  {
188  SEND_WARNING_STREAM_MSG("Cannot compute signal wrenchRef before initialization!");
189  return s;
190  }
191  if(s.size()!=6)
192  s.resize(6);
193 
195 
196  const double & mass = m_massSIN(iter);
197  const Vector & comDes = m_comDesSIN(iter);
198 
199  const Vector & ddcomRef = m_ddcomRefSOUT(iter);
200 
201  assert(comDes.size()==3 && "Unexpected size of signal comDes");
202 
203  Eigen::Vector3d gravity;
204  gravity << 0.0, 0.0, -9.81;
205 
206  const Eigen::Vector3d forceRef = mass * ( ddcomRef - gravity );
207 
208  Eigen::Matrix<double,6,1> wrenchRef;
209  wrenchRef.head<3>() = forceRef;
210  const Eigen::Vector3d comDes3 = comDes;
211  wrenchRef.tail<3>() = comDes3.cross(wrenchRef.head<3>());
212 
213  s = wrenchRef;
214 
216 
217  return s;
218  }
219 
220  /* --- COMMANDS ---------------------------------------------------------- */
221 
222  /* ------------------------------------------------------------------- */
223  /* --- ENTITY -------------------------------------------------------- */
224  /* ------------------------------------------------------------------- */
225 
226  void DcmComController::display(std::ostream& os) const
227  {
228  os << "DcmComController " << getName();
229  try
230  {
231  getProfiler().report_all(3, os);
232  }
233  catch (ExceptionSignal e) {}
234  }
235  } // namespace talos_balance
236  } // namespace sot
237 } // namespace dynamicgraph
238 
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: math/fwd.hh:40
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(AdmittanceControllerEndEffector, "AdmittanceControllerEndEffector")
dynamicgraph::Vector m_dcmIntegralError
true if the entity has been successfully initialized
EIGEN_MAKE_ALIGNED_OPERATOR_NEW DcmComController(const std::string &name)
#define PROFILE_DCMCOMCONTROLLER_ZMPREF_COMPUTATION
#define OUTPUT_SIGNALS
#define PROFILE_DCMCOMCONTROLLER_WRENCHREF_COMPUTATION
#define INPUT_SIGNALS
#define PROFILE_DCMCOMCONTROLLER_DDCOMREF_COMPUTATION