KnownGraspProviderSegment.cpp
Go to the documentation of this file.
2 #include <VirtualRobot/Grasping/GraspSet.h>
3 #include <VirtualRobot/XML/ObjectIO.h>
4 
7 
9 {
11  Base(iceMemory, PROVIDER_SEGMENT_NAME, CORE_SEGMENT_NAME)
12  {
13 
14  }
15 
17  {
18  Base::init();
19 
20  loadMemory();
21  }
22 
23  std::optional<arondto::KnownGraspInfo> KnownGraspProviderSegment::knownGraspInfoFromObjectInfo(const ObjectInfo& info)
24  {
25  std::string objectClassName = info.className();
26  auto fileLocInfo = info.file(".xml", "_Grasps");
27  std::filesystem::path graspFilePath = fileLocInfo.absolutePath;
28 
29  if (std::filesystem::is_regular_file(graspFilePath))
30  {
31  ARMARX_INFO << "loading " << graspFilePath;
32  try {
33  auto manipulationObject = VirtualRobot::ObjectIO::loadManipulationObject(graspFilePath);
34 
35  if(manipulationObject == nullptr)
36  {
37  ARMARX_WARNING << "Invalid file content: " << graspFilePath;
38  return std::nullopt;
39  }
40 
41  arondto::KnownGraspInfo ret;
42  ret.correspondingObject.memoryName = "Object";
43  ret.correspondingObject.coreSegmentName = "Class";
44  ret.correspondingObject.providerSegmentName = "PriorKnowledgeData";
45  ret.correspondingObject.entityName = info.idStr();
46  ret.xml.package = fileLocInfo.package;
47  ret.xml.path = fileLocInfo.relativePath;
48 
49  for (const VirtualRobot::GraspSetPtr& graspSet : manipulationObject->getAllGraspSets())
50  {
51  ARMARX_CHECK_NOT_NULL(graspSet);
52 
53  arondto::KnownGraspSet retGraspSet;
54 
55  retGraspSet.name = graspSet->getName();
56  retGraspSet.robot = graspSet->getRobotType();
57  retGraspSet.endeffector = graspSet->getEndEffector();
58 
59  for (const VirtualRobot::GraspPtr& grasp : graspSet->getGrasps())
60  {
61  ARMARX_CHECK_NOT_NULL(grasp);
62 
63  arondto::KnownGrasp retGrasp;
64 
65  retGrasp.name = grasp->getName();
66  retGrasp.quality = grasp->getQuality();
67  retGrasp.creator = grasp->getCreationMethod();
68  retGrasp.pose = grasp->getTransformation();
69 
70  ARMARX_VERBOSE << "Found grasp '" << retGrasp.name << "' in set '" << retGraspSet.name << "' for obj '" << objectClassName << "' with pose \n" << retGrasp.pose;
71 
72  retGraspSet.grasps.push_back(retGrasp);
73  }
74  ret.graspSets[retGraspSet.name] = retGraspSet;
75  }
76  return ret;
77 
78  } catch (...) {
79  ARMARX_WARNING << graspFilePath << " is not a manipulation object!";
80  return std::nullopt;
81  }
82  }
83  return std::nullopt;
84  }
85 
86  void KnownGraspProviderSegment::loadMemory()
87  {
88  // load data from prior knowledge
89  ObjectFinder objectFinder;
90  const auto now = armem::Time::Now();
91 
92  const bool checkPaths = false;
93  std::vector<ObjectInfo> infos = objectFinder.findAllObjects(checkPaths);
94 
95  const MemoryID providerID = segmentPtr->id().withProviderSegmentName(objectFinder.getPackageName());
96  ARMARX_INFO << "Checking up to " << infos.size() << " object classes from '" << objectFinder.getPackageName() << "' ...";
97 
98  Commit commit;
99  for (ObjectInfo& info : infos)
100  {
101  info.setLogError(false);
102  if (auto knownGraspCandidate = knownGraspInfoFromObjectInfo(info); knownGraspCandidate)
103  {
104  EntityUpdate& update = commit.add();
105  update.entityID = providerID.withEntityName(info.id().str());
106  update.entityID.timestamp = update.arrivedTime = update.referencedTime = update.sentTime = now;
107 
108  update.instancesData =
109  {
110  knownGraspCandidate->toAron()
111  };
112  }
113  }
114  ARMARX_INFO << "Loaded " << commit.updates.size() << " grasp candidates from object classes from '" << objectFinder.getPackageName() << "'.";
115  auto result = iceMemory.commit(commit);
116  if (!result.allSuccess())
117  {
118  ARMARX_WARNING << "Got errors for commit: " << result.allErrorMessages();
119  }
120  }
121 }
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
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_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::ObjectInfo::className
std::string className() const
Definition: ObjectInfo.cpp:47
KnownGraspProviderSegment.h
armarx::armem::grasping::segment::KnownGraspProviderSegment::KnownGraspProviderSegment
KnownGraspProviderSegment(armem::server::MemoryToIceAdapter &iceMemory)
Definition: KnownGraspProviderSegment.cpp:10
armarx::ObjectInfo::file
PackageFileLocation file(const std::string &extension, const std::string &suffix="", bool fixDataPath=false) const
Definition: ObjectInfo.cpp:72
armarx::armem::server::segment::detail::SegmentBase< server::wm::ProviderSegment >::segmentPtr
server::wm::ProviderSegment * segmentPtr
Definition: SpecializedSegment.h:54
armarx::armem::MemoryID::withProviderSegmentName
MemoryID withProviderSegmentName(const std::string &name) const
Definition: MemoryID.cpp:412
armarx::armem::server::segment::detail::SegmentBase< server::wm::ProviderSegment >
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:27
armarx::armem::server::segment::SpecializedProviderSegment::init
virtual void init() override
Definition: SpecializedProviderSegment.cpp:56
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
armarx::ObjectInfo::idStr
std::string idStr() const
Definition: ObjectInfo.cpp:57
MemoryToIceAdapter.h
armarx::armem::grasping::segment::KnownGraspProviderSegment::init
void init() override
Definition: KnownGraspProviderSegment.cpp:16
armarx::armem::grasping::segment
Definition: KnownGraspProviderSegment.cpp:8
armarx::armem::server::segment::detail::SegmentBase< server::wm::ProviderSegment >::iceMemory
MemoryToIceAdapter & iceMemory
Definition: SpecializedSegment.h:60
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::PackageFileLocation::absolutePath
std::filesystem::path absolutePath
The absolute path (in the host's file system).
Definition: ObjectInfo.h:30
armarx::ObjectInfo::id
ObjectID id() const
Return "dataset/name".
Definition: ObjectInfo.cpp:52
RapidXmlReader.h
armarx::ObjectInfo::setLogError
void setLogError(bool enabled)
Definition: ObjectInfo.cpp:32
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::armem::server::MemoryToIceAdapter::commit
data::CommitResult commit(const data::Commit &commitIce, Time timeArrived)
Definition: MemoryToIceAdapter.cpp:112
armarx::ObjectID::str
std::string str() const
Return "dataset/className" or "dataset/className/instanceName".
Definition: ObjectID.cpp:55
armarx::ObjectInfo
Accessor for the object files.
Definition: ObjectInfo.h:37
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:29