sot-talos-balance  2.0.5
Collection of dynamic-graph entities aimed at implementing balance control on talos.
foot-force-difference-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 #define INPUT_SIGNALS \
34  m_phaseSIN << m_gainSwingSIN << m_gainStanceSIN << m_gainDoubleSIN \
35  << m_dfzAdmittanceSIN << m_vdcFrequencySIN << m_vdcDampingSIN \
36  << m_swingAdmittanceSIN << m_wrenchRightDesSIN \
37  << m_wrenchLeftDesSIN << m_wrenchRightSIN << m_wrenchLeftSIN \
38  << m_posRightDesSIN << m_posLeftDesSIN << m_posRightSIN \
39  << m_posLeftSIN
40 
41 #define INNER_SIGNALS m_dz_ctrlSOUT << m_dz_posSOUT
42 
43 #define OUTPUT_SIGNALS \
44  m_vRightSOUT << m_vLeftSOUT << m_gainRightSOUT << m_gainLeftSOUT
45 
48 typedef FootForceDifferenceController EntityClassName;
49 
50 /* --- DG FACTORY ---------------------------------------------------- */
51 DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(FootForceDifferenceController,
52  "FootForceDifferenceController");
53 
54 /* ------------------------------------------------------------------- */
55 /* --- CONSTRUCTION -------------------------------------------------- */
56 /* ------------------------------------------------------------------- */
58  const std::string& name)
59  : Entity(name),
60  CONSTRUCT_SIGNAL_IN(phase, int),
61  CONSTRUCT_SIGNAL_IN(gainSwing, double),
62  CONSTRUCT_SIGNAL_IN(gainStance, double),
63  CONSTRUCT_SIGNAL_IN(gainDouble, double),
64  CONSTRUCT_SIGNAL_IN(dfzAdmittance, double),
65  CONSTRUCT_SIGNAL_IN(vdcFrequency, double),
66  CONSTRUCT_SIGNAL_IN(vdcDamping, double),
67  CONSTRUCT_SIGNAL_IN(swingAdmittance, dynamicgraph::Vector),
68  CONSTRUCT_SIGNAL_IN(wrenchRightDes, dynamicgraph::Vector),
69  CONSTRUCT_SIGNAL_IN(wrenchLeftDes, dynamicgraph::Vector),
70  CONSTRUCT_SIGNAL_IN(wrenchRight, dynamicgraph::Vector),
71  CONSTRUCT_SIGNAL_IN(wrenchLeft, dynamicgraph::Vector),
72  CONSTRUCT_SIGNAL_IN(posRightDes, MatrixHomogeneous),
73  CONSTRUCT_SIGNAL_IN(posLeftDes, MatrixHomogeneous),
74  CONSTRUCT_SIGNAL_IN(posRight, MatrixHomogeneous),
75  CONSTRUCT_SIGNAL_IN(posLeft, MatrixHomogeneous),
76  CONSTRUCT_SIGNAL_INNER(dz_ctrl, double,
77  m_dfzAdmittanceSIN
78  << m_vdcDampingSIN << m_wrenchRightDesSIN
79  << m_wrenchLeftDesSIN << m_wrenchRightSIN
80  << m_wrenchLeftSIN << m_posRightSIN
81  << m_posLeftSIN),
82  CONSTRUCT_SIGNAL_INNER(dz_pos, double,
83  m_vdcFrequencySIN
84  << m_posRightDesSIN << m_posLeftDesSIN
85  << m_posRightSIN << m_posLeftSIN),
86  CONSTRUCT_SIGNAL_OUT(vRight, dynamicgraph::Vector,
87  m_phaseSIN << m_dz_ctrlSINNER << m_dz_posSINNER
88  << m_swingAdmittanceSIN
89  << m_wrenchRightSIN),
90  CONSTRUCT_SIGNAL_OUT(vLeft, dynamicgraph::Vector,
91  m_phaseSIN << m_dz_ctrlSINNER << m_dz_posSINNER
92  << m_swingAdmittanceSIN
93  << m_wrenchLeftSIN),
94  CONSTRUCT_SIGNAL_OUT(
95  gainRight, double,
96  m_phaseSIN << m_gainSwingSIN << m_gainStanceSIN << m_gainDoubleSIN),
97  CONSTRUCT_SIGNAL_OUT(
98  gainLeft, double,
99  m_phaseSIN << m_gainSwingSIN << m_gainStanceSIN << m_gainDoubleSIN),
100  m_eps(15),
101  m_initSucceeded(false) {
102  Entity::signalRegistration(INPUT_SIGNALS << OUTPUT_SIGNALS);
103 
104  /* Commands. */
105  addCommand("init",
106  makeCommandVoid0(*this, &FootForceDifferenceController::init,
107  docCommandVoid0("Initialize the entity.")));
108  addCommand(
109  "getForceThreshold",
110  makeDirectGetter(*this, &m_eps,
111  docDirectGetter("Get force threshold", "double")));
112  addCommand(
113  "setForceThreshold",
114  makeDirectSetter(*this, &m_eps,
115  docDirectSetter("Set force threshold", "double")));
116 }
117 
119 
121  const dg::Vector& wrench, const dg::Vector& swingAdmittance) {
122  assert(swingAdmittance.size() == 3);
123 
124  Eigen::Vector3d res;
125  for (int i = 0; i < 3; i++)
126  if (wrench[i] > m_eps || wrench[i] < -m_eps)
127  res[i] = swingAdmittance[i] * wrench[i];
128  else
129  res[i] = 0;
130  return res;
131 }
132 
133 /* ------------------------------------------------------------------- */
134 /* --- SIGNALS ------------------------------------------------------- */
135 /* ------------------------------------------------------------------- */
136 
138  if (!m_initSucceeded) {
139  SEND_WARNING_STREAM_MSG("Can't compute dz_ctrl before initialization!");
140  return s;
141  }
142 
143  const double& dfzAdmittance = m_dfzAdmittanceSIN(iter);
144  const double& vdcDamping = m_vdcDampingSIN(iter);
145 
146  const Eigen::VectorXd& wrenchRightDes = m_wrenchRightDesSIN(iter);
147  const Eigen::VectorXd& wrenchLeftDes = m_wrenchLeftDesSIN(iter);
148  const Eigen::VectorXd& wrenchRight = m_wrenchRightSIN(iter);
149  const Eigen::VectorXd& wrenchLeft = m_wrenchLeftSIN(iter);
150 
151  const MatrixHomogeneous& posRight = m_posRightSIN(iter);
152  const MatrixHomogeneous& posLeft = m_posLeftSIN(iter);
153 
154  const double RTz = posRight.translation()[2];
155  const double LTz = posLeft.translation()[2];
156 
157  const double RFz_d = wrenchRightDes[2];
158  const double LFz_d = wrenchLeftDes[2];
159 
160  const double RFz = wrenchRight[2];
161  const double LFz = wrenchLeft[2];
162 
163  const double dz_ctrl = dfzAdmittance * ((LFz_d - RFz_d) - (LFz - RFz)) -
164  vdcDamping * (RTz - LTz);
165 
166  s = dz_ctrl;
167 
168  return s;
169 }
170 
172  if (!m_initSucceeded) {
173  SEND_WARNING_STREAM_MSG("Can't compute dz_pos before initialization!");
174  return s;
175  }
176 
177  const double& vdcFrequency = m_vdcFrequencySIN(iter);
178 
179  const MatrixHomogeneous& posRightDes = m_posRightDesSIN(iter);
180  const MatrixHomogeneous& posLeftDes = m_posLeftDesSIN(iter);
181  const MatrixHomogeneous& posRight = m_posRightSIN(iter);
182  const MatrixHomogeneous& posLeft = m_posLeftSIN(iter);
183 
184  const double RTz_d = posRightDes.translation()[2];
185  const double LTz_d = posLeftDes.translation()[2];
186 
187  const double RTz = posRight.translation()[2];
188  const double LTz = posLeft.translation()[2];
189 
190  const double dz_pos = vdcFrequency * ((LTz_d + RTz_d) - (LTz + RTz));
191 
192  s = dz_pos;
193 
194  return s;
195 }
196 
198  if (!m_initSucceeded) {
199  SEND_WARNING_STREAM_MSG("Can't compute vRight before initialization!");
200  return s;
201  }
202  if (s.size() != 6) s.resize(6);
203 
204  const double& dz_ctrl = m_dz_ctrlSINNER(iter);
205  const double& dz_pos = m_dz_posSINNER(iter);
206  const int& phase = m_phaseSIN(iter);
207 
208  s.setZero(6);
209 
210  if (phase == 0) {
211  s[2] = 0.5 * (dz_pos + dz_ctrl);
212  } else if (m_swingAdmittanceSIN.isPlugged() && phase > 0) {
213  const Eigen::VectorXd& wrenchRight = m_wrenchRightSIN(iter);
214  const Eigen::VectorXd& swingAdmittance = m_swingAdmittanceSIN(iter);
215  s.head<3>() = calcSwingAdmittance(wrenchRight, swingAdmittance);
216  }
217 
218  return s;
219 }
220 
222  if (!m_initSucceeded) {
223  SEND_WARNING_STREAM_MSG("Can't compute vLeft before initialization!");
224  return s;
225  }
226  if (s.size() != 6) s.resize(6);
227 
228  const double& dz_ctrl = m_dz_ctrlSINNER(iter);
229  const double& dz_pos = m_dz_posSINNER(iter);
230  const int& phase = m_phaseSIN(iter);
231 
232  s.setZero(6);
233 
234  if (phase == 0) {
235  s[2] = 0.5 * (dz_pos - dz_ctrl);
236  } else if (m_swingAdmittanceSIN.isPlugged() && phase < 0) {
237  const Eigen::VectorXd& wrenchLeft = m_wrenchLeftSIN(iter);
238  const Eigen::VectorXd& swingAdmittance = m_swingAdmittanceSIN(iter);
239  s.head<3>() = calcSwingAdmittance(wrenchLeft, swingAdmittance);
240  }
241 
242  return s;
243 }
244 
245 DEFINE_SIGNAL_OUT_FUNCTION(gainRight, double) {
246  if (!m_initSucceeded) {
247  SEND_WARNING_STREAM_MSG("Can't compute gainRight before initialization!");
248  return s;
249  }
250 
251  const int& phase = m_phaseSIN(iter);
252  const double& gainSwing = m_gainSwingSIN(iter);
253  const double& gainStance = m_gainStanceSIN(iter);
254  const double& gainDouble = m_gainDoubleSIN(iter);
255 
256  if (phase > 0)
257  s = gainSwing;
258  else if (phase < 0)
259  s = gainStance;
260  else
261  s = gainDouble;
262 
263  return s;
264 }
265 
266 DEFINE_SIGNAL_OUT_FUNCTION(gainLeft, double) {
267  if (!m_initSucceeded) {
268  SEND_WARNING_STREAM_MSG("Can't compute gainLeft before initialization!");
269  return s;
270  }
271 
272  const int& phase = m_phaseSIN(iter);
273  const double& gainSwing = m_gainSwingSIN(iter);
274  const double& gainStance = m_gainStanceSIN(iter);
275  const double& gainDouble = m_gainDoubleSIN(iter);
276 
277  if (phase > 0)
278  s = gainStance;
279  else if (phase < 0)
280  s = gainSwing;
281  else
282  s = gainDouble;
283 
284  return s;
285 }
286 
287 /* --- COMMANDS ---------------------------------------------------------- */
288 
289 /* ------------------------------------------------------------------- */
290 /* --- ENTITY -------------------------------------------------------- */
291 /* ------------------------------------------------------------------- */
292 
293 void FootForceDifferenceController::display(std::ostream& os) const {
294  os << "FootForceDifferenceController " << getName();
295  try {
296  getProfiler().report_all(3, os);
297  } catch (ExceptionSignal e) {
298  }
299 }
300 } // namespace talos_balance
301 } // namespace sot
302 } // namespace dynamicgraph
dynamicgraph::sot::talos_balance::FootForceDifferenceController::display
virtual void display(std::ostream &os) const
Definition: foot-force-difference-controller.cpp:293
dynamicgraph::sot::talos_balance::FootForceDifferenceController::calcSwingAdmittance
Eigen::Vector3d calcSwingAdmittance(const dynamicgraph::Vector &wrench, const dynamicgraph::Vector &swingAdmittance)
Definition: foot-force-difference-controller.cpp:120
sot_talos_balance.test.appli_admittance_end_effector.sot
sot
Definition: appli_admittance_end_effector.py:117
sot_talos_balance.test.appli_dcm_zmp_control_ffdc.swingAdmittance
list swingAdmittance
Definition: appli_dcm_zmp_control_ffdc.py:296
dynamicgraph
Definition: treeview.dox:24
dynamicgraph::sot::talos_balance::FootForceDifferenceController::m_initSucceeded
bool m_initSucceeded
Definition: foot-force-difference-controller.hh:109
sot_talos_balance.test.appli_dcm_zmp_control_ffdc.vdcDamping
float vdcDamping
Definition: appli_dcm_zmp_control_ffdc.py:295
OUTPUT_SIGNALS
#define OUTPUT_SIGNALS
Definition: foot-force-difference-controller.cpp:43
sot_talos_balance.test.appli_dcm_zmp_control_ffdc.vdcFrequency
float vdcFrequency
Definition: appli_dcm_zmp_control_ffdc.py:294
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.appli_dcm_zmp_control_ffdc.gainDouble
float gainDouble
Definition: appli_dcm_zmp_control_ffdc.py:300
foot-force-difference-controller.hh
dynamicgraph::sot::talos_balance::math::Vector
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: fwd.hh:36
sot_talos_balance.test.script_test_end_effector.wrench
wrench
Definition: script_test_end_effector.py:9
sot_talos_balance.test.appli_dcm_zmp_control_ffdc.gainSwing
float gainSwing
Definition: appli_dcm_zmp_control_ffdc.py:298
dynamicgraph::sot::talos_balance::FootForceDifferenceController::m_eps
double m_eps
Definition: foot-force-difference-controller.hh:107
sot_talos_balance.test.appli_dcm_zmp_control_ffdc.dfzAdmittance
int dfzAdmittance
Definition: appli_dcm_zmp_control_ffdc.py:293
dynamicgraph::sot::talos_balance::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(AdmittanceControllerEndEffector, "AdmittanceControllerEndEffector")
sot_talos_balance.test.appli_dcm_zmp_control_ffdc.gainStance
float gainStance
Definition: appli_dcm_zmp_control_ffdc.py:299
dynamicgraph::sot::talos_balance::DEFINE_SIGNAL_INNER_FUNCTION
DEFINE_SIGNAL_INNER_FUNCTION(w_force, dynamicgraph::Vector)
Definition: admittance-controller-end-effector.cpp:145
dynamicgraph::sot::talos_balance::FootForceDifferenceController::FootForceDifferenceController
EIGEN_MAKE_ALIGNED_OPERATOR_NEW FootForceDifferenceController(const std::string &name)
Definition: foot-force-difference-controller.cpp:57
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::FootForceDifferenceController::init
void init()
Definition: foot-force-difference-controller.cpp:118
INPUT_SIGNALS
#define INPUT_SIGNALS
Definition: foot-force-difference-controller.cpp:33