sot-talos-balance  1.7.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 << m_v_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_IN(v_des, dynamicgraph::Vector)
60  , CONSTRUCT_SIGNAL_OUT(v_ref, dynamicgraph::Vector, INPUT_SIGNALS)
61  , m_initSucceeded(false)
62  {
63  Entity::signalRegistration( INPUT_SIGNALS << OUTPUT_SIGNALS );
64 
65  /* Commands. */
66  addCommand("init", makeCommandVoid0(*this, &SimpleController6d::init, docCommandVoid0("Initialize the entity.")));
67  }
68 
70  {
71  m_initSucceeded = true;
72  }
73 
74  template <typename Derived>
75  Eigen::Matrix3d SimpleController6d::skew(const Eigen::MatrixBase<Derived> & v)
76  {
77  Eigen::Matrix3d M;
78  M << 0, -v[2], v[1],
79  v[2], 0, -v[0],
80  -v[1], v[0], 0;
81  return M;
82  }
83 
84  /* ------------------------------------------------------------------- */
85  /* --- SIGNALS ------------------------------------------------------- */
86  /* ------------------------------------------------------------------- */
87 
89  {
90  if(!m_initSucceeded)
91  {
92  SEND_WARNING_STREAM_MSG("Cannot compute signal v_ref before initialization!");
93  return s;
94  }
95  if(s.size()!=6)
96  s.resize(6);
97 
99 
100  const Vector & Kp = m_KpSIN(iter);
101 
102  const MatrixHomogeneous & x = m_xSIN(iter);
103  const MatrixHomogeneous & x_des = m_x_desSIN(iter);
104 
105  //const MatrixHomogeneous & x_err = x_des * x.inverse();
106 
107  const Eigen::Vector3d e_O = 0.5*(x.linear().col(0).cross(x_des.linear().col(0))
108  + x.linear().col(1).cross(x_des.linear().col(1))
109  + x.linear().col(2).cross(x_des.linear().col(2))
110  );
111 
112  const Eigen::Matrix3d L = -0.5*(skew(x_des.linear().col(0))*skew(x.linear().col(0))
113  + skew(x_des.linear().col(1))*skew(x.linear().col(1))
114  + skew(x_des.linear().col(2))*skew(x.linear().col(2))
115  );
116 
117  Eigen::Matrix<double,6,1> dv_ref;
118 
119  //dv_ref.head<3>() = Kp.head<3>().cwiseProduct(x_err.translation());
120  dv_ref.head<3>() = x.linear().transpose() * Kp.head<3>().cwiseProduct(x_des.translation()-x.translation());
121 
122  dv_ref.tail<3>() = x.linear().transpose() * L.inverse() * Kp.tail<3>().cwiseProduct(e_O);
123 
124  if(m_v_desSIN.isPlugged()) {
125  const dynamicgraph::Vector & v_des = m_v_desSIN(iter);
126  s = v_des + dv_ref;
127  }
128  else {
129  s = dv_ref;
130  }
131 
133 
134  return s;
135  }
136 
137  /* --- COMMANDS ---------------------------------------------------------- */
138 
139  /* ------------------------------------------------------------------- */
140  /* --- ENTITY -------------------------------------------------------- */
141  /* ------------------------------------------------------------------- */
142 
143  void SimpleController6d::display(std::ostream& os) const
144  {
145  os << "SimpleController6d " << getName();
146  try
147  {
148  getProfiler().report_all(3, os);
149  }
150  catch (ExceptionSignal e) {}
151  }
152  } // namespace talos_balance
153  } // namespace sot
154 } // namespace dynamicgraph
155 
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