sot-talos-balance  2.0.5
Collection of dynamic-graph entities aimed at implementing balance control on talos.
simple-pidd.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_PIDD_DDX_REF_COMPUTATION \
36  "SimplePIDD: ddx_ref computation "
37 
38 #define INPUT_SIGNALS \
39  m_KpSIN << m_KiSIN << m_KdSIN << m_decayFactorSIN << m_xSIN << m_x_desSIN \
40  << m_dxSIN << m_dx_desSIN << m_ddx_desSIN
41 
42 #define OUTPUT_SIGNALS m_ddx_refSOUT << m_dx_refSOUT
43 
46 typedef SimplePIDD EntityClassName;
47 
48 /* --- DG FACTORY ---------------------------------------------------- */
49 DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(SimplePIDD, "SimplePIDD");
50 
51 /* ------------------------------------------------------------------- */
52 /* --- CONSTRUCTION -------------------------------------------------- */
53 /* ------------------------------------------------------------------- */
54 SimplePIDD::SimplePIDD(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(Kd, dynamicgraph::Vector),
59  CONSTRUCT_SIGNAL_IN(decayFactor, double),
60  CONSTRUCT_SIGNAL_IN(x, dynamicgraph::Vector),
61  CONSTRUCT_SIGNAL_IN(x_des, dynamicgraph::Vector),
62  CONSTRUCT_SIGNAL_IN(dx, dynamicgraph::Vector),
63  CONSTRUCT_SIGNAL_IN(dx_des, dynamicgraph::Vector),
64  CONSTRUCT_SIGNAL_IN(ddx_des, dynamicgraph::Vector),
65  CONSTRUCT_SIGNAL_OUT(ddx_ref, dynamicgraph::Vector, INPUT_SIGNALS),
66  CONSTRUCT_SIGNAL_OUT(dx_ref, dynamicgraph::Vector, m_ddx_refSOUT),
67  m_initSucceeded(false) {
68  Entity::signalRegistration(INPUT_SIGNALS << OUTPUT_SIGNALS);
69 
70  /* Commands. */
71  addCommand("init", makeCommandVoid2(
72  *this, &SimplePIDD::init,
73  docCommandVoid2("Initialize the entity.", "time step",
74  "number of elements")));
75  addCommand("resetIntegralError",
76  makeCommandVoid0(*this, &SimplePIDD::resetIntegralError,
77  docCommandVoid0("Set integral error to zero.")));
78  addCommand(
79  "resetVelocity",
80  makeCommandVoid0(*this, &SimplePIDD::resetIntegralError,
81  docCommandVoid0("Set reference velocity to zero.")));
82 }
83 
84 void SimplePIDD::init(const double& dt, const int& N) {
85  m_dt = dt;
86  m_integralError.setZero(N);
87  m_dx_ref.setZero(N);
88  m_initSucceeded = true;
89 }
90 
91 void SimplePIDD::resetVelocity() { m_dx_ref.setZero(); }
92 
94 
95 /* ------------------------------------------------------------------- */
96 /* --- SIGNALS ------------------------------------------------------- */
97 /* ------------------------------------------------------------------- */
98 
100  if (!m_initSucceeded) {
101  SEND_WARNING_STREAM_MSG(
102  "Cannot compute signal ddx_ref before initialization!");
103  return s;
104  }
105 
106  getProfiler().start(PROFILE_SIMPLE_PIDD_DDX_REF_COMPUTATION);
107 
108  const Vector& Kp = m_KpSIN(iter);
109  const Vector& Ki = m_KiSIN(iter);
110  const Vector& Kd = m_KiSIN(iter);
111  const double& decayFactor = m_decayFactorSIN(iter);
112 
113  const Vector& x = m_xSIN(iter);
114  const Vector& x_des = m_x_desSIN(iter);
115 
116  const Vector& dx = m_dxSIN(iter);
117  const Vector& dx_des = m_dx_desSIN(iter);
118 
119  const Vector& ddx_des = m_ddx_desSIN(iter);
120 
121  Vector x_err = x_des - x;
122  Vector dx_err = dx_des - dx;
123 
124  Vector ddx_ref = ddx_des + Kd.cwiseProduct(dx_err) + Kp.cwiseProduct(x_err) +
125  Ki.cwiseProduct(m_integralError);
126 
127  // update the integrator (AFTER using its value)
128  m_integralError += (x_err - decayFactor * m_integralError) * m_dt;
129 
130  s = ddx_ref;
131 
132  getProfiler().stop(PROFILE_SIMPLE_PIDD_DDX_REF_COMPUTATION);
133 
134  return s;
135 }
136 
138  if (!m_initSucceeded) {
139  SEND_WARNING_STREAM_MSG(
140  "Cannot compute signal dx_ref before initialization!");
141  return s;
142  }
143 
144  const Vector& ddx_ref = m_ddx_refSOUT(iter);
145 
146  m_dx_ref += ddx_ref * m_dt;
147 
148  s = m_dx_ref;
149 
150  return s;
151 }
152 
153 /* --- COMMANDS ---------------------------------------------------------- */
154 
155 /* ------------------------------------------------------------------- */
156 /* --- ENTITY -------------------------------------------------------- */
157 /* ------------------------------------------------------------------- */
158 
159 void SimplePIDD::display(std::ostream& os) const {
160  os << "SimplePIDD " << getName();
161  try {
162  getProfiler().report_all(3, os);
163  } catch (ExceptionSignal e) {
164  }
165 }
166 } // namespace talos_balance
167 } // namespace sot
168 } // namespace dynamicgraph
dynamicgraph::sot::talos_balance::SimplePIDD::resetVelocity
void resetVelocity()
Definition: simple-pidd.cpp:91
sot_talos_balance.test.appli_admittance_end_effector.sot
sot
Definition: appli_admittance_end_effector.py:117
dynamicgraph
Definition: treeview.dox:24
dynamicgraph::sot::talos_balance::SimplePIDD::m_integralError
dynamicgraph::Vector m_integralError
Definition: simple-pidd.hh:92
dynamicgraph::sot::talos_balance::SimplePIDD::init
void init(const double &dt, const int &N)
Definition: simple-pidd.cpp:84
simple-pidd.hh
dynamicgraph::sot::talos_balance::SimplePIDD::m_dx_ref
dynamicgraph::Vector m_dx_ref
true if the entity has been successfully initialized
Definition: simple-pidd.hh:91
dynamicgraph::sot::talos_balance::SimplePIDD::resetIntegralError
void resetIntegralError()
Definition: simple-pidd.cpp:93
dynamicgraph::sot::talos_balance::SimplePIDD::SimplePIDD
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SimplePIDD(const std::string &name)
Definition: simple-pidd.cpp:54
dynamicgraph::sot::talos_balance::DEFINE_SIGNAL_OUT_FUNCTION
DEFINE_SIGNAL_OUT_FUNCTION(dq, dynamicgraph::Vector)
Definition: admittance-controller-end-effector.cpp:210
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::math::Vector
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: fwd.hh:36
dynamicgraph::sot::talos_balance::SimplePIDD::m_dt
double m_dt
Definition: simple-pidd.hh:93
sot_talos_balance.test.appli_admittance_single_joint.dt
dt
Definition: appli_admittance_single_joint.py:17
PROFILE_SIMPLE_PIDD_DDX_REF_COMPUTATION
#define PROFILE_SIMPLE_PIDD_DDX_REF_COMPUTATION
Definition: simple-pidd.cpp:35
dynamicgraph::sot::talos_balance::SimplePIDD::m_initSucceeded
bool m_initSucceeded
Definition: simple-pidd.hh:90
OUTPUT_SIGNALS
#define OUTPUT_SIGNALS
Definition: simple-pidd.cpp:42
INPUT_SIGNALS
#define INPUT_SIGNALS
Definition: simple-pidd.cpp:38
dynamicgraph::sot::talos_balance::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(AdmittanceControllerEndEffector, "AdmittanceControllerEndEffector")
dynamicgraph::sot::talos_balance::EntityClassName
AdmittanceControllerEndEffector EntityClassName
Definition: admittance-controller-end-effector.cpp:46
sot_talos_balance.test.appli_dcm_zmp_control.name
name
Definition: appli_dcm_zmp_control.py:298
dynamicgraph::sot::talos_balance::SimplePIDD::display
virtual void display(std::ostream &os) const
Definition: simple-pidd.cpp:159