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