RcsPySim
A robot control and simulation library
ISSBallOnPlate.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 "ISSBallOnPlate.h"
32 
33 #include <Rcs_macros.h>
34 #include <Rcs_typedef.h>
35 
36 #include <algorithm> // std::min
37 
38 
39 namespace Rcs
40 {
41 
43 {
44  // Grab direct references to the used bodies
45  ball = RcsGraph_getBodyByName(graph, "Ball");
46  RCHECK(ball);
47  plate = RcsGraph_getBodyByName(graph, "Plate");
48  RCHECK(plate);
49 
50  // Ensure that the ball's position is either absolute or relative to the plate
51  RCHECK_MSG(ball->parent == NULL || ball->parent == plate,
52  "The ball's parent must be NULL or the Plate, but was %s",
53  ball->parent ? ball->parent->name : "NULL");
54 
55  // Find plate dimensions
56  RcsShape* plateShape = NULL;
57  RCSBODY_TRAVERSE_SHAPES(plate) {
58  if (SHAPE->type == RCSSHAPE_BOX) {
59  // Found the shape
60  plateShape = SHAPE;
61  break;
62  }
63  }
64  RCHECK_MSG(plateShape != NULL, "Plate body must have a box shape.");
65  plateWidth = plateShape->extents[0];
66  plateHeight = plateShape->extents[1];
67 }
68 
70 {
71  // Nothing to destroy
72 }
73 
74 unsigned int ISSBallOnPlate::getDim() const
75 {
76  return 2;
77 }
78 
79 void ISSBallOnPlate::getMinMax(double* min, double* max) const
80 {
81  // Use a safety margin between the edge of the plate and the ball
82  double smallerExtent = std::min(plateWidth/2, plateHeight/2);
83  double minDist = smallerExtent*0.6;
84  double maxDist = smallerExtent*0.8;
85 
86  // Set minimum and maximum relative to the plate's center
87  min[0] = minDist;
88  max[0] = maxDist;
89  min[1] = -M_PI;
90  max[1] = +M_PI;
91 }
92 
93 
94 std::vector<std::string> ISSBallOnPlate::getNames() const
95 {
96  return {"r", "phi"};
97 }
98 
99 void ISSBallOnPlate::applyInitialState(const MatNd* initialState)
100 {
101  double ballX = std::cos(initialState->ele[1]) * initialState->ele[0];
102  double ballY = std::sin(initialState->ele[1]) * initialState->ele[0];
103 
104  if (ball->parent == NULL) {
105  // The initial position is relative to the plate, so shift it if the ball's rbj are absolute.
106  ballX += plate->A_BI->org[0];
107  ballY += plate->A_BI->org[1];
108  }
109 
110  // Set the position to the ball's rigid body joints
111  double* ballRBJ = &graph->q->ele[ball->jnt->jointIndex];
112  ballRBJ[0] = ballX;
113  ballRBJ[1] = ballY;
114 
115  // Update the forward kinematics
116  RcsGraph_setState(graph, graph->q, graph->q_dot);
117 }
118 
119 } /* namespace Rcs */
virtual std::vector< std::string > getNames() const
void applyInitialState(const MatNd *initialState) override
unsigned int getDim() const override
ISSBallOnPlate(RcsGraph *graph)
void getMinMax(double *min, double *max) const override