sot-talos-balance  2.0.5
Collection of dynamic-graph entities aimed at implementing balance control on talos.
dcm-com-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 <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 #include <sot/core/stop-watch.hh>
25 
26 namespace dynamicgraph {
27 namespace sot {
28 namespace talos_balance {
29 namespace dg = ::dynamicgraph;
30 using namespace dg;
31 using namespace dg::command;
32 
33 // Size to be aligned "-------------------------------------------------------"
34 #define PROFILE_DCMCOMCONTROLLER_DDCOMREF_COMPUTATION \
35  "DcmComController: ddcomRef computation "
36 #define PROFILE_DCMCOMCONTROLLER_ZMPREF_COMPUTATION \
37  "DcmComController: zmpRef computation "
38 #define PROFILE_DCMCOMCONTROLLER_WRENCHREF_COMPUTATION \
39  "DcmComController: wrenchRef computation "
40 
41 #define INPUT_SIGNALS \
42  m_KpSIN << m_KiSIN << m_decayFactorSIN << m_omegaSIN << m_massSIN \
43  << m_dcmSIN << m_dcmDesSIN << m_comDesSIN << m_ddcomDesSIN
44 
45 #define OUTPUT_SIGNALS m_ddcomRefSOUT << m_zmpRefSOUT << m_wrenchRefSOUT
46 
49 typedef DcmComController EntityClassName;
50 
51 /* --- DG FACTORY ---------------------------------------------------- */
52 DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(DcmComController, "DcmComController");
53 
54 /* ------------------------------------------------------------------- */
55 /* --- CONSTRUCTION -------------------------------------------------- */
56 /* ------------------------------------------------------------------- */
58  : Entity(name),
59  CONSTRUCT_SIGNAL_IN(Kp, dynamicgraph::Vector),
60  CONSTRUCT_SIGNAL_IN(Ki, dynamicgraph::Vector),
61  CONSTRUCT_SIGNAL_IN(decayFactor, double),
62  CONSTRUCT_SIGNAL_IN(omega, double),
63  CONSTRUCT_SIGNAL_IN(mass, double),
64  CONSTRUCT_SIGNAL_IN(dcm, dynamicgraph::Vector),
65  CONSTRUCT_SIGNAL_IN(dcmDes, dynamicgraph::Vector),
66  CONSTRUCT_SIGNAL_IN(comDes, dynamicgraph::Vector),
67  CONSTRUCT_SIGNAL_IN(ddcomDes, dynamicgraph::Vector),
68  CONSTRUCT_SIGNAL_OUT(ddcomRef, dynamicgraph::Vector, INPUT_SIGNALS),
69  CONSTRUCT_SIGNAL_OUT(zmpRef, dynamicgraph::Vector,
70  m_ddcomRefSOUT << m_comDesSIN << m_omegaSIN),
71  CONSTRUCT_SIGNAL_OUT(wrenchRef, dynamicgraph::Vector,
72  m_ddcomRefSOUT << m_comDesSIN << m_massSIN),
73  m_initSucceeded(false) {
74  Entity::signalRegistration(INPUT_SIGNALS << OUTPUT_SIGNALS);
75 
76  /* Commands. */
77  addCommand("init", makeCommandVoid1(*this, &DcmComController::init,
78  docCommandVoid1("Initialize the entity.",
79  "time step")));
80  addCommand(
81  "resetDcmIntegralError",
82  makeCommandVoid0(*this, &DcmComController::resetDcmIntegralError,
83  docCommandVoid0("Set dcm integral error to zero.")));
84 }
85 
86 void DcmComController::init(const double& dt) {
87  if (!m_KpSIN.isPlugged())
88  return SEND_MSG("Init failed: signal Kp is not plugged", MSG_TYPE_ERROR);
89  if (!m_KiSIN.isPlugged())
90  return SEND_MSG("Init failed: signal Ki is not plugged", MSG_TYPE_ERROR);
91  if (!m_decayFactorSIN.isPlugged())
92  return SEND_MSG("Init failed: signal decayFactor is not plugged",
93  MSG_TYPE_ERROR);
94  if (!m_omegaSIN.isPlugged())
95  return SEND_MSG("Init failed: signal omega is not plugged", MSG_TYPE_ERROR);
96  if (!m_massSIN.isPlugged())
97  return SEND_MSG("Init failed: signal mass is not plugged", MSG_TYPE_ERROR);
98  if (!m_dcmSIN.isPlugged())
99  return SEND_MSG("Init failed: signal dcm is not plugged", MSG_TYPE_ERROR);
100  if (!m_dcmDesSIN.isPlugged())
101  return SEND_MSG("Init failed: signal dcmDes is not plugged",
102  MSG_TYPE_ERROR);
103  if (!m_comDesSIN.isPlugged())
104  return SEND_MSG("Init failed: signal comDes is not plugged",
105  MSG_TYPE_ERROR);
106  if (!m_ddcomDesSIN.isPlugged())
107  return SEND_MSG("Init failed: signal ddcomDes is not plugged",
108  MSG_TYPE_ERROR);
109 
110  m_dt = dt;
112  m_initSucceeded = true;
113 }
114 
116  m_dcmIntegralError.setZero(3);
117 }
118 
119 /* ------------------------------------------------------------------- */
120 /* --- SIGNALS ------------------------------------------------------- */
121 /* ------------------------------------------------------------------- */
122 
124  if (!m_initSucceeded) {
125  SEND_WARNING_STREAM_MSG(
126  "Cannot compute signal ddcomRef before initialization!");
127  return s;
128  }
129  if (s.size() != 3) s.resize(3);
130 
132 
133  const Vector& Kp = m_KpSIN(iter);
134  const Vector& Ki = m_KiSIN(iter);
135  const double& decayFactor = m_decayFactorSIN(iter);
136  const double& omega = m_omegaSIN(iter);
137  const Vector& dcm = m_dcmSIN(iter);
138  const Vector& dcmDes = m_dcmDesSIN(iter);
139  const Vector& ddcomDes = m_ddcomDesSIN(iter);
140 
141  assert(Kp.size() == 3 && "Unexpected size of signal Kp");
142  assert(Ki.size() == 3 && "Unexpected size of signal Ki");
143  assert(dcm.size() == 3 && "Unexpected size of signal dcm");
144  assert(dcmDes.size() == 3 && "Unexpected size of signal dcmDes");
145  assert(ddcomDes.size() == 3 && "Unexpected size of signal ddcomDes");
146 
147  const Eigen::Vector3d dcmError = dcmDes - dcm;
148 
149  const Eigen::Vector3d ddcomRef = ddcomDes +
150  omega * Kp.cwiseProduct(dcmError) +
151  omega * Ki.cwiseProduct(m_dcmIntegralError);
152 
153  // update the integrator (AFTER using its value)
154  m_dcmIntegralError += (dcmError - decayFactor * m_dcmIntegralError) * m_dt;
155 
156  s = ddcomRef;
157 
159 
160  return s;
161 }
162 
164  if (!m_initSucceeded) {
165  SEND_WARNING_STREAM_MSG(
166  "Cannot compute signal zmpRef before initialization!");
167  return s;
168  }
169  if (s.size() != 3) s.resize(3);
170 
171  getProfiler().start(PROFILE_DCMCOMCONTROLLER_ZMPREF_COMPUTATION);
172 
173  const double& omega = m_omegaSIN(iter);
174  const Vector& comDes = m_comDesSIN(iter);
175 
176  const Vector& ddcomRef = m_ddcomRefSOUT(iter);
177 
178  assert(comDes.size() == 3 && "Unexpected size of signal comDes");
179 
180  Eigen::Vector3d zmpRef = comDes - ddcomRef / (omega * omega);
181  zmpRef[2] = 0.0; // maybe needs better way
182 
183  s = zmpRef;
184 
186 
187  return s;
188 }
189 
191  if (!m_initSucceeded) {
192  SEND_WARNING_STREAM_MSG(
193  "Cannot compute signal wrenchRef before initialization!");
194  return s;
195  }
196  if (s.size() != 6) s.resize(6);
197 
199 
200  const double& mass = m_massSIN(iter);
201  const Vector& comDes = m_comDesSIN(iter);
202 
203  const Vector& ddcomRef = m_ddcomRefSOUT(iter);
204 
205  assert(comDes.size() == 3 && "Unexpected size of signal comDes");
206 
207  Eigen::Vector3d gravity;
208  gravity << 0.0, 0.0, -9.81;
209 
210  const Eigen::Vector3d forceRef = mass * (ddcomRef - gravity);
211 
212  Eigen::Matrix<double, 6, 1> wrenchRef;
213  wrenchRef.head<3>() = forceRef;
214  const Eigen::Vector3d comDes3 = comDes;
215  wrenchRef.tail<3>() = comDes3.cross(wrenchRef.head<3>());
216 
217  s = wrenchRef;
218 
220 
221  return s;
222 }
223 
224 /* --- COMMANDS ---------------------------------------------------------- */
225 
226 /* ------------------------------------------------------------------- */
227 /* --- ENTITY -------------------------------------------------------- */
228 /* ------------------------------------------------------------------- */
229 
230 void DcmComController::display(std::ostream& os) const {
231  os << "DcmComController " << getName();
232  try {
233  getProfiler().report_all(3, os);
234  } catch (ExceptionSignal e) {
235  }
236 }
237 } // namespace talos_balance
238 } // namespace sot
239 } // namespace dynamicgraph
PROFILE_DCMCOMCONTROLLER_DDCOMREF_COMPUTATION
#define PROFILE_DCMCOMCONTROLLER_DDCOMREF_COMPUTATION
Definition: dcm-com-controller.cpp:34
sot_talos_balance.test.appli_dcmZmpControl.comDes
comDes
Definition: appli_dcmZmpControl.py:25
dynamicgraph::sot::talos_balance::DcmComController::m_dt
double m_dt
Definition: dcm-com-controller.hh:88
sot_talos_balance.test.appli_admittance_end_effector.sot
sot
Definition: appli_admittance_end_effector.py:117
dynamicgraph
Definition: treeview.dox:24
PROFILE_DCMCOMCONTROLLER_ZMPREF_COMPUTATION
#define PROFILE_DCMCOMCONTROLLER_ZMPREF_COMPUTATION
Definition: dcm-com-controller.cpp:36
sot_talos_balance.test.appli_dcmZmpControl.ddcomDes
tuple ddcomDes
Definition: appli_dcmZmpControl.py:31
sot_talos_balance.test.appli_dcmZmpControl.dcmDes
dcmDes
Definition: appli_dcmZmpControl.py:29
sot_talos_balance.test.appli_ankle_admittance.mass
mass
Definition: appli_ankle_admittance.py:36
dynamicgraph::sot::talos_balance::DcmComController::DcmComController
EIGEN_MAKE_ALIGNED_OPERATOR_NEW DcmComController(const std::string &name)
Definition: dcm-com-controller.cpp:57
dynamicgraph::sot::talos_balance::DcmComController::display
virtual void display(std::ostream &os) const
Definition: dcm-com-controller.cpp:230
dynamicgraph::sot::talos_balance::DcmComController::resetDcmIntegralError
void resetDcmIntegralError()
Definition: dcm-com-controller.cpp:115
dynamicgraph::sot::talos_balance::DEFINE_SIGNAL_OUT_FUNCTION
DEFINE_SIGNAL_OUT_FUNCTION(dq, dynamicgraph::Vector)
Definition: admittance-controller-end-effector.cpp:210
OUTPUT_SIGNALS
#define OUTPUT_SIGNALS
Definition: dcm-com-controller.cpp:45
dynamicgraph::sot::talos_balance::DcmComController::init
void init(const double &dt)
Definition: dcm-com-controller.cpp:86
sot_talos_balance.test.appli_admittance_single_joint.Kp
list Kp
Definition: appli_admittance_single_joint.py:33
PROFILE_DCMCOMCONTROLLER_WRENCHREF_COMPUTATION
#define PROFILE_DCMCOMCONTROLLER_WRENCHREF_COMPUTATION
Definition: dcm-com-controller.cpp:38
dynamicgraph::sot::talos_balance::math::Vector
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: fwd.hh:36
dcm-com-controller.hh
sot_talos_balance.test.appli_admittance_single_joint.dt
dt
Definition: appli_admittance_single_joint.py:17
dynamicgraph::sot::talos_balance::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(AdmittanceControllerEndEffector, "AdmittanceControllerEndEffector")
sot_talos_balance.test.appli_ankle_admittance.omega
omega
Definition: appli_ankle_admittance.py:39
dynamicgraph::sot::talos_balance::DcmComController::m_initSucceeded
bool m_initSucceeded
Definition: dcm-com-controller.hh:86
dynamicgraph::sot::talos_balance::DcmComController::m_dcmIntegralError
dynamicgraph::Vector m_dcmIntegralError
true if the entity has been successfully initialized
Definition: dcm-com-controller.hh:87
dynamicgraph::sot::talos_balance::EntityClassName
AdmittanceControllerEndEffector EntityClassName
Definition: admittance-controller-end-effector.cpp:46
INPUT_SIGNALS
#define INPUT_SIGNALS
Definition: dcm-com-controller.cpp:41
sot_talos_balance.test.appli_dcm_zmp_control.name
name
Definition: appli_dcm_zmp_control.py:298