Ignition Math

API Reference

4.0.0
Inertial.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef IGNITION_MATH_INERTIAL_HH_
18 #define IGNITION_MATH_INERTIAL_HH_
19 
20 #include <ignition/math/config.hh>
22 #include "ignition/math/Pose3.hh"
23 
24 namespace ignition
25 {
26  namespace math
27  {
28  // Inline bracket to help doxygen filtering.
29  inline namespace IGNITION_MATH_VERSION_NAMESPACE {
30  //
35  template<typename T>
36  class Inertial
37  {
39  public: Inertial()
40  {}
41 
45  public: Inertial(const MassMatrix3<T> &_massMatrix,
46  const Pose3<T> &_pose)
47  : massMatrix(_massMatrix), pose(_pose)
48  {}
49 
52  public: Inertial(const Inertial<T> &_inertial)
53  : massMatrix(_inertial.MassMatrix()), pose(_inertial.Pose())
54  {}
55 
57  public: virtual ~Inertial() {}
58 
62  public: bool SetMassMatrix(const MassMatrix3<T> &_m)
63  {
64  this->massMatrix = _m;
65  return this->massMatrix.IsValid();
66  }
67 
70  public: const MassMatrix3<T> &MassMatrix() const
71  {
72  return this->massMatrix;
73  }
74 
78  public: bool SetPose(const Pose3<T> &_pose)
79  {
80  this->pose = _pose;
81  return this->massMatrix.IsValid();
82  }
83 
86  public: const Pose3<T> &Pose() const
87  {
88  return this->pose;
89  }
90 
94  public: Matrix3<T> MOI() const
95  {
96  auto R = Matrix3<T>(this->pose.Rot());
97  return R * this->massMatrix.MOI() * R.Transposed();
98  }
99 
104  public: bool SetInertialRotation(const Quaternion<T> &_q)
105  {
106  auto moi = this->MOI();
107  this->pose.Rot() = _q;
108  auto R = Matrix3<T>(_q);
109  return this->massMatrix.MOI(R.Transposed() * moi * R);
110  }
111 
124  public: bool SetMassMatrixRotation(const Quaternion<T> &_q,
125  const T _tol = 1e-6)
126  {
127  this->pose.Rot() *= this->MassMatrix().PrincipalAxesOffset(_tol) *
128  _q.Inverse();
129  const auto moments = this->MassMatrix().PrincipalMoments(_tol);
130  const auto diag = Matrix3<T>(
131  moments[0], 0, 0,
132  0, moments[1], 0,
133  0, 0, moments[2]);
134  const auto R = Matrix3<T>(_q);
135  return this->massMatrix.MOI(R * diag * R.Transposed());
136  }
137 
141  public: Inertial &operator=(const Inertial<T> &_inertial)
142  {
143  this->massMatrix = _inertial.MassMatrix();
144  this->pose = _inertial.Pose();
145 
146  return *this;
147  }
148 
153  public: bool operator==(const Inertial<T> &_inertial) const
154  {
155  return (this->pose == _inertial.Pose()) &&
156  (this->massMatrix == _inertial.MassMatrix());
157  }
158 
162  public: bool operator!=(const Inertial<T> &_inertial) const
163  {
164  return !(*this == _inertial);
165  }
166 
172  public: Inertial<T> &operator+=(const Inertial<T> &_inertial)
173  {
174  T m1 = this->massMatrix.Mass();
175  T m2 = _inertial.MassMatrix().Mass();
176 
177  // Total mass
178  T mass = m1 + m2;
179 
180  // Only continue if total mass is positive
181  if (mass <= 0)
182  {
183  return *this;
184  }
185 
186  auto com1 = this->Pose().Pos();
187  auto com2 = _inertial.Pose().Pos();
188  // New center of mass location in base frame
189  auto com = (m1*com1 + m2*com2) / mass;
190 
191  // Components of new moment of inertia matrix
192  Vector3<T> ixxyyzz;
193  Vector3<T> ixyxzyz;
194  // First add matrices in base frame
195  {
196  auto moi = this->MOI() + _inertial.MOI();
197  ixxyyzz = Vector3<T>(moi(0, 0), moi(1, 1), moi(2, 2));
198  ixyxzyz = Vector3<T>(moi(0, 1), moi(0, 2), moi(1, 2));
199  }
200  // Then account for parallel axis theorem
201  {
202  auto dc = com1 - com;
203  ixxyyzz.X() += m1 * (std::pow(dc[1], 2) + std::pow(dc[2], 2));
204  ixxyyzz.Y() += m1 * (std::pow(dc[2], 2) + std::pow(dc[0], 2));
205  ixxyyzz.Z() += m1 * (std::pow(dc[0], 2) + std::pow(dc[1], 2));
206  ixxyyzz.X() -= m1 * dc[0] * dc[1];
207  ixxyyzz.Y() -= m1 * dc[0] * dc[2];
208  ixxyyzz.Z() -= m1 * dc[1] * dc[2];
209  }
210  {
211  auto dc = com2 - com;
212  ixxyyzz.X() += m2 * (std::pow(dc[1], 2) + std::pow(dc[2], 2));
213  ixxyyzz.Y() += m2 * (std::pow(dc[2], 2) + std::pow(dc[0], 2));
214  ixxyyzz.Z() += m2 * (std::pow(dc[0], 2) + std::pow(dc[1], 2));
215  ixxyyzz.X() -= m2 * dc[0] * dc[1];
216  ixxyyzz.Y() -= m2 * dc[0] * dc[2];
217  ixxyyzz.Z() -= m2 * dc[1] * dc[2];
218  }
219  this->massMatrix = MassMatrix3<T>(mass, ixxyyzz, ixyxzyz);
220  this->pose = Pose3<T>(com, Quaternion<T>::Identity);
221 
222  return *this;
223  }
224 
230  public: const Inertial<T> operator+(const Inertial<T> &_inertial) const
231  {
232  return Inertial<T>(*this) += _inertial;
233  }
234 
237  private: MassMatrix3<T> massMatrix;
238 
241  private: Pose3<T> pose;
242  };
243 
246  }
247  }
248 }
249 #endif
A class for inertial information about a rigid body consisting of the scalar mass and a 3x3 symmetric...
Definition: MassMatrix3.hh:43
Encapsulates a position and rotation in three space.
Definition: Pose3.hh:34
bool SetPose(const Pose3< T > &_pose)
Set the pose of center of mass reference frame.
Definition: Inertial.hh:78
Inertial()
Default Constructor.
Definition: Inertial.hh:39
bool SetMassMatrix(const MassMatrix3< T > &_m)
Set the mass and inertia matrix.
Definition: Inertial.hh:62
A class for inertial information about a rigid body consisting of the scalar mass, a 3x3 symmetric moment of inertia matrix, and center of mass reference frame pose.
Definition: Inertial.hh:36
T X() const
Get the x value.
Definition: Vector3.hh:648
T Z() const
Get the z value.
Definition: Vector3.hh:662
T Y() const
Get the y value.
Definition: Vector3.hh:655
Inertial(const Inertial< T > &_inertial)
Copy constructor.
Definition: Inertial.hh:52
bool operator==(const Inertial< T > &_inertial) const
Equality comparison operator.
Definition: Inertial.hh:153
const Inertial< T > operator+(const Inertial< T > &_inertial) const
Adds inertial properties to current object. The mass, center of mass location, and inertia matrix are...
Definition: Inertial.hh:230
A 3x3 matrix class.
Definition: Matrix3.hh:39
bool SetInertialRotation(const Quaternion< T > &_q)
Set the inertial pose rotation without affecting the MOI in the base coordinate frame.
Definition: Inertial.hh:104
bool IsValid() const
Verify that inertia values are positive definite and satisfy the triangle inequality.
Definition: MassMatrix3.hh:302
Inertial< T > & operator+=(const Inertial< T > &_inertial)
Adds inertial properties to current object. The mass, center of mass location, and inertia matrix are...
Definition: Inertial.hh:172
Inertial< float > Inertialf
Definition: Inertial.hh:245
Matrix3< T > MOI() const
Get the moment of inertia matrix expressed in the base coordinate frame.
Definition: Inertial.hh:94
Inertial(const MassMatrix3< T > &_massMatrix, const Pose3< T > &_pose)
Constructor.
Definition: Inertial.hh:45
The Vector3 class represents the generic vector containing 3 elements. Since it&#39;s commonly used to ke...
Definition: Vector3.hh:40
Inertial & operator=(const Inertial< T > &_inertial)
Equal operator.
Definition: Inertial.hh:141
T pow(T... args)
Matrix3< T > Transposed() const
Return the transpose of this matrix.
Definition: Matrix3.hh:479
const MassMatrix3< T > & MassMatrix() const
Get the mass and inertia matrix.
Definition: Inertial.hh:70
Definition: Angle.hh:39
Inertial< double > Inertiald
Definition: Inertial.hh:244
A quaternion class.
Definition: Matrix3.hh:34
Quaternion< T > Inverse() const
Get the inverse of this quaternion.
Definition: Quaternion.hh:132
bool SetMassMatrixRotation(const Quaternion< T > &_q, const T _tol=1e-6)
Set the MassMatrix rotation (eigenvectors of inertia matrix) without affecting the MOI in the base co...
Definition: Inertial.hh:124
const Pose3< T > & Pose() const
Get the pose of center of mass reference frame.
Definition: Inertial.hh:86
virtual ~Inertial()
Destructor.
Definition: Inertial.hh:57
bool operator!=(const Inertial< T > &_inertial) const
Inequality test operator.
Definition: Inertial.hh:162