RcsPySim
A robot control and simulation library
PPDRodLength.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 "PPDRodLength.h"
32 
33 #include <Rcs_macros.h>
34 #include <Rcs_typedef.h>
35 #include <Rcs_body.h>
36 #include <Rcs_Vec3d.h>
37 
38 Rcs::PPDRodLength::PPDRodLength() : rodShape(NULL), childJoint(NULL)
39 {
40  // Initial rod direction is along the z-axis
41  Vec3d_setUnitVector(rodDirection, 2);
42  Vec3d_setZero(rodOffset);
43  Vec3d_setZero(jointOffset);
44 }
45 
47 {
49 
50  propertyName = bpi->paramNamePrefix + "length";
51 
52  // Locate the rod shape
53  RCSBODY_TRAVERSE_SHAPES(bpi->body) {
54  if (SHAPE->type == RCSSHAPE_CYLINDER && (SHAPE->computeType & RCSSHAPE_COMPUTE_PHYSICS) != 0) {
55  rodShape = SHAPE;
56  break;
57  }
58  }
59  RCHECK_MSG(rodShape, "No cylinder shape found on body %s.", bpi->body->name);
60 
61  // Get rod length as set in xml
62  double initialRodLength = rodShape->extents[2];
63 
64  // The rod direction in shape frame is along the z-axis
65  Vec3d_setUnitVector(rodDirection, 2);
66 
67  // Convert to body frame
68  Vec3d_transRotateSelf(rodDirection, rodShape->A_CB.rot);
69 
70  // The rod has, per se, no real good way to determine if it's in the positive or negative direction.
71  // however, we can guess by looking on which side of the body origin the rod center is
72  double curDistToOrigin = Vec3d_innerProduct(rodDirection, rodShape->A_CB.org);
73  if (curDistToOrigin < 0) {
74  // the rod goes into -direction, so invert direction
75  Vec3d_constMulSelf(rodDirection, -1);
76  }
77 
78  // The rod offset is the difference between the rod's initial start and the body's origin
79  // shapePos = offset + rodDir * length / 2
80  // => offset = shapePos - rodDir * length / 2
81  Vec3d_constMulAndAdd(rodOffset, rodShape->A_CB.org, rodDirection, -initialRodLength/2);
82 
83  if (STREQ(bpi->body->name, "Arm")) {
84  // locate pendulum body as child if any
85  RcsBody* pendulumChild = NULL;
86  for (RcsBody* child = bpi->body->firstChild; child != NULL; child = child->next) {
87  if (STREQ(child->name, "Pendulum")) {
88  pendulumChild = child;
89  break;
90  }
91  }
92  RCHECK_MSG(pendulumChild, "Arm body doesn't have a pendulum child.");
93 
94  // Extract joint
95  childJoint = pendulumChild->jnt;
96 
97  // Compute offset between rod end and joint if any
98  Vec3d_constMulAndAdd(jointOffset, childJoint->A_JP->org, rodDirection, -initialRodLength);
99  }
100 }
101 
103 {
104  double length;
105  if (!inValues->getProperty(length, propertyName.c_str())) {
106  // not set, ignore
107  return;
108  }
109 
110  // Set shape extends
111  rodShape->extents[2] = length;
112  // The shape origin is in the middle of the rod, the body origin at the end. Thus, shift the shape.
113  Vec3d_constMulAndAdd(rodShape->A_CB.org, rodOffset, rodDirection, length/2);
114 
115  // Also adjust child joint if needed
116  if (childJoint != NULL) {
117  // the joint should be at the end of the rod.
118  Vec3d_constMulAndAdd(childJoint->A_JP->org, jointOffset, rodDirection, length);
119  }
120 }
121 
123 {
124  outValues->setProperty(propertyName.c_str(), rodShape->extents[2]);
125 }
virtual bool getProperty(std::string &out, const char *property)=0
std::string propertyName
Definition: PPDRodLength.h:49
RcsBody * body
The body.
Definition: BodyParamInfo.h:64
virtual void setProperty(const char *property, const std::string &value)=0
std::string paramNamePrefix
prefix for parameter names
Definition: BodyParamInfo.h:67
virtual void getValues(PropertySink *outValues)
double rodOffset[3]
Definition: PPDRodLength.h:56
virtual void init(BodyParamInfo *bodyParamInfo)
RcsShape * rodShape
Definition: PPDRodLength.h:51
virtual void init(BodyParamInfo *bpi)
double jointOffset[3]
Definition: PPDRodLength.h:61
RcsJoint * childJoint
Definition: PPDRodLength.h:59
double rodDirection[3]
Definition: PPDRodLength.h:54
virtual void setValues(PropertySource *inValues)