Segment.cpp
Go to the documentation of this file.
1 // BaseClass
2 #include "Segment.h"
3 
4 // ArmarX
5 #include "motionprimitives.h"
6 
10 
13 
14 // STD / STL
15 #include <iostream>
16 #include <fstream>
17 #include <sstream>
18 
19 
21 {
23  Base(memoryToIceAdapter, "Trajectory", "MovementPrimitive")
24  {
25  }
26 
27  void MPSegment::defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string& prefix)
28  {
29  Base::defineProperties(defs, prefix);
30 
31  defs->optional(properties.motionsPackage, prefix + "MotionsPackage", "Name of the prior knowledge package to load from.");
32  defs->optional(properties.loadFromMotionsPackage, prefix + "LoadFromMotionsPackage", "If true, load the motions from the motions package on startup.");
33  }
34 
36  {
37  Base::init();
38 
39  if (properties.loadFromMotionsPackage)
40  {
41  loadByMotionFinder(properties.motionsPackage);
42  }
43  }
44 
46  {
47 
48  }
49 
50  int MPSegment::loadByMotionFinder(const std::string& packageName)
51  {
52  priorknowledge::motions::MotionFinder motionFinder(packageName, "motions/");
53  int loadedMotions = 0;
54 
55  {
56  auto allMotions = motionFinder.findAll("trajectories");
57  for (const auto& motionFinderInfo : allMotions)
58  {
59  auto pathToInfoJson = motionFinderInfo.getFullPath() / motionFinderInfo.getID();// / (motionFinderInfo.getID() + ".csv"); // todo: needs to be adapted, account for task and joint space
60  for (const auto & entry: std::filesystem::directory_iterator(pathToInfoJson))
61  {
62  if (std::string(entry.path().filename()).rfind("taskspace", 0) == 0)
63  {
64  //ARMARX_IMPORTANT << entry.path().filename();
65  loadSingleMotionFinder(entry.path(), motionFinderInfo.getID(), true);
66  loadedMotions += allMotions.size();
67  }
68  /*else if (std::string(entry.path().filename()).rfind("joint-trajectory", 0) == 0)
69  {
70  loadSingleMotionFinder(entry.path(), motionFinderInfo.getID(), false);
71  loadedMotions += allMotions.size();
72  }*/
73  }
74  }
75 
76  loadedMotions += allMotions.size();
77  }
78 
79  return loadedMotions;
80  }
81 
82  void MPSegment::loadSingleMotionFinder(const std::string &pathToInfoJson, const std::string &entityName, bool taskspace)
83  {
84  if (auto op = util::createFromFile(pathToInfoJson, taskspace); op.has_value())
85  {
86  std::stringstream ss;
87  ss << "Found valid instance at: " << pathToInfoJson << ". The motionID is: ";
88 
90  instance.metadata().referencedTime = armem::Time::Now(); //op->createdDate;
91  instance.metadata().sentTime = armem::Time::Now();
92  instance.metadata().arrivedTime = armem::Time::Now();
93  instance.metadata().confidence = 1.0;
94 
95  if(taskspace)
96  {
97  std::filesystem::path path(pathToInfoJson);
98  for (const auto & entry: std::filesystem::directory_iterator(path.parent_path()))
99  {
100  std::string newname = "joint-trajectory" + std::string(path.filename()).erase(0, 20);
101  if (std::string(entry.path().filename()).rfind(newname, 0) == 0)
102  {
103  if (auto op2 = util::createFromFile(entry.path(), false); op.has_value()) // here now mps::createFromFile(pathToInfoJson)
104  {
105  op->jointSpace = op2->jointSpace;
106  instance.data() = op->toAron();
107  if (this->segmentPtr->hasEntity(entityName))
108  {
109  auto& entity = this->segmentPtr->getEntity(entityName);
110  auto& snapshot = entity.addSnapshot(armem::Time::Now());
111  snapshot.addInstance(instance);
112  }
113  else
114  {
115  auto& entity = this->segmentPtr->addEntity(entityName);
116  auto& snapshot = entity.addSnapshot(armem::Time::Now());
117  snapshot.addInstance(instance);
118  }
119  ARMARX_IMPORTANT << "Full content trajectory: " << op->name;
120  }
121  }
122  }
123  }
124  }
125  else
126  {
127  ARMARX_WARNING << "Found an invalid path to a motion file: " << pathToInfoJson;
128  }
129  }
130 }
MotionFinder.h
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
armarx::armem::server::MemoryToIceAdapter
Helps connecting a Memory server to the Ice interface.
Definition: MemoryToIceAdapter.h:19
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::armem::server::motions::mps::segment::MPSegment::init
virtual void init() override
Definition: Segment.cpp:35
Segment.h
armarx::armem::base::EntityBase::addSnapshot
EntitySnapshotT & addSnapshot(const Time &timestamp)
Add a snapshot at the given time.
Definition: EntityBase.h:629
memory_definitions.h
armarx::armem::server::segment::detail::SegmentBase< server::wm::ProviderSegment >::segmentPtr
server::wm::ProviderSegment * segmentPtr
Definition: SpecializedSegment.h:54
armarx::armem::server::motions::mps::segment::MPSegment::MPSegment
MPSegment(armem::server::MemoryToIceAdapter &iceMemory)
Definition: Segment.cpp:22
armarx::armem::server::motions::mps::segment
Definition: motionprimitives.cpp:19
armarx::armem::base::EntityInstanceMetadata::sentTime
Time sentTime
Time when this value was sent to the memory.
Definition: EntityInstanceBase.h:49
armarx::armem::base::ProviderSegmentBase::hasEntity
bool hasEntity(const std::string &name) const
Definition: ProviderSegmentBase.h:108
armarx::armem::server::segment::detail::SegmentBase< server::wm::ProviderSegment >
armarx::armem::server::wm::ProviderSegment::addEntity
Entity & addEntity(const std::string &name, Args... args)
Definition: memory_definitions.h:74
motionprimitives.h
armarx::priorknowledge::motions::MotionFinder
Definition: MotionFinder.h:7
armarx::armem::base::EntityInstanceMetadata::arrivedTime
Time arrivedTime
Time when this value has arrived at the memory.
Definition: EntityInstanceBase.h:51
armarx::armem::server::segment::SpecializedProviderSegment::init
virtual void init() override
Definition: SpecializedProviderSegment.cpp:56
memory_definitions.h
armarx::armem::base::EntityInstanceMetadata::referencedTime
Time referencedTime
Time this instance refers to.
Definition: EntityInstanceBase.h:46
armarx::armem::server::motions::mps::segment::util::createFromFile
std::optional< armarx::arondto::Trajectory > createFromFile(const std::filesystem::__cxx11::path &pathToInfoJson, bool taskspace)
Definition: motionprimitives.cpp:22
PropertyDefinitionContainer.h
IceUtil::Handle< class PropertyDefinitionContainer >
ProxyPropertyDefinition.h
armarx::armem::server::motions::mps::segment::MPSegment::defineProperties
virtual void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="") override
Definition: Segment.cpp:27
armarx::armem::base::EntityInstanceMetadata::confidence
float confidence
An optional confidence, may be used for things like decay.
Definition: EntityInstanceBase.h:54
armarx::armem::server::motions::mps::segment::MPSegment::onConnect
virtual void onConnect()
Definition: Segment.cpp:45
armarx::armem::base::EntityInstanceBase::metadata
MetadataT & metadata()
Definition: EntityInstanceBase.h:117
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::armem::base::ProviderSegmentBase::getEntity
EntityT & getEntity(const std::string &name)
Definition: ProviderSegmentBase.h:135
armarx::armem::base::EntityInstanceBase::data
const DataT & data() const
Definition: EntityInstanceBase.h:129
armarx::armem::server::segment::SpecializedProviderSegment::defineProperties
virtual void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="") override
Definition: SpecializedProviderSegment.cpp:40