sot-talos-balance  1.6.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_useState(false)
66  , m_initSucceeded(false)
67  {
68  Entity::signalRegistration( INPUT_SIGNALS << OUTPUT_SIGNALS );
69 
70  /* Commands. */
71  addCommand("init", makeCommandVoid2(*this, &SimpleAdmittanceController::init, docCommandVoid2("Initialize the entity.","time step","Number of elements")));
72  addCommand("setPosition", makeCommandVoid1(*this, &SimpleAdmittanceController::setPosition, docCommandVoid1("Set initial reference position.","Initial position")));
73  addCommand("useExternalState", makeDirectSetter(*this,&m_useState, docDirectSetter("use external state","bool")));
74  addCommand("isUsingExternalState", makeDirectGetter(*this,&m_useState, docDirectGetter("use external state","bool")));
75  }
76 
77  void SimpleAdmittanceController::init(const double & dt, const unsigned & n)
78  {
79  if(n<1)
80  return SEND_MSG("n must be at least 1", MSG_TYPE_ERROR);
81  if(!m_KpSIN.isPlugged())
82  return SEND_MSG("Init failed: signal Kp is not plugged", MSG_TYPE_ERROR);
83  if(!m_tauSIN.isPlugged())
84  return SEND_MSG("Init failed: signal tau is not plugged", MSG_TYPE_ERROR);
85  if(!m_tauDesSIN.isPlugged())
86  return SEND_MSG("Init failed: signal tauDes is not plugged", MSG_TYPE_ERROR);
87 
88  m_n = n;
89  m_dt = dt;
90  m_q.setZero(n);
91  m_initSucceeded = true;
92  }
93 
95 
96  {
97  m_q = position;
98  }
99 
100  /* ------------------------------------------------------------------- */
101  /* --- SIGNALS ------------------------------------------------------- */
102  /* ------------------------------------------------------------------- */
103 
105  {
106  if(!m_initSucceeded)
107  {
108  SEND_WARNING_STREAM_MSG("Cannot compute signal dqRef before initialization!");
109  return s;
110  }
111  if(s.size()!=m_n)
112  s.resize(m_n);
113 
115 
116  const Vector & tauDes = m_tauDesSIN(iter);
117  const Vector & tau = m_tauSIN(iter);
118  const Vector & Kp = m_KpSIN(iter);
119 
120  assert(tau.size()==m_n && "Unexpected size of signal tau");
121  assert(tauDes.size()==m_n && "Unexpected size of signal tauDes");
122  assert(Kp.size()==m_n && "Unexpected size of signal Kp");
123 
124  s = Kp.cwiseProduct(tauDes-tau);
125 
127 
128  return s;
129  }
130 
132  {
133  if(!m_initSucceeded)
134  {
135  SEND_WARNING_STREAM_MSG("Cannot compute signal qRef before initialization!");
136  return s;
137  }
138  if(s.size()!=m_n)
139  s.resize(m_n);
140 
142 
143  const Vector & dqRef = m_dqRefSOUT(iter);
144 
145  assert(dqRef.size()==m_n && "Unexpected size of signal dqRef");
146 
147  if(m_useState)
148  {
149  if(!m_stateSIN.isPlugged())
150  {
151  SEND_MSG("Signal state is requested, but is not plugged", MSG_TYPE_ERROR);
152  return s;
153  }
154  const Vector & state = m_stateSIN(iter);
155  assert(state.size()==m_n && "Unexpected size of signal state");
156  m_q = state;
157  }
158 
159  m_q += dqRef*m_dt;
160 
161  s = m_q;
162 
164 
165  return s;
166  }
167 
168 
169  /* --- COMMANDS ---------------------------------------------------------- */
170 
171  /* ------------------------------------------------------------------- */
172  /* --- ENTITY -------------------------------------------------------- */
173  /* ------------------------------------------------------------------- */
174 
175  void SimpleAdmittanceController::display(std::ostream& os) const
176  {
177  os << "SimpleAdmittanceController " << getName();
178  try
179  {
180  getProfiler().report_all(3, os);
181  }
182  catch (ExceptionSignal e) {}
183  }
184  } // namespace talos_balance
185  } // namespace sot
186 } // namespace dynamicgraph
187 
#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