sot-talos-balance  1.5.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 
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  dg::Vector copLocal(3);
120  copLocal[0] = px;
121  copLocal[1] = py;
122  copLocal[2] = pz;
123 
124  dg::Vector 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 
138 
139  const dynamicgraph::Vector & wrenchLeft = m_wrenchLeftSIN(iter);
140  const MatrixHomogeneous & poseLeft = m_poseLeftSIN(iter);
141 
142  assert(wrenchLeft.size()==6 && "Unexpected size of signal wrenchLeft");
143 
144  s = computeCoP(wrenchLeft,poseLeft);
145 
147 
148  return s;
149  }
150 
152  {
153  if(!m_initSucceeded)
154  {
155  SEND_WARNING_STREAM_MSG("Cannot compute signal copRight before initialization!");
156  return s;
157  }
158 
160 
161  const dynamicgraph::Vector & wrenchRight = m_wrenchRightSIN(iter);
162  const MatrixHomogeneous & poseRight = m_poseRightSIN(iter);
163 
164  assert(wrenchRight.size()==6 && "Unexpected size of signal wrenchRight");
165 
166  s = computeCoP(wrenchRight,poseRight);
167 
169 
170  return s;
171  }
172 
174  {
175  if(!m_initSucceeded)
176  {
178  SEND_WARNING_STREAM_MSG("Cannot compute signal zmp before initialization!");
179  return s;
180  }
181 
182  getProfiler().start(PROFILE_SIMPLEZMPESTIMATOR_ZMP_COMPUTATION);
183 
184  const dynamicgraph::Vector & wrenchLeft = m_wrenchLeftSIN(iter);
185  const dynamicgraph::Vector & copLeft = m_copLeftSOUT(iter);
186 
187  const dynamicgraph::Vector & wrenchRight = m_wrenchRightSIN(iter);
188  const dynamicgraph::Vector & copRight = m_copRightSOUT(iter);
189 
190  assert(wrenchLeft.size()==6 && "Unexpected size of signal wrenchLeft");
191  assert(wrenchRight.size()==6 && "Unexpected size of signal wrenchRight");
192 
193  const double fzLeft = wrenchLeft[2];
194  const double fzRight = wrenchRight[2];
195 
196  const double e2 = m_eps/2;
197  const double fz = fzLeft + fzRight;
198  if(fzLeft >= e2 && fzRight < e2)
199  {
201  s = copLeft;
202  }
203  else if(fzLeft < e2 && fzRight >= e2)
204  {
206  s = copRight;
207  }
208  else if(fz >= m_eps)
209  {
211  s = ( copLeft*fzLeft + copRight*fzRight ) / fz;
212  }
213  else
214  {
216  SEND_WARNING_STREAM_MSG("Foot forces on the z-axis are both zero!");
217  s.setZero(3);
218  }
219 
220  getProfiler().stop(PROFILE_SIMPLEZMPESTIMATOR_ZMP_COMPUTATION);
221 
222  return s;
223  }
224 
225  DEFINE_SIGNAL_OUT_FUNCTION(emergencyStop, bool)
226  {
227  const dynamicgraph::Vector & zmp = m_zmpSOUT(iter); // dummy to trigger zmp computation
228  (void) zmp; // disable unused variable warning
230  return s;
231  }
232 
233  /* --- COMMANDS ---------------------------------------------------------- */
234 
235  /* ------------------------------------------------------------------- */
236  /* --- ENTITY -------------------------------------------------------- */
237  /* ------------------------------------------------------------------- */
238 
239  void SimpleZmpEstimator::display(std::ostream& os) const
240  {
241  os << "SimpleZmpEstimator " << getName();
242  try
243  {
244  getProfiler().report_all(3, os);
245  }
246  catch (ExceptionSignal e) {}
247  }
248  } // namespace talos_balance
249  } // namespace sot
250 } // namespace dynamicgraph
251 
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
dynamicgraph::Vector computeCoP(const dynamicgraph::Vector &wrench, const MatrixHomogeneous &pose) const
#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