forked from amburkoff/be2r_cmpc_unitree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpatialInertia.h
181 lines (161 loc) · 4.97 KB
/
SpatialInertia.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*! @file SpatialInertia.h
* @brief Class representing spatial inertia tensors
*
*/
#ifndef LIBBIOMIMETICS_SPATIALINERTIA_H
#define LIBBIOMIMETICS_SPATIALINERTIA_H
#include <cmath>
#include <iostream>
#include <type_traits>
#include "Math/orientation_tools.h"
#include "spatial.h"
using namespace ori;
using namespace spatial;
/*!
* Representation of Rigid Body Inertia as a 6x6 Spatial Inertia Tensor
*/
template <typename T>
class SpatialInertia
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
/*!
* Construct spatial inertia from mass, center of mass, and 3x3 rotational
* inertia
*/
SpatialInertia(T mass, const Vec3<T>& com, const Mat3<T>& inertia)
{
Mat3<T> cSkew = vectorToSkewMat(com);
_inertia.template topLeftCorner<3, 3>() = inertia + mass * cSkew * cSkew.transpose();
_inertia.template topRightCorner<3, 3>() = mass * cSkew;
_inertia.template bottomLeftCorner<3, 3>() = mass * cSkew.transpose();
_inertia.template bottomRightCorner<3, 3>() = mass * Mat3<T>::Identity();
}
/*!
* Construct spatial inertia from 6x6 matrix
*/
explicit SpatialInertia(const Mat6<T>& inertia) { _inertia = inertia; }
/*!
* If no argument is given, zero.
*/
SpatialInertia() { _inertia = Mat6<T>::Zero(); }
/*!
* Construct spatial inertia from mass property vector
*/
explicit SpatialInertia(const MassProperties<T>& a)
{
_inertia(0, 0) = a(4);
_inertia(0, 1) = a(9);
_inertia(0, 2) = a(8);
_inertia(1, 0) = a(9);
_inertia(1, 1) = a(5);
_inertia(1, 2) = a(7);
_inertia(2, 0) = a(8);
_inertia(2, 1) = a(7);
_inertia(2, 2) = a(6);
Mat3<T> cSkew = vectorToSkewMat(Vec3<T>(a(1), a(2), a(3)));
_inertia.template topRightCorner<3, 3>() = cSkew;
_inertia.template bottomLeftCorner<3, 3>() = cSkew.transpose();
_inertia.template bottomRightCorner<3, 3>() = a(0) * Mat3<T>::Identity();
}
/*!
* Construct spatial inertia from pseudo-inertia. This is described in
* Linear Matrix Inequalities for Physically Consistent Inertial Parameter
* Identification: A Statistical Perspective on the Mass Distribution, by
* Wensing, Kim, Slotine
* @param P
*/
explicit SpatialInertia(const Mat4<T>& P)
{
Mat6<T> I;
T m = P(3, 3);
Vec3<T> h = P.template topRightCorner<3, 1>();
Mat3<T> E = P.template topLeftCorner<3, 3>();
Mat3<T> Ibar = E.trace() * Mat3<T>::Identity() - E;
I.template topLeftCorner<3, 3>() = Ibar;
I.template topRightCorner<3, 3>() = vectorToSkewMat(h);
I.template bottomLeftCorner<3, 3>() = vectorToSkewMat(h).transpose();
I.template bottomRightCorner<3, 3>() = m * Mat3<T>::Identity();
_inertia = I;
}
/*!
* Convert spatial inertia to mass property vector
*/
MassProperties<T> asMassPropertyVector()
{
MassProperties<T> a;
Vec3<T> h = matToSkewVec(_inertia.template topRightCorner<3, 3>());
a << _inertia(5, 5), h(0), h(1), h(2), _inertia(0, 0), _inertia(1, 1),
_inertia(2, 2), _inertia(2, 1), _inertia(2, 0), _inertia(1, 0);
return a;
}
/*!
* Get 6x6 spatial inertia
*/
const Mat6<T>& getMatrix() const { return _inertia; }
void setMatrix(const Mat6<T>& mat) { _inertia = mat; }
void addMatrix(const Mat6<T>& mat) { _inertia += mat; }
/*!
* Get mass
*/
T getMass() { return _inertia(5, 5); }
/*!
* Get center of mass location
*/
Vec3<T> getCOM()
{
T m = getMass();
Mat3<T> mcSkew = _inertia.template topRightCorner<3, 3>();
Vec3<T> com = matToSkewVec(mcSkew) / m;
return com;
}
/*!
* Get 3x3 rotational inertia
*/
Mat3<T> getInertiaTensor()
{
T m = getMass();
Mat3<T> mcSkew = _inertia.template topRightCorner<3, 3>();
Mat3<T> I_rot = _inertia.template topLeftCorner<3, 3>() -
mcSkew * mcSkew.transpose() / m;
return I_rot;
}
/*!
* Convert to 4x4 pseudo-inertia matrix. This is described in
* Linear Matrix Inequalities for Physically Consistent Inertial Parameter
* Identification: A Statistical Perspective on the Mass Distribution, by
* Wensing, Kim, Slotine
*/
Mat4<T> getPseudoInertia()
{
Vec3<T> h = matToSkewVec(_inertia.template topRightCorner<3, 3>());
Mat3<T> Ibar = _inertia.template topLeftCorner<3, 3>();
T m = _inertia(5, 5);
Mat4<T> P;
P.template topLeftCorner<3, 3>() =
0.5 * Ibar.trace() * Mat3<T>::Identity() - Ibar;
P.template topRightCorner<3, 1>() = h;
P.template bottomLeftCorner<1, 3>() = h.transpose();
P(3, 3) = m;
return P;
}
/*!
* Flip inertia matrix around an axis. This isn't efficient, but it works!
*/
SpatialInertia flipAlongAxis(CoordinateAxis axis)
{
Mat4<T> P = getPseudoInertia();
Mat4<T> X = Mat4<T>::Identity();
if (axis == CoordinateAxis::X)
X(0, 0) = -1;
else if (axis == CoordinateAxis::Y)
X(1, 1) = -1;
else if (axis == CoordinateAxis::Z)
X(2, 2) = -1;
P = X * P * X;
return SpatialInertia(P);
}
private:
Mat6<T> _inertia;
};
#endif // LIBBIOMIMETICS_SPATIALINERTIA_H