DMPInstance.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package RobotAPI::ArmarXObjects::DMPComponent
17  * @author Mirko Waechter ( mirko dot waechter at kit dot edu )
18  * @date 2015
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #pragma once
24 
25 #include <MMM/MMMCore.h>
26 
27 #include <dmp/representation/dmp/dmpregistration.h>
28 
29 #include <RobotComponents/interface/components/DMPComponentBase.h>
30 
33 
34 //#include <dmp/representation/dmp/dmpregistration.h>
35 #include "dmp/representation/dmp/dmpinterface.h"
36 #include "dmp/representation/dmp/basicdmp.h"
37 #include "dmp/representation/dmp/dmp3rdorder.h"
38 #include "dmp/representation/dmp/quaterniondmp.h"
39 #include "dmp/representation/dmp/endvelodmp.h"
40 #include "dmp/representation/dmp/dmp3rdorderforcefield.h"
41 #include "dmp/representation/dmp/forcefielddmp.h"
42 #include "dmp/representation/dmp/adaptive3rdorderdmp.h"
43 #include "dmp/representation/dmp/simpleendvelodmp.h"
44 #include "dmp/representation/dmp/endveloforcefielddmp.h"
45 #include "dmp/representation/dmp/endveloforcefieldwithobjrepulsiondmp.h"
46 #include "dmp/representation/dmp/periodicdmp.h"
47 #include "dmp/representation/dmp/taskspacedmp.h"
48 
49 
50 namespace armarx
51 {
52 
53 #define ARMARX_DMPTYPE_BASICDMP boost::serialization::guid<DMP::BasicDMP>()
54 #define ARMARX_DMPTYPE_ENDVELODMP boost::serialization::guid<DMP::EndVeloDMP>()
55 #define ARMARX_DMPTYPE_SIMPLEENDVELODMP boost::serialization::guid<DMP::SimpleEndVeloDMP>()
56 #define ARMARX_DMPTYPE_FORCEFIELDDMP boost::serialization::guid<DMP::ForceFieldDMP>()
57 #define ARMARX_DMPTYPE_ENDVELFORCEFILELDDMP boost::serialization::guid<DMP::EndVeloForceFieldDMP>()
58 #define ARMARX_DMPTYPE_DMP3RDORDER boost::serialization::guid<DMP::DMP3rdOrder>()
59 #define ARMARX_DMPTYPE_DMP3RDORDERFORCEFIELD boost::serialization::guid<DMP::DMP3rdOrderForceField>()
60 #define ARMARX_DMPTYPE_ADAPTIVEGOAL3RDORDERDMP boost::serialization::guid<DMP::AdaptiveGoal3rdOrderDMP>()
61 #define ARMARX_DMPTYPE_QUATERNIONDMP boost::serialization::guid<DMP::QuaternionDMP>()
62 #define ARMARX_DMPTYPE_PERIODICDMP boost::serialization::guid<DMP::PeriodicDMP>()
63 #define ARMARX_DMPTYPE_TASKSPACEDMP boost::serialization::guid<DMP::TaskSpaceDMP>()
64 
65 
66  using paraType = boost::variant<double, DMP::DVec, Eigen::Quaternionf>;
67  using configMap = std::map<std::string, paraType >;
68  using configPair = std::pair<std::string, paraType >;
69  using TrajVec = DMP::Vec<DMP::SampledTrajectoryV2>;
70 
71 
72 
73  /**
74  * @defgroup DMPInstance DMPInstance
75  * @ingroup RobotComponents-Components
76  * @brief The DMP Instance class
77  *
78  * Manages an Instance of a DMP, the DMP can be of different types. Each Instance manages exactly one DMP, the Instances are managed with the @see DMPComponent
79  * The Instance manages the Configuration of the DMP and can calculate the states of the DMP and the whole Trajectory for all dimensions for different start and target positions.
80  *
81  */
82  class DMPInstance :
83  virtual public armarx::Logging,
84  virtual public DMPInstanceBase
85  {
86 
87  public:
88  /**
89  * @brief DMPInstance constructs an empty instance of a basicDMP
90  */
93  dmp(new DMP::BasicDMP()),
94  ctime(0),
95  timestep(0.001)
96 
97  {}
98 
99  DMPInstance(DMP::DMPInterfacePtr dmpInter)
100  {
101  dmp = dmpInter;
102  dmpType = dmp->getDMPType();
103  ctime = 0;
104  timestep = 0.001;
105  }
106 
107  /**
108  * @brief DMPInstance constructs a new instance of the specified DMP
109  * @param dmpInter pointer to the dmp
110  * @param DMPType type of dmp
111  */
112  DMPInstance(DMP::DMPInterfacePtr dmpInter, std::string DMPType);
113 
114  // setter functions
115  /**
116  * @brief setParameter set parameters for
117  * @param paraId pointer to the dmp
118  * @param value type of dmp
119  */
120  void setParameter(const std::string& paraId, const Ice::DoubleSeq& value, const ::Ice::Current& = Ice::emptyCurrent) override;
121 
122  /**
123  * @brief setGoal set the goal for dmp
124  * @param value the vector used to specify goal vector
125  */
126  void setGoal(const Ice::DoubleSeq& value, const ::Ice::Current& = Ice::emptyCurrent) override;
127 
128  /**
129  * @brief setStartPosition set the start position for dmp
130  * @param value: the vector used to define start position
131  */
132  void setStartPosition(const Ice::DoubleSeq& value, const ::Ice::Current& = Ice::emptyCurrent) override;
133 
134  /**
135  * @brief setDMPState set current state for dmp
136  * @param state state to set
137  */
138  void setDMPState(const cStateVec& state, const ::Ice::Current& = Ice::emptyCurrent) override;
139 
140  /**
141  * @brief setConfigurationMap set dmp configurations using configMap
142  * @param value: configMap saves all possible configurations for dmp
143  */
144  void setConfigurationMap(const DMPConfigMap& value, const ::Ice::Current& = Ice::emptyCurrent) override;
145 
146  /**
147  * @brief setCanonicalValues: set dmp canonicalValues to control the dmp's state
148  * @param value: current canonical values needed
149  */
150  void setCanonicalValues(const Ice::DoubleSeq& value, const ::Ice::Current& = Ice::emptyCurrent) override;
151 
152  /**
153  * @brief setTemporalFactor: set dmp temporal factor
154  */
155  void setTemporalFactor(double value, const ::Ice::Current& = Ice::emptyCurrent) override;
156 
157  /**
158  * @brief setAmpl: set amplitude for dmp
159  * @param dim: the dimension concerned
160  * @param value: new amplitude
161  */
162  void setAmpl(int dim, double value, const ::Ice::Current& = Ice::emptyCurrent) override;
163 
164 
165  // getter functions
166  /**
167  * @brief getAmpl: get current amplitude of a dimension
168  * @param dim: the dimension concerned
169  */
170  double getAmpl(int dim, const ::Ice::Current& = Ice::emptyCurrent) override;
171 
172  /**
173  * @brief getForceTerm: get force termf for one dimension at a specified canonical values
174  * @param canVal: corresponding canonical value
175  * @param dim: the dimension concerned
176  */
177  double getForceTerm(const Ice::DoubleSeq& canVal, int dim, const ::Ice::Current& = Ice::emptyCurrent) override;
178 
179  /**
180  * @brief getTemporalFactor: get current temporal factor
181  */
182  double getTemporalFactor(const ::Ice::Current& = Ice::emptyCurrent) override;
183 
184  /**
185  * @brief getCanonicalValues: get current canonical values
186  */
187  Ice::DoubleSeq getCanonicalValues(const ::Ice::Current& = Ice::emptyCurrent) override
188  {
189  return canonicalValues;
190  }
191 
192  /**
193  * @brief getDampingFactor: get dmp's damping factor
194  */
195  double getDampingFactor(const ::Ice::Current& = Ice::emptyCurrent) override
196  {
197  return dmp->getDampingFactor();
198  }
199 
200  /**
201  * @brief getSpringFactor: get dmp's spring factor
202  */
203  double getSpringFactor(const ::Ice::Current& = Ice::emptyCurrent) override
204  {
205  return dmp->getSpringFactor();
206  }
207 
208  /**
209  * @brief getDMPType: get dmp type
210  */
211  std::string getDMPType(const ::Ice::Current& = Ice::emptyCurrent) override
212  {
213  return dmp->getDMPType();
214  }
215 
216  /**
217  * @brief getDMPDim: get dimesions of dmp
218  */
219  int getDMPDim(const ::Ice::Current& = Ice::emptyCurrent) override
220  {
221  return dmp->dim();
222  }
223 
224  /**
225  * @brief getStartPosition: get start position of dmp
226  */
227  Ice::DoubleSeq getStartPosition(const ::Ice::Current& = Ice::emptyCurrent) override
228  {
229  return dmp->getStartPositions();
230  }
231 
232  /**
233  * @brief getTrajGoal: get the goal of the training trajectory
234  */
235  Ice::DoubleSeq getTrajGoal(const ::Ice::Current& = Ice::emptyCurrent) override;
236 
237  /**
238  * @brief getTrajStartState: get the start state of the training trajectory
239  */
240  cStateVec getTrajStartState(const ::Ice::Current& = Ice::emptyCurrent) override;
241 
242  Ice::DoubleSeq getGoal(const Ice::Current& = Ice::emptyCurrent);
243 
244 
245  // DMP trainer
246  void trainDMP(const ::Ice::Current& = Ice::emptyCurrent) override;
247 
248  void readTrajectoryFromFile(const std::string& file, double times = 1, const ::Ice::Current& = Ice::emptyCurrent) override;
249 
250 
251  // dmp calculation
252  /**
253  * @brief getNextState: get next state according to the state given
254  */
255  cStateVec getNextStateWithCurrentState(const ::Ice::Current& = Ice::emptyCurrent) override
256  {
257  return getNextState(getCurrentState());
258  }
259  /**
260  * @brief getNextState: get next state according to the state given
261  * @param states: the current state given to the function
262  */
263  cStateVec getNextState(const cStateVec& states, const ::Ice::Current& = Ice::emptyCurrent) override;
264  /**
265  * @brief getCurrentState: get current state in dmp
266  */
267  cStateVec getCurrentState(const ::Ice::Current& = Ice::emptyCurrent) override
268  {
269  return dmpState;
270  }
271 
272  /**
273  * @brief calcTrajectory solves the DMP for all Timesteps
274  *
275  * Calculates the Trajectory for the trained DMP given the goal and inital State for all Dimensions
276  *
277  * @param timestamps The timesteps for which the trajecotry should be calculated
278  * @param goal The target(goal) position for all dimensions of the trajectory
279  * @param initStates the initial States of the DMP
280  * @param canonicalValues The canonical Value for the DMP
281  * @param temporalFactor his factor determines the duration of the
282  * trajectory. The duration scales linear with this factor.
283  * @return The Trajectory which is created by the DMP, containing all the States of the DMP for each timestep
284  */
285  Vec2D calcTrajectory(double startTime, double timestep, double endTime,
286  const ::Ice::DoubleSeq& goal, const cStateVec& initStates, const ::Ice::DoubleSeq& canonicalValues, double temporalFactor, const Ice::Current& = Ice::emptyCurrent) override;
287 
288 
289 
290 
291  // local functions
292  configMap createConfigMap(DMP::Vec<std::string> paraIDs, DMP::Vec<paraType> paraVals);
293 
294  DMP::DMPInterfacePtr getDMP()
295  {
296  return dmp;
297  }
298 
299  // time manager
300  void setTimeStep(double ts)
301  {
302  timestep = ts;
303  }
304 
305  void setCurrentTime(double t)
306  {
307  ctime = t;
308  }
309 
310  double getCurrentTime()
311  {
312  return ctime;
313  }
314  nStateValues calcNextStateFromConfig(double t, double currentT, const Vec2D& currentStates, const Ice::DoubleSeq& canonicalValues, const Ice::Current& = Ice::emptyCurrent) override;
315  nStateValues calcNextState(double t, const Ice::DoubleSeq& goal, double currentT, const Vec2D& currentStates, const Ice::DoubleSeq& canonicalValues, double temporalFactor, const::Ice::Current& = Ice::emptyCurrent) override;
316  Vec2D calcTrajectoryV2(const Ice::DoubleSeq& timestamps, const Ice::DoubleSeq& goal, const Vec2D& initStates, const Ice::DoubleSeq& canonicalValues, double temporalFactor, const Ice::Current& = Ice::emptyCurrent) override;
317  Vec2D calcTrajectoryFromConfig(const Ice::DoubleSeq& timestamps, const Vec2D& initStates, const Ice::DoubleSeq& canonicalValues, const Ice::Current& = Ice::emptyCurrent) override;
318  protected:
319  std::string dmpType;
320  DMP::DMPInterfacePtr dmp;
322  double ctime;
323 
324  double timestep;
325 
327  std::string dmpName;
328  DMP::DVec canonicalValues;
329  cStateVec dmpState;
330 
331  };
332 
333 
335 
336 }
337 
armarx::DMPInstance::trainDMP
void trainDMP(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPInstance.cpp:391
armarx::configPair
std::pair< std::string, paraType > configPair
Definition: DMPInstance.h:68
armarx::DMPInstance::getStartPosition
Ice::DoubleSeq getStartPosition(const ::Ice::Current &=Ice::emptyCurrent) override
getStartPosition: get start position of dmp
Definition: DMPInstance.h:227
armarx::DMPInstance::getDMP
DMP::DMPInterfacePtr getDMP()
Definition: DMPInstance.h:294
armarx::DMPInstance::calcTrajectoryFromConfig
Vec2D calcTrajectoryFromConfig(const Ice::DoubleSeq &timestamps, const Vec2D &initStates, const Ice::DoubleSeq &canonicalValues, const Ice::Current &=Ice::emptyCurrent) override
Definition: DMPInstance.cpp:473
armarx::DMPInstance::dmpName
std::string dmpName
Definition: DMPInstance.h:327
armarx::DMPInstance::calcTrajectoryV2
Vec2D calcTrajectoryV2(const Ice::DoubleSeq &timestamps, const Ice::DoubleSeq &goal, const Vec2D &initStates, const Ice::DoubleSeq &canonicalValues, double temporalFactor, const Ice::Current &=Ice::emptyCurrent) override
Definition: DMPInstance.cpp:433
armarx::DMPInstance::getAmpl
double getAmpl(int dim, const ::Ice::Current &=Ice::emptyCurrent) override
getAmpl: get current amplitude of a dimension
Definition: DMPInstance.cpp:294
armarx::DMPInstance::configs
configMap configs
Definition: DMPInstance.h:321
armarx::DMPInstance::setDMPState
void setDMPState(const cStateVec &state, const ::Ice::Current &=Ice::emptyCurrent) override
setDMPState set current state for dmp
Definition: DMPInstance.cpp:286
armarx::DMPInstance::getDampingFactor
double getDampingFactor(const ::Ice::Current &=Ice::emptyCurrent) override
getDampingFactor: get dmp's damping factor
Definition: DMPInstance.h:195
armarx::DMPInstance::getCurrentState
cStateVec getCurrentState(const ::Ice::Current &=Ice::emptyCurrent) override
getCurrentState: get current state in dmp
Definition: DMPInstance.h:267
armarx::DMPInstance::getDMPDim
int getDMPDim(const ::Ice::Current &=Ice::emptyCurrent) override
getDMPDim: get dimesions of dmp
Definition: DMPInstance.h:219
armarx::DMPInstance::setParameter
void setParameter(const std::string &paraId, const Ice::DoubleSeq &value, const ::Ice::Current &=Ice::emptyCurrent) override
setParameter set parameters for
Definition: DMPInstance.cpp:230
armarx::DMPInstance::ctime
double ctime
Definition: DMPInstance.h:322
armarx::paraType
boost::variant< double, DMP::DVec, Eigen::Quaternionf > paraType
Definition: DMPInstance.h:66
armarx::DMPInstance::getCurrentTime
double getCurrentTime()
Definition: DMPInstance.h:310
armarx::DMPInstance::dmp
DMP::DMPInterfacePtr dmp
Definition: DMPInstance.h:320
armarx::DMPInstance::setTemporalFactor
void setTemporalFactor(double value, const ::Ice::Current &=Ice::emptyCurrent) override
setTemporalFactor: set dmp temporal factor
Definition: DMPInstance.cpp:255
armarx::DMPInstance::setStartPosition
void setStartPosition(const Ice::DoubleSeq &value, const ::Ice::Current &=Ice::emptyCurrent) override
setStartPosition set the start position for dmp
Definition: DMPInstance.cpp:273
armarx::DMPInstance::getTrajStartState
cStateVec getTrajStartState(const ::Ice::Current &=Ice::emptyCurrent) override
getTrajStartState: get the start state of the training trajectory
Definition: DMPInstance.cpp:336
armarx::DMPInstance::setConfigurationMap
void setConfigurationMap(const DMPConfigMap &value, const ::Ice::Current &=Ice::emptyCurrent) override
setConfigurationMap set dmp configurations using configMap
Definition: DMPInstance.cpp:195
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::configMap
std::map< std::string, paraType > configMap
Definition: DMPInstance.h:67
armarx::DMPInstance
Definition: DMPInstance.h:82
armarx::DMPInstance::readTrajectoryFromFile
void readTrajectoryFromFile(const std::string &file, double times=1, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPInstance.cpp:353
armarx::DMPInstance::getSpringFactor
double getSpringFactor(const ::Ice::Current &=Ice::emptyCurrent) override
getSpringFactor: get dmp's spring factor
Definition: DMPInstance.h:203
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::DMPInstance::setTimeStep
void setTimeStep(double ts)
Definition: DMPInstance.h:300
armarx::DMPInstance::calcNextState
nStateValues calcNextState(double t, const Ice::DoubleSeq &goal, double currentT, const Vec2D &currentStates, const Ice::DoubleSeq &canonicalValues, double temporalFactor, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPInstance.cpp:129
armarx::DMPInstance::getTrajGoal
Ice::DoubleSeq getTrajGoal(const ::Ice::Current &=Ice::emptyCurrent) override
getTrajGoal: get the goal of the training trajectory
Definition: DMPInstance.cpp:324
armarx::DMPInstance::dmpState
cStateVec dmpState
Definition: DMPInstance.h:329
armarx::DMPInstance::getNextStateWithCurrentState
cStateVec getNextStateWithCurrentState(const ::Ice::Current &=Ice::emptyCurrent) override
getNextState: get next state according to the state given
Definition: DMPInstance.h:255
armarx::DMPInstance::calcTrajectory
Vec2D calcTrajectory(double startTime, double timestep, double endTime, const ::Ice::DoubleSeq &goal, const cStateVec &initStates, const ::Ice::DoubleSeq &canonicalValues, double temporalFactor, const Ice::Current &=Ice::emptyCurrent) override
calcTrajectory solves the DMP for all Timesteps
Definition: DMPInstance.cpp:397
armarx::DMPInstance::getGoal
Ice::DoubleSeq getGoal(const Ice::Current &=Ice::emptyCurrent)
Definition: DMPInstance.cpp:309
armarx::DMPInstance::canonicalValues
DMP::DVec canonicalValues
Definition: DMPInstance.h:328
armarx::DMPInstance::trajs
TrajVec trajs
Definition: DMPInstance.h:326
armarx::DMPInstance::setCurrentTime
void setCurrentTime(double t)
Definition: DMPInstance.h:305
armarx::DMPInstance::getCanonicalValues
Ice::DoubleSeq getCanonicalValues(const ::Ice::Current &=Ice::emptyCurrent) override
getCanonicalValues: get current canonical values
Definition: DMPInstance.h:187
armarx::DMPInstance::dmpType
std::string dmpType
Definition: DMPInstance.h:319
armarx::DMPInstance::getNextState
cStateVec getNextState(const cStateVec &states, const ::Ice::Current &=Ice::emptyCurrent) override
getNextState: get next state according to the state given
Definition: DMPInstance.cpp:97
Component.h
armarx::TrajVec
DMP::Vec< DMP::SampledTrajectoryV2 > TrajVec
Definition: DMPInstance.h:69
armarx::DMPInstance::setGoal
void setGoal(const Ice::DoubleSeq &value, const ::Ice::Current &=Ice::emptyCurrent) override
setGoal set the goal for dmp
Definition: DMPInstance.cpp:260
ARMARX_DMPTYPE_BASICDMP
#define ARMARX_DMPTYPE_BASICDMP
Definition: DMPInstance.h:53
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:232
armarx::DMPInstance::calcNextStateFromConfig
nStateValues calcNextStateFromConfig(double t, double currentT, const Vec2D &currentStates, const Ice::DoubleSeq &canonicalValues, const Ice::Current &=Ice::emptyCurrent) override
Definition: DMPInstance.cpp:185
armarx::DMPInstance::getForceTerm
double getForceTerm(const Ice::DoubleSeq &canVal, int dim, const ::Ice::Current &=Ice::emptyCurrent) override
getForceTerm: get force termf for one dimension at a specified canonical values
Definition: DMPInstance.cpp:304
armarx::DMPInstance::getDMPType
std::string getDMPType(const ::Ice::Current &=Ice::emptyCurrent) override
getDMPType: get dmp type
Definition: DMPInstance.h:211
armarx::DMPInstance::createConfigMap
configMap createConfigMap(DMP::Vec< std::string > paraIDs, DMP::Vec< paraType > paraVals)
Definition: DMPInstance.cpp:207
armarx::DMPInstance::setAmpl
void setAmpl(int dim, double value, const ::Ice::Current &=Ice::emptyCurrent) override
setAmpl: set amplitude for dmp
Definition: DMPInstance.cpp:250
armarx::DMPInstance::DMPInstance
DMPInstance()
DMPInstance constructs an empty instance of a basicDMP.
Definition: DMPInstance.h:91
armarx::DMPInstance::getTemporalFactor
double getTemporalFactor(const ::Ice::Current &=Ice::emptyCurrent) override
getTemporalFactor: get current temporal factor
Definition: DMPInstance.cpp:299
Logging.h
armarx::DMPInstance::DMPInstance
DMPInstance(DMP::DMPInterfacePtr dmpInter)
Definition: DMPInstance.h:99
armarx::DMPInstance::setCanonicalValues
void setCanonicalValues(const Ice::DoubleSeq &value, const ::Ice::Current &=Ice::emptyCurrent) override
setCanonicalValues: set dmp canonicalValues to control the dmp's state
Definition: DMPInstance.cpp:245
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::DMPInstance::timestep
double timestep
Definition: DMPInstance.h:324