RcsPySim
A robot control and simulation library
AMIntegrate2ndOrder.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 "AMIntegrate2ndOrder.h"
32 
33 #include "../util/integrator.h"
34 
35 #include <Rcs_VecNd.h>
36 #include <Rcs_macros.h>
37 
38 
39 namespace Rcs
40 {
41 
43  ActionModel(wrapped->getGraph()),
44  wrapped(wrapped)
45 {
46  // Create integrator state storage
47  integrated_action = MatNd_create(wrapped->getDim(), 1);
48  integrated_action_dot = MatNd_create(wrapped->getDim(), 1);
49 
50  // Store max_action
51  this->maxAction = MatNd_create(wrapped->getDim(), 1);
52  VecNd_setElementsTo(this->maxAction->ele, maxAction, wrapped->getDim());
53 }
54 
56  ActionModel(wrapped->getGraph()),
57  wrapped(wrapped)
58 {
59  // create integrator state storage
60  integrated_action = MatNd_create(wrapped->getDim(), 1);
61  integrated_action_dot = MatNd_create(wrapped->getDim(), 1);
62 
63  // store max_action
64  RCHECK_MSG(maxAction->m == wrapped->getDim() && maxAction->n == 1, "MaxAction shape must match action dim.");
65  this->maxAction = MatNd_clone(maxAction);
66 }
67 
68 
70 {
71  MatNd_destroy(integrated_action_dot);
72  MatNd_destroy(integrated_action);
73  MatNd_destroy(maxAction);
74  delete wrapped;
75 }
76 
77 unsigned int AMIntegrate2ndOrder::getDim() const
78 {
79  return wrapped->getDim();
80 }
81 
82 void AMIntegrate2ndOrder::getMinMax(double* min, double* max) const
83 {
84  VecNd_constMul(min, maxAction->ele, -1, getDim());
85  VecNd_copy(max, maxAction->ele, getDim());
86 }
87 
88 std::vector<std::string> AMIntegrate2ndOrder::getNames() const
89 {
90  auto wnames = wrapped->getNames();
91  // add the suffix 'dd' to every var to signal that it's the second order derivative
92  for (auto& name : wnames) {
93  name += "dd";
94  }
95  return wnames;
96 }
97 
98 void AMIntegrate2ndOrder::computeCommand(MatNd* q_des, MatNd* q_dot_des, MatNd* T_des, const MatNd* action, double dt)
99 {
100  // perform integration
102 
103  // pass integrated values to wrapped
104  wrapped->computeCommand(q_des, q_dot_des, T_des, integrated_action, dt);
105 }
106 
108 {
109  wrapped->reset();
110  // reset integrator state to current (initial)
112  MatNd_setZero(integrated_action_dot);
113 }
114 
115 void AMIntegrate2ndOrder::getStableAction(MatNd* action) const
116 {
117  // acceleration of 0 is stable
118  MatNd_setZero(action);
119 }
120 
122 {
123  return wrapped;
124 }
125 
126 ActionModel* AMIntegrate2ndOrder::clone(RcsGraph* newGraph) const
127 {
128  return new AMIntegrate2ndOrder(wrapped->clone(newGraph), maxAction->ele[0]);
129 }
130 
131 } /* namespace Rcs */
virtual ActionModel * getWrappedActionModel() const
virtual std::vector< std::string > getNames() const
void intStep2ndOrder(MatNd *x, MatNd *xd, const MatNd *xdd, double dt, IntMode mode)
Definition: integrator.cpp:38
RcsGraph * getGraph()
Definition: ActionModel.h:111
virtual std::vector< std::string > getNames() const
virtual void computeCommand(MatNd *q_des, MatNd *q_dot_des, MatNd *T_des, const MatNd *action, double dt)=0
AMIntegrate2ndOrder(ActionModel *wrapped, double maxAction)
virtual void getStableAction(MatNd *action) const
virtual unsigned int getDim() const
virtual void getMinMax(double *min, double *max) const
virtual void reset()
Definition: ActionModel.cpp:49
ActionModel * clone() const
Definition: ActionModel.h:72
virtual unsigned int getDim() const =0
virtual void getStableAction(MatNd *action) const =0
virtual void computeCommand(MatNd *q_des, MatNd *q_dot_des, MatNd *T_des, const MatNd *action, double dt)