DMPComponent.cpp
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 #include "DMPComponent.h"
26 
27 #include <Ice/ObjectAdapter.h>
28 
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wunknown-pragmas"
31 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
32 #include <dmp/io/MMMConverter.h>
33 #include <dmp/testing/testdataset.h>
34 #pragma GCC diagnostic pop
35 
38 
39 using namespace armarx;
40 
41 void
43 {
44  ARMARX_INFO << "initializing DMP component";
45  usingProxy(getProperty<std::string>("LongtermMemoryName").getValue());
46  ARMARX_INFO << "successfully initialized DMP component";
47 }
48 
49 void
51 {
52  ARMARX_INFO << "connecting DMP component";
53 
54  try
55  {
56  longtermMemoryPrx = getProxy<memoryx::LongtermMemoryInterfacePrx>(
57  getProperty<std::string>("LongtermMemoryName").getValue());
58  }
59  catch (...)
60  {
61  ARMARX_ERROR << "cannot get longterm memory proxy";
62  return;
63  }
64 
65  try
66  {
67  dmpDataMemoryPrx = longtermMemoryPrx->getDMPSegment();
68  }
69  catch (...)
70  {
71  ARMARX_ERROR << "cannot get dmp segment of longterm memory";
72  return;
73  }
74 
75  ARMARX_INFO << "successfully connected DMP component";
76 }
77 
78 void
80 {
81  ARMARX_INFO << "disconnecting DMP component";
82 }
83 
84 void
86 {
87  ARMARX_INFO << "exiting DMP component";
88 }
89 
90 DMPInstanceBasePrx
91 DMPComponent::findInstancePrx(std::string name)
92 {
93  DMPInstancePair dmpInstPair = dmpPool.find(name)->second;
94  DMPInstanceBasePrx result = dmpInstPair.second;
95  if (result)
96  {
97  return result;
98  }
99  else
100  {
101  ARMARX_INFO << "DMP not found.";
102  return DMPInstanceBasePrx();
103  }
104 }
105 
107 DMPComponent::findInstancePtr(std::string name)
108 {
109  DMPInstancePair dmpInstPair = dmpPool.find(name)->second;
110  DMPInstancePtr result = dmpInstPair.first;
111  if (result)
112  {
113  return result;
114  }
115  else
116  {
117  //TODO Correct?
118  ARMARX_INFO << "DMP not found.";
119  return nullptr;
120  }
121 }
122 
123 DMPInstanceBasePrx
124 DMPComponent::getDMP(const std::string& dmpName, const Ice::Current&)
125 {
126  return findInstancePrx(dmpName);
127 }
128 
129 DMPInstanceBasePrx
130 DMPComponent::instantiateDMP(const std::string& dmpName,
131  const std::string& DMPType,
132  int kernelSize,
133  const Ice::Current&)
134 {
135  ARMARX_INFO << "instantiate DMP with name " << dmpName;
136 
137  boost::serialization::extended_type_info const* eti =
138  boost::serialization::extended_type_info::find(DMPType.c_str());
139 
140  DMPInterfacePtr dmp;
141 
142  if (eti)
143  {
144  dmp.reset(static_cast<DMP::DMPInterface*>(eti->construct()));
145  }
146  else
147  {
148  DMPFactory dmpFactory;
149  dmp = dmpFactory.getDMP(DMPType, kernelSize);
150  }
151 
152  DMPInstancePtr dmpPtr = new DMPInstance(dmp, DMPType);
153  dmpPtr->setTimeStep(timestep);
154  dmpPtr->setCurrentTime(ctime);
155 
156  DMPInstanceBasePrx instprx = createDMPInstancePrx(dmpPtr);
157  DMPInstancePair dmpInstPair(dmpPtr, instprx);
158  dmpPool.insert(DMPPair(dmpName, dmpInstPair));
159 
160  ARMARX_INFO << "Successfully instantiate DMP ... ";
161  return instprx;
162 }
163 
164 // Database related functions
165 DMPInstanceBasePrx
166 DMPComponent::getDMPFromDatabase(const std::string& dmpEntityName,
167  const std::string& dmpName,
168  const Ice::Current&)
169 {
170  ARMARX_INFO << "getting DMP from database";
171 
172  if (!dmpDataMemoryPrx->hasEntityByName(dmpEntityName))
173  {
174  ARMARX_ERROR << "DMP with name " + dmpEntityName + " does not exist in the database";
175  return DMPInstanceBasePrx();
176  }
177 
178  memoryx::DMPEntityPtr dmpEntity =
179  memoryx::DMPEntityPtr::dynamicCast(dmpDataMemoryPrx->getDMPEntityByName(dmpEntityName));
180 
181 
182  std::string textStr = dmpEntity->getDMPtextStr();
183  ARMARX_INFO << textStr;
184  std::stringstream istr;
185  istr.str(textStr);
186 
187  boost::archive::text_iarchive ar(istr);
188 
189  DMP::DMPInterface* readPtr;
190 
191  ar >> boost::serialization::make_nvp("dmp", readPtr);
192 
193  DMP::DMPInterfacePtr dptr(readPtr);
194  DMPInstancePtr dmpPtr(new DMPInstance(dptr));
195 
196  DMPInstanceBasePrx dmpPrx = createDMPInstancePrx(dmpPtr);
197  std::string dmpLocalName = dmpName;
198  if (dmpName == "UNKNOWN")
199  {
200  dmpLocalName = dmpPrx->ice_getIdentity().name;
201  }
202 
203  if (dmpPool.find(dmpLocalName) != dmpPool.end())
204  {
205  ARMARX_ERROR << "DMP with DMPName " + dmpLocalName +
206  " exists in DMP pool. It is not allowed to overwrite it.";
207  return dmpPrx;
208  }
209  dmpPtr->setTimeStep(timestep);
210  dmpPtr->setCurrentTime(ctime);
211  dmpPool.insert(DMPPair(dmpLocalName, DMPInstancePair(dmpPtr, dmpPrx)));
212 
213 
214  ARMARX_INFO << "DMP with name: " + dmpEntityName + " is loaded into DMP Pool with dmpName " +
215  dmpLocalName + " and dmpType " + dmpPtr->getDMPType();
216  ARMARX_INFO << "successfully got dmp from database.";
217 
218  return dmpPrx;
219 }
220 
221 DMPInstanceBasePrx
222 DMPComponent::getDMPFromDatabaseById(const std::string& dbId, const Ice::Current&)
223 {
224  ARMARX_INFO << "getting DMP from database";
225 
226  if (!dmpDataMemoryPrx->hasEntityById(dbId))
227  {
228  ARMARX_ERROR << "DMP with ID " + dbId + " does not exist in the database";
229  return DMPInstanceBasePrx();
230  }
231 
232  memoryx::DMPEntityPtr dmpEntity =
233  memoryx::DMPEntityPtr::dynamicCast(dmpDataMemoryPrx->getDMPEntityById(dbId));
234  std::string dmpEntityName = dmpEntity->getDMPName();
235 
236  return getDMPFromDatabase(dmpEntityName);
237 }
238 
239 DMPInstanceBasePrx
240 DMPComponent::getDMPFromFile(const std::string& fileName,
241  const std::string& dmpName,
242  const Ice::Current&)
243 {
244  ARMARX_INFO << "getting DMP from file";
245 
246  if (!std::filesystem::exists(fileName))
247  {
248  ARMARX_ERROR << "The dmp file: " << fileName << " does not exist!!!";
249  return DMPInstanceBasePrx();
250  }
251 
252  std::string ext =
253  fileName.rfind(".") == fileName.npos ? fileName : fileName.substr(fileName.rfind(".") + 1);
254 
255  std::ifstream file(fileName);
256  DMP::DMPInterface* readPtr;
257 
258  if (ext == "xml")
259  {
260  boost::archive::xml_iarchive ar(file);
261  ar >> boost::serialization::make_nvp("dmp", readPtr);
262  }
263  else
264  {
265  ARMARX_ERROR << "The extension of the file cannot be recognized!!!";
266  return DMPInstanceBasePrx();
267  }
268 
269 
270  DMP::DMPInterfacePtr dptr(readPtr);
271  DMPInstancePtr dmpPtr(new DMPInstance(dptr));
272  DMPInstanceBasePrx dmpPrx = createDMPInstancePrx(dmpPtr);
273 
274  std::string dmpLocalName = dmpName;
275  if (dmpLocalName == "UNKNOWN" || dmpLocalName == "")
276  {
277  // dmpLocalName = dptr->getDMPName();
278 
279  // if (dmpLocalName == "UNKNOWN")
280  // {
281  dmpLocalName = dmpPrx->ice_getIdentity().name;
282  // }
283  }
284  dmpPtr->setTimeStep(timestep);
285  dmpPtr->setCurrentTime(ctime);
286  if (dmpPool.find(dmpLocalName) != dmpPool.end())
287  {
288  ARMARX_ERROR << "DMP with DMPName " + dmpLocalName +
289  " exists in DMP pool. It is not allowed to overwrite it.";
290  return dmpPrx;
291  }
292 
293  dmpPool.insert(DMPPair(dmpLocalName, DMPInstancePair(dmpPtr, dmpPrx)));
294 
295  ARMARX_INFO << "DMP with name: " + dmpName + " is loaded into DMP Pool with dmpName " +
296  dmpLocalName + " and dmpType " + dmpPtr->getDMPType();
297  ARMARX_INFO << "successfully got dmp from file.";
298  file.close();
299 
300  return dmpPrx;
301 }
302 
303 void
304 DMPComponent::storeDMPInFile(const std::string& fileName,
305  const std::string& dmpName,
306  const Ice::Current&)
307 {
308  ARMARX_INFO << "storing DMP in the file";
309 
310  std::string ext =
311  fileName.rfind(".") == fileName.npos ? fileName : fileName.substr(fileName.rfind(".") + 1);
312  ofstream ofs(fileName);
313 
314  DMPInstancePtr dmpPtr = findInstancePtr(dmpName);
315  DMPInterface* savedPtr = dmpPtr->getDMP().get();
316  // savedPtr->setDMPName(dmpName);
317 
318  if (ext == "xml")
319  {
320  boost::archive::xml_oarchive ar(ofs);
321  ar << boost::serialization::make_nvp("dmp", savedPtr);
322  }
323  else
324  {
325  ARMARX_ERROR << "The extension of the file cannot be recognized!!!";
326  return;
327  }
328 
329  ARMARX_INFO << "successfully stored DMP in the file!!!";
330  ofs.close();
331 
332  return;
333 }
334 
335 void
336 DMPComponent::storeDMPInDatabase(const std::string& dmpName,
337  const std::string& dmpEntityName,
338  const ::Ice::Current&)
339 {
340 
341  ARMARX_INFO << "storing DMP in the database";
342  memoryx::DMPEntityPtr dmpEntity = new memoryx::DMPEntity(dmpEntityName);
343 
344  DMPInstancePtr dmpPtr = findInstancePtr(dmpName);
345 
346  dmpEntity->setDMPType(dmpPtr->getDMP()->getDMPType());
347  dmpEntity->setDMPName(dmpName);
348 
349  std::stringstream dmptext;
350  boost::archive::text_oarchive ar(dmptext);
351 
352  DMPInterface* savedPtr = dmpPtr->getDMP().get();
353  ar << boost::serialization::make_nvp("dmp", savedPtr);
354 
355  dmpEntity->setDMPtextStr(dmptext.str());
356 
357  std::cout << dmpEntity << std::endl;
358  const std::string entityID = dmpDataMemoryPrx->addEntity(dmpEntity);
359  dmpEntity->setId(entityID);
360 
361  ARMARX_INFO << "successfully stored DMP";
362 }
363 
364 void
365 DMPComponent::removeDMPFromDatabase(const std::string& dmpEntityName, const ::Ice::Current&)
366 {
367 
368  ARMARX_INFO << "removing DMP from database";
369 
370  if (!dmpDataMemoryPrx->hasEntityByName(dmpEntityName))
371  {
372  ARMARX_ERROR << "DMP with name " + dmpEntityName + " does not exist in the database";
373  return;
374  }
375 
376  memoryx::DMPEntityPtr dmpEntity =
377  memoryx::DMPEntityPtr::dynamicCast(dmpDataMemoryPrx->getDMPEntityByName(dmpEntityName));
378  dmpDataMemoryPrx->removeEntity(dmpEntity->getId());
379 
380  if (!dmpDataMemoryPrx->hasEntityByName(dmpEntityName))
381  {
382  ARMARX_INFO << "successfully removed dmp from database";
383  }
384 }
385 
386 void
387 DMPComponent::removeDMPFromDatabaseById(const std::string& dbId, const Ice::Current&)
388 {
389  ARMARX_INFO << "removing DMP from database";
390 
391  if (!dmpDataMemoryPrx->hasEntityById(dbId))
392  {
393  ARMARX_ERROR << "DMP with id " + dbId + " does not exist in the database";
394  return;
395  }
396 
397  memoryx::DMPEntityPtr dmpEntity =
398  memoryx::DMPEntityPtr::dynamicCast(dmpDataMemoryPrx->getDMPEntityById(dbId));
399  dmpDataMemoryPrx->removeEntity(dmpEntity->getId());
400 
401  if (!dmpDataMemoryPrx->hasEntityById(dbId))
402  {
403  ARMARX_INFO << "successfully removed dmp from database";
404  }
405 }
406 
407 // DMP trainner
408 void
409 DMPComponent::trainDMP(const std::string& dmpName, const ::Ice::Current&)
410 {
411  // learn dmp
412  ARMARX_INFO << "In Train DMP (name: " << dmpName << ")";
413 
414  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
415 
416  if (dmpPrx)
417  {
418  dmpPrx->trainDMP();
419  }
420  else
421  {
422  ARMARX_ERROR << "trainDMP: DMP not found.";
423  return;
424  }
425  ARMARX_INFO << "Exit Train DMP (name: " << dmpName << ")";
426 }
427 
428 void
429 DMPComponent::readTrajectoryFromFile(const std::string& dmpName,
430  const std::string& file,
431  double times,
432  const Ice::Current&)
433 {
434 
435  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
436 
437  if (dmpPrx)
438  {
439  dmpPrx->readTrajectoryFromFile(file, times);
440  }
441  else
442  {
443  ARMARX_ERROR << "readTrajectoryFromFile: DMP not found.";
444  return;
445  }
446 }
447 
448 // convenient setter functions
449 void
450 DMPComponent::setDMPState(const std::string& dmpName,
451  const ::armarx::cStateVec& state,
452  const ::Ice::Current&)
453 {
454  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
455 
456  if (dmpPrx)
457  {
458  dmpPrx->setDMPState(state);
459  }
460  else
461  {
462  ARMARX_INFO << "setDMPState: DMP not found.";
463  return;
464  }
465 }
466 
467 void
468 DMPComponent::setAmpl(const std::string& dmpName, int dim, double value, const Ice::Current&)
469 {
470  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
471 
472  if (dmpPrx)
473  {
474  dmpPrx->setAmpl(dim, value);
475  }
476  else
477  {
478  ARMARX_ERROR << "setAmpl: DMP not found.";
479  return;
480  }
481 }
482 
483 void
484 DMPComponent::setGoal(const std::string& dmpName, const Ice::DoubleSeq& value, const Ice::Current&)
485 {
486  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
487 
488  if (dmpPrx)
489  {
490  dmpPrx->setGoal(value);
491  }
492  else
493  {
494  ARMARX_ERROR << "setGoal: DMP not found.";
495  return;
496  }
497 }
498 
499 void
500 DMPComponent::setStartPosition(const std::string& dmpName,
501  const Ice::DoubleSeq& value,
502  const Ice::Current&)
503 {
504  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
505 
506  if (dmpPrx)
507  {
508  dmpPrx->setStartPosition(value);
509  }
510  else
511  {
512  ARMARX_ERROR << "setStartPosition: DMP not found.";
513  }
514 }
515 
516 void
517 DMPComponent::setCanonicalValues(const std::string& dmpName,
518  const Ice::DoubleSeq& value,
519  const Ice::Current&)
520 {
521  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
522 
523  if (dmpPrx)
524  {
525  return dmpPrx->setCanonicalValues(value);
526  }
527  else
528  {
529  ARMARX_ERROR << "setCanonicalValues: DMP not found.";
530  return;
531  }
532 }
533 
534 void
535 DMPComponent::setTemporalFactor(const std::string& dmpName, double tau, const Ice::Current&)
536 {
537  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
538 
539  if (dmpPrx)
540  {
541  dmpPrx->setTemporalFactor(tau);
542  }
543  else
544  {
545  ARMARX_ERROR << "getTemporalFactor: DMP not found.";
546  }
547 }
548 
549 void
550 DMPComponent::setTimeStep(double ts, const Ice::Current&)
551 {
552  timestep = ts;
553  for (DMPMap::iterator it = dmpPool.begin(); it != dmpPool.end(); it++)
554  {
555  it->second.first->setTimeStep(ts);
556  }
557 }
558 
559 // convenient getter functions
560 double
561 DMPComponent::getAmpl(const std::string& dmpName, int dim, const Ice::Current&)
562 {
563 
564  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
565 
566  if (dmpPrx)
567  {
568  return dmpPrx->getAmpl(dim);
569  }
570  else
571  {
572  ARMARX_ERROR << "getAmpl: DMP not found.";
573  return 0;
574  }
575 }
576 
577 cStateVec
578 DMPComponent::getNextState(const std::string& dmpName,
579  const cStateVec& states,
580  const ::Ice::Current&)
581 {
582  cStateVec nextState;
583  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
584 
585  if (dmpPrx)
586  {
587  nextState = dmpPrx->getNextState(states);
588  }
589  else
590  {
591  ARMARX_ERROR << "getNextState: DMP not found.";
592  }
593 
594  ARMARX_INFO << "Successfully got next state";
595 
596  return nextState;
597 }
598 
599 cStateVec
600 DMPComponent::getCurrentState(const std::string& dmpName, const Ice::Current&)
601 {
602  cStateVec curState;
603  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
604 
605  if (dmpPrx)
606  {
607  curState = dmpPrx->getCurrentState();
608  }
609  else
610  {
611  ARMARX_ERROR << "getCurrentState: DMP not found.";
612  }
613 
614  return curState;
615 }
616 
617 Ice::DoubleSeq
618 DMPComponent::getCanonicalValues(const std::string& dmpName, const Ice::Current&)
619 {
620  Ice::DoubleSeq canonicalValues;
621 
622  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
623 
624  if (dmpPrx)
625  {
626  canonicalValues = dmpPrx->getCanonicalValues();
627  }
628  else
629  {
630  ARMARX_ERROR << "getCanonicalValues: DMP not found.";
631  }
632 
633  return canonicalValues;
634 }
635 
636 double
637 DMPComponent::getTemporalFactor(const std::string& dmpName, const Ice::Current&)
638 {
639  double temporalFactor = 0;
640 
641  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
642 
643  if (dmpPrx)
644  {
645  temporalFactor = dmpPrx->getTemporalFactor();
646  }
647  else
648  {
649  ARMARX_ERROR << "getTemporalFactor: DMP not found.";
650  }
651 
652  return temporalFactor;
653 }
654 
655 double
656 DMPComponent::getCurrentTime(const Ice::Current&)
657 {
658 
659  DMPMap::iterator it = dmpPool.begin();
660 
661  return it->second.first->getCurrentTime();
662 }
663 
664 double
665 DMPComponent::getDampingFactor(const std::string& dmpName, const Ice::Current&)
666 {
667  double dampingFactor = 0;
668  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
669 
670  if (dmpPrx)
671  {
672  dampingFactor = dmpPrx->getDampingFactor();
673  }
674  else
675  {
676  ARMARX_ERROR << "getDampingFactor: DMP not found.";
677  }
678  return dampingFactor;
679 }
680 
681 double
682 DMPComponent::getSpringFactor(const std::string& dmpName, const Ice::Current&)
683 {
684  double springFactor{0};
685  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
686 
687  if (dmpPrx)
688  {
689  springFactor = dmpPrx->getSpringFactor();
690  }
691  else
692  {
693  ARMARX_ERROR << "getSpringFactor: DMP not found.";
694  }
695  return springFactor;
696 }
697 
698 int
699 DMPComponent::getDMPDim(const std::string& dmpName, const Ice::Current&)
700 {
701  int dim = -1;
702  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
703 
704  if (dmpPrx)
705  {
706  dim = dmpPrx->getDMPDim();
707  }
708  else
709  {
710  ARMARX_ERROR << "getDMPDim: DMP not found.";
711  }
712 
713  return dim;
714 }
715 
716 string
717 DMPComponent::getDMPType(const std::string& dmpName, const Ice::Current&)
718 {
719  std::string dmpType;
720  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
721 
722  if (dmpPrx)
723  {
724  dmpType = dmpPrx->getDMPType();
725  }
726  else
727  {
728  ARMARX_ERROR << "getDMPType: DMP not found.";
729  }
730  return dmpType;
731 }
732 
733 Ice::DoubleSeq
734 DMPComponent::getStartPosition(const std::string& dmpName, const Ice::Current&)
735 {
736  Ice::DoubleSeq startPositions;
737  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
738 
739  if (dmpPrx)
740  {
741  startPositions = dmpPrx->getStartPosition();
742  }
743  else
744  {
745  ARMARX_ERROR << "getStartPosition: DMP not found.";
746  }
747  return startPositions;
748 }
749 
750 double
751 DMPComponent::getForceTerm(const std::string& dmpName,
752  const Ice::DoubleSeq& canonicalValues,
753  int dim,
754  const Ice::Current&)
755 {
756  double forceterm{0};
757  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
758 
759  if (dmpPrx)
760  {
761  forceterm = dmpPrx->getForceTerm(canonicalValues, dim);
762  }
763  else
764  {
765  ARMARX_ERROR << "getDMPDim: DMP not found.";
766  }
767 
768  return forceterm;
769 }
770 
771 Vec2D
772 DMPComponent::calcTrajectory(const std::string& dmpName,
773  double startTime,
774  double timeStep,
775  double endTime,
776  const Ice::DoubleSeq& goal,
777  const cStateVec& states,
778  const Ice::DoubleSeq& canonicalValues,
779  double temporalFactor,
780  const Ice::Current&)
781 {
782  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
783 
784  if (dmpPrx)
785  {
786  return dmpPrx->calcTrajectory(
787  startTime, timeStep, endTime, goal, states, canonicalValues, temporalFactor);
788  }
789  else
790  {
791  ARMARX_ERROR << "getTemporalFactor: DMP not found.";
792  }
793  return {};
794 }
795 
796 void
797 DMPComponent::resetTime(const Ice::Current&)
798 {
799  ctime = 0;
800  for (DMPMap::iterator it = dmpPool.begin(); it != dmpPool.end(); it++)
801  {
802  it->second.first->setCurrentTime(ctime);
803  }
804 }
805 
806 void
808 {
809  for (DMPMap::iterator it = dmpPool.begin(); it != dmpPool.end(); it++)
810  {
811  ::Ice::DoubleSeq canvals;
812  DMPInstancePtr dmpPtr = it->second.first;
813  if (dmpPtr->getDMPType() == "PeriodicDMP")
814  {
815  canvals.push_back(0.0);
816  dmpPtr->setCanonicalValues(canvals);
817  }
818  else
819  {
820  canvals.push_back(1.0);
821  dmpPtr->setCanonicalValues(canvals);
822  }
823  }
824 }
825 
826 // Trajectory related
827 Ice::DoubleSeq
828 DMPComponent::getTrajGoal(const std::string& dmpName, const Ice::Current&)
829 {
830  Ice::DoubleSeq trajGoal;
831  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
832 
833  if (dmpPrx)
834  {
835  trajGoal = dmpPrx->getTrajGoal();
836  }
837  else
838  {
839  ARMARX_ERROR << "getTrajGoal: DMP not found.";
840  }
841 
842  return trajGoal;
843 }
844 
845 cStateVec
846 DMPComponent::getTrajStartState(const std::string& dmpName, const Ice::Current&)
847 {
848  cStateVec trajStartState;
849  DMPInstanceBasePrx dmpPrx = findInstancePrx(dmpName);
850 
851  if (dmpPrx)
852  {
853  trajStartState = dmpPrx->getTrajStartState();
854  }
855  else
856  {
857  ARMARX_ERROR << "getTrajStartState: DMP not found.";
858  }
859 
860  return trajStartState;
861 }
862 
863 // DMP manager
864 SVector
865 DMPComponent::getDMPNameList(const Ice::Current&)
866 {
867  SVector nameList;
868 
869  for (DMPMap::iterator it = dmpPool.begin(); it != dmpPool.end(); it++)
870  {
871  nameList.push_back(it->first);
872  }
873 
874  return nameList;
875 }
876 
877 void
878 DMPComponent::eraseDMP(const std::string& dmpName, const Ice::Current&)
879 {
880  DMPMap::iterator dmpPoolIt = dmpPool.find(dmpName);
881 
882  if (dmpPoolIt != dmpPool.end())
883  {
884  dmpPool.erase(dmpPoolIt);
885  }
886 }
887 
888 bool
889 DMPComponent::isDMPExist(const std::string& dmpName, const Ice::Current&)
890 {
891  if (dmpPool.find(dmpName) == dmpPool.end())
892  {
893  return false;
894  }
895  else
896  {
897  return true;
898  }
899 }
900 
901 // local functions
902 DMPInstanceBasePrx
903 DMPComponent::createDMPInstancePrx(DMPInstancePtr dmpPtr)
904 {
906  Ice::ObjectPrx obj = adapter->addWithUUID(dmpPtr);
907 
908  return DMPInstanceBasePrx::uncheckedCast(obj);
909 }
910 
911 //PropertyDefinitionsPtr DMPComponent::createPropertyDefinitions()
912 //{
913 // return PropertyDefinitionsPtr(new DMPComponentPropertyDefinitions(
914 // getConfigIdentifier()));
915 //}
DMPComponent.h
armarx::DMPComponent::getTrajStartState
::armarx::cStateVec getTrajStartState(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:846
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:772
armarx::DMPComponent::instantiateDMP
DMPInstanceBasePrx instantiateDMP(const std::string &dmpName, const std::string &DMPType, int kernelSize, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:130
armarx::DMPComponent::getDMPType
std::string getDMPType(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:717
armarx::DMPComponent::setTemporalFactor
void setTemporalFactor(const std::string &dmpName, double tau, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:535
armarx::ManagedIceObject::getObjectAdapter
Ice::ObjectAdapterPtr getObjectAdapter() const
Returns object's Ice adapter.
Definition: ManagedIceObject.cpp:144
armarx::DMPComponent::setAmpl
void setAmpl(const std::string &dmpName, int dim, double value, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:468
armarx::DMPComponent::dmpPool
DMPMap dmpPool
Definition: DMPComponent.h:276
armarx::DMPComponent::getDMPFromDatabaseById
DMPInstanceBasePrx getDMPFromDatabaseById(const std::string &dbId, const Ice::Current &) override
Definition: DMPComponent.cpp:222
armarx::DMPComponent::isDMPExist
bool isDMPExist(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:889
armarx::DMPComponent::ctime
double ctime
Definition: DMPComponent.h:266
armarx::DMPComponent::storeDMPInFile
void storeDMPInFile(const std::string &fileName, const std::string &dmpName, const Ice::Current &) override
Definition: DMPComponent.cpp:304
armarx::DMPComponent::setTimeStep
void setTimeStep(double ts, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:550
armarx::DMPComponent::longtermMemoryPrx
memoryx::LongtermMemoryInterfacePrx longtermMemoryPrx
Definition: DMPComponent.h:278
armarx::DMPComponent::setGoal
void setGoal(const std::string &dmpName, const Ice::DoubleSeq &value, const Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:484
armarx::DMPComponent::setStartPosition
void setStartPosition(const std::string &dmpName, const Ice::DoubleSeq &value, const Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:500
armarx::DMPComponent::trainDMP
void trainDMP(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:409
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:124
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::DMPInstance
Definition: DMPInstance.h:80
armarx::DMPInstancePair
std::pair< DMPInstancePtr, DMPInstanceBasePrx > DMPInstancePair
Definition: DMPComponent.h:100
armarx::DMPComponent::setCanonicalValues
void setCanonicalValues(const std::string &dmpName, const Ice::DoubleSeq &value, const Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:517
armarx::DMPComponent::getCurrentTime
double getCurrentTime(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:656
memoryx::DMPEntity
Definition: DMPEntity.h:37
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::DMPComponent::removeDMPFromDatabaseById
void removeDMPFromDatabaseById(const std::string &dbId, const Ice::Current &) override
Definition: DMPComponent.cpp:387
armarx::DMPComponent::onExitComponent
void onExitComponent() override
Definition: DMPComponent.cpp:85
MemoryXCoreObjectFactories.h
armarx::DMPComponent::dmpDataMemoryPrx
memoryx::PersistentDMPDataSegmentBasePrx dmpDataMemoryPrx
Definition: DMPComponent.h:279
armarx::DMPComponent::getDMPFromFile
DMPInstanceBasePrx getDMPFromFile(const std::string &fileName, const std::string &dmpName="UNKNOWN", const Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:240
armarx::DMPComponent::getCanonicalValues
Ice::DoubleSeq getCanonicalValues(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:618
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
armarx::DMPComponent::setDMPState
void setDMPState(const std::string &dmpName, const ::armarx::cStateVec &state, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:450
armarx::DMPComponent::timestep
double timestep
Definition: DMPComponent.h:267
armarx::DMPComponent::getAmpl
double getAmpl(const std::string &dmpName, int dim, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:561
armarx::DMPComponent::onDisconnectComponent
void onDisconnectComponent() override
Definition: DMPComponent.cpp:79
armarx::DMPComponent::getDMPFromDatabase
DMPInstanceBasePrx getDMPFromDatabase(const std::string &dmpEntityName, const std::string &dmpName="UNKNOWN", const Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:166
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:429
armarx::DMPComponent::getDMPDim
int getDMPDim(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:699
armarx::DMPPair
std::pair< std::string, std::pair< DMPInstancePtr, DMPInstanceBasePrx > > DMPPair
Definition: DMPComponent.h:101
armarx::DMPComponent::onInitComponent
void onInitComponent() override
Definition: DMPComponent.cpp:42
armarx::DMPComponent::getDampingFactor
double getDampingFactor(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:665
armarx::DMPComponent::getCurrentState
cStateVec getCurrentState(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:600
armarx::DMPComponent::eraseDMP
void eraseDMP(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:878
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::DMPComponent::getStartPosition
Ice::DoubleSeq getStartPosition(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:734
armarx::DMPComponent::resetCanonicalValues
void resetCanonicalValues(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:807
MemoryXTypesObjectFactories.h
armarx::DMPComponent::removeDMPFromDatabase
void removeDMPFromDatabase(const std::string &name, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:365
armarx::DMPComponent::getTemporalFactor
double getTemporalFactor(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:637
armarx::DMPComponent::storeDMPInDatabase
void storeDMPInDatabase(const std::string &dmpName, const std::string &name, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:336
armarx::DMPComponent::getSpringFactor
double getSpringFactor(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:682
armarx::DMPComponent::getDMPNameList
SVector getDMPNameList(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:865
armarx::DMPComponent::getNextState
cStateVec getNextState(const std::string &dmpName, const cStateVec &states, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:578
armarx::DMPComponent::getTrajGoal
Ice::DoubleSeq getTrajGoal(const std::string &dmpName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:828
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:154
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::DMPComponent::resetTime
void resetTime(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:797
armarx::DMPComponent::getForceTerm
double getForceTerm(const std::string &dmpName, const Ice::DoubleSeq &canonicalValues, int dim, const Ice::Current &=Ice::emptyCurrent) override
Definition: DMPComponent.cpp:751