Segment.cpp
Go to the documentation of this file.
1 // BaseClass
2 #include "Segment.h"
3 
4 // ArmarX
7 
11 
12 #include "motionprimitives.h"
13 
14 // STD / STL
15 #include <fstream>
16 #include <iostream>
17 #include <sstream>
18 
20 {
22  Base(memoryToIceAdapter, "Trajectory", "MovementPrimitive")
23  {
24  }
25 
26  void
28  {
29  Base::defineProperties(defs, prefix);
30 
31  defs->optional(properties.motionsPackage,
32  prefix + "MotionsPackage",
33  "Name of the prior knowledge package to load from.");
34  defs->optional(properties.loadFromMotionsPackage,
35  prefix + "LoadFromMotionsPackage",
36  "If true, load the motions from the motions package on startup.");
37  }
38 
39  void
41  {
42  Base::init();
43 
44  if (properties.loadFromMotionsPackage)
45  {
46  loadByMotionFinder(properties.motionsPackage);
47  }
48  }
49 
50  void
52  {
53  }
54 
55  int
56  MPSegment::loadByMotionFinder(const std::string& packageName)
57  {
58  priorknowledge::motions::MotionFinder motionFinder(packageName, "motions/");
59  int loadedMotions = 0;
60 
61  {
62  auto allMotions = motionFinder.findAll("trajectories");
63  for (const auto& motionFinderInfo : allMotions)
64  {
65  auto pathToInfoJson =
66  motionFinderInfo.getFullPath() /
67  motionFinderInfo
68  .getID(); // / (motionFinderInfo.getID() + ".csv"); // todo: needs to be adapted, account for task and joint space
69  for (const auto& entry : std::filesystem::directory_iterator(pathToInfoJson))
70  {
71  if (std::string(entry.path().filename()).rfind("taskspace", 0) == 0)
72  {
73  //ARMARX_IMPORTANT << entry.path().filename();
74  loadSingleMotionFinder(entry.path(), motionFinderInfo.getID(), true);
75  loadedMotions += allMotions.size();
76  }
77  /*else if (std::string(entry.path().filename()).rfind("joint-trajectory", 0) == 0)
78  {
79  loadSingleMotionFinder(entry.path(), motionFinderInfo.getID(), false);
80  loadedMotions += allMotions.size();
81  }*/
82  }
83  }
84 
85  loadedMotions += allMotions.size();
86  }
87 
88  return loadedMotions;
89  }
90 
91  void
92  MPSegment::loadSingleMotionFinder(const std::string& pathToInfoJson,
93  const std::string& entityName,
94  bool taskspace)
95  {
96  if (auto op = util::createFromFile(pathToInfoJson, taskspace); op.has_value())
97  {
98  std::stringstream ss;
99  ss << "Found valid instance at: " << pathToInfoJson << ". The motionID is: ";
100 
101  armem::wm::EntityInstance instance;
102  instance.metadata().referencedTime = armem::Time::Now(); //op->createdDate;
103  instance.metadata().sentTime = armem::Time::Now();
104  instance.metadata().arrivedTime = armem::Time::Now();
105  instance.metadata().confidence = 1.0;
106 
107  if (taskspace)
108  {
109  std::filesystem::path path(pathToInfoJson);
110  for (const auto& entry : std::filesystem::directory_iterator(path.parent_path()))
111  {
112  std::string newname =
113  "joint-trajectory" + std::string(path.filename()).erase(0, 20);
114  if (std::string(entry.path().filename()).rfind(newname, 0) == 0)
115  {
116  if (auto op2 = util::createFromFile(entry.path(), false);
117  op.has_value()) // here now mps::createFromFile(pathToInfoJson)
118  {
119  op->jointSpace = op2->jointSpace;
120  instance.data() = op->toAron();
121  if (this->segmentPtr->hasEntity(entityName))
122  {
123  auto& entity = this->segmentPtr->getEntity(entityName);
124  auto& snapshot = entity.addSnapshot(armem::Time::Now());
125  snapshot.addInstance(instance);
126  }
127  else
128  {
129  auto& entity = this->segmentPtr->addEntity(entityName);
130  auto& snapshot = entity.addSnapshot(armem::Time::Now());
131  snapshot.addInstance(instance);
132  }
133  ARMARX_IMPORTANT << "Full content trajectory: " << op->name;
134  }
135  }
136  }
137  }
138  }
139  else
140  {
141  ARMARX_WARNING << "Found an invalid path to a motion file: " << pathToInfoJson;
142  }
143  }
144 } // namespace armarx::armem::server::motions::mps::segment
MotionFinder.h
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
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:51
armarx::armem::server::motions::mps::segment::MPSegment::init
virtual void init() override
Definition: Segment.cpp:40
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:53
armarx::armem::server::motions::mps::segment::MPSegment::MPSegment
MPSegment(armem::server::MemoryToIceAdapter &iceMemory)
Definition: Segment.cpp:21
armarx::armem::server::motions::mps::segment
Definition: motionprimitives.cpp:18
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:66
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:51
armarx::armem::base::EntityInstanceBase::metadata
MetadataT & metadata()
Definition: EntityInstanceBase.h:117
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
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:38