ObjectInstanceToRobotNodeAttachments.cpp
Go to the documentation of this file.
2
5
8
10
11namespace memoryx
12{
13
17
18 void
20 const WorkingMemoryInterfacePrx& workingMemory,
21 const armarx::RobotStateComponentInterfacePrx& robotStateComponent)
22 {
23 ARMARX_CHECK_NOT_NULL(workingMemory);
24 ARMARX_CHECK_NOT_NULL(robotStateComponent);
25
26 this->robotStateComponent = robotStateComponent;
28 robotStateComponent, "", {}, VirtualRobot::RobotIO::RobotDescription::eStructure);
29 this->objectInstanceSegment = workingMemory->getObjectInstancesSegment();
30 }
31
32 void
34 const AttachObjectToRobotNodeInput& input)
35 {
36 ObjectInstancePtr instance = getMatchingObjectInstance(input.object);
37 if (!instance)
38 {
39 ARMARX_WARNING << "No object instance with ID '" << input.object.memoryID
40 << "' or class '" << input.object.className << "' found.";
41 return;
42 }
43
44 ARMARX_VERBOSE << "Attaching object '" << instance->getId() << "' (class '"
45 << instance->getMostProbableClass() << "')"
46 << " to robot node '" << input.robotNodeName << "'.";
47
48 Attachment attachment;
49 attachment.robotNode = input.robotNodeName;
50 if (input.useCurrentPose)
51 {
52 // Get pose in robot node frame.
53 attachment.initialPose = getObjectPoseInFrame(instance, attachment.robotNode);
54 }
55 if (input.objectOffset)
56 {
57 attachment.objectOffset = armarx::PosePtr::dynamicCast(input.objectOffset)->toEigen();
58 }
59
60 this->attachments[getMatchingObjectID(input.object)] = attachment;
61 }
62
63 void
65 const DetachObjectFromRobotNodeInput& detachment)
66 {
67 std::string id = getMatchingObjectID(detachment.object);
68 if (id.empty())
69 {
70 ARMARX_WARNING << "No object instance with ID '" << detachment.object.memoryID
71 << "' or class '" << detachment.object.className << "' found.";
72 return;
73 }
74
75 if (auto it = attachments.find(id); it != attachments.end())
76 {
77 ARMARX_VERBOSE << "Detaching object '" << id << "' from robot node '"
78 << it->second.robotNode << "'.";
79 attachments.erase(it);
80 }
81 else
82 {
83 ARMARX_VERBOSE << "No attachment of object '" << id << "' found.";
84 }
85 }
86
87 std::vector<ObjectInstancePtr>
89 {
90 EntityBaseList entities = objectInstanceSegment->getAllEntities();
91
92 std::vector<ObjectInstancePtr> instances;
93 for (const auto& entity : entities)
94 {
95 ObjectInstancePtr object = ObjectInstancePtr::dynamicCast(entity);
96 if (object)
97 {
98 instances.push_back(object);
99 }
100 }
101
102 return instances;
103 }
104
105 Eigen::Matrix4f
107 const std::string& frame)
108 {
109 if (auto it = attachments.find(object->getId()); it != attachments.end())
110 {
111 return getAttachedObjectPoseInFrame(it->second, frame);
112 }
113 else
114 {
115 return getDetachedObjectPoseInFrame(object, armarx::GlobalFrame);
116 }
117 }
118
119 Eigen::Matrix4f
120 ObjectInstanceToRobotNodeAttachments::getAttachedObjectPoseInFrame(const Attachment& attachment,
121 const std::string& frame)
122 {
123 armarx::RemoteRobot::synchronizeLocalClone(robot, robotStateComponent);
124
125 armarx::FramedPose framed(attachment.initialPose * attachment.objectOffset,
126 attachment.robotNode,
127 robot->getName());
128
129 if (frame == armarx::GlobalFrame)
130 {
131 return framed.toGlobalEigen(robot);
132 }
133 else
134 {
135 framed.changeFrame(robot, frame);
136 return framed.toEigen();
137 }
138 }
139
140 Eigen::Matrix4f
141 ObjectInstanceToRobotNodeAttachments::getDetachedObjectPoseInFrame(ObjectInstancePtr object,
142 const std::string& frame)
143 {
144 if (object->getPose()->getFrame() == frame)
145 {
146 return object->getPose()->toEigen();
147 }
148
149 // Transform by syncing robot.
150 auto it = objectPoseCache.find(object->getId());
151 if (it != objectPoseCache.end() && object->hasLocalizationTimestamp())
152 {
153 const IceUtil::Time localizationTimestamp = object->getLocalizationTimestamp();
154 const IceUtil::Time previousTimestamp = it->second.first;
155 if (previousTimestamp >= localizationTimestamp)
156 {
157 // Stored pose is up to date.
158 armarx::FramedPose& pose = it->second.second;
159 // Transform in-place if necessary.
160 if (pose.getFrame() != frame)
161 {
162 armarx::RemoteRobot::synchronizeLocalClone(robot, robotStateComponent);
163 pose.changeFrame(robot, frame);
164 }
165 return pose.toEigen();
166 }
167 }
168
169 armarx::RemoteRobot::synchronizeLocalClone(robot, robotStateComponent);
170 const IceUtil::Time ts = object->hasLocalizationTimestamp()
171 ? object->getLocalizationTimestamp()
173
174 armarx::FramedPose pose = *object->getPose();
175 if (pose.getFrame() != frame)
176 {
177 armarx::RemoteRobot::synchronizeLocalClone(robot, robotStateComponent);
178 pose.changeFrame(robot, frame);
179 }
180 // Store it in the cache so we don't recompute the framed pose on the next (spurious) update.
181 objectPoseCache[object->getId()] = std::make_pair(ts, pose);
182 return pose.toEigen();
183 }
184
185 bool
187 {
188 return attachments.count(id) > 0;
189 }
190
191 std::string
193 {
194 if (!object.memoryID.empty())
195 {
196 return object.memoryID;
197 }
198 else if (auto instance = getMatchingObjectInstance(object))
199 {
200 return instance->getId();
201 }
202 else
203 {
204 return "";
205 }
206 }
207
210 {
211 if (!object.memoryID.empty())
212 {
213 // Look for instance with ID.
214 for (ObjectInstancePtr instance : queryObjects())
215 {
216 if (instance && instance->getId() == object.memoryID)
217 {
218 return instance;
219 }
220 }
221 return nullptr;
222 }
223 else
224 {
225 // Look for instance with class name.
226 for (ObjectInstancePtr instance : queryObjects())
227 {
228 if (instance && instance->getMostProbableClass() == object.className)
229 {
230 return instance;
231 }
232 }
233 return nullptr;
234 }
235 }
236
237} // namespace memoryx
The FramedPose class.
Definition FramedPose.h:281
std::string getFrame() const
void changeFrame(const SharedRobotInterfacePrx &referenceRobot, const std::string &newFrame)
virtual Eigen::Matrix4f toEigen() const
Definition Pose.cpp:334
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition TimeUtil.cpp:42
std::string getMatchingObjectID(const memoryx::ObjectIdOrClass &object)
void attachObjectToRobotNode(const memoryx::AttachObjectToRobotNodeInput &attachment)
void detachObjectFromRobotNode(const memoryx::DetachObjectFromRobotNodeInput &detachment)
std::vector< ObjectInstancePtr > queryObjects()
Get all entities from objectInstanceSegment and cast them to ObjectInstance.
ObjectInstancePtr getMatchingObjectInstance(const memoryx::ObjectIdOrClass &object)
void initFromProxies(const WorkingMemoryInterfacePrx &workingMemory, const armarx::RobotStateComponentInterfacePrx &robotStateComponent)
Set the proxies.
Eigen::Matrix4f getObjectPoseInFrame(ObjectInstancePtr object, const std::string &frame)
#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...
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
std::string const GlobalFrame
Variable of the global coordinate system.
Definition FramedPose.h:65
::IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface > RobotStateComponentInterfacePrx
VirtualRobot headers.
IceInternal::Handle< ObjectInstance > ObjectInstancePtr