ObjectInstanceToRobotNodeAttachments.cpp
Go to the documentation of this file.
2 
5 
8 
10 
11 namespace memoryx
12 {
13 
15  {
16  }
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  {
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 
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 
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 
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
RemoteRobot.h
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
memoryx::ObjectInstanceToRobotNodeAttachments::getMatchingObjectID
std::string getMatchingObjectID(const memoryx::ObjectIdOrClass &object)
Definition: ObjectInstanceToRobotNodeAttachments.cpp:192
memoryx::ObjectInstanceToRobotNodeAttachments::attachObjectToRobotNode
void attachObjectToRobotNode(const memoryx::AttachObjectToRobotNodeInput &attachment)
Definition: ObjectInstanceToRobotNodeAttachments.cpp:33
armarx::RemoteRobot::synchronizeLocalClone
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
Definition: RemoteRobot.cpp:522
armarx::FramedPose
The FramedPose class.
Definition: FramedPose.h:280
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
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:65
memoryx::ObjectInstanceToRobotNodeAttachments::isObjectAttached
bool isObjectAttached(const std::string &id) const
Definition: ObjectInstanceToRobotNodeAttachments.cpp:186
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
ObjectInstanceToRobotNodeAttachments.h
memoryx::ObjectInstanceToRobotNodeAttachments::getMatchingObjectInstance
ObjectInstancePtr getMatchingObjectInstance(const memoryx::ObjectIdOrClass &object)
Definition: ObjectInstanceToRobotNodeAttachments.cpp:209
memoryx::ObjectInstanceToRobotNodeAttachments::detachObjectFromRobotNode
void detachObjectFromRobotNode(const memoryx::DetachObjectFromRobotNodeInput &detachment)
Definition: ObjectInstanceToRobotNodeAttachments.cpp:64
cxxopts::empty
bool empty(const std::string &s)
Definition: cxxopts.hpp:234
IceInternal::Handle< ObjectInstance >
memoryx::ObjectInstanceToRobotNodeAttachments::ObjectInstanceToRobotNodeAttachments
ObjectInstanceToRobotNodeAttachments()
Definition: ObjectInstanceToRobotNodeAttachments.cpp:14
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:12
armarx::Pose::toEigen
virtual Eigen::Matrix4f toEigen() const
Definition: Pose.cpp:334
memoryx::ObjectInstancePtr
IceInternal::Handle< ObjectInstance > ObjectInstancePtr
Definition: ObjectInstance.h:43
memoryx::ObjectInstanceToRobotNodeAttachments::initFromProxies
void initFromProxies(const WorkingMemoryInterfacePrx &workingMemory, const armarx::RobotStateComponentInterfacePrx &robotStateComponent)
Set the proxies.
Definition: ObjectInstanceToRobotNodeAttachments.cpp:19
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
TimeUtil.h
armarx::RemoteRobot::createLocalClone
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
Definition: RemoteRobot.cpp:381
armarx::armem::index::memoryID
const MemoryID memoryID
Definition: memory_ids.cpp:28
IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface >
memoryx::ObjectInstanceToRobotNodeAttachments::getObjectPoseInFrame
Eigen::Matrix4f getObjectPoseInFrame(ObjectInstancePtr object, const std::string &frame)
Definition: ObjectInstanceToRobotNodeAttachments.cpp:106
armarx::FramedPose::getFrame
std::string getFrame() const
Definition: FramedPose.cpp:453
memoryx::ObjectInstanceToRobotNodeAttachments::queryObjects
std::vector< ObjectInstancePtr > queryObjects()
Get all entities from objectInstanceSegment and cast them to ObjectInstance.
Definition: ObjectInstanceToRobotNodeAttachments.cpp:88
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::FramedPose::changeFrame
void changeFrame(const SharedRobotInterfacePrx &referenceRobot, const std::string &newFrame)
Definition: FramedPose.cpp:469