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