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
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
102 instance.metadata().referencedTime = armem::Time::Now(); //op->createdDate;
103 instance.metadata().sentTime = 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
EntitySnapshotT & addSnapshot(const Time &timestamp)
Add a snapshot at the given time.
Definition EntityBase.h:673
bool hasEntity(const std::string &name) const
EntityT & getEntity(const std::string &name)
Helps connecting a Memory server to the Ice interface.
virtual void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="") override
Definition Segment.cpp:27
MPSegment(armem::server::MemoryToIceAdapter &iceMemory)
Definition Segment.cpp:21
virtual void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="") override
Entity & addEntity(const std::string &name, Args... args)
Client-side working entity instance.
static DateTime Now()
Definition DateTime.cpp:51
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
std::optional< armarx::arondto::Trajectory > createFromFile(const std::filesystem::__cxx11::path &pathToInfoJson, bool taskspace)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
float confidence
An optional confidence, may be used for things like decay.
Time arrivedTime
Time when this value has arrived at the memory.
Time referencedTime
Time this instance refers to.
Time sentTime
Time when this value was sent to the memory.