sot-torque-control  1.6.5
Collection of dynamic-graph entities aimed at implementing torque control on different robots.
device-torque-ctrl.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2017, Andrea Del Prete, LAAS-CNRS
3  *
4  */
5 
6 #ifndef _DeviceTorqueCtrl_H_
7 #define _DeviceTorqueCtrl_H_
8 
9 #include <pinocchio/fwd.hpp>
10 
11 // include pinocchio first
12 
13 #include <dynamic-graph/entity.h>
14 #include <dynamic-graph/linear-algebra.h>
15 #include <dynamic-graph/signal-ptr.h>
16 #include <dynamic-graph/signal.h>
17 
18 #include <Eigen/Cholesky>
19 #include <boost/random/mersenne_twister.hpp>
20 #include <boost/random/normal_distribution.hpp>
21 #include <boost/random/variate_generator.hpp>
22 #include <sot/core/abstract-sot-external-interface.hh>
23 #include <sot/core/device.hh>
24 #include <tsid/robots/robot-wrapper.hpp>
25 #include <tsid/tasks/task-se3-equality.hpp>
26 
27 /* HELPER */
28 #include <dynamic-graph/signal-helper.h>
29 
30 #include <sot/core/robot-utils.hh>
31 
32 namespace dgsot = dynamicgraph::sot;
33 
34 namespace dynamicgraph {
35 namespace sot {
36 namespace torque_control {
37 
59 class DeviceTorqueCtrl : public dgsot::Device {
60  public:
61  static const std::string CLASS_NAME;
62  static const double TIMESTEP_DEFAULT;
63  static const double FORCE_SENSOR_NOISE_STD_DEV;
64 
65  virtual const std::string& getClassName() const { return CLASS_NAME; }
66 
67  DeviceTorqueCtrl(std::string RobotName);
68  virtual ~DeviceTorqueCtrl();
69 
70  virtual void setStateSize(const unsigned int& size);
71  virtual void setState(const dynamicgraph::Vector& st);
72  virtual void setVelocity(const dynamicgraph::Vector& vel);
73  virtual void setControlInputType(const std::string& cit);
74 
75  virtual void init(const double& dt, const std::string& urdfFile);
76 
77  protected:
78  virtual void integrate(const double& dt);
80 
81  void sendMsg(const std::string& msg, MsgType t = MSG_TYPE_INFO,
82  const char* = "", int = 0) {
83  logger_.stream(t) << ("[DeviceTorqueCtrl] " + msg) << '\n';
84  }
85 
87  double timestep_;
89 
91  dynamicgraph::SignalPtr<dynamicgraph::Vector, int>* forcesSIN_[4];
92 
94  dynamicgraph::Signal<dynamicgraph::Vector, int> accelerometerSOUT_;
96  dynamicgraph::Signal<dynamicgraph::Vector, int> gyrometerSOUT_;
98  dynamicgraph::Signal<dynamicgraph::Vector, int> robotStateSOUT_;
100  dynamicgraph::Signal<dynamicgraph::Vector, int> jointsVelocitiesSOUT_;
102  dynamicgraph::Signal<dynamicgraph::Vector, int> jointsAccelerationsSOUT_;
104  dynamicgraph::Signal<dynamicgraph::Vector, int> currentSOUT_;
106  dynamicgraph::Signal<dynamicgraph::Vector, int> p_gainsSOUT_;
107  dynamicgraph::Signal<dynamicgraph::Vector, int> d_gainsSOUT_;
108 
109  DECLARE_SIGNAL_IN(kp_constraints, dynamicgraph::Vector);
110  DECLARE_SIGNAL_IN(kd_constraints, dynamicgraph::Vector);
111  DECLARE_SIGNAL_IN(rotor_inertias, dynamicgraph::Vector);
112  DECLARE_SIGNAL_IN(gear_ratios, dynamicgraph::Vector);
113 
115  dynamicgraph::Vector accelerometer_;
116  dynamicgraph::Vector gyrometer_;
117 
118  dynamicgraph::Vector base6d_encoders_;
119  dynamicgraph::Vector jointsVelocities_;
120  dynamicgraph::Vector jointsAccelerations_;
121 
122  dynamicgraph::Vector wrenches_[4];
123  dynamicgraph::Vector temp6_;
124 
126 
128  tsid::robots::RobotWrapper* m_robot;
129  pinocchio::Data* m_data;
130  tsid::tasks::TaskSE3Equality* m_contactRF;
131  tsid::tasks::TaskSE3Equality* m_contactLF;
132  unsigned int m_nk; // number of contact forces
133 
134  tsid::math::Vector m_q, m_v, m_dv, m_f;
135  tsid::math::Vector m_q_sot, m_v_sot, m_dv_sot;
136 
137  typedef Eigen::LDLT<Eigen::MatrixXd> Cholesky;
139  Eigen::MatrixXd m_K;
140  Eigen::VectorXd m_k;
141  Eigen::MatrixXd m_Jc;
142 
144  Eigen::JacobiSVD<Matrix> m_Jc_svd;
146  Vector m_dJcv;
147  Matrix m_Z;
148  Matrix m_ZMZ;
149  Eigen::LDLT<Matrix> m_ZMZ_chol;
150  Vector m_dv_c;
151  Vector m_dvBar;
152  Vector m_tau_np6;
153  int m_nj;
154 
155  typedef boost::mt11213b ENG; // uniform random number generator
156  typedef boost::normal_distribution<double> DIST; // Normal Distribution
157  typedef boost::variate_generator<ENG, DIST> GEN; // Variate generator
161 
162  RobotUtilShrPtr m_robot_util;
163 };
164 
165 } // end namespace torque_control
166 } // end namespace sot
167 } // end namespace dynamicgraph
168 #endif /* DevicePosCtrl*/
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::forcesSIN_
dynamicgraph::SignalPtr< dynamicgraph::Vector, int > * forcesSIN_[4]
input force sensor values
Definition: device-torque-ctrl.hh:91
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_nk
unsigned int m_nk
Definition: device-torque-ctrl.hh:132
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::normalRandomNumberGenerator_
GEN normalRandomNumberGenerator_
Definition: device-torque-ctrl.hh:160
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_contactLF
tsid::tasks::TaskSE3Equality * m_contactLF
Definition: device-torque-ctrl.hh:131
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::init
virtual void init(const double &dt, const std::string &urdfFile)
Definition: device-torque-ctrl.cpp:122
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_Jc_svd
Eigen::JacobiSVD< Matrix > m_Jc_svd
Definition: device-torque-ctrl.hh:145
dynamicgraph
to read text file
Definition: treeview.dox:22
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_f
tsid::math::Vector m_f
Definition: device-torque-ctrl.hh:134
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::setStateSize
virtual void setStateSize(const unsigned int &size)
Definition: device-torque-ctrl.cpp:189
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::jointsVelocitiesSOUT_
dynamicgraph::Signal< dynamicgraph::Vector, int > jointsVelocitiesSOUT_
joints' velocities computed by the integrator
Definition: device-torque-ctrl.hh:100
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::jointsVelocities_
dynamicgraph::Vector jointsVelocities_
base 6d pose + joints' angles
Definition: device-torque-ctrl.hh:119
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::ENG
boost::mt11213b ENG
number of joints
Definition: device-torque-ctrl.hh:155
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_ZMZ_chol
Eigen::LDLT< Matrix > m_ZMZ_chol
projected mass matrix: Z_c^T*M*Z_c
Definition: device-torque-ctrl.hh:149
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::computeForwardDynamics
void computeForwardDynamics()
Definition: device-torque-ctrl.cpp:252
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_nj
int m_nj
Definition: device-torque-ctrl.hh:153
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_q
tsid::math::Vector m_q
Definition: device-torque-ctrl.hh:134
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::getClassName
virtual const std::string & getClassName() const
Definition: device-torque-ctrl.hh:65
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::setControlInputType
virtual void setControlInputType(const std::string &cit)
Definition: device-torque-ctrl.cpp:243
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_Jc
Eigen::MatrixXd m_Jc
Definition: device-torque-ctrl.hh:141
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::DeviceTorqueCtrl
DeviceTorqueCtrl(std::string RobotName)
Definition: device-torque-ctrl.cpp:32
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::setState
virtual void setState(const dynamicgraph::Vector &st)
Definition: device-torque-ctrl.cpp:204
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::jointsAccelerations_
dynamicgraph::Vector jointsAccelerations_
joints' velocities
Definition: device-torque-ctrl.hh:120
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_ZMZ
Matrix m_ZMZ
base of constraint null space
Definition: device-torque-ctrl.hh:148
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::DIST
boost::normal_distribution< double > DIST
Definition: device-torque-ctrl.hh:156
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_dv
tsid::math::Vector m_dv
Definition: device-torque-ctrl.hh:134
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::randomNumberGenerator_
ENG randomNumberGenerator_
Definition: device-torque-ctrl.hh:158
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::wrenches_
dynamicgraph::Vector wrenches_[4]
joints' accelerations
Definition: device-torque-ctrl.hh:122
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::DECLARE_SIGNAL_IN
DECLARE_SIGNAL_IN(kp_constraints, dynamicgraph::Vector)
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_K_chol
Cholesky m_K_chol
Definition: device-torque-ctrl.hh:138
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::TIMESTEP_DEFAULT
static const double TIMESTEP_DEFAULT
Definition: device-torque-ctrl.hh:62
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_q_sot
tsid::math::Vector m_q_sot
Definition: device-torque-ctrl.hh:135
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::accelerometer_
dynamicgraph::Vector accelerometer_
Intermediate variables to avoid allocation during control.
Definition: device-torque-ctrl.hh:115
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_robot
tsid::robots::RobotWrapper * m_robot
robot geometric/inertial data
Definition: device-torque-ctrl.hh:128
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::accelerometerSOUT_
dynamicgraph::Signal< dynamicgraph::Vector, int > accelerometerSOUT_
Accelerations measured by accelerometers.
Definition: device-torque-ctrl.hh:94
torque_control
Definition: __init__.py:1
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_v
tsid::math::Vector m_v
Definition: device-torque-ctrl.hh:134
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::d_gainsSOUT_
dynamicgraph::Signal< dynamicgraph::Vector, int > d_gainsSOUT_
Definition: device-torque-ctrl.hh:107
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_tau_np6
Vector m_tau_np6
solution of Jc*dv=-dJc*v
Definition: device-torque-ctrl.hh:152
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::jointsAccelerationsSOUT_
dynamicgraph::Signal< dynamicgraph::Vector, int > jointsAccelerationsSOUT_
joints' accelerations computed by the integrator
Definition: device-torque-ctrl.hh:102
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_K
Eigen::MatrixXd m_K
cholesky decomposition of the K matrix
Definition: device-torque-ctrl.hh:139
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::sendMsg
void sendMsg(const std::string &msg, MsgType t=MSG_TYPE_INFO, const char *="", int=0)
Definition: device-torque-ctrl.hh:81
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_Z
Matrix m_Z
Definition: device-torque-ctrl.hh:147
dynamicgraph::sot::torque_control::DeviceTorqueCtrl
Definition: device-torque-ctrl.hh:59
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::setVelocity
virtual void setVelocity(const dynamicgraph::Vector &vel)
Definition: device-torque-ctrl.cpp:232
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_robot_util
RobotUtilShrPtr m_robot_util
Definition: device-torque-ctrl.hh:162
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_dvBar
Vector m_dvBar
constrained accelerations
Definition: device-torque-ctrl.hh:151
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::gyrometer_
dynamicgraph::Vector gyrometer_
Definition: device-torque-ctrl.hh:116
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_data
pinocchio::Data * m_data
Definition: device-torque-ctrl.hh:129
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::~DeviceTorqueCtrl
virtual ~DeviceTorqueCtrl()
Definition: device-torque-ctrl.cpp:120
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::CLASS_NAME
static const std::string CLASS_NAME
Definition: device-torque-ctrl.hh:61
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::timestep_
double timestep_
Current integration step.
Definition: device-torque-ctrl.hh:87
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::p_gainsSOUT_
dynamicgraph::Signal< dynamicgraph::Vector, int > p_gainsSOUT_
proportional and derivative position-control gains
Definition: device-torque-ctrl.hh:106
dynamicgraph::sot
Definition: treeview.dox:23
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::integrate
virtual void integrate(const double &dt)
Definition: device-torque-ctrl.cpp:326
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::base6d_encoders_
dynamicgraph::Vector base6d_encoders_
Definition: device-torque-ctrl.hh:118
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_numericalDamping
double m_numericalDamping
constraint Jacobian
Definition: device-torque-ctrl.hh:143
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::currentSOUT_
dynamicgraph::Signal< dynamicgraph::Vector, int > currentSOUT_
motor currents
Definition: device-torque-ctrl.hh:104
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::GEN
boost::variate_generator< ENG, DIST > GEN
Definition: device-torque-ctrl.hh:157
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::normalDistribution_
DIST normalDistribution_
Definition: device-torque-ctrl.hh:159
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::FORCE_SENSOR_NOISE_STD_DEV
static const double FORCE_SENSOR_NOISE_STD_DEV
Definition: device-torque-ctrl.hh:63
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_dv_sot
tsid::math::Vector m_dv_sot
Definition: device-torque-ctrl.hh:135
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_k
Eigen::VectorXd m_k
Definition: device-torque-ctrl.hh:140
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::robotStateSOUT_
dynamicgraph::Signal< dynamicgraph::Vector, int > robotStateSOUT_
base 6d pose + joints' angles measured by encoders
Definition: device-torque-ctrl.hh:98
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_isTorqueControlled
bool m_isTorqueControlled
Definition: device-torque-ctrl.hh:125
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::temp6_
dynamicgraph::Vector temp6_
Definition: device-torque-ctrl.hh:123
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_initSucceeded
bool m_initSucceeded
Definition: device-torque-ctrl.hh:88
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_contactRF
tsid::tasks::TaskSE3Equality * m_contactRF
Definition: device-torque-ctrl.hh:130
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::Cholesky
Eigen::LDLT< Eigen::MatrixXd > Cholesky
Definition: device-torque-ctrl.hh:137
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_dJcv
Vector m_dJcv
svd of the constraint matrix
Definition: device-torque-ctrl.hh:146
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_dv_c
Vector m_dv_c
Cholesky decomposition of _ZMZ.
Definition: device-torque-ctrl.hh:150
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::m_v_sot
tsid::math::Vector m_v_sot
Definition: device-torque-ctrl.hh:135
dynamicgraph::sot::torque_control::DeviceTorqueCtrl::gyrometerSOUT_
dynamicgraph::Signal< dynamicgraph::Vector, int > gyrometerSOUT_
Rotation velocity measured by gyrometers.
Definition: device-torque-ctrl.hh:96