RcsPySim
A robot control and simulation library
OMTaskPositions.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 "OMTaskPositions.h"
32 
33 #include <Rcs_typedef.h>
34 #include <Rcs_macros.h>
35 
36 #include <algorithm>
37 #include <limits>
38 
39 namespace Rcs
40 {
41 
42 OMTaskPositions::OMTaskPositions(Task* task) : task(task) {}
43 
45 {
46  delete task;
47 }
48 
49 unsigned int OMTaskPositions::getStateDim() const
50 {
51  return task->getDim();
52 }
53 
54 unsigned int OMTaskPositions::getVelocityDim() const
55 {
56  return 0;
57 }
58 
59 void OMTaskPositions::computeObservation(double* state, double* velocity, const MatNd* currentAction, double dt) const
60 {
61  task->computeX(state);
62 }
63 
64 void OMTaskPositions::getLimits(double* minState, double* maxState, double* maxVelocity) const
65 {
66  for (size_t i = 0; i < getStateDim(); ++i) {
67  minState[i] = task->getParameter(i).minVal;
68  maxState[i] = task->getParameter(i).maxVal;
69  }
70 }
71 
72 std::vector<std::string> OMTaskPositions::getStateNames() const
73 {
74  // The dynamical systems do report their var names, but unfortunately also include the unit in that string.
75  // We have to strip that.
76  std::vector<std::string> result;
77  result.reserve(getStateDim());
78 
79  std::string prefix = task->getEffector()->name;
80  prefix += "_";
81 
82  for (auto param : task->getParameters()) {
83  auto paramName = param.name;
84  auto spaceIdx = paramName.find(' ');
85 
86  if (spaceIdx != std::string::npos) {
87  paramName = paramName.substr(0, spaceIdx);
88  }
89 
90  result.push_back(prefix + paramName);
91  }
92  return result;
93 }
94 
96 {
97  for (size_t i = 0; i < getStateDim(); ++i) {
98  task->getParameter(i).minVal = minState;
99  }
100  return this;
101 }
102 
103 OMTaskPositions* OMTaskPositions::setMinState(std::vector<double> minState)
104 {
105  RCHECK_EQ(getStateDim(), minState.size());
106  for (size_t i = 0; i < getStateDim(); ++i) {
107  task->getParameter(i).minVal = minState[i];
108  }
109  return this;
110 }
111 
113 {
114  for (size_t i = 0; i < getStateDim(); ++i) {
115  task->getParameter(i).maxVal = maxState;
116  }
117  return this;
118 }
119 
120 OMTaskPositions* OMTaskPositions::setMaxState(std::vector<double> maxState)
121 {
122  RCHECK_EQ(getStateDim(), maxState.size());
123  for (size_t i = 0; i < getStateDim(); ++i) {
124  task->getParameter(i).maxVal = maxState[i];
125  }
126  return this;
127 }
128 
129 void OMTaskPositions::initTaskBodyNames(const char* effectorName, const char* refBodyName, const char* refFrameName)
130 {
131  if (effectorName != NULL) {
132  RcsBody* effector = RcsGraph_getBodyByName(task->getGraph(), effectorName);
133  RCHECK_MSG(effector, "Effector body %s not found!", effectorName);
134  task->setEffector(effector);
135  }
136  if (refBodyName != NULL) {
137  RcsBody* refBody = RcsGraph_getBodyByName(task->getGraph(), refBodyName);
138  RCHECK_MSG(refBody, "Reference body %s not found!", refBodyName);
139  task->setRefBody(refBody);
140 
141  // If there is no separate reference frame, the relative coordinates should be in the frame of the reference body
142  if (refFrameName == NULL) {
143  task->setRefFrame(refBody);
144  }
145  }
146  if (refFrameName != NULL) {
147  RcsBody* refFrame = RcsGraph_getBodyByName(task->getGraph(), refFrameName);
148  RCHECK_MSG(refFrame, "Reference frame %s not found!", refFrameName);
149  task->setRefFrame(refFrame);
150  }
151 }
152 
153 } /* namespace Rcs */
154 
virtual std::vector< std::string > getStateNames() const
virtual void getLimits(double *minState, double *maxState, double *maxVelocity) const
virtual unsigned int getStateDim() const
OMTaskPositions * setMinState(double minState)
Task * task
Wrapped task object (owned!)
virtual void computeObservation(double *state, double *velocity, const MatNd *currentAction, double dt) const
void initTaskBodyNames(const char *effectorName, const char *refBodyName, const char *refFrameName)
virtual unsigned int getVelocityDim() const
OMTaskPositions * setMaxState(double maxState)