integrator-force-exact.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2010,
3  * François Bleibel,
4  * Olivier Stasse,
5  *
6  * CNRS/AIST
7  *
8  */
9 
10 #include <dynamic-graph/factory.h>
12 
13 #include <sot/core/debug.hh>
15 
16 using namespace dynamicgraph::sot;
17 using namespace dynamicgraph;
19  "IntegratorForceExact");
20 
23  sotDEBUGIN(5);
24 
25  velocitySOUT.setFunction(
26  boost::bind(&IntegratorForceExact::computeVelocityExact, this, _1, _2));
28 
29  sotDEBUGOUT(5);
30 }
31 
33  sotDEBUGIN(5);
34 
35  sotDEBUGOUT(5);
36  return;
37 }
38 
39 /* --- EIGEN VALUE ---------------------------------------------------------- */
40 /* --- EIGEN VALUE ---------------------------------------------------------- */
41 /* --- EIGEN VALUE ---------------------------------------------------------- */
42 
43 extern "C" {
44 void dgeev_(const char* jobvl, const char* jobvr, const int* n, double* a,
45  const int* lda, double* wr, double* wi, double* vl, const int* ldvl,
46  double* vr, const int* ldvr, double* work, const int* lwork,
47  int* info);
48 }
49 
50 int geev(Matrix& a, Eigen::VectorXcd& w, Matrix& vl2, Matrix& vr2) {
51  char jobvl = 'V';
52  char jobvr = 'V';
53  int const n = (int)a.rows();
54 
55  Vector wr(n);
56  Vector wi(n);
57  double* vl_real = MRAWDATA(vl2);
58  const int ldvl = (int)vl2.rows();
59  double* vr_real = MRAWDATA(vr2);
60  const int ldvr = (int)vr2.rows();
61 
62  // workspace query
63  int lwork = -1;
64  double work_temp;
65  int result = 0;
66  dgeev_(&jobvl, &jobvr, &n, MRAWDATA(a), &n, MRAWDATA(wr), MRAWDATA(wi),
67  vl_real, &ldvl, vr_real, &ldvr, &work_temp, &lwork, &result);
68  if (result != 0) return result;
69 
70  lwork = (int)work_temp;
71  Vector work(lwork);
72  dgeev_(&jobvl, &jobvr, &n, MRAWDATA(a), &n, MRAWDATA(wr), MRAWDATA(wi),
73  vl_real, &ldvl, vr_real, &ldvr, MRAWDATA(work), &lwork, &result);
74 
75  for (int i = 0; i < n; i++) w[i] = std::complex<double>(wr[i], wi[i]);
76 
77  return result;
78 }
79 
81  dynamicgraph::Vector& eig) {
82  long int SIZE = M.cols();
83  Matrix Y(M);
84  Eigen::VectorXcd evals(SIZE);
85  Matrix vl(SIZE, SIZE);
86  Matrix vr(SIZE, SIZE);
87 
88  const int errCode = geev(Y, evals, vl, vr);
89  if (errCode < 0) {
91  "Invalid argument to geev", "");
92  } else if (errCode > 0) {
94  "No convergence for given matrix", "");
95  }
96 
97  P.resize(SIZE, SIZE);
98  eig.resize(SIZE);
99  for (unsigned int i = 0; i < SIZE; ++i) {
100  for (unsigned int j = 0; j < SIZE; ++j) {
101  P(i, j) = vr(i, j);
102  }
103  eig(i) = evals(i).real();
104  if (fabsf(static_cast<float>(evals(i).imag())) > 1e-5) {
106  "Error imaginary part not null. ",
107  "(at position %d: %f)", i, evals(i).imag());
108  }
109  }
110  return;
111 }
112 
113 static void expMatrix(const dynamicgraph::Matrix& MiB,
114  dynamicgraph::Matrix& Mexp) {
115  long int SIZE = MiB.cols();
116 
117  dynamicgraph::Matrix Pmib(MiB.cols(), MiB.cols());
118  dynamicgraph::Vector eig_mib(MiB.cols());
119  eigenDecomp(MiB, Pmib, eig_mib);
120  sotDEBUG(45) << "V = " << Pmib;
121  sotDEBUG(45) << "d = " << eig_mib;
122 
123  dynamicgraph::Matrix Pinv(SIZE, SIZE);
124  Pinv = Pmib.inverse();
125  sotDEBUG(45) << "Vinv = " << Pinv;
126 
127  Mexp.resize(SIZE, SIZE);
128  for (unsigned int i = 0; i < SIZE; ++i)
129  for (unsigned int j = 0; j < SIZE; ++j) Pmib(i, j) *= exp(eig_mib(j));
130  Mexp = Pmib * Pinv;
131 
132  sotDEBUG(45) << "expMiB = " << Mexp;
133  return;
134 }
135 
136 /* --- SIGNALS -------------------------------------------------------------- */
137 /* --- SIGNALS -------------------------------------------------------------- */
138 /* --- SIGNALS -------------------------------------------------------------- */
139 
140 /* The derivative of the signal is such that: M v_dot + B v = f. We deduce:
141  * v_dot = M^-1 (f - Bv)
142  * Using Exact method:
143  * dv = exp( M^-1.B.t) ( v0-B^-1.f ) + B^-1.f
144  */
145 
147  dynamicgraph::Vector& res, const sigtime_t& time) {
148  sotDEBUGIN(15);
149 
150  const dynamicgraph::Vector& force = forceSIN(time);
151  const dynamicgraph::Matrix& massInverse = massInverseSIN(time);
152  const dynamicgraph::Matrix& friction = frictionSIN(time);
153  long int nf = force.size(), nv = friction.cols();
154  res.resize(nv);
155  res.setZero();
156 
157  if (!velocityPrecSIN) {
159  zero.fill(0);
161  }
162  const dynamicgraph::Vector& vel = velocityPrecSIN(time);
163  double& dt = this->IntegratorForce::timeStep; // this is &
164 
165  sotDEBUG(15) << "force = " << force;
166  sotDEBUG(15) << "vel = " << vel;
167  sotDEBUG(25) << "Mi = " << massInverse;
168  sotDEBUG(25) << "B = " << friction;
169  sotDEBUG(25) << "dt = " << dt << std::endl;
170 
171  dynamicgraph::Matrix MiB(nv, nv);
172  MiB = massInverse * friction;
173  sotDEBUG(25) << "MiB = " << MiB;
174 
175  dynamicgraph::Matrix MiBexp(nv, nv);
176  MiB *= -dt;
177  expMatrix(MiB, MiBexp);
178  sotDEBUG(25) << "expMiB = " << MiBexp;
179 
180  dynamicgraph::Matrix Binv(nv, nv);
181  Binv = friction.inverse();
182  dynamicgraph::Vector Bif(nf);
183  Bif = Binv * force;
184  sotDEBUG(25) << "Binv = " << Binv;
185  sotDEBUG(25) << "Bif = " << Bif;
186 
187  dynamicgraph::Vector v0_bif = vel;
188  v0_bif -= Bif;
189  sotDEBUG(25) << "Kst = " << v0_bif;
190 
191  res.resize(MiBexp.rows());
192  res = MiBexp * v0_bif;
193 
194  res += Bif;
196  sotDEBUG(25) << "vfin = " << res;
197 
198  sotDEBUGOUT(15);
199  return res;
200 }
geev
int geev(Matrix &a, Eigen::VectorXcd &w, Matrix &vl2, Matrix &vr2)
Definition: integrator-force-exact.cpp:50
exception-dynamic.hh
dynamicgraph
exp
def exp(x)
dynamicgraph::sot::ExceptionDynamic
nv
int nv(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
dynamicgraph::sot::ExceptionDynamic::INTEGRATION
INTEGRATION
i
int i
dynamicgraph::sot::IntegratorForceExact::IntegratorForceExact
IntegratorForceExact(const std::string &name)
Definition: integrator-force-exact.cpp:21
expMatrix
static void expMatrix(const dynamicgraph::Matrix &MiB, dynamicgraph::Matrix &Mexp)
Definition: integrator-force-exact.cpp:113
debug.hh
Matrix
Eigen::MatrixXd Matrix
dynamicgraph::sot::IntegratorForce::frictionSIN
dg::SignalPtr< dynamicgraph::Matrix, sigtime_t > frictionSIN
Definition: integrator-force.h:68
imag
Eigen::Matrix< typename ComplexMatrix::RealScalar, ComplexMatrix::RowsAtCompileTime, ComplexMatrix::ColsAtCompileTime, ComplexMatrix::Options > imag(const Eigen::MatrixBase< ComplexMatrix > &complex_mat)
dynamic_pinocchio.kine_romeo.dt
int dt
Definition: kine_romeo.py:37
dynamicgraph::sot::IntegratorForceExact::computeVelocityExact
dynamicgraph::Vector & computeVelocityExact(dynamicgraph::Vector &res, const sigtime_t &time)
Definition: integrator-force-exact.cpp:146
dgeev_
void dgeev_(const char *jobvl, const char *jobvr, const int *n, double *a, const int *lda, double *wr, double *wi, double *vl, const int *ldvl, double *vr, const int *ldvr, double *work, const int *lwork, int *info)
dynamicgraph::sot::IntegratorForce::timeStep
double timeStep
Definition: integrator-force.h:58
res
res
eigenDecomp
static void eigenDecomp(const dynamicgraph::Matrix &M, dynamicgraph::Matrix &P, dynamicgraph::Vector &eig)
Definition: integrator-force-exact.cpp:80
sotDEBUGOUT
#define sotDEBUGOUT(level)
P
P
sotDEBUGIN
#define sotDEBUGIN(level)
integrator-force-exact.h
M
M
dynamicgraph::SignalTimeDependent::removeDependency
virtual void removeDependency(const SignalBase< Time > &signal)
Vector
Eigen::VectorXd Vector
dynamicgraph::sot::IntegratorForce::velocityDerivativeSOUT
dg::SignalTimeDependent< dynamicgraph::Vector, sigtime_t > velocityDerivativeSOUT
Definition: integrator-force.h:74
a
Vec3f a
Y
Y
dynamicgraph::sot::IntegratorForceExact
Definition: integrator-force-exact.h:53
dynamicgraph::sot
dynamicgraph::sot::IntegratorForce::velocityPrecSIN
dg::SignalPtr< dynamicgraph::Vector, sigtime_t > velocityPrecSIN
Definition: integrator-force.h:72
SOT_THROW
#define SOT_THROW
zero
def zero(n)
dynamicgraph::sot::IntegratorForce
Definition: integrator-force.h:52
dynamicgraph::sot::IntegratorForce::forceSIN
dg::SignalPtr< dynamicgraph::Vector, sigtime_t > forceSIN
Definition: integrator-force.h:66
n
Vec3f n
dynamicgraph::sot::IntegratorForce::massInverseSIN
dg::SignalPtr< dynamicgraph::Matrix, sigtime_t > massInverseSIN
Definition: integrator-force.h:67
dynamicgraph::sot::IntegratorForceExact::~IntegratorForceExact
virtual ~IntegratorForceExact(void)
Definition: integrator-force-exact.cpp:32
MRAWDATA
#define MRAWDATA(x)
compile.name
name
Definition: compile.py:22
dynamicgraph::sot::IntegratorForce::velocitySOUT
dg::SignalTimeDependent< dynamicgraph::Vector, sigtime_t > velocitySOUT
Definition: integrator-force.h:75
dynamicgraph::sot::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(DoubleConstant, "DoubleConstant")
sotDEBUG
#define sotDEBUG(level)


sot-dynamic-pinocchio
Author(s): Olivier Stasse
autogenerated on Fri Jul 28 2023 02:10:01