sot-talos-balance  1.6.0
simple-zmp-estimator.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_SIMPLEZMPESTIMATOR_ZMP_COMPUTATION "SimpleZmpEstimator: zmp computation "
38 #define PROFILE_SIMPLEZMPESTIMATOR_COPLEFT_COMPUTATION "SimpleZmpEstimator: copLeft computation "
39 #define PROFILE_SIMPLEZMPESTIMATOR_COPRIGHT_COMPUTATION "SimpleZmpEstimator: copRight computation "
40 
41 #define INPUT_SIGNALS m_wrenchLeftSIN << m_wrenchRightSIN << m_poseLeftSIN << m_poseRightSIN
42 
43 #define OUTPUT_SIGNALS m_copLeftSOUT << m_copRightSOUT << m_zmpSOUT << m_emergencyStopSOUT
44 
47  typedef SimpleZmpEstimator EntityClassName;
48 
49  /* --- DG FACTORY ---------------------------------------------------- */
50  DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(SimpleZmpEstimator,
51  "SimpleZmpEstimator");
52 
53  /* ------------------------------------------------------------------- */
54  /* --- CONSTRUCTION -------------------------------------------------- */
55  /* ------------------------------------------------------------------- */
56  SimpleZmpEstimator::SimpleZmpEstimator(const std::string & name, const double & eps)
57  : Entity(name)
58  , CONSTRUCT_SIGNAL_IN(wrenchLeft, dynamicgraph::Vector)
59  , CONSTRUCT_SIGNAL_IN(wrenchRight, dynamicgraph::Vector)
60  , CONSTRUCT_SIGNAL_IN(poseLeft, MatrixHomogeneous)
61  , CONSTRUCT_SIGNAL_IN(poseRight, MatrixHomogeneous)
62  , CONSTRUCT_SIGNAL_OUT(copLeft, dynamicgraph::Vector, m_wrenchLeftSIN << m_poseLeftSIN)
63  , CONSTRUCT_SIGNAL_OUT(copRight, dynamicgraph::Vector, m_wrenchRightSIN << m_poseRightSIN)
64  , CONSTRUCT_SIGNAL_OUT(zmp, dynamicgraph::Vector, m_wrenchLeftSIN << m_wrenchRightSIN << m_copLeftSOUT << m_copRightSOUT)
65  , CONSTRUCT_SIGNAL_OUT(emergencyStop, bool, m_zmpSOUT)
66  , m_eps(eps)
67  , m_initSucceeded(false)
68  {
69  Entity::signalRegistration( INPUT_SIGNALS << OUTPUT_SIGNALS );
70 
71  /* Commands. */
72  addCommand("init", makeCommandVoid0(*this, &SimpleZmpEstimator::init, docCommandVoid0("Initialize the entity.")));
73  addCommand("getForceThreshold", makeDirectGetter(*this,&m_eps, docDirectGetter("Get force threshold","double")));
74  addCommand("setForceThreshold", makeDirectSetter(*this,&m_eps, docDirectSetter("Set force threshold","double")));
75  }
76 
78  {
79  if(!m_wrenchLeftSIN.isPlugged())
80  return SEND_MSG("Init failed: signal wrenchLeft is not plugged", MSG_TYPE_ERROR);
81  if(!m_poseLeftSIN.isPlugged())
82  return SEND_MSG("Init failed: signal poseLeft is not plugged", MSG_TYPE_ERROR);
83  if(!m_wrenchRightSIN.isPlugged())
84  return SEND_MSG("Init failed: signal wrenchRight is not plugged", MSG_TYPE_ERROR);
85  if(!m_poseRightSIN.isPlugged())
86  return SEND_MSG("Init failed: signal poseRight is not plugged", MSG_TYPE_ERROR);
87 
88  m_initSucceeded = true;
89  }
90 
91  /* ------------------------------------------------------------------- */
92  /* --- SIGNALS ------------------------------------------------------- */
93  /* ------------------------------------------------------------------- */
94 
95  Eigen::Vector3d
96  SimpleZmpEstimator::computeCoP(const dg::Vector & wrench, const MatrixHomogeneous & pose) const
97  {
98  const double h = pose(2,3);
99 
100  const double fx = wrench[0];
101  const double fy = wrench[1];
102  const double fz = wrench[2];
103  const double tx = wrench[3];
104  const double ty = wrench[4];
105 
106  double px, py;
107  if(fz >= m_eps/2)
108  {
109  px = (- ty - fx*h)/fz;
110  py = ( tx - fy*h)/fz;
111  }
112  else
113  {
114  px = 0.0;
115  py = 0.0;
116  }
117  const double pz = - h;
118 
119  Eigen::Vector3d copLocal;
120  copLocal[0] = px;
121  copLocal[1] = py;
122  copLocal[2] = pz;
123 
124  Eigen::Vector3d cop = pose.linear()*copLocal + pose.translation();
125 
126  return cop;
127  }
128 
130  {
131  if(!m_initSucceeded)
132  {
133  SEND_WARNING_STREAM_MSG("Cannot compute signal copLeft before initialization!");
134  return s;
135  }
136  if(s.size()!=3)
137  s.resize(3);
138 
140 
141  const dynamicgraph::Vector & wrenchLeft = m_wrenchLeftSIN(iter);
142  const MatrixHomogeneous & poseLeft = m_poseLeftSIN(iter);
143 
144  assert(wrenchLeft.size()==6 && "Unexpected size of signal wrenchLeft");
145 
146  s = computeCoP(wrenchLeft,poseLeft);
147 
149 
150  return s;
151  }
152 
154  {
155  if(!m_initSucceeded)
156  {
157  SEND_WARNING_STREAM_MSG("Cannot compute signal copRight before initialization!");
158  return s;
159  }
160  if(s.size()!=3)
161  s.resize(3);
162 
164 
165  const dynamicgraph::Vector & wrenchRight = m_wrenchRightSIN(iter);
166  const MatrixHomogeneous & poseRight = m_poseRightSIN(iter);
167 
168  assert(wrenchRight.size()==6 && "Unexpected size of signal wrenchRight");
169 
170  s = computeCoP(wrenchRight,poseRight);
171 
173 
174  return s;
175  }
176 
178  {
179  if(!m_initSucceeded)
180  {
182  SEND_WARNING_STREAM_MSG("Cannot compute signal zmp before initialization!");
183  return s;
184  }
185  if(s.size()!=3)
186  s.resize(3);
187 
188  getProfiler().start(PROFILE_SIMPLEZMPESTIMATOR_ZMP_COMPUTATION);
189 
190  const dynamicgraph::Vector & wrenchLeft = m_wrenchLeftSIN(iter);
191  const dynamicgraph::Vector & copLeft = m_copLeftSOUT(iter);
192 
193  const dynamicgraph::Vector & wrenchRight = m_wrenchRightSIN(iter);
194  const dynamicgraph::Vector & copRight = m_copRightSOUT(iter);
195 
196  assert(wrenchLeft.size()==6 && "Unexpected size of signal wrenchLeft");
197  assert(wrenchRight.size()==6 && "Unexpected size of signal wrenchRight");
198 
199  const double fzLeft = wrenchLeft[2];
200  const double fzRight = wrenchRight[2];
201 
202  const double e2 = m_eps/2;
203  const double fz = fzLeft + fzRight;
204  if(fzLeft >= e2 && fzRight < e2)
205  {
207  s = copLeft;
208  }
209  else if(fzLeft < e2 && fzRight >= e2)
210  {
212  s = copRight;
213  }
214  else if(fz >= m_eps)
215  {
217  s = ( copLeft*fzLeft + copRight*fzRight ) / fz;
218  }
219  else
220  {
222  SEND_WARNING_STREAM_MSG("Foot forces on the z-axis are both zero!");
223  s.setZero(3);
224  }
225 
226  getProfiler().stop(PROFILE_SIMPLEZMPESTIMATOR_ZMP_COMPUTATION);
227 
228  return s;
229  }
230 
231  DEFINE_SIGNAL_OUT_FUNCTION(emergencyStop, bool)
232  {
233  const dynamicgraph::Vector & zmp = m_zmpSOUT(iter); // dummy to trigger zmp computation
234  (void) zmp; // disable unused variable warning
236  return s;
237  }
238 
239  /* --- COMMANDS ---------------------------------------------------------- */
240 
241  /* ------------------------------------------------------------------- */
242  /* --- ENTITY -------------------------------------------------------- */
243  /* ------------------------------------------------------------------- */
244 
245  void SimpleZmpEstimator::display(std::ostream& os) const
246  {
247  os << "SimpleZmpEstimator " << getName();
248  try
249  {
250  getProfiler().report_all(3, os);
251  }
252  catch (ExceptionSignal e) {}
253  }
254  } // namespace talos_balance
255  } // namespace sot
256 } // namespace dynamicgraph
257 
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SimpleZmpEstimator(const std::string &name, const double &eps=1.0)
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: math/fwd.hh:40
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(AdmittanceControllerEndEffector, "AdmittanceControllerEndEffector")
#define INPUT_SIGNALS
#define PROFILE_SIMPLEZMPESTIMATOR_COPRIGHT_COMPUTATION
#define PROFILE_SIMPLEZMPESTIMATOR_COPLEFT_COMPUTATION
bool m_emergency_stop_triggered
true if the entity has been successfully initialized
#define OUTPUT_SIGNALS
#define PROFILE_SIMPLEZMPESTIMATOR_ZMP_COMPUTATION
Eigen::Vector3d computeCoP(const dynamicgraph::Vector &wrench, const MatrixHomogeneous &pose) const