DMPComponent.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2015-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package RobotAPI::ArmarXObjects::DMPComponent
19  * @author Mirko Waechter ( mirko dot waechter at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
27 
28 
29 #include <RobotComponents/interface/components/DMPComponentBase.h>
30 #include <MemoryX/interface/components/WorkingMemoryInterface.h>
31 #include <MemoryX/interface/components/LongtermMemoryInterface.h>
33 
34 #include <MemoryX/interface/memorytypes/MemoryEntities.h>
35 #include <MemoryX/interface/memorytypes/MemorySegments.h>
36 
37 #include <boost/archive/text_oarchive.hpp>
38 #include <boost/archive/text_iarchive.hpp>
39 
40 #include <boost/archive/xml_iarchive.hpp>
41 #include <boost/archive/xml_oarchive.hpp>
42 
43 #include <boost/archive/binary_iarchive.hpp>
44 #include <boost/archive/binary_oarchive.hpp>
45 
46 #include <boost/variant/variant.hpp>
47 #include <boost/variant/get.hpp>
48 
49 
50 #pragma GCC diagnostic ignored "-Wunknown-pragmas"
51 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
52 #include <dmp/representation/dmp/dmpregistration.h>
53 #include <dmp/representation/dmpfactory.h>
54 #pragma GCC diagnostic pop
55 #pragma GCC diagnostic pop
56 
57 #include <filesystem>
58 
59 
61 //#include "dmp/representation/dmp/dmpinterface.h"
62 //#include "dmp/representation/dmp/basicdmp.h"
63 //#include "dmp/representation/dmp/dmp3rdorder.h"
64 //#include "dmp/representation/dmp/quaterniondmp.h"
65 //#include "dmp/representation/dmp/endvelodmp.h"
66 //#include "dmp/representation/dmp/dmp3rdorderforcefield.h"
67 //#include "dmp/representation/dmp/forcefielddmp.h"
68 //#include "dmp/representation/dmp/adaptive3rdorderdmp.h"
69 //#include "dmp/representation/dmp/simpleendvelodmp.h"
70 ////#include "dmp/representation/dmp/endveloforcefielddmp.h"
71 ////#include "dmp/representation/dmp/endveloforcefieldwithobjrepulsiondmp.h"
72 //#include "dmp/representation/dmp/periodictransientdmp.h"
73 
74 #include "DMPInstance.h"
75 
76 namespace armarx
77 {
78  /**
79  * @class DMPComponentPropertyDefinitions
80  * @brief
81  */
82 
83 
86  {
87  public:
90  {
91  // defineRequiredProperty<std::string>("LongtermMemoryName", "Description");
92  defineOptionalProperty<std::string>("LongtermMemoryName", "LongtermMemory", "Name of the LongtermMemory component");
93  }
94  };
95 
96  /**
97  * @defgroup Component-DMPComponent DMPComponent
98  * @ingroup RobotComponents-Components
99  * @brief A brief description
100  *
101  *
102  * Detailed Description
103  */
104 
105  using DMPInstancePair = std::pair<DMPInstancePtr, DMPInstanceBasePrx >;
106  using DMPPair = std::pair<std::string, std::pair<DMPInstancePtr, DMPInstanceBasePrx> >;
107  using DMPMap = std::map<std::string, std::pair<DMPInstancePtr, DMPInstanceBasePrx> >;
108 
109  /**
110  * @ingroup Component-DMPComponent
111  * @brief The DMPComponent class
112  */
113  class DMPComponent :
114  virtual public Component,
115  virtual public DMPComponentBase
116  {
117  public:
118 
120  ctime(0.0),
121  timestep(0.001)
122  {
123 
124  }
125 
126  /**
127  * @see armarx::ManagedIceObject::getDefaultName()
128  */
129  std::string getDefaultName() const override
130  {
131  return "DMPComponent";
132  }
133 
134  /**
135  * @see PropertyUser::createPropertyDefinitions()
136  */
138  {
140  {
142  };
143  }
144 
145  DMPInstanceBasePrx getDMP(const std::string& dmpName, const Ice::Current& = Ice::emptyCurrent) override;
146 
147  // DMP Database related
148  DMPInstanceBasePrx getDMPFromDatabase(const std::string& dmpEntityName, const std::string& dmpName = "UNKNOWN", const Ice::Current& = Ice::emptyCurrent) override;
149  DMPInstanceBasePrx getDMPFromDatabaseById(const std::string& dbId, const Ice::Current&) override;
150  DMPInstanceBasePrx getDMPFromFile(const std::string& fileName, const std::string& dmpName = "UNKNOWN", const Ice::Current& = Ice::emptyCurrent) override;
151 
152  void storeDMPInFile(const std::string& fileName, const std::string& dmpName, const Ice::Current&) override;
153  void storeDMPInDatabase(const std::string& dmpName, const std::string& name, const ::Ice::Current& = Ice::emptyCurrent) override;
154  void removeDMPFromDatabase(const std::string& name, const ::Ice::Current& = Ice::emptyCurrent) override;
155  void removeDMPFromDatabaseById(const std::string& dbId, const Ice::Current&) override;
156 
157 
158  //transmit data from client to server (using ice)
159  DMPInstanceBasePrx instantiateDMP(const std::string& dmpName, const std::string& DMPType, int kernelSize, const ::Ice::Current& = Ice::emptyCurrent) override;
160 
161  void setDMPState(const std::string& dmpName, const ::armarx::cStateVec& state, const ::Ice::Current& = Ice::emptyCurrent) override;
162  void setGoal(const std::string& dmpName, const Ice::DoubleSeq& value, const Ice::Current& = Ice::emptyCurrent) override;
163  void setStartPosition(const std::string& dmpName, const Ice::DoubleSeq& value, const Ice::Current& = Ice::emptyCurrent) override;
164  void setCanonicalValues(const std::string& dmpName, const Ice::DoubleSeq& value, const Ice::Current& = Ice::emptyCurrent) override;
165 
166  void readTrajectoryFromFile(const std::string& dmpName, const std::string& file, double times = 1, const ::Ice::Current& = Ice::emptyCurrent) override;
167 
168  void trainDMP(const std::string& dmpName, const ::Ice::Current& = Ice::emptyCurrent) override;
169  void setAmpl(const std::string& dmpName, int dim, double value, const ::Ice::Current& = Ice::emptyCurrent) override;
170  double getAmpl(const std::string& dmpName, int dim, const ::Ice::Current& = Ice::emptyCurrent) override;
171 
172 
173  double getTemporalFactor(const std::string& dmpName, const ::Ice::Current& = Ice::emptyCurrent) override;
174  void setTemporalFactor(const std::string& dmpName, double tau, const ::Ice::Current& = Ice::emptyCurrent) override;
175 
176 
177  Vec2D calcTrajectory(const std::string& dmpName, double startTime, double timeStep, double endTime,
178  const ::Ice::DoubleSeq& goal,
179  const cStateVec& states,
180  const ::Ice::DoubleSeq& canonicalValues, double temporalFactor, const ::Ice::Current& = Ice::emptyCurrent) override;
181 
182  // time manager
183  double getTimeStep(const ::Ice::Current& = Ice::emptyCurrent) override
184  {
185  return timestep;
186  }
187 
188  void setTimeStep(double ts, const ::Ice::Current& = Ice::emptyCurrent) override;
189 
190  double getCurrentTime(const ::Ice::Current& = Ice::emptyCurrent) override;
191 
192  void resetTime(const ::Ice::Current& = Ice::emptyCurrent) override;
193 
194  void resetCanonicalValues(const ::Ice::Current& = Ice::emptyCurrent) override;
195 
196 
197  double getDampingFactor(const std::string& dmpName, const ::Ice::Current& = Ice::emptyCurrent) override;
198 
199  double getSpringFactor(const std::string& dmpName, const ::Ice::Current& = Ice::emptyCurrent) override;
200 
201  double getForceTerm(const std::string& dmpName, const Ice::DoubleSeq& canonicalValues, int dim, const Ice::Current& = Ice::emptyCurrent) override;
202 
203  //transmit data from server to client (using ice)
204  cStateVec getNextState(const std::string& dmpName, const cStateVec& states, const ::Ice::Current& = Ice::emptyCurrent) override;
205  cStateVec getCurrentState(const std::string& dmpName, const ::Ice::Current& = Ice::emptyCurrent) override;
206  Ice::DoubleSeq getCanonicalValues(const std::string& dmpName, const ::Ice::Current& = Ice::emptyCurrent) override;
207  Ice::DoubleSeq getTrajGoal(const std::string& dmpName, const ::Ice::Current& = Ice::emptyCurrent) override;
208  ::armarx::cStateVec getTrajStartState(const std::string& dmpName, const ::Ice::Current& = Ice::emptyCurrent) override;
209 
210  std::string getDMPType(const std::string& dmpName, const ::Ice::Current& = Ice::emptyCurrent) override;
211 
212  Ice::DoubleSeq getStartPosition(const std::string& dmpName, const ::Ice::Current& = Ice::emptyCurrent) override;
213 
214  SVector getDMPNameList(const ::Ice::Current& = Ice::emptyCurrent) override;
215 
216  void eraseDMP(const std::string& dmpName, const ::Ice::Current& = Ice::emptyCurrent) override;
217  bool isDMPExist(const std::string& dmpName, const ::Ice::Current& = Ice::emptyCurrent) override;
218  int getDMPDim(const std::string& dmpName, const ::Ice::Current& = Ice::emptyCurrent) override;
219 
220 
221  double ctime;
222  double timestep;
223  std::vector<int> usedDimensions;
224 
225  private:
226  DMPInstanceBasePrx findInstancePrx(std::string name);
227  DMPInstancePtr findInstancePtr(std::string name);
228  DMPInstanceBasePrx createDMPInstancePrx(DMPInstancePtr dmpPtr);
229 
230  protected:
232 
233  memoryx::LongtermMemoryInterfacePrx longtermMemoryPrx;
234  memoryx::PersistentDMPDataSegmentBasePrx dmpDataMemoryPrx;
235 
236  /**
237  * @see armarx::ManagedIceObject::onInitComponent()
238  */
239  void onInitComponent() override;
240 
241  /**
242  * @see armarx::ManagedIceObject::onConnectComponent()
243  */
244  void onConnectComponent() override;
245 
246  /**
247  * @see armarx::ManagedIceObject::onDisconnectComponent()
248  */
249  void onDisconnectComponent() override;
250 
251  /**
252  * @see armarx::ManagedIceObject::onExitComponent()
253  */
254  void onExitComponent() override;
255 
256  /**
257  * @see PropertyUser::createPropertyDefinitions()
258  */
259  // virtual armarx::PropertyDefinitionsPtr createPropertyDefinitions();
260  };
262 }
263 
264 
265 
armarx::DMPComponentPropertyDefinitions
Definition: DMPComponent.h:84
armarx::DMPComponent::getTrajStartState
::armarx::cStateVec getTrajStartState(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:784
armarx::DMPComponent::calcTrajectory
Vec2D calcTrajectory(const std::string &dmpName, double startTime, double timeStep, double endTime, const ::Ice::DoubleSeq &goal, const cStateVec &states, const ::Ice::DoubleSeq &canonicalValues, double temporalFactor, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:716
armarx::DMPComponent::getDefaultName
std::string getDefaultName() const override
Definition: DMPComponent.h:129
armarx::DMPComponent::instantiateDMP
DMPInstanceBasePrx instantiateDMP(const std::string &dmpName, const std::string &DMPType, int kernelSize, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:126
armarx::DMPComponent::getDMPType
std::string getDMPType(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:663
armarx::DMPComponent
The DMPComponent class.
Definition: DMPComponent.h:113
armarx::DMPInstancePair
std::pair< DMPInstancePtr, DMPInstanceBasePrx > DMPInstancePair
Definition: DMPComponent.h:105
armarx::DMPComponent::setTemporalFactor
void setTemporalFactor(const std::string &dmpName, double tau, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:491
armarx::DMPComponent::setAmpl
void setAmpl(const std::string &dmpName, int dim, double value, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:432
armarx::DMPComponent::dmpPool
DMPMap dmpPool
Definition: DMPComponent.h:231
armarx::DMPComponent::getDMPFromDatabaseById
DMPInstanceBasePrx getDMPFromDatabaseById(const std::string &dbId, const Ice::Current &) override
Definition: DMPComponent.cpp:208
armarx::DMPComponent::isDMPExist
bool isDMPExist(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:827
armarx::DMPComponent::ctime
double ctime
Definition: DMPComponent.h:221
armarx::DMPComponent::storeDMPInFile
void storeDMPInFile(const std::string &fileName, const std::string &dmpName, const Ice::Current &) override
Definition: DMPComponent.cpp:283
armarx::DMPComponent::setTimeStep
void setTimeStep(double ts, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:505
armarx::DMPComponent::longtermMemoryPrx
memoryx::LongtermMemoryInterfacePrx longtermMemoryPrx
Definition: DMPComponent.h:233
armarx::DMPComponent::usedDimensions
std::vector< int > usedDimensions
Definition: DMPComponent.h:223
armarx::DMPComponent::setGoal
void setGoal(const std::string &dmpName, const Ice::DoubleSeq &value, const Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:447
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:333
DMPInstance.h
armarx::DMPComponent::setStartPosition
void setStartPosition(const std::string &dmpName, const Ice::DoubleSeq &value, const Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:462
armarx::DMPMap
std::map< std::string, std::pair< DMPInstancePtr, DMPInstanceBasePrx > > DMPMap
Definition: DMPComponent.h:107
armarx::DMPComponent::trainDMP
void trainDMP(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:379
armarx::DMPComponent::onConnectComponent
void onConnectComponent() override
Definition: DMPComponent.cpp:50
armarx::DMPComponent::getDMP
DMPInstanceBasePrx getDMP(const std::string &dmpName, const Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:121
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::DMPComponentPropertyDefinitions::DMPComponentPropertyDefinitions
DMPComponentPropertyDefinitions(std::string prefix)
Definition: DMPComponent.h:88
armarx::DMPComponent::setCanonicalValues
void setCanonicalValues(const std::string &dmpName, const Ice::DoubleSeq &value, const Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:476
armarx::DMPComponent::DMPComponent
DMPComponent()
Definition: DMPComponent.h:119
armarx::DMPComponent::getCurrentTime
double getCurrentTime(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:604
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::DMPComponent::removeDMPFromDatabaseById
void removeDMPFromDatabaseById(const std::string &dbId, const Ice::Current &) override
Definition: DMPComponent.cpp:358
armarx::DMPComponent::onExitComponent
void onExitComponent() override
Definition: DMPComponent.cpp:84
armarx::DMPComponent::dmpDataMemoryPrx
memoryx::PersistentDMPDataSegmentBasePrx dmpDataMemoryPrx
Definition: DMPComponent.h:234
DMPEntity.h
armarx::DMPComponent::getTimeStep
double getTimeStep(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.h:183
armarx::DMPComponent::getDMPFromFile
DMPInstanceBasePrx getDMPFromFile(const std::string &fileName, const std::string &dmpName="UNKNOWN", const Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:225
armarx::DMPComponent::getCanonicalValues
Ice::DoubleSeq getCanonicalValues(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:568
armarx::DMPComponent::setDMPState
void setDMPState(const std::string &dmpName, const ::armarx::cStateVec &state, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:417
armarx::DMPComponent::timestep
double timestep
Definition: DMPComponent.h:222
armarx::DMPComponent::getAmpl
double getAmpl(const std::string &dmpName, int dim, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:515
armarx::DMPComponent::onDisconnectComponent
void onDisconnectComponent() override
Definition: DMPComponent.cpp:78
armarx::DMPComponent::getDMPFromDatabase
DMPInstanceBasePrx getDMPFromDatabase(const std::string &dmpEntityName, const std::string &dmpName="UNKNOWN", const Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:158
armarx::DMPComponent::readTrajectoryFromFile
void readTrajectoryFromFile(const std::string &dmpName, const std::string &file, double times=1, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:398
Component.h
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:95
armarx::DMPComponent::getDMPDim
int getDMPDim(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:646
armarx::DMPPair
std::pair< std::string, std::pair< DMPInstancePtr, DMPInstanceBasePrx > > DMPPair
Definition: DMPComponent.h:106
armarx::DMPComponent::onInitComponent
void onInitComponent() override
Definition: DMPComponent.cpp:42
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
armarx::DMPComponent::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: DMPComponent.h:137
armarx::DMPComponent::getDampingFactor
double getDampingFactor(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:614
armarx::DMPComponent::getCurrentState
cStateVec getCurrentState(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:551
armarx::DMPComponent::eraseDMP
void eraseDMP(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:817
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
armarx::DMPComponent::getStartPosition
Ice::DoubleSeq getStartPosition(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:679
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::DMPComponent::resetCanonicalValues
void resetCanonicalValues(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:744
armarx::DMPComponent::removeDMPFromDatabase
void removeDMPFromDatabase(const std::string &name, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:338
armarx::DMPComponent::getTemporalFactor
double getTemporalFactor(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:586
armarx::DMPComponent::storeDMPInDatabase
void storeDMPInDatabase(const std::string &dmpName, const std::string &name, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:311
armarx::DMPComponent::getSpringFactor
double getSpringFactor(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:630
armarx::DMPComponent::getDMPNameList
SVector getDMPNameList(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:805
armarx::DMPComponent::getNextState
cStateVec getNextState(const std::string &dmpName, const cStateVec &states, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:532
armarx::DMPComponent::getTrajGoal
Ice::DoubleSeq getTrajGoal(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:767
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::DMPComponent::resetTime
void resetTime(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:734
armarx::DMPComponent::getForceTerm
double getForceTerm(const std::string &dmpName, const Ice::DoubleSeq &canonicalValues, int dim, const Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:695