ObjectReader.cpp
Go to the documentation of this file.
1#include "ObjectReader.h"
2
3#include <map>
4#include <optional>
5#include <string>
6#include <vector>
7
11
16#include <RobotAPI/interface/ArmarXObjects/ArmarXObjectsTypes.h>
17#include <RobotAPI/interface/objectpose/ObjectPoseStorageInterface.h>
19
20// #include <RobotAPI/libraries/armem_robot_state/aron/Robot.aron.generated.h>
21// #include <RobotAPI/libraries/armem_robot_state/aron_conversions.h>
22// #include <RobotAPI/libraries/armem_robot_state/robot_conversions.h>
23
25{
26 void
28 {
29 const std::string prefix = propertyPrefix;
30
31 def->optional(properties.memoryName, prefix + "MemoryName");
32 }
33
34 void
36 {
37 // Wait for the memory to become available and add it as dependency.
38 ARMARX_IMPORTANT << "Waiting for memory '" << properties.memoryName << "' ...";
39 try
40 {
41 // simply wait until memory is ready. Do nothing with reader but get prx
42 auto r = memoryNameSystem.useReader(properties.memoryName);
43
44 // cast MemoryPrx to objPoseStoragePrx --> NOT WORKING DUE TO SOME ICE CACHING OR SO.
45 // Explanation:
46 // When the readingPrx disconnects and reconnects, the cast fails as it uses internally
47 // a ice_isA call which results in connection refused. The reason is, that for some
48 // reason the mns thinks that the reconnected prx is similar to the old info
49 // (check ice_identity) so it returnes the cached prx in its server map. Could be fixed
50 // by always querying the mns component for new proxies, but this may slow the system
51 // down.
52 //this->objPoseStorage =
53 // objpose::ObjectPoseStorageInterfacePrx::uncheckedCast(r.readingPrx);
54 // Current fix: Get prx from mns:
55 this->readingPrx = r.readingPrx;
56
57 ARMARX_INFO << "Connected to Memory '" << properties.memoryName << "'";
58 }
60 {
61 ARMARX_ERROR << e.what();
62 return;
63 }
64 }
65
66 std::map<std::string, bool>
69 {
70 std::map<std::string, bool> ret;
71 auto providers = getObjectPoseStorage()->getAvailableProvidersInfo();
72 for (const auto& [k, p] : providers)
73 {
74 // TODO: check supported objects?
75 ret[k] = this->requestLocalization(instanceId, k, until);
76 }
77 return ret;
78 }
79
80 bool
82 const std::string& provider,
84 {
85
86 armarx::data::ObjectID requestObject;
87 armarx::toIce(requestObject, instanceId);
88
89 armarx::objpose::observer::RequestObjectsInput req;
90 req.provider = provider;
91 req.request.objectIDs = {requestObject};
92 req.request.relativeTimeoutMS = until.toMilliSeconds();
93
94 auto requestResult = getObjectPoseStorage()->requestObjects(req);
95
96 if (requestResult.results.count(requestObject))
97 {
98 return requestResult.results.at(requestObject).result.success;
99 }
100 return false;
101 }
102
103 std::optional<objpose::ObjectPose>
105 {
106 // TODO: Shall we throw an exception if no instance index is set?
107
108 auto objectPoses = getObjectPoseStorage()->getObjectPoses();
109 for (const auto& pose : objectPoses)
110 {
111 ObjectID oid;
112 fromIce(pose.objectID, oid);
113
114 // fully specified with instance name?
115 if (not instanceId.instanceName().empty())
116 {
117 if (oid == instanceId)
118 {
120 fromIce(pose, oi);
121 return oi;
122 }
123 }
124 else // only class name provided => take the first matching instance
125 {
126 if (oid.equalClass(instanceId))
127 {
129 fromIce(pose, oi);
130 return oi;
131 }
132 }
133 }
134 return std::nullopt;
135 }
136
137 std::map<std::string, objpose::ObjectPose>
139 {
140 std::map<std::string, objpose::ObjectPose> ret;
141 auto objectPoses = getObjectPoseStorage()->getObjectPoses();
142 for (const auto& pose : objectPoses)
143 {
144 ObjectID oid;
145 fromIce(pose.objectID, oid);
146 fromIce(pose, ret[oid.str()]);
147 }
148 return ret;
149 }
150
151 std::map<std::string, objpose::ObjectPose>
153 {
154 std::map<std::string, objpose::ObjectPose> ret;
155 auto objectPoses = getObjectPoseStorage()->getObjectPoses();
156 for (const auto& pose : objectPoses)
157 {
158 ObjectID oid;
159 fromIce(pose.objectID, oid);
160
161 if (oid.equalClass(classId))
162 {
163 fromIce(pose, ret[oid.str()]);
164 }
165 }
166 return ret;
167 }
168
169 std::map<std::string, std::vector<objpose::ObjectPose>>
171 {
172
173 std::map<std::string, std::vector<objpose::ObjectPose>> ret;
174
175 // Not working: does not return all provider segment names
176 // const auto providers = getObjectPoseStorage()->getAvailableProviderNames();
177 // ARMARX_VERBOSE << "Found providers: " << providers;
178
179
180 for (const auto& providerName : {"--placeholder--"})
181 {
182 // const auto objects = getObjectPoseStorage()->getObjectPosesByProvider(providerName);
183
184 const auto objects = getObjectPoseStorage()->getObjectPoses();
185
186 // filter attached objects
187 const auto isAttached = [](const objpose::ObjectPose& objectPose) noexcept -> bool
188 { return objectPose.attachment.has_value(); };
189
190 std::vector<objpose::ObjectPose> attachedObjects = {};
191 for (const auto& obj : objects)
192 {
193 ARMARX_VERBOSE << obj.objectID;
194 if (isAttached(obj))
195 {
196 attachedObjects.emplace_back(obj);
197 }
198 }
199
200
201 if (not attachedObjects.empty())
202 {
203 ARMARX_VERBOSE << "Provider " << QUOTED(providerName) << " has "
204 << attachedObjects.size() << " attached objects";
205 ret[providerName] = attachedObjects;
206 }
207 }
208
209 return ret;
210 }
211
212
213} // namespace armarx::armem::obj::instance
#define QUOTED(x)
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition ObjectID.h:11
bool equalClass(const ObjectID &rhs) const
Indicates whether dataset and class name are equal.
Definition ObjectID.cpp:77
std::string instanceName() const
Definition ObjectID.h:36
std::string str() const
Return "dataset/className" or "dataset/className/instanceName".
Definition ObjectID.cpp:60
The memory name system (MNS) client.
Reader useReader(const MemoryID &memoryID)
Use a memory server and get a reader for it.
server::ReadingMemoryInterfacePrx readingPrx
Definition Reader.h:227
Indicates that a query to the Memory Name System failed.
Definition mns.h:25
std::optional< objpose::ObjectPose > queryLatestObjectInstance(const ObjectID &instanceId)
std::map< std::string, objpose::ObjectPose > queryLatestObjectInstances()
void connect(armem::client::MemoryNameSystem &memoryNameSystem)
objpose::ObjectPoseStorageInterfacePrx getObjectPoseStorage() const
std::map< std::string, std::vector< objpose::ObjectPose > > queryAttachedObjectInstancesForProviders()
std::map< std::string, bool > requestLocalization(const ObjectID &instanceId, const armarx::core::time::Duration &until)
void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr &def)
Represents a duration.
Definition Duration.h:17
std::int64_t toMilliSeconds() const
Returns the amount of milliseconds.
Definition Duration.cpp:60
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
void fromIce(const data::MemoryID &ice, MemoryID &id)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
void toIce(std::map< IceKeyT, IceValueT > &iceMap, const boost::container::flat_map< CppKeyT, CppValueT > &cppMap)
An object pose as stored by the ObjectPoseStorage.
Definition ObjectPose.h:34