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 
27 
28 #include <RobotComponents/interface/components/DMPComponentBase.h>
29 
30 #include <MMM/MMMCore.h>
31 #include <dmp/representation/dmp/dmpregistration.h>
32 
33 //#include <dmp/representation/dmp/dmpregistration.h>
34 #include "dmp/representation/dmp/adaptive3rdorderdmp.h"
35 #include "dmp/representation/dmp/basicdmp.h"
36 #include "dmp/representation/dmp/dmp3rdorder.h"
37 #include "dmp/representation/dmp/dmp3rdorderforcefield.h"
38 #include "dmp/representation/dmp/dmpinterface.h"
39 #include "dmp/representation/dmp/endvelodmp.h"
40 #include "dmp/representation/dmp/endveloforcefielddmp.h"
41 #include "dmp/representation/dmp/endveloforcefieldwithobjrepulsiondmp.h"
42 #include "dmp/representation/dmp/forcefielddmp.h"
43 #include "dmp/representation/dmp/periodicdmp.h"
44 #include "dmp/representation/dmp/quaterniondmp.h"
45 #include "dmp/representation/dmp/simpleendvelodmp.h"
46 #include "dmp/representation/dmp/taskspacedmp.h"
47 
48 namespace armarx
49 {
50 
51 #define ARMARX_DMPTYPE_BASICDMP boost::serialization::guid<DMP::BasicDMP>()
52 #define ARMARX_DMPTYPE_ENDVELODMP boost::serialization::guid<DMP::EndVeloDMP>()
53 #define ARMARX_DMPTYPE_SIMPLEENDVELODMP boost::serialization::guid<DMP::SimpleEndVeloDMP>()
54 #define ARMARX_DMPTYPE_FORCEFIELDDMP boost::serialization::guid<DMP::ForceFieldDMP>()
55 #define ARMARX_DMPTYPE_ENDVELFORCEFILELDDMP boost::serialization::guid<DMP::EndVeloForceFieldDMP>()
56 #define ARMARX_DMPTYPE_DMP3RDORDER boost::serialization::guid<DMP::DMP3rdOrder>()
57 #define ARMARX_DMPTYPE_DMP3RDORDERFORCEFIELD \
58  boost::serialization::guid<DMP::DMP3rdOrderForceField>()
59 #define ARMARX_DMPTYPE_ADAPTIVEGOAL3RDORDERDMP \
60  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  * @defgroup DMPInstance DMPInstance
73  * @ingroup RobotComponents-Components
74  * @brief The DMP Instance class
75  *
76  * 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
77  * 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.
78  *
79  */
80  class DMPInstance : virtual public armarx::Logging, virtual public DMPInstanceBase
81  {
82 
83  public:
84  /**
85  * @brief DMPInstance constructs an empty instance of a basicDMP
86  */
88  dmpType(ARMARX_DMPTYPE_BASICDMP), dmp(new DMP::BasicDMP()), ctime(0), timestep(0.001)
89 
90  {
91  }
92 
93  DMPInstance(DMP::DMPInterfacePtr dmpInter)
94  {
95  dmp = dmpInter;
96  dmpType = dmp->getDMPType();
97  ctime = 0;
98  timestep = 0.001;
99  }
100 
101  /**
102  * @brief DMPInstance constructs a new instance of the specified DMP
103  * @param dmpInter pointer to the dmp
104  * @param DMPType type of dmp
105  */
106  DMPInstance(DMP::DMPInterfacePtr dmpInter, std::string DMPType);
107 
108  // setter functions
109  /**
110  * @brief setParameter set parameters for
111  * @param paraId pointer to the dmp
112  * @param value type of dmp
113  */
114  void setParameter(const std::string& paraId,
115  const Ice::DoubleSeq& value,
116  const ::Ice::Current& = Ice::emptyCurrent) override;
117 
118  /**
119  * @brief setGoal set the goal for dmp
120  * @param value the vector used to specify goal vector
121  */
122  void setGoal(const Ice::DoubleSeq& value,
123  const ::Ice::Current& = Ice::emptyCurrent) override;
124 
125  /**
126  * @brief setStartPosition set the start position for dmp
127  * @param value: the vector used to define start position
128  */
129  void setStartPosition(const Ice::DoubleSeq& value,
130  const ::Ice::Current& = Ice::emptyCurrent) override;
131 
132  /**
133  * @brief setDMPState set current state for dmp
134  * @param state state to set
135  */
136  void setDMPState(const cStateVec& state,
137  const ::Ice::Current& = Ice::emptyCurrent) override;
138 
139  /**
140  * @brief setConfigurationMap set dmp configurations using configMap
141  * @param value: configMap saves all possible configurations for dmp
142  */
143  void setConfigurationMap(const DMPConfigMap& value,
144  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,
151  const ::Ice::Current& = Ice::emptyCurrent) override;
152 
153  /**
154  * @brief setTemporalFactor: set dmp temporal factor
155  */
156  void setTemporalFactor(double value, const ::Ice::Current& = Ice::emptyCurrent) override;
157 
158  /**
159  * @brief setAmpl: set amplitude for dmp
160  * @param dim: the dimension concerned
161  * @param value: new amplitude
162  */
163  void setAmpl(int dim, double value, const ::Ice::Current& = Ice::emptyCurrent) override;
164 
165 
166  // getter functions
167  /**
168  * @brief getAmpl: get current amplitude of a dimension
169  * @param dim: the dimension concerned
170  */
171  double getAmpl(int dim, const ::Ice::Current& = Ice::emptyCurrent) override;
172 
173  /**
174  * @brief getForceTerm: get force termf for one dimension at a specified canonical values
175  * @param canVal: corresponding canonical value
176  * @param dim: the dimension concerned
177  */
178  double getForceTerm(const Ice::DoubleSeq& canVal,
179  int dim,
180  const ::Ice::Current& = Ice::emptyCurrent) override;
181 
182  /**
183  * @brief getTemporalFactor: get current temporal factor
184  */
185  double getTemporalFactor(const ::Ice::Current& = Ice::emptyCurrent) override;
186 
187  /**
188  * @brief getCanonicalValues: get current canonical values
189  */
190  Ice::DoubleSeq
191  getCanonicalValues(const ::Ice::Current& = Ice::emptyCurrent) override
192  {
193  return canonicalValues;
194  }
195 
196  /**
197  * @brief getDampingFactor: get dmp's damping factor
198  */
199  double
200  getDampingFactor(const ::Ice::Current& = Ice::emptyCurrent) override
201  {
202  return dmp->getDampingFactor();
203  }
204 
205  /**
206  * @brief getSpringFactor: get dmp's spring factor
207  */
208  double
209  getSpringFactor(const ::Ice::Current& = Ice::emptyCurrent) override
210  {
211  return dmp->getSpringFactor();
212  }
213 
214  /**
215  * @brief getDMPType: get dmp type
216  */
217  std::string
218  getDMPType(const ::Ice::Current& = Ice::emptyCurrent) override
219  {
220  return dmp->getDMPType();
221  }
222 
223  /**
224  * @brief getDMPDim: get dimesions of dmp
225  */
226  int
227  getDMPDim(const ::Ice::Current& = Ice::emptyCurrent) override
228  {
229  return dmp->dim();
230  }
231 
232  /**
233  * @brief getStartPosition: get start position of dmp
234  */
235  Ice::DoubleSeq
236  getStartPosition(const ::Ice::Current& = Ice::emptyCurrent) override
237  {
238  return dmp->getStartPositions();
239  }
240 
241  /**
242  * @brief getTrajGoal: get the goal of the training trajectory
243  */
244  Ice::DoubleSeq getTrajGoal(const ::Ice::Current& = Ice::emptyCurrent) override;
245 
246  /**
247  * @brief getTrajStartState: get the start state of the training trajectory
248  */
249  cStateVec getTrajStartState(const ::Ice::Current& = Ice::emptyCurrent) override;
250 
251  Ice::DoubleSeq getGoal(const Ice::Current& = Ice::emptyCurrent);
252 
253 
254  // DMP trainer
255  void trainDMP(const ::Ice::Current& = Ice::emptyCurrent) override;
256 
257  void readTrajectoryFromFile(const std::string& file,
258  double times = 1,
259  const ::Ice::Current& = Ice::emptyCurrent) override;
260 
261  // dmp calculation
262  /**
263  * @brief getNextState: get next state according to the state given
264  */
265  cStateVec
266  getNextStateWithCurrentState(const ::Ice::Current& = Ice::emptyCurrent) override
267  {
268  return getNextState(getCurrentState());
269  }
270 
271  /**
272  * @brief getNextState: get next state according to the state given
273  * @param states: the current state given to the function
274  */
275  cStateVec getNextState(const cStateVec& states,
276  const ::Ice::Current& = Ice::emptyCurrent) override;
277 
278  /**
279  * @brief getCurrentState: get current state in dmp
280  */
281  cStateVec
282  getCurrentState(const ::Ice::Current& = Ice::emptyCurrent) override
283  {
284  return dmpState;
285  }
286 
287  /**
288  * @brief calcTrajectory solves the DMP for all Timesteps
289  *
290  * Calculates the Trajectory for the trained DMP given the goal and inital State for all Dimensions
291  *
292  * @param timestamps The timesteps for which the trajecotry should be calculated
293  * @param goal The target(goal) position for all dimensions of the trajectory
294  * @param initStates the initial States of the DMP
295  * @param canonicalValues The canonical Value for the DMP
296  * @param temporalFactor his factor determines the duration of the
297  * trajectory. The duration scales linear with this factor.
298  * @return The Trajectory which is created by the DMP, containing all the States of the DMP for each timestep
299  */
300  Vec2D calcTrajectory(double startTime,
301  double timestep,
302  double endTime,
303  const ::Ice::DoubleSeq& goal,
304  const cStateVec& initStates,
305  const ::Ice::DoubleSeq& canonicalValues,
306  double temporalFactor,
307  const Ice::Current& = Ice::emptyCurrent) override;
308 
309 
310  // local functions
311  configMap createConfigMap(DMP::Vec<std::string> paraIDs, DMP::Vec<paraType> paraVals);
312 
313  DMP::DMPInterfacePtr
315  {
316  return dmp;
317  }
318 
319  // time manager
320  void
321  setTimeStep(double ts)
322  {
323  timestep = ts;
324  }
325 
326  void
327  setCurrentTime(double t)
328  {
329  ctime = t;
330  }
331 
332  double
334  {
335  return ctime;
336  }
337 
338  nStateValues calcNextStateFromConfig(double t,
339  double currentT,
340  const Vec2D& currentStates,
341  const Ice::DoubleSeq& canonicalValues,
342  const Ice::Current& = Ice::emptyCurrent) override;
343  nStateValues calcNextState(double t,
344  const Ice::DoubleSeq& goal,
345  double currentT,
346  const Vec2D& currentStates,
347  const Ice::DoubleSeq& canonicalValues,
348  double temporalFactor,
349  const ::Ice::Current& = Ice::emptyCurrent) override;
350  Vec2D calcTrajectoryV2(const Ice::DoubleSeq& timestamps,
351  const Ice::DoubleSeq& goal,
352  const Vec2D& initStates,
353  const Ice::DoubleSeq& canonicalValues,
354  double temporalFactor,
355  const Ice::Current& = Ice::emptyCurrent) override;
356  Vec2D calcTrajectoryFromConfig(const Ice::DoubleSeq& timestamps,
357  const Vec2D& initStates,
358  const Ice::DoubleSeq& canonicalValues,
359  const Ice::Current& = Ice::emptyCurrent) override;
360 
361  protected:
362  std::string dmpType;
363  DMP::DMPInterfacePtr dmp;
365  double ctime;
366 
367  double timestep;
368 
370  std::string dmpName;
372  cStateVec dmpState;
373  };
374 
376 
377 } // namespace armarx
armarx::DMPInstance::trainDMP
void trainDMP(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPInstance.cpp:424
armarx::DMPInstance::getStartPosition
Ice::DoubleSeq getStartPosition(const ::Ice::Current &=Ice::emptyCurrent) override
getStartPosition: get start position of dmp
Definition: DMPInstance.h:236
armarx::DMPInstance::getDMP
DMP::DMPInterfacePtr getDMP()
Definition: DMPInstance.h:314
armarx::configMap
std::map< std::string, paraType > configMap
Definition: DMPInstance.h:67
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:513
armarx::DMPInstance::dmpName
std::string dmpName
Definition: DMPInstance.h:370
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:469
armarx::DMPInstance::getAmpl
double getAmpl(int dim, const ::Ice::Current &=Ice::emptyCurrent) override
getAmpl: get current amplitude of a dimension
Definition: DMPInstance.cpp:323
armarx::DMPInstance::configs
configMap configs
Definition: DMPInstance.h:364
armarx::DMPInstance::setDMPState
void setDMPState(const cStateVec &state, const ::Ice::Current &=Ice::emptyCurrent) override
setDMPState set current state for dmp
Definition: DMPInstance.cpp:315
DMP
Definition: NJointTaskSpaceAdaptiveDMPController.h:24
armarx::DMPInstance::getDampingFactor
double getDampingFactor(const ::Ice::Current &=Ice::emptyCurrent) override
getDampingFactor: get dmp's damping factor
Definition: DMPInstance.h:200
armarx::DMPInstance::getCurrentState
cStateVec getCurrentState(const ::Ice::Current &=Ice::emptyCurrent) override
getCurrentState: get current state in dmp
Definition: DMPInstance.h:282
armarx::DMPInstance::getDMPDim
int getDMPDim(const ::Ice::Current &=Ice::emptyCurrent) override
getDMPDim: get dimesions of dmp
Definition: DMPInstance.h:227
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:251
armarx::DMPInstance::ctime
double ctime
Definition: DMPInstance.h:365
armarx::paraType
boost::variant< double, DMP::DVec, Eigen::Quaternionf > paraType
Definition: DMPInstance.h:66
armarx::DMPInstance::getCurrentTime
double getCurrentTime()
Definition: DMPInstance.h:333
armarx::DMPInstance::dmp
DMP::DMPInterfacePtr dmp
Definition: DMPInstance.h:363
armarx::DMPInstance::setTemporalFactor
void setTemporalFactor(double value, const ::Ice::Current &=Ice::emptyCurrent) override
setTemporalFactor: set dmp temporal factor
Definition: DMPInstance.cpp:281
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:301
armarx::DMPInstance::getTrajStartState
cStateVec getTrajStartState(const ::Ice::Current &=Ice::emptyCurrent) override
getTrajStartState: get the start state of the training trajectory
Definition: DMPInstance.cpp:369
armarx::DMPInstance::setConfigurationMap
void setConfigurationMap(const DMPConfigMap &value, const ::Ice::Current &=Ice::emptyCurrent) override
setConfigurationMap set dmp configurations using configMap
Definition: DMPInstance.cpp:212
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::DMPInstance
Definition: DMPInstance.h:80
armarx::configPair
std::pair< std::string, paraType > configPair
Definition: DMPInstance.h:68
armarx::DMPInstance::readTrajectoryFromFile
void readTrajectoryFromFile(const std::string &file, double times=1, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPInstance.cpp:387
armarx::DMPInstance::getSpringFactor
double getSpringFactor(const ::Ice::Current &=Ice::emptyCurrent) override
getSpringFactor: get dmp's spring factor
Definition: DMPInstance.h:209
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::DMPInstance::setTimeStep
void setTimeStep(double ts)
Definition: DMPInstance.h:321
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:136
armarx::DMPInstance::getTrajGoal
Ice::DoubleSeq getTrajGoal(const ::Ice::Current &=Ice::emptyCurrent) override
getTrajGoal: get the goal of the training trajectory
Definition: DMPInstance.cpp:357
armarx::DMPInstance::dmpState
cStateVec dmpState
Definition: DMPInstance.h:372
armarx::DMPInstance::getNextStateWithCurrentState
cStateVec getNextStateWithCurrentState(const ::Ice::Current &=Ice::emptyCurrent) override
getNextState: get next state according to the state given
Definition: DMPInstance.h:266
armarx::control::common::mp::DVec
Ice::DoubleSeq DVec
Definition: MP.h:47
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:430
armarx::DMPInstance::getGoal
Ice::DoubleSeq getGoal(const Ice::Current &=Ice::emptyCurrent)
Definition: DMPInstance.cpp:341
armarx::DMPInstance::canonicalValues
DMP::DVec canonicalValues
Definition: DMPInstance.h:371
armarx::DMPInstance::trajs
TrajVec trajs
Definition: DMPInstance.h:369
armarx::DMPInstance::setCurrentTime
void setCurrentTime(double t)
Definition: DMPInstance.h:327
armarx::DMPInstance::getCanonicalValues
Ice::DoubleSeq getCanonicalValues(const ::Ice::Current &=Ice::emptyCurrent) override
getCanonicalValues: get current canonical values
Definition: DMPInstance.h:191
armarx::DMPInstance::dmpType
std::string dmpType
Definition: DMPInstance.h:362
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:103
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:287
ARMARX_DMPTYPE_BASICDMP
#define ARMARX_DMPTYPE_BASICDMP
Definition: DMPInstance.h:51
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:239
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:199
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:335
armarx::DMPInstance::getDMPType
std::string getDMPType(const ::Ice::Current &=Ice::emptyCurrent) override
getDMPType: get dmp type
Definition: DMPInstance.h:218
armarx::DMPInstance::createConfigMap
configMap createConfigMap(DMP::Vec< std::string > paraIDs, DMP::Vec< paraType > paraVals)
Definition: DMPInstance.cpp:227
armarx::DMPInstance::setAmpl
void setAmpl(int dim, double value, const ::Ice::Current &=Ice::emptyCurrent) override
setAmpl: set amplitude for dmp
Definition: DMPInstance.cpp:275
armarx::DMPInstance::DMPInstance
DMPInstance()
DMPInstance constructs an empty instance of a basicDMP.
Definition: DMPInstance.h:87
armarx::DMPInstance::getTemporalFactor
double getTemporalFactor(const ::Ice::Current &=Ice::emptyCurrent) override
getTemporalFactor: get current temporal factor
Definition: DMPInstance.cpp:329
Logging.h
armarx::DMPInstance::DMPInstance
DMPInstance(DMP::DMPInterfacePtr dmpInter)
Definition: DMPInstance.h:93
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:269
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::DMPInstance::timestep
double timestep
Definition: DMPInstance.h:367