RcsPySim
A robot control and simulation library
OMDynamicalSystemGoalDistance.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 #include <Rcs_VecNd.h>
39 
40 #include <algorithm>
41 #include <limits>
42 
43 namespace Rcs
44 {
45 
47  actionModel(actionModel),
48  maxDistance(std::numeric_limits<double>::infinity())
49 {
50  auto amik = dynamic_cast<AMIKGeneric*>(actionModel->getWrappedActionModel());
51  RCHECK_MSG(amik, "AMDynamicalSystemActivation must wrap an AMIKGeneric");
52 
53  controller = new ControllerBase(actionModel->getGraph());
54  for (auto tsk : amik->getController()->getTasks()) {
55  controller->add(tsk->clone(actionModel->getGraph()));
56  }
57 }
58 
60 {
61  delete controller;
62 }
63 
65 {
66  return actionModel->getDim();
67 }
68 
70 {
71  return 0;
72 }
73 
75  double* state, double* velocity, const MatNd* currentAction, double dt) const
76 {
77  // Compute controller state
78  Eigen::VectorXd x_curr = Eigen::VectorXd::Zero(controller->getTaskDim());
79  MatNd x_curr_mat = viewEigen2MatNd(x_curr);
80  controller->computeX(&x_curr_mat);
81 
82  // Compute goal distance derivative
83  auto& tasks = actionModel->getDynamicalSystems();
84  for (size_t i = 0; i < tasks.size(); ++i) {
85  // Compute distance
86  double dist = tasks[i]->goalDistance(x_curr);
87  state[i] = dist;
88 
89  // DEBUG
90  REXEC(7) {
91  std::cout << "goal distance pos of task " << i << std::endl << state[i] << std::endl;
92  }
93  }
94 }
95 
96 void OMDynamicalSystemGoalDistance::getLimits(double* minState, double* maxState, double* maxVelocity) const
97 {
98  VecNd_setZero(minState, getStateDim()); // minimum distance is 0
99  VecNd_setElementsTo(maxState, maxDistance, getStateDim());
100 }
101 
102 std::vector<std::string> OMDynamicalSystemGoalDistance::getStateNames() const
103 {
104  std::vector<std::string> result;
105  result.reserve(getStateDim());
106  for (size_t ds = 0; ds < actionModel->getDynamicalSystems().size(); ++ds) {
107  std::ostringstream os;
108  os << "GD_DS" << ds;
109  result.push_back(os.str());
110  }
111  return result;
112 }
113 
114 } /* namespace Rcs */
RcsGraph * getGraph()
Definition: ActionModel.h:111
virtual std::vector< std::string > getStateNames() const
AMDynamicalSystemActivation * actionModel
Task activation action model, provides the tasks (not owned)
virtual void computeObservation(double *state, double *velocity, const MatNd *currentAction, double dt) const
virtual unsigned int getDim() const
Get the number of DS, i.e. entries in the dynamicalSystems vector, owned by the action model...
virtual void getLimits(double *minState, double *maxState, double *maxVelocity) const
virtual ActionModel * getWrappedActionModel() const
ControllerBase * controller
Controller to extract the task space state.
const std::vector< DynamicalSystem * > & getDynamicalSystems() const
Get a vector of the owned dynamical systems.
MatNd viewEigen2MatNd(M &src)
Definition: eigen_matnd.h:48
OMDynamicalSystemGoalDistance(AMDynamicalSystemActivation *actionModel)