RcsPySim
A robot control and simulation library
OMDynamicalSystemDiscrepancy.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 
32 #include "../action/ActionModelIK.h"
33 #include "../util/eigen_matnd.h"
34 
35 #include <ControllerBase.h>
36 #include <Rcs_typedef.h>
37 #include <Rcs_macros.h>
38 
39 namespace Rcs
40 {
41 
43  actionModel)
44 {
45  auto amik = dynamic_cast<AMIKGeneric*>(actionModel->getWrappedActionModel());
46  RCHECK_MSG(amik, "AMDynamicalSystemActivation must wrap an AMIKGeneric");
47 
48  controller = new ControllerBase(actionModel->getGraph());
49  for (auto tsk : amik->getController()->getTasks()) {
50  controller->add(tsk->clone(actionModel->getGraph()));
51  }
52 
53  x_curr = MatNd_create(controller->getTaskDim(), 1);
54 }
55 
57 {
58  return 0; // does not have a velocity field
59 }
60 
62 {
63  delete controller;
64  MatNd_destroy(x_curr);
65 }
66 
68 {
69  return (unsigned int) controller->getTaskDim(); // equal the number of controller tasks
70 }
71 
73  double* state, double* velocity, const MatNd* currentAction,
74  double dt) const
75 {
76  // Save last state
77  Eigen::VectorXd x_last;
78  copyMatNd2Eigen(x_last, x_curr);
79 
80  // Compute current task state
81  controller->computeX(x_curr); // the controller is defined on the actionModel's graph
82 
83  // Compute actual movement in task space
84  Eigen::VectorXd delta_x = viewMatNd2Eigen(x_curr) - x_last;
85 
86  // Compute discrepancy between what the action model commanded and what the robot did (desired - actual)
87  Eigen::VectorXd accumulatedDiscrepancy = actionModel->getX() - viewMatNd2Eigen(x_curr);
88  Eigen::VectorXd incrementalDiscrepancy = actionModel->getXdot()*dt - delta_x;
89  Eigen::VectorXd velocityDiscrepancy = actionModel->getXdot() - delta_x/dt;
90 
91  // Print if debug level is exceeded
92  REXEC(7) {
93  std::cout << "Accumulated dynamical system discrepancy: " << accumulatedDiscrepancy << std::endl;
94  std::cout << "Incremental dynamical system discrepancy: " << incrementalDiscrepancy << std::endl;
95  }
96 
97 // state = &incrementalDiscrepancy(0);
98 // velocity = &velocityDiscrepancy(0);
99  for (unsigned int i = 0; i < getStateDim(); i++) {
100  state[i] = incrementalDiscrepancy[i];
101  }
102 }
103 
105 {
106  // fill x_curr with current state
107  controller->computeX(x_curr);
108 }
109 
110 std::vector<std::string> OMDynamicalSystemDiscrepancy::getStateNames() const
111 {
112  std::vector<std::string> result;
113  result.reserve(getStateDim());
114 
115  // Get names from controller tasks
116  for (auto task : controller->getTasks()) {
117  std::ostringstream prefix;
118  std::string name = "UNSET";
119  if (task->getEffector()) {
120  // If there is an effector, we use its name
121  name = task->getEffector()->name;
122  }
123  else {
124  // If not, e.g. for TaskJoint, then we use
125  name = task->getName();
126  }
127  prefix << "DiscrepDS_" << name << "_";
128  for (auto param : task->getParameters()) {
129  auto paramName = param.name;
130  // The tasks do report their var names, but unfortunately also include the unit in that string.
131  // We have to strip that.
132  auto spaceIdx = paramName.find(' ');
133  if (spaceIdx != std::string::npos) {
134  paramName = paramName.substr(0, spaceIdx);
135  }
136 
137  result.push_back(prefix.str() + paramName);
138  }
139  }
140  return result;
141 }
142 
143 } /* namespace Rcs */
void copyMatNd2Eigen(M &dst, const MatNd *src)
Definition: eigen_matnd.h:120
Eigen::Map< MatNdMappable< M > > viewMatNd2Eigen(MatNd *src)
Definition: eigen_matnd.h:88
RcsGraph * getGraph()
Definition: ActionModel.h:111
OMDynamicalSystemDiscrepancy(AMDynamicalSystemActivation *actionModel)
virtual ActionModel * getWrappedActionModel() const
unsigned int getVelocityDim() const override
AMDynamicalSystemActivation * actionModel
virtual std::vector< std::string > getStateNames() const
virtual void computeObservation(double *state, double *velocity, const MatNd *currentAction, double dt) const