RcsPySim
A robot control and simulation library
PPDMassProperties.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (c) 2020, Fabio Muratore, Honda Research Institute Europe GmbH, and
3  Technical University of Darmstadt.
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8  1. Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  2. Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  3. Neither the name of Fabio Muratore, Honda Research Institute Europe GmbH,
14  or Technical University of Darmstadt, nor the names of its contributors may
15  be used to endorse or promote products derived from this software without
16  specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  DISCLAIMED. IN NO EVENT SHALL FABIO MURATORE, HONDA RESEARCH INSTITUTE EUROPE GMBH,
22  OR TECHNICAL UNIVERSITY OF DARMSTADT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  POSSIBILITY OF SUCH DAMAGE.
29 *******************************************************************************/
30 
31 #include "PPDMassProperties.h"
32 #include "PPDSingleVar.h"
33 
34 #include <Rcs_typedef.h>
35 #include <Rcs_body.h>
36 #include <Rcs_shape.h>
37 #include <Rcs_math.h>
38 
39 namespace Rcs
40 {
41 
42 #define DEF_MASS_PARAM(name, modflag, var) \
43  addChild(new PPDSingleVar<double>((name), (modflag), [](BodyParamInfo& bpi) -> double& {return (var);}))
44 
46 {
47  DEF_MASS_PARAM("mass", BodyParamInfo::MOD_MASS, bpi.body->m);
48 
49  DEF_MASS_PARAM("com_x", BodyParamInfo::MOD_COM, bpi.body->Inertia->org[0]);
50  DEF_MASS_PARAM("com_y", BodyParamInfo::MOD_COM, bpi.body->Inertia->org[1]);
51  DEF_MASS_PARAM("com_z", BodyParamInfo::MOD_COM, bpi.body->Inertia->org[2]);
52 
53  DEF_MASS_PARAM("i_xx", BodyParamInfo::MOD_INERTIA, bpi.body->Inertia->rot[0][0]);
54  DEF_MASS_PARAM("i_xy", BodyParamInfo::MOD_INERTIA, bpi.body->Inertia->rot[0][1]);
55  DEF_MASS_PARAM("i_xz", BodyParamInfo::MOD_INERTIA, bpi.body->Inertia->rot[0][2]);
56  DEF_MASS_PARAM("i_yy", BodyParamInfo::MOD_INERTIA, bpi.body->Inertia->rot[1][1]);
57  DEF_MASS_PARAM("i_yz", BodyParamInfo::MOD_INERTIA, bpi.body->Inertia->rot[1][2]);
58  DEF_MASS_PARAM("i_zz", BodyParamInfo::MOD_INERTIA, bpi.body->Inertia->rot[2][2]);
59 }
60 
62 
63 
65 {
66  PPDCompound::setValues(inValues);
67 
68  if (bodyParamInfo->body->m == 0.0) {
69  return;
70  }
71  double v_bdy = RcsBody_computeVolume(bodyParamInfo->body);
72  if (v_bdy == 0.0) {
73  return;
74  }
75 
77  // Recompute the CoM from the shape since that function is not exported
78  Vec3d_setZero(bodyParamInfo->body->Inertia->org);
79 
80  RCSBODY_TRAVERSE_SHAPES(bodyParamInfo->body) {
81  double r_com_sh[3], v_sh;
82  v_sh = RcsShape_computeVolume(SHAPE);
83  RcsShape_computeLocalCOM(SHAPE, r_com_sh);
84  Vec3d_constMulSelf(r_com_sh, v_sh/v_bdy);
85  Vec3d_addSelf(bodyParamInfo->body->Inertia->org, r_com_sh);
86  }
87  }
88 
90  Mat3d_setZero(bodyParamInfo->body->Inertia->rot);
91  // Recompute the inertia from shapes using existing COM
92  double density = bodyParamInfo->body->m/v_bdy; // Density: rho = m / V
93 
94  RCSBODY_TRAVERSE_SHAPES(bodyParamInfo->body) {
95  // Compute the shape's inertia around its COM (it is represented in the shape's frame of reference)
96  double I_s[3][3];
97  RcsShape_computeInertiaTensor(SHAPE, density, I_s);
98 
99  // Rotate inertia tensor from the shape frame into the body frame:
100  // B_I = A_BC^T C_I A_CB
101  Mat3d_similarityTransform(I_s, (double (*)[3]) SHAPE->A_CB.rot, I_s);
102 
103  // Add the Steiner term related to gravity center of shape in body frame
104  double r_b_sgc[3]; // vector from body origin to shape gravity center
105  RcsShape_computeLocalCOM(SHAPE, r_b_sgc);
106  double r_sgc_bgc[3]; // vector from shape COM to body COM
107  Vec3d_sub(r_sgc_bgc, bodyParamInfo->body->Inertia->org, r_b_sgc);
108  double m_sh = density*RcsShape_computeVolume(SHAPE);
109  Math_addSteinerToInertia(I_s, r_sgc_bgc, m_sh);
110  Mat3d_addSelf(bodyParamInfo->body->Inertia->rot, I_s);
111  }
112  }
113  else {
114  // Fill up symmetric elements of inertia
115  bodyParamInfo->body->Inertia->rot[1][0] = bodyParamInfo->body->Inertia->rot[0][1];
116  bodyParamInfo->body->Inertia->rot[2][0] = bodyParamInfo->body->Inertia->rot[0][2];
117  bodyParamInfo->body->Inertia->rot[2][1] = bodyParamInfo->body->Inertia->rot[1][2];
118  }
119 }
120 
121 } /* namespace Rcs */
RcsBody * body
The body.
Definition: BodyParamInfo.h:64
virtual void setValues(PropertySource *inValues)
Definition: PPDCompound.cpp:52
bool isChanged(int flag)
Check if a parameter changed.
Definition: BodyParamInfo.h:81
virtual void setValues(PropertySource *inValues)
#define DEF_MASS_PARAM(name, modflag, var)
virtual ~PPDMassProperties()