sot-talos-balance  1.5.0
simple-pid.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_SIMPLE_PID_DX_REF_COMPUTATION "SimplePID: dx_ref computation "
38 
39 #define INPUT_SIGNALS m_KpSIN << m_KiSIN << m_decayFactorSIN << m_xSIN << m_x_desSIN << m_dx_desSIN
40 
41 #define OUTPUT_SIGNALS m_dx_refSOUT
42 
45  typedef SimplePID EntityClassName;
46 
47  /* --- DG FACTORY ---------------------------------------------------- */
49  "SimplePID");
50 
51  /* ------------------------------------------------------------------- */
52  /* --- CONSTRUCTION -------------------------------------------------- */
53  /* ------------------------------------------------------------------- */
54  SimplePID::SimplePID(const std::string& name)
55  : Entity(name)
56  , CONSTRUCT_SIGNAL_IN(Kp, dynamicgraph::Vector)
57  , CONSTRUCT_SIGNAL_IN(Ki, dynamicgraph::Vector)
58  , CONSTRUCT_SIGNAL_IN(decayFactor, double)
59  , CONSTRUCT_SIGNAL_IN(x, dynamicgraph::Vector)
60  , CONSTRUCT_SIGNAL_IN(x_des, dynamicgraph::Vector)
61  , CONSTRUCT_SIGNAL_IN(dx_des, dynamicgraph::Vector)
62  , CONSTRUCT_SIGNAL_OUT(dx_ref, dynamicgraph::Vector, INPUT_SIGNALS)
63  , m_initSucceeded(false)
64  {
65  Entity::signalRegistration( INPUT_SIGNALS << OUTPUT_SIGNALS );
66 
67  /* Commands. */
68  addCommand("init", makeCommandVoid2(*this, &SimplePID::init, docCommandVoid2("Initialize the entity.","time step","number of elements")));
69  addCommand("resetIntegralError", makeCommandVoid0(*this, &SimplePID::resetIntegralError, docCommandVoid0("Set integral error to zero.")));
70  }
71 
72  void SimplePID::init(const double & dt, const int & N)
73  {
74  m_dt = dt;
75  m_integralError.setZero(N);
76  m_initSucceeded = true;
77  }
78 
80  {
81  m_integralError.setZero();
82  }
83 
84  /* ------------------------------------------------------------------- */
85  /* --- SIGNALS ------------------------------------------------------- */
86  /* ------------------------------------------------------------------- */
87 
89  {
90  if(!m_initSucceeded)
91  {
92  SEND_WARNING_STREAM_MSG("Cannot compute signal dx_ref before initialization!");
93  return s;
94  }
95 
96  getProfiler().start(PROFILE_SIMPLE_PID_DX_REF_COMPUTATION);
97 
98 
99  const Vector & Kp = m_KpSIN(iter);
100  const Vector & Ki = m_KiSIN(iter);
101  const double & decayFactor = m_decayFactorSIN(iter);
102 
103  const Vector & x = m_xSIN(iter);
104  const Vector & x_des = m_x_desSIN(iter);
105 
106  const Vector & dx_des = m_dx_desSIN(iter);
107 
108  Vector x_err = x_des - x;
109 
110  Vector ddx_ref = dx_des + Kp.cwiseProduct(x_err) + Ki.cwiseProduct(m_integralError);
111 
112  // update the integrator (AFTER using its value)
113  m_integralError += ( x_err - decayFactor*m_integralError ) * m_dt;
114 
115  s = ddx_ref;
116 
117  getProfiler().stop(PROFILE_SIMPLE_PID_DX_REF_COMPUTATION);
118 
119  return s;
120  }
121 
122  /* --- COMMANDS ---------------------------------------------------------- */
123 
124  /* ------------------------------------------------------------------- */
125  /* --- ENTITY -------------------------------------------------------- */
126  /* ------------------------------------------------------------------- */
127 
128  void SimplePID::display(std::ostream& os) const
129  {
130  os << "SimplePID " << getName();
131  try
132  {
133  getProfiler().report_all(3, os);
134  }
135  catch (ExceptionSignal e) {}
136  }
137  } // namespace talos_balance
138  } // namespace sot
139 } // namespace dynamicgraph
140 
dynamicgraph::Vector m_integralError
true if the entity has been successfully initialized
Definition: simple-pid.hh:85
virtual void display(std::ostream &os) const
Definition: simple-pid.cpp:128
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SimplePID(const std::string &name)
Definition: simple-pid.cpp:54
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: math/fwd.hh:40
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(AdmittanceControllerEndEffector, "AdmittanceControllerEndEffector")
#define INPUT_SIGNALS
Definition: simple-pid.cpp:39
void init(const double &dt, const int &N)
Definition: simple-pid.cpp:72
#define PROFILE_SIMPLE_PID_DX_REF_COMPUTATION
Definition: simple-pid.cpp:37
#define OUTPUT_SIGNALS
Definition: simple-pid.cpp:41