sot-talos-balance  2.0.5
Collection of dynamic-graph entities aimed at implementing balance control on talos.
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 <dynamic-graph/all-commands.h>
20 #include <dynamic-graph/command-bind.h>
21 #include <dynamic-graph/factory.h>
22 
23 #include <sot/core/debug.hh>
24 
25 #include "sot/core/stop-watch.hh"
26 
27 namespace dynamicgraph {
28 namespace sot {
29 namespace talos_balance {
30 namespace dg = ::dynamicgraph;
31 using namespace dg;
32 using namespace dg::command;
33 
34 // Size to be aligned "-------------------------------------------------------"
35 #define PROFILE_SIMPLE_PID_DX_REF_COMPUTATION \
36  "SimplePID: dx_ref computation "
37 
38 #define INPUT_SIGNALS \
39  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 ---------------------------------------------------- */
48 DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(SimplePID, "SimplePID");
49 
50 /* ------------------------------------------------------------------- */
51 /* --- CONSTRUCTION -------------------------------------------------- */
52 /* ------------------------------------------------------------------- */
53 SimplePID::SimplePID(const std::string& name)
54  : Entity(name),
55  CONSTRUCT_SIGNAL_IN(Kp, dynamicgraph::Vector),
56  CONSTRUCT_SIGNAL_IN(Ki, dynamicgraph::Vector),
57  CONSTRUCT_SIGNAL_IN(decayFactor, double),
58  CONSTRUCT_SIGNAL_IN(x, dynamicgraph::Vector),
59  CONSTRUCT_SIGNAL_IN(x_des, dynamicgraph::Vector),
60  CONSTRUCT_SIGNAL_IN(dx_des, dynamicgraph::Vector),
61  CONSTRUCT_SIGNAL_OUT(dx_ref, dynamicgraph::Vector, INPUT_SIGNALS),
62  m_initSucceeded(false) {
63  Entity::signalRegistration(INPUT_SIGNALS << OUTPUT_SIGNALS);
64 
65  /* Commands. */
66  addCommand("init", makeCommandVoid2(
67  *this, &SimplePID::init,
68  docCommandVoid2("Initialize the entity.", "time step",
69  "number of elements")));
70  addCommand("resetIntegralError",
71  makeCommandVoid0(*this, &SimplePID::resetIntegralError,
72  docCommandVoid0("Set integral error to zero.")));
73 }
74 
75 void SimplePID::init(const double& dt, const int& N) {
76  m_dt = dt;
77  m_integralError.setZero(N);
78  m_initSucceeded = true;
79 }
80 
82 
83 /* ------------------------------------------------------------------- */
84 /* --- SIGNALS ------------------------------------------------------- */
85 /* ------------------------------------------------------------------- */
86 
88  if (!m_initSucceeded) {
89  SEND_WARNING_STREAM_MSG(
90  "Cannot compute signal dx_ref before initialization!");
91  return s;
92  }
93 
94  getProfiler().start(PROFILE_SIMPLE_PID_DX_REF_COMPUTATION);
95 
96  const Vector& Kp = m_KpSIN(iter);
97  const Vector& Ki = m_KiSIN(iter);
98  const double& decayFactor = m_decayFactorSIN(iter);
99 
100  const Vector& x = m_xSIN(iter);
101  const Vector& x_des = m_x_desSIN(iter);
102 
103  const Vector& dx_des = m_dx_desSIN(iter);
104 
105  Vector x_err = x_des - x;
106 
107  Vector ddx_ref =
108  dx_des + Kp.cwiseProduct(x_err) + Ki.cwiseProduct(m_integralError);
109 
110  // update the integrator (AFTER using its value)
111  m_integralError += (x_err - decayFactor * m_integralError) * m_dt;
112 
113  s = ddx_ref;
114 
115  getProfiler().stop(PROFILE_SIMPLE_PID_DX_REF_COMPUTATION);
116 
117  return s;
118 }
119 
120 /* --- COMMANDS ---------------------------------------------------------- */
121 
122 /* ------------------------------------------------------------------- */
123 /* --- ENTITY -------------------------------------------------------- */
124 /* ------------------------------------------------------------------- */
125 
126 void SimplePID::display(std::ostream& os) const {
127  os << "SimplePID " << getName();
128  try {
129  getProfiler().report_all(3, os);
130  } catch (ExceptionSignal e) {
131  }
132 }
133 } // namespace talos_balance
134 } // namespace sot
135 } // namespace dynamicgraph
sot_talos_balance.test.appli_admittance_end_effector.sot
sot
Definition: appli_admittance_end_effector.py:117
dynamicgraph
Definition: treeview.dox:24
OUTPUT_SIGNALS
#define OUTPUT_SIGNALS
Definition: simple-pid.cpp:41
dynamicgraph::sot::talos_balance::SimplePID::init
void init(const double &dt, const int &N)
Definition: simple-pid.cpp:75
dynamicgraph::sot::talos_balance::SimplePID::display
virtual void display(std::ostream &os) const
Definition: simple-pid.cpp:126
dynamicgraph::sot::talos_balance::DEFINE_SIGNAL_OUT_FUNCTION
DEFINE_SIGNAL_OUT_FUNCTION(dq, dynamicgraph::Vector)
Definition: admittance-controller-end-effector.cpp:210
simple-pid.hh
sot_talos_balance.test.script_test_end_effector.x
x
Definition: script_test_end_effector.py:10
sot_talos_balance.test.appli_admittance_single_joint.Kp
list Kp
Definition: appli_admittance_single_joint.py:33
dynamicgraph::sot::talos_balance::SimplePID::resetIntegralError
void resetIntegralError()
Definition: simple-pid.cpp:81
dynamicgraph::sot::talos_balance::math::Vector
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: fwd.hh:36
dynamicgraph::sot::talos_balance::SimplePID::m_dt
double m_dt
Definition: simple-pid.hh:86
sot_talos_balance.test.appli_admittance_single_joint.dt
dt
Definition: appli_admittance_single_joint.py:17
dynamicgraph::sot::talos_balance::SimplePID::SimplePID
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SimplePID(const std::string &name)
Definition: simple-pid.cpp:53
dynamicgraph::sot::talos_balance::SimplePID::m_integralError
dynamicgraph::Vector m_integralError
true if the entity has been successfully initialized
Definition: simple-pid.hh:85
dynamicgraph::sot::talos_balance::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(AdmittanceControllerEndEffector, "AdmittanceControllerEndEffector")
PROFILE_SIMPLE_PID_DX_REF_COMPUTATION
#define PROFILE_SIMPLE_PID_DX_REF_COMPUTATION
Definition: simple-pid.cpp:35
dynamicgraph::sot::talos_balance::EntityClassName
AdmittanceControllerEndEffector EntityClassName
Definition: admittance-controller-end-effector.cpp:46
dynamicgraph::sot::talos_balance::SimplePID::m_initSucceeded
bool m_initSucceeded
Definition: simple-pid.hh:84
sot_talos_balance.test.appli_dcm_zmp_control.name
name
Definition: appli_dcm_zmp_control.py:298
INPUT_SIGNALS
#define INPUT_SIGNALS
Definition: simple-pid.cpp:38