tsid 1.9.0
Efficient Task Space Inverse Dynamics for Multi-body Systems based on Pinocchio
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1//
2// Copyright (c) 2017 CNRS
3//
4
5#ifndef __invdyn_math_utils_hpp__
6#define __invdyn_math_utils_hpp__
7
8#include "tsid/math/fwd.hpp"
9
10#include <pinocchio/spatial/se3.hpp>
11#include <pinocchio/spatial/explog.hpp>
12
13#include <iostream>
14#include <fstream>
15#include <vector>
16
17#define PRINT_VECTOR(a) \
18 std::cout << #a << "(" << a.rows() << "x" << a.cols() \
19 << "): " << a.transpose().format(math::CleanFmt) << std::endl
20#define PRINT_MATRIX(a) \
21 std::cout << #a << "(" << a.rows() << "x" << a.cols() << "):\n" \
22 << a.format(math::CleanFmt) << std::endl
23
24namespace tsid {
25template <typename T>
26std::string toString(const T& v) {
27 std::stringstream ss;
28 ss << v;
29 return ss.str();
30}
31
32template <typename T>
33std::string toString(const std::vector<T>& v,
34 const std::string separator = ", ") {
35 std::stringstream ss;
36 for (int i = 0; i < v.size() - 1; i++) ss << v[i] << separator;
37 ss << v[v.size() - 1];
38 return ss.str();
39}
40
41template <typename T, int n>
42std::string toString(const Eigen::MatrixBase<T>& v,
43 const std::string separator = ", ") {
44 if (v.rows() > v.cols()) return toString(v.transpose(), separator);
45 std::stringstream ss;
46 ss << v;
47 return ss.str();
48}
49} // namespace tsid
50
51namespace tsid {
52namespace math {
53static const Eigen::IOFormat CleanFmt(1, 0, ", ", "\n", "[", "]");
54
65static const Eigen::IOFormat matlabPrintFormat(Eigen::FullPrecision,
66 Eigen::DontAlignCols, " ", ";\n",
67 "", "", "[", "];");
68
72void SE3ToXYZQUAT(const pinocchio::SE3& M, RefVector xyzQuat);
73
78void SE3ToVector(const pinocchio::SE3& M, RefVector vec);
79
80void vectorToSE3(RefVector vec, pinocchio::SE3& M);
81
82void errorInSE3(const pinocchio::SE3& M, const pinocchio::SE3& Mdes,
83 pinocchio::Motion& error);
84
85void solveWithDampingFromSvd(Eigen::JacobiSVD<Eigen::MatrixXd>& svd,
87 double damping = 0.0);
88
90 double damping = 0.0);
91
92void pseudoInverse(ConstRefMatrix A, RefMatrix Apinv, double tolerance,
93 unsigned int computationOptions = Eigen::ComputeThinU |
94 Eigen::ComputeThinV);
95
97 Eigen::JacobiSVD<Eigen::MatrixXd>& svdDecomposition,
98 RefMatrix Apinv, double tolerance,
99 unsigned int computationOptions);
100
102 Eigen::JacobiSVD<Eigen::MatrixXd>& svdDecomposition,
103 RefMatrix Apinv, double tolerance, double* nullSpaceBasisOfA,
104 int& nullSpaceRows, int& nullSpaceCols,
105 unsigned int computationOptions);
106
108 Eigen::JacobiSVD<Eigen::MatrixXd>& svdDecomposition,
109 RefMatrix Apinv, double tolerance,
110 double dampingFactor,
111 unsigned int computationOptions = Eigen::ComputeThinU |
112 Eigen::ComputeThinV,
113 double* nullSpaceBasisOfA = 0, int* nullSpaceRows = 0,
114 int* nullSpaceCols = 0);
115
117 const Eigen::JacobiSVD<Eigen::MatrixXd>& svdDecomposition, double tolerance,
118 double* nullSpaceBasisMatrix, int& rows, int& cols);
119
121 const Eigen::JacobiSVD<Eigen::MatrixXd>& svdDecomposition, int rank,
122 double* nullSpaceBasisMatrix, int& rows, int& cols);
123
124template <typename Derived>
125inline bool isFinite(const Eigen::MatrixBase<Derived>& x) {
126 return ((x - x).array() == (x - x).array()).all();
127}
128
129template <typename Derived>
130inline bool is_nan(const Eigen::MatrixBase<Derived>& x) {
131 return ((x.array() == x.array())).all();
132}
133
137template <class Matrix>
138bool writeMatrixToFile(const std::string& filename,
139 const Eigen::MatrixBase<Matrix>& matrix) {
140 typedef typename Matrix::Index Index;
141 typedef typename Matrix::Scalar Scalar;
142
143 std::ofstream out(filename.c_str(),
144 std::ios::out | std::ios::binary | std::ios::trunc);
145 if (!out.is_open()) return false;
146 Index rows = matrix.rows(), cols = matrix.cols();
147 out.write((char*)(&rows), sizeof(Index));
148 out.write((char*)(&cols), sizeof(Index));
149 out.write((char*)matrix.data(), rows * cols * sizeof(Scalar));
150 out.close();
151 return true;
152}
153
157template <class Matrix>
158bool readMatrixFromFile(const std::string& filename,
159 const Eigen::MatrixBase<Matrix>& matrix) {
160 typedef typename Matrix::Index Index;
161 typedef typename Matrix::Scalar Scalar;
162
163 std::ifstream in(filename.c_str(), std::ios::in | std::ios::binary);
164 if (!in.is_open()) return false;
165 Index rows = 0, cols = 0;
166 in.read((char*)(&rows), sizeof(Index));
167 in.read((char*)(&cols), sizeof(Index));
168
169 Eigen::MatrixBase<Matrix>& matrix_ =
170 const_cast<Eigen::MatrixBase<Matrix>&>(matrix);
171
172 matrix_.resize(rows, cols);
173 in.read((char*)matrix_.data(), rows * cols * sizeof(Scalar));
174 in.close();
175 return true;
176}
177
178} // namespace math
179} // namespace tsid
180
181#endif // ifndef __invdyn_math_utils_hpp__
Definition constraint-base.hpp:13
Eigen::Ref< Matrix > RefMatrix
Definition fwd.hpp:37
void nullSpaceBasisFromDecomposition(const Eigen::JacobiSVD< Eigen::MatrixXd > &svdDecomposition, double tolerance, double *nullSpaceBasisMatrix, int &rows, int &cols)
Definition utils.cpp:162
void svdSolveWithDamping(ConstRefMatrix A, ConstRefVector b, RefVector sol, double damping=0.0)
Definition utils.cpp:60
std::size_t Index
Definition fwd.hpp:40
bool is_nan(const Eigen::MatrixBase< Derived > &x)
Definition utils.hpp:130
void vectorToSE3(RefVector vec, pinocchio::SE3 &M)
Definition utils.cpp:25
void pseudoInverse(ConstRefMatrix A, RefMatrix Apinv, double tolerance, unsigned int computationOptions=Eigen::ComputeThinU|Eigen::ComputeThinV)
Definition utils.cpp:69
bool isFinite(const Eigen::MatrixBase< Derived > &x)
Definition utils.hpp:125
void solveWithDampingFromSvd(Eigen::JacobiSVD< Eigen::MatrixXd > &svd, ConstRefVector b, RefVector sol, double damping=0.0)
Definition utils.cpp:42
void SE3ToXYZQUAT(const pinocchio::SE3 &M, RefVector xyzQuat)
Definition utils.cpp:10
const Eigen::Ref< const Matrix > ConstRefMatrix
Definition fwd.hpp:38
const Eigen::Ref< const Vector > ConstRefVector
Definition fwd.hpp:35
bool readMatrixFromFile(const std::string &filename, const Eigen::MatrixBase< Matrix > &matrix)
Definition utils.hpp:158
bool writeMatrixToFile(const std::string &filename, const Eigen::MatrixBase< Matrix > &matrix)
Definition utils.hpp:138
void dampedPseudoInverse(ConstRefMatrix A, Eigen::JacobiSVD< Eigen::MatrixXd > &svdDecomposition, RefMatrix Apinv, double tolerance, double dampingFactor, unsigned int computationOptions=Eigen::ComputeThinU|Eigen::ComputeThinV, double *nullSpaceBasisOfA=0, int *nullSpaceRows=0, int *nullSpaceCols=0)
Definition utils.cpp:123
double Scalar
Definition fwd.hpp:21
void errorInSE3(const pinocchio::SE3 &M, const pinocchio::SE3 &Mdes, pinocchio::Motion &error)
Definition utils.cpp:33
Eigen::Ref< Vector > RefVector
Definition fwd.hpp:34
void SE3ToVector(const pinocchio::SE3 &M, RefVector vec)
Definition utils.cpp:17
Definition constraint-bound.hpp:25
std::string toString(const T &v)
Definition utils.hpp:26