sot-talos-balance  1.5.0
simple-admittance-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 
38 #define PROFILE_SIMPLE_ADMITTANCECONTROLLER_QREF_COMPUTATION "SimpleAdmittanceController: qRef computation "
39 
40 #define PROFILE_SIMPLE_ADMITTANCECONTROLLER_DQREF_COMPUTATION "SimpleAdmittanceController: dqRef computation "
41 
42 #define INPUT_SIGNALS m_KpSIN << m_stateSIN << m_tauSIN << m_tauDesSIN
43 
44 #define OUTPUT_SIGNALS m_qRefSOUT << m_dqRefSOUT
45 
48  typedef SimpleAdmittanceController EntityClassName;
49 
50  /* --- DG FACTORY ---------------------------------------------------- */
51  DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(SimpleAdmittanceController,
52  "SimpleAdmittanceController");
53 
54  /* ------------------------------------------------------------------- */
55  /* --- CONSTRUCTION -------------------------------------------------- */
56  /* ------------------------------------------------------------------- */
58  : Entity(name)
59  , CONSTRUCT_SIGNAL_IN(Kp, dynamicgraph::Vector)
60  , CONSTRUCT_SIGNAL_IN(state, dynamicgraph::Vector)
61  , CONSTRUCT_SIGNAL_IN(tau, dynamicgraph::Vector)
62  , CONSTRUCT_SIGNAL_IN(tauDes, dynamicgraph::Vector)
63  , CONSTRUCT_SIGNAL_OUT(dqRef, dynamicgraph::Vector, INPUT_SIGNALS)
64  , CONSTRUCT_SIGNAL_OUT(qRef, dynamicgraph::Vector, m_dqRefSOUT)
65  , m_initSucceeded(false)
66  {
67  Entity::signalRegistration( INPUT_SIGNALS << OUTPUT_SIGNALS );
68 
69  /* Commands. */
70  addCommand("init", makeCommandVoid2(*this, &SimpleAdmittanceController::init, docCommandVoid2("Initialize the entity.","time step","Number of elements")));
71  addCommand("setPosition", makeCommandVoid1(*this, &SimpleAdmittanceController::setPosition, docCommandVoid1("Set initial reference position.","Initial position")));
72  addCommand("useExternalState", makeDirectSetter(*this,&m_useState, docDirectSetter("use external state","bool")));
73  addCommand("isUsingExternalState", makeDirectGetter(*this,&m_useState, docDirectGetter("use external state","bool")));
74  }
75 
76  void SimpleAdmittanceController::init(const double & dt, const unsigned & n)
77  {
78  if(n<1)
79  return SEND_MSG("n must be at least 1", MSG_TYPE_ERROR);
80  if(!m_KpSIN.isPlugged())
81  return SEND_MSG("Init failed: signal Kp is not plugged", MSG_TYPE_ERROR);
82  if(!m_tauSIN.isPlugged())
83  return SEND_MSG("Init failed: signal tau is not plugged", MSG_TYPE_ERROR);
84  if(!m_tauDesSIN.isPlugged())
85  return SEND_MSG("Init failed: signal tauDes is not plugged", MSG_TYPE_ERROR);
86 
87  m_n = n;
88  m_dt = dt;
89  m_q.setZero(n);
90  m_initSucceeded = true;
91  }
92 
94 
95  {
96  m_q = position;
97  }
98 
99  /* ------------------------------------------------------------------- */
100  /* --- SIGNALS ------------------------------------------------------- */
101  /* ------------------------------------------------------------------- */
102 
104  {
105  if(!m_initSucceeded)
106  {
107  SEND_WARNING_STREAM_MSG("Cannot compute signal dqRef before initialization!");
108  return s;
109  }
110 
112 
113  const Vector & tauDes = m_tauDesSIN(iter);
114  const Vector & tau = m_tauSIN(iter);
115  const Vector & Kp = m_KpSIN(iter);
116 
117  assert(tau.size()==m_n && "Unexpected size of signal tau");
118  assert(tauDes.size()==m_n && "Unexpected size of signal tauDes");
119  assert(Kp.size()==m_n && "Unexpected size of signal Kp");
120 
121  const Vector & dqRef = Kp.cwiseProduct(tauDes-tau);
122 
123  s = dqRef;
124 
126 
127  return s;
128  }
129 
131  {
132  if(!m_initSucceeded)
133  {
134  SEND_WARNING_STREAM_MSG("Cannot compute signal qRef before initialization!");
135  return s;
136  }
137 
139 
140  const Vector & dqRef = m_dqRefSOUT(iter);
141 
142  assert(dqRef.size()==m_n && "Unexpected size of signal dqRef");
143 
144  if(m_useState)
145  {
146  if(!m_stateSIN.isPlugged())
147  {
148  SEND_MSG("Signal state is requested, but is not plugged", MSG_TYPE_ERROR);
149  return s;
150  }
151  const Vector & state = m_stateSIN(iter);
152  assert(state.size()==m_n && "Unexpected size of signal state");
153  m_q = state;
154  }
155 
156  m_q += dqRef*m_dt;
157 
158  s = m_q;
159 
161 
162  return s;
163  }
164 
165 
166  /* --- COMMANDS ---------------------------------------------------------- */
167 
168  /* ------------------------------------------------------------------- */
169  /* --- ENTITY -------------------------------------------------------- */
170  /* ------------------------------------------------------------------- */
171 
172  void SimpleAdmittanceController::display(std::ostream& os) const
173  {
174  os << "SimpleAdmittanceController " << getName();
175  try
176  {
177  getProfiler().report_all(3, os);
178  }
179  catch (ExceptionSignal e) {}
180  }
181  } // namespace talos_balance
182  } // namespace sot
183 } // namespace dynamicgraph
184 
#define PROFILE_SIMPLE_ADMITTANCECONTROLLER_DQREF_COMPUTATION
#define PROFILE_SIMPLE_ADMITTANCECONTROLLER_QREF_COMPUTATION
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: math/fwd.hh:40
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(AdmittanceControllerEndEffector, "AdmittanceControllerEndEffector")
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SimpleAdmittanceController(const std::string &name)
#define INPUT_SIGNALS
#define OUTPUT_SIGNALS