sot-talos-balance  1.7.0
joint-position-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 <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_JOINTPOSITIONCONTROLLER_DQREF_COMPUTATION "JointPositionController: dqRef computation "
38 
39 #define INPUT_SIGNALS m_KpSIN << m_stateSIN << m_qDesSIN << m_dqDesSIN
40 
41 #define OUTPUT_SIGNALS m_dqRefSOUT
42 
45  typedef JointPositionController EntityClassName;
46 
47  /* --- DG FACTORY ---------------------------------------------------- */
48  DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(JointPositionController,
49  "JointPositionController");
50 
51  /* ------------------------------------------------------------------- */
52  /* --- CONSTRUCTION -------------------------------------------------- */
53  /* ------------------------------------------------------------------- */
55  : Entity(name)
56  , CONSTRUCT_SIGNAL_IN(Kp, dynamicgraph::Vector)
57  , CONSTRUCT_SIGNAL_IN(state, dynamicgraph::Vector)
58  , CONSTRUCT_SIGNAL_IN(qDes, dynamicgraph::Vector)
59  , CONSTRUCT_SIGNAL_IN(dqDes, dynamicgraph::Vector)
60  , CONSTRUCT_SIGNAL_OUT(dqRef, dynamicgraph::Vector, INPUT_SIGNALS)
61  , m_initSucceeded(false)
62  {
63  Entity::signalRegistration( INPUT_SIGNALS << OUTPUT_SIGNALS );
64 
65  /* Commands. */
66  addCommand("init", makeCommandVoid1(*this, &JointPositionController::init, docCommandVoid1("Initialize the entity.","Control gains")));
67  }
68 
69  void JointPositionController::init(const unsigned & n)
70  {
71  if(n<1)
72  return SEND_MSG("n must be at least 1", MSG_TYPE_ERROR);
73  if(!m_KpSIN.isPlugged())
74  return SEND_MSG("Init failed: signal Kp is not plugged", MSG_TYPE_ERROR);
75  if(!m_stateSIN.isPlugged())
76  return SEND_MSG("Init failed: signal q is not plugged", MSG_TYPE_ERROR);
77  if(!m_qDesSIN.isPlugged())
78  return SEND_MSG("Init failed: signal qDes is not plugged", MSG_TYPE_ERROR);
79  if(!m_dqDesSIN.isPlugged())
80  return SEND_MSG("Init failed: signal dqDes is not plugged", MSG_TYPE_ERROR);
81 
82  m_n = n;
83  m_initSucceeded = true;
84  }
85 
86  /* ------------------------------------------------------------------- */
87  /* --- SIGNALS ------------------------------------------------------- */
88  /* ------------------------------------------------------------------- */
89 
91  {
92  if(!m_initSucceeded)
93  {
94  SEND_WARNING_STREAM_MSG("Cannot compute signal dqRef before initialization!");
95  return s;
96  }
97  if(s.size()!=m_n)
98  s.resize(m_n);
99 
101 
102  const Vector & state = m_stateSIN(iter);
103  const Vector & qDes = m_qDesSIN(iter);
104  const Vector & dqDes = m_dqDesSIN(iter);
105  const Vector & Kp = m_KpSIN(iter);
106 
107  assert(state.size()==m_n+6 && "Unexpected size of signal state");
108  assert(qDes.size()==m_n && "Unexpected size of signal qDes");
109  assert(dqDes.size()==m_n && "Unexpected size of signal dqDes");
110  assert(Kp.size()==m_n && "Unexpected size of signal Kp");
111 
112  s = dqDes + Kp.cwiseProduct(qDes - state.tail(m_n));
113 
115 
116  return s;
117  }
118 
119 
120  /* --- COMMANDS ---------------------------------------------------------- */
121 
122  /* ------------------------------------------------------------------- */
123  /* --- ENTITY -------------------------------------------------------- */
124  /* ------------------------------------------------------------------- */
125 
126  void JointPositionController::display(std::ostream& os) const
127  {
128  os << "JointPositionController " << getName();
129  try
130  {
131  getProfiler().report_all(3, os);
132  }
133  catch (ExceptionSignal e) {}
134  }
135  } // namespace talos_balance
136  } // namespace sot
137 } // namespace dynamicgraph
138 
#define INPUT_SIGNALS
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: math/fwd.hh:40
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(AdmittanceControllerEndEffector, "AdmittanceControllerEndEffector")
#define OUTPUT_SIGNALS
#define PROFILE_JOINTPOSITIONCONTROLLER_DQREF_COMPUTATION
EIGEN_MAKE_ALIGNED_OPERATOR_NEW JointPositionController(const std::string &name)