Rigid Body Dynamics Library
Body.h
Go to the documentation of this file.
1/*
2 * RBDL - Rigid Body Dynamics Library
3 * Copyright (c) 2011-2018 Martin Felis <martin@fysx.org>
4 *
5 * Licensed under the zlib license. See LICENSE for more details.
6 */
7
8#ifndef RBDL_BODY_H
9#define RBDL_BODY_H
10
11#include "rbdl/rbdl_math.h"
12#include "rbdl/rbdl_mathutils.h"
13#include <assert.h>
14#include <iostream>
15#include "rbdl/Logging.h"
16#include "rbdl/rbdl_errors.h"
17
18namespace RigidBodyDynamics
19{
20
28struct RBDL_DLLAPI Body {
29 Body() :
30 mMass (0.),
31 mCenterOfMass (0., 0., 0.),
32 mInertia (Math::Matrix3d::Zero()),
33 mIsVirtual (false)
34 { };
35 Body(const Body &body) :
36 mMass (body.mMass),
37 mCenterOfMass (body.mCenterOfMass),
38 mInertia (body.mInertia),
39 mIsVirtual (body.mIsVirtual)
40 {};
41 Body& operator= (const Body &body)
42 {
43 if (this != &body) {
44 mMass = body.mMass;
45 mInertia = body.mInertia;
46 mCenterOfMass = body.mCenterOfMass;
47 mIsVirtual = body.mIsVirtual;
48 }
49
50 return *this;
51 }
52
64 Body(const Math::Scalar &mass,
65 const Math::Vector3d &com,
66 const Math::Vector3d &gyration_radii) :
67 mMass (mass),
68 mCenterOfMass(com),
69 mIsVirtual (false)
70 {
71 mInertia = Math::Matrix3d (
72 gyration_radii[0], 0., 0.,
73 0., gyration_radii[1], 0.,
74 0., 0., gyration_radii[2]
75 );
76 }
77
89 Body(const Math::Scalar &mass,
90 const Math::Vector3d &com,
91 const Math::Matrix3d &inertia_C) :
92 mMass (mass),
93 mCenterOfMass(com),
94 mInertia (inertia_C),
95 mIsVirtual (false) { }
96
108 void Join (const Math::SpatialTransform &transform, const Body &other_body)
109 {
110#ifndef RBDL_USE_CASADI_MATH
111 // nothing to do if we join a massles body to the current.
112 if (other_body.mMass == 0. && other_body.mInertia == Math::Matrix3d::Zero()) {
113 return;
114 }
115#endif
116
117 Math::Scalar other_mass = other_body.mMass;
118 Math::Scalar new_mass = mMass + other_mass;
119
120#ifndef RBDL_USE_CASADI_MATH
121 if (new_mass == 0.) {
122 throw Errors::RBDLError("Error: cannot join bodies as both have zero mass!\n");
123 }
124#endif
125
126 Math::Vector3d other_com = transform.E.transpose() * other_body.mCenterOfMass +
127 transform.r;
128 Math::Vector3d new_com = (1 / new_mass ) * (mMass * mCenterOfMass + other_mass *
129 other_com);
130
131 LOG << "other_com = " << std::endl << other_com.transpose() << std::endl;
132 LOG << "rotation = " << std::endl << transform.E << std::endl;
133
134 // We have to transform the inertia of other_body to the new COM. This
135 // is done in 4 steps:
136 //
137 // 1. Transform the inertia from other origin to other COM
138 // 2. Rotate the inertia that it is aligned to the frame of this body
139 // 3. Transform inertia of other_body to the origin of the frame of
140 // this body
141 // 4. Sum the two inertias
142 // 5. Transform the summed inertia to the new COM
143
146 other_body.mCenterOfMass, other_body.mInertia);
149 mInertia);
150
151 Math::Matrix3d inertia_other = other_rbi.toMatrix().block<3,3>(0,0);
152 LOG << "inertia_other = " << std::endl << inertia_other << std::endl;
153
154 // 1. Transform the inertia from other origin to other COM
155 Math::Matrix3d other_com_cross = Math::VectorCrossMatrix(
156 other_body.mCenterOfMass);
157 Math::Matrix3d inertia_other_com = inertia_other - other_mass * other_com_cross
158 * other_com_cross.transpose();
159 LOG << "inertia_other_com = " << std::endl << inertia_other_com << std::endl;
160
161 // 2. Rotate the inertia that it is aligned to the frame of this body
162 Math::Matrix3d inertia_other_com_rotated = transform.E.transpose() *
163 inertia_other_com * transform.E;
164 LOG << "inertia_other_com_rotated = " << std::endl << inertia_other_com_rotated
165 << std::endl;
166
167 // 3. Transform inertia of other_body to the origin of the frame of this body
168 Math::Matrix3d inertia_other_com_rotated_this_origin = Math::parallel_axis (
169 inertia_other_com_rotated, other_mass, other_com);
170 LOG << "inertia_other_com_rotated_this_origin = " << std::endl <<
171 inertia_other_com_rotated_this_origin << std::endl;
172
173 // 4. Sum the two inertias
174 Math::Matrix3d inertia_summed = Math::Matrix3d (this_rbi.toMatrix().block<3,3>
175 (0,0)) + inertia_other_com_rotated_this_origin;
176 LOG << "inertia_summed = " << std::endl << inertia_summed << std::endl;
177
178 // 5. Transform the summed inertia to the new COM
179 Math::Matrix3d new_inertia = inertia_summed - new_mass *
181 new_com).transpose();
182
183 LOG << "new_mass = " << new_mass << std::endl;
184 LOG << "new_com = " << new_com.transpose() << std::endl;
185 LOG << "new_inertia = " << std::endl << new_inertia << std::endl;
186
187 *this = Body (new_mass, new_com, new_inertia);
188 }
189
190 ~Body() {};
191
198
200};
201
208struct RBDL_DLLAPI FixedBody {
215
217 unsigned int mMovableParent;
219 // fixed body.
222
223 static FixedBody CreateFromBody (const Body& body)
224 {
225 FixedBody fbody;
226
227 fbody.mMass = body.mMass;
228 fbody.mCenterOfMass = body.mCenterOfMass;
229 fbody.mInertia = body.mInertia;
230
231 return fbody;
232 }
233};
234
235}
236
237/* RBDL_BODY_H */
238#endif
Base class for all RBDL exceptions.
Definition: rbdl_errors.h:35
Matrix3d VectorCrossMatrix(const Vector3d &vector)
RBDL_DLLAPI Matrix3d parallel_axis(const Matrix3d &inertia, Scalar mass, const Vector3d &com)
Translates the inertia matrix to a new center.
Describes all properties of a single body.
Definition: Body.h:28
Math::Matrix3d mInertia
Inertia matrix at the center of mass.
Definition: Body.h:197
Body(const Math::Scalar &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
Body(const Body &body)
Definition: Body.h:35
void Join(const Math::SpatialTransform &transform, const Body &other_body)
Joins inertial parameters of two bodies to create a composite body.
Definition: Body.h:108
Math::Vector3d mCenterOfMass
The position of the center of mass in body coordinates.
Definition: Body.h:195
Body(const Math::Scalar &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:89
Math::Scalar mMass
The mass of the body.
Definition: Body.h:190
Keeps the information of a body and how it is attached to another body.
Definition: Body.h:208
Math::Matrix3d mInertia
The spatial inertia that contains both mass and inertia information.
Definition: Body.h:214
static FixedBody CreateFromBody(const Body &body)
Definition: Body.h:223
Math::SpatialTransform mBaseTransform
Definition: Body.h:221
unsigned int mMovableParent
Id of the movable body that this fixed body is attached to.
Definition: Body.h:217
Math::SpatialTransform mParentTransform
Transforms spatial quantities expressed for the parent to the.
Definition: Body.h:220
Math::Vector3d mCenterOfMass
The position of the center of mass in body coordinates.
Definition: Body.h:212
Math::Scalar mMass
The mass of the body.
Definition: Body.h:210
Compact representation for Spatial Inertia.
static SpatialRigidBodyInertia createFromMassComInertiaC(Scalar mass, const Vector3d &com, const Matrix3d &inertia_C)
Compact representation of spatial transformations.