RcsPySim
A robot control and simulation library
PPDSphereRadius.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 "PPDSphereRadius.h"
32 
33 #include <Rcs_macros.h>
34 #include <Rcs_typedef.h>
35 
36 namespace Rcs
37 {
38 
39 PPDSphereRadius::PPDSphereRadius(std::string prevBodyName, unsigned int shapeIdx, unsigned int shapeIdxPrevBody) :
40  PPDSingleVar("radius", BodyParamInfo::MOD_SHAPE,
41  [](BodyParamInfo& bpi) -> double& { return bpi.body->shape[0]->extents[0]; }),
43 
45 
47 {
48  // Adapt properties
49  PPDSingleVar::setValues(inValues);
50 
51  // Check if the ball position variable is relative to another body
52  RcsBody* prevBody = RcsGraph_getBodyByName(bodyParamInfo->graph, prevBodyName.c_str());
53  double zOffset = 0.;
54  if (prevBody != nullptr) {
55  if (bodyParamInfo->body->parent == prevBody) {
56  // Sphere rigid body coordinates are relative. Add relative transform of the previous body's shape.
57  zOffset += prevBody->shape[shapeIdxPrevBody]->A_CB.org[2];
58  }
59  else {
60  // The sphere's rigid body coordinates are absolute, shift them accordingly
61  // Note: this assumes that the refBody is level, i.e. not tilted
62  zOffset = prevBody->A_BI->org[2];
63  }
64 
65  if (prevBody->shape[shapeIdxPrevBody]->type == RCSSHAPE_TYPE::RCSSHAPE_BOX) {
66  zOffset += prevBody->shape[shapeIdxPrevBody]->extents[2]/2.;
67  }
68  else if (prevBody->shape[shapeIdxPrevBody]->type == RCSSHAPE_TYPE::RCSSHAPE_CYLINDER) {
69  zOffset += prevBody->shape[shapeIdxPrevBody]->extents[2]/2.;
70  }
71  else if (prevBody->shape[shapeIdxPrevBody]->type == RCSSHAPE_TYPE::RCSSHAPE_SPHERE) {
72  zOffset += prevBody->shape[shapeIdxPrevBody]->extents[0];
73  }
74  else {
75  REXEC(4) {
76  std::cout << "No default vertical offset found for previous body shape " <<
77  prevBody->shape[0]->type << std::endl;
78  }
79  }
80  }
81  else {
82  REXEC(4) {
83  std::cout << "No reference body found for adding an offset to the randomized sphere's position! " <<
84  "Received " << prevBodyName << std::endl;
85  }
86  }
87 
88  // Adapt the sphere's z-position
89  double newRadius = bodyParamInfo->body->shape[shapeIdx]->extents[0];
90  bodyParamInfo->graph->q->ele[bodyParamInfo->body->jnt->jointIndex + 2] = zOffset + newRadius;
91 
92  // Make sure the state is propagated
93  RcsGraph_setState(bodyParamInfo->graph, nullptr, bodyParamInfo->graph->q_dot);
94 
95  RLOG(4, "New radius = %f; New z-position = %f", newRadius, zOffset + newRadius);
96 }
97 
99 {
100  // Check if the ball is valid
101  PPDSingleVar::init(bodyParamInfo);
102  RCHECK_MSG(bodyParamInfo->body->shape != nullptr, "Invalid ball body %s", bodyParamInfo->body->name);
103  RCHECK_MSG(bodyParamInfo->body->shape[shapeIdx] != nullptr, "Invalid ball body %s", bodyParamInfo->body->name);
104  RCHECK_MSG(bodyParamInfo->body->shape[shapeIdx]->type == RCSSHAPE_SPHERE, "Invalid ball body %s",
105  bodyParamInfo->body->name);
106  RCHECK_MSG((bodyParamInfo->body->shape[shapeIdx]->computeType & RCSSHAPE_COMPUTE_PHYSICS) != 0,
107  "Invalid ball body %s", bodyParamInfo->body->name);
108  RCHECK_MSG(bodyParamInfo->body->rigid_body_joints, "Invalid ball body %s", bodyParamInfo->body->name);
109 }
110 
111 } /* namespace Rcs */
virtual void setValues(PropertySource *inValues)
virtual void init(BodyParamInfo *bodyParamInfo)
RcsBody * body
The body.
Definition: BodyParamInfo.h:64
unsigned int shapeIdxPrevBody
The spheres&#39;s index within the previous body. This is given by the order of the shapes in the config ...
virtual void setValues(PropertySource *inValues)
Definition: PPDSingleVar.h:70
virtual ~PPDSphereRadius()
virtual void init(BodyParamInfo *bodyParamInfo)
Definition: PPDSingleVar.h:85
RcsGraph * graph
The graph containing the body.
Definition: BodyParamInfo.h:61
PPDSphereRadius(std::string prevBodyName, unsigned int shapeIdx=0, unsigned int shapeIdxPrevBody=0)
std::string prevBodyName
Name of the previous body if the graph to which the sphere is placed relative to. ...
unsigned int shapeIdx
The spheres&#39;s index within given the body. This is given by the order of the shapes in the config xml...