sot-talos-balance  1.6.0
simple-controller-6d.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_CONTROLLER_6D_DX_REF_COMPUTATION "SimpleController6d: v_ref computation "
38 
39 #define INPUT_SIGNALS m_KpSIN << m_xSIN << m_x_desSIN
40 
41 #define OUTPUT_SIGNALS m_v_refSOUT
42 
45  typedef SimpleController6d EntityClassName;
46 
47  /* --- DG FACTORY ---------------------------------------------------- */
48  DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(SimpleController6d,
49  "SimpleController6d");
50 
51  /* ------------------------------------------------------------------- */
52  /* --- CONSTRUCTION -------------------------------------------------- */
53  /* ------------------------------------------------------------------- */
54  SimpleController6d::SimpleController6d(const std::string& name)
55  : Entity(name)
56  , CONSTRUCT_SIGNAL_IN(Kp, dynamicgraph::Vector)
57  , CONSTRUCT_SIGNAL_IN(x, dynamicgraph::Vector)
58  , CONSTRUCT_SIGNAL_IN(x_des, dynamicgraph::Vector)
59  , CONSTRUCT_SIGNAL_OUT(v_ref, dynamicgraph::Vector, INPUT_SIGNALS)
60  , m_initSucceeded(false)
61  {
62  Entity::signalRegistration( INPUT_SIGNALS << OUTPUT_SIGNALS );
63 
64  /* Commands. */
65  addCommand("init", makeCommandVoid0(*this, &SimpleController6d::init, docCommandVoid0("Initialize the entity.")));
66  }
67 
69  {
70  m_initSucceeded = true;
71  }
72 
73  template <typename Derived>
74  Eigen::Matrix3d SimpleController6d::skew(const Eigen::MatrixBase<Derived> & v)
75  {
76  Eigen::Matrix3d M;
77  M << 0, -v[2], v[1],
78  v[2], 0, -v[0],
79  -v[1], v[0], 0;
80  return M;
81  }
82 
83  /* ------------------------------------------------------------------- */
84  /* --- SIGNALS ------------------------------------------------------- */
85  /* ------------------------------------------------------------------- */
86 
88  {
89  if(!m_initSucceeded)
90  {
91  SEND_WARNING_STREAM_MSG("Cannot compute signal v_ref before initialization!");
92  return s;
93  }
94 
96 
97  const Vector & Kp = m_KpSIN(iter);
98 
99  const MatrixHomogeneous & x = m_xSIN(iter);
100  const MatrixHomogeneous & x_des = m_x_desSIN(iter);
101 
102  //const MatrixHomogeneous & x_err = x_des * x.inverse();
103 
104  const Eigen::Vector3d e_O = 0.5*(x.linear().col(0).cross(x_des.linear().col(0))
105  + x.linear().col(1).cross(x_des.linear().col(1))
106  + x.linear().col(2).cross(x_des.linear().col(2))
107  );
108 
109  const Eigen::Matrix3d L = -0.5*(skew(x_des.linear().col(0))*skew(x.linear().col(0))
110  + skew(x_des.linear().col(1))*skew(x.linear().col(1))
111  + skew(x_des.linear().col(2))*skew(x.linear().col(2))
112  );
113 
114  Eigen::Matrix<double,6,1> dv_ref;
115 
116  //dv_ref.head<3>() = Kp.head<3>().cwiseProduct(x_err.translation());
117  dv_ref.head<3>() = x.linear().transpose() * Kp.head<3>().cwiseProduct(x_des.translation()-x.translation());
118 
119  dv_ref.tail<3>() = x.linear().transpose() * L.inverse() * Kp.tail<3>().cwiseProduct(e_O);
120 
121  s = dv_ref;
122 
124 
125  return s;
126  }
127 
128  /* --- COMMANDS ---------------------------------------------------------- */
129 
130  /* ------------------------------------------------------------------- */
131  /* --- ENTITY -------------------------------------------------------- */
132  /* ------------------------------------------------------------------- */
133 
134  void SimpleController6d::display(std::ostream& os) const
135  {
136  os << "SimpleController6d " << getName();
137  try
138  {
139  getProfiler().report_all(3, os);
140  }
141  catch (ExceptionSignal e) {}
142  }
143  } // namespace talos_balance
144  } // namespace sot
145 } // namespace dynamicgraph
146 
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: math/fwd.hh:40
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(AdmittanceControllerEndEffector, "AdmittanceControllerEndEffector")
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SimpleController6d(const std::string &name)
#define INPUT_SIGNALS
Eigen::Matrix3d skew(const Eigen::MatrixBase< Derived > &v)
#define OUTPUT_SIGNALS
#define PROFILE_SIMPLE_CONTROLLER_6D_DX_REF_COMPUTATION