Body.h
Go to the documentation of this file.
1 /*
2  * Original Copyright (c) 2011-2016 Martin Felis <martin.felis@iwr.uni-heidelberg.de>
3  *
4  *
5  * RDL - Robot Dynamics Library
6  * Modifications Copyright (c) 2017 Jordan Lack <jlack1987@gmail.com>
7  *
8  * Licensed under the zlib license. See LICENSE for more details.
9  */
10 
11 #ifndef RDL_BODY_H
12 #define RDL_BODY_H
13 
17 #include <assert.h>
18 #include <iostream>
20 
21 namespace RobotDynamics
22 {
30 struct Body
31 {
32  Body() : mMass(0.), mCenterOfMass(0., 0., 0.), mInertia(Math::Matrix3d::Zero(3, 3)), mIsVirtual(false)
33  {
34  }
35 
37  {
38  }
39 
40  Body& operator=(const Body& body)
41  {
42  if (this != &body)
43  {
44  mMass = body.mMass;
45  mInertia = body.mInertia;
47  mIsVirtual = body.mIsVirtual;
48  }
49 
50  return *this;
51  }
52 
64  Body(const double& mass, const Math::Vector3d& com, const Math::Vector3d& gyration_radii) : mMass(mass), mCenterOfMass(com), mIsVirtual(false)
65  {
66  mInertia = Math::Matrix3d(gyration_radii[0], 0., 0., 0., gyration_radii[1], 0., 0., 0., gyration_radii[2]);
67  }
68 
80  Body(const double& mass, const Math::Vector3d& com, const Math::Matrix3d& inertia_C) : mMass(mass), mCenterOfMass(com), mInertia(inertia_C), mIsVirtual(false)
81  {
82  }
83 
98  void join(const Math::SpatialTransform& transform, const Body& other_body)
99  {
100  // nothing to do if we join a massles body to the current.
101  if ((other_body.mMass == 0.) && (other_body.mInertia == Math::Matrix3d::Zero()))
102  {
103  return;
104  }
105 
106  double other_mass = other_body.mMass;
107  double new_mass = mMass + other_mass;
108 
109  if (new_mass == 0.)
110  {
111  throw RdlException("Error: cannot join bodies as both have zero mass!");
112  }
113 
114  Math::Vector3d other_com = transform.E.transpose() * other_body.mCenterOfMass + transform.r;
115  Math::Vector3d new_com = (1 / new_mass) * (mMass * mCenterOfMass + other_mass * other_com);
116 
117  // We have to transform the inertia of other_body to the new COM. This
118  // is done in 4 steps:
119  //
120  // 1. Transform the inertia from other origin to other COM
121  // 2. Rotate the inertia that it is aligned to the frame of this body
122  // 3. Transform inertia of other_body to the origin of the frame of
123  // this body
124  // 4. Sum the two inertias
125  // 5. Transform the summed inertia to the new COM
126 
127  Math::RigidBodyInertia other_rbi = Math::createFromMassComInertiaC(other_body.mMass, other_body.mCenterOfMass, other_body.mInertia);
129 
130  Math::Matrix3d inertia_other = other_rbi.toMatrix().block<3, 3>(0, 0);
131 
132  // 1. Transform the inertia from other origin to other COM
133  Math::Matrix3d other_com_cross = Math::toTildeForm(other_body.mCenterOfMass);
134  Math::Matrix3d inertia_other_com = inertia_other - other_mass * other_com_cross * other_com_cross.transpose();
135 
136  // 2. Rotate the inertia that it is aligned to the frame of this body
137  Math::Matrix3d inertia_other_com_rotated = transform.E.transpose() * inertia_other_com * transform.E;
138 
139  // 3. Transform inertia of other_body to the origin of the frame of this body
140  Math::Matrix3d inertia_other_com_rotated_this_origin = Math::parallel_axis(inertia_other_com_rotated, other_mass, other_com);
141 
142  // 4. Sum the two inertias
143  Math::Matrix3d inertia_summed = Math::Matrix3d(this_rbi.toMatrix().block<3, 3>(0, 0)) + inertia_other_com_rotated_this_origin;
144 
145  // 5. Transform the summed inertia to the new COM
146  Math::Matrix3d new_inertia = inertia_summed - new_mass * Math::toTildeForm(new_com) * Math::toTildeForm(new_com).transpose();
147 
148  *this = Body(new_mass, new_com, new_inertia);
149  }
150 
152  {
153  }
154 
156  double mMass;
157 
160 
163 
165 };
166 
173 struct FixedBody
174 {
176  double mMass;
177 
180 
183 
185  unsigned int mMovableParent;
186 
188  // fixed body.
191 
192  static FixedBody CreateFromBody(const Body& body)
193  {
194  FixedBody fbody;
195 
196  fbody.mMass = body.mMass;
197  fbody.mCenterOfMass = body.mCenterOfMass;
198  fbody.mInertia = body.mInertia;
199 
200  return fbody;
201  }
202 };
203 } // namespace RobotDynamics
204 
205 #endif // ifndef RDL_BODY_H
Body(const double &mass, const Math::Vector3d &com, const Math::Vector3d &gyration_radii)
Constructs a body from mass, center of mass and radii of gyration.
Definition: Body.h:64
Describes all properties of a single body.
Definition: Body.h:30
Math::Vector3d mCenterOfMass
The position of the center of mass in body coordinates.
Definition: Body.h:179
Math::Matrix3d mInertia
The spatial inertia that contains both mass and inertia information.
Definition: Body.h:182
Body(const Body &body)
Definition: Body.h:36
Keeps the information of a body and how it is attached to another body.
Definition: Body.h:173
Body & operator=(const Body &body)
Definition: Body.h:40
void join(const Math::SpatialTransform &transform, const Body &other_body)
Joins inertial parameters of two bodies to create a composite body.
Definition: Body.h:98
See V. Duindum p39-40 & Featherstone p32-33.
static Matrix3d toTildeForm(const Point3d &p)
Definition: Point3.hpp:370
double mMass
The mass of the body.
Definition: Body.h:156
Math::Matrix3d mInertia
Inertia matrix at the center of mass.
Definition: Body.h:162
double mMass
The mass of the body.
Definition: Body.h:176
Compact representation of spatial transformations.
This class stores a bodies mass, center of mass, and inertia information. The inertia elements are st...
Body(const double &mass, const Math::Vector3d &com, const Math::Matrix3d &inertia_C)
Constructs a body from mass, center of mass, and a 3x3 inertia matrix.
Definition: Body.h:80
Math::SpatialTransform mBaseTransform
Definition: Body.h:190
Math::Vector3d mCenterOfMass
The position of the center of mass in body coordinates.
Definition: Body.h:159
A custom exception.
Matrix3d parallel_axis(const Matrix3d &inertia, double mass, const Vector3d &com)
Translates the inertia matrix to a new center.
unsigned int mMovableParent
Id of the movable body that this fixed body is attached to.
Definition: Body.h:185
Math::SpatialTransform mParentTransform
Transforms spatial quantities expressed for the parent to the.
Definition: Body.h:189
Namespace for all structures of the RobotDynamics library.
Definition: Body.h:21
static FixedBody CreateFromBody(const Body &body)
Definition: Body.h:192
static RigidBodyInertia createFromMassComInertiaC(double mass, const Vector3d &com, const Matrix3d &inertia_C)


rdl_dynamics
Author(s):
autogenerated on Tue Apr 20 2021 02:25:27